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>
29 #include "convert_json.h"
31 static bool parseDependsLine(std::istream &is,
32 std::string &dep, std::set<char> &symbols)
34 std::getline(is, dep);
37 size_t pos = dep.size();
38 while(pos > 0 && !string_allowed(dep.substr(pos-1, 1), MODNAME_ALLOWED_CHARS)){
39 // last character is a symbol, not part of the modname
40 symbols.insert(dep[pos-1]);
43 dep = trim(dep.substr(0, pos));
47 void parseModContents(ModSpec &spec)
49 // NOTE: this function works in mutual recursion with getModsInPath
51 info.readConfigFile((spec.path+DIR_DELIM+"mod.conf").c_str());
53 if (info.exists("name"))
54 spec.name = info.get("name");
57 spec.optdepends.clear();
58 spec.is_modpack = false;
59 spec.modpack_content.clear();
61 // Handle modpacks (defined by containing modpack.txt)
62 std::ifstream modpack_is((spec.path+DIR_DELIM+"modpack.txt").c_str());
63 if(modpack_is.good()){ //a modpack, recursively get the mods in it
64 modpack_is.close(); // We don't actually need the file
65 spec.is_modpack = true;
66 spec.modpack_content = getModsInPath(spec.path, true);
68 // modpacks have no dependencies; they are defined and
69 // tracked separately for each mod in the modpack
71 else{ // not a modpack, parse the dependencies
72 std::ifstream is((spec.path+DIR_DELIM+"depends.txt").c_str());
75 std::set<char> symbols;
76 if(parseDependsLine(is, dep, symbols)){
77 if(symbols.count('?') != 0){
78 spec.optdepends.insert(dep);
81 spec.depends.insert(dep);
88 std::map<std::string, ModSpec> getModsInPath(std::string path, bool part_of_modpack)
90 // NOTE: this function works in mutual recursion with parseModContents
92 std::map<std::string, ModSpec> result;
93 std::vector<fs::DirListNode> dirlist = fs::GetDirListing(path);
94 for (const fs::DirListNode &dln : dirlist) {
97 const std::string &modname = dln.name;
98 // Ignore all directories beginning with a ".", especially
99 // VCS directories like ".git" or ".svn"
100 if (modname[0] == '.')
102 std::string modpath = path + DIR_DELIM + modname;
104 ModSpec spec(modname, modpath);
105 spec.part_of_modpack = part_of_modpack;
106 parseModContents(spec);
107 result.insert(std::make_pair(modname, spec));
112 std::vector<ModSpec> flattenMods(std::map<std::string, ModSpec> mods)
114 std::vector<ModSpec> result;
115 for (const auto &it : mods) {
116 const ModSpec &mod = it.second;
117 if (mod.is_modpack) {
118 std::vector<ModSpec> content = flattenMods(mod.modpack_content);
119 result.reserve(result.size() + content.size());
120 result.insert(result.end(),content.begin(),content.end());
125 result.push_back(mod);
131 ModConfiguration::ModConfiguration(const std::string &worldpath)
135 void ModConfiguration::printUnsatisfiedModsError() const
137 for (const ModSpec &mod : m_unsatisfied_mods) {
138 errorstream << "mod \"" << mod.name << "\" has unsatisfied dependencies: ";
139 for (const std::string &unsatisfied_depend : mod.unsatisfied_depends)
140 errorstream << " \"" << unsatisfied_depend << "\"";
141 errorstream << std::endl;
145 void ModConfiguration::addModsInPath(const std::string &path)
147 addMods(flattenMods(getModsInPath(path)));
150 void ModConfiguration::addMods(const std::vector<ModSpec> &new_mods)
152 // Maintain a map of all existing m_unsatisfied_mods.
153 // Keys are mod names and values are indices into m_unsatisfied_mods.
154 std::map<std::string, u32> existing_mods;
155 for(u32 i = 0; i < m_unsatisfied_mods.size(); ++i){
156 existing_mods[m_unsatisfied_mods[i].name] = i;
160 for(int want_from_modpack = 1; want_from_modpack >= 0; --want_from_modpack){
162 // Add all the mods that come from modpacks
164 // Add all the mods that didn't come from modpacks
166 std::set<std::string> seen_this_iteration;
168 for (const ModSpec &mod : new_mods) {
169 if (mod.part_of_modpack != (bool)want_from_modpack)
172 if (existing_mods.count(mod.name) == 0) {
173 // GOOD CASE: completely new mod.
174 m_unsatisfied_mods.push_back(mod);
175 existing_mods[mod.name] = m_unsatisfied_mods.size() - 1;
176 } else if(seen_this_iteration.count(mod.name) == 0) {
177 // BAD CASE: name conflict in different levels.
178 u32 oldindex = existing_mods[mod.name];
179 const ModSpec &oldmod = m_unsatisfied_mods[oldindex];
180 warningstream<<"Mod name conflict detected: \""
181 <<mod.name<<"\""<<std::endl
182 <<"Will not load: "<<oldmod.path<<std::endl
183 <<"Overridden by: "<<mod.path<<std::endl;
184 m_unsatisfied_mods[oldindex] = mod;
186 // If there was a "VERY BAD CASE" name conflict
187 // in an earlier level, ignore it.
188 m_name_conflicts.erase(mod.name);
190 // VERY BAD CASE: name conflict in the same level.
191 u32 oldindex = existing_mods[mod.name];
192 const ModSpec &oldmod = m_unsatisfied_mods[oldindex];
193 warningstream<<"Mod name conflict detected: \""
194 <<mod.name<<"\""<<std::endl
195 <<"Will not load: "<<oldmod.path<<std::endl
196 <<"Will not load: "<<mod.path<<std::endl;
197 m_unsatisfied_mods[oldindex] = mod;
198 m_name_conflicts.insert(mod.name);
201 seen_this_iteration.insert(mod.name);
206 void ModConfiguration::addModsFromConfig(const std::string &settings_path, const std::set<std::string> &mods)
209 std::set<std::string> load_mod_names;
211 conf.readConfigFile(settings_path.c_str());
212 std::vector<std::string> names = conf.getNames();
213 for (const std::string &name : names) {
214 if (name.compare(0,9,"load_mod_")==0 && conf.getBool(name))
215 load_mod_names.insert(name.substr(9));
218 std::vector<ModSpec> addon_mods;
219 for (const std::string &i : mods) {
220 std::vector<ModSpec> addon_mods_in_path = flattenMods(getModsInPath(i));
221 for (std::vector<ModSpec>::const_iterator it = addon_mods_in_path.begin();
222 it != addon_mods_in_path.end(); ++it) {
223 const ModSpec& mod = *it;
224 if (load_mod_names.count(mod.name) != 0)
225 addon_mods.push_back(mod);
227 conf.setBool("load_mod_" + mod.name, false);
230 conf.updateConfigFile(settings_path.c_str());
233 checkConflictsAndDeps();
235 // complain about mods declared to be loaded, but not found
236 for (const ModSpec &addon_mod : addon_mods)
237 load_mod_names.erase(addon_mod.name);
239 std::vector<ModSpec> unsatisfiedMods = getUnsatisfiedMods();
241 for (const ModSpec &unsatisfiedMod : unsatisfiedMods)
242 load_mod_names.erase(unsatisfiedMod.name);
244 if (!load_mod_names.empty()) {
245 errorstream << "The following mods could not be found:";
246 for (const std::string &mod : load_mod_names)
247 errorstream << " \"" << mod << "\"";
248 errorstream << std::endl;
252 void ModConfiguration::checkConflictsAndDeps()
254 // report on name conflicts
255 if (!m_name_conflicts.empty()) {
256 std::string s = "Unresolved name conflicts for mods ";
257 for (std::unordered_set<std::string>::const_iterator it =
258 m_name_conflicts.begin(); it != m_name_conflicts.end(); ++it) {
259 if (it != m_name_conflicts.begin()) s += ", ";
260 s += std::string("\"") + (*it) + "\"";
266 // get the mods in order
267 resolveDependencies();
270 void ModConfiguration::resolveDependencies()
272 // Step 1: Compile a list of the mod names we're working with
273 std::set<std::string> modnames;
274 for (const ModSpec &mod : m_unsatisfied_mods) {
275 modnames.insert(mod.name);
278 // Step 2: get dependencies (including optional dependencies)
279 // of each mod, split mods into satisfied and unsatisfied
280 std::list<ModSpec> satisfied;
281 std::list<ModSpec> unsatisfied;
282 for (ModSpec mod : m_unsatisfied_mods) {
283 mod.unsatisfied_depends = mod.depends;
284 // check which optional dependencies actually exist
285 for (const std::string &optdep : mod.optdepends) {
286 if (modnames.count(optdep) != 0)
287 mod.unsatisfied_depends.insert(optdep);
289 // if a mod has no depends it is initially satisfied
290 if (mod.unsatisfied_depends.empty())
291 satisfied.push_back(mod);
293 unsatisfied.push_back(mod);
296 // Step 3: mods without unmet dependencies can be appended to
298 while(!satisfied.empty()){
299 ModSpec mod = satisfied.back();
300 m_sorted_mods.push_back(mod);
301 satisfied.pop_back();
302 for (auto it = unsatisfied.begin(); it != unsatisfied.end(); ) {
304 mod2.unsatisfied_depends.erase(mod.name);
305 if (mod2.unsatisfied_depends.empty()) {
306 satisfied.push_back(mod2);
307 it = unsatisfied.erase(it);
314 // Step 4: write back list of unsatisfied mods
315 m_unsatisfied_mods.assign(unsatisfied.begin(), unsatisfied.end());
318 ServerModConfiguration::ServerModConfiguration(const std::string &worldpath):
319 ModConfiguration(worldpath)
321 SubgameSpec gamespec = findWorldSubgame(worldpath);
323 // Add all game mods and all world mods
324 addModsInPath(gamespec.gamemods_path);
325 addModsInPath(worldpath + DIR_DELIM + "worldmods");
328 std::string worldmt = worldpath + DIR_DELIM + "world.mt";
329 addModsFromConfig(worldmt, gamespec.addon_mods_paths);
333 ClientModConfiguration::ClientModConfiguration(const std::string &path):
334 ModConfiguration(path)
336 std::set<std::string> paths;
337 std::string path_user = porting::path_user + DIR_DELIM + "clientmods";
339 paths.insert(path_user);
341 std::string settings_path = path_user + DIR_DELIM + "mods.conf";
342 addModsFromConfig(settings_path, paths);
346 ModMetadata::ModMetadata(const std::string &mod_name):
351 void ModMetadata::clear()
357 bool ModMetadata::save(const std::string &root_path)
360 for (StringMap::const_iterator it = m_stringvars.begin();
361 it != m_stringvars.end(); ++it) {
362 json[it->first] = it->second;
365 if (!fs::PathExists(root_path)) {
366 if (!fs::CreateAllDirs(root_path)) {
367 errorstream << "ModMetadata[" << m_mod_name << "]: Unable to save. '"
368 << root_path << "' tree cannot be created." << std::endl;
371 } else if (!fs::IsDir(root_path)) {
372 errorstream << "ModMetadata[" << m_mod_name << "]: Unable to save. '"
373 << root_path << "' is not a directory." << std::endl;
377 bool w_ok = fs::safeWriteToFile(root_path + DIR_DELIM + m_mod_name,
378 fastWriteJson(json));
383 errorstream << "ModMetadata[" << m_mod_name << "]: failed write file." << std::endl;
388 bool ModMetadata::load(const std::string &root_path)
390 m_stringvars.clear();
392 std::ifstream is((root_path + DIR_DELIM + m_mod_name).c_str(), std::ios_base::binary);
398 Json::CharReaderBuilder builder;
399 builder.settings_["collectComments"] = false;
402 if (!Json::parseFromStream(builder, is, &root, &errs)) {
403 errorstream << "ModMetadata[" << m_mod_name << "]: failed read data "
404 "(Json decoding failure). Message: " << errs << std::endl;
408 const Json::Value::Members attr_list = root.getMemberNames();
409 for (const auto &it : attr_list) {
410 Json::Value attr_value = root[it];
411 m_stringvars[it] = attr_value.asString();
417 bool ModMetadata::setString(const std::string &name, const std::string &var)
419 m_modified = Metadata::setString(name, var);