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