staticobject.cpp 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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 "content_sao.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<<serializeString(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 = deSerializeString(is);
  42. }
  43. void StaticObjectList::serialize(std::ostream &os)
  44. {
  45. // version
  46. u8 version = 0;
  47. writeU8(os, version);
  48. // count
  49. size_t count = m_stored.size() + m_active.size();
  50. // Make sure it fits into u16, else it would get truncated and cause e.g.
  51. // issue #2610 (Invalid block data in database: unsupported NameIdMapping version).
  52. if (count > U16_MAX) {
  53. errorstream << "StaticObjectList::serialize(): "
  54. << "too many objects (" << count << ") in list, "
  55. << "not writing them to disk." << std::endl;
  56. writeU16(os, 0); // count = 0
  57. return;
  58. }
  59. writeU16(os, count);
  60. for (StaticObject &s_obj : m_stored) {
  61. s_obj.serialize(os);
  62. }
  63. for (auto &i : m_active) {
  64. StaticObject s_obj = i.second;
  65. s_obj.serialize(os);
  66. }
  67. }
  68. void StaticObjectList::deSerialize(std::istream &is)
  69. {
  70. // version
  71. u8 version = readU8(is);
  72. // count
  73. u16 count = readU16(is);
  74. for(u16 i = 0; i < count; i++) {
  75. StaticObject s_obj;
  76. s_obj.deSerialize(is, version);
  77. m_stored.push_back(s_obj);
  78. }
  79. }