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 <json/json.h>
24 #include "content/mods.h"
25 #include "database/database.h"
28 #include "content/subgames.h"
31 #include "convert_json.h"
32 #include "script/common/c_internal.h"
34 void ModSpec::checkAndLog() const
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.");
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;
50 if (handling_mode == DeprecatedHandlingMode::Error)
51 throw ModError(os.str());
53 warningstream << os.str();
57 bool parseDependsString(std::string &dep, std::unordered_set<char> &symbols)
61 size_t pos = dep.size();
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]);
68 dep = trim(dep.substr(0, pos));
72 bool parseModContents(ModSpec &spec)
74 // NOTE: this function works in mutual recursion with getModsInPath
77 spec.optdepends.clear();
78 spec.is_modpack = false;
79 spec.modpack_content.clear();
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);
87 } else if (!fs::IsFile(spec.path + DIR_DELIM + "init.lua")) {
93 info.readConfigFile((spec.path + DIR_DELIM + "mod.conf").c_str());
95 if (info.exists("name"))
96 spec.name = info.get("name");
98 spec.deprecation_msgs.push_back("Mods not having a mod.conf file with the name is deprecated.");
100 if (info.exists("author"))
101 spec.author = info.get("author");
103 if (info.exists("release"))
104 spec.release = info.getS32("release");
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");
112 dep.erase(std::remove_if(dep.begin(), dep.end(),
113 static_cast<int (*)(int)>(&std::isspace)), dep.end());
115 for (const auto &dependency : str_split(dep, ',')) {
116 spec.depends.insert(dependency);
120 if (info.exists("optional_depends")) {
121 mod_conf_has_depends = true;
122 std::string dep = info.get("optional_depends");
124 dep.erase(std::remove_if(dep.begin(), dep.end(),
125 static_cast<int (*)(int)>(&std::isspace)), dep.end());
127 for (const auto &dependency : str_split(dep, ',')) {
128 spec.optdepends.insert(dependency);
132 // Fallback to depends.txt
133 if (!mod_conf_has_depends) {
134 std::vector<std::string> dependencies;
136 std::ifstream is((spec.path + DIR_DELIM + "depends.txt").c_str());
139 spec.deprecation_msgs.push_back("depends.txt is deprecated, please use mod.conf instead.");
143 std::getline(is, dep);
144 dependencies.push_back(dep);
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);
153 spec.depends.insert(dependency);
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.");
167 std::map<std::string, ModSpec> getModsInPath(
168 const std::string &path, const std::string &virtual_path, bool part_of_modpack)
170 // NOTE: this function works in mutual recursion with parseModContents
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;
177 for (const fs::DirListNode &dln : dirlist) {
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] == '.')
188 mod_path.append(path).append(DIR_DELIM).append(modname);
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);
194 ModSpec spec(modname, mod_path, part_of_modpack, mod_virtual_path);
195 parseModContents(spec);
196 result.insert(std::make_pair(modname, spec));
201 std::vector<ModSpec> flattenMods(const std::map<std::string, ModSpec> &mods)
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());
211 } else // not a modpack
213 result.push_back(mod);
220 ModStorage::ModStorage(const std::string &mod_name, ModStorageDatabase *database):
221 m_mod_name(mod_name), m_database(database)
225 void ModStorage::clear()
227 m_database->removeModEntries(m_mod_name);
230 bool ModStorage::contains(const std::string &name) const
232 return m_database->hasModEntry(m_mod_name, name);
235 bool ModStorage::setString(const std::string &name, const std::string &var)
238 return m_database->removeModEntry(m_mod_name, name);
240 return m_database->setModEntry(m_mod_name, name, var);
243 const StringMap &ModStorage::getStrings(StringMap *place) const
246 m_database->getModEntries(m_mod_name, place);
250 const std::vector<std::string> &ModStorage::getKeys(std::vector<std::string> *place) const
253 m_database->getModKeys(m_mod_name, place);
257 const std::string *ModStorage::getStringRaw(const std::string &name, std::string *place) const
259 return m_database->getModEntry(m_mod_name, name, place) ? place : nullptr;