staticobject.cpp 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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 "log.h"
  19. void StaticObject::serialize(std::ostream &os)
  20. {
  21. // type
  22. writeU8(os, type);
  23. // pos
  24. writeV3F1000(os, pos);
  25. // data
  26. os<<serializeString(data);
  27. }
  28. void StaticObject::deSerialize(std::istream &is, u8 version)
  29. {
  30. // type
  31. type = readU8(is);
  32. // pos
  33. pos = readV3F1000(is);
  34. // data
  35. data = deSerializeString(is);
  36. }
  37. void StaticObjectList::serialize(std::ostream &os)
  38. {
  39. // version
  40. u8 version = 0;
  41. writeU8(os, version);
  42. // count
  43. size_t count = m_stored.size() + m_active.size();
  44. // Make sure it fits into u16, else it would get truncated and cause e.g.
  45. // issue #2610 (Invalid block data in database: unsupported NameIdMapping version).
  46. if (count > U16_MAX) {
  47. errorstream << "StaticObjectList::serialize(): "
  48. << "too many objects (" << count << ") in list, "
  49. << "not writing them to disk." << std::endl;
  50. writeU16(os, 0); // count = 0
  51. return;
  52. }
  53. writeU16(os, count);
  54. for (StaticObject &s_obj : m_stored) {
  55. s_obj.serialize(os);
  56. }
  57. for (auto &i : m_active) {
  58. StaticObject s_obj = i.second;
  59. s_obj.serialize(os);
  60. }
  61. }
  62. void StaticObjectList::deSerialize(std::istream &is)
  63. {
  64. // version
  65. u8 version = readU8(is);
  66. // count
  67. u16 count = readU16(is);
  68. for(u16 i = 0; i < count; i++) {
  69. StaticObject s_obj;
  70. s_obj.deSerialize(is, version);
  71. m_stored.push_back(s_obj);
  72. }
  73. }