3 Copyright (C) 2018 nerzhul, Loic Blot <loic.blot@unix-experience.fr>
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.
23 #include "scripting_server.h"
24 #include "content/subgames.h"
26 #include "util/metricsbackend.h"
31 * All new calls to this class must be tested in test_servermodmanager.cpp
35 * Creates a ServerModManager which targets worldpath
38 ServerModManager::ServerModManager(const std::string &worldpath) :
39 ModConfiguration(worldpath)
41 SubgameSpec gamespec = findWorldSubgame(worldpath);
43 // Add all game mods and all world mods
44 addModsInPath(gamespec.gamemods_path);
45 addModsInPath(worldpath + DIR_DELIM + "worldmods");
48 std::string worldmt = worldpath + DIR_DELIM + "world.mt";
49 addModsFromConfig(worldmt, gamespec.addon_mods_paths);
53 // This function cannot be currenctly easily tested but it should be ASAP
54 void ServerModManager::loadMods(ServerScripting *script)
57 infostream << "Server: Loading mods: ";
58 for (const ModSpec &mod : m_sorted_mods) {
59 infostream << mod.name << " ";
61 infostream << std::endl;
62 // Load and run "mod" scripts
63 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 "
68 "Only characters [a-z0-9_] are allowed.");
70 std::string script_path = mod.path + DIR_DELIM + "init.lua";
71 auto t = porting::getTimeMs();
72 script->loadMod(script_path, mod.name);
73 infostream << "Mod \"" << mod.name << "\" loaded after "
74 << (porting::getTimeMs() - t) << " ms" << std::endl;
77 // Run a callback when mods are loaded
78 script->on_mods_loaded();
82 const ModSpec *ServerModManager::getModSpec(const std::string &modname) const
84 std::vector<ModSpec>::const_iterator it;
85 for (it = m_sorted_mods.begin(); it != m_sorted_mods.end(); ++it) {
86 const ModSpec &mod = *it;
87 if (mod.name == modname)
93 void ServerModManager::getModNames(std::vector<std::string> &modlist) const
95 for (const ModSpec &spec : m_sorted_mods)
96 modlist.push_back(spec.name);
99 void ServerModManager::getModsMediaPaths(std::vector<std::string> &paths) const
101 for (const ModSpec &spec : m_sorted_mods) {
102 fs::GetRecursiveDirs(paths, spec.path + DIR_DELIM + "textures");
103 fs::GetRecursiveDirs(paths, spec.path + DIR_DELIM + "sounds");
104 fs::GetRecursiveDirs(paths, spec.path + DIR_DELIM + "media");
105 fs::GetRecursiveDirs(paths, spec.path + DIR_DELIM + "models");
106 fs::GetRecursiveDirs(paths, spec.path + DIR_DELIM + "locale");