Skip to content

Commit

Permalink
[LuaKeyLoader] Now loading key definitions from Lua (see #109).
Browse files Browse the repository at this point in the history
  • Loading branch information
Unarelith committed Jun 19, 2020
1 parent c1176a4 commit b45ac1c
Show file tree
Hide file tree
Showing 10 changed files with 202 additions and 3 deletions.
10 changes: 10 additions & 0 deletions mods/default/inventory.lua
Original file line number Diff line number Diff line change
Expand Up @@ -90,3 +90,13 @@ function show_inventory(client, screen_width, screen_height, gui_scale)
gui:show(client)
end

mod:key {
id = "inventory",
name = "Inventory",
default_key = "E"
}

mod:key_callback("default:inventory", function(client, screen_width, screen_height, gui_scale)
show_inventory(client, screen_width, screen_height, gui_scale)
end)

3 changes: 2 additions & 1 deletion source/client/core/KeyboardHandler.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,10 @@ class KeyboardHandler : public gk::InputHandler {
std::string getKeyName(gk::GameKey key) { return gk::KeyboardUtils::getNameFromKey(m_keys[key]); }
void setKeycode(gk::GameKey key, sf::Keyboard::Key keycode) { m_keys[key] = keycode; }

protected:
void addKey(gk::GameKey key, const std::string &name, sf::Keyboard::Key defaultKey);
u32 keyCount() { return m_keyNames.size(); }

protected:
std::unordered_map<gk::GameKey, sf::Keyboard::Key> m_keys;
std::unordered_map<std::string, gk::GameKey> m_keyNames;
};
Expand Down
29 changes: 28 additions & 1 deletion source/common/core/Registry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -112,11 +112,28 @@ Dimension &Registry::registerSerializedDimension(sf::Packet &packet) {
m_dimensions.back().deserialize(packet);

u16 id = m_dimensions.size() - 1;
m_dimensionsID.emplace(m_biomes.back().stringID(), id);
m_dimensionsID.emplace(m_dimensions.back().stringID(), id);

return m_dimensions.back();
}

Key &Registry::registerKey(const std::string &stringID, const std::string &label) {
u16 id = m_keys.size();
m_keysID.emplace(stringID, id);
m_keys.emplace_back(id, stringID, label);
return m_keys.back();
}

Key &Registry::registerSerializedKey(sf::Packet &packet) {
m_keys.emplace_back();
m_keys.back().deserialize(packet);

u16 id = m_keys.size() - 1;
m_keysID.emplace(m_biomes.back().stringID(), id);

return m_keys.back();
}

entt::entity Registry::registerEntity(const std::string &stringID) {
auto it = m_entities.find(stringID);
if (it == m_entities.end()) {
Expand Down Expand Up @@ -231,6 +248,10 @@ void Registry::serialize(sf::Packet &packet) const {
for (auto &it : m_dimensions) {
packet << u8(DataType::Dimension) << it;
}

for (auto &it : m_keys) {
packet << u8(DataType::Key) << it;
}
}

void Registry::deserialize(sf::Packet &packet) {
Expand Down Expand Up @@ -261,6 +282,9 @@ void Registry::deserialize(sf::Packet &packet) {
else if (type == u8(DataType::Dimension)) {
registerSerializedDimension(packet);
}
else if (type == u8(DataType::Key)) {
registerSerializedKey(packet);
}
}
}

Expand All @@ -272,13 +296,15 @@ void Registry::clear() {
m_trees.clear();
m_biomes.clear();
m_dimensions.clear();
m_keys.clear();

m_blocksID.clear();
m_itemsID.clear();
m_skiesID.clear();
m_treesID.clear();
m_biomesID.clear();
m_dimensionsID.clear();
m_keysID.clear();

m_entities.clear();
m_entityRegistry.clear();
Expand All @@ -293,6 +319,7 @@ void Registry::initUsertype(sol::state &lua) {
"get_tree", &Registry::getTree,
"get_biome", &Registry::getBiome,
"get_recipe", &Registry::getRecipe,
"get_key", &Registry::getKey,

"get_block_from_string", &Registry::getBlockFromStringID,
"get_item_from_string", &Registry::getItemFromStringID,
Expand Down
11 changes: 10 additions & 1 deletion source/common/core/Registry.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
#include "Block.hpp"
#include "Dimension.hpp"
#include "Item.hpp"
#include "Key.hpp"
#include "Network.hpp"
#include "Recipe.hpp"
#include "Tree.hpp"
Expand Down Expand Up @@ -84,6 +85,9 @@ class Registry : public ISerializable {
Dimension &registerDimension(const std::string &stringID, const std::string &label);
Dimension &registerSerializedDimension(sf::Packet &packet);

Key &registerKey(const std::string &stringID, const std::string &label);
Key &registerSerializedKey(sf::Packet &packet);

entt::registry &entityRegistry() { return m_entityRegistry; }
entt::entity registerEntity(const std::string &stringID);

Expand All @@ -93,6 +97,7 @@ class Registry : public ISerializable {
const Tree &getTree(u16 id) const { return m_trees.at(id); }
const Biome &getBiome(u16 id) const { return m_biomes.at(id); }
const Dimension &getDimension(u16 id) const { return m_dimensions.at(id); }
const Key &getKey(u16 id) const { return m_keys.at(id); }

const Block &getBlockFromStringID(const std::string &stringID);
const Item &getItemFromStringID(const std::string &stringID);
Expand All @@ -115,6 +120,7 @@ class Registry : public ISerializable {
const std::vector<Tree> &trees() const { return m_trees; }
const std::vector<Biome> &biomes() const { return m_biomes; }
const std::vector<Dimension> &dimensions() const { return m_dimensions; }
const std::vector<Key> &keys() const { return m_keys; }

static Registry &getInstance() { return *s_instance; }
static void setInstance(Registry &instance) { s_instance = &instance; }
Expand All @@ -129,13 +135,15 @@ class Registry : public ISerializable {
std::vector<Tree> m_trees;
std::vector<Biome> m_biomes;
std::vector<Dimension> m_dimensions;
std::vector<Key> m_keys;

std::unordered_map<std::string, u32> m_blocksID;
std::unordered_map<std::string, u32> m_itemsID;
std::unordered_map<std::string, u16> m_skiesID;
std::unordered_map<std::string, u16> m_treesID;
std::unordered_map<std::string, u16> m_biomesID;
std::unordered_map<std::string, u16> m_dimensionsID;
std::unordered_map<std::string, u16> m_keysID;

std::unordered_map<std::string, entt::entity> m_entities;

Expand All @@ -149,7 +157,8 @@ class Registry : public ISerializable {
Sky,
Tree,
Biome,
Dimension
Dimension,
Key
};
};

Expand Down
2 changes: 2 additions & 0 deletions source/common/world/Dimension.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ class Dimension : public ISerializable {

u16 id() const { return m_id; }

const std::string &stringID() const { return m_stringID; }

void addBiome(const std::string &biome) { m_biomes.emplace_back(biome); }

void serialize(sf::Packet &packet) const override;
Expand Down
63 changes: 63 additions & 0 deletions source/common/world/Key.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
* =====================================================================================
*
* OpenMiner
*
* Copyright (C) 2018-2020 Unarelith, Quentin Bazin <[email protected]>
* 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
*
* =====================================================================================
*/
#ifndef KEY_HPP_
#define KEY_HPP_

#include <string>

#include <gk/core/IntTypes.hpp>

#include "ISerializable.hpp"
#include "NetworkUtils.hpp"

class Key : public ISerializable {
public:
Key() = default;
Key(u16 id, const std::string &stringID, const std::string &name)
: m_id(id), m_stringID(stringID), m_name(name) {}

void serialize(sf::Packet &packet) const override { packet << m_id << m_stringID << m_name << m_defaultKey; }
void deserialize(sf::Packet &packet) override { packet >> m_id >> m_stringID >> m_name >> m_defaultKey; }

u16 id() const { return m_id; }

const std::string &stringID() const { return m_stringID; }
const std::string &name() const { return m_name; }

const std::string &defaultKey() const { return m_defaultKey; }
void setDefaultKey(const std::string &defaultKey) { m_defaultKey = defaultKey; }

private:
u16 m_id;

std::string m_stringID;
std::string m_name;

std::string m_defaultKey;
};

#endif // KEY_HPP_
2 changes: 2 additions & 0 deletions source/server/lua/LuaMod.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ void LuaMod::commit() {
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;
case DefinitionType::Key: m_keyLoader.loadKey(it.second); break;
case DefinitionType::Entity: m_entityLoader.loadEntity(it.second); break;
default: break;
}
Expand Down Expand Up @@ -77,6 +78,7 @@ void LuaMod::initUsertype(sol::state &lua) {
"tree", DEF_FUNC(DefinitionType::Tree),
"biome", DEF_FUNC(DefinitionType::Biome),
"dimension", DEF_FUNC(DefinitionType::Dimension),
"key", DEF_FUNC(DefinitionType::Key),
"entity", DEF_FUNC(DefinitionType::Entity)
);
}
Expand Down
3 changes: 3 additions & 0 deletions source/server/lua/LuaMod.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
#include "LuaDimensionLoader.hpp"
#include "LuaEntityLoader.hpp"
#include "LuaItemLoader.hpp"
#include "LuaKeyLoader.hpp"
#include "LuaRecipeLoader.hpp"
#include "LuaSkyLoader.hpp"

Expand Down Expand Up @@ -77,6 +78,7 @@ class LuaMod {
Tree,
Biome,
Dimension,
Key,
Entity,
};

Expand All @@ -96,6 +98,7 @@ class LuaMod {
LuaBiomeLoader m_biomeLoader{*this};
LuaDimensionLoader m_dimensionLoader{*this};
LuaEntityLoader m_entityLoader{*this, m_worldController};
LuaKeyLoader m_keyLoader{*this};
};

#endif // LUAMOD_HPP_
38 changes: 38 additions & 0 deletions source/server/lua/loader/LuaKeyLoader.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* =====================================================================================
*
* OpenMiner
*
* Copyright (C) 2018-2020 Unarelith, Quentin Bazin <[email protected]>
* 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 "LuaKeyLoader.hpp"
#include "LuaMod.hpp"
#include "Registry.hpp"

void LuaKeyLoader::loadKey(const sol::table &table) const {
std::string stringID = m_mod.id() + ":" + table["id"].get<std::string>();
std::string name = table["name"].get_or<std::string>(stringID);
std::string defaultKey = table["default_key"].get_or<std::string>("");

Registry::getInstance().registerKey(stringID, name).setDefaultKey(defaultKey);
}

44 changes: 44 additions & 0 deletions source/server/lua/loader/LuaKeyLoader.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* =====================================================================================
*
* OpenMiner
*
* Copyright (C) 2018-2020 Unarelith, Quentin Bazin <[email protected]>
* 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
*
* =====================================================================================
*/
#ifndef LUAKEYLOADER_HPP_
#define LUAKEYLOADER_HPP_

#include <sol/sol.hpp>

class LuaMod;

class LuaKeyLoader {
public:
LuaKeyLoader(LuaMod &mod) : m_mod(mod) {}

void loadKey(const sol::table &table) const;

private:
LuaMod &m_mod;
};

#endif // LUAKEYLOADER_HPP_

0 comments on commit b45ac1c

Please sign in to comment.