Noise: Prevent unittest crash caused by division by zero
[oweals/minetest.git] / src / mods.cpp
index 6fce8e93d1d45140a0cad4379ff9089be74d814c..7e2d83944407c78a8cef5781a1cb60f5fb3f9754 100644 (file)
@@ -144,7 +144,8 @@ void ModConfiguration::printUnsatisfiedModsError() const
                it != m_unsatisfied_mods.end(); ++it) {
                ModSpec mod = *it;
                errorstream << "mod \"" << mod.name << "\" has unsatisfied dependencies: ";
-               for (UNORDERED_SET<std::string>::iterator dep_it = mod.unsatisfied_depends.begin();
+               for (std::unordered_set<std::string>::iterator dep_it =
+                       mod.unsatisfied_depends.begin();
                        dep_it != mod.unsatisfied_depends.end(); ++dep_it)
                        errorstream << " \"" << *dep_it << "\"";
                errorstream << std::endl;
@@ -214,13 +215,62 @@ void ModConfiguration::addMods(const std::vector<ModSpec> &new_mods)
        }
 }
 
+void ModConfiguration::addModsFormConfig(const std::string &settings_path, const std::set<std::string> &mods)
+{
+       Settings conf;
+       std::set<std::string> load_mod_names;
+
+       conf.readConfigFile(settings_path.c_str());
+       std::vector<std::string> names = conf.getNames();
+       for (std::vector<std::string>::iterator it = names.begin();
+               it != names.end(); ++it) {
+               std::string name = *it;
+               if (name.compare(0,9,"load_mod_")==0 && conf.getBool(name))
+                       load_mod_names.insert(name.substr(9));
+       }
+
+       std::vector<ModSpec> addon_mods;
+       for (std::set<std::string>::const_iterator i = mods.begin();
+                       i != mods.end(); ++i) {
+               std::vector<ModSpec> addon_mods_in_path = flattenMods(getModsInPath(*i));
+               for (std::vector<ModSpec>::const_iterator it = addon_mods_in_path.begin();
+                               it != addon_mods_in_path.end(); ++it) {
+                       const ModSpec& mod = *it;
+                       if (load_mod_names.count(mod.name) != 0)
+                               addon_mods.push_back(mod);
+                       else
+                               conf.setBool("load_mod_" + mod.name, false);
+               }
+       }
+       conf.updateConfigFile(settings_path.c_str());
+
+       addMods(addon_mods);
+       checkConflictsAndDeps();
+
+       // complain about mods declared to be loaded, but not found
+       for (std::vector<ModSpec>::iterator it = addon_mods.begin();
+                       it != addon_mods.end(); ++it)
+               load_mod_names.erase((*it).name);
+       std::vector<ModSpec> UnsatisfiedMods = getUnsatisfiedMods();
+       for (std::vector<ModSpec>::iterator it = UnsatisfiedMods.begin();
+                       it != UnsatisfiedMods.end(); ++it)
+               load_mod_names.erase((*it).name);
+       if (!load_mod_names.empty()) {
+               errorstream << "The following mods could not be found:";
+               for (std::set<std::string>::iterator it = load_mod_names.begin();
+                               it != load_mod_names.end(); ++it)
+                       errorstream << " \"" << (*it) << "\"";
+               errorstream << std::endl;
+       }
+}
+
 void ModConfiguration::checkConflictsAndDeps()
 {
        // report on name conflicts
        if (!m_name_conflicts.empty()) {
                std::string s = "Unresolved name conflicts for mods ";
-               for (UNORDERED_SET<std::string>::const_iterator it = m_name_conflicts.begin();
-                       it != m_name_conflicts.end(); ++it) {
+               for (std::unordered_set<std::string>::const_iterator it =
+                       m_name_conflicts.begin(); it != m_name_conflicts.end(); ++it) {
                        if (it != m_name_conflicts.begin()) s += ", ";
                        s += std::string("\"") + (*it) + "\"";
                }
@@ -250,7 +300,7 @@ void ModConfiguration::resolveDependencies()
                ModSpec mod = *it;
                mod.unsatisfied_depends = mod.depends;
                // check which optional dependencies actually exist
-               for (UNORDERED_SET<std::string>::iterator it_optdep = mod.optdepends.begin();
+               for (std::unordered_set<std::string>::iterator it_optdep = mod.optdepends.begin();
                                it_optdep != mod.optdepends.end(); ++it_optdep) {
                        std::string optdep = *it_optdep;
                        if (modnames.count(optdep) != 0)
@@ -296,80 +346,28 @@ ServerModConfiguration::ServerModConfiguration(const std::string &worldpath):
        addModsInPath(gamespec.gamemods_path);
        addModsInPath(worldpath + DIR_DELIM + "worldmods");
 
-       // check world.mt file for mods explicitely declared to be
-       // loaded or not by a load_mod_<modname> = ... line.
-       std::string worldmt = worldpath+DIR_DELIM+"world.mt";
-       Settings worldmt_settings;
-       worldmt_settings.readConfigFile(worldmt.c_str());
-       std::vector<std::string> names = worldmt_settings.getNames();
-       std::set<std::string> include_mod_names;
-       for (std::vector<std::string>::const_iterator it = names.begin();
-               it != names.end(); ++it) {
-               std::string name = *it;
-               // for backwards compatibility: exclude only mods which are
-               // explicitely excluded. if mod is not mentioned at all, it is
-               // enabled. So by default, all installed mods are enabled.
-               if (name.compare(0,9,"load_mod_") == 0 &&
-                       worldmt_settings.getBool(name)) {
-                       include_mod_names.insert(name.substr(9));
-               }
-       }
-
-       // Collect all mods that are also in include_mod_names
-       std::vector<ModSpec> addon_mods;
-       for (std::set<std::string>::const_iterator it_path = gamespec.addon_mods_paths.begin();
-               it_path != gamespec.addon_mods_paths.end(); ++it_path) {
-               std::vector<ModSpec> addon_mods_in_path = flattenMods(getModsInPath(*it_path));
-               for (std::vector<ModSpec>::const_iterator it = addon_mods_in_path.begin();
-                       it != addon_mods_in_path.end(); ++it) {
-                       const ModSpec& mod = *it;
-                       if (include_mod_names.count(mod.name) != 0)
-                               addon_mods.push_back(mod);
-                       else
-                               worldmt_settings.setBool("load_mod_" + mod.name, false);
-               }
-       }
-       worldmt_settings.updateConfigFile(worldmt.c_str());
-
-       addMods(addon_mods);
-
-       checkConflictsAndDeps();
+       // Load normal mods
+       std::string worldmt = worldpath + DIR_DELIM + "world.mt";
+       addModsFormConfig(worldmt, gamespec.addon_mods_paths);
 }
 
 #ifndef SERVER
 ClientModConfiguration::ClientModConfiguration(const std::string &path):
        ModConfiguration(path)
 {
-       addModsInPath(path);
-       addModsInPath(porting::path_user + DIR_DELIM + "clientmods");
-       checkConflictsAndDeps();
-}
-#endif
-
-#if USE_CURL
-Json::Value getModstoreUrl(const std::string &url)
-{
-       std::vector<std::string> extra_headers;
+       std::set<std::string> paths;
+       std::string path_user = porting::path_user + DIR_DELIM + "clientmods";
+       paths.insert(path);
+       paths.insert(path_user);
 
-       bool special_http_header = true;
-
-       try {
-               special_http_header = g_settings->getBool("modstore_disable_special_http_header");
-       } catch (SettingNotFoundException) {}
-
-       if (special_http_header) {
-               extra_headers.push_back("Accept: application/vnd.minetest.mmdb-v1+json");
-       }
-       return fetchJsonValue(url, special_http_header ? &extra_headers : NULL);
+       std::string settings_path = path_user + DIR_DELIM + "mods.conf";
+       addModsFormConfig(settings_path, paths);
 }
-
 #endif
 
 ModMetadata::ModMetadata(const std::string &mod_name):
-       m_mod_name(mod_name),
-       m_modified(false)
+       m_mod_name(mod_name)
 {
-       m_stringvars.clear();
 }
 
 void ModMetadata::clear()