clientobject: add null checks
[waspsaliva.git] / src / itemstackmetadata.cpp
blob4aa1a09030541e42ceb6800ac242f29799c16661
1 /*
2 Minetest
3 Copyright (C) 2017-8 rubenwardy <rw@rubenwardy.com>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation; either version 2.1 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 #include "itemstackmetadata.h"
22 #include "util/serialize.h"
23 #include "util/strfnd.h"
25 #define DESERIALIZE_START '\x01'
26 #define DESERIALIZE_KV_DELIM '\x02'
27 #define DESERIALIZE_PAIR_DELIM '\x03'
28 #define DESERIALIZE_START_STR "\x01"
29 #define DESERIALIZE_KV_DELIM_STR "\x02"
30 #define DESERIALIZE_PAIR_DELIM_STR "\x03"
32 #define TOOLCAP_KEY "tool_capabilities"
34 void ItemStackMetadata::clear()
36 Metadata::clear();
37 updateToolCapabilities();
40 bool ItemStackMetadata::setString(const std::string &name, const std::string &var)
42 bool result = Metadata::setString(name, var);
43 if (name == TOOLCAP_KEY)
44 updateToolCapabilities();
45 return result;
48 void ItemStackMetadata::serialize(std::ostream &os) const
50 std::ostringstream os2;
51 os2 << DESERIALIZE_START;
52 for (const auto &stringvar : m_stringvars) {
53 if (!stringvar.first.empty() || !stringvar.second.empty())
54 os2 << stringvar.first << DESERIALIZE_KV_DELIM
55 << stringvar.second << DESERIALIZE_PAIR_DELIM;
57 os << serializeJsonStringIfNeeded(os2.str());
60 void ItemStackMetadata::deSerialize(std::istream &is)
62 std::string in = deSerializeJsonStringIfNeeded(is);
64 m_stringvars.clear();
66 if (!in.empty()) {
67 if (in[0] == DESERIALIZE_START) {
68 Strfnd fnd(in);
69 fnd.to(1);
70 while (!fnd.at_end()) {
71 std::string name = fnd.next(DESERIALIZE_KV_DELIM_STR);
72 std::string var = fnd.next(DESERIALIZE_PAIR_DELIM_STR);
73 m_stringvars[name] = var;
75 } else {
76 // BACKWARDS COMPATIBILITY
77 m_stringvars[""] = in;
80 updateToolCapabilities();
83 void ItemStackMetadata::updateToolCapabilities()
85 if (contains(TOOLCAP_KEY)) {
86 toolcaps_overridden = true;
87 toolcaps_override = ToolCapabilities();
88 std::istringstream is(getString(TOOLCAP_KEY));
89 toolcaps_override.deserializeJson(is);
90 } else {
91 toolcaps_overridden = false;
95 void ItemStackMetadata::setToolCapabilities(const ToolCapabilities &caps)
97 std::ostringstream os;
98 caps.serializeJson(os);
99 setString(TOOLCAP_KEY, os.str());
102 void ItemStackMetadata::clearToolCapabilities()
104 setString(TOOLCAP_KEY, "");