From 693a2fef35dfc00445606fa64402e5609511099d Mon Sep 17 00:00:00 2001 From: David Markowitz Date: Mon, 20 May 2024 20:49:42 -0700 Subject: [PATCH] move logic to Item --- dGame/dComponents/InventoryComponent.cpp | 68 +----------------------- dGame/dComponents/InventoryComponent.h | 4 -- dGame/dInventory/Item.cpp | 66 ++++++++++++++++++++++- dGame/dInventory/Item.h | 8 +++ 4 files changed, 75 insertions(+), 71 deletions(-) diff --git a/dGame/dComponents/InventoryComponent.cpp b/dGame/dComponents/InventoryComponent.cpp index 229085c2..acb27796 100644 --- a/dGame/dComponents/InventoryComponent.cpp +++ b/dGame/dComponents/InventoryComponent.cpp @@ -38,38 +38,6 @@ #include "CDObjectSkillsTable.h" #include "CDSkillBehaviorTable.h" -namespace { - const std::map ExtraSettingSaveAbbreviations = { - { "assemblyPartLOTs", "ma" }, - { "blueprintID", "b" }, - { "userModelID", "ui" }, - { "userModelName", "un" }, - { "userModelDesc", "ud" }, - { "userModelHasBhvr", "ub" }, - { "userModelBehaviors", "ubh" }, - { "userModelBehaviorSourceID", "ubs" }, - { "userModelPhysicsType", "up" }, - { "userModelMod", "um" }, - { "userModelOpt", "uo" }, - { "reforgedLOT", "rl" }, - }; - - const std::map ExtraSettingLoadAbbreviations = { - { "ma", "assemblyPartLOTs" }, - { "b", "blueprintID" }, - { "ui", "userModelID" }, - { "un", "userModelName" }, - { "ud", "userModelDesc" }, - { "ub", "userModelHasBhvr" }, - { "ubh", "userModelBehaviors" }, - { "ubs", "userModelBehaviorSourceID" }, - { "up", "userModelPhysicsType" }, - { "um", "userModelMod" }, - { "uo", "userModelOpt" }, - { "rl", "reforgedLOT" }, - }; -} - InventoryComponent::InventoryComponent(Entity* parent) : Component(parent) { this->m_Dirty = true; this->m_Equipped = {}; @@ -592,7 +560,7 @@ void InventoryComponent::LoadXml(const tinyxml2::XMLDocument& document) { auto* item = new Item(id, lot, inventory, slot, count, bound, {}, parent, subKey); - LoadItemConfigXml(*itemElement, item); + item->LoadConfigXml(*itemElement); if (equipped) { const auto info = Inventory::FindItemComponent(lot); @@ -698,7 +666,7 @@ void InventoryComponent::UpdateXml(tinyxml2::XMLDocument& document) { itemElement->SetAttribute("parent", item->GetParent()); // End custom xml - SaveItemConfigXml(*itemElement, item); + item->SaveConfigXml(*itemElement); bagElement->LinkEndChild(itemElement); } @@ -1635,35 +1603,3 @@ bool InventoryComponent::SetSkill(BehaviorSlot slot, uint32_t skillId) { m_Skills.insert_or_assign(slot, skillId); return true; } - -void InventoryComponent::SaveItemConfigXml(tinyxml2::XMLElement& i, const Item* const item) const { - tinyxml2::XMLElement* x = nullptr; - - for (const auto* config : item->GetConfig()) { - const auto& key = GeneralUtils::UTF16ToWTF8(config->GetKey()); - const auto saveKey = ExtraSettingSaveAbbreviations.find(key); - if (saveKey == ExtraSettingSaveAbbreviations.end()) { - continue; - } - - if (!x) { - x = i.InsertNewChildElement("x"); - } - - const auto dataToSave = config->GetString(false); - x->SetAttribute(saveKey->second.c_str(), dataToSave.c_str()); - } -} - -void InventoryComponent::LoadItemConfigXml(const tinyxml2::XMLElement& i, Item* const item) { - const auto* x = i.FirstChildElement("x"); - if (!x) return; - - for (const auto& pair : ExtraSettingLoadAbbreviations) { - const auto* data = x->Attribute(pair.first.c_str()); - if (!data) continue; - - const auto value = pair.second + "=" + data; - item->GetConfig().push_back(LDFBaseData::DataFromString(value)); - } -} diff --git a/dGame/dComponents/InventoryComponent.h b/dGame/dComponents/InventoryComponent.h index c4c92922..a1eb14d1 100644 --- a/dGame/dComponents/InventoryComponent.h +++ b/dGame/dComponents/InventoryComponent.h @@ -477,10 +477,6 @@ private: * @param document the xml doc to load from */ void UpdatePetXml(tinyxml2::XMLDocument& document); - - void SaveItemConfigXml(tinyxml2::XMLElement& i, const Item* const item) const; - - void LoadItemConfigXml(const tinyxml2::XMLElement& i, Item* const item); }; #endif diff --git a/dGame/dInventory/Item.cpp b/dGame/dInventory/Item.cpp index b486791a..1f831486 100644 --- a/dGame/dInventory/Item.cpp +++ b/dGame/dInventory/Item.cpp @@ -27,6 +27,38 @@ #include "CDComponentsRegistryTable.h" #include "CDPackageComponentTable.h" +namespace { + const std::map ExtraSettingSaveAbbreviations = { + { "assemblyPartLOTs", "ma" }, + { "blueprintID", "b" }, + { "userModelID", "ui" }, + { "userModelName", "un" }, + { "userModelDesc", "ud" }, + { "userModelHasBhvr", "ub" }, + { "userModelBehaviors", "ubh" }, + { "userModelBehaviorSourceID", "ubs" }, + { "userModelPhysicsType", "up" }, + { "userModelMod", "um" }, + { "userModelOpt", "uo" }, + { "reforgedLOT", "rl" }, + }; + + const std::map ExtraSettingLoadAbbreviations = { + { "ma", "assemblyPartLOTs" }, + { "b", "blueprintID" }, + { "ui", "userModelID" }, + { "un", "userModelName" }, + { "ud", "userModelDesc" }, + { "ub", "userModelHasBhvr" }, + { "ubh", "userModelBehaviors" }, + { "ubs", "userModelBehaviorSourceID" }, + { "up", "userModelPhysicsType" }, + { "um", "userModelMod" }, + { "uo", "userModelOpt" }, + { "rl", "reforgedLOT" }, + }; +} + Item::Item(const LWOOBJID id, const LOT lot, Inventory* inventory, const uint32_t slot, const uint32_t count, const bool bound, const std::vector& config, const LWOOBJID parent, LWOOBJID subKey, eLootSourceType lootSourceType) { if (!Inventory::IsValidItem(lot)) { return; @@ -255,7 +287,7 @@ bool Item::Consume() { auto skills = skillsTable->Query([this](const CDObjectSkills entry) { return entry.objectTemplate == static_cast(lot); - }); + }); auto success = false; @@ -519,3 +551,35 @@ Item::~Item() { config.clear(); } + +void Item::SaveConfigXml(tinyxml2::XMLElement& i) const { + tinyxml2::XMLElement* x = nullptr; + + for (const auto* config : this->config) { + const auto& key = GeneralUtils::UTF16ToWTF8(config->GetKey()); + const auto saveKey = ExtraSettingSaveAbbreviations.find(key); + if (saveKey == ExtraSettingSaveAbbreviations.end()) { + continue; + } + + if (!x) { + x = i.InsertNewChildElement("x"); + } + + const auto dataToSave = config->GetString(false); + x->SetAttribute(saveKey->second.c_str(), dataToSave.c_str()); + } +} + +void Item::LoadConfigXml(const tinyxml2::XMLElement& i) { + const auto* x = i.FirstChildElement("x"); + if (!x) return; + + for (const auto& pair : ExtraSettingLoadAbbreviations) { + const auto* data = x->Attribute(pair.first.c_str()); + if (!data) continue; + + const auto value = pair.second + "=" + data; + config.push_back(LDFBaseData::DataFromString(value)); + } +} diff --git a/dGame/dInventory/Item.h b/dGame/dInventory/Item.h index 23b45782..72ff264c 100644 --- a/dGame/dInventory/Item.h +++ b/dGame/dInventory/Item.h @@ -9,6 +9,10 @@ #include "eInventoryType.h" #include "eLootSourceType.h" +namespace tinyxml2 { + class XMLElement; +}; + /** * An item that can be stored in an inventory and optionally consumed or equipped * TODO: ideally this should be a component @@ -220,6 +224,10 @@ public: */ void RemoveFromInventory(); + void SaveConfigXml(tinyxml2::XMLElement& i) const; + + void LoadConfigXml(const tinyxml2::XMLElement& i); + private: /** * The object ID of this item