refactor(io): use the filesystem library where appropiate

This commit is contained in:
FutureRave 2022-11-21 18:35:15 +00:00
parent cae0793e7f
commit 337b52891c
10 changed files with 60 additions and 62 deletions

View File

@ -153,7 +153,7 @@ namespace steam_proxy
const auto self = utils::nt::library::get_by_address(start_mod_unsafe); const auto self = utils::nt::library::get_by_address(start_mod_unsafe);
const auto path = self.get_path(); const auto path = self.get_path();
const std::string cmdline = utils::string::va("\"%s\" -proc %d", path.data(), GetCurrentProcessId()); const auto* cmdline = utils::string::va("\"%s\" -proc %d", path.generic_string().data(), GetCurrentProcessId());
steam::game_id game_id; steam::game_id game_id;
game_id.raw.type = 1; // k_EGameIDTypeGameMod game_id.raw.type = 1; // k_EGameIDTypeGameMod
@ -162,7 +162,7 @@ namespace steam_proxy
const auto* mod_id = "bo3"; const auto* mod_id = "bo3";
game_id.raw.mod_id = *reinterpret_cast<const unsigned int*>(mod_id) | 0x80000000; game_id.raw.mod_id = *reinterpret_cast<const unsigned int*>(mod_id) | 0x80000000;
client_user.invoke<bool>("SpawnProcess", path.data(), cmdline.data(), our_directory, client_user.invoke<bool>("SpawnProcess", path.generic_string().data(), cmdline, our_directory,
&game_id.bits, title.data(), 0, 0, 0); &game_id.bits, title.data(), 0, 0, 0);
return ownership_state::success; return ownership_state::success;

View File

@ -42,7 +42,7 @@ namespace updater
std::string get_self_file() std::string get_self_file()
{ {
const auto self = utils::nt::library::get_by_address(get_self_file); const auto self = utils::nt::library::get_by_address(get_self_file);
return self.get_path(); return self.get_path().generic_string();
} }
std::string get_leftover_file() std::string get_leftover_file()

View File

@ -83,7 +83,7 @@ namespace
auto game_path = std::filesystem::path(game_file); auto game_path = std::filesystem::path(game_file);
game_path.replace_extension(".start"); game_path.replace_extension(".start");
utils::io::remove_file(game_path.generic_string()); utils::io::remove_file(game_path);
} }
PIMAGE_TLS_CALLBACK* get_tls_callbacks() PIMAGE_TLS_CALLBACK* get_tls_callbacks()

View File

@ -32,7 +32,7 @@ namespace utils
return value->second; return value->second;
} }
return ""; return {};
} }
void info_string::parse(std::string buffer) void info_string::parse(std::string buffer)

View File

@ -4,9 +4,9 @@
namespace utils::io namespace utils::io
{ {
bool remove_file(const std::string& file) bool remove_file(const std::filesystem::path& file)
{ {
if(DeleteFileA(file.data()) != FALSE) if(DeleteFileW(file.wstring().data()) != FALSE)
{ {
return true; return true;
} }
@ -14,9 +14,9 @@ namespace utils::io
return GetLastError() == ERROR_FILE_NOT_FOUND; return GetLastError() == ERROR_FILE_NOT_FOUND;
} }
bool move_file(const std::string& src, const std::string& target) bool move_file(const std::filesystem::path& src, const std::filesystem::path& target)
{ {
return MoveFileA(src.data(), target.data()) == TRUE; return MoveFileW(src.wstring().data(), target.wstring().data()) == TRUE;
} }
bool file_exists(const std::string& file) bool file_exists(const std::string& file)
@ -37,7 +37,7 @@ namespace utils::io
if (stream.is_open()) if (stream.is_open())
{ {
stream.write(data.data(), data.size()); stream.write(data.data(), static_cast<std::streamsize>(data.size()));
stream.close(); stream.close();
return true; return true;
} }
@ -68,8 +68,8 @@ namespace utils::io
if (size > -1) if (size > -1)
{ {
data->resize(static_cast<uint32_t>(size)); data->resize(static_cast<std::uint32_t>(size));
stream.read(const_cast<char*>(data->data()), size); stream.read(data->data(), size);
stream.close(); stream.close();
return true; return true;
} }
@ -78,7 +78,7 @@ namespace utils::io
return false; return false;
} }
size_t file_size(const std::string& file) std::size_t file_size(const std::string& file)
{ {
if (file_exists(file)) if (file_exists(file))
{ {
@ -87,29 +87,29 @@ namespace utils::io
if (stream.good()) if (stream.good())
{ {
stream.seekg(0, std::ios::end); stream.seekg(0, std::ios::end);
return static_cast<size_t>(stream.tellg()); return static_cast<std::size_t>(stream.tellg());
} }
} }
return 0; return 0;
} }
bool create_directory(const std::string& directory) bool create_directory(const std::filesystem::path& directory)
{ {
return std::filesystem::create_directories(directory); return std::filesystem::create_directories(directory);
} }
bool directory_exists(const std::string& directory) bool directory_exists(const std::filesystem::path& directory)
{ {
return std::filesystem::is_directory(directory); return std::filesystem::is_directory(directory);
} }
bool directory_is_empty(const std::string& directory) bool directory_is_empty(const std::filesystem::path& directory)
{ {
return std::filesystem::is_empty(directory); return std::filesystem::is_empty(directory);
} }
std::vector<std::string> list_files(const std::string& directory) std::vector<std::string> list_files(const std::filesystem::path& directory)
{ {
std::vector<std::string> files; std::vector<std::string> files;

View File

@ -6,16 +6,16 @@
namespace utils::io namespace utils::io
{ {
bool remove_file(const std::string& file); bool remove_file(const std::filesystem::path& file);
bool move_file(const std::string& src, const std::string& target); bool move_file(const std::filesystem::path& src, const std::filesystem::path& target);
bool file_exists(const std::string& file); bool file_exists(const std::string& file);
bool write_file(const std::string& file, const std::string& data, bool append = false); bool write_file(const std::string& file, const std::string& data, bool append = false);
bool read_file(const std::string& file, std::string* data); bool read_file(const std::string& file, std::string* data);
std::string read_file(const std::string& file); std::string read_file(const std::string& file);
size_t file_size(const std::string& file); size_t file_size(const std::string& file);
bool create_directory(const std::string& directory); bool create_directory(const std::filesystem::path& directory);
bool directory_exists(const std::string& directory); bool directory_exists(const std::filesystem::path& directory);
bool directory_is_empty(const std::string& directory); bool directory_is_empty(const std::filesystem::path& directory);
std::vector<std::string> list_files(const std::string& directory); std::vector<std::string> list_files(const std::filesystem::path& directory);
void copy_folder(const std::filesystem::path& src, const std::filesystem::path& target); void copy_folder(const std::filesystem::path& src, const std::filesystem::path& target);
} }

View File

@ -121,28 +121,28 @@ namespace utils::nt
std::string library::get_name() const std::string library::get_name() const
{ {
if (!this->is_valid()) return ""; if (!this->is_valid()) return {};
auto path = this->get_path(); const auto path = this->get_path();
const auto pos = path.find_last_of("/\\"); const auto pos = path.generic_string().find_last_of("/\\");
if (pos == std::string::npos) return path; if (pos == std::string::npos) return path.generic_string();
return path.substr(pos + 1); return path.generic_string().substr(pos + 1);
} }
std::string library::get_path() const std::filesystem::path library::get_path() const
{ {
if (!this->is_valid()) return ""; if (!this->is_valid()) return {};
char name[MAX_PATH] = {0}; wchar_t name[MAX_PATH] = {0};
GetModuleFileNameA(this->module_, name, sizeof name); GetModuleFileNameW(this->module_, name, MAX_PATH);
return name; return {name};
} }
std::string library::get_folder() const std::filesystem::path library::get_folder() const
{ {
if (!this->is_valid()) return ""; if (!this->is_valid()) return {};
const auto path = std::filesystem::path(this->get_path()); const auto path = std::filesystem::path(this->get_path());
return path.parent_path().generic_string(); return path.parent_path().generic_string();
@ -265,7 +265,7 @@ namespace utils::nt
GetCurrentDirectoryA(sizeof(current_dir), current_dir); GetCurrentDirectoryA(sizeof(current_dir), current_dir);
auto* const command_line = GetCommandLineA(); auto* const command_line = GetCommandLineA();
CreateProcessA(self.get_path().data(), command_line, nullptr, nullptr, false, NULL, nullptr, current_dir, CreateProcessA(self.get_path().generic_string().data(), command_line, nullptr, nullptr, false, NULL, nullptr, current_dir,
&startup_info, &process_info); &startup_info, &process_info);
if (process_info.hThread && process_info.hThread != INVALID_HANDLE_VALUE) CloseHandle(process_info.hThread); if (process_info.hThread && process_info.hThread != INVALID_HANDLE_VALUE) CloseHandle(process_info.hThread);

View File

@ -41,27 +41,27 @@ namespace utils::nt
operator HMODULE() const; operator HMODULE() const;
void unprotect() const; void unprotect() const;
void* get_entry_point() const; [[nodiscard]] void* get_entry_point() const;
size_t get_relative_entry_point() const; [[nodiscard]] size_t get_relative_entry_point() const;
bool is_valid() const; [[nodiscard]] bool is_valid() const;
std::string get_name() const; [[nodiscard]] std::string get_name() const;
std::string get_path() const; [[nodiscard]] std::filesystem::path get_path() const;
std::string get_folder() const; [[nodiscard]] std::filesystem::path get_folder() const;
std::uint8_t* get_ptr() const; [[nodiscard]] std::uint8_t* get_ptr() const;
void free(); void free();
HMODULE get_handle() const; [[nodiscard]] HMODULE get_handle() const;
template <typename T> template <typename T>
T get_proc(const std::string& process) const [[nodiscard]] T get_proc(const std::string& process) const
{ {
if (!this->is_valid()) T{}; if (!this->is_valid()) T{};
return reinterpret_cast<T>(GetProcAddress(this->module_, process.data())); return reinterpret_cast<T>(GetProcAddress(this->module_, process.data()));
} }
template <typename T> template <typename T>
std::function<T> get(const std::string& process) const [[nodiscard]] std::function<T> get(const std::string& process) const
{ {
if (!this->is_valid()) return std::function<T>(); if (!this->is_valid()) return std::function<T>();
return static_cast<T*>(this->get_proc<void*>(process)); return static_cast<T*>(this->get_proc<void*>(process));
@ -91,13 +91,13 @@ namespace utils::nt
return T(); return T();
} }
std::vector<PIMAGE_SECTION_HEADER> get_section_headers() const; [[nodiscard]] std::vector<PIMAGE_SECTION_HEADER> get_section_headers() const;
PIMAGE_NT_HEADERS get_nt_headers() const; [[nodiscard]] PIMAGE_NT_HEADERS get_nt_headers() const;
PIMAGE_DOS_HEADER get_dos_header() const; [[nodiscard]] PIMAGE_DOS_HEADER get_dos_header() const;
PIMAGE_OPTIONAL_HEADER get_optional_header() const; [[nodiscard]] PIMAGE_OPTIONAL_HEADER get_optional_header() const;
void** get_iat_entry(const std::string& module_name, const std::string& proc_name) const; [[nodiscard]] void** get_iat_entry(const std::string& module_name, const std::string& proc_name) const;
private: private:
HMODULE module_; HMODULE module_;

View File

@ -36,9 +36,9 @@ namespace utils::string
std::string to_lower(std::string text) std::string to_lower(std::string text)
{ {
std::transform(text.begin(), text.end(), text.begin(), [](const char input) std::transform(text.begin(), text.end(), text.begin(), [](const unsigned char input)
{ {
return static_cast<char>(tolower(input)); return static_cast<char>(std::tolower(input));
}); });
return text; return text;
@ -46,9 +46,9 @@ namespace utils::string
std::string to_upper(std::string text) std::string to_upper(std::string text)
{ {
std::transform(text.begin(), text.end(), text.begin(), [](const char input) std::transform(text.begin(), text.end(), text.begin(), [](const unsigned char input)
{ {
return static_cast<char>(toupper(input)); return static_cast<char>(std::toupper(input));
}); });
return text; return text;
@ -105,12 +105,12 @@ namespace utils::string
return {}; return {};
} }
void strip(const char* in, char* out, int max) void strip(const char* in, char* out, size_t max)
{ {
if (!in || !out) return; if (!in || !out) return;
max--; max--;
auto current = 0; size_t current = 0;
while (*in != 0 && current < max) while (*in != 0 && current < max)
{ {
const auto color_index = (*(in + 1) - 48) >= 0xC ? 7 : (*(in + 1) - 48); const auto color_index = (*(in + 1) - 48) >= 0xC ? 7 : (*(in + 1) - 48);
@ -128,11 +128,10 @@ namespace utils::string
++in; ++in;
} }
*out = '\0'; *out = '\0';
} }
#pragma warning(push)
#pragma warning(disable: 4100)
std::string convert(const std::wstring& wstr) std::string convert(const std::wstring& wstr)
{ {
std::string result; std::string result;
@ -158,7 +157,6 @@ namespace utils::string
return result; return result;
} }
#pragma warning(pop)
std::string replace(std::string str, const std::string& from, const std::string& to) std::string replace(std::string str, const std::string& from, const std::string& to)
{ {

View File

@ -91,7 +91,7 @@ namespace utils::string
std::string get_clipboard_data(); std::string get_clipboard_data();
void strip(const char* in, char* out, int max); void strip(const char* in, char* out, size_t max);
std::string convert(const std::wstring& wstr); std::string convert(const std::wstring& wstr);
std::wstring convert(const std::string& str); std::wstring convert(const std::string& str);