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