Use appdata folder for patched binary/update

This commit is contained in:
Federico Cecchetto 2022-08-27 16:16:49 +02:00
parent bb6d7db67c
commit e08791440d
5 changed files with 114 additions and 58 deletions

View File

@ -11,6 +11,7 @@
#include <utils/io.hpp>
#include <utils/hook.hpp>
#include <utils/flags.hpp>
#include <utils/properties.hpp>
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");

View File

@ -19,6 +19,7 @@
#include <utils/cryptography.hpp>
#include <utils/io.hpp>
#include <utils/string.hpp>
#include <utils/properties.hpp>
#define MASTER "https://master.fed0001.xyz/"
@ -63,17 +64,14 @@ namespace updater
std::vector<std::string> garbage_files{};
};
utils::concurrency::container<update_data_t> update_data;
// remove this at some point
std::vector<std::string> old_data_files =
{
{"./data/ui_scripts"},
{"./data/polrus"},
{"./data/fonts"},
{"./data/localizedstrings"},
{"./cdata"},
};
utils::concurrency::container<update_data_t> 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<bool>([](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<bool>([](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<std::string> find_garbage_files(const std::vector<std::string>& update_files)
{
std::vector<std::string> 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);

View File

@ -7,6 +7,7 @@
#include <utils/string.hpp>
#include <utils/flags.hpp>
#include <utils/io.hpp>
#include <utils/properties.hpp>
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()

View File

@ -0,0 +1,24 @@
#include "io.hpp"
#include "properties.hpp"
#include <gsl/gsl>
#include <ShlObj.h>
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;
}
}

View File

@ -0,0 +1,6 @@
#pragma once
namespace utils::properties
{
std::filesystem::path get_appdata_path();
}