itemstackmetadata.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /*
  2. Minetest
  3. Copyright (C) 2017-8 rubenwardy <rw@rubenwardy.com>
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU Lesser General Public License as published by
  6. the Free Software Foundation; either version 2.1 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public License along
  13. with this program; if not, write to the Free Software Foundation, Inc.,
  14. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  15. */
  16. #include "itemstackmetadata.h"
  17. #include "util/serialize.h"
  18. #include "util/strfnd.h"
  19. #include <algorithm>
  20. #define DESERIALIZE_START '\x01'
  21. #define DESERIALIZE_KV_DELIM '\x02'
  22. #define DESERIALIZE_PAIR_DELIM '\x03'
  23. #define DESERIALIZE_START_STR "\x01"
  24. #define DESERIALIZE_KV_DELIM_STR "\x02"
  25. #define DESERIALIZE_PAIR_DELIM_STR "\x03"
  26. #define TOOLCAP_KEY "tool_capabilities"
  27. void ItemStackMetadata::clear()
  28. {
  29. SimpleMetadata::clear();
  30. updateToolCapabilities();
  31. }
  32. static void sanitize_string(std::string &str)
  33. {
  34. str.erase(std::remove(str.begin(), str.end(), DESERIALIZE_START), str.end());
  35. str.erase(std::remove(str.begin(), str.end(), DESERIALIZE_KV_DELIM), str.end());
  36. str.erase(std::remove(str.begin(), str.end(), DESERIALIZE_PAIR_DELIM), str.end());
  37. }
  38. bool ItemStackMetadata::setString(const std::string &name, const std::string &var)
  39. {
  40. std::string clean_name = name;
  41. std::string clean_var = var;
  42. sanitize_string(clean_name);
  43. sanitize_string(clean_var);
  44. bool result = SimpleMetadata::setString(clean_name, clean_var);
  45. if (clean_name == TOOLCAP_KEY)
  46. updateToolCapabilities();
  47. return result;
  48. }
  49. void ItemStackMetadata::serialize(std::ostream &os) const
  50. {
  51. std::ostringstream os2(std::ios_base::binary);
  52. os2 << DESERIALIZE_START;
  53. for (const auto &stringvar : m_stringvars) {
  54. if (!stringvar.first.empty() || !stringvar.second.empty())
  55. os2 << stringvar.first << DESERIALIZE_KV_DELIM
  56. << stringvar.second << DESERIALIZE_PAIR_DELIM;
  57. }
  58. os << serializeJsonStringIfNeeded(os2.str());
  59. }
  60. void ItemStackMetadata::deSerialize(std::istream &is)
  61. {
  62. std::string in = deSerializeJsonStringIfNeeded(is);
  63. m_stringvars.clear();
  64. if (!in.empty()) {
  65. if (in[0] == DESERIALIZE_START) {
  66. Strfnd fnd(in);
  67. fnd.to(1);
  68. while (!fnd.at_end()) {
  69. std::string name = fnd.next(DESERIALIZE_KV_DELIM_STR);
  70. std::string var = fnd.next(DESERIALIZE_PAIR_DELIM_STR);
  71. m_stringvars[name] = var;
  72. }
  73. } else {
  74. // BACKWARDS COMPATIBILITY
  75. m_stringvars[""] = in;
  76. }
  77. }
  78. updateToolCapabilities();
  79. }
  80. void ItemStackMetadata::updateToolCapabilities()
  81. {
  82. if (contains(TOOLCAP_KEY)) {
  83. toolcaps_overridden = true;
  84. toolcaps_override = ToolCapabilities();
  85. std::istringstream is(getString(TOOLCAP_KEY));
  86. toolcaps_override.deserializeJson(is);
  87. } else {
  88. toolcaps_overridden = false;
  89. }
  90. }
  91. void ItemStackMetadata::setToolCapabilities(const ToolCapabilities &caps)
  92. {
  93. std::ostringstream os;
  94. caps.serializeJson(os);
  95. setString(TOOLCAP_KEY, os.str());
  96. }
  97. void ItemStackMetadata::clearToolCapabilities()
  98. {
  99. setString(TOOLCAP_KEY, "");
  100. }