3 Copyright (C) 2010-2013 celeron55, Perttu Ahola <celeron55@gmail.com>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation; either version 2.1 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 #include "staticobject.h"
21 #include "util/serialize.h"
22 #include "server/serveractiveobject.h"
24 StaticObject::StaticObject(const ServerActiveObject *s_obj, const v3f &pos_):
25 type(s_obj->getType()),
28 s_obj->getStaticData(&data);
31 void StaticObject::serialize(std::ostream &os)
36 writeV3F1000(os, pos);
38 os<<serializeString16(data);
41 void StaticObject::deSerialize(std::istream &is, u8 version)
46 pos = readV3F1000(is);
48 data = deSerializeString16(is);
51 void StaticObjectList::serialize(std::ostream &os)
53 // Check for problems first
54 auto problematic = [] (StaticObject &obj) -> bool {
55 if (obj.data.size() > U16_MAX) {
56 errorstream << "StaticObjectList::serialize(): "
57 "object has excessive static data (" << obj.data.size() <<
58 "), deleting it." << std::endl;
63 for (auto it = m_stored.begin(); it != m_stored.end(); ) {
65 it = m_stored.erase(it);
69 for (auto it = m_active.begin(); it != m_active.end(); ) {
70 if (problematic(it->second))
71 it = m_active.erase(it);
81 size_t count = m_stored.size() + m_active.size();
82 // Make sure it fits into u16, else it would get truncated and cause e.g.
83 // issue #2610 (Invalid block data in database: unsupported NameIdMapping version).
84 if (count > U16_MAX) {
85 errorstream << "StaticObjectList::serialize(): "
86 << "too many objects (" << count << ") in list, "
87 << "not writing them to disk." << std::endl;
88 writeU16(os, 0); // count = 0
93 for (StaticObject &s_obj : m_stored) {
97 for (auto &i : m_active) {
98 StaticObject s_obj = i.second;
102 void StaticObjectList::deSerialize(std::istream &is)
104 if (m_active.size()) {
105 errorstream << "StaticObjectList::deSerialize(): "
106 << "deserializing objects while " << m_active.size()
107 << " active objects already exist (not cleared). "
108 << m_stored.size() << " stored objects _were_ cleared"
114 u8 version = readU8(is);
116 u16 count = readU16(is);
117 for(u16 i = 0; i < count; i++) {
119 s_obj.deSerialize(is, version);
120 m_stored.push_back(s_obj);