Create mods.json if its missing

This commit is contained in:
Tixx 2025-01-18 22:29:32 +01:00
parent 649514ca1a
commit d52a791dd9
No known key found for this signature in database
GPG Key ID: EC6E7A2BAABF0B8C

View File

@ -396,7 +396,18 @@ nlohmann::json modUsage = {};
void UpdateModUsage(const std::string& fileName) { void UpdateModUsage(const std::string& fileName) {
try { 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()) { if (!file.is_open()) {
error("Failed to open or create mods.json"); error("Failed to open or create mods.json");
return; return;
@ -420,6 +431,7 @@ void UpdateModUsage(const std::string& fileName) {
file.clear(); file.clear();
file.seekp(0, std::ios::beg); file.seekp(0, std::ios::beg);
file << modUsage.dump(); file << modUsage.dump();
file.close();
} catch (std::exception& e) { } catch (std::exception& e) {
error("Failed to update mods.json: " + std::string(e.what())); error("Failed to update mods.json: " + std::string(e.what()));
} }