]> git.lizzy.rs Git - dragonfireclient.git/blob - src/staticobject.cpp
Merge pull request #59 from PrairieAstronomer/readme_irrlicht_change
[dragonfireclient.git] / src / staticobject.cpp
1 /*
2 Minetest
3 Copyright (C) 2010-2013 celeron55, Perttu Ahola <celeron55@gmail.com>
4
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.
9
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.
14
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.
18 */
19
20 #include "staticobject.h"
21 #include "util/serialize.h"
22 #include "server/serveractiveobject.h"
23
24 StaticObject::StaticObject(const ServerActiveObject *s_obj, const v3f &pos_):
25         type(s_obj->getType()),
26         pos(pos_)
27 {
28         s_obj->getStaticData(&data);
29 }
30
31 void StaticObject::serialize(std::ostream &os)
32 {
33         // type
34         writeU8(os, type);
35         // pos
36         writeV3F1000(os, pos);
37         // data
38         os<<serializeString16(data);
39 }
40
41 void StaticObject::deSerialize(std::istream &is, u8 version)
42 {
43         // type
44         type = readU8(is);
45         // pos
46         pos = readV3F1000(is);
47         // data
48         data = deSerializeString16(is);
49 }
50
51 void StaticObjectList::serialize(std::ostream &os)
52 {
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;
59                         return true;
60                 }
61                 return false;
62         };
63         for (auto it = m_stored.begin(); it != m_stored.end(); ) {
64                 if (problematic(*it))
65                         it = m_stored.erase(it);
66                 else
67                         it++;
68         }
69         for (auto it = m_active.begin(); it != m_active.end(); ) {
70                 if (problematic(it->second))
71                         it = m_active.erase(it);
72                 else
73                         it++;
74         }
75
76         // version
77         u8 version = 0;
78         writeU8(os, version);
79
80         // count
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
89                 return;
90         }
91         writeU16(os, count);
92
93         for (StaticObject &s_obj : m_stored) {
94                 s_obj.serialize(os);
95         }
96
97         for (auto &i : m_active) {
98                 StaticObject s_obj = i.second;
99                 s_obj.serialize(os);
100         }
101 }
102 void StaticObjectList::deSerialize(std::istream &is)
103 {
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"
109                         << std::endl;
110         }
111         m_stored.clear();
112
113         // version
114         u8 version = readU8(is);
115         // count
116         u16 count = readU16(is);
117         for(u16 i = 0; i < count; i++) {
118                 StaticObject s_obj;
119                 s_obj.deSerialize(is, version);
120                 m_stored.push_back(s_obj);
121         }
122 }
123