From d35567dd477f3fee2765b9a94a292dd464717674 Mon Sep 17 00:00:00 2001 From: Tixx <83774803+WiserTixx@users.noreply.github.com> Date: Sun, 22 Dec 2024 12:07:25 +0100 Subject: [PATCH 1/4] Look for new mods in the old format --- src/Network/Resources.cpp | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/src/Network/Resources.cpp b/src/Network/Resources.cpp index 8dbb2ec..6df646c 100644 --- a/src/Network/Resources.cpp +++ b/src/Network/Resources.cpp @@ -458,6 +458,40 @@ void NewSyncResources(SOCKET Sock, const std::string& Mods, const std::vectorFileName).filename(); + fs::exists(OldCachedPath) && GetSha256HashReallyFast(OldCachedPath.string()) == ModInfoIter->Hash) { + debug("Mod '" + FileName + "' found in old cache, copying it to the new cache"); + std::this_thread::sleep_for(std::chrono::milliseconds(50)); + try { + fs::copy_file(OldCachedPath, PathToSaveTo, fs::copy_options::overwrite_existing); + + if (!fs::exists(GetGamePath() + "mods/multiplayer")) { + fs::create_directories(GetGamePath() + "mods/multiplayer"); + } + + auto modname = ModInfoIter->FileName; + +#if defined(__linux__) + // Linux version of the game doesnt support uppercase letters in mod names + for (char& c : modname) { + c = ::tolower(c); + } +#endif + + debug("Mod name: " + modname); + auto name = std::filesystem::path(GetGamePath()) / "mods/multiplayer" / modname; + std::string tmp_name = name.string(); + tmp_name += ".tmp"; + + fs::copy_file(PathToSaveTo, tmp_name, fs::copy_options::overwrite_existing); + fs::rename(tmp_name, name); + } catch (std::exception& e) { + error("Failed copy to the mods folder! " + std::string(e.what())); + Terminate = true; + continue; + } + WaitForConfirm(); + continue; } CheckForDir(); std::string FName = ModInfoIter->FileName; From c485fba26b1497f7adbcc837b6a640ab75025459 Mon Sep 17 00:00:00 2001 From: Tixx <83774803+WiserTixx@users.noreply.github.com> Date: Sun, 22 Dec 2024 12:13:59 +0100 Subject: [PATCH 2/4] Change an easily confusable warning to debug --- src/Network/Resources.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Network/Resources.cpp b/src/Network/Resources.cpp index 6df646c..502b96c 100644 --- a/src/Network/Resources.cpp +++ b/src/Network/Resources.cpp @@ -382,7 +382,7 @@ struct ModInfo { } } catch (const std::exception& e) { debug(std::string("Failed to receive mod list: ") + e.what()); - warn("Failed to receive new mod list format! This server may be outdated, but everything should still work as expected."); + debug("Failed to receive new mod list format! This server may be outdated, but everything should still work as expected."); } return std::make_pair(success, modInfos); } From 649514ca1aa114f027b37f8582a0b66d19f57c9b Mon Sep 17 00:00:00 2001 From: Tixx <83774803+WiserTixx@users.noreply.github.com> Date: Tue, 24 Dec 2024 13:53:50 +0100 Subject: [PATCH 3/4] Save mod usage date --- src/Network/Resources.cpp | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/src/Network/Resources.cpp b/src/Network/Resources.cpp index 502b96c..80914b9 100644 --- a/src/Network/Resources.cpp +++ b/src/Network/Resources.cpp @@ -392,6 +392,40 @@ struct ModInfo { std::string HashAlgorithm; }; +nlohmann::json modUsage = {}; + +void UpdateModUsage(const std::string& fileName) { + try { + std::fstream file(fs::path(CachingDirectory) / "mods.json", std::ios::in | std::ios::out); + if (!file.is_open()) { + error("Failed to open or create mods.json"); + return; + } + + if (modUsage.empty()) { + auto Size = fs::file_size(fs::path(CachingDirectory) / "mods.json"); + std::string modsJson(Size, 0); + file.read(&modsJson[0], Size); + + if (!modsJson.empty()) { + auto parsedModJson = nlohmann::json::parse(modsJson, nullptr, false); + + if (parsedModJson.is_object()) + modUsage = parsedModJson; + } + } + + modUsage[fileName] = std::chrono::duration_cast(std::chrono::system_clock::now().time_since_epoch()).count(); + + file.clear(); + file.seekp(0, std::ios::beg); + file << modUsage.dump(); + } catch (std::exception& e) { + error("Failed to update mods.json: " + std::string(e.what())); + } +} + + void NewSyncResources(SOCKET Sock, const std::string& Mods, const std::vector ModInfos) { if (ModInfos.empty()) { CoreSend("L"); @@ -451,6 +485,7 @@ void NewSyncResources(SOCKET Sock, const std::string& Mods, const std::vectorsubstr(pos)); } WaitForConfirm(); } From d52a791dd9b424ee39cedc055c78fc5060d4040c Mon Sep 17 00:00:00 2001 From: Tixx <83774803+WiserTixx@users.noreply.github.com> Date: Sat, 18 Jan 2025 22:29:32 +0100 Subject: [PATCH 4/4] Create mods.json if its missing --- src/Network/Resources.cpp | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/src/Network/Resources.cpp b/src/Network/Resources.cpp index 80914b9..876c6f1 100644 --- a/src/Network/Resources.cpp +++ b/src/Network/Resources.cpp @@ -396,7 +396,18 @@ nlohmann::json modUsage = {}; void UpdateModUsage(const std::string& fileName) { try { - std::fstream file(fs::path(CachingDirectory) / "mods.json", std::ios::in | std::ios::out); + fs::path usageFile = fs::path(CachingDirectory) / "mods.json"; + + if (!fs::exists(usageFile)) { + if (std::ofstream file(usageFile); !file.is_open()) { + error("Failed to create mods.json"); + return; + } else { + file.close(); + } + } + + std::fstream file(usageFile, std::ios::in | std::ios::out); if (!file.is_open()) { error("Failed to open or create mods.json"); return; @@ -420,6 +431,7 @@ void UpdateModUsage(const std::string& fileName) { file.clear(); file.seekp(0, std::ios::beg); file << modUsage.dump(); + file.close(); } catch (std::exception& e) { error("Failed to update mods.json: " + std::string(e.what())); }