-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[BlockMetadata] Added. Now used in FurnaceWidget and furnace.lua
- Loading branch information
Showing
14 changed files
with
173 additions
and
265 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
/* | ||
* ===================================================================================== | ||
* | ||
* Filename: BlockMetadata.hpp | ||
* | ||
* Description: | ||
* | ||
* Created: 08/02/2020 16:02:50 | ||
* | ||
* Author: Quentin Bazin, <[email protected]> | ||
* | ||
* ===================================================================================== | ||
*/ | ||
#ifndef BLOCKMETADATA_HPP_ | ||
#define BLOCKMETADATA_HPP_ | ||
|
||
#include <memory> | ||
#include <string> | ||
#include <unordered_map> | ||
|
||
#include <sol.hpp> | ||
|
||
#include <gk/core/IntTypes.hpp> | ||
#include <gk/core/Exception.hpp> | ||
|
||
#include "ISerializable.hpp" | ||
|
||
class BlockMetadataValue { | ||
public: | ||
enum class Type : u8 { | ||
Undefined, | ||
String, | ||
Int | ||
}; | ||
|
||
template<typename T> | ||
T &get() const { | ||
return *std::static_pointer_cast<T>(m_value); | ||
} | ||
|
||
template<typename T> | ||
void set(const T &value, Type type) { | ||
m_value = std::make_shared<T>(value); | ||
m_type = type; | ||
} | ||
|
||
Type type() const { return m_type; } | ||
|
||
private: | ||
std::shared_ptr<void> m_value; | ||
Type m_type = Type::Undefined; | ||
}; | ||
|
||
class BlockMetadata : public ISerializable { | ||
public: | ||
void setString(const std::string &name, const std::string &value); | ||
void setInt(const std::string &name, int value); | ||
|
||
template<typename T> | ||
T &get(const std::string &name) { | ||
auto it = m_data.find(name); | ||
if (it == m_data.end()) | ||
throw EXCEPTION("Unable to find metadata named", name); | ||
|
||
return it->second.get<T>(); | ||
} | ||
|
||
template<typename T> | ||
sol::object getLuaObject(const std::string &name, sol::this_state state) { | ||
sol::state_view lua{state}; | ||
|
||
auto it = m_data.find(name); | ||
if (it == m_data.end()) | ||
return sol::make_object(lua, sol::lua_nil); | ||
|
||
return sol::make_object<T>(lua, it->second.get<T>()); | ||
} | ||
|
||
void serialize(sf::Packet &packet) const override; | ||
void deserialize(sf::Packet &packet) override; | ||
|
||
private: | ||
std::unordered_map<std::string, BlockMetadataValue> m_data; | ||
}; | ||
|
||
#endif // BLOCKMETADATA_HPP_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
/* | ||
* ===================================================================================== | ||
* | ||
* Filename: BlockMetadata.cpp | ||
* | ||
* Description: | ||
* | ||
* Created: 08/02/2020 16:03:18 | ||
* | ||
* Author: Quentin Bazin, <[email protected]> | ||
* | ||
* ===================================================================================== | ||
*/ | ||
#include <SFML/Network/Packet.hpp> | ||
|
||
#include "BlockMetadata.hpp" | ||
|
||
void BlockMetadata::setString(const std::string &name, const std::string &value) { | ||
m_data[name].set(value, BlockMetadataValue::Type::String); | ||
} | ||
|
||
void BlockMetadata::setInt(const std::string &name, int value) { | ||
m_data[name].set(value, BlockMetadataValue::Type::Int); | ||
} | ||
|
||
void BlockMetadata::serialize(sf::Packet &packet) const { | ||
packet << u32(m_data.size()); | ||
for (auto &it : m_data) { | ||
packet << u8(it.second.type()) << it.first; | ||
if (it.second.type() == BlockMetadataValue::Type::String) { | ||
packet << it.second.get<std::string>(); | ||
} | ||
else if (it.second.type() == BlockMetadataValue::Type::Int) { | ||
packet << it.second.get<int>(); | ||
} | ||
} | ||
} | ||
|
||
void BlockMetadata::deserialize(sf::Packet &packet) { | ||
u32 size; | ||
packet >> size; | ||
for (u32 i = 0 ; i < size ; ++i) { | ||
u8 typeU8; | ||
std::string name; | ||
packet >> typeU8 >> name; | ||
|
||
BlockMetadataValue::Type type = BlockMetadataValue::Type(typeU8); | ||
if (type == BlockMetadataValue::Type::String) { | ||
std::string value; | ||
packet >> value; | ||
m_data[name].set(value, type); | ||
} | ||
else if (type == BlockMetadataValue::Type::Int) { | ||
int value; | ||
packet >> value; | ||
m_data[name].set(value, type); | ||
} | ||
} | ||
} | ||
|
Oops, something went wrong.