3 Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
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.
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.
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.
22 #include "irrlichttypes.h"
28 #include <json/json.h>
29 #include <unordered_set>
30 #include "util/basic_macros.h"
34 #define MODNAME_ALLOWED_CHARS "abcdefghijklmnopqrstuvwxyz0123456789_"
45 std::unordered_set<std::string> depends;
46 std::unordered_set<std::string> optdepends;
47 std::unordered_set<std::string> unsatisfied_depends;
49 bool part_of_modpack = false;
50 bool is_modpack = false;
53 std::map<std::string, ModSpec> modpack_content;
54 ModSpec(const std::string &name = "", const std::string &path = "") :
55 name(name), path(path)
58 ModSpec(const std::string &name, const std::string &path, bool part_of_modpack) :
59 name(name), path(path), part_of_modpack(part_of_modpack)
64 // Retrieves depends, optdepends, is_modpack and modpack_content
65 void parseModContents(ModSpec &mod);
67 std::map<std::string, ModSpec> getModsInPath(
68 const std::string &path, bool part_of_modpack = false);
70 // replaces modpack Modspecs with their content
71 std::vector<ModSpec> flattenMods(std::map<std::string, ModSpec> mods);
73 // a ModConfiguration is a subset of installed mods, expected to have
74 // all dependencies fullfilled, so it can be used as a list of mods to
75 // load when the game starts.
76 class ModConfiguration
79 // checks if all dependencies are fullfilled.
80 bool isConsistent() const { return m_unsatisfied_mods.empty(); }
82 const std::vector<ModSpec> &getMods() const { return m_sorted_mods; }
84 const std::vector<ModSpec> &getUnsatisfiedMods() const
86 return m_unsatisfied_mods;
89 void printUnsatisfiedModsError() const;
92 ModConfiguration(const std::string &worldpath);
93 // adds all mods in the given path. used for games, modpacks
94 // and world-specific mods (worldmods-folders)
95 void addModsInPath(const std::string &path);
97 // adds all mods in the set.
98 void addMods(const std::vector<ModSpec> &new_mods);
100 void addModsFromConfig(const std::string &settings_path,
101 const std::set<std::string> &mods);
103 void checkConflictsAndDeps();
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;
113 // move mods from m_unsatisfied_mods to m_sorted_mods
114 // in an order that satisfies dependencies
115 void resolveDependencies();
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;
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;
131 // Deleted default constructor
132 ModConfiguration() = default;
136 class ClientModConfiguration : public ModConfiguration
139 ClientModConfiguration(const std::string &path);
143 class ModMetadata : public Metadata
146 ModMetadata() = delete;
147 ModMetadata(const std::string &mod_name);
148 ~ModMetadata() = default;
150 virtual void clear();
152 bool save(const std::string &root_path);
153 bool load(const std::string &root_path);
155 bool isModified() const { return m_modified; }
156 const std::string &getModName() const { return m_mod_name; }
158 virtual bool setString(const std::string &name, const std::string &var);
161 std::string m_mod_name;
162 bool m_modified = false;