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