staticobject.cpp 3.4 KB

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