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 <regex>
10 #include "ErrorReporter.h"
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 
37  FileTypeHolder(int id, const std::string &name);
38 
39  int ID;
40  std::string Name;
41  };
42 
43 
45  // just the path, everything else is worked out by the constructor //
46  FileDefinitionType(FileSystem* instance, const std::string &path);
48 
50  std::string GetNameWithExtension() const;
51 
52  // operation for sorting //
53  bool operator < (const FileDefinitionType& other) const;
54 
55  std::string RelativePath;
56  std::string Name;
58  };
59 
60  struct FileDefSorter{
61  DLLEXPORT bool operator()(const std::shared_ptr<FileDefinitionType>& first,
62  const std::shared_ptr<FileDefinitionType>& second);
63  };
64 
66  class FileSystem{
67  public:
69  DLLEXPORT ~FileSystem();
70 
72  DLLEXPORT bool Init(LErrorReporter* errorreport);
73 
75  DLLEXPORT bool ReSearchFiles();
76 
77  DLLEXPORT void SortFileVectors();
78  DLLEXPORT void CreateIndexesForVecs(bool forcerecreation = false);
79 
81  DLLEXPORT int RegisterExtension(const std::string &extension);
82 
85  DLLEXPORT void GetExtensionIDS(const std::string& extensions, std::vector<int> &ids);
86 
89  DLLEXPORT const std::string& GetExtensionName(int id) const;
90 
91 
94  DLLEXPORT std::string SearchForFile(FILEGROUP which, const std::string& name,
95  const std::string &extensions, bool searchall = true);
96 
98  DLLEXPORT std::vector<std::shared_ptr<FileDefinitionType>> FindAllMatchingFiles(
99  FILEGROUP which, const std::string& regexname, const std::string &extensions,
100  bool searchall = true);
101 
102  // direct access to files //
103  DLLEXPORT std::vector<std::shared_ptr<FileDefinitionType>>& GetModelFiles();
104  DLLEXPORT std::vector<std::shared_ptr<FileDefinitionType>>& GetSoundFiles();
105  DLLEXPORT std::vector<std::shared_ptr<FileDefinitionType>>& GetAllFiles();
106  DLLEXPORT std::vector<std::shared_ptr<FileDefinitionType>>& GetScriptFiles();
107  // ------------------ Static part ------------------ //
108 
109  DLLEXPORT static std::string GetDataFolder();
110  DLLEXPORT static std::string GetModelsFolder();
111  DLLEXPORT static std::string GetScriptsFolder();
112  DLLEXPORT static std::string GetShaderFolder();
113  DLLEXPORT static std::string GetTextureFolder();
114  DLLEXPORT static std::string GetFontFolder();
115  DLLEXPORT static std::string GetSoundFolder();
116 
117  #ifdef LEVIATHAN_USING_OGRE
118  DLLEXPORT static void RegisterOGREResourceGroups(bool testload = false);
120  DLLEXPORT static void RegisterOGREResourceLocation(const std::string &location);
121  #endif // LEVIATHAN_USING_OGRE
122 
123  DLLEXPORT static bool DoesExtensionMatch(FileDefinitionType* file,
124  const std::vector<int> &Ids);
125 
126 #ifdef _WIN32
127  DLLEXPORT static void GetWindowsFolder(std::wstring &path);
128  DLLEXPORT static void GetSpecialFolder(std::wstring &path, int specialtype);
129 #endif
130 
131  DLLEXPORT static void SetDataFolder(const std::string &folder);
132  DLLEXPORT static void SetModelsFolder(const std::string &folder);
133  DLLEXPORT static void SetScriptsFolder(const std::string &folder);
134  DLLEXPORT static void SetShaderFolder(const std::string &folder);
135  DLLEXPORT static void SetTextureFolder(const std::string &folder);
136 
137  // file handling //
138  DLLEXPORT static bool LoadDataDump(const std::string &file,
139  std::vector<std::shared_ptr<NamedVariableList>> &vec, LErrorReporter* errorreport);
140 
141  // Warning: \todo linux version ignores the defined pattern //
142  DLLEXPORT static bool GetFilesInDirectory(std::vector<std::string> &files,
143  const std::string &dirpath, const std::string &pattern = "*.*",
144  bool recursive = true);
145 
146  // file operations //
147  DLLEXPORT static size_t GetFileLength(const std::string &name);
148  DLLEXPORT static bool FileExists(const std::string &name);
149  DLLEXPORT static bool WriteToFile(const std::string &data, const std::string &filename);
150  DLLEXPORT static bool WriteToFile(const std::wstring &data, const std::wstring &filename);
151  DLLEXPORT static bool AppendToFile(const std::string &data, const std::string &filepath);
152 
153  DLLEXPORT static bool ReadFileEntirely(const std::string &file,
154  std::string &resultreceiver);
155 
156  DLLEXPORT static bool ReadFileEntirely(const std::wstring &file,
157  std::wstring &resultreceiver);
158 
159 
160  DLLEXPORT static FileSystem* Get();
161 
162  private:
163  // file search functions //
164  std::shared_ptr<FileDefinitionType> _SearchForFileInVec(
165  std::vector<std::shared_ptr<FileDefinitionType>> &vec, std::vector<int> &extensions,
166  const std::string &name, bool UseIndexVector, std::vector<CharWithIndex*>* Index);
167 
168  void _SearchForFilesInVec(std::vector<std::shared_ptr<FileDefinitionType>>& vec,
169  std::vector<std::shared_ptr<FileDefinitionType>>& results,
170  std::vector<int>& extensions, const std::regex &regex);
171 
172  void _CreateIndexesIfMissing(std::vector<std::shared_ptr<FileDefinitionType>> &vec,
173  std::vector<CharWithIndex*> &resultvec, bool &indexed, const bool &force /*= false*/);
174 
175  // ------------------------------------ //
176  // vector that holds string value of file extension and it's id code //
177  std::vector<FileTypeHolder*> FileTypes;
178  int CurrentFileExtID;
179 
180  // file holders //
181  std::vector<std::shared_ptr<FileDefinitionType>> AllFiles;
182 
183  std::vector<std::shared_ptr<FileDefinitionType>> TextureFiles;
184  std::vector<std::shared_ptr<FileDefinitionType>> ModelFiles;
185  std::vector<std::shared_ptr<FileDefinitionType>> SoundFiles;
186  std::vector<std::shared_ptr<FileDefinitionType>> ScriptFiles;
187 
188  // index vectors //
189  bool IsAllIndexed;
190  std::vector<CharWithIndex*> AllIndexes;
191 
192  bool IsTextureIndexed;
193  std::vector<CharWithIndex*> TextureIndexes;
194 
195  bool IsModelIndexed;
196  std::vector<CharWithIndex*> ModelIndexes;
197 
198  bool IsSoundIndexed;
199  std::vector<CharWithIndex*> SoundIndexes;
200 
201  bool IsScriptIndexed;
202  std::vector<CharWithIndex*> ScriptIndexes;
203 
204  // vector sorting //
205  bool IsSorted;
206  bool IsBeingSorted;
207  bool ShouldSortStop;
208 
209  LErrorReporter* ErrorReporter = nullptr;
210 
211  // ------------------------------------ //
212  static std::string DataFolder;
213  static std::string ModelsFolder;
214  static std::string ScriptsFolder;
215  static std::string ShaderFolder;
216  static std::string TextureFolder;
217  static std::string MaterialFolder;
218  static std::string FontFolder;
219  static std::string SoundFolder;
220 
221  static FileSystem* Staticaccess;
222  };
223 
224 }
225 
Class for indexing and searching game data directory.
Definition: FileSystem.h:66
Helper class for indexing.
Definition: FileSystem.h:24
#define DLLEXPORT
Definition: Include.h:118
The access mask controls which registered functions and classes a script sees.
Definition: GameModule.h:12