]> git.lizzy.rs Git - minetest.git/commitdiff
Mods: Combine mod loading checks and deprection logging (#11503)
authorSmallJoker <SmallJoker@users.noreply.github.com>
Sat, 31 Jul 2021 17:54:52 +0000 (19:54 +0200)
committerGitHub <noreply@github.com>
Sat, 31 Jul 2021 17:54:52 +0000 (19:54 +0200)
This limits the logged deprecation messages to the mods that are loaded
Unifies the mod naming convention check for CSM & SSM

src/client/client.cpp
src/content/mods.cpp
src/content/mods.h
src/server/mods.cpp

index 17661c2421daea99821362e2904626c805281507..923369afeee348f21b2d83b92de1140eb8e06369 100644 (file)
@@ -177,11 +177,7 @@ void Client::loadMods()
 
        // Load "mod" scripts
        for (const ModSpec &mod : m_mods) {
-               if (!string_allowed(mod.name, MODNAME_ALLOWED_CHARS)) {
-                       throw ModError("Error loading mod \"" + mod.name +
-                               "\": Mod name does not follow naming conventions: "
-                                       "Only characters [a-z0-9_] are allowed.");
-               }
+               mod.checkAndLog();
                scanModIntoMemory(mod.name, mod.path);
        }
 
index 434004b297f7461564e60553423637559f4d508b..6f088a5b35cf011740c9946fd3bdca3ab70e4506 100644 (file)
@@ -30,6 +30,29 @@ with this program; if not, write to the Free Software Foundation, Inc.,
 #include "convert_json.h"
 #include "script/common/c_internal.h"
 
+void ModSpec::checkAndLog() const
+{
+       if (!string_allowed(name, MODNAME_ALLOWED_CHARS)) {
+               throw ModError("Error loading mod \"" + name +
+                       "\": Mod name does not follow naming conventions: "
+                               "Only characters [a-z0-9_] are allowed.");
+       }
+
+       // Log deprecation messages
+       auto handling_mode = get_deprecated_handling_mode();
+       if (!deprecation_msgs.empty() && handling_mode != DeprecatedHandlingMode::Ignore) {
+               std::ostringstream os;
+               os << "Mod " << name << " at " << path << ":" << std::endl;
+               for (auto msg : deprecation_msgs)
+                       os << "\t" << msg << std::endl;
+
+               if (handling_mode == DeprecatedHandlingMode::Error)
+                       throw ModError(os.str());
+               else
+                       warningstream << os.str();
+       }
+}
+
 bool parseDependsString(std::string &dep, std::unordered_set<char> &symbols)
 {
        dep = trim(dep);
@@ -45,21 +68,6 @@ bool parseDependsString(std::string &dep, std::unordered_set<char> &symbols)
        return !dep.empty();
 }
 
-static void log_mod_deprecation(const ModSpec &spec, const std::string &warning)
-{
-       auto handling_mode = get_deprecated_handling_mode();
-       if (handling_mode != DeprecatedHandlingMode::Ignore) {
-               std::ostringstream os;
-               os << warning << " (" << spec.name << " at " << spec.path << ")" << std::endl;
-
-               if (handling_mode == DeprecatedHandlingMode::Error) {
-                       throw ModError(os.str());
-               } else {
-                       warningstream << os.str();
-               }
-       }
-}
-
 void parseModContents(ModSpec &spec)
 {
        // NOTE: this function works in mutual recursion with getModsInPath
@@ -89,7 +97,7 @@ void parseModContents(ModSpec &spec)
                if (info.exists("name"))
                        spec.name = info.get("name");
                else
-                       log_mod_deprecation(spec, "Mods not having a mod.conf file with the name is deprecated.");
+                       spec.deprecation_msgs.push_back("Mods not having a mod.conf file with the name is deprecated.");
 
                if (info.exists("author"))
                        spec.author = info.get("author");
@@ -130,7 +138,7 @@ void parseModContents(ModSpec &spec)
                        std::ifstream is((spec.path + DIR_DELIM + "depends.txt").c_str());
 
                        if (is.good())
-                               log_mod_deprecation(spec, "depends.txt is deprecated, please use mod.conf instead.");
+                               spec.deprecation_msgs.push_back("depends.txt is deprecated, please use mod.conf instead.");
 
                        while (is.good()) {
                                std::string dep;
@@ -153,7 +161,7 @@ void parseModContents(ModSpec &spec)
                if (info.exists("description"))
                        spec.desc = info.get("description");
                else if (fs::ReadFile(spec.path + DIR_DELIM + "description.txt", spec.desc))
-                       log_mod_deprecation(spec, "description.txt is deprecated, please use mod.conf instead.");
+                       spec.deprecation_msgs.push_back("description.txt is deprecated, please use mod.conf instead.");
        }
 }
 
index b3500fbc8927b29ddbbd3353e662e86182d51787..b56a97edbb910fc1122bd0ccbc4d5ac1b620ebd0 100644 (file)
@@ -49,6 +49,9 @@ struct ModSpec
        bool part_of_modpack = false;
        bool is_modpack = false;
 
+       // For logging purposes
+       std::vector<const char *> deprecation_msgs;
+
        // if modpack:
        std::map<std::string, ModSpec> modpack_content;
        ModSpec(const std::string &name = "", const std::string &path = "") :
@@ -59,6 +62,8 @@ struct ModSpec
                        name(name), path(path), part_of_modpack(part_of_modpack)
        {
        }
+
+       void checkAndLog() const;
 };
 
 // Retrieves depends, optdepends, is_modpack and modpack_content
index 83fa12da9f5ba12b36412c0931d4b0513c48c6c6..609d8c3462dcf028592a83a6035f794787f36ec1 100644 (file)
@@ -61,12 +61,8 @@ void ServerModManager::loadMods(ServerScripting *script)
        infostream << std::endl;
        // Load and run "mod" scripts
        for (const ModSpec &mod : m_sorted_mods) {
-               if (!string_allowed(mod.name, MODNAME_ALLOWED_CHARS)) {
-                       throw ModError("Error loading mod \"" + mod.name +
-                                       "\": Mod name does not follow naming "
-                                       "conventions: "
-                                       "Only characters [a-z0-9_] are allowed.");
-               }
+               mod.checkAndLog();
+
                std::string script_path = mod.path + DIR_DELIM + "init.lua";
                auto t = porting::getTimeMs();
                script->loadMod(script_path, mod.name);