diff --git a/src/client/component/filesystem.cpp b/src/client/component/filesystem.cpp index 8e74802a..fac0d881 100644 --- a/src/client/component/filesystem.cpp +++ b/src/client/component/filesystem.cpp @@ -11,6 +11,7 @@ #include #include #include +#include namespace filesystem { @@ -37,7 +38,7 @@ namespace filesystem initialized = true; - filesystem::register_path(L"" CLIENT_DATA_FOLDER); + filesystem::register_path(utils::properties::get_appdata_path() / CLIENT_DATA_FOLDER); filesystem::register_path(L"."); filesystem::register_path(L"h2-mod"); diff --git a/src/client/component/updater.cpp b/src/client/component/updater.cpp index aa157692..e3cbcfbf 100644 --- a/src/client/component/updater.cpp +++ b/src/client/component/updater.cpp @@ -19,6 +19,7 @@ #include #include #include +#include #define MASTER "https://master.fed0001.xyz/" @@ -63,17 +64,14 @@ namespace updater std::vector garbage_files{}; }; - utils::concurrency::container update_data; - // remove this at some point std::vector old_data_files = { - {"./data/ui_scripts"}, - {"./data/polrus"}, - {"./data/fonts"}, - {"./data/localizedstrings"}, + {"./cdata"}, }; + utils::concurrency::container update_data; + std::string select(const std::string& main, const std::string& develop) { if (GIT_BRANCH == "develop"s) @@ -84,6 +82,18 @@ namespace updater return main; } + std::string load_binary_name() + { + utils::nt::library self; + return self.get_name(); + } + + std::string get_binary_name() + { + static const auto name = load_binary_name(); + return name; + } + void notify(const std::string& name) { scheduler::once([=]() @@ -118,9 +128,22 @@ namespace updater bool check_file(const std::string& name, const std::string& sha) { std::string data; - if (!utils::io::read_file(name, &data)) + + if (get_binary_name() == name) { - return false; + if (!utils::io::read_file(name, &data)) + { + return false; + } + } + else + { + const auto appdata_folder = utils::properties::get_appdata_path(); + const auto path = (appdata_folder / name).generic_string(); + if (!utils::io::read_file(path, &data)) + { + return false; + } } if (utils::cryptography::sha1::compute(data, true) != sha) @@ -131,18 +154,6 @@ namespace updater return true; } - std::string load_binary_name() - { - utils::nt::library self; - return self.get_name(); - } - - std::string get_binary_name() - { - static const auto name = load_binary_name(); - return name; - } - std::string get_time_str() { return utils::string::va("%i", uint32_t(time(nullptr))); @@ -153,39 +164,6 @@ namespace updater return utils::http::get_data(MASTER + select(DATA_PATH, DATA_PATH_DEV) + name + "?" + get_time_str()); } - bool is_update_cancelled() - { - return update_data.access([](update_data_t& data_) - { - return data_.cancelled; - }); - } - - bool write_file(const std::string& name, const std::string& data) - { - if (get_binary_name() == name && - utils::io::file_exists(name) && - !utils::io::move_file(name, name + ".old")) - { - return false; - } - - return utils::io::write_file(name, data); - } - - void delete_old_file() - { - utils::io::remove_file(get_binary_name() + ".old"); - } - - void reset_data() - { - update_data.access([](update_data_t& data_) - { - data_ = {}; - }); - } - bool has_old_data_files() { bool has = false; @@ -208,23 +186,68 @@ namespace updater } } + bool is_update_cancelled() + { + return update_data.access([](update_data_t& data_) + { + return data_.cancelled; + }); + } + + bool write_file(const std::string& name, const std::string& data) + { + if (get_binary_name() == name && + utils::io::file_exists(name) && + !utils::io::move_file(name, name + ".old")) + { + return false; + } + + if (get_binary_name() == name) + { + return utils::io::write_file(name, data); + } + else + { + const auto appdata_folder = utils::properties::get_appdata_path(); + const auto path = (appdata_folder / name).generic_string(); + return utils::io::write_file(path, data); + } + } + + void delete_old_file() + { + utils::io::remove_file(get_binary_name() + ".old"); + } + + void reset_data() + { + update_data.access([](update_data_t& data_) + { + data_ = {}; + }); + } + std::vector find_garbage_files(const std::vector& update_files) { std::vector garbage_files{}; - if (!utils::io::directory_exists("cdata")) + const auto appdata_folder = utils::properties::get_appdata_path(); + const auto path = (appdata_folder / CLIENT_DATA_FOLDER).generic_string(); + if (!utils::io::directory_exists(path)) { return {}; } - const auto current_files = utils::io::list_files_recursively(CLIENT_DATA_FOLDER); + const auto current_files = utils::io::list_files_recursively(path); for (const auto& file : current_files) { bool found = false; for (const auto& update_file : update_files) { + const auto update_file_ = (appdata_folder / update_file).generic_string(); const auto path_a = std::filesystem::path(file); - const auto path_b = std::filesystem::path(update_file); + const auto path_b = std::filesystem::path(update_file_); const auto is_directory = utils::io::directory_exists(file); const auto compare = path_a.compare(path_b); diff --git a/src/client/main.cpp b/src/client/main.cpp index 0575b4f2..0c15e44b 100644 --- a/src/client/main.cpp +++ b/src/client/main.cpp @@ -7,6 +7,7 @@ #include #include #include +#include DECLSPEC_NORETURN void WINAPI exit_hook(const int code) { @@ -59,7 +60,7 @@ void apply_aslr_patch(std::string* data) void get_aslr_patched_binary(std::string* binary, std::string* data) { - const auto patched_binary = "h2_sp_patched.exe"s; + const auto patched_binary = (utils::properties::get_appdata_path() / "bin/h2_sp64_bnet_ship.exe"s).generic_string(); try { @@ -127,6 +128,7 @@ FARPROC load_binary(const launcher::mode mode) void remove_crash_file() { utils::io::remove_file("__h2Exe"); + utils::io::remove_file("h2_sp_patched.exe"); // remove this at some point } void verify_version() diff --git a/src/common/utils/properties.cpp b/src/common/utils/properties.cpp new file mode 100644 index 00000000..21926d0c --- /dev/null +++ b/src/common/utils/properties.cpp @@ -0,0 +1,24 @@ +#include "io.hpp" +#include "properties.hpp" +#include +#include + +namespace utils::properties +{ + std::filesystem::path get_appdata_path() + { + PWSTR path; + if (!SUCCEEDED(SHGetKnownFolderPath(FOLDERID_LocalAppData, 0, nullptr, &path))) + { + throw std::runtime_error("Failed to read APPDATA path!"); + } + + auto _ = gsl::finally([&path] + { + CoTaskMemFree(path); + }); + + static auto appdata = std::filesystem::path(path) / "h2-mod"; + return appdata; + } +} diff --git a/src/common/utils/properties.hpp b/src/common/utils/properties.hpp new file mode 100644 index 00000000..103e6ae6 --- /dev/null +++ b/src/common/utils/properties.hpp @@ -0,0 +1,6 @@ +#pragma once + +namespace utils::properties +{ + std::filesystem::path get_appdata_path(); +}