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