Leviathan  0.8.0.0
Leviathan game engine
FileSystem.h
Go to the documentation of this file.
1 #pragma once
2 // ------------------------------------ //
3 #include "Define.h"
4 // ------------------------------------ //
5 // ---- includes ---- //
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 #ifdef LEVIATHAN_USING_OGRE
117  DLLEXPORT static void RegisterOGREResourceGroups(bool testload = false);
119  DLLEXPORT static void RegisterOGREResourceLocation(const std::string& location);
120 #endif // LEVIATHAN_USING_OGRE
121 
122  DLLEXPORT static bool DoesExtensionMatch(
123  FileDefinitionType* file, const std::vector<int>& Ids);
124 
125 #ifdef _WIN32
126  DLLEXPORT static void GetWindowsFolder(std::wstring& path);
127  DLLEXPORT static void GetSpecialFolder(std::wstring& path, int specialtype);
128 #endif
129 
130  DLLEXPORT static void SetDataFolder(const std::string& folder);
131  DLLEXPORT static void SetModelsFolder(const std::string& folder);
132  DLLEXPORT static void SetScriptsFolder(const std::string& folder);
133  DLLEXPORT static void SetShaderFolder(const std::string& folder);
134  DLLEXPORT static void SetTextureFolder(const std::string& folder);
135 
136  // file handling //
137  DLLEXPORT static bool LoadDataDump(const std::string& file,
138  std::vector<std::shared_ptr<NamedVariableList>>& vec, LErrorReporter* errorreport);
139 
140  // Warning: \todo linux version ignores the defined pattern //
141  DLLEXPORT static bool GetFilesInDirectory(std::vector<std::string>& files,
142  const std::string& dirpath, const std::string& pattern = "*.*", bool recursive = true);
143 
144  // file operations //
145  DLLEXPORT static size_t GetFileLength(const std::string& name);
146  DLLEXPORT static bool FileExists(const std::string& name);
147  DLLEXPORT static bool WriteToFile(const std::string& data, const std::string& filename);
148  DLLEXPORT static bool WriteToFile(const std::wstring& data, const std::wstring& filename);
149  DLLEXPORT static bool AppendToFile(const std::string& data, const std::string& filepath);
150 
151  DLLEXPORT static bool ReadFileEntirely(
152  const std::string& file, std::string& resultreceiver);
153 
154  DLLEXPORT static bool ReadFileEntirely(
155  const std::wstring& file, std::wstring& resultreceiver);
156 
157 
158  DLLEXPORT static FileSystem* Get();
159 
160 private:
161  // file search functions //
162  std::shared_ptr<FileDefinitionType> _SearchForFileInVec(
163  std::vector<std::shared_ptr<FileDefinitionType>>& vec, std::vector<int>& extensions,
164  const std::string& name, bool UseIndexVector, std::vector<CharWithIndex*>* Index);
165 
166  void _SearchForFilesInVec(std::vector<std::shared_ptr<FileDefinitionType>>& vec,
167  std::vector<std::shared_ptr<FileDefinitionType>>& results,
168  std::vector<int>& extensions, const std::regex& regex);
169 
170  void _CreateIndexesIfMissing(std::vector<std::shared_ptr<FileDefinitionType>>& vec,
171  std::vector<CharWithIndex*>& resultvec, bool& indexed, const bool& force /*= false*/);
172 
173  // ------------------------------------ //
174  // vector that holds string value of file extension and it's id code //
175  std::vector<FileTypeHolder*> FileTypes;
176  int CurrentFileExtID;
177 
178  // file holders //
179  std::vector<std::shared_ptr<FileDefinitionType>> AllFiles;
180 
181  std::vector<std::shared_ptr<FileDefinitionType>> TextureFiles;
182  std::vector<std::shared_ptr<FileDefinitionType>> ModelFiles;
183  std::vector<std::shared_ptr<FileDefinitionType>> SoundFiles;
184  std::vector<std::shared_ptr<FileDefinitionType>> ScriptFiles;
185 
186  // index vectors //
187  bool IsAllIndexed;
188  std::vector<CharWithIndex*> AllIndexes;
189 
190  bool IsTextureIndexed;
191  std::vector<CharWithIndex*> TextureIndexes;
192 
193  bool IsModelIndexed;
194  std::vector<CharWithIndex*> ModelIndexes;
195 
196  bool IsSoundIndexed;
197  std::vector<CharWithIndex*> SoundIndexes;
198 
199  bool IsScriptIndexed;
200  std::vector<CharWithIndex*> ScriptIndexes;
201 
202  // vector sorting //
203  bool IsSorted;
204  bool IsBeingSorted;
205  bool ShouldSortStop;
206 
207  LErrorReporter* ErrorReporter = nullptr;
208 
209  // ------------------------------------ //
210  static std::string DataFolder;
211  static std::string ModelsFolder;
212  static std::string ScriptsFolder;
213  static std::string ShaderFolder;
214  static std::string TextureFolder;
215  static std::string MaterialFolder;
216  static std::string FontFolder;
217  static std::string SoundFolder;
218 
219  static FileSystem* Staticaccess;
220 };
221 
222 } // namespace Leviathan
static DLLEXPORT std::string GetScriptsFolder()
Definition: FileSystem.cpp:214
DLLEXPORT void SortFileVectors()
Definition: FileSystem.cpp:574
static DLLEXPORT std::string GetModelsFolder()
Definition: FileSystem.cpp:208
static DLLEXPORT std::string GetDataFolder()
Definition: FileSystem.cpp:202
static DLLEXPORT void SetModelsFolder(const std::string &folder)
Definition: FileSystem.cpp:284
FileDefinitionType(FileSystem *instance, const std::string &path)
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:742
static DLLEXPORT void SetDataFolder(const std::string &folder)
Definition: FileSystem.cpp:278
DLLEXPORT std::vector< std::shared_ptr< FileDefinitionType > > & GetModelFiles()
Definition: FileSystem.cpp:790
DLLEXPORT bool ReSearchFiles()
Destroys the current index and recreates it.
Definition: FileSystem.cpp:166
static DLLEXPORT bool AppendToFile(const std::string &data, const std::string &filepath)
Definition: FileSystem.cpp:494
Class for indexing and searching game data directory.
Definition: FileSystem.h:65
static DLLEXPORT void RegisterOGREResourceLocation(const std::string &location)
static DLLEXPORT bool GetFilesInDirectory(std::vector< std::string > &files, const std::string &dirpath, const std::string &pattern="*.*", bool recursive=true)
Definition: FileSystem.cpp:388
static DLLEXPORT std::string GetShaderFolder()
Definition: FileSystem.cpp:220
FileTypeHolder(int id, const std::string &name)
DLLEXPORT void GetExtensionIDS(const std::string &extensions, std::vector< int > &ids)
Returns list of matching ids to the extensions.
Definition: FileSystem.cpp:638
static DLLEXPORT bool DoesExtensionMatch(FileDefinitionType *file, const std::vector< int > &Ids)
Definition: FileSystem.cpp:244
DLLEXPORT bool operator()(const std::shared_ptr< FileDefinitionType > &first, const std::shared_ptr< FileDefinitionType > &second)
DLLEXPORT void CreateIndexesForVecs(bool forcerecreation=false)
Definition: FileSystem.cpp:608
static DLLEXPORT FileSystem * Get()
Definition: FileSystem.cpp:78
static DLLEXPORT void SetScriptsFolder(const std::string &folder)
Definition: FileSystem.cpp:290
static DLLEXPORT void SetTextureFolder(const std::string &folder)
Definition: FileSystem.cpp:302
static DLLEXPORT void SetShaderFolder(const std::string &folder)
Definition: FileSystem.cpp:296
static DLLEXPORT std::string GetFontFolder()
Definition: FileSystem.cpp:232
DLLEXPORT ~FileSystem()
Definition: FileSystem.cpp:45
DLLEXPORT std::vector< std::shared_ptr< FileDefinitionType > > & GetAllFiles()
Definition: FileSystem.cpp:800
DLLEXPORT bool Init(LErrorReporter *errorreport)
Runs the indexing and sorting.
Definition: FileSystem.cpp:95
std::string GetNameWithExtension() const
DLLEXPORT std::vector< std::shared_ptr< FileDefinitionType > > & GetScriptFiles()
Definition: FileSystem.cpp:805
DLLEXPORT FileSystem()
Definition: FileSystem.cpp:33
static DLLEXPORT bool FileExists(const std::string &name)
Definition: FileSystem.cpp:455
DLLEXPORT std::string SearchForFile(FILEGROUP which, const std::string &name, const std::string &extensions, bool searchall=true)
Searches for a file.
Definition: FileSystem.cpp:671
static DLLEXPORT std::string GetSoundFolder()
Definition: FileSystem.cpp:238
DLLEXPORT std::vector< std::shared_ptr< FileDefinitionType > > & GetSoundFiles()
Definition: FileSystem.cpp:795
Helper class for indexing.
Definition: FileSystem.h:24
static DLLEXPORT bool ReadFileEntirely(const std::string &file, std::string &resultreceiver)
Definition: FileSystem.cpp:543
static DLLEXPORT bool LoadDataDump(const std::string &file, std::vector< std::shared_ptr< NamedVariableList >> &vec, LErrorReporter *errorreport)
Definition: FileSystem.cpp:308
static DLLEXPORT std::string GetTextureFolder()
Definition: FileSystem.cpp:226
#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:435
static DLLEXPORT void RegisterOGREResourceGroups(bool testload=false)
Definition: FileSystem.cpp:937
DLLEXPORT int RegisterExtension(const std::string &extension)
Reserves a number for an extension string.
Definition: FileSystem.cpp:623
static DLLEXPORT bool WriteToFile(const std::string &data, const std::string &filename)
Definition: FileSystem.cpp:461
bool operator<(const FileDefinitionType &other) const
DLLEXPORT const std::string & GetExtensionName(int id) const
Retrieves the name of the extension from the id.
Definition: FileSystem.cpp:654