]> git.lizzy.rs Git - minetest.git/blob - src/itemstackmetadata.cpp
Hardware coloring for itemstacks
[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 (StringMap::const_iterator
17                         it = m_stringvars.begin();
18                         it != m_stringvars.end(); ++it) {
19                 os2 << it->first << DESERIALIZE_KV_DELIM
20                     << it->second << DESERIALIZE_PAIR_DELIM;
21         }
22         os << serializeJsonStringIfNeeded(os2.str());
23 }
24
25 void ItemStackMetadata::deSerialize(std::istream &is)
26 {
27         std::string in = deSerializeJsonStringIfNeeded(is);
28
29         m_stringvars.clear();
30
31         if (!in.empty() && 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 }