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