cleanup
[waspsaliva.git] / src / staticobject.cpp
blob86e455b9f65093247d5aaf09609733657e6d1db0
1 /*
2 Minetest
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()),
26 pos(pos_)
28 s_obj->getStaticData(&data);
31 void StaticObject::serialize(std::ostream &os)
33 // type
34 writeU8(os, type);
35 // pos
36 writeV3F1000(os, pos);
37 // data
38 os<<serializeString16(data);
40 void StaticObject::deSerialize(std::istream &is, u8 version)
42 // type
43 type = readU8(is);
44 // pos
45 pos = readV3F1000(is);
46 // data
47 data = deSerializeString16(is);
50 void StaticObjectList::serialize(std::ostream &os)
52 // version
53 u8 version = 0;
54 writeU8(os, version);
56 // count
57 size_t count = m_stored.size() + m_active.size();
58 // Make sure it fits into u16, else it would get truncated and cause e.g.
59 // issue #2610 (Invalid block data in database: unsupported NameIdMapping version).
60 if (count > U16_MAX) {
61 errorstream << "StaticObjectList::serialize(): "
62 << "too many objects (" << count << ") in list, "
63 << "not writing them to disk." << std::endl;
64 writeU16(os, 0); // count = 0
65 return;
67 writeU16(os, count);
69 for (StaticObject &s_obj : m_stored) {
70 s_obj.serialize(os);
73 for (auto &i : m_active) {
74 StaticObject s_obj = i.second;
75 s_obj.serialize(os);
78 void StaticObjectList::deSerialize(std::istream &is)
80 if (m_active.size()) {
81 errorstream << "StaticObjectList::deSerialize(): "
82 << "deserializing objects while " << m_active.size()
83 << " active objects already exist (not cleared). "
84 << m_stored.size() << " stored objects _were_ cleared"
85 << std::endl;
87 m_stored.clear();
89 // version
90 u8 version = readU8(is);
91 // count
92 u16 count = readU16(is);
93 for(u16 i = 0; i < count; i++) {
94 StaticObject s_obj;
95 s_obj.deSerialize(is, version);
96 m_stored.push_back(s_obj);