]> git.lizzy.rs Git - dragonfireclient.git/blob - src/server/mods.cpp
Merge branch 'master' of https://github.com/minetest/minetest
[dragonfireclient.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 #include "util/metricsbackend.h"
27
28 /**
29  * Manage server mods
30  *
31  * All new calls to this class must be tested in test_servermodmanager.cpp
32  */
33
34 /**
35  * Creates a ServerModManager which targets worldpath
36  * @param worldpath
37  */
38 ServerModManager::ServerModManager(const std::string &worldpath) :
39                 ModConfiguration(worldpath)
40 {
41         SubgameSpec gamespec = findWorldSubgame(worldpath);
42
43         // Add all game mods and all world mods
44         std::string game_virtual_path;
45         game_virtual_path.append("games/").append(gamespec.id).append("/mods");
46         addModsInPath(gamespec.gamemods_path, game_virtual_path);
47         addModsInPath(worldpath + DIR_DELIM + "worldmods", "worldmods");
48
49         // Load normal mods
50         std::string worldmt = worldpath + DIR_DELIM + "world.mt";
51         addModsFromConfig(worldmt, gamespec.addon_mods_paths);
52 }
53
54 // clang-format off
55 // This function cannot be currenctly easily tested but it should be ASAP
56 void ServerModManager::loadMods(ServerScripting *script)
57 {
58         // Print mods
59         infostream << "Server: Loading mods: ";
60         for (const ModSpec &mod : m_sorted_mods) {
61                 infostream << mod.name << " ";
62         }
63         infostream << std::endl;
64         // Load and run "mod" scripts
65         for (const ModSpec &mod : m_sorted_mods) {
66                 mod.checkAndLog();
67
68                 std::string script_path = mod.path + DIR_DELIM + "init.lua";
69                 auto t = porting::getTimeMs();
70                 script->loadMod(script_path, mod.name);
71                 infostream << "Mod \"" << mod.name << "\" loaded after "
72                         << (porting::getTimeMs() - t) << " ms" << std::endl;
73         }
74
75         // Run a callback when mods are loaded
76         script->on_mods_loaded();
77 }
78
79 // clang-format on
80 const ModSpec *ServerModManager::getModSpec(const std::string &modname) const
81 {
82         std::vector<ModSpec>::const_iterator it;
83         for (it = m_sorted_mods.begin(); it != m_sorted_mods.end(); ++it) {
84                 const ModSpec &mod = *it;
85                 if (mod.name == modname)
86                         return &mod;
87         }
88         return NULL;
89 }
90
91 void ServerModManager::getModNames(std::vector<std::string> &modlist) const
92 {
93         for (const ModSpec &spec : m_sorted_mods)
94                 modlist.push_back(spec.name);
95 }
96
97 void ServerModManager::getModsMediaPaths(std::vector<std::string> &paths) const
98 {
99         for (auto it = m_sorted_mods.crbegin(); it != m_sorted_mods.crend(); it++) {
100                 const ModSpec &spec = *it;
101                 fs::GetRecursiveDirs(paths, spec.path + DIR_DELIM + "textures");
102                 fs::GetRecursiveDirs(paths, spec.path + DIR_DELIM + "sounds");
103                 fs::GetRecursiveDirs(paths, spec.path + DIR_DELIM + "media");
104                 fs::GetRecursiveDirs(paths, spec.path + DIR_DELIM + "models");
105                 fs::GetRecursiveDirs(paths, spec.path + DIR_DELIM + "locale");
106         }
107 }