]> git.lizzy.rs Git - dragonfireclient.git/blob - src/mods.h
Remove remaining modstore code (#6120)
[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 <unordered_set>
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         std::unordered_set<std::string> depends;
42         std::unordered_set<std::string> optdepends;
43         std::unordered_set<std::string> unsatisfied_depends;
44
45         bool part_of_modpack = false;
46         bool is_modpack = false;
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         {}
53 };
54
55 // Retrieves depends, optdepends, is_modpack and modpack_content
56 void parseModContents(ModSpec &mod);
57
58 std::map<std::string,ModSpec> getModsInPath(std::string path, bool part_of_modpack = false);
59
60 // replaces modpack Modspecs with their content
61 std::vector<ModSpec> flattenMods(std::map<std::string,ModSpec> mods);
62
63 // a ModConfiguration is a subset of installed mods, expected to have
64 // all dependencies fullfilled, so it can be used as a list of mods to
65 // load when the game starts.
66 class ModConfiguration
67 {
68 public:
69         // checks if all dependencies are fullfilled.
70         bool isConsistent() const
71         {
72                 return m_unsatisfied_mods.empty();
73         }
74
75         std::vector<ModSpec> getMods()
76         {
77                 return m_sorted_mods;
78         }
79
80         const std::vector<ModSpec> &getUnsatisfiedMods() const
81         {
82                 return m_unsatisfied_mods;
83         }
84
85         void printUnsatisfiedModsError() const;
86
87 protected:
88         ModConfiguration(const std::string &worldpath);
89         // adds all mods in the given path. used for games, modpacks
90         // and world-specific mods (worldmods-folders)
91         void addModsInPath(const std::string &path);
92
93         // adds all mods in the set.
94         void addMods(const std::vector<ModSpec> &new_mods);
95
96         void addModsFormConfig(const std::string &settings_path, const std::set<std::string> &mods);
97
98         void checkConflictsAndDeps();
99 private:
100         // move mods from m_unsatisfied_mods to m_sorted_mods
101         // in an order that satisfies dependencies
102         void resolveDependencies();
103
104         // mods with unmet dependencies. Before dependencies are resolved,
105         // this is where all mods are stored. Afterwards this contains
106         // only the ones with really unsatisfied dependencies.
107         std::vector<ModSpec> m_unsatisfied_mods;
108
109         // list of mods sorted such that they can be loaded in the
110         // given order with all dependencies being fullfilled. I.e.,
111         // every mod in this list has only dependencies on mods which
112         // appear earlier in the vector.
113         std::vector<ModSpec> m_sorted_mods;
114
115         // set of mod names for which an unresolved name conflict
116         // exists. A name conflict happens when two or more mods
117         // at the same level have the same name but different paths.
118         // Levels (mods in higher levels override mods in lower levels):
119         // 1. game mod in modpack; 2. game mod;
120         // 3. world mod in modpack; 4. world mod;
121         // 5. addon mod in modpack; 6. addon mod.
122         std::unordered_set<std::string> m_name_conflicts;
123
124         // Deleted default constructor
125         ModConfiguration() {}
126
127 };
128
129 class ServerModConfiguration: public ModConfiguration
130 {
131 public:
132         ServerModConfiguration(const std::string &worldpath);
133
134 };
135
136 #ifndef SERVER
137 class ClientModConfiguration: public ModConfiguration
138 {
139 public:
140         ClientModConfiguration(const std::string &path);
141 };
142 #endif
143
144 struct ModLicenseInfo {
145         int id;
146         std::string shortinfo;
147         std::string url;
148 };
149
150 struct ModAuthorInfo {
151         int id;
152         std::string username;
153 };
154
155 class ModMetadata: public Metadata
156 {
157 public:
158         ModMetadata(const std::string &mod_name);
159         ~ModMetadata() {}
160
161         virtual void clear();
162
163         bool save(const std::string &root_path);
164         bool load(const std::string &root_path);
165
166         bool isModified() const { return m_modified; }
167         const std::string &getModName() const { return m_mod_name; }
168
169         virtual bool setString(const std::string &name, const std::string &var);
170 private:
171         std::string m_mod_name;
172         bool m_modified = false;
173 };
174
175 #endif