mirror of
https://github.com/DarkflameUniverse/DarkflameServer.git
synced 2025-08-05 18:24:12 +00:00
Add more modular saving of config data for items (#1591)
* stubbing for saving item extra data * add declaration to header * modularize loading for all possible extra data * move logic to Item * remove extra map
This commit is contained in:
@@ -27,6 +27,23 @@
|
||||
#include "CDComponentsRegistryTable.h"
|
||||
#include "CDPackageComponentTable.h"
|
||||
|
||||
namespace {
|
||||
const std::map<std::string, std::string> ExtraSettingAbbreviations = {
|
||||
{ "assemblyPartLOTs", "ma" },
|
||||
{ "blueprintID", "b" },
|
||||
{ "userModelID", "ui" },
|
||||
{ "userModelName", "un" },
|
||||
{ "userModelDesc", "ud" },
|
||||
{ "userModelHasBhvr", "ub" },
|
||||
{ "userModelBehaviors", "ubh" },
|
||||
{ "userModelBehaviorSourceID", "ubs" },
|
||||
{ "userModelPhysicsType", "up" },
|
||||
{ "userModelMod", "um" },
|
||||
{ "userModelOpt", "uo" },
|
||||
{ "reforgedLOT", "rl" },
|
||||
};
|
||||
}
|
||||
|
||||
Item::Item(const LWOOBJID id, const LOT lot, Inventory* inventory, const uint32_t slot, const uint32_t count, const bool bound, const std::vector<LDFBaseData*>& config, const LWOOBJID parent, LWOOBJID subKey, eLootSourceType lootSourceType) {
|
||||
if (!Inventory::IsValidItem(lot)) {
|
||||
return;
|
||||
@@ -122,6 +139,10 @@ uint32_t Item::GetSlot() const {
|
||||
return slot;
|
||||
}
|
||||
|
||||
std::vector<LDFBaseData*> Item::GetConfig() const {
|
||||
return config;
|
||||
}
|
||||
|
||||
std::vector<LDFBaseData*>& Item::GetConfig() {
|
||||
return config;
|
||||
}
|
||||
@@ -251,7 +272,7 @@ bool Item::Consume() {
|
||||
|
||||
auto skills = skillsTable->Query([this](const CDObjectSkills entry) {
|
||||
return entry.objectTemplate == static_cast<uint32_t>(lot);
|
||||
});
|
||||
});
|
||||
|
||||
auto success = false;
|
||||
|
||||
@@ -515,3 +536,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 = ExtraSettingAbbreviations.find(key);
|
||||
if (saveKey == ExtraSettingAbbreviations.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 : ExtraSettingAbbreviations) {
|
||||
const auto* data = x->Attribute(pair.second.c_str());
|
||||
if (!data) continue;
|
||||
|
||||
const auto value = pair.first + "=" + data;
|
||||
config.push_back(LDFBaseData::DataFromString(value));
|
||||
}
|
||||
}
|
||||
|
@@ -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
|
||||
@@ -116,6 +120,12 @@ public:
|
||||
*/
|
||||
std::vector<LDFBaseData*>& GetConfig();
|
||||
|
||||
/**
|
||||
* Returns current config info for this item, e.g. for rockets
|
||||
* @return current config info for this item
|
||||
*/
|
||||
std::vector<LDFBaseData*> GetConfig() const;
|
||||
|
||||
/**
|
||||
* Returns the database info for this item
|
||||
* @return the database info for this item
|
||||
@@ -214,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
|
||||
|
Reference in New Issue
Block a user