]> git.lizzy.rs Git - dragonfireclient.git/blob - src/object_properties.cpp
Fix use-after-free in node meta cleanup
[dragonfireclient.git] / src / object_properties.cpp
1 /*
2 Minetest
3 Copyright (C) 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 "object_properties.h"
21 #include "irrlichttypes_bloated.h"
22 #include "exceptions.h"
23 #include "util/serialize.h"
24 #include "util/basic_macros.h"
25 #include <sstream>
26
27 static const video::SColor NULL_BGCOLOR{0, 1, 1, 1};
28
29 ObjectProperties::ObjectProperties()
30 {
31         textures.emplace_back("no_texture.png");
32         colors.emplace_back(255,255,255,255);
33 }
34
35 std::string ObjectProperties::dump()
36 {
37         std::ostringstream os(std::ios::binary);
38         os << "hp_max=" << hp_max;
39         os << ", breath_max=" << breath_max;
40         os << ", physical=" << physical;
41         os << ", collideWithObjects=" << collideWithObjects;
42         os << ", collisionbox=" << PP(collisionbox.MinEdge) << "," << PP(collisionbox.MaxEdge);
43         os << ", visual=" << visual;
44         os << ", mesh=" << mesh;
45         os << ", visual_size=" << PP(visual_size);
46         os << ", textures=[";
47         for (const std::string &texture : textures) {
48                 os << "\"" << texture << "\" ";
49         }
50         os << "]";
51         os << ", colors=[";
52         for (const video::SColor &color : colors) {
53                 os << "\"" << color.getAlpha() << "," << color.getRed() << ","
54                         << color.getGreen() << "," << color.getBlue() << "\" ";
55         }
56         os << "]";
57         os << ", spritediv=" << PP2(spritediv);
58         os << ", initial_sprite_basepos=" << PP2(initial_sprite_basepos);
59         os << ", is_visible=" << is_visible;
60         os << ", makes_footstep_sound=" << makes_footstep_sound;
61         os << ", automatic_rotate="<< automatic_rotate;
62         os << ", backface_culling="<< backface_culling;
63         os << ", glow=" << glow;
64         os << ", nametag=" << nametag;
65         os << ", nametag_color=" << "\"" << nametag_color.getAlpha() << "," << nametag_color.getRed()
66                         << "," << nametag_color.getGreen() << "," << nametag_color.getBlue() << "\" ";
67
68         if (nametag_bgcolor)
69                 os << ", nametag_bgcolor=" << "\"" << nametag_color.getAlpha() << "," << nametag_color.getRed()
70                    << "," << nametag_color.getGreen() << "," << nametag_color.getBlue() << "\" ";
71         else
72                 os << ", nametag_bgcolor=null ";
73
74         os << ", selectionbox=" << PP(selectionbox.MinEdge) << "," << PP(selectionbox.MaxEdge);
75         os << ", pointable=" << pointable;
76         os << ", static_save=" << static_save;
77         os << ", eye_height=" << eye_height;
78         os << ", zoom_fov=" << zoom_fov;
79         os << ", use_texture_alpha=" << use_texture_alpha;
80         os << ", damage_texture_modifier=" << damage_texture_modifier;
81         os << ", shaded=" << shaded;
82         os << ", show_on_minimap=" << show_on_minimap;
83         return os.str();
84 }
85
86 bool ObjectProperties::validate()
87 {
88         const char *func = "ObjectProperties::validate(): ";
89         bool ret = true;
90
91         // cf. where serializeString16 is used below
92         for (u32 i = 0; i < textures.size(); i++) {
93                 if (textures[i].size() > U16_MAX) {
94                         warningstream << func << "texture " << (i+1) << " has excessive length, "
95                                 "clearing it." << std::endl;
96                         textures[i].clear();
97                         ret = false;
98                 }
99         }
100         if (nametag.length() > U16_MAX) {
101                 warningstream << func << "nametag has excessive length, clearing it." << std::endl;
102                 nametag.clear();
103                 ret = false;
104         }
105         if (infotext.length() > U16_MAX) {
106                 warningstream << func << "infotext has excessive length, clearing it." << std::endl;
107                 infotext.clear();
108                 ret = false;
109         }
110         if (wield_item.length() > U16_MAX) {
111                 warningstream << func << "wield_item has excessive length, clearing it." << std::endl;
112                 wield_item.clear();
113                 ret = false;
114         }
115
116         return ret;
117 }
118
119 void ObjectProperties::serialize(std::ostream &os) const
120 {
121         writeU8(os, 4); // PROTOCOL_VERSION >= 37
122         writeU16(os, hp_max);
123         writeU8(os, physical);
124         writeF32(os, 0.f); // Removed property (weight)
125         writeV3F32(os, collisionbox.MinEdge);
126         writeV3F32(os, collisionbox.MaxEdge);
127         writeV3F32(os, selectionbox.MinEdge);
128         writeV3F32(os, selectionbox.MaxEdge);
129         writeU8(os, pointable);
130         os << serializeString16(visual);
131         writeV3F32(os, visual_size);
132         writeU16(os, textures.size());
133         for (const std::string &texture : textures) {
134                 os << serializeString16(texture);
135         }
136         writeV2S16(os, spritediv);
137         writeV2S16(os, initial_sprite_basepos);
138         writeU8(os, is_visible);
139         writeU8(os, makes_footstep_sound);
140         writeF32(os, automatic_rotate);
141         os << serializeString16(mesh);
142         writeU16(os, colors.size());
143         for (video::SColor color : colors) {
144                 writeARGB8(os, color);
145         }
146         writeU8(os, collideWithObjects);
147         writeF32(os, stepheight);
148         writeU8(os, automatic_face_movement_dir);
149         writeF32(os, automatic_face_movement_dir_offset);
150         writeU8(os, backface_culling);
151         os << serializeString16(nametag);
152         writeARGB8(os, nametag_color);
153         writeF32(os, automatic_face_movement_max_rotation_per_sec);
154         os << serializeString16(infotext);
155         os << serializeString16(wield_item);
156         writeS8(os, glow);
157         writeU16(os, breath_max);
158         writeF32(os, eye_height);
159         writeF32(os, zoom_fov);
160         writeU8(os, use_texture_alpha);
161         os << serializeString16(damage_texture_modifier);
162         writeU8(os, shaded);
163         writeU8(os, show_on_minimap);
164
165         if (!nametag_bgcolor)
166                 writeARGB8(os, NULL_BGCOLOR);
167         else if (nametag_bgcolor.value().getAlpha() == 0)
168                 writeARGB8(os, video::SColor(0, 0, 0, 0));
169         else
170                 writeARGB8(os, nametag_bgcolor.value());
171
172         // Add stuff only at the bottom.
173         // Never remove anything, because we don't want new versions of this
174 }
175
176 void ObjectProperties::deSerialize(std::istream &is)
177 {
178         int version = readU8(is);
179         if (version != 4)
180                 throw SerializationError("unsupported ObjectProperties version");
181
182         hp_max = readU16(is);
183         physical = readU8(is);
184         readU32(is); // removed property (weight)
185         collisionbox.MinEdge = readV3F32(is);
186         collisionbox.MaxEdge = readV3F32(is);
187         selectionbox.MinEdge = readV3F32(is);
188         selectionbox.MaxEdge = readV3F32(is);
189         pointable = readU8(is);
190         visual = deSerializeString16(is);
191         visual_size = readV3F32(is);
192         textures.clear();
193         u32 texture_count = readU16(is);
194         for (u32 i = 0; i < texture_count; i++){
195                 textures.push_back(deSerializeString16(is));
196         }
197         spritediv = readV2S16(is);
198         initial_sprite_basepos = readV2S16(is);
199         is_visible = readU8(is);
200         makes_footstep_sound = readU8(is);
201         automatic_rotate = readF32(is);
202         mesh = deSerializeString16(is);
203         colors.clear();
204         u32 color_count = readU16(is);
205         for (u32 i = 0; i < color_count; i++){
206                 colors.push_back(readARGB8(is));
207         }
208         collideWithObjects = readU8(is);
209         stepheight = readF32(is);
210         automatic_face_movement_dir = readU8(is);
211         automatic_face_movement_dir_offset = readF32(is);
212         backface_culling = readU8(is);
213         nametag = deSerializeString16(is);
214         nametag_color = readARGB8(is);
215         automatic_face_movement_max_rotation_per_sec = readF32(is);
216         infotext = deSerializeString16(is);
217         wield_item = deSerializeString16(is);
218         glow = readS8(is);
219         breath_max = readU16(is);
220         eye_height = readF32(is);
221         zoom_fov = readF32(is);
222         use_texture_alpha = readU8(is);
223         try {
224                 damage_texture_modifier = deSerializeString16(is);
225                 u8 tmp = readU8(is);
226                 if (is.eof())
227                         return;
228                 shaded = tmp;
229                 tmp = readU8(is);
230                 if (is.eof())
231                         return;
232                 show_on_minimap = tmp;
233
234                 auto bgcolor = readARGB8(is);
235                 if (bgcolor != NULL_BGCOLOR)
236                         nametag_bgcolor = bgcolor;
237                 else
238                         nametag_bgcolor = nullopt;
239         } catch (SerializationError &e) {}
240 }