2024-10-28 15:57:39 +01:00
|
|
|
// Luanti
|
|
|
|
// SPDX-License-Identifier: LGPL-2.1-or-later
|
|
|
|
// Copyright (C) 2017-8 rubenwardy <rw@rubenwardy.com>
|
2018-04-06 10:52:29 +02:00
|
|
|
|
|
|
|
|
2017-01-31 20:49:01 +01:00
|
|
|
#include "itemstackmetadata.h"
|
|
|
|
#include "util/serialize.h"
|
|
|
|
#include "util/strfnd.h"
|
2024-02-02 21:21:00 +01:00
|
|
|
|
2021-01-28 17:32:37 +01:00
|
|
|
#include <algorithm>
|
2024-02-02 21:21:00 +01:00
|
|
|
#include <optional>
|
2017-01-31 20:49:01 +01:00
|
|
|
|
|
|
|
#define DESERIALIZE_START '\x01'
|
|
|
|
#define DESERIALIZE_KV_DELIM '\x02'
|
|
|
|
#define DESERIALIZE_PAIR_DELIM '\x03'
|
|
|
|
#define DESERIALIZE_START_STR "\x01"
|
|
|
|
#define DESERIALIZE_KV_DELIM_STR "\x02"
|
|
|
|
#define DESERIALIZE_PAIR_DELIM_STR "\x03"
|
|
|
|
|
2017-04-19 01:30:27 +02:00
|
|
|
#define TOOLCAP_KEY "tool_capabilities"
|
2024-02-02 21:21:00 +01:00
|
|
|
#define WEAR_BAR_KEY "wear_color"
|
2017-04-19 01:30:27 +02:00
|
|
|
|
|
|
|
void ItemStackMetadata::clear()
|
|
|
|
{
|
2022-09-26 23:03:43 +02:00
|
|
|
SimpleMetadata::clear();
|
2017-04-19 01:30:27 +02:00
|
|
|
updateToolCapabilities();
|
2024-02-02 21:21:00 +01:00
|
|
|
updateWearBarParams();
|
2017-04-19 01:30:27 +02:00
|
|
|
}
|
|
|
|
|
2021-01-28 17:32:37 +01:00
|
|
|
static void sanitize_string(std::string &str)
|
|
|
|
{
|
|
|
|
str.erase(std::remove(str.begin(), str.end(), DESERIALIZE_START), str.end());
|
|
|
|
str.erase(std::remove(str.begin(), str.end(), DESERIALIZE_KV_DELIM), str.end());
|
|
|
|
str.erase(std::remove(str.begin(), str.end(), DESERIALIZE_PAIR_DELIM), str.end());
|
|
|
|
}
|
|
|
|
|
2024-02-17 15:35:33 +01:00
|
|
|
bool ItemStackMetadata::setString(const std::string &name, std::string_view var)
|
2017-04-19 01:30:27 +02:00
|
|
|
{
|
2021-01-28 17:32:37 +01:00
|
|
|
std::string clean_name = name;
|
2024-02-17 15:35:33 +01:00
|
|
|
std::string clean_var(var);
|
2021-01-28 17:32:37 +01:00
|
|
|
sanitize_string(clean_name);
|
|
|
|
sanitize_string(clean_var);
|
|
|
|
|
2022-09-26 23:03:43 +02:00
|
|
|
bool result = SimpleMetadata::setString(clean_name, clean_var);
|
2021-01-28 17:32:37 +01:00
|
|
|
if (clean_name == TOOLCAP_KEY)
|
2017-04-19 01:30:27 +02:00
|
|
|
updateToolCapabilities();
|
2024-02-02 21:21:00 +01:00
|
|
|
else if (clean_name == WEAR_BAR_KEY)
|
|
|
|
updateWearBarParams();
|
2017-04-19 01:30:27 +02:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2017-01-31 20:49:01 +01:00
|
|
|
void ItemStackMetadata::serialize(std::ostream &os) const
|
|
|
|
{
|
2021-09-11 21:06:57 +02:00
|
|
|
std::ostringstream os2(std::ios_base::binary);
|
2017-01-31 20:49:01 +01:00
|
|
|
os2 << DESERIALIZE_START;
|
2017-08-18 08:07:59 +02:00
|
|
|
for (const auto &stringvar : m_stringvars) {
|
2017-08-19 20:43:02 +02:00
|
|
|
if (!stringvar.first.empty() || !stringvar.second.empty())
|
|
|
|
os2 << stringvar.first << DESERIALIZE_KV_DELIM
|
|
|
|
<< stringvar.second << DESERIALIZE_PAIR_DELIM;
|
2017-01-31 20:49:01 +01:00
|
|
|
}
|
|
|
|
os << serializeJsonStringIfNeeded(os2.str());
|
|
|
|
}
|
|
|
|
|
|
|
|
void ItemStackMetadata::deSerialize(std::istream &is)
|
|
|
|
{
|
|
|
|
std::string in = deSerializeJsonStringIfNeeded(is);
|
|
|
|
|
|
|
|
m_stringvars.clear();
|
|
|
|
|
2017-06-27 12:34:11 +02:00
|
|
|
if (!in.empty()) {
|
|
|
|
if (in[0] == DESERIALIZE_START) {
|
|
|
|
Strfnd fnd(in);
|
|
|
|
fnd.to(1);
|
|
|
|
while (!fnd.at_end()) {
|
|
|
|
std::string name = fnd.next(DESERIALIZE_KV_DELIM_STR);
|
|
|
|
std::string var = fnd.next(DESERIALIZE_PAIR_DELIM_STR);
|
2024-09-25 23:24:30 +02:00
|
|
|
m_stringvars[name] = std::move(var);
|
2017-06-27 12:34:11 +02:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// BACKWARDS COMPATIBILITY
|
2024-09-25 23:24:30 +02:00
|
|
|
m_stringvars[""] = std::move(in);
|
2017-01-31 20:49:01 +01:00
|
|
|
}
|
|
|
|
}
|
2017-04-19 01:30:27 +02:00
|
|
|
updateToolCapabilities();
|
2024-02-02 21:21:00 +01:00
|
|
|
updateWearBarParams();
|
2017-04-19 01:30:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void ItemStackMetadata::updateToolCapabilities()
|
|
|
|
{
|
|
|
|
if (contains(TOOLCAP_KEY)) {
|
|
|
|
toolcaps_overridden = true;
|
|
|
|
toolcaps_override = ToolCapabilities();
|
|
|
|
std::istringstream is(getString(TOOLCAP_KEY));
|
|
|
|
toolcaps_override.deserializeJson(is);
|
|
|
|
} else {
|
|
|
|
toolcaps_overridden = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ItemStackMetadata::setToolCapabilities(const ToolCapabilities &caps)
|
|
|
|
{
|
|
|
|
std::ostringstream os;
|
|
|
|
caps.serializeJson(os);
|
|
|
|
setString(TOOLCAP_KEY, os.str());
|
|
|
|
}
|
|
|
|
|
|
|
|
void ItemStackMetadata::clearToolCapabilities()
|
|
|
|
{
|
|
|
|
setString(TOOLCAP_KEY, "");
|
2017-01-31 20:49:01 +01:00
|
|
|
}
|
2024-02-02 21:21:00 +01:00
|
|
|
|
|
|
|
void ItemStackMetadata::updateWearBarParams()
|
|
|
|
{
|
|
|
|
if (contains(WEAR_BAR_KEY)) {
|
|
|
|
std::istringstream is(getString(WEAR_BAR_KEY));
|
|
|
|
wear_bar_override = WearBarParams::deserializeJson(is);
|
|
|
|
} else {
|
|
|
|
wear_bar_override.reset();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ItemStackMetadata::setWearBarParams(const WearBarParams ¶ms)
|
|
|
|
{
|
|
|
|
std::ostringstream os;
|
|
|
|
params.serializeJson(os);
|
|
|
|
setString(WEAR_BAR_KEY, os.str());
|
|
|
|
}
|
|
|
|
|
|
|
|
void ItemStackMetadata::clearWearBarParams()
|
|
|
|
{
|
|
|
|
setString(WEAR_BAR_KEY, "");
|
|
|
|
}
|