Civ backgrounds for minimap
[0ad.git] / source / ps / Mod.cpp
blobcbf13726c6deddf9899ed3fe082a46b09eb1f729
1 /* Copyright (C) 2022 Wildfire Games.
2 * This file is part of 0 A.D.
4 * 0 A.D. is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 2 of the License, or
7 * (at your option) any later version.
9 * 0 A.D. is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with 0 A.D. If not, see <http://www.gnu.org/licenses/>.
18 #include "precompiled.h"
20 #include "ps/Mod.h"
22 #include "i18n/L10n.h"
23 #include "lib/file/file_system.h"
24 #include "lib/file/vfs/vfs.h"
25 #include "lib/sysdep/os.h"
26 #include "lib/utf8.h"
27 #include "ps/Filesystem.h"
28 #include "ps/GameSetup/GameSetup.h"
29 #include "ps/GameSetup/Paths.h"
30 #include "ps/Profiler2.h"
31 #include "scriptinterface/JSON.h"
32 #include "scriptinterface/Object.h"
33 #include "scriptinterface/ScriptExceptions.h"
34 #include "scriptinterface/ScriptInterface.h"
36 #if !OS_WIN
37 #include "lib/os_path.h"
38 #endif
40 #include <algorithm>
41 #include <boost/algorithm/string/split.hpp>
42 #include <boost/algorithm/string/classification.hpp>
43 #if OS_WIN
44 #include <filesystem>
45 #endif
46 #include <fstream>
47 #include <sstream>
48 #include <unordered_map>
50 namespace
52 /**
53 * Global instance of Mod, always exists.
55 Mod g_ModInstance;
57 bool LoadModJSON(const PIVFS& vfs, OsPath modsPath, OsPath mod, std::string& text)
59 #if OS_WIN
60 const std::filesystem::path modJsonPath = (modsPath / mod / L"mod.json").fileSystemPath();
61 #else
62 const std::string modJsonPath = OsString(modsPath / mod / L"mod.json");
63 #endif
64 // Attempt to open mod.json first.
65 std::ifstream modjson(modJsonPath);
66 if (!modjson)
68 modjson.close();
70 // Fallback: open the archive and read mod.json there.
71 // This can take in the hundreds of milliseconds with large mods.
72 vfs->Clear();
73 if (vfs->Mount(L"", modsPath / mod / "", VFS_MOUNT_MUST_EXIST, VFS_MIN_PRIORITY) < 0)
74 return false;
76 CVFSFile modinfo;
77 if (modinfo.Load(vfs, L"mod.json", false) != PSRETURN_OK)
78 return false;
80 text = modinfo.GetAsString();
82 // Attempt to write the mod.json file so we'll take the fast path next time.
83 std::ofstream out_mod_json(modJsonPath);
84 if (out_mod_json)
86 out_mod_json << text;
87 out_mod_json.close();
89 else
91 // Print a warning - we'll keep trying, which could have adverse effects.
92 if (L10n::IsInitialised())
93 LOGWARNING(g_L10n.Translate("Could not write external mod.json for zipped mod '%s'. The mod should be reinstalled."), mod.string8());
94 else
95 LOGWARNING("Could not write external mod.json for zipped mod '%s'. The mod should be reinstalled.", mod.string8());
97 return true;
99 else
101 std::stringstream buffer;
102 buffer << modjson.rdbuf();
103 text = buffer.str();
104 return true;
108 bool ParseModJSON(const ScriptRequest& rq, const PIVFS& vfs, OsPath modsPath, OsPath mod, Mod::ModData& data)
110 std::string text;
111 if (!LoadModJSON(vfs, modsPath, mod, text))
112 return false;
114 JS::RootedValue json(rq.cx);
115 if (!Script::ParseJSON(rq, text, &json))
116 return false;
118 Script::FromJSVal(rq, json, data);
120 // Complete - FromJSVal won't convert everything.
121 data.m_Pathname = utf8_from_wstring(mod.string());
122 data.m_Text = text;
123 if (!Script::GetProperty(rq, json, "dependencies", data.m_Dependencies))
124 return false;
125 return true;
128 } // anonymous namespace
130 Mod& Mod::Instance()
132 return g_ModInstance;
135 const std::vector<CStr>& Mod::GetEnabledMods() const
137 return m_EnabledMods;
140 const std::vector<CStr>& Mod::GetIncompatibleMods() const
142 return m_IncompatibleMods;
145 const std::vector<Mod::ModData>& Mod::GetAvailableMods() const
147 return m_AvailableMods;
150 bool Mod::EnableMods(const std::vector<CStr>& mods, const bool addPublic)
152 m_IncompatibleMods.clear();
153 m_EnabledMods.clear();
155 std::unordered_map<CStr, int> counts;
156 for (const CStr& mod : mods)
158 // Ignore duplicates.
159 if (counts.try_emplace(mod, 0).first->second++ > 0)
160 continue;
161 m_EnabledMods.emplace_back(mod);
164 if (addPublic && counts["public"] == 0)
165 m_EnabledMods.insert(m_EnabledMods.begin(), "public");
167 if (counts["mod"] == 0)
168 m_EnabledMods.insert(m_EnabledMods.begin(), "mod");
170 m_IncompatibleMods = CheckForIncompatibleMods(m_EnabledMods);
172 for (const CStr& mod : m_IncompatibleMods)
173 m_EnabledMods.erase(std::find(m_EnabledMods.begin(), m_EnabledMods.end(), mod));
175 return m_IncompatibleMods.empty();
178 const Mod::ModData* Mod::GetModData(const CStr& mod) const
180 std::vector<ModData>::const_iterator it = std::find_if(m_AvailableMods.begin(), m_AvailableMods.end(),
181 [&mod](const ModData& modData) { return modData.m_Pathname == mod; });
182 if (it == m_AvailableMods.end())
183 return nullptr;
184 return std::addressof(*it);
187 const std::vector<const Mod::ModData*> Mod::GetEnabledModsData() const
189 std::vector<const ModData*> loadedMods;
190 for (const CStr& mod : m_EnabledMods)
192 if (mod == "mod" || mod == "user")
193 continue;
195 const ModData* data = GetModData(mod);
197 // This ought be impossible, but let's handle it anyways since it's not a reason to crash.
198 if (!data)
200 LOGERROR("Unavailable mod '%s' was enabled.", mod);
201 continue;
204 loadedMods.emplace_back(data);
206 return loadedMods;
209 bool Mod::AreModsPlayCompatible(const std::vector<const Mod::ModData*>& modsA, const std::vector<const Mod::ModData*>& modsB)
211 // Mods must be loaded in the same order.
212 std::vector<const Mod::ModData*>::const_iterator a = modsA.begin();
213 std::vector<const Mod::ModData*>::const_iterator b = modsB.begin();
215 while (a != modsA.end() || b != modsB.end())
217 if (a != modsA.end() && (*a)->m_IgnoreInCompatibilityChecks)
219 ++a;
220 continue;
222 if (b != modsB.end() && (*b)->m_IgnoreInCompatibilityChecks)
224 ++b;
225 continue;
227 // If at this point one of the two lists still contains items, the sizes are different -> fail.
228 if (a == modsA.end() || b == modsB.end())
229 return false;
231 if ((*a)->m_Pathname != (*b)->m_Pathname)
232 return false;
233 if ((*a)->m_Version != (*b)->m_Version)
234 return false;
235 ++a;
236 ++b;
238 return true;
241 void Mod::UpdateAvailableMods(const ScriptInterface& scriptInterface)
243 PROFILE2("UpdateAvailableMods");
245 m_AvailableMods.clear();
246 const Paths paths(g_CmdLineArgs);
248 // loop over all possible paths
249 OsPath modPath = paths.RData()/"mods";
250 OsPath modUserPath = paths.UserData()/"mods";
252 DirectoryNames modDirs;
253 DirectoryNames modDirsUser;
255 GetDirectoryEntries(modPath, NULL, &modDirs);
256 // Sort modDirs so that we can do a fast lookup below
257 std::sort(modDirs.begin(), modDirs.end());
259 PIVFS vfs = CreateVfs();
261 ScriptRequest rq(scriptInterface);
262 for (DirectoryNames::iterator iter = modDirs.begin(); iter != modDirs.end(); ++iter)
264 ModData data;
265 if (!ParseModJSON(rq, vfs, modPath, *iter, data))
266 continue;
267 // Valid mod data, add it to our structure
268 m_AvailableMods.emplace_back(std::move(data));
271 GetDirectoryEntries(modUserPath, NULL, &modDirsUser);
273 for (DirectoryNames::iterator iter = modDirsUser.begin(); iter != modDirsUser.end(); ++iter)
275 // Ignore mods in the user folder if we have already found them in modDirs.
276 if (std::binary_search(modDirs.begin(), modDirs.end(), *iter))
277 continue;
279 ModData data;
280 if (!ParseModJSON(rq, vfs, modUserPath, *iter, data))
281 continue;
282 // Valid mod data, add it to our structure
283 m_AvailableMods.emplace_back(std::move(data));
287 std::vector<CStr> Mod::CheckForIncompatibleMods(const std::vector<CStr>& mods) const
289 std::vector<CStr> incompatibleMods;
290 std::unordered_map<CStr, std::vector<CStr>> modDependencies;
291 std::unordered_map<CStr, CStr> modNameVersions;
292 for (const CStr& mod : mods)
294 if (mod == "mod" || mod == "user")
295 continue;
297 std::vector<ModData>::const_iterator it = std::find_if(m_AvailableMods.begin(), m_AvailableMods.end(),
298 [&mod](const ModData& modData) { return modData.m_Pathname == mod; });
300 if (it == m_AvailableMods.end())
302 incompatibleMods.push_back(mod);
303 continue;
306 modNameVersions.emplace(it->m_Name, it->m_Version);
307 modDependencies.emplace(it->m_Pathname, it->m_Dependencies);
310 static const std::vector<CStr> toCheck = { "<=", ">=", "=", "<", ">" };
311 for (const CStr& mod : mods)
313 if (mod == "mod" || mod == "user")
314 continue;
316 const std::unordered_map<CStr, std::vector<CStr>>::iterator res = modDependencies.find(mod);
317 if (res == modDependencies.end())
318 continue;
319 const std::vector<CStr> deps = res->second;
320 if (deps.empty())
321 continue;
323 for (const CStr& dep : deps)
325 if (dep.empty())
326 continue;
327 // 0ad<=0.0.24
328 for (const CStr& op : toCheck)
330 const int pos = dep.Find(op.c_str());
331 if (pos == -1)
332 continue;
333 //0ad
334 const CStr modToCheck = dep.substr(0, pos);
335 //0.0.24
336 const CStr versionToCheck = dep.substr(pos + op.size());
337 const std::unordered_map<CStr, CStr>::iterator it = modNameVersions.find(modToCheck);
338 // Could not find the mod, or 0.0.25(0ad) , <=, 0.0.24(required version)
339 if (it == modNameVersions.end() || !CompareVersionStrings(it->second, op, versionToCheck))
340 incompatibleMods.push_back(mod);
341 break;
347 return incompatibleMods;
350 bool Mod::CompareVersionStrings(const CStr& version, const CStr& op, const CStr& required) const
352 std::vector<CStr> versionSplit;
353 std::vector<CStr> requiredSplit;
354 static const std::string toIgnore = "-,_";
355 boost::split(versionSplit, version, boost::is_any_of(toIgnore), boost::token_compress_on);
356 boost::split(requiredSplit, required, boost::is_any_of(toIgnore), boost::token_compress_on);
357 boost::split(versionSplit, versionSplit[0], boost::is_any_of("."), boost::token_compress_on);
358 boost::split(requiredSplit, requiredSplit[0], boost::is_any_of("."), boost::token_compress_on);
360 const bool eq = op.Find("=") != -1;
361 const bool lt = op.Find("<") != -1;
362 const bool gt = op.Find(">") != -1;
364 const size_t min = std::min(versionSplit.size(), requiredSplit.size());
366 for (size_t i = 0; i < min; ++i)
368 const int diff = versionSplit[i].ToInt() - requiredSplit[i].ToInt();
369 if ((gt && diff > 0) || (lt && diff < 0))
370 return true;
372 if ((gt && diff < 0) || (lt && diff > 0) || (eq && diff))
373 return false;
376 const size_t versionSize = versionSplit.size();
377 const size_t requiredSize = requiredSplit.size();
378 if (versionSize == requiredSize)
379 return eq;
380 return versionSize < requiredSize ? lt : gt;