]> git.lizzy.rs Git - dragonfireclient.git/blob - src/mods.h
Huge LBM lookup performance improvement on mapblock loading (#7195)
[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 #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         std::string desc;
41
42         //if normal mod:
43         std::unordered_set<std::string> depends;
44         std::unordered_set<std::string> optdepends;
45         std::unordered_set<std::string> unsatisfied_depends;
46
47         bool part_of_modpack = false;
48         bool is_modpack = false;
49
50         // if modpack:
51         std::map<std::string,ModSpec> modpack_content;
52         ModSpec(const std::string &name_ = "", const std::string &path_ = ""):
53                 name(name_),
54                 path(path_)
55         {}
56         ModSpec(const std::string &name_, const std::string &path_, bool part_of_modpack_):
57                 name(name_),
58                 path(path_),
59                 part_of_modpack(part_of_modpack_)
60         {}
61 };
62
63 // Retrieves depends, optdepends, is_modpack and modpack_content
64 void parseModContents(ModSpec &mod);
65
66 std::map<std::string,ModSpec> getModsInPath(const std::string &path,
67         bool part_of_modpack = false);
68
69 // replaces modpack Modspecs with their content
70 std::vector<ModSpec> flattenMods(std::map<std::string,ModSpec> mods);
71
72 // a ModConfiguration is a subset of installed mods, expected to have
73 // all dependencies fullfilled, so it can be used as a list of mods to
74 // load when the game starts.
75 class ModConfiguration
76 {
77 public:
78         // checks if all dependencies are fullfilled.
79         bool isConsistent() const
80         {
81                 return m_unsatisfied_mods.empty();
82         }
83
84         const std::vector<ModSpec> &getMods() const
85         {
86                 return m_sorted_mods;
87         }
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, const std::set<std::string> &mods);
106
107         void checkConflictsAndDeps();
108 protected:
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 private:
116         // move mods from m_unsatisfied_mods to m_sorted_mods
117         // in an order that satisfies dependencies
118         void resolveDependencies();
119
120         // mods with unmet dependencies. Before dependencies are resolved,
121         // this is where all mods are stored. Afterwards this contains
122         // only the ones with really unsatisfied dependencies.
123         std::vector<ModSpec> m_unsatisfied_mods;
124
125         // set of mod names for which an unresolved name conflict
126         // exists. A name conflict happens when two or more mods
127         // at the same level have the same name but different paths.
128         // Levels (mods in higher levels override mods in lower levels):
129         // 1. game mod in modpack; 2. game mod;
130         // 3. world mod in modpack; 4. world mod;
131         // 5. addon mod in modpack; 6. addon mod.
132         std::unordered_set<std::string> m_name_conflicts;
133
134         // Deleted default constructor
135         ModConfiguration() = default;
136
137 };
138
139 #ifndef SERVER
140 class ClientModConfiguration: public ModConfiguration
141 {
142 public:
143         ClientModConfiguration(const std::string &path);
144 };
145 #endif
146
147 class ModMetadata: public Metadata
148 {
149 public:
150         ModMetadata() = delete;
151         ModMetadata(const std::string &mod_name);
152         ~ModMetadata() = default;
153
154         virtual void clear();
155
156         bool save(const std::string &root_path);
157         bool load(const std::string &root_path);
158
159         bool isModified() const { return m_modified; }
160         const std::string &getModName() const { return m_mod_name; }
161
162         virtual bool setString(const std::string &name, const std::string &var);
163 private:
164         std::string m_mod_name;
165         bool m_modified = false;
166 };