]> git.lizzy.rs Git - minetest.git/blob - src/content/mods.cpp
Add keybind to swap items between hands
[minetest.git] / src / content / mods.cpp
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 #include <cctype>
21 #include <fstream>
22 #include <json/json.h>
23 #include <algorithm>
24 #include "content/mods.h"
25 #include "database/database.h"
26 #include "filesys.h"
27 #include "log.h"
28 #include "content/subgames.h"
29 #include "settings.h"
30 #include "porting.h"
31 #include "convert_json.h"
32 #include "script/common/c_internal.h"
33
34 void ModSpec::checkAndLog() const
35 {
36         if (!string_allowed(name, MODNAME_ALLOWED_CHARS)) {
37                 throw ModError("Error loading mod \"" + name +
38                         "\": Mod name does not follow naming conventions: "
39                                 "Only characters [a-z0-9_] are allowed.");
40         }
41
42         // Log deprecation messages
43         auto handling_mode = get_deprecated_handling_mode();
44         if (!deprecation_msgs.empty() && handling_mode != DeprecatedHandlingMode::Ignore) {
45                 std::ostringstream os;
46                 os << "Mod " << name << " at " << path << ":" << std::endl;
47                 for (auto msg : deprecation_msgs)
48                         os << "\t" << msg << std::endl;
49
50                 if (handling_mode == DeprecatedHandlingMode::Error)
51                         throw ModError(os.str());
52                 else
53                         warningstream << os.str();
54         }
55 }
56
57 bool parseDependsString(std::string &dep, std::unordered_set<char> &symbols)
58 {
59         dep = trim(dep);
60         symbols.clear();
61         size_t pos = dep.size();
62         while (pos > 0 &&
63                         !string_allowed(dep.substr(pos - 1, 1), MODNAME_ALLOWED_CHARS)) {
64                 // last character is a symbol, not part of the modname
65                 symbols.insert(dep[pos - 1]);
66                 --pos;
67         }
68         dep = trim(dep.substr(0, pos));
69         return !dep.empty();
70 }
71
72 bool parseModContents(ModSpec &spec)
73 {
74         // NOTE: this function works in mutual recursion with getModsInPath
75
76         spec.depends.clear();
77         spec.optdepends.clear();
78         spec.is_modpack = false;
79         spec.modpack_content.clear();
80
81         // Handle modpacks (defined by containing modpack.txt)
82         if (fs::IsFile(spec.path + DIR_DELIM + "modpack.txt") ||
83                         fs::IsFile(spec.path + DIR_DELIM + "modpack.conf")) {
84                 spec.is_modpack = true;
85                 spec.modpack_content = getModsInPath(spec.path, spec.virtual_path, true);
86                 return true;
87         } else if (!fs::IsFile(spec.path + DIR_DELIM + "init.lua")) {
88                 return false;
89         }
90
91
92         Settings info;
93         info.readConfigFile((spec.path + DIR_DELIM + "mod.conf").c_str());
94
95         if (info.exists("name"))
96                 spec.name = info.get("name");
97         else
98                 spec.deprecation_msgs.push_back("Mods not having a mod.conf file with the name is deprecated.");
99
100         if (info.exists("author"))
101                 spec.author = info.get("author");
102
103         if (info.exists("release"))
104                 spec.release = info.getS32("release");
105
106         // Attempt to load dependencies from mod.conf
107         bool mod_conf_has_depends = false;
108         if (info.exists("depends")) {
109                 mod_conf_has_depends = true;
110                 std::string dep = info.get("depends");
111                 // clang-format off
112                 dep.erase(std::remove_if(dep.begin(), dep.end(),
113                                 static_cast<int (*)(int)>(&std::isspace)), dep.end());
114                 // clang-format on
115                 for (const auto &dependency : str_split(dep, ',')) {
116                         spec.depends.insert(dependency);
117                 }
118         }
119
120         if (info.exists("optional_depends")) {
121                 mod_conf_has_depends = true;
122                 std::string dep = info.get("optional_depends");
123                 // clang-format off
124                 dep.erase(std::remove_if(dep.begin(), dep.end(),
125                                 static_cast<int (*)(int)>(&std::isspace)), dep.end());
126                 // clang-format on
127                 for (const auto &dependency : str_split(dep, ',')) {
128                         spec.optdepends.insert(dependency);
129                 }
130         }
131
132         // Fallback to depends.txt
133         if (!mod_conf_has_depends) {
134                 std::vector<std::string> dependencies;
135
136                 std::ifstream is((spec.path + DIR_DELIM + "depends.txt").c_str());
137
138                 if (is.good())
139                         spec.deprecation_msgs.push_back("depends.txt is deprecated, please use mod.conf instead.");
140
141                 while (is.good()) {
142                         std::string dep;
143                         std::getline(is, dep);
144                         dependencies.push_back(dep);
145                 }
146
147                 for (auto &dependency : dependencies) {
148                         std::unordered_set<char> symbols;
149                         if (parseDependsString(dependency, symbols)) {
150                                 if (symbols.count('?') != 0) {
151                                         spec.optdepends.insert(dependency);
152                                 } else {
153                                         spec.depends.insert(dependency);
154                                 }
155                         }
156                 }
157         }
158
159         if (info.exists("description"))
160                 spec.desc = info.get("description");
161         else if (fs::ReadFile(spec.path + DIR_DELIM + "description.txt", spec.desc))
162                 spec.deprecation_msgs.push_back("description.txt is deprecated, please use mod.conf instead.");
163
164         return true;
165 }
166
167 std::map<std::string, ModSpec> getModsInPath(
168                 const std::string &path, const std::string &virtual_path, bool part_of_modpack)
169 {
170         // NOTE: this function works in mutual recursion with parseModContents
171
172         std::map<std::string, ModSpec> result;
173         std::vector<fs::DirListNode> dirlist = fs::GetDirListing(path);
174         std::string mod_path;
175         std::string mod_virtual_path;
176
177         for (const fs::DirListNode &dln : dirlist) {
178                 if (!dln.dir)
179                         continue;
180
181                 const std::string &modname = dln.name;
182                 // Ignore all directories beginning with a ".", especially
183                 // VCS directories like ".git" or ".svn"
184                 if (modname[0] == '.')
185                         continue;
186
187                 mod_path.clear();
188                 mod_path.append(path).append(DIR_DELIM).append(modname);
189
190                 mod_virtual_path.clear();
191                 // Intentionally uses / to keep paths same on different platforms
192                 mod_virtual_path.append(virtual_path).append("/").append(modname);
193
194                 ModSpec spec(modname, mod_path, part_of_modpack, mod_virtual_path);
195                 parseModContents(spec);
196                 result.insert(std::make_pair(modname, spec));
197         }
198         return result;
199 }
200
201 std::vector<ModSpec> flattenMods(const std::map<std::string, ModSpec> &mods)
202 {
203         std::vector<ModSpec> result;
204         for (const auto &it : mods) {
205                 const ModSpec &mod = it.second;
206                 if (mod.is_modpack) {
207                         std::vector<ModSpec> content = flattenMods(mod.modpack_content);
208                         result.reserve(result.size() + content.size());
209                         result.insert(result.end(), content.begin(), content.end());
210
211                 } else // not a modpack
212                 {
213                         result.push_back(mod);
214                 }
215         }
216         return result;
217 }
218
219
220 ModStorage::ModStorage(const std::string &mod_name, ModStorageDatabase *database):
221         m_mod_name(mod_name), m_database(database)
222 {
223 }
224
225 void ModStorage::clear()
226 {
227         m_database->removeModEntries(m_mod_name);
228 }
229
230 bool ModStorage::contains(const std::string &name) const
231 {
232         return m_database->hasModEntry(m_mod_name, name);
233 }
234
235 bool ModStorage::setString(const std::string &name, const std::string &var)
236 {
237         if (var.empty())
238                 return m_database->removeModEntry(m_mod_name, name);
239         else
240                 return m_database->setModEntry(m_mod_name, name, var);
241 }
242
243 const StringMap &ModStorage::getStrings(StringMap *place) const
244 {
245         place->clear();
246         m_database->getModEntries(m_mod_name, place);
247         return *place;
248 }
249
250 const std::vector<std::string> &ModStorage::getKeys(std::vector<std::string> *place) const
251 {
252         place->clear();
253         m_database->getModKeys(m_mod_name, place);
254         return *place;
255 }
256
257 const std::string *ModStorage::getStringRaw(const std::string &name, std::string *place) const
258 {
259         return m_database->getModEntry(m_mod_name, name, place) ? place : nullptr;
260 }