2024-10-28 15:57:39 +01:00
|
|
|
// Luanti
|
|
|
|
// SPDX-License-Identifier: LGPL-2.1-or-later
|
|
|
|
// Copyright (C) 2018 nerzhul, Loic Blot <loic.blot@unix-experience.fr>
|
2018-03-16 08:41:33 +01:00
|
|
|
|
|
|
|
#include "mods.h"
|
|
|
|
#include "filesys.h"
|
|
|
|
#include "log.h"
|
|
|
|
#include "scripting_server.h"
|
2018-04-17 14:54:50 +01:00
|
|
|
#include "content/subgames.h"
|
2020-04-08 20:13:23 +02:00
|
|
|
#include "porting.h"
|
2018-03-16 08:41:33 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Manage server mods
|
|
|
|
*
|
|
|
|
* All new calls to this class must be tested in test_servermodmanager.cpp
|
|
|
|
*/
|
|
|
|
|
2025-04-08 12:15:16 +02:00
|
|
|
ServerModManager::ServerModManager(const std::string &worldpath, SubgameSpec gamespec)
|
2018-03-16 08:41:33 +01:00
|
|
|
{
|
|
|
|
// Add all game mods and all world mods
|
2022-05-07 16:44:46 +01:00
|
|
|
configuration.addGameMods(gamespec);
|
|
|
|
configuration.addModsInPath(worldpath + DIR_DELIM + "worldmods", "worldmods");
|
2018-03-16 08:41:33 +01:00
|
|
|
|
|
|
|
// Load normal mods
|
|
|
|
std::string worldmt = worldpath + DIR_DELIM + "world.mt";
|
2022-05-07 16:44:46 +01:00
|
|
|
configuration.addModsFromConfig(worldmt, gamespec.addon_mods_paths);
|
|
|
|
configuration.checkConflictsAndDeps();
|
2018-03-16 08:41:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// This function cannot be currenctly easily tested but it should be ASAP
|
2024-03-12 14:13:24 +01:00
|
|
|
void ServerModManager::loadMods(ServerScripting &script)
|
2018-03-16 08:41:33 +01:00
|
|
|
{
|
|
|
|
// Print mods
|
|
|
|
infostream << "Server: Loading mods: ";
|
2022-05-07 16:44:46 +01:00
|
|
|
for (const ModSpec &mod : configuration.getMods()) {
|
2018-03-16 08:41:33 +01:00
|
|
|
infostream << mod.name << " ";
|
|
|
|
}
|
|
|
|
infostream << std::endl;
|
2025-04-07 20:23:13 +02:00
|
|
|
|
2018-03-16 08:41:33 +01:00
|
|
|
// Load and run "mod" scripts
|
2025-04-07 20:23:13 +02:00
|
|
|
auto t0 = porting::getTimeMs();
|
2022-05-07 16:44:46 +01:00
|
|
|
for (const ModSpec &mod : configuration.getMods()) {
|
2021-07-31 19:54:52 +02:00
|
|
|
mod.checkAndLog();
|
|
|
|
|
2025-04-07 20:23:13 +02:00
|
|
|
auto t1 = porting::getTimeMs();
|
2018-03-16 08:41:33 +01:00
|
|
|
std::string script_path = mod.path + DIR_DELIM + "init.lua";
|
2024-03-12 14:13:24 +01:00
|
|
|
script.loadMod(script_path, mod.name);
|
2018-03-24 00:31:33 +01:00
|
|
|
infostream << "Mod \"" << mod.name << "\" loaded after "
|
2025-04-07 20:23:13 +02:00
|
|
|
<< (porting::getTimeMs() - t1) << " ms" << std::endl;
|
2018-03-16 08:41:33 +01:00
|
|
|
}
|
2018-06-06 12:53:59 +02:00
|
|
|
|
|
|
|
// Run a callback when mods are loaded
|
2024-03-12 14:13:24 +01:00
|
|
|
script.on_mods_loaded();
|
2025-04-07 20:23:13 +02:00
|
|
|
|
|
|
|
infostream << "All mods loaded after " << (porting::getTimeMs() - t0)
|
|
|
|
<< " ms" << std::endl;
|
2018-03-16 08:41:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
const ModSpec *ServerModManager::getModSpec(const std::string &modname) const
|
|
|
|
{
|
2022-05-07 16:44:46 +01:00
|
|
|
for (const auto &mod : configuration.getMods()) {
|
2018-03-16 08:41:33 +01:00
|
|
|
if (mod.name == modname)
|
|
|
|
return &mod;
|
|
|
|
}
|
2022-05-07 16:44:46 +01:00
|
|
|
|
|
|
|
return nullptr;
|
2018-03-16 08:41:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void ServerModManager::getModNames(std::vector<std::string> &modlist) const
|
|
|
|
{
|
2022-05-07 16:44:46 +01:00
|
|
|
for (const ModSpec &spec : configuration.getMods())
|
2018-03-16 08:41:33 +01:00
|
|
|
modlist.push_back(spec.name);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ServerModManager::getModsMediaPaths(std::vector<std::string> &paths) const
|
|
|
|
{
|
2022-07-29 10:19:36 +02:00
|
|
|
// Iterate mods in reverse load order: Media loading expects higher priority media files first
|
|
|
|
// and mods loading later should be able to override media of already loaded mods
|
2022-07-30 12:51:23 +01:00
|
|
|
const auto &mods = configuration.getMods();
|
2022-07-29 10:19:36 +02:00
|
|
|
for (auto it = mods.crbegin(); it != mods.crend(); it++) {
|
|
|
|
const ModSpec &spec = *it;
|
2020-08-20 22:25:29 +02:00
|
|
|
fs::GetRecursiveDirs(paths, spec.path + DIR_DELIM + "textures");
|
|
|
|
fs::GetRecursiveDirs(paths, spec.path + DIR_DELIM + "sounds");
|
|
|
|
fs::GetRecursiveDirs(paths, spec.path + DIR_DELIM + "media");
|
|
|
|
fs::GetRecursiveDirs(paths, spec.path + DIR_DELIM + "models");
|
|
|
|
fs::GetRecursiveDirs(paths, spec.path + DIR_DELIM + "locale");
|
2025-01-19 20:42:40 +01:00
|
|
|
fs::GetRecursiveDirs(paths, spec.path + DIR_DELIM + "fonts");
|
2018-03-16 08:41:33 +01:00
|
|
|
}
|
|
|
|
}
|