From 7b1b135e3f66f82ef8ea76b1cbb8f5d414311fbd Mon Sep 17 00:00:00 2001 From: Diavolo Date: Thu, 11 Aug 2022 12:31:19 +0200 Subject: [PATCH 1/2] [Filesystem] Cleanup --- src/Components/Modules/FileSystem.cpp | 28 ++++++++++++++------------- src/Components/Modules/FileSystem.hpp | 28 +++++++++++++-------------- 2 files changed, 29 insertions(+), 27 deletions(-) diff --git a/src/Components/Modules/FileSystem.cpp b/src/Components/Modules/FileSystem.cpp index d8b61308..00408819 100644 --- a/src/Components/Modules/FileSystem.cpp +++ b/src/Components/Modules/FileSystem.cpp @@ -15,21 +15,23 @@ namespace Components int handle; const auto len = Game::FS_FOpenFileReadForThread(filePath.data(), &handle, thread); - if (handle) + if (!handle) { - auto* buf = AllocateFile(len + 1); - - [[maybe_unused]] auto bytesRead = Game::FS_Read(buf, len, handle); - - assert(bytesRead == len); - - buf[len] = '\0'; - - Game::FS_FCloseFile(handle); - - this->buffer.append(buf, len); - FreeFile(buf); + return; } + + auto* buf = AllocateFile(len + 1); + + [[maybe_unused]] auto bytesRead = Game::FS_Read(buf, len, handle); + + assert(bytesRead == len); + + buf[len] = '\0'; + + Game::FS_FCloseFile(handle); + + this->buffer.append(buf, len); + FreeFile(buf); } void FileSystem::RawFile::read() diff --git a/src/Components/Modules/FileSystem.hpp b/src/Components/Modules/FileSystem.hpp index aca27f7a..542e68e7 100644 --- a/src/Components/Modules/FileSystem.hpp +++ b/src/Components/Modules/FileSystem.hpp @@ -8,7 +8,7 @@ namespace Components class AbstractFile { public: - virtual ~AbstractFile() {}; + virtual ~AbstractFile() = default; virtual bool exists() = 0; virtual std::string getName() = 0; @@ -19,12 +19,12 @@ namespace Components { public: File() = default; - File(std::string file) : filePath{std::move(file)} { this->read(); }; - File(std::string file, Game::FsThread thread) : filePath{std::move(file)} { this->read(thread); }; + File(std::string file) : filePath{std::move(file)} { this->read(); } + File(std::string file, Game::FsThread thread) : filePath{std::move(file)} { this->read(thread); } - bool exists() override { return !this->buffer.empty(); }; - std::string getName() override { return this->filePath; }; - std::string& getBuffer() override { return this->buffer; }; + bool exists() override { return !this->buffer.empty(); } + std::string getName() override { return this->filePath; } + std::string& getBuffer() override { return this->buffer; } private: std::string filePath; @@ -36,12 +36,12 @@ namespace Components class RawFile : public AbstractFile { public: - RawFile() {}; - RawFile(const std::string& file) : filePath(file) { this->read(); }; + RawFile() = default; + RawFile(std::string file) : filePath(std::move(file)) { this->read(); } - bool exists() override { return !this->buffer.empty(); }; - std::string getName() override { return this->filePath; }; - std::string& getBuffer() override { return this->buffer; }; + bool exists() override { return !this->buffer.empty(); } + std::string getName() override { return this->filePath; } + std::string& getBuffer() override { return this->buffer; } private: std::string filePath; @@ -53,7 +53,7 @@ namespace Components class FileReader { public: - FileReader() : handle(0), size(-1), name() {}; + FileReader() : handle(0), size(-1), name() {} FileReader(const std::string& file); ~FileReader(); @@ -73,8 +73,8 @@ namespace Components class FileWriter { public: - FileWriter(const std::string& file, bool append = false) : handle(0), filePath(file) { this->open(append); }; - ~FileWriter() { this->close(); }; + FileWriter(std::string file, bool append = false) : handle(0), filePath(std::move(file)) { this->open(append); } + ~FileWriter() { this->close(); } void write(const std::string& data); From b8d4ff6e2606c8688f16e0e6f172795f2eefe953 Mon Sep 17 00:00:00 2001 From: Diavolo Date: Thu, 11 Aug 2022 12:44:03 +0200 Subject: [PATCH 2/2] [Filesystem] Fix macro --- src/Components/Modules/FileSystem.cpp | 2 +- src/Components/Modules/FileSystem.hpp | 2 +- src/Components/Modules/Theatre.cpp | 8 ++++---- src/Components/Modules/VisionFile.cpp | 2 +- 4 files changed, 7 insertions(+), 7 deletions(-) diff --git a/src/Components/Modules/FileSystem.cpp b/src/Components/Modules/FileSystem.cpp index 00408819..ab6970f2 100644 --- a/src/Components/Modules/FileSystem.cpp +++ b/src/Components/Modules/FileSystem.cpp @@ -202,7 +202,7 @@ namespace Components return fileList; } - bool FileSystem::DeleteFile(const std::string& folder, const std::string& file) + bool FileSystem::_DeleteFile(const std::string& folder, const std::string& file) { char path[MAX_PATH] = { 0 }; Game::FS_BuildPathToFile(Dvar::Var("fs_basepath").get(), reinterpret_cast(0x63D0BB8), Utils::String::VA("%s/%s", folder.data(), file.data()), reinterpret_cast(&path)); diff --git a/src/Components/Modules/FileSystem.hpp b/src/Components/Modules/FileSystem.hpp index 542e68e7..2a1064f5 100644 --- a/src/Components/Modules/FileSystem.hpp +++ b/src/Components/Modules/FileSystem.hpp @@ -92,7 +92,7 @@ namespace Components static std::filesystem::path GetAppdataPath(); static std::vector GetFileList(const std::string& path, const std::string& extension); static std::vector GetSysFileList(const std::string& path, const std::string& extension, bool folders = false); - static bool DeleteFile(const std::string& folder, const std::string& file); + static bool _DeleteFile(const std::string& folder, const std::string& file); private: static std::mutex Mutex; diff --git a/src/Components/Modules/Theatre.cpp b/src/Components/Modules/Theatre.cpp index e7d0fa41..99a645be 100644 --- a/src/Components/Modules/Theatre.cpp +++ b/src/Components/Modules/Theatre.cpp @@ -226,8 +226,8 @@ namespace Components Logger::Print("Deleting demo {}...\n", info.name); - FileSystem::DeleteFile("demos", info.name + ".dm_13"); - FileSystem::DeleteFile("demos", info.name + ".dm_13.json"); + FileSystem::_DeleteFile("demos", info.name + ".dm_13"); + FileSystem::_DeleteFile("demos", info.name + ".dm_13.json"); // Reset our ui_demo_* dvars here, because the theater menu needs it. Dvar::Var("ui_demo_mapname").set(""); @@ -310,8 +310,8 @@ namespace Components for (int i = 0; i < numDel; ++i) { Logger::Print("Deleting old demo {}\n", files[i]); - FileSystem::DeleteFile("demos", files[i].data()); - FileSystem::DeleteFile("demos", Utils::String::VA("%s.json", files[i].data())); + FileSystem::_DeleteFile("demos", files[i].data()); + FileSystem::_DeleteFile("demos", Utils::String::VA("%s.json", files[i].data())); } Command::Execute(Utils::String::VA("record auto_%lld", time(nullptr)), true); diff --git a/src/Components/Modules/VisionFile.cpp b/src/Components/Modules/VisionFile.cpp index 1a1eb6da..04ef18aa 100644 --- a/src/Components/Modules/VisionFile.cpp +++ b/src/Components/Modules/VisionFile.cpp @@ -40,7 +40,7 @@ namespace Components assert(dvar); assert(parsedValue); - Game::Dvar_SetFromStringFromSource(dvar, parsedValue, Game::DvarSetSource::DVAR_SOURCE_INTERNAL); + Game::Dvar_SetFromStringFromSource(dvar, parsedValue, Game::DVAR_SOURCE_INTERNAL); Logger::Print("Overriding '{}' from '{}'\n", dvar->name, filename); // Successfully found and tried to apply the string value to the dvar