nodemetadata.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. /*
  2. Minetest
  3. Copyright (C) 2010-2013 celeron55, Perttu Ahola <celeron55@gmail.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 "nodemetadata.h"
  17. #include "exceptions.h"
  18. #include "gamedef.h"
  19. #include "inventory.h"
  20. #include "log.h"
  21. #include "util/serialize.h"
  22. #include "constants.h" // MAP_BLOCKSIZE
  23. #include <sstream>
  24. /*
  25. NodeMetadata
  26. */
  27. NodeMetadata::NodeMetadata(IGameDef *gamedef):
  28. m_stringvars(),
  29. m_inventory(new Inventory(gamedef->idef()))
  30. {
  31. }
  32. NodeMetadata::~NodeMetadata()
  33. {
  34. delete m_inventory;
  35. }
  36. void NodeMetadata::serialize(std::ostream &os) const
  37. {
  38. int num_vars = m_stringvars.size();
  39. writeU32(os, num_vars);
  40. for(std::map<std::string, std::string>::const_iterator
  41. i = m_stringvars.begin(); i != m_stringvars.end(); i++){
  42. os<<serializeString(i->first);
  43. os<<serializeLongString(i->second);
  44. }
  45. m_inventory->serialize(os);
  46. }
  47. void NodeMetadata::deSerialize(std::istream &is)
  48. {
  49. m_stringvars.clear();
  50. int num_vars = readU32(is);
  51. for(int i=0; i<num_vars; i++){
  52. std::string name = deSerializeString(is);
  53. std::string var = deSerializeLongString(is);
  54. m_stringvars[name] = var;
  55. }
  56. m_inventory->deSerialize(is);
  57. }
  58. void NodeMetadata::clear()
  59. {
  60. m_stringvars.clear();
  61. m_inventory->clear();
  62. }
  63. /*
  64. NodeMetadataList
  65. */
  66. void NodeMetadataList::serialize(std::ostream &os) const
  67. {
  68. /*
  69. Version 0 is a placeholder for "nothing to see here; go away."
  70. */
  71. if(m_data.size() == 0){
  72. writeU8(os, 0); // version
  73. return;
  74. }
  75. writeU8(os, 1); // version
  76. u16 count = m_data.size();
  77. writeU16(os, count);
  78. for(std::map<v3s16, NodeMetadata*>::const_iterator
  79. i = m_data.begin();
  80. i != m_data.end(); i++)
  81. {
  82. v3s16 p = i->first;
  83. NodeMetadata *data = i->second;
  84. u16 p16 = p.Z*MAP_BLOCKSIZE*MAP_BLOCKSIZE + p.Y*MAP_BLOCKSIZE + p.X;
  85. writeU16(os, p16);
  86. data->serialize(os);
  87. }
  88. }
  89. void NodeMetadataList::deSerialize(std::istream &is, IGameDef *gamedef)
  90. {
  91. m_data.clear();
  92. u8 version = readU8(is);
  93. if(version == 0){
  94. // Nothing
  95. return;
  96. }
  97. if(version != 1){
  98. infostream<<__FUNCTION_NAME<<": version "<<version<<" not supported"
  99. <<std::endl;
  100. throw SerializationError("NodeMetadataList::deSerialize");
  101. }
  102. u16 count = readU16(is);
  103. for(u16 i=0; i<count; i++)
  104. {
  105. u16 p16 = readU16(is);
  106. v3s16 p(0,0,0);
  107. p.Z += p16 / MAP_BLOCKSIZE / MAP_BLOCKSIZE;
  108. p16 -= p.Z * MAP_BLOCKSIZE * MAP_BLOCKSIZE;
  109. p.Y += p16 / MAP_BLOCKSIZE;
  110. p16 -= p.Y * MAP_BLOCKSIZE;
  111. p.X += p16;
  112. if(m_data.find(p) != m_data.end())
  113. {
  114. infostream<<"WARNING: NodeMetadataList::deSerialize(): "
  115. <<"already set data at position"
  116. <<"("<<p.X<<","<<p.Y<<","<<p.Z<<"): Ignoring."
  117. <<std::endl;
  118. continue;
  119. }
  120. NodeMetadata *data = new NodeMetadata(gamedef);
  121. data->deSerialize(is);
  122. m_data[p] = data;
  123. }
  124. }
  125. NodeMetadataList::~NodeMetadataList()
  126. {
  127. clear();
  128. }
  129. NodeMetadata* NodeMetadataList::get(v3s16 p)
  130. {
  131. std::map<v3s16, NodeMetadata*>::const_iterator n = m_data.find(p);
  132. if(n == m_data.end())
  133. return NULL;
  134. return n->second;
  135. }
  136. void NodeMetadataList::remove(v3s16 p)
  137. {
  138. NodeMetadata *olddata = get(p);
  139. if(olddata)
  140. {
  141. delete olddata;
  142. m_data.erase(p);
  143. }
  144. }
  145. void NodeMetadataList::set(v3s16 p, NodeMetadata *d)
  146. {
  147. remove(p);
  148. m_data.insert(std::make_pair(p, d));
  149. }
  150. void NodeMetadataList::clear()
  151. {
  152. for(std::map<v3s16, NodeMetadata*>::iterator
  153. i = m_data.begin();
  154. i != m_data.end(); i++)
  155. {
  156. delete i->second;
  157. }
  158. m_data.clear();
  159. }
  160. std::string NodeMetadata::getString(const std::string &name, unsigned short recursion) const
  161. {
  162. std::map<std::string, std::string>::const_iterator it;
  163. it = m_stringvars.find(name);
  164. if (it == m_stringvars.end()) {
  165. return "";
  166. }
  167. return resolveString(it->second, recursion);
  168. }
  169. void NodeMetadata::setString(const std::string &name, const std::string &var)
  170. {
  171. if (var.empty()) {
  172. m_stringvars.erase(name);
  173. } else {
  174. m_stringvars[name] = var;
  175. }
  176. }
  177. std::string NodeMetadata::resolveString(const std::string &str, unsigned short recursion) const
  178. {
  179. if (recursion > 1) {
  180. return str;
  181. }
  182. if (str.substr(0, 2) == "${" && str[str.length() - 1] == '}') {
  183. return getString(str.substr(2, str.length() - 3), recursion + 1);
  184. }
  185. return str;
  186. }