Skip to content

Commit 32cb9d0

Browse files
authoredJul 31, 2021
Mods: Combine mod loading checks and deprection logging (#11503)
This limits the logged deprecation messages to the mods that are loaded Unifies the mod naming convention check for CSM & SSM
1 parent e7cd4cf commit 32cb9d0

File tree

4 files changed

+34
-29
lines changed

4 files changed

+34
-29
lines changed
 

‎src/client/client.cpp

+1-5
Original file line numberDiff line numberDiff line change
@@ -177,11 +177,7 @@ void Client::loadMods()
177177

178178
// Load "mod" scripts
179179
for (const ModSpec &mod : m_mods) {
180-
if (!string_allowed(mod.name, MODNAME_ALLOWED_CHARS)) {
181-
throw ModError("Error loading mod \"" + mod.name +
182-
"\": Mod name does not follow naming conventions: "
183-
"Only characters [a-z0-9_] are allowed.");
184-
}
180+
mod.checkAndLog();
185181
scanModIntoMemory(mod.name, mod.path);
186182
}
187183

‎src/content/mods.cpp

+26-18
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,29 @@ with this program; if not, write to the Free Software Foundation, Inc.,
3030
#include "convert_json.h"
3131
#include "script/common/c_internal.h"
3232

33+
void ModSpec::checkAndLog() const
34+
{
35+
if (!string_allowed(name, MODNAME_ALLOWED_CHARS)) {
36+
throw ModError("Error loading mod \"" + name +
37+
"\": Mod name does not follow naming conventions: "
38+
"Only characters [a-z0-9_] are allowed.");
39+
}
40+
41+
// Log deprecation messages
42+
auto handling_mode = get_deprecated_handling_mode();
43+
if (!deprecation_msgs.empty() && handling_mode != DeprecatedHandlingMode::Ignore) {
44+
std::ostringstream os;
45+
os << "Mod " << name << " at " << path << ":" << std::endl;
46+
for (auto msg : deprecation_msgs)
47+
os << "\t" << msg << std::endl;
48+
49+
if (handling_mode == DeprecatedHandlingMode::Error)
50+
throw ModError(os.str());
51+
else
52+
warningstream << os.str();
53+
}
54+
}
55+
3356
bool parseDependsString(std::string &dep, std::unordered_set<char> &symbols)
3457
{
3558
dep = trim(dep);
@@ -45,21 +68,6 @@ bool parseDependsString(std::string &dep, std::unordered_set<char> &symbols)
4568
return !dep.empty();
4669
}
4770

48-
static void log_mod_deprecation(const ModSpec &spec, const std::string &warning)
49-
{
50-
auto handling_mode = get_deprecated_handling_mode();
51-
if (handling_mode != DeprecatedHandlingMode::Ignore) {
52-
std::ostringstream os;
53-
os << warning << " (" << spec.name << " at " << spec.path << ")" << std::endl;
54-
55-
if (handling_mode == DeprecatedHandlingMode::Error) {
56-
throw ModError(os.str());
57-
} else {
58-
warningstream << os.str();
59-
}
60-
}
61-
}
62-
6371
void parseModContents(ModSpec &spec)
6472
{
6573
// NOTE: this function works in mutual recursion with getModsInPath
@@ -89,7 +97,7 @@ void parseModContents(ModSpec &spec)
8997
if (info.exists("name"))
9098
spec.name = info.get("name");
9199
else
92-
log_mod_deprecation(spec, "Mods not having a mod.conf file with the name is deprecated.");
100+
spec.deprecation_msgs.push_back("Mods not having a mod.conf file with the name is deprecated.");
93101

94102
if (info.exists("author"))
95103
spec.author = info.get("author");
@@ -130,7 +138,7 @@ void parseModContents(ModSpec &spec)
130138
std::ifstream is((spec.path + DIR_DELIM + "depends.txt").c_str());
131139

132140
if (is.good())
133-
log_mod_deprecation(spec, "depends.txt is deprecated, please use mod.conf instead.");
141+
spec.deprecation_msgs.push_back("depends.txt is deprecated, please use mod.conf instead.");
134142

135143
while (is.good()) {
136144
std::string dep;
@@ -153,7 +161,7 @@ void parseModContents(ModSpec &spec)
153161
if (info.exists("description"))
154162
spec.desc = info.get("description");
155163
else if (fs::ReadFile(spec.path + DIR_DELIM + "description.txt", spec.desc))
156-
log_mod_deprecation(spec, "description.txt is deprecated, please use mod.conf instead.");
164+
spec.deprecation_msgs.push_back("description.txt is deprecated, please use mod.conf instead.");
157165
}
158166
}
159167

‎src/content/mods.h

+5
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,9 @@ struct ModSpec
4949
bool part_of_modpack = false;
5050
bool is_modpack = false;
5151

52+
// For logging purposes
53+
std::vector<const char *> deprecation_msgs;
54+
5255
// if modpack:
5356
std::map<std::string, ModSpec> modpack_content;
5457
ModSpec(const std::string &name = "", const std::string &path = "") :
@@ -59,6 +62,8 @@ struct ModSpec
5962
name(name), path(path), part_of_modpack(part_of_modpack)
6063
{
6164
}
65+
66+
void checkAndLog() const;
6267
};
6368

6469
// Retrieves depends, optdepends, is_modpack and modpack_content

‎src/server/mods.cpp

+2-6
Original file line numberDiff line numberDiff line change
@@ -61,12 +61,8 @@ void ServerModManager::loadMods(ServerScripting *script)
6161
infostream << std::endl;
6262
// Load and run "mod" scripts
6363
for (const ModSpec &mod : m_sorted_mods) {
64-
if (!string_allowed(mod.name, MODNAME_ALLOWED_CHARS)) {
65-
throw ModError("Error loading mod \"" + mod.name +
66-
"\": Mod name does not follow naming "
67-
"conventions: "
68-
"Only characters [a-z0-9_] are allowed.");
69-
}
64+
mod.checkAndLog();
65+
7066
std::string script_path = mod.path + DIR_DELIM + "init.lua";
7167
auto t = porting::getTimeMs();
7268
script->loadMod(script_path, mod.name);

0 commit comments

Comments
 (0)
Please sign in to comment.