182 lines
5.4 KiB
C++
182 lines
5.4 KiB
C++
/*
|
|
* =====================================================================================
|
|
*
|
|
* OpenMiner
|
|
*
|
|
* Copyright (C) 2018-2020 Unarelith, Quentin Bazin <openminer@unarelith.net>
|
|
* Copyright (C) 2019-2020 the OpenMiner contributors (see CONTRIBUTORS.md)
|
|
*
|
|
* This file is part of OpenMiner.
|
|
*
|
|
* OpenMiner is free software; you can redistribute it and/or
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
* License as published by the Free Software Foundation; either
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
*
|
|
* OpenMiner is distributed in the hope that it will be useful,
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
* Lesser General Public License for more details.
|
|
*
|
|
* You should have received a copy of the GNU Lesser General Public License
|
|
* along with OpenMiner; if not, write to the Free Software Foundation, Inc.,
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
*
|
|
* =====================================================================================
|
|
*/
|
|
#include <gk/core/Exception.hpp>
|
|
#include <gk/core/Utils.hpp>
|
|
|
|
#include <filesystem.hpp>
|
|
|
|
#include "LuaMod.hpp"
|
|
#include "ScriptEngine.hpp"
|
|
#include "ServerModLoader.hpp"
|
|
|
|
namespace fs = ghc::filesystem;
|
|
|
|
struct ModEntry {
|
|
fs::path path;
|
|
std::string id;
|
|
std::vector<std::string> dependencies;
|
|
};
|
|
|
|
void ServerModLoader::loadMods() {
|
|
std::unordered_map<std::string, ModEntry> mods;
|
|
sol::state lua;
|
|
|
|
fs::path basePath = fs::current_path();
|
|
fs::directory_iterator dir("mods/");
|
|
for (const auto &entry : dir) {
|
|
if (fs::exists(entry.path().string() + "/config.lua")) {
|
|
try {
|
|
lua.safe_script_file(entry.path().string() + "/config.lua");
|
|
|
|
sol::table config = lua["mod_config"].get<sol::table>();
|
|
|
|
ModEntry mod;
|
|
mod.path = entry.path().string();
|
|
mod.id = config["id"];
|
|
|
|
if (!mod.id.empty()) {
|
|
if (mod.id != entry.path().filename().string())
|
|
gkWarning() << ("Mod ID '" + mod.id + "' is different from folder name '" + entry.path().filename().string() + "'").c_str();
|
|
|
|
auto it = mods.find(mod.id);
|
|
if (it == mods.end()) {
|
|
sol::object dependencies = config["dependencies"];
|
|
if (dependencies.get_type() == sol::type::table) {
|
|
for (auto &it : dependencies.as<sol::table>()) {
|
|
mod.dependencies.emplace_back(it.second.as<std::string>());
|
|
}
|
|
}
|
|
else
|
|
gkWarning() << ("Failed to load dependencies for mod '" + mod.id + "'").c_str();
|
|
|
|
mods.emplace(mod.id, mod);
|
|
}
|
|
else {
|
|
gkError() << ("Trying to load mod '" + mod.id + "' twice").c_str();
|
|
gkError() << "Loaded path: " << it->second.path.string();
|
|
gkError() << "Current path:" << mod.path.string();
|
|
}
|
|
}
|
|
else
|
|
gkError() << ("Failed to load mod '" + mod.path.filename().string() + "': Mod ID required in 'config.lua'").c_str();
|
|
}
|
|
catch (sol::error &e) {
|
|
gkError() << e.what();
|
|
}
|
|
|
|
}
|
|
else
|
|
gkError() << ("The mod at '" + entry.path().string() + "' doesn't contain a 'config.lua' file.").c_str();
|
|
}
|
|
|
|
{
|
|
// Small BFS to check cyclic dependencies
|
|
for (auto &modit : mods) {
|
|
std::queue<std::string> queue;
|
|
queue.emplace(modit.first);
|
|
while (!queue.empty()) {
|
|
std::string modID = queue.front();
|
|
queue.pop();
|
|
|
|
auto it = mods.find(modID);
|
|
if (it == mods.end())
|
|
break;
|
|
|
|
for (auto &dep : it->second.dependencies) {
|
|
if (dep == modit.first)
|
|
throw EXCEPTION("Cyclic dependency detected for mod '"
|
|
+ modit.second.id + "' in mod '" + it->second.id + "'");
|
|
|
|
queue.emplace(dep);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// TODO: Build a dependency graph
|
|
// Two types of dependencies should be handled
|
|
// - Required (cyclic depedencies not handled)
|
|
// - Optional (cyclic dependencies handled)
|
|
|
|
// TODO: Load 'mods/config.lua' to let user define a preferred mod path (see #82)
|
|
|
|
m_scriptEngine.init();
|
|
m_scriptEngine.luaCore().setModLoader(this);
|
|
|
|
for (auto &it : mods) {
|
|
if (fs::exists(it.second.path.string() + "/init.lua")) {
|
|
fs::current_path(it.second.path.string());
|
|
|
|
try {
|
|
m_scriptEngine.lua().safe_script_file("init.lua");
|
|
}
|
|
catch (const sol::error &e) {
|
|
gkError() << "Error: Failed to load mod at" << it.second.path.string();
|
|
gkError() << e.what();
|
|
}
|
|
|
|
fs::current_path(basePath);
|
|
|
|
gkInfo() << "Mod" << it.first << "loaded";
|
|
}
|
|
else
|
|
gkError() << ("The mod at '" + it.second.path.string() + "' doesn't contain an 'init.lua' file.").c_str();
|
|
}
|
|
|
|
for (auto &it : m_mods) {
|
|
// gkDebug() << "Applying mod" << it.second.id() << "...";
|
|
it.second.commit();
|
|
}
|
|
}
|
|
|
|
LuaMod &ServerModLoader::registerMod(const std::string &name) {
|
|
// gkDebug("Registering mod" << mod.id() << "...";
|
|
|
|
if (!gk::regexMatch(name, "^[A-Za-z0-9_]+$") || name == "group")
|
|
throw std::runtime_error("Mod name '" + name + "' is invalid.");
|
|
|
|
auto it = m_mods.find(name);
|
|
if (it != m_mods.end())
|
|
throw std::runtime_error("The mod '" + name + "' has already been loaded. Mod name must be unique.");
|
|
|
|
m_mods.emplace(
|
|
std::piecewise_construct,
|
|
std::forward_as_tuple(name),
|
|
std::forward_as_tuple(name, m_registry, m_worldController, m_players)
|
|
);
|
|
|
|
return m_mods.at(name);
|
|
}
|
|
|
|
// Please update 'docs/lua-api-cpp.md' if you change this
|
|
void ServerModLoader::initUsertype(sol::state &lua) {
|
|
lua.new_usertype<ServerModLoader>("ServerModLoader",
|
|
"register_mod", &ServerModLoader::registerMod
|
|
);
|
|
}
|
|
|