From a6445c65aa005bba03645ea50e8206fca222ce3a Mon Sep 17 00:00:00 2001 From: sippeangelo Date: Tue, 19 Jan 2016 12:15:20 +0100 Subject: [PATCH] Fixed EntityParser to handle string enums properly. --- .../Schema/Entities/EditorWidgetRotate.xml | 12 ++++-- .../Schema/Entities/EditorWidgetScale.xml | 12 ++++-- .../Schema/Entities/EditorWidgetTranslate.xml | 24 +++++++++--- src/Engine/Core/EntityFile.cpp | 37 ++++++++++--------- 4 files changed, 56 insertions(+), 29 deletions(-) diff --git a/resources/Schema/Entities/EditorWidgetRotate.xml b/resources/Schema/Entities/EditorWidgetRotate.xml index fc41b596..55dd9af4 100644 --- a/resources/Schema/Entities/EditorWidgetRotate.xml +++ b/resources/Schema/Entities/EditorWidgetRotate.xml @@ -9,7 +9,9 @@ - 2 + + + @@ -22,7 +24,9 @@ - 2 + + + @@ -35,7 +39,9 @@ - 2 + + + diff --git a/resources/Schema/Entities/EditorWidgetScale.xml b/resources/Schema/Entities/EditorWidgetScale.xml index f4c44d66..b317b1cc 100644 --- a/resources/Schema/Entities/EditorWidgetScale.xml +++ b/resources/Schema/Entities/EditorWidgetScale.xml @@ -12,7 +12,9 @@ - 3 + + + @@ -25,7 +27,9 @@ - 3 + + + @@ -38,7 +42,9 @@ - 3 + + + diff --git a/resources/Schema/Entities/EditorWidgetTranslate.xml b/resources/Schema/Entities/EditorWidgetTranslate.xml index bc43538d..56943adc 100644 --- a/resources/Schema/Entities/EditorWidgetTranslate.xml +++ b/resources/Schema/Entities/EditorWidgetTranslate.xml @@ -12,7 +12,9 @@ - 1 + + + @@ -25,7 +27,9 @@ - 1 + + + @@ -38,7 +42,9 @@ - 1 + + + @@ -51,7 +57,9 @@ - 1 + + + @@ -64,7 +72,9 @@ - 1 + + + @@ -77,7 +87,9 @@ - 1 + + + diff --git a/src/Engine/Core/EntityFile.cpp b/src/Engine/Core/EntityFile.cpp index c1125e10..d5fb4c12 100644 --- a/src/Engine/Core/EntityFile.cpp +++ b/src/Engine/Core/EntityFile.cpp @@ -86,23 +86,26 @@ void EntityFile::WriteAttributeData(char* outData, const ComponentInfo::Field_t& void EntityFile::WriteValueData(char* outData, const ComponentInfo::Field_t& field, const char* valueData) { - if (field.Type == "int" || field.Type == "enum") { - int value = boost::lexical_cast(valueData); - memcpy(outData, reinterpret_cast(&value), field.Stride); - } else if (field.Type == "float") { - float value = boost::lexical_cast(valueData); - memcpy(outData, reinterpret_cast(&value), field.Stride); - } else if (field.Type == "double") { - double value = boost::lexical_cast(valueData); - memcpy(outData, reinterpret_cast(&value), field.Stride); - } else if (field.Type == "bool") { - bool value = (valueData[0] == 't'); // Lazy bool evaluation - memcpy(outData, reinterpret_cast(&value), field.Stride); - } else if (field.Type == "string") { - new (outData) std::string(valueData); - } else { - LOG_WARNING("Unknown value data type: %s", field.Type.c_str()); - } + // Catch and ignore casting errors so whitespace around string enums won't mess anything up + try { + if (field.Type == "int" || field.Type == "enum") { + int value = boost::lexical_cast(valueData); + memcpy(outData, reinterpret_cast(&value), field.Stride); + } else if (field.Type == "float") { + float value = boost::lexical_cast(valueData); + memcpy(outData, reinterpret_cast(&value), field.Stride); + } else if (field.Type == "double") { + double value = boost::lexical_cast(valueData); + memcpy(outData, reinterpret_cast(&value), field.Stride); + } else if (field.Type == "bool") { + bool value = (valueData[0] == 't'); // Lazy bool evaluation + memcpy(outData, reinterpret_cast(&value), field.Stride); + } else if (field.Type == "string") { + new (outData) std::string(valueData); + } else { + LOG_WARNING("Unknown value data type: %s", field.Type.c_str()); + } + } catch (const boost::bad_lexical_cast&) { } } EntityFileSAXHandler::EntityFileSAXHandler(const EntityFileHandler* handler, xercesc::SAX2XMLReader* reader) : m_Handler(handler)