Skip to content

Commit

Permalink
[ServerModLoader] Now handling mod name conflicts.
Browse files Browse the repository at this point in the history
  • Loading branch information
Unarelith committed Apr 3, 2020
1 parent 9015830 commit ead5e7d
Show file tree
Hide file tree
Showing 10 changed files with 47 additions and 45 deletions.
2 changes: 1 addition & 1 deletion docs/lua-api-cpp.md
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@

## ServerModLoader

- `void register_mod(LuaMod mod)` (see [Lua Mod API](/lua-api-mod#example))
- `LuaMod *register_mod(string mod_id)` (see [Lua Mod API](/lua-api-mod#example))

## World

Expand Down
10 changes: 4 additions & 6 deletions docs/lua-api-mod.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,20 @@

You can create a mod using this function:
```
LuaMod.new("mod_name")
openminer.mod_loader:register_mod("mod_name")
```

Mod name should match this regex: `[a-zA-Z0-9_]+`
Mod name should match this regex: `^[a-zA-Z0-9_]+$`

Everytime your define an `id` for a mod element, this name will be prepended to it.
Everytime you define an `id` for a mod element, this name will be prepended to it.
For example, `myitem` will become `mymod:myitem`.

The name can't be `group` because this namespace is already used by the engine.

## Example

```lua
local mod = LuaMod.new("mymod")
local mod = openminer.mod_loader:register_mod("mymod")

mod:block {
id = "myblock",
Expand Down Expand Up @@ -50,8 +50,6 @@ mod:smelting_recipe {
input = {id = "mymod:myitem", amount = 1},
output = {id = "mymod:myblock", amount = 1}
}

openminer.mod_loader:register_mod(mod)
```

## Functions
Expand Down
6 changes: 2 additions & 4 deletions mods/creative_inventory/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@
--
-- =====================================================================================
--
mod = LuaMod.new("creative_inventory")
mod = openminer.mod_loader:register_mod("creative_inventory")

function show_creative_window(client, screen_width, screen_height, gui_scale)
items = {}
for k, v in pairs(openminer:registry():items()) do
for k, v in pairs(openminer.registry:items()) do
if k ~= 1 and not v:has_group("group:ci_ignore") then
items[#items + 1] = {v:string_id()}
end
Expand Down Expand Up @@ -104,5 +104,3 @@ function show_creative_window(client, screen_width, screen_height, gui_scale)
gui:show(client);
end

openminer.mod_loader:register_mod(mod)

4 changes: 2 additions & 2 deletions mods/default/blocks.lua
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,7 @@ mod:block {
tiles = "redstone_lamp_off.png",

on_block_activated = function(pos, player, world, client, server, screen_width, screen_height, gui_scale)
local block = openminer:registry():get_block_from_string("default:redstone_lamp_on")
local block = openminer.registry:get_block_from_string("default:redstone_lamp_on")
world:set_block(pos.x, pos.y, pos.z, block:id())
end
}
Expand All @@ -320,7 +320,7 @@ mod:block {
},

on_block_activated = function(pos, player, world, client, server, screen_width, screen_height, gui_scale)
local block = openminer:registry():get_block_from_string("default:redstone_lamp_off")
local block = openminer.registry:get_block_from_string("default:redstone_lamp_off")
world:set_block(pos.x, pos.y, pos.z, block:id())
end
}
Expand Down
6 changes: 3 additions & 3 deletions mods/default/blocks/door.lua
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ mod:block {
end,

on_block_destroyed = function(pos, world)
local lower_block_id = openminer:registry():get_block_from_string("default:door_wood_lower"):id()
local lower_block_id = openminer.registry:get_block_from_string("default:door_wood_lower"):id()
if world:get_block(pos.x, pos.y, pos.z - 1) == lower_block_id then
world:set_block(pos.x, pos.y, pos.z - 1, 0)
end
Expand Down Expand Up @@ -91,13 +91,13 @@ mod:block {
local data = world:get_data(pos.x, pos.y, pos.z)
world:set_data(pos.x, pos.y, pos.z + 1, data)

local upper_block_id = openminer:registry():get_block_from_string("default:door_wood_upper"):id()
local upper_block_id = openminer.registry:get_block_from_string("default:door_wood_upper"):id()
world:set_block(pos.x, pos.y, pos.z + 1, upper_block_id)
end
end,

on_block_destroyed = function(pos, world)
local upper_block_id = openminer:registry():get_block_from_string("default:door_wood_upper"):id()
local upper_block_id = openminer.registry:get_block_from_string("default:door_wood_upper"):id()
if world:get_block(pos.x, pos.y, pos.z + 1) == upper_block_id then
world:set_block(pos.x, pos.y, pos.z + 1, 0)
end
Expand Down
2 changes: 1 addition & 1 deletion mods/default/blocks/furnace.lua
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ mod:block {
local current_burn_time = data.meta:get_int("current_burn_time") or 0
local item_progress = data.meta:get_int("item_progress") or 0

local recipe = openminer:registry():get_recipe(data.inventory)
local recipe = openminer.registry:get_recipe(data.inventory)
if recipe and recipe:type() ~= "smelt" then
recipe = nil
end
Expand Down
4 changes: 1 addition & 3 deletions mods/default/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
--
-- =====================================================================================
--
mod = LuaMod.new("default")
mod = openminer.mod_loader:register_mod("default")

dofile("blocks.lua")
dofile("items.lua")
Expand Down Expand Up @@ -135,5 +135,3 @@ function show_inventory(client, screen_width, screen_height, gui_scale)
gui:show(client)
end

openminer.mod_loader:register_mod(mod)

4 changes: 2 additions & 2 deletions source/client/states/SettingsMenuState.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -198,12 +198,12 @@ void SettingsMenuState::addGraphicsButtons() {
button.setText("Mipmap Levels: " + std::to_string(Config::mipmapLevels));
});

m_menuWidget.addButton("AO Strength: " + gk::to_string(Config::aoStrength, 2), [] (TextButton &button) {
m_menuWidget.addButton("AO Strength: " + gk::toString(Config::aoStrength, 2), [] (TextButton &button) {
Config::aoStrength += 0.25f;
if (Config::aoStrength > 1.5f)
Config::aoStrength = 0.f;

button.setText("AO Strength: " + gk::to_string(Config::aoStrength, 2));
button.setText("AO Strength: " + gk::toString(Config::aoStrength, 2));

World::isReloadRequested = true;
});
Expand Down
52 changes: 30 additions & 22 deletions source/server/lua/ServerModLoader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@
*
* =====================================================================================
*/
#include <gk/core/Debug.hpp>
#include <gk/core/Exception.hpp>
#include <gk/core/Utils.hpp>

#include <filesystem.hpp>

Expand All @@ -38,24 +39,26 @@ void ServerModLoader::loadMods() {
m_scriptEngine.init();
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")) {
fs::current_path(entry.path().string());
m_scriptEngine.lua().safe_script_file("init.lua");
fs::current_path(basePath);
fs::path basePath = fs::current_path();
fs::directory_iterator dir("mods/");
for (const auto &entry : dir) {
if (fs::exists(entry.path().string() + "/init.lua")) {
fs::current_path(entry.path().string());

std::cout << "Mod '" + entry.path().filename().string() + "' loaded" << std::endl;
try {
m_scriptEngine.lua().safe_script_file("init.lua");
}
catch (const sol::error &e) {
std::cerr << "Error: Failed to load mod at '" << entry.path().string() << "'" << std::endl;
std::cerr << e.what() << std::endl;
}
else
DEBUG("WARNING: The mod '" + entry.path().filename().string() + "' doesn't contain an 'init.lua' file.");

fs::current_path(basePath);

std::cout << "Mod '" + entry.path().filename().string() + "' loaded" << std::endl;
}
}
catch (const sol::error &e) {
std::cerr << e.what() << std::endl;
return;
else
DEBUG("WARNING: The mod '" + entry.path().filename().string() + "' doesn't contain an 'init.lua' file.");
}

for (auto &it : m_mods) {
Expand All @@ -64,13 +67,18 @@ void ServerModLoader::loadMods() {
}
}

void ServerModLoader::registerMod(LuaMod &mod) {
LuaMod &ServerModLoader::registerMod(const std::string &name) {
// 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.");
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(name, name);

return m_mods.at(name);
}

2 changes: 1 addition & 1 deletion source/server/lua/ServerModLoader.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class ServerModLoader {

void loadMods();

void registerMod(LuaMod &mod);
LuaMod &registerMod(const std::string &name);

private:
ScriptEngine &m_scriptEngine;
Expand Down

0 comments on commit ead5e7d

Please sign in to comment.