diff --git a/src/client/component/filesystem.cpp b/src/client/component/filesystem.cpp index 8a8cdf91..3134fa26 100644 --- a/src/client/component/filesystem.cpp +++ b/src/client/component/filesystem.cpp @@ -37,7 +37,7 @@ namespace filesystem filesystem::register_path(L"."); filesystem::register_path(L"h2-mod"); - filesystem::register_path(L"cdata"); + filesystem::register_path(L"" CLIENT_DATA_FOLDER); localized_strings::clear(); diff --git a/src/client/component/filesystem.hpp b/src/client/component/filesystem.hpp index d65af0d0..80ecfdb7 100644 --- a/src/client/component/filesystem.hpp +++ b/src/client/component/filesystem.hpp @@ -1,5 +1,7 @@ #pragma once +#include "updater.hpp" + namespace filesystem { std::string read_file(const std::string& path); diff --git a/src/client/component/updater.cpp b/src/client/component/updater.cpp index 79c50212..78b81eab 100644 --- a/src/client/component/updater.cpp +++ b/src/client/component/updater.cpp @@ -4,6 +4,7 @@ #include "scheduler.hpp" #include "updater.hpp" #include "game/ui_scripting/execution.hpp" +#include "console.hpp" #include "version.h" @@ -57,10 +58,20 @@ namespace updater std::string error{}; std::string current_file{}; std::vector required_files{}; + 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"}, + }; + std::string select(const std::string& main, const std::string& develop) { if (GIT_BRANCH == "develop"s) @@ -172,6 +183,67 @@ namespace updater data_ = {}; }); } + + bool has_old_data_files() + { + bool has = false; + for (const auto& file : old_data_files) + { + if (utils::io::directory_exists(file)) + { + has = true; + } + } + + return has; + } + + void delete_old_data_files() + { + for (const auto& file : old_data_files) + { + std::filesystem::remove_all(file); + } + } + + std::vector find_garbage_files(const std::vector& update_files) + { + std::vector garbage_files{}; + + if (!utils::io::directory_exists("cdata")) + { + return {}; + } + + const auto current_files = utils::io::list_files_recursively(CLIENT_DATA_FOLDER); + for (const auto& file : current_files) + { + bool found = false; + for (const auto& update_file : update_files) + { + const auto path_a = std::filesystem::path(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); + + if ((is_directory && compare == -1) || compare == 0) + { + found = true; + break; + } + } + + if (!found) + { +#ifdef DEBUG + console::info("[Updater] Found extra file %s\n", file.data()); +#endif + garbage_files.push_back(file); + } + } + + return garbage_files; + } } void relaunch() @@ -231,7 +303,7 @@ namespace updater { return update_data.access([](update_data_t& data_) { - return data_.required_files.size() > 0; + return data_.required_files.size() > 0 || data_.garbage_files.size() > 0 || has_old_data_files(); }); } @@ -262,7 +334,7 @@ namespace updater void cancel_update() { #ifdef DEBUG - printf("[Updater] Cancelling update\n"); + console::info("[Updater] Cancelling update\n"); #endif return update_data.access([](update_data_t& data_) @@ -277,7 +349,7 @@ namespace updater reset_data(); #ifdef DEBUG - printf("[Updater] starting update check\n"); + console::info("[Updater] starting update check\n"); #endif scheduler::once([]() @@ -306,6 +378,7 @@ namespace updater } std::vector required_files; + std::vector update_files; const auto files = j.GetArray(); for (const auto& file : files) @@ -318,6 +391,8 @@ namespace updater const auto name = file[0].GetString(); const auto sha = file[2].GetString(); + update_files.push_back(name); + if (!check_file(name, sha)) { if (get_binary_name() == name) @@ -329,18 +404,21 @@ namespace updater } #ifdef DEBUG - printf("[Updater] need file %s\n", name); + console::info("[Updater] need file %s\n", name); #endif required_files.push_back(name); } } - update_data.access([&required_files](update_data_t& data_) + const auto garbage_files = find_garbage_files(update_files); + + update_data.access([&](update_data_t& data_) { data_.check.done = true; data_.check.success = true; data_.required_files = required_files; + data_.garbage_files = garbage_files; }); notify("update_check_done"); @@ -350,7 +428,7 @@ namespace updater void start_update_download() { #ifdef DEBUG - printf("[Updater] starting update download\n"); + console::info("[Updater] starting update download\n"); #endif if (!is_update_check_done() || !get_update_check_status() || is_update_cancelled()) @@ -358,6 +436,18 @@ namespace updater return; } + delete_old_data_files(); + + const auto garbage_files = update_data.access>([](update_data_t& data_) + { + return data_.garbage_files; + }); + + for (const auto& file : garbage_files) + { + std::filesystem::remove_all(file); + } + scheduler::once([]() { const auto required_files = update_data.access>([](update_data_t& data_) @@ -375,7 +465,7 @@ namespace updater }); #ifdef DEBUG - printf("[Updater] downloading file %s\n", file.data()); + console::info("[Updater] downloading file %s\n", file.data()); #endif const auto data = download_file(file); diff --git a/src/client/component/updater.hpp b/src/client/component/updater.hpp index 9a3dd45e..0301a6c3 100644 --- a/src/client/component/updater.hpp +++ b/src/client/component/updater.hpp @@ -1,5 +1,7 @@ #pragma once +#define CLIENT_DATA_FOLDER "cdata" + namespace updater { void relaunch(); diff --git a/src/common/utils/io.cpp b/src/common/utils/io.cpp index 9b161d39..2e38c6e5 100644 --- a/src/common/utils/io.cpp +++ b/src/common/utils/io.cpp @@ -121,6 +121,18 @@ namespace utils::io return files; } + std::vector list_files_recursively(const std::string& directory) + { + std::vector files; + + for (auto& file : std::filesystem::recursive_directory_iterator(directory)) + { + files.push_back(file.path().generic_string()); + } + + return files; + } + void copy_folder(const std::filesystem::path& src, const std::filesystem::path& target) { std::filesystem::copy(src, target, diff --git a/src/common/utils/io.hpp b/src/common/utils/io.hpp index 38344987..19e8c143 100644 --- a/src/common/utils/io.hpp +++ b/src/common/utils/io.hpp @@ -18,5 +18,6 @@ namespace utils::io bool directory_is_empty(const std::string& directory); bool remove_directory(const std::string& directory); std::vector list_files(const std::string& directory); + std::vector list_files_recursively(const std::string& directory); void copy_folder(const std::filesystem::path& src, const std::filesystem::path& target); }