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