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