]> git.lizzy.rs Git - dragonfireclient.git/blob - src/server/mods.cpp
Optimize getting active objects a bit. #8674
[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
26 /**
27  * Manage server mods
28  *
29  * All new calls to this class must be tested in test_servermodmanager.cpp
30  */
31
32 /**
33  * Creates a ServerModManager which targets worldpath
34  * @param worldpath
35  */
36 ServerModManager::ServerModManager(const std::string &worldpath) :
37                 ModConfiguration(worldpath)
38 {
39         SubgameSpec gamespec = findWorldSubgame(worldpath);
40
41         // Add all game mods and all world mods
42         addModsInPath(gamespec.gamemods_path);
43         addModsInPath(worldpath + DIR_DELIM + "worldmods");
44
45         // Load normal mods
46         std::string worldmt = worldpath + DIR_DELIM + "world.mt";
47         addModsFromConfig(worldmt, gamespec.addon_mods_paths);
48 }
49
50 // clang-format off
51 // This function cannot be currenctly easily tested but it should be ASAP
52 void ServerModManager::loadMods(ServerScripting *script)
53 {
54         // Print mods
55         infostream << "Server: Loading mods: ";
56         for (const ModSpec &mod : m_sorted_mods) {
57                 infostream << mod.name << " ";
58         }
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 "
65                                         "conventions: "
66                                         "Only characters [a-z0-9_] are allowed.");
67                 }
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;
77         }
78
79         // Run a callback when mods are loaded
80         script->on_mods_loaded();
81 }
82
83 // clang-format on
84 const ModSpec *ServerModManager::getModSpec(const std::string &modname) const
85 {
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)
90                         return &mod;
91         }
92         return NULL;
93 }
94
95 void ServerModManager::getModNames(std::vector<std::string> &modlist) const
96 {
97         for (const ModSpec &spec : m_sorted_mods)
98                 modlist.push_back(spec.name);
99 }
100
101 void ServerModManager::getModsMediaPaths(std::vector<std::string> &paths) const
102 {
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");
109         }
110 }