Leviathan  0.8.0.0
Leviathan game engine
FileSystem.h
Go to the documentation of this file.
1 // Leviathan Game Engine
2 // Copyright (c) 2012-2019 Henri Hyyryläinen
3 #pragma once
4 #include "Define.h"
5 // ------------------------------------ //
7 #include "Common/ThreadSafe.h"
8 
9 #include "ErrorReporter.h"
10 #include <regex>
11 
12 
13 namespace Leviathan {
14 
15 enum FILEGROUP {
21 };
22 
25 public:
26  CharWithIndex();
27  CharWithIndex(char character, size_t index);
28 
29  char Char;
30  size_t Index;
31 };
32 
35 public:
36  FileTypeHolder(int id, const std::string& name);
37 
38  int ID;
39  std::string Name;
40 };
41 
42 
44  // just the path, everything else is worked out by the constructor //
45  FileDefinitionType(FileSystem* instance, const std::string& path);
47 
49  std::string GetNameWithExtension() const;
50 
51  // operation for sorting //
52  bool operator<(const FileDefinitionType& other) const;
53 
54  std::string RelativePath;
55  std::string Name;
57 };
58 
59 struct FileDefSorter {
60  DLLEXPORT bool operator()(const std::shared_ptr<FileDefinitionType>& first,
61  const std::shared_ptr<FileDefinitionType>& second);
62 };
63 
65 class FileSystem {
66 public:
69 
71  DLLEXPORT bool Init(LErrorReporter* errorreport);
72 
74  DLLEXPORT bool ReSearchFiles();
75 
77  DLLEXPORT void CreateIndexesForVecs(bool forcerecreation = false);
78 
80  DLLEXPORT int RegisterExtension(const std::string& extension);
81 
84  DLLEXPORT void GetExtensionIDS(const std::string& extensions, std::vector<int>& ids);
85 
88  DLLEXPORT const std::string& GetExtensionName(int id) const;
89 
90 
93  DLLEXPORT std::string SearchForFile(FILEGROUP which, const std::string& name,
94  const std::string& extensions, bool searchall = true);
95 
97  DLLEXPORT std::vector<std::shared_ptr<FileDefinitionType>> FindAllMatchingFiles(
98  FILEGROUP which, const std::string& regexname, const std::string& extensions,
99  bool searchall = true);
100 
101  // direct access to files //
102  DLLEXPORT std::vector<std::shared_ptr<FileDefinitionType>>& GetModelFiles();
103  DLLEXPORT std::vector<std::shared_ptr<FileDefinitionType>>& GetSoundFiles();
104  DLLEXPORT std::vector<std::shared_ptr<FileDefinitionType>>& GetAllFiles();
105  DLLEXPORT std::vector<std::shared_ptr<FileDefinitionType>>& GetScriptFiles();
106  // ------------------ Static part ------------------ //
107 
108  DLLEXPORT static std::string GetDataFolder();
109  DLLEXPORT static std::string GetModelsFolder();
110  DLLEXPORT static std::string GetScriptsFolder();
111  DLLEXPORT static std::string GetShaderFolder();
112  DLLEXPORT static std::string GetTextureFolder();
113  DLLEXPORT static std::string GetFontFolder();
114  DLLEXPORT static std::string GetSoundFolder();
115 
116  DLLEXPORT static bool DoesExtensionMatch(
117  FileDefinitionType* file, const std::vector<int>& Ids);
118 
119 #ifdef _WIN32
120  DLLEXPORT static void GetWindowsFolder(std::wstring& path);
121  DLLEXPORT static void GetSpecialFolder(std::wstring& path, int specialtype);
122 #endif
123 
124  DLLEXPORT static void SetDataFolder(const std::string& folder);
125  DLLEXPORT static void SetModelsFolder(const std::string& folder);
126  DLLEXPORT static void SetScriptsFolder(const std::string& folder);
127  DLLEXPORT static void SetShaderFolder(const std::string& folder);
128  DLLEXPORT static void SetTextureFolder(const std::string& folder);
129 
130  // file handling //
131  DLLEXPORT static bool LoadDataDump(const std::string& file,
132  std::vector<std::shared_ptr<NamedVariableList>>& vec, LErrorReporter* errorreport);
133 
134  // Warning: \todo linux version ignores the defined pattern //
135  DLLEXPORT static bool GetFilesInDirectory(std::vector<std::string>& files,
136  const std::string& dirpath, const std::string& pattern = "*.*", bool recursive = true);
137 
138  // file operations //
139  DLLEXPORT static size_t GetFileLength(const std::string& name);
140  DLLEXPORT static bool FileExists(const std::string& name);
141  DLLEXPORT static bool WriteToFile(const std::string& data, const std::string& filename);
142  DLLEXPORT static bool WriteToFile(const std::wstring& data, const std::wstring& filename);
143  DLLEXPORT static bool AppendToFile(const std::string& data, const std::string& filepath);
144 
145  DLLEXPORT static bool ReadFileEntirely(
146  const std::string& file, std::string& resultreceiver);
147 
148  DLLEXPORT static bool ReadFileEntirely(
149  const std::wstring& file, std::wstring& resultreceiver);
150 
151 
152  DLLEXPORT static FileSystem* Get();
153 
154 private:
155  // file search functions //
156  std::shared_ptr<FileDefinitionType> _SearchForFileInVec(
157  std::vector<std::shared_ptr<FileDefinitionType>>& vec, std::vector<int>& extensions,
158  const std::string& name, bool UseIndexVector, std::vector<CharWithIndex*>* Index);
159 
160  void _SearchForFilesInVec(std::vector<std::shared_ptr<FileDefinitionType>>& vec,
161  std::vector<std::shared_ptr<FileDefinitionType>>& results,
162  std::vector<int>& extensions, const std::regex& regex);
163 
164  void _CreateIndexesIfMissing(std::vector<std::shared_ptr<FileDefinitionType>>& vec,
165  std::vector<CharWithIndex*>& resultvec, bool& indexed, const bool& force /*= false*/);
166 
167  // ------------------------------------ //
168  // vector that holds string value of file extension and it's id code //
169  std::vector<FileTypeHolder*> FileTypes;
170  int CurrentFileExtID;
171 
172  // file holders //
173  std::vector<std::shared_ptr<FileDefinitionType>> AllFiles;
174 
175  std::vector<std::shared_ptr<FileDefinitionType>> TextureFiles;
176  std::vector<std::shared_ptr<FileDefinitionType>> ModelFiles;
177  std::vector<std::shared_ptr<FileDefinitionType>> SoundFiles;
178  std::vector<std::shared_ptr<FileDefinitionType>> ScriptFiles;
179 
180  // index vectors //
181  bool IsAllIndexed;
182  std::vector<CharWithIndex*> AllIndexes;
183 
184  bool IsTextureIndexed;
185  std::vector<CharWithIndex*> TextureIndexes;
186 
187  bool IsModelIndexed;
188  std::vector<CharWithIndex*> ModelIndexes;
189 
190  bool IsSoundIndexed;
191  std::vector<CharWithIndex*> SoundIndexes;
192 
193  bool IsScriptIndexed;
194  std::vector<CharWithIndex*> ScriptIndexes;
195 
196  // vector sorting //
197  bool IsSorted;
198  bool IsBeingSorted;
199  bool ShouldSortStop;
200 
201  LErrorReporter* ErrorReporter = nullptr;
202 
203  // ------------------------------------ //
204  static std::string DataFolder;
205  static std::string ModelsFolder;
206  static std::string ScriptsFolder;
207  static std::string ShaderFolder;
208  static std::string TextureFolder;
209  static std::string MaterialFolder;
210  static std::string FontFolder;
211  static std::string SoundFolder;
212 
213  static FileSystem* Staticaccess;
214 };
215 
216 } // namespace Leviathan
static DLLEXPORT std::string GetScriptsFolder()
Definition: FileSystem.cpp:210
DLLEXPORT void SortFileVectors()
Definition: FileSystem.cpp:569
static DLLEXPORT std::string GetModelsFolder()
Definition: FileSystem.cpp:204
static DLLEXPORT std::string GetDataFolder()
Definition: FileSystem.cpp:198
static DLLEXPORT void SetModelsFolder(const std::string &folder)
Definition: FileSystem.cpp:280
FileDefinitionType(FileSystem *instance, const std::string &path)
Definition: FileSystem.cpp:898
DLLEXPORT std::vector< std::shared_ptr< FileDefinitionType > > FindAllMatchingFiles(FILEGROUP which, const std::string &regexname, const std::string &extensions, bool searchall=true)
Returns all matching files.
Definition: FileSystem.cpp:737
static DLLEXPORT void SetDataFolder(const std::string &folder)
Definition: FileSystem.cpp:274
DLLEXPORT std::vector< std::shared_ptr< FileDefinitionType > > & GetModelFiles()
Definition: FileSystem.cpp:785
DLLEXPORT bool ReSearchFiles()
Destroys the current index and recreates it.
Definition: FileSystem.cpp:162
static DLLEXPORT bool AppendToFile(const std::string &data, const std::string &filepath)
Definition: FileSystem.cpp:489
Class for indexing and searching game data directory.
Definition: FileSystem.h:65
static DLLEXPORT bool GetFilesInDirectory(std::vector< std::string > &files, const std::string &dirpath, const std::string &pattern="*.*", bool recursive=true)
Definition: FileSystem.cpp:384
static DLLEXPORT std::string GetShaderFolder()
Definition: FileSystem.cpp:216
FileTypeHolder(int id, const std::string &name)
Definition: FileSystem.cpp:938
DLLEXPORT void GetExtensionIDS(const std::string &extensions, std::vector< int > &ids)
Returns list of matching ids to the extensions.
Definition: FileSystem.cpp:633
static DLLEXPORT bool DoesExtensionMatch(FileDefinitionType *file, const std::vector< int > &Ids)
Definition: FileSystem.cpp:240
DLLEXPORT bool operator()(const std::shared_ptr< FileDefinitionType > &first, const std::shared_ptr< FileDefinitionType > &second)
Definition: FileSystem.cpp:924
DLLEXPORT void CreateIndexesForVecs(bool forcerecreation=false)
Definition: FileSystem.cpp:603
static DLLEXPORT FileSystem * Get()
Definition: FileSystem.cpp:74
static DLLEXPORT void SetScriptsFolder(const std::string &folder)
Definition: FileSystem.cpp:286
static DLLEXPORT void SetTextureFolder(const std::string &folder)
Definition: FileSystem.cpp:298
static DLLEXPORT void SetShaderFolder(const std::string &folder)
Definition: FileSystem.cpp:292
static DLLEXPORT std::string GetFontFolder()
Definition: FileSystem.cpp:228
DLLEXPORT ~FileSystem()
Definition: FileSystem.cpp:41
DLLEXPORT std::vector< std::shared_ptr< FileDefinitionType > > & GetAllFiles()
Definition: FileSystem.cpp:795
DLLEXPORT bool Init(LErrorReporter *errorreport)
Runs the indexing and sorting.
Definition: FileSystem.cpp:91
std::string GetNameWithExtension() const
Definition: FileSystem.cpp:918
DLLEXPORT std::vector< std::shared_ptr< FileDefinitionType > > & GetScriptFiles()
Definition: FileSystem.cpp:800
DLLEXPORT FileSystem()
Definition: FileSystem.cpp:29
static DLLEXPORT bool FileExists(const std::string &name)
Definition: FileSystem.cpp:451
DLLEXPORT std::string SearchForFile(FILEGROUP which, const std::string &name, const std::string &extensions, bool searchall=true)
Searches for a file.
Definition: FileSystem.cpp:666
static DLLEXPORT std::string GetSoundFolder()
Definition: FileSystem.cpp:234
DLLEXPORT std::vector< std::shared_ptr< FileDefinitionType > > & GetSoundFiles()
Definition: FileSystem.cpp:790
Helper class for indexing.
Definition: FileSystem.h:24
static DLLEXPORT bool ReadFileEntirely(const std::string &file, std::string &resultreceiver)
Definition: FileSystem.cpp:538
static DLLEXPORT bool LoadDataDump(const std::string &file, std::vector< std::shared_ptr< NamedVariableList >> &vec, LErrorReporter *errorreport)
Definition: FileSystem.cpp:304
static DLLEXPORT std::string GetTextureFolder()
Definition: FileSystem.cpp:222
#define DLLEXPORT
Definition: Include.h:84
The access mask controls which registered functions and classes a script sees.
Definition: GameModule.h:12
static DLLEXPORT size_t GetFileLength(const std::string &name)
Definition: FileSystem.cpp:431
DLLEXPORT int RegisterExtension(const std::string &extension)
Reserves a number for an extension string.
Definition: FileSystem.cpp:618
static DLLEXPORT bool WriteToFile(const std::string &data, const std::string &filename)
Definition: FileSystem.cpp:456
bool operator<(const FileDefinitionType &other) const
Definition: FileSystem.cpp:911
DLLEXPORT const std::string & GetExtensionName(int id) const
Retrieves the name of the extension from the id.
Definition: FileSystem.cpp:649