Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor ProductPool serialize #1003

Merged
merged 2 commits into from
Jun 26, 2021
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
51 changes: 25 additions & 26 deletions OPHD/ProductPool.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#include "ProductPool.h"

#include <NAS2D/ParserHelper.h>

#include <algorithm>


Expand Down Expand Up @@ -210,37 +212,34 @@ void ProductPool::verifyCount()
}


void ProductPool::serialize(NAS2D::Xml::XmlElement* element)
NAS2D::Dictionary ProductPool::serialize()
{
element->attribute(constants::SaveGameProductDigger, count(ProductType::PRODUCT_DIGGER));
element->attribute(constants::SaveGameProductDozer, count(ProductType::PRODUCT_DOZER));
element->attribute(constants::SaveGameProductMiner, count(ProductType::PRODUCT_MINER));
element->attribute(constants::SaveGameProductExplorer, count(ProductType::PRODUCT_EXPLORER));
element->attribute(constants::SaveGameProductTruck, count(ProductType::PRODUCT_TRUCK));
element->attribute(constants::SaveGameProductMaintenanceParts, count(ProductType::PRODUCT_MAINTENANCE_PARTS));
element->attribute(constants::SaveGameProductClothing, count(ProductType::PRODUCT_CLOTHING));
element->attribute(constants::SaveGameProductMedicine, count(ProductType::PRODUCT_MEDICINE));
return NAS2D::Dictionary{{
{constants::SaveGameProductDigger, count(ProductType::PRODUCT_DIGGER)},
{constants::SaveGameProductDozer, count(ProductType::PRODUCT_DOZER)},
{constants::SaveGameProductMiner, count(ProductType::PRODUCT_MINER)},
{constants::SaveGameProductExplorer, count(ProductType::PRODUCT_EXPLORER)},
{constants::SaveGameProductTruck, count(ProductType::PRODUCT_TRUCK)},
{constants::SaveGameProductMaintenanceParts, count(ProductType::PRODUCT_MAINTENANCE_PARTS)},
{constants::SaveGameProductClothing, count(ProductType::PRODUCT_CLOTHING)},
{constants::SaveGameProductMedicine, count(ProductType::PRODUCT_MEDICINE)},
}};
}


void ProductPool::deserialize(NAS2D::Xml::XmlElement* element)
void ProductPool::deserialize(const NAS2D::Dictionary& dictionary)
{
/// \todo This should probably trigger an exception.
if (element == nullptr) { return; }
const auto required = std::vector<std::string>{constants::SaveGameProductDigger, constants::SaveGameProductDozer, constants::SaveGameProductMiner, constants::SaveGameProductExplorer, constants::SaveGameProductTruck, constants::SaveGameProductMaintenanceParts, constants::SaveGameProductClothing, constants::SaveGameProductMedicine};
NAS2D::reportMissingOrUnexpected(dictionary.keys(), required, {});

mProducts[ProductType::PRODUCT_DIGGER] = dictionary.get<int>(constants::SaveGameProductDigger);
mProducts[ProductType::PRODUCT_DOZER] = dictionary.get<int>(constants::SaveGameProductDozer);
mProducts[ProductType::PRODUCT_MINER] = dictionary.get<int>(constants::SaveGameProductMiner);
mProducts[ProductType::PRODUCT_EXPLORER] = dictionary.get<int>(constants::SaveGameProductExplorer);
mProducts[ProductType::PRODUCT_TRUCK] = dictionary.get<int>(constants::SaveGameProductTruck);
mProducts[ProductType::PRODUCT_MAINTENANCE_PARTS] = dictionary.get<int>(constants::SaveGameProductMaintenanceParts);
mProducts[ProductType::PRODUCT_CLOTHING] = dictionary.get<int>(constants::SaveGameProductClothing);
mProducts[ProductType::PRODUCT_MEDICINE] = dictionary.get<int>(constants::SaveGameProductMedicine);

const auto* attribute = element->firstAttribute();
while (attribute)
{
if (attribute->name() == constants::SaveGameProductDigger) { attribute->queryIntValue(mProducts[ProductType::PRODUCT_DIGGER]); }
else if (attribute->name() == constants::SaveGameProductDozer) { attribute->queryIntValue(mProducts[ProductType::PRODUCT_DOZER]); }
else if (attribute->name() == constants::SaveGameProductMiner) { attribute->queryIntValue(mProducts[ProductType::PRODUCT_MINER]); }
else if (attribute->name() == constants::SaveGameProductExplorer) { attribute->queryIntValue(mProducts[ProductType::PRODUCT_EXPLORER]); }
else if (attribute->name() == constants::SaveGameProductTruck) { attribute->queryIntValue(mProducts[ProductType::PRODUCT_TRUCK]); }
else if (attribute->name() == constants::SaveGameProductMaintenanceParts) { attribute->queryIntValue(mProducts[ProductType::PRODUCT_MAINTENANCE_PARTS]); }
else if (attribute->name() == constants::SaveGameProductClothing) { attribute->queryIntValue(mProducts[ProductType::PRODUCT_CLOTHING]); }
else if (attribute->name() == constants::SaveGameProductMedicine) { attribute->queryIntValue(mProducts[ProductType::PRODUCT_MEDICINE]); }

attribute = attribute->next();
}
mCurrentStorageCount = computeCurrentStorage(mProducts);
}
6 changes: 3 additions & 3 deletions OPHD/ProductPool.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#include "Common.h"
#include "Constants.h"

#include <NAS2D/Xml/XmlElement.h>
#include <NAS2D/Dictionary.h>

#include <array>

Expand Down Expand Up @@ -37,8 +37,8 @@ class ProductPool

int availableStorage() const;

void serialize(NAS2D::Xml::XmlElement* element);
void deserialize(NAS2D::Xml::XmlElement* element);
NAS2D::Dictionary serialize();
void deserialize(const NAS2D::Dictionary& dictionary);

void verifyCount();

Expand Down
5 changes: 4 additions & 1 deletion OPHD/States/MapViewStateIO.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include <NAS2D/StringUtils.h>
#include <NAS2D/Xml/XmlDocument.h>
#include <NAS2D/Xml/XmlMemoryBuffer.h>
#include <NAS2D/ParserHelper.h>

#include <map>
#include <string>
Expand Down Expand Up @@ -495,7 +496,9 @@ void MapViewState::readStructures(Xml::XmlElement* element)
if (structure.isWarehouse())
{
auto& warehouse = *static_cast<Warehouse*>(&structure);
warehouse.products().deserialize(structureNode->firstChildElement("warehouse_products"));
warehouse.products().deserialize(NAS2D::attributesToDictionary(
*structureNode->firstChildElement("warehouse_products")
));
}

if (structure.isFactory())
Expand Down
7 changes: 4 additions & 3 deletions OPHD/StructureManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,10 @@ namespace

if (structure->isWarehouse())
{
auto* warehouse_products = new NAS2D::Xml::XmlElement("warehouse_products");
static_cast<Warehouse*>(structure)->products().serialize(warehouse_products);
structureElement->linkEndChild(warehouse_products);
structureElement->linkEndChild(NAS2D::dictionaryToAttributes(
"warehouse_products",
static_cast<Warehouse*>(structure)->products().serialize()
));
}

if (structure->isRobotCommand())
Expand Down