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(); }