itemstackmetadata.cpp 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  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. if (!stringvar.first.empty() || !stringvar.second.empty())
  16. os2 << stringvar.first << DESERIALIZE_KV_DELIM
  17. << stringvar.second << DESERIALIZE_PAIR_DELIM;
  18. }
  19. os << serializeJsonStringIfNeeded(os2.str());
  20. }
  21. void ItemStackMetadata::deSerialize(std::istream &is)
  22. {
  23. std::string in = deSerializeJsonStringIfNeeded(is);
  24. m_stringvars.clear();
  25. if (!in.empty()) {
  26. if (in[0] == DESERIALIZE_START) {
  27. Strfnd fnd(in);
  28. fnd.to(1);
  29. while (!fnd.at_end()) {
  30. std::string name = fnd.next(DESERIALIZE_KV_DELIM_STR);
  31. std::string var = fnd.next(DESERIALIZE_PAIR_DELIM_STR);
  32. m_stringvars[name] = var;
  33. }
  34. } else {
  35. // BACKWARDS COMPATIBILITY
  36. m_stringvars[""] = in;
  37. }
  38. }
  39. }