Fixes for compiling with a newer (system) jsoncpp (#4429)
[oweals/minetest.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 "config.h"
31
32 #define MODNAME_ALLOWED_CHARS "abcdefghijklmnopqrstuvwxyz0123456789_"
33
34 struct ModSpec
35 {
36         std::string name;
37         std::string path;
38         //if normal mod:
39         std::set<std::string> depends;
40         std::set<std::string> optdepends;
41         std::set<std::string> unsatisfied_depends;
42
43         bool part_of_modpack;
44         bool is_modpack;
45         // if modpack:
46         std::map<std::string,ModSpec> modpack_content;
47         ModSpec(const std::string &name_="", const std::string &path_=""):
48                 name(name_),
49                 path(path_),
50                 depends(),
51                 optdepends(),
52                 unsatisfied_depends(),
53                 part_of_modpack(false),
54                 is_modpack(false),
55                 modpack_content()
56         {}
57 };
58
59 // Retrieves depends, optdepends, is_modpack and modpack_content
60 void parseModContents(ModSpec &mod);
61
62 std::map<std::string,ModSpec> getModsInPath(std::string path, bool part_of_modpack = false);
63
64 // If failed, returned modspec has name==""
65 ModSpec findCommonMod(const std::string &modname);
66
67 // expands modpack contents, but does not replace them.
68 std::map<std::string, ModSpec> flattenModTree(std::map<std::string, ModSpec> mods);
69
70 // replaces modpack Modspecs with their content
71 std::vector<ModSpec> flattenMods(std::map<std::string,ModSpec> mods);
72
73 // a ModConfiguration is a subset of installed mods, expected to have
74 // all dependencies fullfilled, so it can be used as a list of mods to
75 // load when the game starts.
76 class ModConfiguration
77 {
78 public:
79         ModConfiguration():
80                 m_unsatisfied_mods(),
81                 m_sorted_mods(),
82                 m_name_conflicts()
83         {}
84
85
86         ModConfiguration(std::string worldpath);
87
88         // checks if all dependencies are fullfilled.
89         bool isConsistent()
90         {
91                 return m_unsatisfied_mods.empty();
92         }
93
94         std::vector<ModSpec> getMods()
95         {
96                 return m_sorted_mods;
97         }
98
99         std::vector<ModSpec> getUnsatisfiedMods()
100         {
101                 return m_unsatisfied_mods;
102         }
103
104 private:
105         // adds all mods in the given path. used for games, modpacks
106         // and world-specific mods (worldmods-folders)
107         void addModsInPath(std::string path);
108
109         // adds all mods in the set.
110         void addMods(std::vector<ModSpec> new_mods);
111
112         // move mods from m_unsatisfied_mods to m_sorted_mods
113         // in an order that satisfies dependencies
114         void resolveDependencies();
115
116         // mods with unmet dependencies. Before dependencies are resolved,
117         // this is where all mods are stored. Afterwards this contains
118         // only the ones with really unsatisfied dependencies.
119         std::vector<ModSpec> m_unsatisfied_mods;
120
121         // list of mods sorted such that they can be loaded in the
122         // given order with all dependencies being fullfilled. I.e.,
123         // every mod in this list has only dependencies on mods which
124         // appear earlier in the vector.
125         std::vector<ModSpec> m_sorted_mods;
126
127         // set of mod names for which an unresolved name conflict
128         // exists. A name conflict happens when two or more mods
129         // at the same level have the same name but different paths.
130         // Levels (mods in higher levels override mods in lower levels):
131         // 1. game mod in modpack; 2. game mod;
132         // 3. world mod in modpack; 4. world mod;
133         // 5. addon mod in modpack; 6. addon mod.
134         std::set<std::string> m_name_conflicts;
135
136 };
137
138 #if USE_CURL
139 Json::Value getModstoreUrl(std::string url);
140 #else
141 inline Json::Value getModstoreUrl(std::string url) {
142         return Json::Value();
143 }
144 #endif
145
146 struct ModLicenseInfo {
147         int id;
148         std::string shortinfo;
149         std::string url;
150 };
151
152 struct ModAuthorInfo {
153         int id;
154         std::string username;
155 };
156
157 struct ModStoreMod {
158         int id;
159         std::string title;
160         std::string basename;
161         ModAuthorInfo author;
162         float rating;
163         bool valid;
164 };
165
166 struct ModStoreCategoryInfo {
167         int id;
168         std::string name;
169 };
170
171 struct ModStoreVersionEntry {
172         int id;
173         std::string date;
174         std::string file;
175         bool approved;
176         //ugly version number
177         int mtversion;
178 };
179
180 struct ModStoreTitlePic {
181         int id;
182         std::string file;
183         std::string description;
184         int mod;
185 };
186
187 struct ModStoreModDetails {
188         /* version_set?? */
189         std::vector<ModStoreCategoryInfo> categories;
190         ModAuthorInfo author;
191         ModLicenseInfo license;
192         ModStoreTitlePic titlepic;
193         int id;
194         std::string title;
195         std::string basename;
196         std::string description;
197         std::string repository;
198         float rating;
199         std::vector<std::string> depends;
200         std::vector<std::string> softdeps;
201
202         std::string download_url;
203         std::string screenshot_url;
204         std::vector<ModStoreVersionEntry> versions;
205         bool valid;
206 };
207
208 #endif