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

Item custom attributes #1997

Merged
merged 21 commits into from Dec 30, 2017
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/enums.h
Expand Up @@ -89,6 +89,8 @@ enum itemAttrTypes : uint32_t {
ITEM_ATTRIBUTE_CHARGES = 1 << 20,
ITEM_ATTRIBUTE_FLUIDTYPE = 1 << 21,
ITEM_ATTRIBUTE_DOORID = 1 << 22,

ITEM_ATTRIBUTE_CUSTOM = 1U << 31
};

enum VipStatus_t : uint8_t {
Expand Down
76 changes: 76 additions & 0 deletions src/item.cpp
Expand Up @@ -617,6 +617,30 @@ Attr_ReadValue Item::readAttr(AttrTypes_t attr, PropStream& propStream)
return ATTR_READ_ERROR;
}

case ATTR_CUSTOM_ATTRIBUTES: {
uint64_t size;
if (!propStream.read<uint64_t>(size)) {
return ATTR_READ_ERROR;
}

for (uint64_t i = 0; i < size; i++) {
// Unserialize key type and value
std::string key;
if (!propStream.readString(key)) {
return ATTR_READ_ERROR;
};

// Unserialize value type and value
ItemAttributes::CustomAttribute val;
if (!val.unserialize(propStream)) {
return ATTR_READ_ERROR;
}

setCustomAttribute(key, val);
}
break;
}

default:
return ATTR_READ_ERROR;
}
Expand Down Expand Up @@ -749,6 +773,19 @@ void Item::serializeAttr(PropWriteStream& propWriteStream) const
propWriteStream.write<uint8_t>(ATTR_SHOOTRANGE);
propWriteStream.write<uint8_t>(getIntAttr(ITEM_ATTRIBUTE_SHOOTRANGE));
}

if (hasAttribute(ITEM_ATTRIBUTE_CUSTOM)) {
const ItemAttributes::CustomAttributeMap* customAttrMap = attributes->getCustomAttributeMap();
propWriteStream.write<uint8_t>(ATTR_CUSTOM_ATTRIBUTES);
propWriteStream.write<uint64_t>(static_cast<uint64_t>(customAttrMap->size()));
for (const auto &entry : *customAttrMap) {
// Serializing key type and value
propWriteStream.writeString(entry.first);

// Serializing value type and value
entry.second.serialize(propWriteStream);
}
}
}

bool Item::hasProperty(ITEMPROPERTY prop) const
Expand Down Expand Up @@ -1538,6 +1575,9 @@ LightInfo Item::getLightInfo() const
}

std::string ItemAttributes::emptyString;
int64_t ItemAttributes::emptyInt;
double ItemAttributes::emptyDouble;
bool ItemAttributes::emptyBool;

const std::string& ItemAttributes::getStrAttr(itemAttrTypes type) const
{
Expand Down Expand Up @@ -1675,3 +1715,39 @@ bool Item::hasMarketAttributes() const
}
return true;
}

template<>
const std::string& ItemAttributes::CustomAttribute::get<std::string>() {
if (value.type() == typeid(std::string)) {
return boost::get<std::string>(value);
}

return emptyString;
}

template<>
const int64_t& ItemAttributes::CustomAttribute::get<int64_t>() {
if (value.type() == typeid(int64_t)) {
return boost::get<int64_t>(value);
}

return emptyInt;
}

template<>
const double& ItemAttributes::CustomAttribute::get<double>() {
if (value.type() == typeid(double)) {
return boost::get<double>(value);
}

return emptyDouble;
}

template<>
const bool& ItemAttributes::CustomAttribute::get<bool>() {
if (value.type() == typeid(bool)) {
return boost::get<bool>(value);
}

return emptyBool;
}