Merge pull request #422 from diamante0018/cleanup-filesystem

[Filesystem] Cleanup
This commit is contained in:
Edo 2022-08-12 10:28:39 +02:00 committed by GitHub
commit d79b3710a4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 36 additions and 34 deletions

View File

@ -15,21 +15,23 @@ namespace Components
int handle; int handle;
const auto len = Game::FS_FOpenFileReadForThread(filePath.data(), &handle, thread); const auto len = Game::FS_FOpenFileReadForThread(filePath.data(), &handle, thread);
if (handle) if (!handle)
{ {
auto* buf = AllocateFile(len + 1); return;
[[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);
} }
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() void FileSystem::RawFile::read()
@ -200,7 +202,7 @@ namespace Components
return fileList; 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 }; char path[MAX_PATH] = { 0 };
Game::FS_BuildPathToFile(Dvar::Var("fs_basepath").get<const char*>(), reinterpret_cast<char*>(0x63D0BB8), Utils::String::VA("%s/%s", folder.data(), file.data()), reinterpret_cast<char**>(&path)); Game::FS_BuildPathToFile(Dvar::Var("fs_basepath").get<const char*>(), reinterpret_cast<char*>(0x63D0BB8), Utils::String::VA("%s/%s", folder.data(), file.data()), reinterpret_cast<char**>(&path));

View File

@ -8,7 +8,7 @@ namespace Components
class AbstractFile class AbstractFile
{ {
public: public:
virtual ~AbstractFile() {}; virtual ~AbstractFile() = default;
virtual bool exists() = 0; virtual bool exists() = 0;
virtual std::string getName() = 0; virtual std::string getName() = 0;
@ -19,12 +19,12 @@ namespace Components
{ {
public: public:
File() = default; File() = default;
File(std::string file) : filePath{std::move(file)} { this->read(); }; 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, Game::FsThread thread) : filePath{std::move(file)} { this->read(thread); }
bool exists() override { return !this->buffer.empty(); }; bool exists() override { return !this->buffer.empty(); }
std::string getName() override { return this->filePath; }; std::string getName() override { return this->filePath; }
std::string& getBuffer() override { return this->buffer; }; std::string& getBuffer() override { return this->buffer; }
private: private:
std::string filePath; std::string filePath;
@ -36,12 +36,12 @@ namespace Components
class RawFile : public AbstractFile class RawFile : public AbstractFile
{ {
public: public:
RawFile() {}; RawFile() = default;
RawFile(const std::string& file) : filePath(file) { this->read(); }; RawFile(std::string file) : filePath(std::move(file)) { this->read(); }
bool exists() override { return !this->buffer.empty(); }; bool exists() override { return !this->buffer.empty(); }
std::string getName() override { return this->filePath; }; std::string getName() override { return this->filePath; }
std::string& getBuffer() override { return this->buffer; }; std::string& getBuffer() override { return this->buffer; }
private: private:
std::string filePath; std::string filePath;
@ -53,7 +53,7 @@ namespace Components
class FileReader class FileReader
{ {
public: public:
FileReader() : handle(0), size(-1), name() {}; FileReader() : handle(0), size(-1), name() {}
FileReader(const std::string& file); FileReader(const std::string& file);
~FileReader(); ~FileReader();
@ -73,8 +73,8 @@ namespace Components
class FileWriter class FileWriter
{ {
public: public:
FileWriter(const std::string& file, bool append = false) : handle(0), filePath(file) { this->open(append); }; FileWriter(std::string file, bool append = false) : handle(0), filePath(std::move(file)) { this->open(append); }
~FileWriter() { this->close(); }; ~FileWriter() { this->close(); }
void write(const std::string& data); void write(const std::string& data);
@ -92,7 +92,7 @@ namespace Components
static std::filesystem::path GetAppdataPath(); static std::filesystem::path GetAppdataPath();
static std::vector<std::string> GetFileList(const std::string& path, const std::string& extension); static std::vector<std::string> GetFileList(const std::string& path, const std::string& extension);
static std::vector<std::string> GetSysFileList(const std::string& path, const std::string& extension, bool folders = false); static std::vector<std::string> 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: private:
static std::mutex Mutex; static std::mutex Mutex;

View File

@ -226,8 +226,8 @@ namespace Components
Logger::Print("Deleting demo {}...\n", info.name); Logger::Print("Deleting demo {}...\n", info.name);
FileSystem::DeleteFile("demos", info.name + ".dm_13"); FileSystem::_DeleteFile("demos", info.name + ".dm_13");
FileSystem::DeleteFile("demos", info.name + ".dm_13.json"); FileSystem::_DeleteFile("demos", info.name + ".dm_13.json");
// Reset our ui_demo_* dvars here, because the theater menu needs it. // Reset our ui_demo_* dvars here, because the theater menu needs it.
Dvar::Var("ui_demo_mapname").set(""); Dvar::Var("ui_demo_mapname").set("");
@ -310,8 +310,8 @@ namespace Components
for (int i = 0; i < numDel; ++i) for (int i = 0; i < numDel; ++i)
{ {
Logger::Print("Deleting old demo {}\n", files[i]); Logger::Print("Deleting old demo {}\n", files[i]);
FileSystem::DeleteFile("demos", files[i].data()); FileSystem::_DeleteFile("demos", files[i].data());
FileSystem::DeleteFile("demos", Utils::String::VA("%s.json", 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); Command::Execute(Utils::String::VA("record auto_%lld", time(nullptr)), true);

View File

@ -40,7 +40,7 @@ namespace Components
assert(dvar); assert(dvar);
assert(parsedValue); 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); Logger::Print("Overriding '{}' from '{}'\n", dvar->name, filename);
// Successfully found and tried to apply the string value to the dvar // Successfully found and tried to apply the string value to the dvar