on_death: Fix callback number of pushed arguments (Fixes #6451)
[oweals/minetest.git] / src / itemstackmetadata.cpp
1 #include "itemstackmetadata.h"
2 #include "util/serialize.h"
3 #include "util/strfnd.h"
4
5 #define DESERIALIZE_START '\x01'
6 #define DESERIALIZE_KV_DELIM '\x02'
7 #define DESERIALIZE_PAIR_DELIM '\x03'
8 #define DESERIALIZE_START_STR "\x01"
9 #define DESERIALIZE_KV_DELIM_STR "\x02"
10 #define DESERIALIZE_PAIR_DELIM_STR "\x03"
11
12 void ItemStackMetadata::serialize(std::ostream &os) const
13 {
14         std::ostringstream os2;
15         os2 << DESERIALIZE_START;
16         for (const auto &stringvar : m_stringvars) {
17                 if (!stringvar.first.empty() || !stringvar.second.empty())
18                         os2 << stringvar.first << DESERIALIZE_KV_DELIM
19                                 << stringvar.second << DESERIALIZE_PAIR_DELIM;
20         }
21         os << serializeJsonStringIfNeeded(os2.str());
22 }
23
24 void ItemStackMetadata::deSerialize(std::istream &is)
25 {
26         std::string in = deSerializeJsonStringIfNeeded(is);
27
28         m_stringvars.clear();
29
30         if (!in.empty()) {
31                 if (in[0] == DESERIALIZE_START) {
32                         Strfnd fnd(in);
33                         fnd.to(1);
34                         while (!fnd.at_end()) {
35                                 std::string name = fnd.next(DESERIALIZE_KV_DELIM_STR);
36                                 std::string var  = fnd.next(DESERIALIZE_PAIR_DELIM_STR);
37                                 m_stringvars[name] = var;
38                         }
39                 } else {
40                         // BACKWARDS COMPATIBILITY
41                         m_stringvars[""] = in;
42                 }
43         }
44 }