Formspecs: Add starting frame to `animated_image` (#9411)
[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 "content_sao.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<<serializeString(data);
39 }
40 void StaticObject::deSerialize(std::istream &is, u8 version)
41 {
42         // type
43         type = readU8(is);
44         // pos
45         pos = readV3F1000(is);
46         // data
47         data = deSerializeString(is);
48 }
49
50 void StaticObjectList::serialize(std::ostream &os)
51 {
52         // version
53         u8 version = 0;
54         writeU8(os, version);
55
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;
66         }
67         writeU16(os, count);
68
69         for (StaticObject &s_obj : m_stored) {
70                 s_obj.serialize(os);
71         }
72
73         for (auto &i : m_active) {
74                 StaticObject s_obj = i.second;
75                 s_obj.serialize(os);
76         }
77 }
78 void StaticObjectList::deSerialize(std::istream &is)
79 {
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;
86         }
87         m_stored.clear();
88
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);
97         }
98 }
99