[ServerModLoader] Now storing mods.

The changes are only committed on success.
Also, the mods now use their own working directory instead of the main.
This commit is contained in:
Quentin Bazin 2020-04-03 01:11:36 +02:00
parent 474eeec8f6
commit 9015830aef
6 changed files with 81 additions and 23 deletions

View File

@ -325,7 +325,7 @@ mod:block {
end
}
dofile("mods/default/blocks/workbench.lua")
dofile("mods/default/blocks/furnace.lua")
dofile("mods/default/blocks/door.lua")
dofile("blocks/workbench.lua")
dofile("blocks/furnace.lua")
dofile("blocks/door.lua")

View File

@ -26,13 +26,13 @@
--
mod = LuaMod.new("default")
dofile("mods/default/blocks.lua")
dofile("mods/default/items.lua")
dofile("mods/default/recipes.lua")
dofile("mods/default/sky.lua")
dofile("mods/default/trees.lua")
dofile("mods/default/biomes.lua")
dofile("mods/default/dimensions.lua")
dofile("blocks.lua")
dofile("items.lua")
dofile("recipes.lua")
dofile("sky.lua")
dofile("trees.lua")
dofile("biomes.lua")
dofile("dimensions.lua")
-- openminer:add_listener(EventType.OnBlockPlaced, function(pos, player, world, client, server)
-- server:send_chat_message(0, "Block placed at " .. pos.x .. ";" .. pos.y .. ";" .. pos.z .. " by Client" .. player:client_id(), client);

View File

@ -30,6 +30,26 @@
#include "Registry.hpp"
#include "Tree.hpp"
void LuaMod::commit() {
while (!m_defs.empty()) {
auto &it = m_defs.front();
switch (it.first) {
case DefinitionType::Block: m_blockLoader.loadBlock(it.second); break;
case DefinitionType::Item: m_itemLoader.loadItem(it.second); break;
case DefinitionType::CraftingRecipe: m_recipeLoader.loadCraftingRecipe(it.second); break;
case DefinitionType::SmeltingRecipe: m_recipeLoader.loadSmeltingRecipe(it.second); break;
case DefinitionType::Sky: m_skyLoader.loadSky(it.second); break;
case DefinitionType::Tree: m_biomeLoader.loadTree(it.second); break;
case DefinitionType::Biome: m_biomeLoader.loadBiome(it.second); break;
case DefinitionType::Dimension: m_dimensionLoader.loadDimension(it.second); break;
default: break;
}
m_defs.pop();
}
}
void LuaMod::initUsertype(sol::state &lua) {
lua.new_usertype<LuaMod>("LuaMod",
sol::constructors<LuaMod(std::string)>(),
@ -46,34 +66,34 @@ void LuaMod::initUsertype(sol::state &lua) {
}
void LuaMod::registerBlock(const sol::table &table) {
m_blockLoader.loadBlock(table);
m_defs.emplace(DefinitionType::Block, table);
}
void LuaMod::registerItem(const sol::table &table) {
m_itemLoader.loadItem(table);
m_defs.emplace(DefinitionType::Item, table);
}
void LuaMod::registerCraftingRecipe(const sol::table &table) {
m_recipeLoader.loadCraftingRecipe(table);
m_defs.emplace(DefinitionType::CraftingRecipe, table);
}
void LuaMod::registerSmeltingRecipe(const sol::table &table) {
m_recipeLoader.loadSmeltingRecipe(table);
m_defs.emplace(DefinitionType::SmeltingRecipe, table);
}
void LuaMod::registerSky(const sol::table &table) {
m_skyLoader.loadSky(table);
m_defs.emplace(DefinitionType::Sky, table);
}
void LuaMod::registerTree(const sol::table &table) {
m_biomeLoader.loadTree(table);
m_defs.emplace(DefinitionType::Tree, table);
}
void LuaMod::registerBiome(const sol::table &table) {
m_biomeLoader.loadBiome(table);
m_defs.emplace(DefinitionType::Biome, table);
}
void LuaMod::registerDimension(const sol::table &table) {
m_dimensionLoader.loadDimension(table);
m_defs.emplace(DefinitionType::Dimension, table);
}

View File

@ -27,6 +27,8 @@
#ifndef LUAMOD_HPP_
#define LUAMOD_HPP_
#include <queue>
#include "LuaBiomeLoader.hpp"
#include "LuaBlockLoader.hpp"
#include "LuaDimensionLoader.hpp"
@ -42,6 +44,8 @@ class LuaMod {
// Check if this name != "group"
LuaMod(const std::string &id) : m_id(id) {}
void commit();
const std::string &id() const { return m_id; }
static void initUsertype(sol::state &lua);
@ -56,6 +60,19 @@ class LuaMod {
void registerBiome(const sol::table &table);
void registerDimension(const sol::table &table);
enum class DefinitionType {
Block,
Item,
CraftingRecipe,
SmeltingRecipe,
Sky,
Tree,
Biome,
Dimension,
};
std::queue<std::pair<DefinitionType, sol::table>> m_defs;
std::string m_id;
LuaBlockLoader m_blockLoader{*this};

View File

@ -39,10 +39,14 @@ void ServerModLoader::loadMods() {
m_scriptEngine.luaCore().setModLoader(this);
try {
fs::path basePath = fs::current_path();
fs::directory_iterator dir("mods/");
for (const auto &entry : dir) {
if (fs::exists(entry.path().string() + "/init.lua")) {
m_scriptEngine.lua().safe_script_file(entry.path().string() + "/init.lua");
fs::current_path(entry.path().string());
m_scriptEngine.lua().safe_script_file("init.lua");
fs::current_path(basePath);
std::cout << "Mod '" + entry.path().filename().string() + "' loaded" << std::endl;
}
else
@ -53,9 +57,20 @@ void ServerModLoader::loadMods() {
std::cerr << e.what() << std::endl;
return;
}
for (auto &it : m_mods) {
// DEBUG("Applying mod '" + it.second.id() + "'...");
it.second.commit();
}
}
void ServerModLoader::registerMod(const LuaMod &mod) {
DEBUG("Registering mod", mod.id());
void ServerModLoader::registerMod(LuaMod &mod) {
// DEBUG("Registering mod '" + mod.id() + "'...");
auto it = m_mods.find(mod.id());
if (it == m_mods.end())
m_mods.emplace(mod.id(), mod);
else
DEBUG("ERROR: The mod '" + mod.id() + "' has already been loaded. Mod name must be unique.");
}

View File

@ -27,7 +27,11 @@
#ifndef SERVERMODLOADER_HPP_
#define SERVERMODLOADER_HPP_
class LuaMod;
#include <string>
#include <unordered_map>
#include "LuaMod.hpp"
class ScriptEngine;
class ServerModLoader {
@ -36,10 +40,12 @@ class ServerModLoader {
void loadMods();
void registerMod(const LuaMod &mod);
void registerMod(LuaMod &mod);
private:
ScriptEngine &m_scriptEngine;
std::unordered_map<std::string, LuaMod> m_mods;
};
#endif // SERVERMODLOADER_HPP_