diff options
author | Aki <please@ignore.pl> | 2022-04-14 00:28:37 +0200 |
---|---|---|
committer | Aki <please@ignore.pl> | 2022-04-14 00:28:37 +0200 |
commit | 341c93b0fb4aedd262581ce6e81b700a9bdc1423 (patch) | |
tree | fcade8d6163d000e534134e32b64db126d766c9b /StarsEx/DataLoader.cpp | |
parent | 92de53ac4747fe3caf2ee4c4fa3dcb2ec86a5ef2 (diff) | |
download | starshatter-341c93b0fb4aedd262581ce6e81b700a9bdc1423.zip starshatter-341c93b0fb4aedd262581ce6e81b700a9bdc1423.tar.gz starshatter-341c93b0fb4aedd262581ce6e81b700a9bdc1423.tar.bz2 |
Switched to use new Sources everywhere applicable in the Loader
Diffstat (limited to 'StarsEx/DataLoader.cpp')
-rw-r--r-- | StarsEx/DataLoader.cpp | 294 |
1 files changed, 57 insertions, 237 deletions
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<DataArchive> 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<DataArchive> 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<Text>& 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<Text> &list) +DataLoader::ListArchiveFiles(int src, const char* filter, List<Text> &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<DataSource> 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<Text>& 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: |