From 341c93b0fb4aedd262581ce6e81b700a9bdc1423 Mon Sep 17 00:00:00 2001 From: Aki Date: Thu, 14 Apr 2022 00:28:37 +0200 Subject: Switched to use new Sources everywhere applicable in the Loader --- StarsEx/DataLoader.cpp | 294 ++++++++++-------------------------------------- StarsEx/DataLoader.h | 7 +- StarsEx/ModInfo.cpp | 13 ++- StarsEx/ModInfo.h | 1 + StarsEx/ShipDesign.cpp | 8 +- StarsEx/ShipDesign.h | 2 +- StarsEx/Skin.cpp | 13 +-- StarsEx/Skin.h | 6 +- StarsEx/Starshatter.cpp | 8 +- 9 files changed, 80 insertions(+), 272 deletions(-) (limited to 'StarsEx') diff --git a/StarsEx/DataLoader.cpp b/StarsEx/DataLoader.cpp index 487dead..ac230db 100644 --- a/StarsEx/DataLoader.cpp +++ b/StarsEx/DataLoader.cpp @@ -33,9 +33,9 @@ DataLoader::DataLoader() : DataLoader::~DataLoader() { - archives.destroy(); sources.destroy(); - delete work_directory_source; + if (work_directory_source) + delete work_directory_source; } // +--------------------------------------------------------------------+ @@ -52,8 +52,10 @@ void DataLoader::Close() { Bitmap::ClearCache(); - delete loader; - loader = nullptr; + if (loader) { + delete loader; + loader = nullptr; + } } void @@ -133,67 +135,6 @@ DataLoader::UnmountSource(int src) // +--------------------------------------------------------------------+ -int -DataLoader::EnableDatafile(const char* name) -{ - int status = FAILED; - - FILE* f; - fopen_s(&f, name, "rb"); - - if (f) { - ::fclose(f); - - DataArchive* a = new DataArchive(name); - - if (a && a->NumFiles() >= 1) { - status = 0; - - bool found = false; - ListIter iter = archives; - while (++iter && !found) { - DataArchive* archive = iter.value(); - if (!strcmp(archive->Name(), name)) { - found = true; - } - } - - if (!found) - archives.append(a); - } - else { - last_error = Text::format("Invalid datafile '%s'", name); - status = FAILED; - delete a; - } - - loader = this; - } - else { - last_error = Text::format("Could not open datafile '%s'", name); - status = FAILED; - } - - return status; -} - -int -DataLoader::DisableDatafile(const char* name) -{ - ListIter iter = archives; - while (++iter) { - DataArchive* a = iter.value(); - if (!strcmp(a->Name(), name)) { - delete iter.removeItem(); - return 0; - } - } - - return FAILED; -} - -// +--------------------------------------------------------------------+ - void DataLoader::SetDataPath(const char* path) { @@ -208,27 +149,16 @@ DataLoader::SetDataPath(const char* path) bool DataLoader::FindFile(const char* name) { - // assemble file name: - char filename[1024]; - strcpy_s(filename, datapath); - strcat_s(filename, name); - - // first check current directory: if (work_directory_source) { - bool found = work_directory_source->Find(datapath, name); - if (found) + if (work_directory_source->Find(datapath, name)) return true; } - - // then check datafiles, from last to first: - int narchives = archives.size(); - for (int i = 0; i < narchives; i++) { - DataArchive* a = archives[narchives-1-i]; - if (a->FindEntry(filename) > -1) { + for (int i = sources.size() - 1; i >= 0; i--) { + DataSource* src = sources[i]; + if (src->Find(datapath, name)) { return true; } } - return false; } @@ -238,141 +168,50 @@ int DataLoader::ListFiles(const char* filter, List& list, bool recurse) { list.destroy(); - - ListFileSystem(filter, list, datapath, recurse); - - // then check datafile(s): - int narchives = archives.size(); - for (int i = 0; i < narchives; i++) { - DataArchive* a = archives[narchives-1-i]; - ListArchiveFiles(a->Name(), filter, list); + if (work_directory_source) + work_directory_source->ListFiles(datapath, filter, list, recurse); + for (int i = sources.size() - 1; i >= 0; i--) { + DataSource* source = sources[i]; + source->ListFiles(datapath, filter, list, recurse); } - return list.size(); } int -DataLoader::ListArchiveFiles(const char* archive_name, const char* filter, List &list) +DataLoader::ListArchiveFiles(int src, const char* filter, List &list) { - int pathlen = datapath.length(); - DataArchive* a = 0; - - if (archive_name) { - int narchives = archives.size(); - for (int i = 0; i < narchives && !a; i++) { - a = archives[narchives-1-i]; - - if (_stricmp(a->Name(), archive_name)) - a = 0; - } - } - - if (!a) { - ListFileSystem(filter, list, datapath, true); + if (src < 0) { + if (work_directory_source) + work_directory_source->ListFiles(datapath, filter, list, true); return list.size(); } - - if (!strcmp(filter, "*.*")) { - int count = a->NumFiles(); - for (int n = 0; n < count; n++) { - DataEntry* entry = a->GetFile(n); - Text entry_name = entry->name; - entry_name.setSensitive(false); - - if (entry_name.contains(datapath)) { - Text fname = entry_name(pathlen, 1000); - - if (!list.contains(&fname)) - list.append(new Text(fname)); - } - } - } - else { - char data_filter[256]; - ZeroMemory(data_filter, 256); - - const char* pf = filter; - char* pdf = data_filter; - - while (*pf) { - if (*pf != '*') - *pdf++ = *pf; - pf++; - } - - int count = a->NumFiles(); - for (int n = 0; n < count; n++) { - DataEntry* entry = a->GetFile(n); - Text entry_name = entry->name; - entry_name.setSensitive(false); - - if (entry_name.contains(datapath) && entry_name.contains(data_filter)) { - Text fname = entry_name(pathlen, 1000); - - if (!list.contains(&fname)) - list.append(new Text(fname)); - } - } + ListIter iter = sources; + while (++iter) { + if (iter.value()->Id() == src) + return iter.value()->ListFiles(datapath, filter, list, true); } - return list.size(); } // +--------------------------------------------------------------------+ -void -DataLoader::ListFileSystem(const char* filter, List& list, Text base_path, bool recurse) -{ - if (work_directory_source) { - work_directory_source->ListFiles(base_path, filter, list, recurse); - } -} - -// +--------------------------------------------------------------------+ - int DataLoader::LoadBuffer(const char* name, BYTE*& buf, bool null_terminate, bool optional) { - buf = 0; - - // assemble file name: - char filename[1024]; - strcpy_s(filename, datapath); - strcat_s(filename, name); - + buf = nullptr; if (work_directory_source) { int len = work_directory_source->Load(datapath, name, buf, null_terminate); if (len > 0) return len; - if (buf) - delete buf; } - - // then check datafile(s): - int narchives = archives.size(); - - // vox files are usually in their own archive, - // so check there first - if (narchives > 1 && strstr(filename, "Vox")) { - for (int i = 0; i < narchives; i++) { - DataArchive* a = archives[narchives-1-i]; - if (strstr(a->Name(), "vox")) { - int index = a->FindEntry(filename); - if (index > -1) - return a->ExpandEntry(index, buf, null_terminate); - } - } - } - - for (int i = 0; i < narchives; i++) { - DataArchive* a = archives[narchives-1-i]; - int index = a->FindEntry(filename); - if (index > -1) - return a->ExpandEntry(index, buf, null_terminate); + for (int i = sources.size() - 1; i >= 0; i--) { + DataSource* source = sources[i]; + int len = source->Load(datapath, name, buf, null_terminate); + if (len > 0) + return len; } - if (!optional) - Print("WARNING - DataLoader could not load buffer '%s'\n", filename); + Print("WARNING - DataLoader could not load buffer '%s' '%s'\n", datapath.data(), name); return 0; } @@ -563,27 +402,20 @@ DataLoader::LoadIndexed(const char* name, Bitmap& bitmap, int type) } if (!loaded) { - // then check datafile: - int len = 0; - BYTE* tmp_buf = 0; - - int narchives = archives.size(); - for (int i = 0; i < narchives; i++) { - DataArchive* a = archives[narchives-1-i]; - int index = a->FindEntry(filename); - if (index > -1) { - len = a->ExpandEntry(index, tmp_buf); - + std::uint8_t* tmp_buf = nullptr; + for (int i = sources.size() - 1; i >= 0; i--) { + DataSource* source = sources[i]; + const int len = source->Load(datapath, name, tmp_buf); + if (len > 0) { if (pcx_file) - pcx.LoadBuffer(tmp_buf, len); - + pcx.LoadBuffer(tmp_buf, len); else - d3dx.LoadBuffer(tmp_buf, len); - - ReleaseBuffer(tmp_buf); + d3dx.LoadBuffer(tmp_buf, len); break; } } + if (tmp_buf) + ReleaseBuffer(tmp_buf); } // now copy the image into the bitmap: @@ -659,26 +491,20 @@ DataLoader::LoadHiColor(const char* name, Bitmap& bitmap, int type) } if (!loaded) { - // then check datafile: - int len = 0; - BYTE* tmp_buf = 0; - - int narchives = archives.size(); - for (int i = 0; i < narchives; i++) { - DataArchive* a = archives[narchives-1-i]; - int index = a->FindEntry(filename); - if (index > -1) { - len = a->ExpandEntry(index, tmp_buf); - + std::uint8_t* tmp_buf = nullptr; + for (int i = sources.size() - 1; i >= 0; i--) { + DataSource* source = sources[i]; + const int len = source->Load(datapath, name2, tmp_buf); + if (len > 0) { if (pcx_file) - pcx.LoadBuffer(tmp_buf, len); + pcx.LoadBuffer(tmp_buf, len); else - d3dx.LoadBuffer(tmp_buf, len); - - ReleaseBuffer(tmp_buf); + d3dx.LoadBuffer(tmp_buf, len); break; } } + if (tmp_buf) + ReleaseBuffer(tmp_buf); } // now copy the image into the bitmap: @@ -742,26 +568,20 @@ DataLoader::LoadAlpha(const char* name, Bitmap& bitmap, int type) } if (!loaded) { - // then check datafile: - int len = 0; - BYTE* tmp_buf = 0; - - int narchives = archives.size(); - for (int i = 0; i < narchives; i++) { - DataArchive* a = archives[narchives-1-i]; - int index = a->FindEntry(filename); - if (index > -1) { - len = a->ExpandEntry(index, tmp_buf); - + std::uint8_t* tmp_buf = nullptr; + for (int i = sources.size() - 1; i >= 0; i--) { + DataSource* source = sources[i]; + const int len = source->Load(datapath, name2, tmp_buf); + if (len > 0) { if (pcx_file) - pcx.LoadBuffer(tmp_buf, len); + pcx.LoadBuffer(tmp_buf, len); else - d3dx.LoadBuffer(tmp_buf, len); - - ReleaseBuffer(tmp_buf); + d3dx.LoadBuffer(tmp_buf, len); break; } } + if (tmp_buf) + ReleaseBuffer(tmp_buf); } // now copy the alpha values into the bitmap: diff --git a/StarsEx/DataLoader.h b/StarsEx/DataLoader.h index 820224a..a518a67 100644 --- a/StarsEx/DataLoader.h +++ b/StarsEx/DataLoader.h @@ -47,9 +47,6 @@ public: int MountDatafile(const char* name, Group group=Group::DEFAULT, int pos=-1); int UnmountSource(int src); - int EnableDatafile(const char* name); - int DisableDatafile(const char* name); - void SetDataPath(const char* path); const char* GetDataPath() const { return datapath; } @@ -58,7 +55,7 @@ public: bool FindFile(const char* fname); int ListFiles(const char* filter, List& list, bool recurse=false); - int ListArchiveFiles(const char* archive, const char* filter, List& list); + int ListArchiveFiles(int src, const char* filter, List& list); int LoadBuffer(const char* name, BYTE*& buf, bool null_terminate=false, bool optional=false); int LoadBitmap(const char* name, Bitmap& bmp, int type=0, bool optional=false); int CacheBitmap(const char* name, Bitmap*& bmp, int type=0, bool optional=false); @@ -74,7 +71,6 @@ private: int LoadHiColor(const char* name, Bitmap& bmp, int type); int LoadAlpha( const char* name, Bitmap& bmp, int type); - void ListFileSystem(const char* filter, List& list, Text base_path, bool recurse); int LoadPartialFile(const char* fname, BYTE*& buf, int max_load, bool optional=false); int LoadOggStream(const char* fname, Sound*& snd); @@ -83,7 +79,6 @@ private: Text last_error; - List archives; List sources; DataSource* work_directory_source; static DataLoader* loader; diff --git a/StarsEx/ModInfo.cpp b/StarsEx/ModInfo.cpp index 0c75ed1..07e6466 100644 --- a/StarsEx/ModInfo.cpp +++ b/StarsEx/ModInfo.cpp @@ -25,11 +25,11 @@ // +-------------------------------------------------------------------+ ModInfo::ModInfo() -: logo(0), distribute(false), enabled(false), catalog(0) +: logo(0), distribute(false), enabled(false), source(-1), catalog(0) { } ModInfo::ModInfo(const char* fname) -: logo(0), distribute(false), enabled(false), catalog(0) +: logo(0), distribute(false), enabled(false), source(-1), catalog(0) { if (fname && *fname) { Load(fname); @@ -37,7 +37,7 @@ ModInfo::ModInfo(const char* fname) } ModInfo::ModInfo(const char* n, const char* v, const char* u) -: name(n), logo(0), version(v), url(u), distribute(false), enabled(false), catalog(0) +: name(n), logo(0), version(v), url(u), distribute(false), enabled(false), source(-1), catalog(0) { } ModInfo::~ModInfo() @@ -257,13 +257,13 @@ ModInfo::Enable() DataLoader* loader = DataLoader::GetLoader(); if (loader && !enabled) { - loader->EnableDatafile(filename); + source = loader->MountDatafile(filename, DataLoader::Group::MOD); enabled = true; if (catalog) ShipDesign::LoadCatalog(catalog->Path(), catalog->File(), true); - ShipDesign::LoadSkins("/Mods/Skins/", filename); + ShipDesign::LoadSkins("/Mods/Skins/", source); for (int i = 0; i < campaigns.size(); i++) { ModCampaign* c = campaigns[i]; @@ -280,7 +280,8 @@ ModInfo::Disable() DataLoader* loader = DataLoader::GetLoader(); if (loader) { - loader->DisableDatafile(filename); + loader->UnmountSource(source); + source = -1; enabled = false; } diff --git a/StarsEx/ModInfo.h b/StarsEx/ModInfo.h index 3ee2054..4e1ff47 100644 --- a/StarsEx/ModInfo.h +++ b/StarsEx/ModInfo.h @@ -71,6 +71,7 @@ private: Text version; bool distribute; bool enabled; + int source; List campaigns; ModCatalog* catalog; diff --git a/StarsEx/ShipDesign.cpp b/StarsEx/ShipDesign.cpp index cda8013..9cb2fa0 100644 --- a/StarsEx/ShipDesign.cpp +++ b/StarsEx/ShipDesign.cpp @@ -749,7 +749,7 @@ ShipDesign::LoadCatalog(const char* path, const char* fname, bool mod) // +--------------------------------------------------------------------+ void -ShipDesign::LoadSkins(const char* path, const char* archive) +ShipDesign::LoadSkins(const char* path, int source) { // Load MOD Skin Files: List list; @@ -758,7 +758,7 @@ ShipDesign::LoadSkins(const char* path, const char* archive) loader->UseFileSystem(true); loader->SetDataPath(path); - loader->ListArchiveFiles(archive, "*.def", list); + loader->ListArchiveFiles(source, "*.def", list); ListIter iter = list; while (++iter) { @@ -813,7 +813,7 @@ ShipDesign::LoadSkins(const char* path, const char* archive) Skin* skin = design->ParseSkin(val); if (skin) - skin->SetPath(archive); + skin->SetSource(source); } } } @@ -998,7 +998,7 @@ ShipDesign::ClearModCatalog() while (++iter) { Skin* skin = iter.value(); - if (*skin->Path()) + if (skin->Source() >= 0) iter.removeItem(); } } diff --git a/StarsEx/ShipDesign.h b/StarsEx/ShipDesign.h index c0159b0..80d8ea4 100644 --- a/StarsEx/ShipDesign.h +++ b/StarsEx/ShipDesign.h @@ -132,7 +132,7 @@ public: static const char* ClassName(int type); static int LoadCatalog(const char* path, const char* file, bool mod=false); - static void LoadSkins(const char* path, const char* archive=0); + static void LoadSkins(const char* path, int source=-1); static void PreloadCatalog(int index=-1); static int StandardCatalogSize(); diff --git a/StarsEx/Skin.cpp b/StarsEx/Skin.cpp index 62363c5..b5bd8ab 100644 --- a/StarsEx/Skin.cpp +++ b/StarsEx/Skin.cpp @@ -27,8 +27,6 @@ Skin::Skin(const char* n) else { ZeroMemory(name, NAMELEN); } - - ZeroMemory(path, 256); } // +--------------------------------------------------------------------+ @@ -50,16 +48,9 @@ Skin::SetName(const char* n) } void -Skin::SetPath(const char* n) +Skin::SetSource(int src) { - if (n && *n) { - strncpy_s(path, n, 256); - path[255] = 0; - } - - else { - ZeroMemory(path, 256); - } + source = src; } // +--------------------------------------------------------------------+ diff --git a/StarsEx/Skin.h b/StarsEx/Skin.h index 25c5a75..a0478f4 100644 --- a/StarsEx/Skin.h +++ b/StarsEx/Skin.h @@ -46,17 +46,17 @@ public: // accessors / mutators const char* Name() const { return name; } - const char* Path() const { return path; } int NumCells() const { return cells.size(); } + int Source() const { return source; } void SetName(const char* n); - void SetPath(const char* n); + void SetSource(int src); void AddMaterial(const Material* mtl); protected: char name[NAMELEN]; - char path[256]; List cells; + int source; }; // +--------------------------------------------------------------------+ diff --git a/StarsEx/Starshatter.cpp b/StarsEx/Starshatter.cpp index dc60533..129efa9 100644 --- a/StarsEx/Starshatter.cpp +++ b/StarsEx/Starshatter.cpp @@ -169,7 +169,7 @@ chat_mode(0), exit_time(1.2), cutscene(0) DataLoader::Initialize(); loader = DataLoader::GetLoader(); - int loadstat = loader->EnableDatafile("shatter.dat"); + int loadstat = loader->MountDatafile("shatter.dat", DataLoader::Group::BASE); if (loadstat == DataLoader::FAILED) { ::MessageBox(hwnd, loader->LastError(), "Starshatter - Error", MB_OK); @@ -178,13 +178,13 @@ chat_mode(0), exit_time(1.2), cutscene(0) } if (loader->FindFile("vox.dat")) - loader->EnableDatafile("vox.dat"); + loader->MountDatafile("vox.dat", DataLoader::Group::BASE); if (loader->FindFile("start.dat")) - loader->EnableDatafile("start.dat"); + loader->MountDatafile("start.dat", DataLoader::Group::BASE); if (loader->FindFile("content.dat")) - loader->EnableDatafile("content.dat"); + loader->MountDatafile("content.dat", DataLoader::Group::BASE); LoadVideoConfig("video.cfg"); -- cgit v1.1