]> git.lizzy.rs Git - minetest.git/blob - src/server/mods.cpp
Add keybind to swap items between hands
[minetest.git] / src / server / mods.cpp
1 /*
2 Minetest
3 Copyright (C) 2018 nerzhul, Loic Blot <loic.blot@unix-experience.fr>
4
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.
9
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.
14
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.
18 */
19
20 #include "mods.h"
21 #include "filesys.h"
22 #include "log.h"
23 #include "scripting_server.h"
24 #include "content/subgames.h"
25 #include "porting.h"
26
27 /**
28  * Manage server mods
29  *
30  * All new calls to this class must be tested in test_servermodmanager.cpp
31  */
32
33 /**
34  * Creates a ServerModManager which targets worldpath
35  * @param worldpath
36  */
37 ServerModManager::ServerModManager(const std::string &worldpath):
38         configuration()
39 {
40         SubgameSpec gamespec = findWorldSubgame(worldpath);
41
42         // Add all game mods and all world mods
43         configuration.addGameMods(gamespec);
44         configuration.addModsInPath(worldpath + DIR_DELIM + "worldmods", "worldmods");
45
46         // Load normal mods
47         std::string worldmt = worldpath + DIR_DELIM + "world.mt";
48         configuration.addModsFromConfig(worldmt, gamespec.addon_mods_paths);
49         configuration.checkConflictsAndDeps();
50 }
51
52 // clang-format off
53 // This function cannot be currenctly easily tested but it should be ASAP
54 void ServerModManager::loadMods(ServerScripting *script)
55 {
56         // Print mods
57         infostream << "Server: Loading mods: ";
58         for (const ModSpec &mod : configuration.getMods()) {
59                 infostream << mod.name << " ";
60         }
61
62         infostream << std::endl;
63         // Load and run "mod" scripts
64         for (const ModSpec &mod : configuration.getMods()) {
65                 mod.checkAndLog();
66
67                 std::string script_path = mod.path + DIR_DELIM + "init.lua";
68                 auto t = porting::getTimeMs();
69                 script->loadMod(script_path, mod.name);
70                 infostream << "Mod \"" << mod.name << "\" loaded after "
71                         << (porting::getTimeMs() - t) << " ms" << std::endl;
72         }
73
74         // Run a callback when mods are loaded
75         script->on_mods_loaded();
76 }
77
78 // clang-format on
79 const ModSpec *ServerModManager::getModSpec(const std::string &modname) const
80 {
81         for (const auto &mod : configuration.getMods()) {
82                 if (mod.name == modname)
83                         return &mod;
84         }
85
86         return nullptr;
87 }
88
89 void ServerModManager::getModNames(std::vector<std::string> &modlist) const
90 {
91         for (const ModSpec &spec : configuration.getMods())
92                 modlist.push_back(spec.name);
93 }
94
95 void ServerModManager::getModsMediaPaths(std::vector<std::string> &paths) const
96 {
97         // Iterate mods in reverse load order: Media loading expects higher priority media files first
98         // and mods loading later should be able to override media of already loaded mods
99         const auto &mods = configuration.getMods();
100         for (auto it = mods.crbegin(); it != mods.crend(); it++) {
101                 const ModSpec &spec = *it;
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");
107         }
108 }