Proper versioning of new network-serialized stuff
[oweals/minetest.git] / src / object_properties.cpp
1 /*
2 Minetest-c55
3 Copyright (C) 2012 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 "object_properties.h"
21 #include "irrlichttypes_bloated.h"
22 #include "util/serialize.h"
23 #include <sstream>
24 #include <map>
25
26 #define PP(x) "("<<(x).X<<","<<(x).Y<<","<<(x).Z<<")"
27 #define PP2(x) "("<<(x).X<<","<<(x).Y<<")"
28
29 ObjectProperties::ObjectProperties():
30         hp_max(1),
31         physical(false),
32         weight(5),
33         collisionbox(-0.5,-0.5,-0.5, 0.5,0.5,0.5),
34         visual("sprite"),
35         mesh(""),
36         visual_size(1,1),
37         spritediv(1,1),
38         initial_sprite_basepos(0,0),
39         is_visible(true),
40         makes_footstep_sound(false),
41         automatic_rotate(0)
42 {
43         textures.push_back("unknown_object.png");
44         colors.push_back(video::SColor(255,255,255,255));
45 }
46
47 std::string ObjectProperties::dump()
48 {
49         std::ostringstream os(std::ios::binary);
50         os<<"hp_max="<<hp_max;
51         os<<", physical="<<physical;
52         os<<", weight="<<weight;
53         os<<", collisionbox="<<PP(collisionbox.MinEdge)<<","<<PP(collisionbox.MaxEdge);
54         os<<", visual="<<visual;
55         os<<", mesh="<<mesh;
56         os<<", visual_size="<<PP2(visual_size);
57         os<<", textures=[";
58         for(u32 i=0; i<textures.size(); i++){
59                 os<<"\""<<textures[i]<<"\" ";
60         }
61         os<<"]";
62         os<<", colors=[";
63         for(u32 i=0; i<colors.size(); i++){
64                 os<<"\""<<colors[i].getAlpha()<<","<<colors[i].getRed()<<","<<colors[i].getGreen()<<","<<colors[i].getBlue()<<"\" ";
65         }
66         os<<"]";
67         os<<", spritediv="<<PP2(spritediv);
68         os<<", initial_sprite_basepos="<<PP2(initial_sprite_basepos);
69         os<<", is_visible="<<is_visible;
70         os<<", makes_footstep_sound="<<makes_footstep_sound;
71         os<<", automatic_rotate="<<automatic_rotate;
72         return os.str();
73 }
74
75 void ObjectProperties::serialize(std::ostream &os) const
76 {
77         writeU8(os, 2); // version
78         writeS16(os, hp_max);
79         writeU8(os, physical);
80         writeF1000(os, weight);
81         writeV3F1000(os, collisionbox.MinEdge);
82         writeV3F1000(os, collisionbox.MaxEdge);
83         os<<serializeString(visual);
84         os<<serializeString(mesh);
85         writeV2F1000(os, visual_size);
86         writeU16(os, textures.size());
87         for(u32 i=0; i<textures.size(); i++){
88                 os<<serializeString(textures[i]);
89         }
90         writeU16(os, colors.size());
91         for(u32 i=0; i<colors.size(); i++){
92                 writeARGB8(os, colors[i]);
93         }
94         writeV2S16(os, spritediv);
95         writeV2S16(os, initial_sprite_basepos);
96         writeU8(os, is_visible);
97         writeU8(os, makes_footstep_sound);
98         writeF1000(os, automatic_rotate);
99         // Stuff below should be moved to correct place in a version that otherwise changes
100         // the protocol version
101 }
102
103 void ObjectProperties::deSerialize(std::istream &is)
104 {
105         int version = readU8(is);
106         if(version == 2) // In PROTOCOL_VERSION 14
107         {
108                 hp_max = readS16(is);
109                 physical = readU8(is);
110                 weight = readF1000(is);
111                 collisionbox.MinEdge = readV3F1000(is);
112                 collisionbox.MaxEdge = readV3F1000(is);
113                 visual = deSerializeString(is);
114                 mesh = deSerializeString(is);
115                 visual_size = readV2F1000(is);
116                 textures.clear();
117                 u32 texture_count = readU16(is);
118                 for(u32 i=0; i<texture_count; i++){
119                         textures.push_back(deSerializeString(is));
120                 }
121                 u32 color_count = readU16(is);
122                 for(u32 i=0; i<color_count; i++){
123                         colors.push_back(readARGB8(is));
124                 }
125                 spritediv = readV2S16(is);
126                 initial_sprite_basepos = readV2S16(is);
127                 is_visible = readU8(is);
128                 makes_footstep_sound = readU8(is);
129                 automatic_rotate = readF1000(is);
130                 // If you add anything here, insert it primarily inside the try-catch
131                 // block to not need to increase the version.
132                 try{
133                         // Stuff below should be moved to correct place in a version that
134                         // otherwise changes the protocol version
135                 }catch(SerializationError &e){}
136         }
137         else if(version == 1) // In PROTOCOL_VERSION 13
138         {
139                 hp_max = readS16(is);
140                 physical = readU8(is);
141                 weight = readF1000(is);
142                 collisionbox.MinEdge = readV3F1000(is);
143                 collisionbox.MaxEdge = readV3F1000(is);
144                 visual = deSerializeString(is);
145                 visual_size = readV2F1000(is);
146                 textures.clear();
147                 u32 texture_count = readU16(is);
148                 for(u32 i=0; i<texture_count; i++){
149                         textures.push_back(deSerializeString(is));
150                 }
151                 spritediv = readV2S16(is);
152                 initial_sprite_basepos = readV2S16(is);
153                 is_visible = readU8(is);
154                 makes_footstep_sound = readU8(is);
155                 try{
156                         automatic_rotate = readF1000(is);
157                 }catch(SerializationError &e){}
158         }
159         else
160         {
161                 throw SerializationError("unsupported ObjectProperties version");
162         }
163 }
164