Leviathan  0.8.0.0
Leviathan game engine
Leviathan::FileSystem Class Reference

Class for indexing and searching game data directory. More...

#include <FileSystem.h>

Public Member Functions

DLLEXPORT FileSystem ()
 
DLLEXPORT ~FileSystem ()
 
DLLEXPORT bool Init (LErrorReporter *errorreport)
 Runs the indexing and sorting. More...
 
DLLEXPORT bool ReSearchFiles ()
 Destroys the current index and recreates it. More...
 
DLLEXPORT void SortFileVectors ()
 
DLLEXPORT void CreateIndexesForVecs (bool forcerecreation=false)
 
DLLEXPORT int RegisterExtension (const std::string &extension)
 Reserves a number for an extension string. More...
 
DLLEXPORT void GetExtensionIDS (const std::string &extensions, std::vector< int > &ids)
 Returns list of matching ids to the extensions. More...
 
DLLEXPORT const std::string & GetExtensionName (int id) const
 Retrieves the name of the extension from the id. More...
 
DLLEXPORT std::string SearchForFile (FILEGROUP which, const std::string &name, const std::string &extensions, bool searchall=true)
 Searches for a file. More...
 
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. More...
 
DLLEXPORT std::vector< std::shared_ptr< FileDefinitionType > > & GetModelFiles ()
 
DLLEXPORT std::vector< std::shared_ptr< FileDefinitionType > > & GetSoundFiles ()
 
DLLEXPORT std::vector< std::shared_ptr< FileDefinitionType > > & GetAllFiles ()
 
DLLEXPORT std::vector< std::shared_ptr< FileDefinitionType > > & GetScriptFiles ()
 

Static Public Member Functions

static DLLEXPORT std::string GetDataFolder ()
 
static DLLEXPORT std::string GetModelsFolder ()
 
static DLLEXPORT std::string GetScriptsFolder ()
 
static DLLEXPORT std::string GetShaderFolder ()
 
static DLLEXPORT std::string GetTextureFolder ()
 
static DLLEXPORT std::string GetFontFolder ()
 
static DLLEXPORT std::string GetSoundFolder ()
 
static DLLEXPORT void RegisterOGREResourceGroups (bool testload=false)
 
static DLLEXPORT void RegisterOGREResourceLocation (const std::string &location)
 
static DLLEXPORT bool DoesExtensionMatch (FileDefinitionType *file, const std::vector< int > &Ids)
 
static DLLEXPORT void SetDataFolder (const std::string &folder)
 
static DLLEXPORT void SetModelsFolder (const std::string &folder)
 
static DLLEXPORT void SetScriptsFolder (const std::string &folder)
 
static DLLEXPORT void SetShaderFolder (const std::string &folder)
 
static DLLEXPORT void SetTextureFolder (const std::string &folder)
 
static DLLEXPORT bool LoadDataDump (const std::string &file, std::vector< std::shared_ptr< NamedVariableList >> &vec, LErrorReporter *errorreport)
 
static DLLEXPORT bool GetFilesInDirectory (std::vector< std::string > &files, const std::string &dirpath, const std::string &pattern="*.*", bool recursive=true)
 
static DLLEXPORT size_t GetFileLength (const std::string &name)
 
static DLLEXPORT bool FileExists (const std::string &name)
 
static DLLEXPORT bool WriteToFile (const std::string &data, const std::string &filename)
 
static DLLEXPORT bool WriteToFile (const std::wstring &data, const std::wstring &filename)
 
static DLLEXPORT bool AppendToFile (const std::string &data, const std::string &filepath)
 
static DLLEXPORT bool ReadFileEntirely (const std::string &file, std::string &resultreceiver)
 
static DLLEXPORT bool ReadFileEntirely (const std::wstring &file, std::wstring &resultreceiver)
 
static DLLEXPORT FileSystemGet ()
 

Detailed Description

Class for indexing and searching game data directory.

Definition at line 66 of file FileSystem.h.

Constructor & Destructor Documentation

◆ FileSystem()

Leviathan::FileSystem::FileSystem ( )

Definition at line 34 of file FileSystem.cpp.

35 {
36  // set static access //
37  Staticaccess = this;
38  // set default values //
39  CurrentFileExtID = 25;
40 
41  // index creation flags //
42  IsAllIndexed = IsTextureIndexed = IsModelIndexed = IsSoundIndexed = IsScriptIndexed =
43  IsSorted = IsBeingSorted = ShouldSortStop = false;
44 }

◆ ~FileSystem()

Leviathan::FileSystem::~FileSystem ( )

Definition at line 46 of file FileSystem.cpp.

47 {
48 
49  // Helps catch errors with tests etc.
50  if(Staticaccess == this)
51  Staticaccess = nullptr;
52 
53  SAFE_DELETE_VECTOR(FileTypes);
54 
55  AllFiles.clear();
56 
57  TextureFiles.clear();
58  ModelFiles.clear();
59  SoundFiles.clear();
60  ScriptFiles.clear();
61 
62  // Clear indexes //
63  IsAllIndexed = false;
64  SAFE_DELETE_VECTOR(AllIndexes);
65 
66  IsTextureIndexed = false;
67  SAFE_DELETE_VECTOR(TextureIndexes);
68 
69  IsModelIndexed = false;
70  SAFE_DELETE_VECTOR(ModelIndexes);
71 
72  IsSoundIndexed = false;
73  SAFE_DELETE_VECTOR(SoundIndexes);
74 
75  IsScriptIndexed = false;
76  SAFE_DELETE_VECTOR(ScriptIndexes);
77 }
#define SAFE_DELETE_VECTOR(x)
Definition: Define.h:123

Member Function Documentation

◆ AppendToFile()

bool Leviathan::FileSystem::AppendToFile ( const std::string &  data,
const std::string &  filepath 
)
static

Definition at line 495 of file FileSystem.cpp.

496 {
497 
498  ofstream file(filepath, ofstream::app | ios::binary);
499 
500  if(file.is_open()) {
501 
502  file << data;
503 
504  file.close();
505  return true;
506  }
507 
508  file.close();
509  return false;
510 }

◆ CreateIndexesForVecs()

DLLEXPORT void Leviathan::FileSystem::CreateIndexesForVecs ( bool  forcerecreation = false)

Definition at line 609 of file FileSystem.cpp.

610 {
611  // check are vectors sorted, if not call sort //
612  if(!IsSorted) {
613 
614  SortFileVectors();
615  }
616 
617  _CreateIndexesIfMissing(AllFiles, AllIndexes, IsAllIndexed, forcerecreation);
618  _CreateIndexesIfMissing(TextureFiles, TextureIndexes, IsTextureIndexed, forcerecreation);
619  _CreateIndexesIfMissing(ModelFiles, ModelIndexes, IsModelIndexed, forcerecreation);
620  _CreateIndexesIfMissing(SoundFiles, SoundIndexes, IsSoundIndexed, forcerecreation);
621  _CreateIndexesIfMissing(ScriptFiles, ScriptIndexes, IsScriptIndexed, forcerecreation);
622 }
DLLEXPORT void SortFileVectors()
Definition: FileSystem.cpp:575

◆ DoesExtensionMatch()

DLLEXPORT bool Leviathan::FileSystem::DoesExtensionMatch ( FileDefinitionType file,
const std::vector< int > &  Ids 
)
static

Definition at line 245 of file FileSystem.cpp.

247 {
248  // check does file contain an extension id that is in the vector //
249  for(size_t i = 0; i < Ids.size(); i++) {
250  if(file->ExtensionID == Ids[i]) {
251  // match found //
252  return true;
253  }
254  }
255  return false;
256 }

◆ FileExists()

DLLEXPORT bool Leviathan::FileSystem::FileExists ( const std::string &  name)
static

Definition at line 456 of file FileSystem.cpp.

457 {
458 
459  return boost::filesystem::exists(name);
460 }

◆ FindAllMatchingFiles()

DLLEXPORT vector< shared_ptr< FileDefinitionType > > Leviathan::FileSystem::FindAllMatchingFiles ( FILEGROUP  which,
const std::string &  regexname,
const std::string &  extensions,
bool  searchall = true 
)

Returns all matching files.

Definition at line 743 of file FileSystem.cpp.

746 {
747  // generate info about the search file //
748  vector<int> ExtensionIDS;
749  GetExtensionIDS(extensions, ExtensionIDS);
750 
751  // create regex //
752  regex usedregex(regexname, regex_constants::ECMAScript | regex_constants::icase);
753 
754  vector<shared_ptr<FileDefinitionType>> foundfiles;
755 
756  if(searchall) {
757 
758  _SearchForFilesInVec(AllFiles, foundfiles, ExtensionIDS, usedregex);
759 
760  } else {
761  // specific vector //
762  vector<shared_ptr<FileDefinitionType>>* targetvector = NULL;
763 
764  switch(which) {
765  case FILEGROUP_MODEL: {
766  targetvector = &ModelFiles;
767  } break;
768  case FILEGROUP_TEXTURE: {
769  targetvector = &TextureFiles;
770  } break;
771  case FILEGROUP_SOUND: {
772  targetvector = &SoundFiles;
773  } break;
774  case FILEGROUP_SCRIPT: {
775  targetvector = &ScriptFiles;
776  } break;
777  case FILEGROUP_OTHER: {
778  targetvector = &AllFiles;
779  } break;
780  }
781 
782  _SearchForFilesInVec(*targetvector, foundfiles, ExtensionIDS, usedregex);
783  }
784 
785  // return what we found //
786  return foundfiles;
787 }
DLLEXPORT void GetExtensionIDS(const std::string &extensions, std::vector< int > &ids)
Returns list of matching ids to the extensions.
Definition: FileSystem.cpp:639

◆ Get()

DLLEXPORT FileSystem * FileSystem::Get ( )
static

Definition at line 79 of file FileSystem.cpp.

80 {
81 
82  return Staticaccess;
83 }

◆ GetAllFiles()

vector< shared_ptr< FileDefinitionType > > & Leviathan::FileSystem::GetAllFiles ( )

Definition at line 801 of file FileSystem.cpp.

802 {
803  return AllFiles;
804 }

◆ GetDataFolder()

string Leviathan::FileSystem::GetDataFolder ( )
static

Definition at line 203 of file FileSystem.cpp.

204 {
205 
206  return (DataFolder);
207 }

◆ GetExtensionIDS()

void Leviathan::FileSystem::GetExtensionIDS ( const std::string &  extensions,
std::vector< int > &  ids 
)

Returns list of matching ids to the extensions.

Parameters
extensions'|' separated list of extensions, example: "png|jpg|txt"

Definition at line 639 of file FileSystem.cpp.

640 {
641  // generate info about the extensions //
642  vector<string> Exts;
643  StringOperations::CutString(extensions, string("|"), Exts);
644  if(Exts.size() == 0) {
645  // just one extension //
646  ids.push_back(RegisterExtension(extensions));
647  return;
648  }
649 
650  for(size_t i = 0; i < Exts.size(); i++) {
651  ids.push_back(RegisterExtension(Exts[i]));
652  }
653 }
static bool CutString(const StringTypeN &strtocut, const StringTypeN &separator, std::vector< StringTypeN > &vec)
DLLEXPORT int RegisterExtension(const std::string &extension)
Reserves a number for an extension string.
Definition: FileSystem.cpp:624

◆ GetExtensionName()

DLLEXPORT const string & Leviathan::FileSystem::GetExtensionName ( int  id) const

Retrieves the name of the extension from the id.

Exceptions
NotFoundwhen extension with id not registered

Definition at line 655 of file FileSystem.cpp.

656 {
657  // Look for it //
658  for(size_t i = 0; i < FileTypes.size(); i++) {
659  if(FileTypes[i]->ID == id)
660  return FileTypes[i]->Name;
661  }
662 
663  // Not found //
664 #ifndef ALTERNATIVE_EXCEPTIONS_FATAL
665  throw NotFound("No extension corresponds with id");
666 #else
667  LEVIATHAN_ASSERT(0, "No extension corresponds with id");
668  return FileTypes[0]->Name;
669 #endif // ALTERNATIVE_EXCEPTIONS_FATAL
670 }

◆ GetFileLength()

size_t Leviathan::FileSystem::GetFileLength ( const std::string &  name)
static

Definition at line 436 of file FileSystem.cpp.

437 {
438 
439  ifstream file(name, ios::binary);
440 
441  if(file.good()) {
442 
443  file.seekg(0, ios::end);
444  auto returnval = file.tellg();
445  file.close();
446  return returnval;
447  }
448 
449 #ifndef ALTERNATIVE_EXCEPTIONS_FATAL
450  throw InvalidArgument("Cannot determine file size it doesn't exist");
451 #else
452  return 0;
453 #endif
454 }

◆ GetFilesInDirectory()

DLLEXPORT bool Leviathan::FileSystem::GetFilesInDirectory ( std::vector< std::string > &  files,
const std::string &  dirpath,
const std::string &  pattern = "*.*",
bool  recursive = true 
)
static

Definition at line 389 of file FileSystem.cpp.

391 {
392  dirent* ent;
393  struct stat st;
394 
395  // Start searching //
396  DIR* dir = opendir(dirpath.c_str());
397 
398  if(!dir) {
399 
400  // Non-existant directory
401  return false;
402  }
403 
404  while((ent = readdir(dir)) != NULL) {
405  const string file_name = ent->d_name;
406 
407  // Ignore if starts with a '.' //
408  if(file_name[0] == '.')
409  continue;
410 
411  const string full_file_name = dirpath + "/" + file_name;
412 
413  // Get info to determine if it is a dirpath //
414  if(stat(full_file_name.c_str(), &st) == -1)
415  continue;
416 
417  // Check if it is a dirpath //
418  if((st.st_mode & S_IFDIR) != 0) {
419  // Go into dirpath if recursive search //
420  if(recursive) {
421  // \todo fix performance //
422  GetFilesInDirectory(files, full_file_name, pattern, recursive);
423  }
424  continue;
425  }
426 
427  files.push_back(full_file_name);
428  }
429 
430  closedir(dir);
431 
432  return true;
433 }
static DLLEXPORT bool GetFilesInDirectory(std::vector< std::string > &files, const std::string &dirpath, const std::string &pattern="*.*", bool recursive=true)
Definition: FileSystem.cpp:389

◆ GetFontFolder()

string Leviathan::FileSystem::GetFontFolder ( )
static

Definition at line 233 of file FileSystem.cpp.

234 {
235 
236  return (DataFolder + FontFolder);
237 }

◆ GetModelFiles()

vector< shared_ptr< FileDefinitionType > > & Leviathan::FileSystem::GetModelFiles ( )

Definition at line 791 of file FileSystem.cpp.

792 {
793  return ModelFiles;
794 }

◆ GetModelsFolder()

string Leviathan::FileSystem::GetModelsFolder ( )
static

Definition at line 209 of file FileSystem.cpp.

210 {
211 
212  return (DataFolder + ModelsFolder);
213 }

◆ GetScriptFiles()

vector< shared_ptr< FileDefinitionType > > & Leviathan::FileSystem::GetScriptFiles ( )

Definition at line 806 of file FileSystem.cpp.

807 {
808  return ScriptFiles;
809 }

◆ GetScriptsFolder()

string Leviathan::FileSystem::GetScriptsFolder ( )
static

Definition at line 215 of file FileSystem.cpp.

216 {
217 
218  return (DataFolder + ScriptsFolder);
219 }

◆ GetShaderFolder()

string Leviathan::FileSystem::GetShaderFolder ( )
static

Definition at line 221 of file FileSystem.cpp.

222 {
223 
224  return (DataFolder + ShaderFolder);
225 }

◆ GetSoundFiles()

vector< shared_ptr< FileDefinitionType > > & Leviathan::FileSystem::GetSoundFiles ( )

Definition at line 796 of file FileSystem.cpp.

797 {
798  return SoundFiles;
799 }

◆ GetSoundFolder()

string Leviathan::FileSystem::GetSoundFolder ( )
static

Definition at line 239 of file FileSystem.cpp.

240 {
241 
242  return (DataFolder + SoundFolder);
243 }

◆ GetTextureFolder()

string Leviathan::FileSystem::GetTextureFolder ( )
static

Definition at line 227 of file FileSystem.cpp.

228 {
229 
230  return (DataFolder + TextureFolder);
231 }

◆ Init()

DLLEXPORT bool Leviathan::FileSystem::Init ( LErrorReporter errorreport)

Runs the indexing and sorting.

Definition at line 96 of file FileSystem.cpp.

97 {
98 
99  ErrorReporter = errorreport;
100 
101  IsSorted = false;
102 
103  // use find files function on data folder and then save results to appropriate vectors //
104  vector<string> files;
105 #ifdef _WIN32
106  GetFilesInDirectory(files, "./Data/");
107 #else
108  GetFilesInDirectory(files, "./Data");
109 #endif
110 
111  if(files.size() < 1) {
112 
113  ErrorReporter->Error(
114  std::string("FileSystem: SearchFiles: No files inside data folder, "
115  "cannot possibly work"));
116  return false;
117  }
118 
119  // save to appropriate places //
120  for(size_t i = 0; i < files.size(); i++) {
121 
122  // create new object for storing this //
123  auto tmpptr = make_shared<FileDefinitionType>(this, files[i]);
124 
125  if(files[i].find("./Data/Textures/") == 0) {
126 
127  // add to texture files //
128  TextureFiles.push_back((tmpptr));
129 
130  } else if(files[i].find("./Data/Models/") == 0) {
131 
132  // add to texture files //
133  ModelFiles.push_back(tmpptr);
134 
135  } else if(files[i].find("./Data/Sound/") == 0) {
136 
137  // add to texture files //
138  SoundFiles.push_back(tmpptr);
139 
140  } else if(files[i].find("./Data/Scripts/") == 0) {
141 
142  // add to texture files //
143  ScriptFiles.push_back(tmpptr);
144  }
145 
146  // everything should be in AllFiles vector //
147  AllFiles.push_back(tmpptr);
148  }
149  // print some info //
150  ErrorReporter->Info("FileSystem: found " + Convert::ToString(AllFiles.size()) +
151  " files in Data folder with " + Convert::ToString(FileTypes.size()) +
152  " different types of extensions");
153 
154  // sort for quick finding //
155  auto starttime = Time::GetTimeMicro64();
157 
158  auto elapsed = Time::GetTimeMicro64() - starttime;
159 
160  // print info //
161  ErrorReporter->Info("FileSystem: vectors sorted and indexes created, took " +
162  Convert::ToString(elapsed) + " micro seconds");
163 
164  return true;
165 }
static DLLEXPORT bool GetFilesInDirectory(std::vector< std::string > &files, const std::string &dirpath, const std::string &pattern="*.*", bool recursive=true)
Definition: FileSystem.cpp:389
DLLEXPORT void CreateIndexesForVecs(bool forcerecreation=false)
Definition: FileSystem.cpp:609
static DLLEXPORT int64_t GetTimeMicro64()
static std::string ToString(const T &val)
Definition: Convert.h:72
virtual void Error(const std::string &Text)=0
virtual void Info(const std::string &Text)=0

◆ LoadDataDump()

DLLEXPORT bool FileSystem::LoadDataDump ( const std::string &  file,
std::vector< std::shared_ptr< NamedVariableList >> &  vec,
LErrorReporter errorreport 
)
static

Definition at line 309 of file FileSystem.cpp.

311 {
312  // Get data //
313  ifstream stream(file);
314 
315  if(!stream.good()) {
316  // no file ! //
317  errorreport->Error("FileSystem: LoadDataDump: Failed to read file: " + file);
318  return false;
319  }
320 
321  // count length //
322  stream.seekg(0, ios::end);
323  auto length = stream.tellg();
324  stream.seekg(0, ios::beg);
325 
326  if(length == std::streampos(0)) {
327 
328  // empty file ! //
329  return false;
330  }
331 
332  // TODO: directly copy into the string
333  unique_ptr<char[]> Buff(new char[(size_t)length + 1]);
334 
335  // set null terminator, just in case
336  (Buff.get())[length] = '\0';
337 
338  stream.read(Buff.get(), length);
339 
340  stream.close();
341 
342  string filecontents = Buff.get();
343 
344  // Create values //
345  return NamedVariableList::ProcessDataDump(filecontents, vec, errorreport);
346 }
static DLLEXPORT bool ProcessDataDump(const std::string &data, std::vector< std::shared_ptr< NamedVariableList >> &vec, LErrorReporter *errorreport, std::map< std::string, std::shared_ptr< VariableBlock >> *predefined=NULL)
Definition: NamedVars.cpp:609
virtual void Error(const std::string &Text)=0

◆ ReadFileEntirely() [1/2]

DLLEXPORT bool Leviathan::FileSystem::ReadFileEntirely ( const std::string &  file,
std::string &  resultreceiver 
)
static

Definition at line 544 of file FileSystem.cpp.

546 {
547 
548  ifstream reader(file, ios::in | ios::binary);
549 
550  if(reader) {
551 
552  // go to end to count length //
553  reader.seekg(0, ios::end);
554 
555  streamoff rpos = reader.tellg();
556 
557 
558  // cannot be loaded //
559  LEVIATHAN_ASSERT(std::numeric_limits<streamoff>::max() >= rpos, "file is too large");
560 
561  resultreceiver.resize(static_cast<size_t>(rpos));
562  // back to start //
563  reader.seekg(0, ios::beg);
564  // read the actual data //
565  reader.read(&resultreceiver[0], resultreceiver.size());
566 
567  // done, cleanup //
568  reader.close();
569  return true;
570  }
571 
572  return false;
573 }
static bool in(Reader::Char c, Reader::Char c1, Reader::Char c2, Reader::Char c3, Reader::Char c4)
Definition: jsoncpp.cpp:245

◆ ReadFileEntirely() [2/2]

DLLEXPORT bool Leviathan::FileSystem::ReadFileEntirely ( const std::wstring &  file,
std::wstring &  resultreceiver 
)
static

Definition at line 512 of file FileSystem.cpp.

514 {
515 #ifdef _WIN32
516  wifstream reader(file, ios::in | ios::binary);
517 #else
518  wifstream reader(Convert::Utf16ToUtf8(file), ios::in | ios::binary);
519 #endif
520  if(reader) {
521 
522  // go to end to count length //
523  reader.seekg(0, ios::end);
524 
525  streamoff rpos = reader.tellg();
526 
527  // cannot be loaded //
528  LEVIATHAN_ASSERT(std::numeric_limits<streamoff>::max() >= rpos, "file is too large");
529 
530  resultreceiver.resize(static_cast<size_t>(rpos));
531  // back to start //
532  reader.seekg(0, ios::beg);
533  // read the actual data //
534  reader.read(&resultreceiver[0], resultreceiver.size());
535 
536  // done, cleanup //
537  reader.close();
538  return true;
539  }
540 
541  return false;
542 }
static DLLEXPORT std::string Utf16ToUtf8(const std::wstring &utf16str)
Encodes an UTF8 string from a wide string (wstring/utf16)
Definition: Convert.cpp:108
static bool in(Reader::Char c, Reader::Char c1, Reader::Char c2, Reader::Char c3, Reader::Char c4)
Definition: jsoncpp.cpp:245

◆ RegisterExtension()

DLLEXPORT int Leviathan::FileSystem::RegisterExtension ( const std::string &  extension)

Reserves a number for an extension string.

Definition at line 624 of file FileSystem.cpp.

625 {
626  // check does it exist //
627  for(size_t i = 0; i < FileTypes.size(); i++) {
628  if(StringOperations::CompareInsensitive(FileTypes[i]->Name, extension))
629  return FileTypes[i]->ID;
630  }
631 
632  // add //
633  CurrentFileExtID++;
634  FileTypes.push_back(new FileTypeHolder(CurrentFileExtID, extension));
635 
636  return CurrentFileExtID;
637 }
static bool CompareInsensitive(const StringTypeN &data, const StringTypeN &second)

◆ RegisterOGREResourceGroups()

DLLEXPORT void Leviathan::FileSystem::RegisterOGREResourceGroups ( bool  testload = false)
static
Parameters
testloadIf true skips parsing scripts. Use only when unittesting

Definition at line 927 of file FileSystem.cpp.

928 {
929  // get the resource managing singleton //
930  Ogre::ResourceGroupManager& manager = Ogre::ResourceGroupManager::getSingleton();
931 
932  // Internal resources
933  manager.createResourceGroup("LeviathanInternal");
934  RegisterSubFolders(manager, "LeviathanInternal", "CoreOgreScripts");
935 
936  // All generated stuff can be dumped here
937  manager.createResourceGroup("Generated");
938 
939 
940  // Models folder //
941  manager.createResourceGroup("MainModelsFolder");
942  RegisterSubFolders(manager, "MainModelsFolder", DataFolder + ModelsFolder);
943 
944  // Textures folder //
945  manager.createResourceGroup("MainTexturesFolder");
946  RegisterSubFolders(manager, "MainTexturesFolder", DataFolder + TextureFolder);
947 
948  // Materials folder //
949  manager.createResourceGroup("MainMaterialsFolder");
950  RegisterSubFolders(manager, "MainMaterialsFolder", DataFolder + MaterialFolder);
951 
952  // Scripts folder //
953  manager.createResourceGroup("GuiScripts");
954  if(boost::filesystem::exists(DataFolder + ScriptsFolder + "GUI")) {
955  // Uppercase
956  RegisterSubFolders(manager, "GuiScripts", DataFolder + ScriptsFolder + "GUI");
957  } else {
958 
959  // Lowercase
960  RegisterSubFolders(manager, "GuiScripts", DataFolder + ScriptsFolder + "gui");
961  }
962 
963 
964  // shaders //
965  manager.createResourceGroup("ShadersFolder");
966  RegisterSubFolders(manager, "ShadersFolder", DataFolder + ShaderFolder);
967 
968  // // Terrain group //
969  // manager.createResourceGroup("Terrain");
970 
971  // folder = DataFolder+"Cache/Terrain/";
972 
973  // manager.addResourceLocation(folder, "FileSystem", "Terrain", true, false);
974 
975  // // add cache to general //
976  // folder = DataFolder+"Cache/";
977  // manager.addResourceLocation(folder, "FileSystem", "General");
978 
979 
980  // Fonts group //
981  manager.createResourceGroup("Fonts");
982  RegisterSubFolders(manager, "Fonts", DataFolder + FontFolder);
983 
984  // possibly register addon folders //
985 
986 
987  // initialize the groups //
988  if(testload) {
989 
990  LOG_INFO("FileSystem: RegisterOGREResourceGroups: doing test load, skipping parsing");
991 
992  } else {
993 
994  manager.initialiseAllResourceGroups(true);
995  }
996 
997  // load the groups //
998  // manager.loadResourceGroup("MainModelsFolder");
999 }
#define LOG_INFO(x)
Definition: Define.h:81
void RegisterSubFolders(Ogre::ResourceGroupManager &manager, const std::string &groupname, const Ogre::String &basefolder)
Ogre recursive resource groups don&#39;t work so we hack around it with this.
Definition: FileSystem.cpp:907

◆ RegisterOGREResourceLocation()

DLLEXPORT void Leviathan::FileSystem::RegisterOGREResourceLocation ( const std::string &  location)
static

Definition at line 1001 of file FileSystem.cpp.

1002 {
1003 
1004  Ogre::ResourceGroupManager& manager = Ogre::ResourceGroupManager::getSingleton();
1005 
1006  Ogre::String groupname = location + "_group";
1007 
1008  // check does it exist //
1009  auto groups = manager.getResourceGroups();
1010 
1011  for(size_t i = 0; i < groups.size(); i++) {
1012  if(groups[i] == groupname)
1013  return;
1014  }
1015 
1016  manager.createResourceGroup(groupname);
1017 
1018  manager.addResourceLocation(location, "FileSystem", groupname, true);
1019 
1020  manager.initialiseResourceGroup(groupname, true);
1021 }

◆ ReSearchFiles()

DLLEXPORT bool Leviathan::FileSystem::ReSearchFiles ( )

Destroys the current index and recreates it.

Definition at line 167 of file FileSystem.cpp.

168 {
169 
170  // Reset values //
171  CurrentFileExtID = 25;
172  IsSorted = false;
173 
174  SAFE_DELETE_VECTOR(FileTypes);
175 
176  AllFiles.clear();
177 
178  TextureFiles.clear();
179  ModelFiles.clear();
180  SoundFiles.clear();
181  ScriptFiles.clear();
182 
183  // Clear indexes //
184  IsAllIndexed = false;
185  SAFE_DELETE_VECTOR(AllIndexes);
186 
187  IsTextureIndexed = false;
188  SAFE_DELETE_VECTOR(TextureIndexes);
189 
190  IsModelIndexed = false;
191  SAFE_DELETE_VECTOR(ModelIndexes);
192 
193  IsSoundIndexed = false;
194  SAFE_DELETE_VECTOR(SoundIndexes);
195 
196  IsScriptIndexed = false;
197  SAFE_DELETE_VECTOR(ScriptIndexes);
198 
199  // Search again //
200  return Init(ErrorReporter);
201 }
#define SAFE_DELETE_VECTOR(x)
Definition: Define.h:123
DLLEXPORT bool Init(LErrorReporter *errorreport)
Runs the indexing and sorting.
Definition: FileSystem.cpp:96

◆ SearchForFile()

DLLEXPORT string Leviathan::FileSystem::SearchForFile ( FILEGROUP  which,
const std::string &  name,
const std::string &  extensions,
bool  searchall = true 
)

Searches for a file.

Returns
Full path to the file or an empty string if not found

Definition at line 672 of file FileSystem.cpp.

674 {
675  if(name.empty())
676  return "";
677 
678  // generate info about the search file //
679  vector<int> ExtensionIDS;
680  GetExtensionIDS(extensions, ExtensionIDS);
681 
682  switch(which) {
683  case FILEGROUP_MODEL: {
684  shared_ptr<FileDefinitionType> result =
685  _SearchForFileInVec(ModelFiles, ExtensionIDS, name, IsModelIndexed, &ModelIndexes);
686  if(result.get() != NULL) {
687  // found //
688  return result.get()->RelativePath;
689  }
690  } break;
691  case FILEGROUP_TEXTURE: {
692  shared_ptr<FileDefinitionType> result = _SearchForFileInVec(
693  TextureFiles, ExtensionIDS, name, IsTextureIndexed, &TextureIndexes);
694 
695  if(result.get() != NULL) {
696  // found //
697  return result.get()->RelativePath;
698  }
699  } break;
700  case FILEGROUP_SOUND: {
701  shared_ptr<FileDefinitionType> result =
702  _SearchForFileInVec(SoundFiles, ExtensionIDS, name, IsSoundIndexed, &SoundIndexes);
703  if(result.get() != NULL) {
704  // found //
705  return result.get()->RelativePath;
706  }
707  } break;
708  case FILEGROUP_SCRIPT: {
709  shared_ptr<FileDefinitionType> result = _SearchForFileInVec(
710  ScriptFiles, ExtensionIDS, name, IsScriptIndexed, &ScriptIndexes);
711  if(result.get() != NULL) {
712  // found //
713  return result.get()->RelativePath;
714  }
715  } break;
716  case FILEGROUP_OTHER: {
717  shared_ptr<FileDefinitionType> result =
718  _SearchForFileInVec(AllFiles, ExtensionIDS, name, IsAllIndexed, &AllIndexes);
719  if(result.get() != NULL) {
720  // found //
721  return result.get()->RelativePath;
722  }
723  } break;
724  }
725 
726 
727  // still not found, if searchall specified search all files vector //
728  if(searchall) {
729  shared_ptr<FileDefinitionType> result =
730  _SearchForFileInVec(AllFiles, ExtensionIDS, name, IsAllIndexed, &AllIndexes);
731  if(result.get() != NULL) {
732  // found //
733  return result.get()->RelativePath;
734  }
735  }
736  // not found return empty and if debug build warn //
737 
738  ErrorReporter->Error("FileSystem: File not found: " + name + "." + extensions);
739 
740  return "";
741 }
DLLEXPORT void GetExtensionIDS(const std::string &extensions, std::vector< int > &ids)
Returns list of matching ids to the extensions.
Definition: FileSystem.cpp:639
virtual void Error(const std::string &Text)=0

◆ SetDataFolder()

DLLEXPORT void Leviathan::FileSystem::SetDataFolder ( const std::string &  folder)
static

Definition at line 279 of file FileSystem.cpp.

280 {
281 
282  DataFolder = folder;
283 }

◆ SetModelsFolder()

void Leviathan::FileSystem::SetModelsFolder ( const std::string &  folder)
static

Definition at line 285 of file FileSystem.cpp.

286 {
287 
288  ModelsFolder = folder;
289 }

◆ SetScriptsFolder()

void Leviathan::FileSystem::SetScriptsFolder ( const std::string &  folder)
static

Definition at line 291 of file FileSystem.cpp.

292 {
293 
294  ScriptsFolder = folder;
295 }

◆ SetShaderFolder()

void Leviathan::FileSystem::SetShaderFolder ( const std::string &  folder)
static

Definition at line 297 of file FileSystem.cpp.

298 {
299 
300  ShaderFolder = folder;
301 }

◆ SetTextureFolder()

void Leviathan::FileSystem::SetTextureFolder ( const std::string &  folder)
static

Definition at line 303 of file FileSystem.cpp.

304 {
305 
306  TextureFolder = folder;
307 }

◆ SortFileVectors()

DLLEXPORT void Leviathan::FileSystem::SortFileVectors ( )

Definition at line 575 of file FileSystem.cpp.

576 {
577  // check if already sorted //
578  if(IsSorted)
579  return;
580 
581  ShouldSortStop = false;
582  // call sort on the vectors //
583  IsBeingSorted = true;
584 
585  // looping so that other thread can cancel the action before it is finished //
586  for(int i = 0; i < 5; i++) {
587  // switch on index and call std sort //
588  switch(i) {
589  case 0: sort(AllFiles.begin(), AllFiles.end(), FileDefSorter()); break;
590  case 1: sort(TextureFiles.begin(), TextureFiles.end(), FileDefSorter()); break;
591  case 2: sort(ModelFiles.begin(), ModelFiles.end(), FileDefSorter()); break;
592  case 3: sort(SoundFiles.begin(), SoundFiles.end(), FileDefSorter()); break;
593  case 4: sort(ScriptFiles.begin(), ScriptFiles.end(), FileDefSorter()); break;
594  }
595 
596  // check for end
597  if(ShouldSortStop) {
598  // asked to stop //
599  goto end;
600  }
601  }
602  // sort done
603  IsSorted = true;
604 
605 end:
606  IsBeingSorted = false;
607 }

◆ WriteToFile() [1/2]

bool Leviathan::FileSystem::WriteToFile ( const std::string &  data,
const std::string &  filename 
)
static

Definition at line 462 of file FileSystem.cpp.

463 {
464  ofstream file(filename, ios::binary);
465  if(file.is_open()) {
466 
467  file << data;
468 
469  file.close();
470  return true;
471  }
472 
473  file.close();
474  return false;
475 }

◆ WriteToFile() [2/2]

bool Leviathan::FileSystem::WriteToFile ( const std::wstring &  data,
const std::wstring &  filename 
)
static

Definition at line 477 of file FileSystem.cpp.

478 {
479 #ifdef _WIN32
480  wofstream file(filename, ios::binary);
481 #else
482  wofstream file(Convert::Utf16ToUtf8(filename), ios::binary);
483 #endif
484  if(file.is_open()) {
485  file << data;
486 
487  file.close();
488  return true;
489  }
490 
491  file.close();
492  return false;
493 }
static DLLEXPORT std::string Utf16ToUtf8(const std::wstring &utf16str)
Encodes an UTF8 string from a wide string (wstring/utf16)
Definition: Convert.cpp:108

The documentation for this class was generated from the following files: