Line data Source code
1 : /* Copyright (C) 2026 Wildfire Games. 2 : * This file is part of 0 A.D. 3 : * 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. 8 : * 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. 13 : * 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/>. 16 : */ 17 : 18 : #ifndef INCLUDED_MOD 19 : #define INCLUDED_MOD 20 : 21 : #include "ps/CStr.h" 22 : 23 : #include <vector> 24 : 25 : namespace Script { class Interface; } 26 : 27 : #define g_Mods (Mod::Instance()) 28 : 29 : class Mod 30 : { 31 : friend class TestMod; 32 : public: 33 : // Singleton-like interface. 34 : static Mod& Instance(); 35 : 36 : /** 37 : * Parsed mod.json data for C++ usage. 38 : * Note that converting to/from JS is lossy. 39 : */ 40 0 : struct ModData 41 : { 42 : // 'Folder name' of the mod, e.g. 'public' for the main 0 A.D. mod. 43 : CStr m_Pathname; 44 : // "name" property in the mod.json 45 : CStr m_Name; 46 : 47 : CStr m_Version; 48 : std::vector<CStr> m_Dependencies; 49 : // If true, the mod is assumed to be 'GUI-only', i.e. ignored for MP or replay compatibility checks. 50 : bool m_IgnoreInCompatibilityChecks; 51 : 52 : // For convenience when exporting to JS, keep a record of the full file. 53 : CStr m_Text; 54 : }; 55 : 56 : const std::vector<CStr>& GetEnabledMods() const; 57 : const std::vector<CStr>& GetIncompatibleMods() const; 58 : const std::vector<ModData>& GetAvailableMods() const; 59 : 60 : /** 61 : * Fetches available mods and stores some metadata about them. 62 : * This may open the zipped mod archives, depending on the situation, 63 : * and/or try to write files to the user mod folder, 64 : * which can be quite slow, so should be run rarely. 65 : * TODO: if this did not need the scriptInterface to parse JSON, 66 : * we could run it in different contexts and possibly cleaner. 67 : */ 68 : void UpdateAvailableMods(const Script::Interface& scriptInterface); 69 : 70 : /** 71 : * Enables specified mods (& mods required by the engine). 72 : * @param addPublic - if true, enable the public mod. 73 : * @return whether the mods were enabled successfully. This can fail if e.g. mods are incompatible. 74 : * If true, GetEnabledMods() should be non-empty, GetIncompatibleMods() empty. Otherwise, GetIncompatibleMods() is non-empty. 75 : */ 76 : bool EnableMods(const std::vector<CStr>& mods, const bool addPublic); 77 : 78 : /** 79 : * Get data for the given mod. 80 : * @param the mod path name (e.g. 'public') 81 : * @return the mod data or nullptr if unavailable. 82 : * TODO: switch to std::optional or something related. 83 : */ 84 : const ModData* GetModData(const CStr& mod) const; 85 : 86 : /** 87 : * Get a list of the enabled mod's data (intended for compatibility checks). 88 : * "user" mod and "mod" mod are ignored as they are irrelevant for compatibility checks. 89 : */ 90 : const std::vector<const Mod::ModData*> GetEnabledModsData() const; 91 : 92 : /** 93 : * @return whether the two lists are compatible for replaying / MP play. 94 : */ 95 : static bool AreModsPlayCompatible(const std::vector<const Mod::ModData*>& modsA, const std::vector<const Mod::ModData*>& modsB); 96 : private: 97 : 98 : /** 99 : * Checks a list of @a mods and returns the incompatible mods, if any. 100 : */ 101 : std::vector<CStr> CheckForIncompatibleMods(const std::vector<CStr>& mods) const; 102 : bool CompareVersionStrings(const CStr& required, const CStr& op, const CStr& version) const; 103 : 104 : std::vector<CStr> m_EnabledMods; 105 : // Of the currently loaded mods, these are the incompatible with the engine and cannot be loaded. 106 : std::vector<CStr> m_IncompatibleMods; 107 : 108 : std::vector<ModData> m_AvailableMods; 109 : }; 110 : 111 : #endif // INCLUDED_MOD