]> git.lizzy.rs Git - dragonfireclient.git/blob - src/mods.h
Fix various variables passed by copy instead of const ref (#5610)
[dragonfireclient.git] / src / mods.h
1 /*
2 Minetest
3 Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation; either version 2.1 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 GNU Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20 #ifndef MODS_HEADER
21 #define MODS_HEADER
22
23 #include "irrlichttypes.h"
24 #include <list>
25 #include <set>
26 #include <vector>
27 #include <string>
28 #include <map>
29 #include <json/json.h>
30 #include "util/cpp11_container.h"
31 #include "config.h"
32 #include "metadata.h"
33
34 #define MODNAME_ALLOWED_CHARS "abcdefghijklmnopqrstuvwxyz0123456789_"
35
36 struct ModSpec
37 {
38         std::string name;
39         std::string path;
40         //if normal mod:
41         UNORDERED_SET<std::string> depends;
42         UNORDERED_SET<std::string> optdepends;
43         UNORDERED_SET<std::string> unsatisfied_depends;
44
45         bool part_of_modpack;
46         bool is_modpack;
47         // if modpack:
48         std::map<std::string,ModSpec> modpack_content;
49         ModSpec(const std::string &name_="", const std::string &path_=""):
50                 name(name_),
51                 path(path_),
52                 depends(),
53                 optdepends(),
54                 unsatisfied_depends(),
55                 part_of_modpack(false),
56                 is_modpack(false),
57                 modpack_content()
58         {}
59 };
60
61 // Retrieves depends, optdepends, is_modpack and modpack_content
62 void parseModContents(ModSpec &mod);
63
64 std::map<std::string,ModSpec> getModsInPath(std::string path, bool part_of_modpack = false);
65
66 // replaces modpack Modspecs with their content
67 std::vector<ModSpec> flattenMods(std::map<std::string,ModSpec> mods);
68
69 // a ModConfiguration is a subset of installed mods, expected to have
70 // all dependencies fullfilled, so it can be used as a list of mods to
71 // load when the game starts.
72 class ModConfiguration
73 {
74 public:
75         // checks if all dependencies are fullfilled.
76         bool isConsistent() const
77         {
78                 return m_unsatisfied_mods.empty();
79         }
80
81         std::vector<ModSpec> getMods()
82         {
83                 return m_sorted_mods;
84         }
85
86         const std::vector<ModSpec> &getUnsatisfiedMods() const
87         {
88                 return m_unsatisfied_mods;
89         }
90
91         void printUnsatisfiedModsError() const;
92
93 protected:
94         ModConfiguration(const std::string &worldpath);
95         // adds all mods in the given path. used for games, modpacks
96         // and world-specific mods (worldmods-folders)
97         void addModsInPath(const std::string &path);
98
99         // adds all mods in the set.
100         void addMods(const std::vector<ModSpec> &new_mods);
101
102         void checkConflictsAndDeps();
103 private:
104         // move mods from m_unsatisfied_mods to m_sorted_mods
105         // in an order that satisfies dependencies
106         void resolveDependencies();
107
108         // mods with unmet dependencies. Before dependencies are resolved,
109         // this is where all mods are stored. Afterwards this contains
110         // only the ones with really unsatisfied dependencies.
111         std::vector<ModSpec> m_unsatisfied_mods;
112
113         // list of mods sorted such that they can be loaded in the
114         // given order with all dependencies being fullfilled. I.e.,
115         // every mod in this list has only dependencies on mods which
116         // appear earlier in the vector.
117         std::vector<ModSpec> m_sorted_mods;
118
119         // set of mod names for which an unresolved name conflict
120         // exists. A name conflict happens when two or more mods
121         // at the same level have the same name but different paths.
122         // Levels (mods in higher levels override mods in lower levels):
123         // 1. game mod in modpack; 2. game mod;
124         // 3. world mod in modpack; 4. world mod;
125         // 5. addon mod in modpack; 6. addon mod.
126         UNORDERED_SET<std::string> m_name_conflicts;
127
128         // Deleted default constructor
129         ModConfiguration() {}
130
131 };
132
133 class ServerModConfiguration: public ModConfiguration
134 {
135 public:
136         ServerModConfiguration(const std::string &worldpath);
137
138 };
139
140 #ifndef SERVER
141 class ClientModConfiguration: public ModConfiguration
142 {
143 public:
144         ClientModConfiguration(const std::string &path);
145 };
146 #endif
147
148 #if USE_CURL
149 Json::Value getModstoreUrl(const std::string &url);
150 #else
151 inline Json::Value getModstoreUrl(const std::string &url)
152 {
153         return Json::Value();
154 }
155 #endif
156
157 struct ModLicenseInfo {
158         int id;
159         std::string shortinfo;
160         std::string url;
161 };
162
163 struct ModAuthorInfo {
164         int id;
165         std::string username;
166 };
167
168 struct ModStoreMod {
169         int id;
170         std::string title;
171         std::string basename;
172         ModAuthorInfo author;
173         float rating;
174         bool valid;
175 };
176
177 struct ModStoreCategoryInfo {
178         int id;
179         std::string name;
180 };
181
182 struct ModStoreVersionEntry {
183         int id;
184         std::string date;
185         std::string file;
186         bool approved;
187         //ugly version number
188         int mtversion;
189 };
190
191 struct ModStoreTitlePic {
192         int id;
193         std::string file;
194         std::string description;
195         int mod;
196 };
197
198 struct ModStoreModDetails {
199         /* version_set?? */
200         std::vector<ModStoreCategoryInfo> categories;
201         ModAuthorInfo author;
202         ModLicenseInfo license;
203         ModStoreTitlePic titlepic;
204         int id;
205         std::string title;
206         std::string basename;
207         std::string description;
208         std::string repository;
209         float rating;
210         std::vector<std::string> depends;
211         std::vector<std::string> softdeps;
212
213         std::string download_url;
214         std::string screenshot_url;
215         std::vector<ModStoreVersionEntry> versions;
216         bool valid;
217 };
218
219 class ModMetadata: public Metadata
220 {
221 public:
222         ModMetadata(const std::string &mod_name);
223         ~ModMetadata() {}
224
225         virtual void clear();
226
227         bool save(const std::string &root_path);
228         bool load(const std::string &root_path);
229
230         bool isModified() const { return m_modified; }
231         const std::string &getModName() const { return m_mod_name; }
232
233         virtual bool setString(const std::string &name, const std::string &var);
234 private:
235         std::string m_mod_name;
236         bool m_modified;
237 };
238
239 #endif