Delete irrlichttypes_extrabloated.h (#15723)
[minetest.git] / src / server / mods.cpp
blobbfafe701de9fdf69b77ce636e659de2f67f838b0
1 // Luanti
2 // SPDX-License-Identifier: LGPL-2.1-or-later
3 // Copyright (C) 2018 nerzhul, Loic Blot <loic.blot@unix-experience.fr>
5 #include "mods.h"
6 #include "filesys.h"
7 #include "log.h"
8 #include "scripting_server.h"
9 #include "content/subgames.h"
10 #include "porting.h"
12 /**
13 * Manage server mods
15 * All new calls to this class must be tested in test_servermodmanager.cpp
18 /**
19 * Creates a ServerModManager which targets worldpath
20 * @param worldpath
22 ServerModManager::ServerModManager(const std::string &worldpath):
23 configuration()
25 SubgameSpec gamespec = findWorldSubgame(worldpath);
27 // Add all game mods and all world mods
28 configuration.addGameMods(gamespec);
29 configuration.addModsInPath(worldpath + DIR_DELIM + "worldmods", "worldmods");
31 // Load normal mods
32 std::string worldmt = worldpath + DIR_DELIM + "world.mt";
33 configuration.addModsFromConfig(worldmt, gamespec.addon_mods_paths);
34 configuration.checkConflictsAndDeps();
37 // This function cannot be currenctly easily tested but it should be ASAP
38 void ServerModManager::loadMods(ServerScripting &script)
40 // Print mods
41 infostream << "Server: Loading mods: ";
42 for (const ModSpec &mod : configuration.getMods()) {
43 infostream << mod.name << " ";
46 infostream << std::endl;
47 // Load and run "mod" scripts
48 for (const ModSpec &mod : configuration.getMods()) {
49 mod.checkAndLog();
51 std::string script_path = mod.path + DIR_DELIM + "init.lua";
52 auto t = porting::getTimeMs();
53 script.loadMod(script_path, mod.name);
54 infostream << "Mod \"" << mod.name << "\" loaded after "
55 << (porting::getTimeMs() - t) << " ms" << std::endl;
58 // Run a callback when mods are loaded
59 script.on_mods_loaded();
62 const ModSpec *ServerModManager::getModSpec(const std::string &modname) const
64 for (const auto &mod : configuration.getMods()) {
65 if (mod.name == modname)
66 return &mod;
69 return nullptr;
72 void ServerModManager::getModNames(std::vector<std::string> &modlist) const
74 for (const ModSpec &spec : configuration.getMods())
75 modlist.push_back(spec.name);
78 void ServerModManager::getModsMediaPaths(std::vector<std::string> &paths) const
80 // Iterate mods in reverse load order: Media loading expects higher priority media files first
81 // and mods loading later should be able to override media of already loaded mods
82 const auto &mods = configuration.getMods();
83 for (auto it = mods.crbegin(); it != mods.crend(); it++) {
84 const ModSpec &spec = *it;
85 fs::GetRecursiveDirs(paths, spec.path + DIR_DELIM + "textures");
86 fs::GetRecursiveDirs(paths, spec.path + DIR_DELIM + "sounds");
87 fs::GetRecursiveDirs(paths, spec.path + DIR_DELIM + "media");
88 fs::GetRecursiveDirs(paths, spec.path + DIR_DELIM + "models");
89 fs::GetRecursiveDirs(paths, spec.path + DIR_DELIM + "locale");
90 fs::GetRecursiveDirs(paths, spec.path + DIR_DELIM + "fonts");