diff --git a/mods/default/blocks.lua b/mods/default/blocks.lua index 1236ae6a..1c49d350 100644 --- a/mods/default/blocks.lua +++ b/mods/default/blocks.lua @@ -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") diff --git a/mods/default/init.lua b/mods/default/init.lua index 2230ff57..a5341223 100644 --- a/mods/default/init.lua +++ b/mods/default/init.lua @@ -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); diff --git a/source/server/lua/LuaMod.cpp b/source/server/lua/LuaMod.cpp index 4e4115f3..73e12bd8 100644 --- a/source/server/lua/LuaMod.cpp +++ b/source/server/lua/LuaMod.cpp @@ -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", sol::constructors(), @@ -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); } diff --git a/source/server/lua/LuaMod.hpp b/source/server/lua/LuaMod.hpp index 4a2a362e..eb17a53d 100644 --- a/source/server/lua/LuaMod.hpp +++ b/source/server/lua/LuaMod.hpp @@ -27,6 +27,8 @@ #ifndef LUAMOD_HPP_ #define LUAMOD_HPP_ +#include + #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> m_defs; + std::string m_id; LuaBlockLoader m_blockLoader{*this}; diff --git a/source/server/lua/ServerModLoader.cpp b/source/server/lua/ServerModLoader.cpp index 60732e8e..2763184b 100644 --- a/source/server/lua/ServerModLoader.cpp +++ b/source/server/lua/ServerModLoader.cpp @@ -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."); } diff --git a/source/server/lua/ServerModLoader.hpp b/source/server/lua/ServerModLoader.hpp index 3856211e..2d5ac9a9 100644 --- a/source/server/lua/ServerModLoader.hpp +++ b/source/server/lua/ServerModLoader.hpp @@ -27,7 +27,11 @@ #ifndef SERVERMODLOADER_HPP_ #define SERVERMODLOADER_HPP_ -class LuaMod; +#include +#include + +#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 m_mods; }; #endif // SERVERMODLOADER_HPP_