itemstackmetadata.cpp 1.1 KB

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