Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
88 changes: 81 additions & 7 deletions src/fx/matrixfx/bitmapshapes/ShapeDefinitions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,14 +73,27 @@ ShapeDefinitions* ShapeDefinitions::GetShapeDefinitionsFromShapeDefinitionsXmlFi
try
{
xml = FileReader::ReadFileToString(fileName);
if (xml.empty())
{
Log::Warning(StringExtensions::Build("ShapeDefinitions file {0} is empty.", fileName));
return nullptr;
}
}
catch (const std::exception& e)
{
Log::Exception(StringExtensions::Build("Could not load ShapeDefinitions from {0}.", fileName));
Log::Exception(StringExtensions::Build("Could not load ShapeDefinitions from {0}. Error: {1}", fileName, e.what()));
throw std::runtime_error(StringExtensions::Build("Could not read ShapeDefinitions file {0}.", fileName));
}

return GetShapeDefinitionsFromShapeDefinitionsXml(xml);
try
{
return GetShapeDefinitionsFromShapeDefinitionsXml(xml);
}
catch (const std::exception& e)
{
Log::Exception(StringExtensions::Build("Could not parse ShapeDefinitions XML from {0}. Error: {1}", fileName, e.what()));
return nullptr;
}
}

bool ShapeDefinitions::TestShapeDefinitionsShapeDefinitionsXmlFile(const std::string& fileName)
Expand All @@ -105,13 +118,14 @@ ShapeDefinitions* ShapeDefinitions::GetShapeDefinitionsFromShapeDefinitionsXml(c
tinyxml2::XMLError result = doc.Parse(shapeDefinitionsXml.c_str());
if (result != tinyxml2::XML_SUCCESS)
{
Log::Exception("Could not deserialize the ShapeDefinitions from XML data.");
Log::Exception(StringExtensions::Build("Could not deserialize the ShapeDefinitions from XML data. Error: {0}", doc.ErrorStr() ? doc.ErrorStr() : "unknown error"));
throw std::runtime_error("Could not deserialize the ShapeDefinitions from XML data.");
}

tinyxml2::XMLElement* root = doc.FirstChildElement("ShapeDefinitions");
if (!root)
{
Log::Exception("Could not find ShapeDefinitions root element in XML");
throw std::runtime_error("Could not find ShapeDefinitions root element.");
}

Expand All @@ -120,10 +134,70 @@ ShapeDefinitions* ShapeDefinitions::GetShapeDefinitionsFromShapeDefinitionsXml(c
tinyxml2::XMLElement* shapesElement = root->FirstChildElement("Shapes");
if (shapesElement)
{
tinyxml2::XMLPrinter printer;
shapesElement->Accept(&printer);
std::string shapesXml = printer.CStr();
shapeDefinitions->m_shapes.ReadXml(shapesXml);
try
{
for (tinyxml2::XMLElement* shapeElement = shapesElement->FirstChildElement(); shapeElement != nullptr; shapeElement = shapeElement->NextSiblingElement())
{
std::string elementName = shapeElement->Name() ? shapeElement->Name() : "";

if (elementName == "Shape")
{
Shape* shape = new Shape();
bool shapeValid = true;

try
{
tinyxml2::XMLElement* nameElement = shapeElement->FirstChildElement("Name");
if (nameElement && nameElement->GetText())
shape->SetName(nameElement->GetText());

tinyxml2::XMLElement* frameElement = shapeElement->FirstChildElement("BitmapFrameNumber");
if (frameElement)
shape->SetBitmapFrameNumber(frameElement->IntText(0));

tinyxml2::XMLElement* topElement = shapeElement->FirstChildElement("BitmapTop");
if (topElement)
shape->SetBitmapTop(topElement->IntText(0));

tinyxml2::XMLElement* leftElement = shapeElement->FirstChildElement("BitmapLeft");
if (leftElement)
shape->SetBitmapLeft(leftElement->IntText(0));

tinyxml2::XMLElement* widthElement = shapeElement->FirstChildElement("BitmapWidth");
if (widthElement)
shape->SetBitmapWidth(widthElement->IntText(0));

tinyxml2::XMLElement* heightElement = shapeElement->FirstChildElement("BitmapHeight");
if (heightElement)
shape->SetBitmapHeight(heightElement->IntText(0));

if (!shape->GetName().empty() && !shapeDefinitions->m_shapes.Contains(shape->GetName()))
{
shapeDefinitions->m_shapes.Add(shape);
}
else
{
shapeValid = false;
}
}
catch (const std::exception& e)
{
Log::Warning(StringExtensions::Build("Error processing shape: {0}", e.what()));
shapeValid = false;
}

if (!shapeValid)
{
delete shape;
}
}
}
}
catch (const std::exception& e)
{
Log::Exception(StringExtensions::Build("Exception during shape processing: {0}", e.what()));
return shapeDefinitions;
}
}

return shapeDefinitions;
Expand Down
84 changes: 59 additions & 25 deletions src/fx/matrixfx/bitmapshapes/ShapeList.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,26 @@ void ShapeList::WriteXml(std::string& writer)

void ShapeList::ReadXml(const std::string& reader)
{
if (reader.empty())
{
Log::Warning("ShapeList::ReadXml - Empty XML string provided");
return;
}

tinyxml2::XMLDocument doc;
tinyxml2::XMLError result = doc.Parse(reader.c_str());
if (result != tinyxml2::XML_SUCCESS)
{
Log::Warning(StringExtensions::Build("ShapeList::ReadXml - XML parse error: {0}", doc.ErrorStr() ? doc.ErrorStr() : "unknown error"));
return;
}

tinyxml2::XMLElement* root = doc.FirstChildElement();
if (!root)
{
Log::Warning("ShapeList::ReadXml - No root element found");
return;
}

for (tinyxml2::XMLElement* element = root->FirstChildElement(); element != nullptr; element = element->NextSiblingElement())
{
Expand All @@ -47,34 +59,56 @@ void ShapeList::ReadXml(const std::string& reader)
if (t == "Shape")
{
Shape* shape = new Shape();
bool shapeValid = true;

tinyxml2::XMLElement* nameElement = element->FirstChildElement("Name");
if (nameElement)
shape->SetName(nameElement->GetText() ? nameElement->GetText() : "");

tinyxml2::XMLElement* frameElement = element->FirstChildElement("BitmapFrameNumber");
if (frameElement)
shape->SetBitmapFrameNumber(frameElement->IntText(0));

tinyxml2::XMLElement* topElement = element->FirstChildElement("BitmapTop");
if (topElement)
shape->SetBitmapTop(topElement->IntText(0));

tinyxml2::XMLElement* leftElement = element->FirstChildElement("BitmapLeft");
if (leftElement)
shape->SetBitmapLeft(leftElement->IntText(0));

tinyxml2::XMLElement* widthElement = element->FirstChildElement("BitmapWidth");
if (widthElement)
shape->SetBitmapWidth(widthElement->IntText(0));

tinyxml2::XMLElement* heightElement = element->FirstChildElement("BitmapHeight");
if (heightElement)
shape->SetBitmapHeight(heightElement->IntText(0));
try
{
tinyxml2::XMLElement* nameElement = element->FirstChildElement("Name");
if (nameElement)
shape->SetName(nameElement->GetText() ? nameElement->GetText() : "");

tinyxml2::XMLElement* frameElement = element->FirstChildElement("BitmapFrameNumber");
if (frameElement)
shape->SetBitmapFrameNumber(frameElement->IntText(0));

tinyxml2::XMLElement* topElement = element->FirstChildElement("BitmapTop");
if (topElement)
shape->SetBitmapTop(topElement->IntText(0));

tinyxml2::XMLElement* leftElement = element->FirstChildElement("BitmapLeft");
if (leftElement)
shape->SetBitmapLeft(leftElement->IntText(0));

tinyxml2::XMLElement* widthElement = element->FirstChildElement("BitmapWidth");
if (widthElement)
shape->SetBitmapWidth(widthElement->IntText(0));

tinyxml2::XMLElement* heightElement = element->FirstChildElement("BitmapHeight");
if (heightElement)
shape->SetBitmapHeight(heightElement->IntText(0));

if (shape->GetName().empty())
{
shapeValid = false;
}
else if (!this->Contains(shape->GetName()))
{
this->Add(shape);
}
else
{
shapeValid = false;
}
}
catch (const std::exception& e)
{
Log::Warning(StringExtensions::Build("Error processing shape: {0}", e.what()));
shapeValid = false;
}

if (!this->Contains(shape->GetName()))
if (!shapeValid)
{
this->Add(shape);
delete shape;
}
}
else
Expand Down