[LuaKeyLoader] Now loading key definitions from Lua (see #109).
This commit is contained in:
parent
c1176a438d
commit
b45ac1cdbd
@ -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)
|
||||
|
||||
|
@ -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;
|
||||
};
|
||||
|
@ -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()) {
|
||||
@ -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) {
|
||||
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -272,6 +296,7 @@ void Registry::clear() {
|
||||
m_trees.clear();
|
||||
m_biomes.clear();
|
||||
m_dimensions.clear();
|
||||
m_keys.clear();
|
||||
|
||||
m_blocksID.clear();
|
||||
m_itemsID.clear();
|
||||
@ -279,6 +304,7 @@ void Registry::clear() {
|
||||
m_treesID.clear();
|
||||
m_biomesID.clear();
|
||||
m_dimensionsID.clear();
|
||||
m_keysID.clear();
|
||||
|
||||
m_entities.clear();
|
||||
m_entityRegistry.clear();
|
||||
@ -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,
|
||||
|
@ -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"
|
||||
@ -84,6 +85,9 @@ class Registry : public ISerializable {
|
||||
Dimension ®isterDimension(const std::string &stringID, const std::string &label);
|
||||
Dimension ®isterSerializedDimension(sf::Packet &packet);
|
||||
|
||||
Key ®isterKey(const std::string &stringID, const std::string &label);
|
||||
Key ®isterSerializedKey(sf::Packet &packet);
|
||||
|
||||
entt::registry &entityRegistry() { return m_entityRegistry; }
|
||||
entt::entity registerEntity(const std::string &stringID);
|
||||
|
||||
@ -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);
|
||||
@ -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; }
|
||||
@ -129,6 +135,7 @@ 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;
|
||||
@ -136,6 +143,7 @@ class Registry : public ISerializable {
|
||||
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;
|
||||
|
||||
@ -149,7 +157,8 @@ class Registry : public ISerializable {
|
||||
Sky,
|
||||
Tree,
|
||||
Biome,
|
||||
Dimension
|
||||
Dimension,
|
||||
Key
|
||||
};
|
||||
};
|
||||
|
||||
|
@ -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;
|
||||
|
63
source/common/world/Key.hpp
Normal file
63
source/common/world/Key.hpp
Normal file
@ -0,0 +1,63 @@
|
||||
/*
|
||||
* =====================================================================================
|
||||
*
|
||||
* 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
|
||||
*
|
||||
* =====================================================================================
|
||||
*/
|
||||
#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_
|
@ -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;
|
||||
}
|
||||
@ -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)
|
||||
);
|
||||
}
|
||||
|
@ -38,6 +38,7 @@
|
||||
#include "LuaDimensionLoader.hpp"
|
||||
#include "LuaEntityLoader.hpp"
|
||||
#include "LuaItemLoader.hpp"
|
||||
#include "LuaKeyLoader.hpp"
|
||||
#include "LuaRecipeLoader.hpp"
|
||||
#include "LuaSkyLoader.hpp"
|
||||
|
||||
@ -77,6 +78,7 @@ class LuaMod {
|
||||
Tree,
|
||||
Biome,
|
||||
Dimension,
|
||||
Key,
|
||||
Entity,
|
||||
};
|
||||
|
||||
@ -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
source/server/lua/loader/LuaKeyLoader.cpp
Normal file
38
source/server/lua/loader/LuaKeyLoader.cpp
Normal file
@ -0,0 +1,38 @@
|
||||
/*
|
||||
* =====================================================================================
|
||||
*
|
||||
* 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 "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
source/server/lua/loader/LuaKeyLoader.hpp
Normal file
44
source/server/lua/loader/LuaKeyLoader.hpp
Normal file
@ -0,0 +1,44 @@
|
||||
/*
|
||||
* =====================================================================================
|
||||
*
|
||||
* 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
|
||||
*
|
||||
* =====================================================================================
|
||||
*/
|
||||
#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_
|
Loading…
x
Reference in New Issue
Block a user