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 65 of file FileSystem.h.

Constructor & Destructor Documentation

◆ FileSystem()

Leviathan::FileSystem::FileSystem ( )

Definition at line 33 of file FileSystem.cpp.

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

◆ ~FileSystem()

Leviathan::FileSystem::~FileSystem ( )

Definition at line 45 of file FileSystem.cpp.

46 {
47 
48  // Helps catch errors with tests etc.
49  if(Staticaccess == this)
50  Staticaccess = nullptr;
51 
52  SAFE_DELETE_VECTOR(FileTypes);
53 
54  AllFiles.clear();
55 
56  TextureFiles.clear();
57  ModelFiles.clear();
58  SoundFiles.clear();
59  ScriptFiles.clear();
60 
61  // Clear indexes //
62  IsAllIndexed = false;
63  SAFE_DELETE_VECTOR(AllIndexes);
64 
65  IsTextureIndexed = false;
66  SAFE_DELETE_VECTOR(TextureIndexes);
67 
68  IsModelIndexed = false;
69  SAFE_DELETE_VECTOR(ModelIndexes);
70 
71  IsSoundIndexed = false;
72  SAFE_DELETE_VECTOR(SoundIndexes);
73 
74  IsScriptIndexed = false;
75  SAFE_DELETE_VECTOR(ScriptIndexes);
76 }
#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 494 of file FileSystem.cpp.

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

◆ CreateIndexesForVecs()

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

Definition at line 608 of file FileSystem.cpp.

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

◆ DoesExtensionMatch()

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

Definition at line 244 of file FileSystem.cpp.

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

◆ FileExists()

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

Definition at line 455 of file FileSystem.cpp.

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

◆ 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 742 of file FileSystem.cpp.

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

◆ Get()

DLLEXPORT FileSystem * FileSystem::Get ( )
static

Definition at line 78 of file FileSystem.cpp.

79 {
80 
81  return Staticaccess;
82 }

◆ GetAllFiles()

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

Definition at line 800 of file FileSystem.cpp.

801 {
802  return AllFiles;
803 }

◆ GetDataFolder()

string Leviathan::FileSystem::GetDataFolder ( )
static

Definition at line 202 of file FileSystem.cpp.

203 {
204 
205  return (DataFolder);
206 }

◆ 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 638 of file FileSystem.cpp.

639 {
640  // generate info about the extensions //
641  vector<string> Exts;
642  StringOperations::CutString(extensions, string("|"), Exts);
643  if(Exts.size() == 0) {
644  // just one extension //
645  ids.push_back(RegisterExtension(extensions));
646  return;
647  }
648 
649  for(size_t i = 0; i < Exts.size(); i++) {
650  ids.push_back(RegisterExtension(Exts[i]));
651  }
652 }
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:623

◆ 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 654 of file FileSystem.cpp.

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

◆ GetFileLength()

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

Definition at line 435 of file FileSystem.cpp.

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

◆ 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 388 of file FileSystem.cpp.

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

◆ GetFontFolder()

string Leviathan::FileSystem::GetFontFolder ( )
static

Definition at line 232 of file FileSystem.cpp.

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

◆ GetModelFiles()

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

Definition at line 790 of file FileSystem.cpp.

791 {
792  return ModelFiles;
793 }

◆ GetModelsFolder()

string Leviathan::FileSystem::GetModelsFolder ( )
static

Definition at line 208 of file FileSystem.cpp.

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

◆ GetScriptFiles()

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

Definition at line 805 of file FileSystem.cpp.

806 {
807  return ScriptFiles;
808 }

◆ GetScriptsFolder()

string Leviathan::FileSystem::GetScriptsFolder ( )
static

Definition at line 214 of file FileSystem.cpp.

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

◆ GetShaderFolder()

string Leviathan::FileSystem::GetShaderFolder ( )
static

Definition at line 220 of file FileSystem.cpp.

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

◆ GetSoundFiles()

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

Definition at line 795 of file FileSystem.cpp.

796 {
797  return SoundFiles;
798 }

◆ GetSoundFolder()

string Leviathan::FileSystem::GetSoundFolder ( )
static

Definition at line 238 of file FileSystem.cpp.

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

◆ GetTextureFolder()

string Leviathan::FileSystem::GetTextureFolder ( )
static

Definition at line 226 of file FileSystem.cpp.

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

◆ Init()

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

Runs the indexing and sorting.

Definition at line 95 of file FileSystem.cpp.

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

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

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

◆ ReadFileEntirely() [2/2]

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

Definition at line 511 of file FileSystem.cpp.

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

◆ RegisterExtension()

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

Reserves a number for an extension string.

Definition at line 623 of file FileSystem.cpp.

624 {
625  // check does it exist //
626  for(size_t i = 0; i < FileTypes.size(); i++) {
627  if(StringOperations::CompareInsensitive(FileTypes[i]->Name, extension))
628  return FileTypes[i]->ID;
629  }
630 
631  // add //
632  CurrentFileExtID++;
633  FileTypes.push_back(new FileTypeHolder(CurrentFileExtID, extension));
634 
635  return CurrentFileExtID;
636 }
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 937 of file FileSystem.cpp.

938 {
939  // get the resource managing singleton //
940  Ogre::ResourceGroupManager& manager = Ogre::ResourceGroupManager::getSingleton();
941 
942  // Internal resources
943  manager.createResourceGroup("LeviathanInternal");
944  RegisterSubFolders(manager, "LeviathanInternal", "CoreOgreScripts");
945 
946  // All generated stuff can be dumped here
947  manager.createResourceGroup("Generated");
948 
949 
950  // Models folder //
951  manager.createResourceGroup("MainModelsFolder");
952  RegisterSubFolders(manager, "MainModelsFolder", DataFolder + ModelsFolder);
953 
954  // Textures folder //
955  manager.createResourceGroup("MainTexturesFolder");
956  RegisterSubFolders(manager, "MainTexturesFolder", DataFolder + TextureFolder);
957 
958  // Materials folder //
959  manager.createResourceGroup("MainMaterialsFolder");
960  RegisterSubFolders(manager, "MainMaterialsFolder", DataFolder + MaterialFolder);
961 
962  // Scripts folder //
963 
964  // Only one of these is registered
965  if(!RegisterIfExists(manager, "GuiScripts", DataFolder + ScriptsFolder + "GUI")) {
966  RegisterIfExists(manager, "GuiScripts", DataFolder + ScriptsFolder + "gui");
967  }
968 
969  // shaders //
970  manager.createResourceGroup("ShadersFolder");
971  RegisterSubFolders(manager, "ShadersFolder", DataFolder + ShaderFolder);
972 
973  // // Terrain group //
974  // manager.createResourceGroup("Terrain");
975 
976  // folder = DataFolder+"Cache/Terrain/";
977 
978  // manager.addResourceLocation(folder, "FileSystem", "Terrain", true, false);
979 
980  // // add cache to general //
981  // folder = DataFolder+"Cache/";
982  // manager.addResourceLocation(folder, "FileSystem", "General");
983 
984 
985  // Fonts group //
986  manager.createResourceGroup("Fonts");
987  RegisterSubFolders(manager, "Fonts", DataFolder + FontFolder);
988 
989  // possibly register addon folders //
990 
991 
992  // initialize the groups //
993  if(testload) {
994 
995  LOG_INFO("FileSystem: RegisterOGREResourceGroups: doing test load, skipping parsing");
996 
997  } else {
998 
999  manager.initialiseAllResourceGroups(true);
1000  }
1001 
1002  // load the groups //
1003  // manager.loadResourceGroup("MainModelsFolder");
1004 }
bool RegisterIfExists(Ogre::ResourceGroupManager &manager, const std::string &groupname, const Ogre::String &basefolder)
Definition: FileSystem.cpp:926
#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:906

◆ RegisterOGREResourceLocation()

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

Definition at line 1006 of file FileSystem.cpp.

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

◆ ReSearchFiles()

DLLEXPORT bool Leviathan::FileSystem::ReSearchFiles ( )

Destroys the current index and recreates it.

Definition at line 166 of file FileSystem.cpp.

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

◆ 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 671 of file FileSystem.cpp.

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

◆ SetDataFolder()

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

Definition at line 278 of file FileSystem.cpp.

279 {
280 
281  DataFolder = folder;
282 }

◆ SetModelsFolder()

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

Definition at line 284 of file FileSystem.cpp.

285 {
286 
287  ModelsFolder = folder;
288 }

◆ SetScriptsFolder()

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

Definition at line 290 of file FileSystem.cpp.

291 {
292 
293  ScriptsFolder = folder;
294 }

◆ SetShaderFolder()

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

Definition at line 296 of file FileSystem.cpp.

297 {
298 
299  ShaderFolder = folder;
300 }

◆ SetTextureFolder()

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

Definition at line 302 of file FileSystem.cpp.

303 {
304 
305  TextureFolder = folder;
306 }

◆ SortFileVectors()

DLLEXPORT void Leviathan::FileSystem::SortFileVectors ( )

Definition at line 574 of file FileSystem.cpp.

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

◆ WriteToFile() [1/2]

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

Definition at line 461 of file FileSystem.cpp.

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

◆ WriteToFile() [2/2]

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

Definition at line 476 of file FileSystem.cpp.

477 {
478 #ifdef _WIN32
479  wofstream file(filename, ios::binary);
480 #else
481  wofstream file(Convert::Utf16ToUtf8(filename), ios::binary);
482 #endif
483  if(file.is_open()) {
484  file << data;
485 
486  file.close();
487  return true;
488  }
489 
490  file.close();
491  return false;
492 }
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: