Refactor IO (#575)

* [IO]: Use filesystem where possible

* [Library]: One small thing
This commit is contained in:
Edo 2022-11-22 01:34:17 +01:00 committed by GitHub
parent 18cc5c3c2e
commit 6a8088281f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 39 additions and 46 deletions

View File

@ -408,7 +408,7 @@ namespace Components
{
std::string hash;
for(int i = 0; i < ARRAYSIZE(Maps::UserMapFiles); ++i)
for (std::size_t i = 0; i < ARRAYSIZE(Maps::UserMapFiles); ++i)
{
std::string filePath = Utils::String::VA("usermaps/%s/%s%s", map.data(), map.data(), Maps::UserMapFiles[i]);
if (Utils::IO::FileExists(filePath))

View File

@ -68,7 +68,7 @@ namespace Utils
return DeleteFileA(file.data()) == TRUE;
}
size_t FileSize(const std::string& file)
std::size_t FileSize(const std::string& file)
{
if (FileExists(file))
{
@ -77,7 +77,7 @@ namespace Utils
if (stream.good())
{
stream.seekg(0, std::ios::end);
return static_cast<size_t>(stream.tellg());
return static_cast<std::size_t>(stream.tellg());
}
}
@ -89,21 +89,21 @@ namespace Utils
return std::filesystem::create_directories(dir);
}
bool DirectoryExists(const std::string& directory)
bool DirectoryExists(const std::filesystem::path& directory)
{
return std::filesystem::is_directory(directory);
}
bool DirectoryIsEmpty(const std::string& directory)
bool DirectoryIsEmpty(const std::filesystem::path& directory)
{
return std::filesystem::is_empty(directory);
}
std::vector<std::string> ListFiles(const std::string& dir)
std::vector<std::string> ListFiles(const std::filesystem::path& directory)
{
std::vector<std::string> files;
for (auto& file : std::filesystem::directory_iterator(dir))
for (auto& file : std::filesystem::directory_iterator(directory))
{
files.push_back(file.path().generic_string());
}

View File

@ -1,18 +1,15 @@
#pragma once
namespace Utils
namespace Utils::IO
{
namespace IO
{
bool FileExists(const std::string& file);
bool WriteFile(const std::string& file, const std::string& data, bool append = false);
bool ReadFile(const std::string& file, std::string* data);
std::string ReadFile(const std::string& file);
bool RemoveFile(const std::string& file);
size_t FileSize(const std::string& file);
bool CreateDir(const std::string& dir);
bool DirectoryExists(const std::string& file);
bool DirectoryIsEmpty(const std::string& file);
std::vector<std::string> ListFiles(const std::string& dir);
}
bool FileExists(const std::string& file);
bool WriteFile(const std::string& file, const std::string& data, bool append = false);
bool ReadFile(const std::string& file, std::string* data);
std::string ReadFile(const std::string& file);
bool RemoveFile(const std::string& file);
std::size_t FileSize(const std::string& file);
bool CreateDir(const std::string& dir);
bool DirectoryExists(const std::filesystem::path& file);
bool DirectoryIsEmpty(const std::filesystem::path& file);
std::vector<std::string> ListFiles(const std::filesystem::path& directory);
}

View File

@ -2,14 +2,9 @@
namespace Utils
{
Library Library::Load(const std::string& name)
{
return Library(LoadLibraryA(name.data()));
}
Library Library::Load(const std::filesystem::path& path)
{
return Library::Load(path.generic_string());
return Library(LoadLibraryA(path.generic_string().data()));
}
Library Library::GetByAddress(void* address)
@ -66,31 +61,34 @@ namespace Utils
if (!this->isValid())
return {};
auto path = this->getPath();
const auto pos = path.find_last_of("/\\");
const auto path = this->getPath();
const auto generic_path = path.generic_string();
const auto pos = generic_path.find_last_of("/\\");
if (pos == std::string::npos)
return path;
{
return generic_path;
}
return path.substr(pos + 1);
return generic_path.substr(pos + 1);
}
std::string Library::getPath() const
std::filesystem::path Library::getPath() const
{
if (!this->isValid())
return {};
char name[MAX_PATH] = {0};
GetModuleFileNameA(this->module_, name, sizeof(name));
wchar_t name[MAX_PATH] = {0};
GetModuleFileNameW(this->module_, name, MAX_PATH);
return name;
return {name};
}
std::string Library::getFolder() const
std::filesystem::path Library::getFolder() const
{
if (!this->isValid())
return {};
const auto path = std::filesystem::path(this->getPath());
const auto path = this->getPath();
return path.parent_path().generic_string();
}

View File

@ -5,7 +5,6 @@ namespace Utils
class Library
{
public:
static Library Load(const std::string& name);
static Library Load(const std::filesystem::path& path);
static Library GetByAddress(void* address);
@ -21,23 +20,22 @@ namespace Utils
operator bool() const;
operator HMODULE() const;
bool isValid() const;
HMODULE getModule() const;
std::string getName() const;
std::string getPath() const;
std::string getFolder() const;
std::uint8_t* getPtr() const;
[[nodiscard]] bool isValid() const;
[[nodiscard]] HMODULE getModule() const;
[[nodiscard]] std::string getName() const;
[[nodiscard]] std::filesystem::path getPath() const;
[[nodiscard]] std::filesystem::path getFolder() const;
void free();
template <typename T>
T getProc(const std::string& process) const
[[nodiscard]] T getProc(const std::string& process) const
{
if (!this->isValid()) T{};
return reinterpret_cast<T>(GetProcAddress(this->module_, process.data()));
}
template <typename T>
std::function<T> get(const std::string& process) const
[[nodiscard]] std::function<T> get(const std::string& process) const
{
if (!this->isValid()) return std::function<T>();
return static_cast<T*>(this->getProc<void*>(process));