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