From 6c184947c3886ce80aa9eb9807a700025a344442 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20Blot?= Date: Fri, 16 Mar 2018 08:41:33 +0100 Subject: Server: delegate mod management & config to ServerModConfiguration (#7131) * Server: delegate mod management & config to ServerModConfiguration (rename it to ServerModManager) * Use c++11 range based loops * Add unittests + experimental/default mod as a test case to permit testing mod loading in future tests --- src/server/CMakeLists.txt | 3 ++ src/server/mods.cpp | 100 ++++++++++++++++++++++++++++++++++++++++++++++ src/server/mods.h | 43 ++++++++++++++++++++ 3 files changed, 146 insertions(+) create mode 100644 src/server/CMakeLists.txt create mode 100644 src/server/mods.cpp create mode 100644 src/server/mods.h (limited to 'src/server') diff --git a/src/server/CMakeLists.txt b/src/server/CMakeLists.txt new file mode 100644 index 000000000..b892e83b3 --- /dev/null +++ b/src/server/CMakeLists.txt @@ -0,0 +1,3 @@ +set(server_SRCS + ${CMAKE_CURRENT_SOURCE_DIR}/mods.cpp + PARENT_SCOPE) diff --git a/src/server/mods.cpp b/src/server/mods.cpp new file mode 100644 index 000000000..778241b95 --- /dev/null +++ b/src/server/mods.cpp @@ -0,0 +1,100 @@ +/* +Minetest +Copyright (C) 2018 nerzhul, Loic Blot + +This program is free software; you can redistribute it and/or modify +it under the terms of the GNU Lesser General Public License as published by +the Free Software Foundation; either version 2.1 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU Lesser General Public License for more details. + +You should have received a copy of the GNU Lesser General Public License along +with this program; if not, write to the Free Software Foundation, Inc., +51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. +*/ + +#include "mods.h" +#include "filesys.h" +#include "log.h" +#include "scripting_server.h" +#include "subgame.h" + +/** + * Manage server mods + * + * All new calls to this class must be tested in test_servermodmanager.cpp + */ + +/** + * Creates a ServerModManager which targets worldpath + * @param worldpath + */ +ServerModManager::ServerModManager(const std::string &worldpath) : + ModConfiguration(worldpath) +{ + SubgameSpec gamespec = findWorldSubgame(worldpath); + + // Add all game mods and all world mods + addModsInPath(gamespec.gamemods_path); + addModsInPath(worldpath + DIR_DELIM + "worldmods"); + + // Load normal mods + std::string worldmt = worldpath + DIR_DELIM + "world.mt"; + addModsFromConfig(worldmt, gamespec.addon_mods_paths); +} + +// This function cannot be currenctly easily tested but it should be ASAP +void ServerModManager::loadMods(ServerScripting *script) +{ + // Print mods + infostream << "Server: Loading mods: "; + for (const ModSpec &mod : m_sorted_mods) { + infostream << mod.name << " "; + } + 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."); + } + std::string script_path = mod.path + DIR_DELIM + "init.lua"; + infostream << " [" << padStringRight(mod.name, 12) << "] [\"" + << script_path << "\"]" << std::endl; + script->loadMod(script_path, mod.name); + } +} + +const ModSpec *ServerModManager::getModSpec(const std::string &modname) const +{ + std::vector::const_iterator it; + for (it = m_sorted_mods.begin(); it != m_sorted_mods.end(); ++it) { + const ModSpec &mod = *it; + if (mod.name == modname) + return &mod; + } + return NULL; +} + +void ServerModManager::getModNames(std::vector &modlist) const +{ + for (const ModSpec &spec : m_sorted_mods) + modlist.push_back(spec.name); +} + +void ServerModManager::getModsMediaPaths(std::vector &paths) const +{ + for (const ModSpec &spec : m_sorted_mods) { + paths.push_back(spec.path + DIR_DELIM + "textures"); + paths.push_back(spec.path + DIR_DELIM + "sounds"); + paths.push_back(spec.path + DIR_DELIM + "media"); + paths.push_back(spec.path + DIR_DELIM + "models"); + paths.push_back(spec.path + DIR_DELIM + "locale"); + } +} diff --git a/src/server/mods.h b/src/server/mods.h new file mode 100644 index 000000000..9e4b23f30 --- /dev/null +++ b/src/server/mods.h @@ -0,0 +1,43 @@ +/* +Minetest +Copyright (C) 2018 nerzhul, Loic Blot + +This program is free software; you can redistribute it and/or modify +it under the terms of the GNU Lesser General Public License as published by +the Free Software Foundation; either version 2.1 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU Lesser General Public License for more details. + +You should have received a copy of the GNU Lesser General Public License along +with this program; if not, write to the Free Software Foundation, Inc., +51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. +*/ + +#pragma once + +#include "../mods.h" + +class ServerScripting; + +/** + * Manage server mods + * + * All new calls to this class must be tested in test_servermodmanager.cpp + */ +class ServerModManager : public ModConfiguration +{ +public: + /** + * Creates a ServerModManager which targets worldpath + * @param worldpath + */ + ServerModManager(const std::string &worldpath); + void loadMods(ServerScripting *script); + const ModSpec *getModSpec(const std::string &modname) const; + void getModNames(std::vector &modlist) const; + void getModsMediaPaths(std::vector &paths) const; +}; -- cgit v1.2.3