]> git.lizzy.rs Git - minetest.git/blob - src/itemstackmetadata.cpp
8x block meshes (#13133)
[minetest.git] / src / itemstackmetadata.cpp
1 /*
2 Minetest
3 Copyright (C) 2017-8 rubenwardy <rw@rubenwardy.com>
4
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.
9
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.
14
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.
18 */
19
20
21 #include "itemstackmetadata.h"
22 #include "util/serialize.h"
23 #include "util/strfnd.h"
24 #include <algorithm>
25
26 #define DESERIALIZE_START '\x01'
27 #define DESERIALIZE_KV_DELIM '\x02'
28 #define DESERIALIZE_PAIR_DELIM '\x03'
29 #define DESERIALIZE_START_STR "\x01"
30 #define DESERIALIZE_KV_DELIM_STR "\x02"
31 #define DESERIALIZE_PAIR_DELIM_STR "\x03"
32
33 #define TOOLCAP_KEY "tool_capabilities"
34
35 void ItemStackMetadata::clear()
36 {
37         SimpleMetadata::clear();
38         updateToolCapabilities();
39 }
40
41 static void sanitize_string(std::string &str)
42 {
43         str.erase(std::remove(str.begin(), str.end(), DESERIALIZE_START), str.end());
44         str.erase(std::remove(str.begin(), str.end(), DESERIALIZE_KV_DELIM), str.end());
45         str.erase(std::remove(str.begin(), str.end(), DESERIALIZE_PAIR_DELIM), str.end());
46 }
47
48 bool ItemStackMetadata::setString(const std::string &name, const std::string &var)
49 {
50         std::string clean_name = name;
51         std::string clean_var = var;
52         sanitize_string(clean_name);
53         sanitize_string(clean_var);
54
55         bool result = SimpleMetadata::setString(clean_name, clean_var);
56         if (clean_name == TOOLCAP_KEY)
57                 updateToolCapabilities();
58         return result;
59 }
60
61 void ItemStackMetadata::serialize(std::ostream &os) const
62 {
63         std::ostringstream os2(std::ios_base::binary);
64         os2 << DESERIALIZE_START;
65         for (const auto &stringvar : m_stringvars) {
66                 if (!stringvar.first.empty() || !stringvar.second.empty())
67                         os2 << stringvar.first << DESERIALIZE_KV_DELIM
68                                 << stringvar.second << DESERIALIZE_PAIR_DELIM;
69         }
70         os << serializeJsonStringIfNeeded(os2.str());
71 }
72
73 void ItemStackMetadata::deSerialize(std::istream &is)
74 {
75         std::string in = deSerializeJsonStringIfNeeded(is);
76
77         m_stringvars.clear();
78
79         if (!in.empty()) {
80                 if (in[0] == DESERIALIZE_START) {
81                         Strfnd fnd(in);
82                         fnd.to(1);
83                         while (!fnd.at_end()) {
84                                 std::string name = fnd.next(DESERIALIZE_KV_DELIM_STR);
85                                 std::string var  = fnd.next(DESERIALIZE_PAIR_DELIM_STR);
86                                 m_stringvars[name] = var;
87                         }
88                 } else {
89                         // BACKWARDS COMPATIBILITY
90                         m_stringvars[""] = in;
91                 }
92         }
93         updateToolCapabilities();
94 }
95
96 void ItemStackMetadata::updateToolCapabilities()
97 {
98         if (contains(TOOLCAP_KEY)) {
99                 toolcaps_overridden = true;
100                 toolcaps_override = ToolCapabilities();
101                 std::istringstream is(getString(TOOLCAP_KEY));
102                 toolcaps_override.deserializeJson(is);
103         } else {
104                 toolcaps_overridden = false;
105         }
106 }
107
108 void ItemStackMetadata::setToolCapabilities(const ToolCapabilities &caps)
109 {
110         std::ostringstream os;
111         caps.serializeJson(os);
112         setString(TOOLCAP_KEY, os.str());
113 }
114
115 void ItemStackMetadata::clearToolCapabilities()
116 {
117         setString(TOOLCAP_KEY, "");
118 }