From 94ca59386cb94877ea15856a3c17c116c756a16d Mon Sep 17 00:00:00 2001 From: Aki Date: Sun, 10 Apr 2022 22:27:26 +0200 Subject: Switched to use FileSystemDataSource in DataLoader --- StarsEx/DataLoader.cpp | 128 +++++++++++++------------------------------------ StarsEx/DataLoader.h | 4 +- StarsEx/DataSource.cpp | 29 +++++++---- StarsEx/DataSource.h | 2 +- 4 files changed, 56 insertions(+), 107 deletions(-) (limited to 'StarsEx') diff --git a/StarsEx/DataLoader.cpp b/StarsEx/DataLoader.cpp index 1dcbe0e..024a9b3 100644 --- a/StarsEx/DataLoader.cpp +++ b/StarsEx/DataLoader.cpp @@ -8,6 +8,7 @@ */ #include "DataLoader.h" +#include "DataSource.h" #include "Archive.h" #include "Color.h" #include "D3DXImage.h" @@ -24,8 +25,11 @@ DataLoader* DataLoader::loader = 0; // +--------------------------------------------------------------------+ -DataLoader::DataLoader() - : datapath(""), video(0), use_file_system(true), enable_media(true) +DataLoader::DataLoader() : + datapath(""), + video(nullptr), + enable_media(true), + work_directory_source(new FileSystemDataSource()) { } @@ -72,7 +76,15 @@ DataLoader::Reset() void DataLoader::UseFileSystem(bool use) { - use_file_system = use; + if (work_directory_source) { + if (!use) { + delete work_directory_source; + work_directory_source = nullptr; + } + } + else if (use) { + work_directory_source = new FileSystemDataSource(); + } } void @@ -196,14 +208,12 @@ DataLoader::FindFile(const char* name) strcat_s(filename, name); // first check current directory: - if (use_file_system) { - FILE* f; - ::fopen_s(&f, filename, "rb"); - - if (f) { - ::fclose(f); + if (work_directory_source) { + work_directory_source->SetPrefix(datapath); + bool found = work_directory_source->Find(name); + work_directory_source->SetPrefix(); + if (found) return true; - } } // then check datafiles, from last to first: @@ -309,61 +319,10 @@ DataLoader::ListArchiveFiles(const char* archive_name, const char* filter, List< void DataLoader::ListFileSystem(const char* filter, List& list, Text base_path, bool recurse) { - if (use_file_system) { - char data_filter[256]; - ZeroMemory(data_filter, 256); - - const char* pf = filter; - char* pdf = data_filter; - - while (*pf) { - if (*pf != '*') - *pdf++ = *pf; - pf++; - } - - int pathlen = base_path.length(); - - // assemble win32 find filter: - char win32_filter[1024]; - strcpy_s(win32_filter, datapath); - - if (recurse) - strcat_s(win32_filter, "*.*"); - else - strcat_s(win32_filter, filter); - - // first check current directory: - WIN32_FIND_DATA data; - HANDLE hFind = FindFirstFile(win32_filter, &data); - - if (hFind != INVALID_HANDLE_VALUE) { - do { - if ((data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) != 0) { - if (recurse && data.cFileName[0] != '.') { - Text old_datapath = datapath; - - Text newpath = datapath; - newpath += data.cFileName; - newpath += "/"; - datapath = newpath; - - ListFileSystem(filter, list, base_path, recurse); - - datapath = old_datapath; - } - } - else { - if (!strcmp(filter, "*.*") || strstr(data.cFileName, data_filter)) { - Text full_name = datapath; - full_name += data.cFileName; - - list.append(new Text(full_name(pathlen, 1000))); - } - } - } - while (FindNextFile(hFind, &data)); - } + if (work_directory_source) { + work_directory_source->SetPrefix(base_path); + work_directory_source->ListFiles(filter, list, recurse); + work_directory_source->SetPrefix(); } } @@ -379,33 +338,14 @@ DataLoader::LoadBuffer(const char* name, BYTE*& buf, bool null_terminate, bool o strcpy_s(filename, datapath); strcat_s(filename, name); - if (use_file_system) { - // first check current directory: - FILE* f; - ::fopen_s(&f, filename, "rb"); - - if (f) { - ::fseek(f, 0, SEEK_END); - int len = ftell(f); - ::fseek(f, 0, SEEK_SET); - - if (null_terminate) { - buf = new BYTE[len+1]; - if (buf) - buf[len] = 0; - } - - else { - buf = new BYTE[len]; - } - - if (buf) - ::fread(buf, len, 1, f); - - ::fclose(f); - + if (work_directory_source) { + work_directory_source->SetPrefix(datapath); + int len = work_directory_source->Load(name, buf, null_terminate); + work_directory_source->SetPrefix(); + if (len > 0) return len; - } + if (buf) + delete buf; } // then check datafile(s): @@ -614,7 +554,7 @@ DataLoader::LoadIndexed(const char* name, Bitmap& bitmap, int type) // first try to load from current directory: bool loaded = false; - if (use_file_system) { + if (work_directory_source) { if (pcx_file) loaded = pcx.Load(filename) == PCX_OK; @@ -710,7 +650,7 @@ DataLoader::LoadHiColor(const char* name, Bitmap& bitmap, int type) // first try to load from current directory: bool loaded = false; - if (use_file_system) { + if (work_directory_source) { if (pcx_file) loaded = pcx.Load(filename) == PCX_OK; @@ -793,7 +733,7 @@ DataLoader::LoadAlpha(const char* name, Bitmap& bitmap, int type) // first try to load from current directory: bool loaded = false; - if (use_file_system) { + if (work_directory_source) { if (pcx_file) loaded = pcx.Load(filename) == PCX_OK; diff --git a/StarsEx/DataLoader.h b/StarsEx/DataLoader.h index 81098e8..af8b3d5 100644 --- a/StarsEx/DataLoader.h +++ b/StarsEx/DataLoader.h @@ -52,7 +52,7 @@ public: void SetDataPath(const char* path); const char* GetDataPath() const { return datapath; } - bool IsFileSystemEnabled() const { return use_file_system; } + bool IsFileSystemEnabled() const { return work_directory_source != nullptr; } bool IsMediaLoadEnabled() const { return enable_media; } bool FindFile(const char* fname); @@ -79,11 +79,11 @@ private: Text datapath; Video* video; - bool use_file_system; bool enable_media; List archives; List sources; + DataSource* work_directory_source; static DataLoader* loader; }; diff --git a/StarsEx/DataSource.cpp b/StarsEx/DataSource.cpp index b587971..f352e05 100644 --- a/StarsEx/DataSource.cpp +++ b/StarsEx/DataSource.cpp @@ -13,6 +13,7 @@ #include "Archive.h" #include "List.h" #include "Text.h" +#include "Utils.h" DataSource::DataSource() : @@ -120,19 +121,27 @@ FileSystemDataSource::ListFiles(Text filter, List& items, bool recurse) co std::filesystem::path full_path {m_path}; full_path.append(m_prefix.data()); filter = filter.replace("*", ""); - const auto check = [&items, &filter](const std::filesystem::directory_entry& entry){ - const auto filename = entry.path().filename().string(); + const auto prefix = full_path.string().length(); + const auto check = [&](const std::filesystem::directory_entry& entry){ + const auto filename = entry.path().filename().string(); // more of a reason to switch to string soon const auto index = filename.find(filter.data()); - if (index != decltype(filename)::npos) - items.append(new Text(entry.path().string().c_str())); + if (index != decltype(filename)::npos) { + Text path = entry.path().string().c_str(); + items.append(new Text(path.substring(prefix, path.length()))); + } }; - if (recurse) { - for (const auto& entry : std::filesystem::recursive_directory_iterator(full_path)) - check(entry); + try { + if (recurse) { + for (const auto& entry : std::filesystem::recursive_directory_iterator(full_path)) + check(entry); + } + else { + for (const auto& entry : std::filesystem::directory_iterator(full_path)) + check(entry); + } } - else { - for (const auto& entry : std::filesystem::directory_iterator(full_path)) - check(entry); + catch (const std::filesystem::filesystem_error& err) { + Print("FS::ListFiles: %s\n", err.what()); } return items.size(); } diff --git a/StarsEx/DataSource.h b/StarsEx/DataSource.h index 3da58ca..3c4a84d 100644 --- a/StarsEx/DataSource.h +++ b/StarsEx/DataSource.h @@ -49,7 +49,7 @@ protected: class FileSystemDataSource : public DataSource { public: - explicit FileSystemDataSource(const char* path=""); + explicit FileSystemDataSource(const char* path="."); ~FileSystemDataSource() override; bool Find(const char* name) const override; -- cgit v1.1