staticobject.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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 "staticobject.h"
  17. #include "util/serialize.h"
  18. #include "server/serveractiveobject.h"
  19. StaticObject::StaticObject(const ServerActiveObject *s_obj, const v3f &pos_):
  20. type(s_obj->getType()),
  21. pos(pos_)
  22. {
  23. s_obj->getStaticData(&data);
  24. }
  25. void StaticObject::serialize(std::ostream &os)
  26. {
  27. // type
  28. writeU8(os, type);
  29. // pos
  30. writeV3F1000(os, pos);
  31. // data
  32. os<<serializeString16(data);
  33. }
  34. void StaticObject::deSerialize(std::istream &is, u8 version)
  35. {
  36. // type
  37. type = readU8(is);
  38. // pos
  39. pos = readV3F1000(is);
  40. // data
  41. data = deSerializeString16(is);
  42. }
  43. void StaticObjectList::serialize(std::ostream &os)
  44. {
  45. // Check for problems first
  46. auto problematic = [] (StaticObject &obj) -> bool {
  47. if (obj.data.size() > U16_MAX) {
  48. errorstream << "StaticObjectList::serialize(): "
  49. "object has excessive static data (" << obj.data.size() <<
  50. "), deleting it." << std::endl;
  51. return true;
  52. }
  53. return false;
  54. };
  55. for (auto it = m_stored.begin(); it != m_stored.end(); ) {
  56. if (problematic(*it))
  57. it = m_stored.erase(it);
  58. else
  59. it++;
  60. }
  61. for (auto it = m_active.begin(); it != m_active.end(); ) {
  62. if (problematic(it->second))
  63. it = m_active.erase(it);
  64. else
  65. it++;
  66. }
  67. // version
  68. u8 version = 0;
  69. writeU8(os, version);
  70. // count
  71. size_t count = m_stored.size() + m_active.size();
  72. // Make sure it fits into u16, else it would get truncated and cause e.g.
  73. // issue #2610 (Invalid block data in database: unsupported NameIdMapping version).
  74. if (count > U16_MAX) {
  75. errorstream << "StaticObjectList::serialize(): "
  76. << "too many objects (" << count << ") in list, "
  77. << "not writing them to disk." << std::endl;
  78. writeU16(os, 0); // count = 0
  79. return;
  80. }
  81. writeU16(os, count);
  82. for (StaticObject &s_obj : m_stored) {
  83. s_obj.serialize(os);
  84. }
  85. for (auto &i : m_active) {
  86. StaticObject s_obj = i.second;
  87. s_obj.serialize(os);
  88. }
  89. }
  90. void StaticObjectList::deSerialize(std::istream &is)
  91. {
  92. if (m_active.size()) {
  93. errorstream << "StaticObjectList::deSerialize(): "
  94. << "deserializing objects while " << m_active.size()
  95. << " active objects already exist (not cleared). "
  96. << m_stored.size() << " stored objects _were_ cleared"
  97. << std::endl;
  98. }
  99. m_stored.clear();
  100. // version
  101. u8 version = readU8(is);
  102. // count
  103. u16 count = readU16(is);
  104. for(u16 i = 0; i < count; i++) {
  105. StaticObject s_obj;
  106. s_obj.deSerialize(is, version);
  107. m_stored.push_back(s_obj);
  108. }
  109. }