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"
29 * All new calls to this class must be tested in test_servermodmanager.cpp
33 * Creates a ServerModManager which targets worldpath
36 ServerModManager::ServerModManager(const std::string &worldpath) :
37 ModConfiguration(worldpath)
39 SubgameSpec gamespec = findWorldSubgame(worldpath);
41 // Add all game mods and all world mods
42 addModsInPath(gamespec.gamemods_path);
43 addModsInPath(worldpath + DIR_DELIM + "worldmods");
46 std::string worldmt = worldpath + DIR_DELIM + "world.mt";
47 addModsFromConfig(worldmt, gamespec.addon_mods_paths);
51 // This function cannot be currenctly easily tested but it should be ASAP
52 void ServerModManager::loadMods(ServerScripting *script)
55 infostream << "Server: Loading mods: ";
56 for (const ModSpec &mod : m_sorted_mods) {
57 infostream << mod.name << " ";
59 infostream << std::endl;
60 // Load and run "mod" scripts
61 for (const ModSpec &mod : m_sorted_mods) {
62 if (!string_allowed(mod.name, MODNAME_ALLOWED_CHARS)) {
63 throw ModError("Error loading mod \"" + mod.name +
64 "\": Mod name does not follow naming "
66 "Only characters [a-z0-9_] are allowed.");
68 std::string script_path = mod.path + DIR_DELIM + "init.lua";
69 infostream << " [" << padStringRight(mod.name, 12) << "] [\""
70 << script_path << "\"]" << std::endl;
71 auto t = std::chrono::steady_clock::now();
72 script->loadMod(script_path, mod.name);
73 infostream << "Mod \"" << mod.name << "\" loaded after "
74 << std::chrono::duration_cast<std::chrono::milliseconds>(
75 std::chrono::steady_clock::now() - t).count() * 0.001f
76 << " seconds" << std::endl;
79 // Run a callback when mods are loaded
80 script->on_mods_loaded();
84 const ModSpec *ServerModManager::getModSpec(const std::string &modname) const
86 std::vector<ModSpec>::const_iterator it;
87 for (it = m_sorted_mods.begin(); it != m_sorted_mods.end(); ++it) {
88 const ModSpec &mod = *it;
89 if (mod.name == modname)
95 void ServerModManager::getModNames(std::vector<std::string> &modlist) const
97 for (const ModSpec &spec : m_sorted_mods)
98 modlist.push_back(spec.name);
101 void ServerModManager::getModsMediaPaths(std::vector<std::string> &paths) const
103 for (const ModSpec &spec : m_sorted_mods) {
104 paths.push_back(spec.path + DIR_DELIM + "textures");
105 paths.push_back(spec.path + DIR_DELIM + "sounds");
106 paths.push_back(spec.path + DIR_DELIM + "media");
107 paths.push_back(spec.path + DIR_DELIM + "models");
108 paths.push_back(spec.path + DIR_DELIM + "locale");