Index: ps/trunk/source/ps/Mod.cpp =================================================================== --- ps/trunk/source/ps/Mod.cpp (revision 25978) +++ ps/trunk/source/ps/Mod.cpp (revision 25979) @@ -1,370 +1,370 @@ /* Copyright (C) 2021 Wildfire Games. * This file is part of 0 A.D. * * 0 A.D. is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 2 of the License, or * (at your option) any later version. * * 0 A.D. is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with 0 A.D. If not, see . */ #include "precompiled.h" #include "ps/Mod.h" #include "i18n/L10n.h" #include "lib/file/file_system.h" #include "lib/file/vfs/vfs.h" #include "lib/utf8.h" #include "ps/Filesystem.h" #include "ps/GameSetup/GameSetup.h" #include "ps/GameSetup/Paths.h" #include "ps/Profiler2.h" #include "scriptinterface/JSON.h" #include "scriptinterface/Object.h" #include "scriptinterface/ScriptExceptions.h" #include "scriptinterface/ScriptInterface.h" #include #include #include #include #include #include namespace { /** * Global instance of Mod, always exists. */ Mod g_ModInstance; bool LoadModJSON(const PIVFS& vfs, OsPath modsPath, OsPath mod, std::string& text) { // Attempt to open mod.json first. std::ifstream modjson; modjson.open((modsPath / mod / L"mod.json").string8()); if (!modjson.is_open()) { modjson.close(); // Fallback: open the archive and read mod.json there. // This can take in the hundreds of milliseconds with large mods. vfs->Clear(); if (vfs->Mount(L"", modsPath / mod / "", VFS_MOUNT_MUST_EXIST, VFS_MIN_PRIORITY) < 0) return false; CVFSFile modinfo; if (modinfo.Load(vfs, L"mod.json", false) != PSRETURN_OK) return false; text = modinfo.GetAsString(); // Attempt to write the mod.json file so we'll take the fast path next time. std::ofstream out_mod_json((modsPath / mod / L"mod.json").string8()); if (out_mod_json.good()) { out_mod_json << text; out_mod_json.close(); } else { // Print a warning - we'll keep trying, which could have adverse effects. if (L10n::IsInitialised()) LOGWARNING(g_L10n.Translate("Could not write external mod.json for zipped mod '%s'. The mod should be reinstalled."), mod.string8()); else LOGWARNING("Could not write external mod.json for zipped mod '%s'. The mod should be reinstalled.", mod.string8()); } return true; } else { std::stringstream buffer; buffer << modjson.rdbuf(); text = buffer.str(); return true; } } bool ParseModJSON(const ScriptRequest& rq, const PIVFS& vfs, OsPath modsPath, OsPath mod, Mod::ModData& data) { std::string text; if (!LoadModJSON(vfs, modsPath, mod, text)) return false; JS::RootedValue json(rq.cx); if (!Script::ParseJSON(rq, text, &json)) return false; Script::FromJSVal(rq, json, data); // Complete - FromJSVal won't convert everything. data.m_Pathname = utf8_from_wstring(mod.string()); data.m_Text = text; if (!Script::GetProperty(rq, json, "dependencies", data.m_Dependencies)) return false; return true; } } // anonymous namespace Mod& Mod::Instance() { return g_ModInstance; } const std::vector& Mod::GetEnabledMods() const { return m_EnabledMods; } const std::vector& Mod::GetIncompatibleMods() const { return m_IncompatibleMods; } const std::vector& Mod::GetAvailableMods() const { return m_AvailableMods; } bool Mod::EnableMods(const std::vector& mods, const bool addPublic) { m_IncompatibleMods.clear(); m_EnabledMods.clear(); std::unordered_map counts; for (const CStr& mod : mods) { // Ignore duplicates. if (counts.try_emplace(mod, 0).first->second++ > 0) continue; m_EnabledMods.emplace_back(mod); } if (addPublic && counts["public"] == 0) m_EnabledMods.insert(m_EnabledMods.begin(), "public"); if (counts["mod"] == 0) m_EnabledMods.insert(m_EnabledMods.begin(), "mod"); m_IncompatibleMods = CheckForIncompatibleMods(m_EnabledMods); for (const CStr& mod : m_IncompatibleMods) m_EnabledMods.erase(std::find(m_EnabledMods.begin(), m_EnabledMods.end(), mod)); return m_IncompatibleMods.empty(); } const Mod::ModData* Mod::GetModData(const CStr& mod) const { std::vector::const_iterator it = std::find_if(m_AvailableMods.begin(), m_AvailableMods.end(), [&mod](const ModData& modData) { return modData.m_Pathname == mod; }); if (it == m_AvailableMods.end()) return nullptr; return std::addressof(*it); } const std::vector Mod::GetEnabledModsData() const { std::vector loadedMods; for (const CStr& mod : m_EnabledMods) { if (mod == "mod" || mod == "user") continue; const ModData* data = GetModData(mod); // This ought be impossible, but let's handle it anyways since it's not a reason to crash. if (!data) { LOGERROR("Unavailable mod '%s' was enabled.", mod); continue; } loadedMods.emplace_back(data); } return loadedMods; } bool Mod::AreModsPlayCompatible(const std::vector& modsA, const std::vector& modsB) { // Mods must be loaded in the same order. std::vector::const_iterator a = modsA.begin(); std::vector::const_iterator b = modsB.begin(); while (a != modsA.end() || b != modsB.end()) { if (a != modsA.end() && (*a)->m_IgnoreInCompatibilityChecks) { ++a; continue; } if (b != modsB.end() && (*b)->m_IgnoreInCompatibilityChecks) { ++b; continue; } // If at this point one of the two lists still contains items, the sizes are different -> fail. if (a == modsA.end() || b == modsB.end()) return false; if ((*a)->m_Pathname != (*b)->m_Pathname) return false; if ((*a)->m_Version != (*b)->m_Version) return false; ++a; ++b; } return true; } void Mod::UpdateAvailableMods(const ScriptInterface& scriptInterface) { PROFILE2("UpdateAvailableMods"); m_AvailableMods.clear(); const Paths paths(g_CmdLineArgs); // loop over all possible paths OsPath modPath = paths.RData()/"mods"; OsPath modUserPath = paths.UserData()/"mods"; DirectoryNames modDirs; DirectoryNames modDirsUser; GetDirectoryEntries(modPath, NULL, &modDirs); // Sort modDirs so that we can do a fast lookup below std::sort(modDirs.begin(), modDirs.end()); PIVFS vfs = CreateVfs(); ScriptRequest rq(scriptInterface); for (DirectoryNames::iterator iter = modDirs.begin(); iter != modDirs.end(); ++iter) { ModData data; if (!ParseModJSON(rq, vfs, modPath, *iter, data)) continue; // Valid mod data, add it to our structure m_AvailableMods.emplace_back(std::move(data)); } GetDirectoryEntries(modUserPath, NULL, &modDirsUser); for (DirectoryNames::iterator iter = modDirsUser.begin(); iter != modDirsUser.end(); ++iter) { // Ignore mods in the user folder if we have already found them in modDirs. if (std::binary_search(modDirs.begin(), modDirs.end(), *iter)) continue; ModData data; if (!ParseModJSON(rq, vfs, modUserPath, *iter, data)) continue; // Valid mod data, add it to our structure m_AvailableMods.emplace_back(std::move(data)); } } std::vector Mod::CheckForIncompatibleMods(const std::vector& mods) const { std::vector incompatibleMods; std::unordered_map> modDependencies; std::unordered_map modNameVersions; for (const CStr& mod : mods) { if (mod == "mod" || mod == "user") continue; std::vector::const_iterator it = std::find_if(m_AvailableMods.begin(), m_AvailableMods.end(), [&mod](const ModData& modData) { return modData.m_Pathname == mod; }); if (it == m_AvailableMods.end()) { incompatibleMods.push_back(mod); continue; } modNameVersions.emplace(it->m_Name, it->m_Version); - modDependencies.emplace(it->m_Name, it->m_Dependencies); + modDependencies.emplace(it->m_Pathname, it->m_Dependencies); } static const std::vector toCheck = { "<=", ">=", "=", "<", ">" }; for (const CStr& mod : mods) { if (mod == "mod" || mod == "user") continue; const std::unordered_map>::iterator res = modDependencies.find(mod); if (res == modDependencies.end()) continue; const std::vector deps = res->second; if (deps.empty()) continue; for (const CStr& dep : deps) { if (dep.empty()) continue; // 0ad<=0.0.24 for (const CStr& op : toCheck) { const int pos = dep.Find(op.c_str()); if (pos == -1) continue; //0ad const CStr modToCheck = dep.substr(0, pos); //0.0.24 const CStr versionToCheck = dep.substr(pos + op.size()); const std::unordered_map::iterator it = modNameVersions.find(modToCheck); // Could not find the mod, or 0.0.25(0ad) , <=, 0.0.24(required version) if (it == modNameVersions.end() || !CompareVersionStrings(it->second, op, versionToCheck)) incompatibleMods.push_back(mod); break; } } } return incompatibleMods; } bool Mod::CompareVersionStrings(const CStr& version, const CStr& op, const CStr& required) const { std::vector versionSplit; std::vector requiredSplit; static const std::string toIgnore = "-,_"; boost::split(versionSplit, version, boost::is_any_of(toIgnore), boost::token_compress_on); boost::split(requiredSplit, required, boost::is_any_of(toIgnore), boost::token_compress_on); boost::split(versionSplit, versionSplit[0], boost::is_any_of("."), boost::token_compress_on); boost::split(requiredSplit, requiredSplit[0], boost::is_any_of("."), boost::token_compress_on); const bool eq = op.Find("=") != -1; const bool lt = op.Find("<") != -1; const bool gt = op.Find(">") != -1; const size_t min = std::min(versionSplit.size(), requiredSplit.size()); for (size_t i = 0; i < min; ++i) { const int diff = versionSplit[i].ToInt() - requiredSplit[i].ToInt(); if ((gt && diff > 0) || (lt && diff < 0)) return true; if ((gt && diff < 0) || (lt && diff > 0) || (eq && diff)) return false; } const size_t versionSize = versionSplit.size(); const size_t requiredSize = requiredSplit.size(); if (versionSize == requiredSize) return eq; return versionSize < requiredSize ? lt : gt; } Index: ps/trunk/source/ps/tests/test_Mod.h =================================================================== --- ps/trunk/source/ps/tests/test_Mod.h (revision 25978) +++ ps/trunk/source/ps/tests/test_Mod.h (revision 25979) @@ -1,176 +1,196 @@ /* Copyright (C) 2021 Wildfire Games. * * Permission is hereby granted, free of charge, to any person obtaining * a copy of this software and associated documentation files (the * "Software"), to deal in the Software without restriction, including * without limitation the rights to use, copy, modify, merge, publish, * distribute, sublicense, and/or sell copies of the Software, and to * permit persons to whom the Software is furnished to do so, subject to * the following conditions: * * The above copyright notice and this permission notice shall be included * in all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ #include "lib/self_test.h" #include "ps/CLogger.h" #include "ps/Mod.h" #include "scriptinterface/JSON.h" #include "scriptinterface/ScriptInterface.h" class TestMod : public CxxTest::TestSuite { Mod m_Mods; public: void test_version_check() { CStr eq = "="; CStr lt = "<"; CStr gt = ">"; CStr leq = "<="; CStr geq = ">="; CStr required = "0.0.24";// 0ad <= required CStr version = "0.0.24";// 0ad version // 0.0.24 = 0.0.24 TS_ASSERT(m_Mods.CompareVersionStrings(version, eq, required)); TS_ASSERT(!m_Mods.CompareVersionStrings(version, lt, required)); TS_ASSERT(!m_Mods.CompareVersionStrings(version, gt, required)); TS_ASSERT(m_Mods.CompareVersionStrings(version, leq, required)); TS_ASSERT(m_Mods.CompareVersionStrings(version, geq, required)); // 0.0.23 <= 0.0.24 version = "0.0.23"; TS_ASSERT(!m_Mods.CompareVersionStrings(version, eq, required)); TS_ASSERT(m_Mods.CompareVersionStrings(version, lt, required)); TS_ASSERT(!m_Mods.CompareVersionStrings(version, gt, required)); TS_ASSERT(m_Mods.CompareVersionStrings(version, leq, required)); TS_ASSERT(!m_Mods.CompareVersionStrings(version, geq, required)); // 0.0.25 >= 0.0.24 version = "0.0.25"; TS_ASSERT(!m_Mods.CompareVersionStrings(version, eq, required)); TS_ASSERT(!m_Mods.CompareVersionStrings(version, lt, required)); TS_ASSERT(m_Mods.CompareVersionStrings(version, gt, required)); TS_ASSERT(!m_Mods.CompareVersionStrings(version, leq, required)); TS_ASSERT(m_Mods.CompareVersionStrings(version, geq, required)); // 0.0.9 <= 0.1.0 version = "0.0.9"; required = "0.1.0"; TS_ASSERT(!m_Mods.CompareVersionStrings(version, eq, required)); TS_ASSERT(m_Mods.CompareVersionStrings(version, lt, required)); TS_ASSERT(!m_Mods.CompareVersionStrings(version, gt, required)); TS_ASSERT(m_Mods.CompareVersionStrings(version, leq, required)); TS_ASSERT(!m_Mods.CompareVersionStrings(version, geq, required)); // 5.3 <= 5.3.0 version = "5.3"; required = "5.3.0"; TS_ASSERT(!m_Mods.CompareVersionStrings(version, eq, required)); TS_ASSERT(m_Mods.CompareVersionStrings(version, lt, required)); TS_ASSERT(!m_Mods.CompareVersionStrings(version, gt, required)); TS_ASSERT(m_Mods.CompareVersionStrings(version, leq, required)); TS_ASSERT(!m_Mods.CompareVersionStrings(version, geq, required)); } void test_compatible() { ScriptInterface script("Test", "Test", g_ScriptContext); ScriptRequest rq(script); JS::RootedObject obj(rq.cx, JS_NewPlainObject(rq.cx)); m_Mods.m_AvailableMods = { Mod::ModData{ "public", "0ad", "0.0.25", {}, false, "" }, Mod::ModData{ "wrong", "wrong", "0.0.1", { "0ad=0.0.24" }, false, "" }, Mod::ModData{ "good", "good", "0.0.2", { "0ad=0.0.25" }, false, "" }, Mod::ModData{ "good2", "good2", "0.0.4", { "0ad>=0.0.24" }, false, "" }, }; std::vector mods; mods.clear(); mods.push_back("public"); TS_ASSERT(m_Mods.CheckForIncompatibleMods(mods).empty()); mods.clear(); mods.push_back("mod"); mods.push_back("public"); TS_ASSERT(m_Mods.CheckForIncompatibleMods(mods).empty()); mods.clear(); mods.push_back("public"); mods.push_back("good"); TS_ASSERT(m_Mods.CheckForIncompatibleMods(mods).empty()); mods.clear(); mods.push_back("public"); mods.push_back("good2"); TS_ASSERT(m_Mods.CheckForIncompatibleMods(mods).empty()); mods.clear(); mods.push_back("public"); mods.push_back("wrong"); TS_ASSERT(!m_Mods.CheckForIncompatibleMods(mods).empty()); mods.clear(); mods.push_back("public"); mods.push_back("does_not_exist"); TS_ASSERT(!m_Mods.CheckForIncompatibleMods(mods).empty()); mods.clear(); mods.push_back("good2"); TS_ASSERT(m_Mods.CheckForIncompatibleMods(mods).size() == 1); } + void test_different_name_and_path() + { + ScriptInterface script("Test", "Test", g_ScriptContext); + + ScriptRequest rq(script); + JS::RootedObject obj(rq.cx, JS_NewPlainObject(rq.cx)); + + m_Mods.m_AvailableMods = { + Mod::ModData{ "public", "0ad", "0.0.25", {}, false, "" }, + Mod::ModData{ "wrong", "wrong_name", "0.10.0", { "0ad=0.0.24" }, false, ""} + }; + + std::vector mods; + + mods.clear(); + mods.push_back("public"); + mods.push_back("wrong"); + TS_ASSERT(!m_Mods.CheckForIncompatibleMods(mods).empty()); + } + void test_play_compatible() { Mod::ModData a1 = { "a", "a", "0.0.1", {}, false, "" }; Mod::ModData a2 = { "a", "a", "0.0.2", {}, false, "" }; Mod::ModData b = { "b", "b", "0.0.1", {}, false, "" }; Mod::ModData c = { "c", "c", "0.0.1", {}, true, "" }; using ModList = std::vector; { ModList l1 = { &a1 }; ModList l2 = { &a2 }; TS_ASSERT(!Mod::AreModsPlayCompatible(l1, l2)); } { ModList l1 = { &a1, &b }; ModList l2 = { &a1, &b, &c }; TS_ASSERT(Mod::AreModsPlayCompatible(l1, l2)); } { ModList l1 = { &c, &b, &a1 }; ModList l2 = { &b, &c, &a1 }; TS_ASSERT(Mod::AreModsPlayCompatible(l1, l2)); } { ModList l1 = { &b, &c, &a1 }; ModList l2 = { &b, &c, &a2 }; TS_ASSERT(!Mod::AreModsPlayCompatible(l1, l2)); } { ModList l1 = { &c }; ModList l2 = {}; TS_ASSERT(Mod::AreModsPlayCompatible(l1, l2)); } { ModList l1 = {}; ModList l2 = { &b }; TS_ASSERT(!Mod::AreModsPlayCompatible(l1, l2)); } } };