EntityFileParser
This commit is contained in:
@@ -37,15 +37,21 @@ public:
|
|||||||
// @param EntityID The parent of the entity
|
// @param EntityID The parent of the entity
|
||||||
typedef std::function<void(EntityID, EntityID)> OnStartEntityCallback;
|
typedef std::function<void(EntityID, EntityID)> OnStartEntityCallback;
|
||||||
void SetStartEntityCallback(OnStartEntityCallback c) { m_OnStartEntityCallback = c; }
|
void SetStartEntityCallback(OnStartEntityCallback c) { m_OnStartEntityCallback = c; }
|
||||||
|
// @param EntityID The entity the component corresponds to
|
||||||
// @param std::string Type name of the component
|
// @param std::string Type name of the component
|
||||||
typedef std::function<void(std::string)> OnStartComponentCallback;
|
typedef std::function<void(EntityID, std::string)> OnStartComponentCallback;
|
||||||
void SetStartComponentCallback(OnStartComponentCallback c) { m_OnStartComponentCallback = c; }
|
void SetStartComponentCallback(OnStartComponentCallback c) { m_OnStartComponentCallback = c; }
|
||||||
|
// @param EntityID Entity
|
||||||
|
// @param std::string Component name
|
||||||
// @param std::string Field name
|
// @param std::string Field name
|
||||||
// @param std::string Field type
|
// @param std::map<std::string, std::string> Field attribute names and values
|
||||||
typedef std::function<void(std::string, std::string)> OnStartFieldCallback;
|
typedef std::function<void(EntityID, std::string, std::string, std::map<std::string, std::string>)> OnStartFieldCallback;
|
||||||
void SetStartFieldCallback(OnStartFieldCallback c) { m_OnStartFieldCallback = c; }
|
void SetStartFieldCallback(OnStartFieldCallback c) { m_OnStartFieldCallback = c; }
|
||||||
|
// @param EntityID Entity
|
||||||
|
// @param std::string Component name
|
||||||
|
// @param std::string Field name
|
||||||
// @param char* Field data
|
// @param char* Field data
|
||||||
typedef std::function<void(char*)> OnStartFieldDataCallback;
|
typedef std::function<void(EntityID, std::string, std::string, const char*)> OnStartFieldDataCallback;
|
||||||
void SetStartFieldDataCallback(OnStartFieldDataCallback c) { m_OnStartFieldDataCallback = c; }
|
void SetStartFieldDataCallback(OnStartFieldDataCallback c) { m_OnStartFieldDataCallback = c; }
|
||||||
|
|
||||||
private:
|
private:
|
||||||
@@ -72,15 +78,16 @@ public:
|
|||||||
{
|
{
|
||||||
// 0 is imaginary world entity
|
// 0 is imaginary world entity
|
||||||
m_EntityStack.push(0);
|
m_EntityStack.push(0);
|
||||||
|
m_StateStack.push(State::Unknown);
|
||||||
}
|
}
|
||||||
|
|
||||||
void startElement(const XMLCh* const _uri, const XMLCh* const _localName, const XMLCh* const _qname, const xercesc::Attributes& attrs) override
|
void startElement(const XMLCh* const _uri, const XMLCh* const _localName, const XMLCh* const _qname, const xercesc::Attributes& attrs) override
|
||||||
{
|
{
|
||||||
std::string name = XS::ToString(_localName);
|
std::string name = XS::ToString(_localName);
|
||||||
|
|
||||||
if (m_CurrentScope == State::Unknown) {
|
if (m_StateStack.top() == State::Unknown || m_StateStack.top() == State::Entity) {
|
||||||
if (name == "Entity") {
|
if (name == "Entity") {
|
||||||
m_CurrentScope = State::Entity;
|
m_StateStack.push(State::Entity);
|
||||||
onStartEntity(attrs);
|
onStartEntity(attrs);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -91,16 +98,16 @@ public:
|
|||||||
}
|
}
|
||||||
|
|
||||||
std::string uri = XS::ToString(_uri);
|
std::string uri = XS::ToString(_uri);
|
||||||
if (m_CurrentScope == State::Entity) {
|
if (m_StateStack.top() == State::Entity) {
|
||||||
if (uri == "components") {
|
if (uri == "components") {
|
||||||
m_CurrentScope = State::Component;
|
m_StateStack.push(State::Component);
|
||||||
onStartComponent(name);
|
onStartComponent(name);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (m_CurrentScope == State::Component) {
|
if (m_StateStack.top() == State::Component) {
|
||||||
m_CurrentScope = State::ComponentField;
|
m_StateStack.push(State::ComponentField);
|
||||||
onStartComponentField(name, attrs);
|
onStartComponentField(name, attrs);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -108,7 +115,7 @@ public:
|
|||||||
|
|
||||||
void characters(const XMLCh* const chars, const XMLSize_t length) override
|
void characters(const XMLCh* const chars, const XMLSize_t length) override
|
||||||
{
|
{
|
||||||
if (m_CurrentScope == State::ComponentField) {
|
if (m_StateStack.top() == State::ComponentField) {
|
||||||
char* transcoded = xercesc::XMLString::transcode(chars);
|
char* transcoded = xercesc::XMLString::transcode(chars);
|
||||||
onFieldData(transcoded);
|
onFieldData(transcoded);
|
||||||
}
|
}
|
||||||
@@ -117,25 +124,25 @@ public:
|
|||||||
void endElement(const XMLCh* const _uri, const XMLCh* const _localName, const XMLCh* const _qname) override
|
void endElement(const XMLCh* const _uri, const XMLCh* const _localName, const XMLCh* const _qname) override
|
||||||
{
|
{
|
||||||
std::string name = XS::ToString(_localName);
|
std::string name = XS::ToString(_localName);
|
||||||
if (m_CurrentScope == State::Entity) {
|
if (m_StateStack.top() == State::Entity) {
|
||||||
if (name == "Entity") {
|
if (name == "Entity") {
|
||||||
m_CurrentScope = State::Unknown;
|
m_StateStack.pop();
|
||||||
onEndEntity();
|
onEndEntity();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string uri = XS::ToString(_uri);
|
std::string uri = XS::ToString(_uri);
|
||||||
if (m_CurrentScope == State::Component) {
|
if (m_StateStack.top() == State::Component) {
|
||||||
//if (uri == "components") {
|
//if (uri == "components") {
|
||||||
m_CurrentScope = State::Entity;
|
m_StateStack.pop();
|
||||||
onEndComponent(name);
|
onEndComponent(name);
|
||||||
return;
|
return;
|
||||||
//}
|
//}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (m_CurrentScope == State::ComponentField) {
|
if (m_StateStack.top() == State::ComponentField) {
|
||||||
m_CurrentScope = State::Component;
|
m_StateStack.pop();
|
||||||
onEndComponentField(name);
|
onEndComponentField(name);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -150,10 +157,14 @@ public:
|
|||||||
|
|
||||||
private:
|
private:
|
||||||
const EntityFileHandler* m_Handler;
|
const EntityFileHandler* m_Handler;
|
||||||
|
xercesc::XMLGrammarPool* m_GrammarPool;
|
||||||
xercesc::SAX2XMLReader* m_Reader;
|
xercesc::SAX2XMLReader* m_Reader;
|
||||||
State m_CurrentScope = State::Unknown;
|
//State m_CurrentScope = State::Unknown;
|
||||||
|
std::stack<State> m_StateStack;
|
||||||
unsigned int m_NextEntityID = 1;
|
unsigned int m_NextEntityID = 1;
|
||||||
std::stack<EntityID> m_EntityStack;
|
std::stack<EntityID> m_EntityStack;
|
||||||
|
std::string m_CurrentComponent;
|
||||||
|
std::string m_CurrentField;
|
||||||
std::map<std::string, std::string> m_CurrentAttributes;
|
std::map<std::string, std::string> m_CurrentAttributes;
|
||||||
|
|
||||||
void onStartEntity(const xercesc::Attributes& attrs)
|
void onStartEntity(const xercesc::Attributes& attrs)
|
||||||
@@ -189,41 +200,36 @@ private:
|
|||||||
void onStartComponent(std::string name)
|
void onStartComponent(std::string name)
|
||||||
{
|
{
|
||||||
//LOG_DEBUG(" Component: %s", name.c_str());
|
//LOG_DEBUG(" Component: %s", name.c_str());
|
||||||
|
m_CurrentComponent = name;
|
||||||
if (m_Handler->m_OnStartComponentCallback) {
|
if (m_Handler->m_OnStartComponentCallback) {
|
||||||
m_Handler->m_OnStartComponentCallback(name);
|
m_Handler->m_OnStartComponentCallback(m_EntityStack.top(), name);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
void onEndComponent(std::string name)
|
void onEndComponent(std::string name) { }
|
||||||
{
|
|
||||||
}
|
|
||||||
void onStartComponentField(std::string field, const xercesc::Attributes& attrs)
|
void onStartComponentField(std::string field, const xercesc::Attributes& attrs)
|
||||||
{
|
{
|
||||||
//LOG_DEBUG(" Field: %s", field.c_str());
|
//LOG_DEBUG(" Field: %s", field.c_str());
|
||||||
|
m_CurrentField = field;
|
||||||
|
m_CurrentAttributes.clear();
|
||||||
for (int i = 0; i < attrs.getLength(); i++) {
|
for (int i = 0; i < attrs.getLength(); i++) {
|
||||||
auto name = attrs.getQName(i);
|
auto name = attrs.getQName(i);
|
||||||
auto value = attrs.getValue(name);
|
auto value = attrs.getValue(name);
|
||||||
|
|
||||||
//LOG_DEBUG(" %s = %s", (char*)XS::ToString(name), (char*)XS::ToString(value));
|
//LOG_DEBUG(" %s = %s", (char*)XS::ToString(name), (char*)XS::ToString(value));
|
||||||
m_CurrentAttributes[XS::ToString(name).operator std::string()] = XS::ToString(value).operator std::string();
|
m_CurrentAttributes[XS::ToString(name).operator std::string()] = XS::ToString(value).operator std::string();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (m_Handler->m_OnStartFieldCallback) {
|
if (m_Handler->m_OnStartFieldCallback) {
|
||||||
m_Handler->m_OnStartFieldCallback(field, "comment?");
|
m_Handler->m_OnStartFieldCallback(m_EntityStack.top(), m_CurrentComponent, field, m_CurrentAttributes);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
void onEndComponentField(std::string field)
|
void onEndComponentField(std::string field) { }
|
||||||
{
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
void onFieldData(char* data)
|
void onFieldData(char* data)
|
||||||
{
|
{
|
||||||
//LOG_DEBUG(" Data: %s", data);
|
//LOG_DEBUG(" Data: %s", data);
|
||||||
|
|
||||||
if (m_Handler->m_OnStartFieldDataCallback) {
|
if (m_Handler->m_OnStartFieldDataCallback) {
|
||||||
m_Handler->m_OnStartFieldDataCallback(data);
|
m_Handler->m_OnStartFieldDataCallback(m_EntityStack.top(), m_CurrentComponent, m_CurrentField, data);
|
||||||
}
|
}
|
||||||
|
|
||||||
xercesc::XMLString::release(&data);
|
xercesc::XMLString::release(&data);
|
||||||
|
|||||||
@@ -0,0 +1,108 @@
|
|||||||
|
#include "EntityFile.h"
|
||||||
|
#include "World.h"
|
||||||
|
|
||||||
|
class EntityFileParser
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
EntityFileParser(EntityFile* entityFile)
|
||||||
|
: m_EntityFile(entityFile)
|
||||||
|
{
|
||||||
|
m_Handler.SetStartEntityCallback(std::bind(&EntityFileParser::onStartEntity, this, std::placeholders::_1, std::placeholders::_2));
|
||||||
|
m_Handler.SetStartComponentCallback(std::bind(&EntityFileParser::onStartComponent, this, std::placeholders::_1, std::placeholders::_2));
|
||||||
|
m_Handler.SetStartFieldCallback(std::bind(&EntityFileParser::onStartComponentField, this, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3, std::placeholders::_4));
|
||||||
|
m_Handler.SetStartFieldDataCallback(std::bind(&EntityFileParser::onFieldData, this, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3, std::placeholders::_4));
|
||||||
|
}
|
||||||
|
|
||||||
|
void MergeEntities(World* world)
|
||||||
|
{
|
||||||
|
m_World = world;
|
||||||
|
m_EntityIDMapper[0] = 0;
|
||||||
|
m_EntityFile->Parse(&m_Handler);
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
EntityFile* m_EntityFile;
|
||||||
|
EntityFileHandler m_Handler;
|
||||||
|
World* m_World = nullptr;
|
||||||
|
// Maps EntityIDs local to the file to real IDs in the world
|
||||||
|
std::map<EntityID, EntityID> m_EntityIDMapper;
|
||||||
|
|
||||||
|
void onStartEntity(EntityID entity, EntityID parent)
|
||||||
|
{
|
||||||
|
EntityID realParent = m_EntityIDMapper.at(parent);
|
||||||
|
EntityID realEntity = m_World->CreateEntity(realParent);
|
||||||
|
m_EntityIDMapper[entity] = realEntity;
|
||||||
|
LOG_DEBUG("Created entity #%i (%i) with parent %i (%i)", entity, realEntity, parent, realParent);
|
||||||
|
}
|
||||||
|
|
||||||
|
void onStartComponent(EntityID entity, std::string component)
|
||||||
|
{
|
||||||
|
EntityID realEntity = m_EntityIDMapper.at(entity);
|
||||||
|
m_World->AttachComponent(entity, component);
|
||||||
|
LOG_DEBUG("Attached component of type \"%s\" to entity #%i (%i)", component.c_str(), entity, realEntity);
|
||||||
|
}
|
||||||
|
|
||||||
|
void onStartComponentField(EntityID entity, std::string componentType, std::string fieldName, std::map<std::string, std::string> attributes)
|
||||||
|
{
|
||||||
|
EntityID realEntity = m_EntityIDMapper.at(entity);
|
||||||
|
ComponentWrapper component = m_World->GetComponent(realEntity, componentType);
|
||||||
|
std::string fieldType = component.Info.FieldTypes.at(fieldName);
|
||||||
|
|
||||||
|
LOG_DEBUG("Field \"%s\" type \"%s\"", fieldName.c_str(), fieldType.c_str());
|
||||||
|
LOG_DEBUG("Attributes:");
|
||||||
|
for (auto& kv : attributes) {
|
||||||
|
LOG_DEBUG("\t%s = %s", kv.first.c_str(), kv.second.c_str());
|
||||||
|
}
|
||||||
|
|
||||||
|
char* data = component.Data + component.Info.FieldOffsets.at(fieldName);
|
||||||
|
if (fieldType == "Vector") {
|
||||||
|
glm::vec3 vec;
|
||||||
|
vec.x = boost::lexical_cast<float>(attributes.at("X"));
|
||||||
|
vec.y = boost::lexical_cast<float>(attributes.at("Y"));
|
||||||
|
vec.z = boost::lexical_cast<float>(attributes.at("Z"));
|
||||||
|
memcpy(data, reinterpret_cast<char*>(&vec), EntityFile::GetTypeStride(fieldType));
|
||||||
|
} else if (fieldType == "Color") {
|
||||||
|
glm::vec4 vec;
|
||||||
|
vec.r = boost::lexical_cast<float>(attributes.at("R"));
|
||||||
|
vec.g = boost::lexical_cast<float>(attributes.at("G"));
|
||||||
|
vec.b = boost::lexical_cast<float>(attributes.at("B"));
|
||||||
|
vec.a = boost::lexical_cast<float>(attributes.at("A"));
|
||||||
|
memcpy(data, reinterpret_cast<char*>(&vec), EntityFile::GetTypeStride(fieldType));
|
||||||
|
} else if (fieldType == "Quaternion") {
|
||||||
|
glm::quat q;
|
||||||
|
q.x = boost::lexical_cast<float>(attributes.at("X"));
|
||||||
|
q.y = boost::lexical_cast<float>(attributes.at("Y"));
|
||||||
|
q.z = boost::lexical_cast<float>(attributes.at("Z"));
|
||||||
|
q.w = boost::lexical_cast<float>(attributes.at("W"));
|
||||||
|
memcpy(data, reinterpret_cast<char*>(&q), EntityFile::GetTypeStride(fieldType));
|
||||||
|
} else if (!attributes.empty()) {
|
||||||
|
LOG_WARNING("%i attributes not handled by any type conversion!", attributes.size());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void onFieldData(EntityID entity, std::string componentType, std::string fieldName, const char* fieldData)
|
||||||
|
{
|
||||||
|
EntityID realEntity = m_EntityIDMapper.at(entity);
|
||||||
|
ComponentWrapper component = m_World->GetComponent(realEntity, componentType);
|
||||||
|
std::string fieldType = component.Info.FieldTypes.at(fieldName);
|
||||||
|
|
||||||
|
char* data = component.Data + component.Info.FieldOffsets.at(fieldName);
|
||||||
|
if (fieldType == "int") {
|
||||||
|
int value = boost::lexical_cast<int>(fieldData);
|
||||||
|
memcpy(data, reinterpret_cast<char*>(&value), EntityFile::GetTypeStride(fieldType));
|
||||||
|
} else if (fieldType == "float") {
|
||||||
|
float value = boost::lexical_cast<float>(fieldData);
|
||||||
|
memcpy(data, reinterpret_cast<char*>(&value), EntityFile::GetTypeStride(fieldType));
|
||||||
|
} else if (fieldType == "double") {
|
||||||
|
double value = boost::lexical_cast<double>(fieldData);
|
||||||
|
memcpy(data, reinterpret_cast<char*>(&value), EntityFile::GetTypeStride(fieldType));
|
||||||
|
} else if (fieldType == "bool") {
|
||||||
|
bool value = (fieldData[0] == 't'); // Lazy bool evaluation
|
||||||
|
memcpy(data, reinterpret_cast<char*>(&value), EntityFile::GetTypeStride(fieldType));
|
||||||
|
} else if (fieldType == "string") {
|
||||||
|
new (data) std::string(fieldData);
|
||||||
|
} else {
|
||||||
|
LOG_WARNING("Unknown data type: %s", fieldType.c_str());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
@@ -21,7 +21,7 @@ public:
|
|||||||
{
|
{
|
||||||
m_EntityFile = ResourceManager::Load<EntityFile>(path);
|
m_EntityFile = ResourceManager::Load<EntityFile>(path);
|
||||||
EntityFileHandler handler;
|
EntityFileHandler handler;
|
||||||
handler.SetStartComponentCallback(std::bind(&EntityFilePreprocessor::onStartComponent, this, std::placeholders::_1));
|
handler.SetStartComponentCallback(std::bind(&EntityFilePreprocessor::onStartComponent, this, std::placeholders::_1, std::placeholders::_2));
|
||||||
m_EntityFile->Parse(&handler);
|
m_EntityFile->Parse(&handler);
|
||||||
|
|
||||||
LOG_DEBUG("___ COMPONENT DEFINITIONS ___");
|
LOG_DEBUG("___ COMPONENT DEFINITIONS ___");
|
||||||
@@ -57,7 +57,7 @@ private:
|
|||||||
std::map<std::string, unsigned int> m_ComponentCounts;
|
std::map<std::string, unsigned int> m_ComponentCounts;
|
||||||
std::map<std::string, ComponentInfo> m_ComponentInfo;
|
std::map<std::string, ComponentInfo> m_ComponentInfo;
|
||||||
|
|
||||||
void onStartComponent(std::string type)
|
void onStartComponent(EntityID entity, std::string type)
|
||||||
{
|
{
|
||||||
//LOG_DEBUG("Component: %s", type.c_str());
|
//LOG_DEBUG("Component: %s", type.c_str());
|
||||||
m_ComponentCounts[type]++;
|
m_ComponentCounts[type]++;
|
||||||
|
|||||||
@@ -20,6 +20,7 @@
|
|||||||
#include "PlayerSystem.h"
|
#include "PlayerSystem.h"
|
||||||
#include "Editor/EditorSystem.h"
|
#include "Editor/EditorSystem.h"
|
||||||
#include "Core/EntityFile.h"
|
#include "Core/EntityFile.h"
|
||||||
|
#include "Core/EntityFileParser.h"
|
||||||
|
|
||||||
// Network
|
// Network
|
||||||
#include <boost/thread.hpp>
|
#include <boost/thread.hpp>
|
||||||
|
|||||||
+5
-3
@@ -49,9 +49,11 @@ Game::Game(int argc, char* argv[])
|
|||||||
m_World = new World();
|
m_World = new World();
|
||||||
std::string mapToLoad = m_Config->Get<std::string>("Debug.LoadMap", "");
|
std::string mapToLoad = m_Config->Get<std::string>("Debug.LoadMap", "");
|
||||||
if (!mapToLoad.empty()) {
|
if (!mapToLoad.empty()) {
|
||||||
//ResourceManager::Load<EntityXMLFile>(mapToLoad)->PopulateWorld(m_World);
|
auto file = ResourceManager::Load<EntityFile>(mapToLoad);
|
||||||
EntityFilePreprocessor fp(mapToLoad);
|
EntityFilePreprocessor fpp(mapToLoad);
|
||||||
fp.RegisterComponents(m_World);
|
fpp.RegisterComponents(m_World);
|
||||||
|
EntityFileParser fp(file);
|
||||||
|
fp.MergeEntities(m_World);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create system pipeline
|
// Create system pipeline
|
||||||
|
|||||||
Reference in New Issue
Block a user