incrementaltp: respect physics overrides
[waspsaliva.git] / src / util / serialize.h
blobb3ec28eab58ccf10e826db8cd37e38b44a4b55ab
1 /*
2 Minetest
3 Copyright (C) 2010-2013 celeron55, Perttu Ahola <celeron55@gmail.com>
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.
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.
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.
20 #pragma once
22 #include "irrlichttypes_bloated.h"
23 #include "exceptions.h" // for SerializationError
24 #include "debug.h" // for assert
25 #include "ieee_float.h"
27 #include "config.h"
28 #if HAVE_ENDIAN_H
29 #ifdef _WIN32
30 #define __BYTE_ORDER 0
31 #define __LITTLE_ENDIAN 0
32 #define __BIG_ENDIAN 1
33 #elif defined(__MACH__) && defined(__APPLE__)
34 #include <machine/endian.h>
35 #elif defined(__FreeBSD__) || defined(__DragonFly__)
36 #include <sys/endian.h>
37 #else
38 #include <endian.h>
39 #endif
40 #endif
41 #include <cstring> // for memcpy
42 #include <iostream>
43 #include <string>
44 #include <vector>
46 #define FIXEDPOINT_FACTOR 1000.0f
48 // 0x7FFFFFFF / 1000.0f is not serializable.
49 // The limited float precision at this magnitude may cause the result to round
50 // to a greater value than can be represented by a 32 bit integer when increased
51 // by a factor of FIXEDPOINT_FACTOR. As a result, [F1000_MIN..F1000_MAX] does
52 // not represent the full range, but rather the largest safe range, of values on
53 // all supported architectures. Note: This definition makes assumptions on
54 // platform float-to-int conversion behavior.
55 #define F1000_MIN ((float)(s32)((float)(-0x7FFFFFFF - 1) / FIXEDPOINT_FACTOR))
56 #define F1000_MAX ((float)(s32)((float)(0x7FFFFFFF) / FIXEDPOINT_FACTOR))
58 #define STRING_MAX_LEN 0xFFFF
59 #define WIDE_STRING_MAX_LEN 0xFFFF
60 // 64 MB ought to be enough for anybody - Billy G.
61 #define LONG_STRING_MAX_LEN (64 * 1024 * 1024)
64 extern FloatType g_serialize_f32_type;
66 #if HAVE_ENDIAN_H
67 // use machine native byte swapping routines
68 // Note: memcpy below is optimized out by modern compilers
70 inline u16 readU16(const u8 *data)
72 u16 val;
73 memcpy(&val, data, 2);
74 return be16toh(val);
77 inline u32 readU32(const u8 *data)
79 u32 val;
80 memcpy(&val, data, 4);
81 return be32toh(val);
84 inline u64 readU64(const u8 *data)
86 u64 val;
87 memcpy(&val, data, 8);
88 return be64toh(val);
91 inline void writeU16(u8 *data, u16 i)
93 u16 val = htobe16(i);
94 memcpy(data, &val, 2);
97 inline void writeU32(u8 *data, u32 i)
99 u32 val = htobe32(i);
100 memcpy(data, &val, 4);
103 inline void writeU64(u8 *data, u64 i)
105 u64 val = htobe64(i);
106 memcpy(data, &val, 8);
109 #else
110 // generic byte-swapping implementation
112 inline u16 readU16(const u8 *data)
114 return
115 ((u16)data[0] << 8) | ((u16)data[1] << 0);
118 inline u32 readU32(const u8 *data)
120 return
121 ((u32)data[0] << 24) | ((u32)data[1] << 16) |
122 ((u32)data[2] << 8) | ((u32)data[3] << 0);
125 inline u64 readU64(const u8 *data)
127 return
128 ((u64)data[0] << 56) | ((u64)data[1] << 48) |
129 ((u64)data[2] << 40) | ((u64)data[3] << 32) |
130 ((u64)data[4] << 24) | ((u64)data[5] << 16) |
131 ((u64)data[6] << 8) | ((u64)data[7] << 0);
134 inline void writeU16(u8 *data, u16 i)
136 data[0] = (i >> 8) & 0xFF;
137 data[1] = (i >> 0) & 0xFF;
140 inline void writeU32(u8 *data, u32 i)
142 data[0] = (i >> 24) & 0xFF;
143 data[1] = (i >> 16) & 0xFF;
144 data[2] = (i >> 8) & 0xFF;
145 data[3] = (i >> 0) & 0xFF;
148 inline void writeU64(u8 *data, u64 i)
150 data[0] = (i >> 56) & 0xFF;
151 data[1] = (i >> 48) & 0xFF;
152 data[2] = (i >> 40) & 0xFF;
153 data[3] = (i >> 32) & 0xFF;
154 data[4] = (i >> 24) & 0xFF;
155 data[5] = (i >> 16) & 0xFF;
156 data[6] = (i >> 8) & 0xFF;
157 data[7] = (i >> 0) & 0xFF;
160 #endif // HAVE_ENDIAN_H
162 //////////////// read routines ////////////////
164 inline u8 readU8(const u8 *data)
166 return ((u8)data[0] << 0);
169 inline s8 readS8(const u8 *data)
171 return (s8)readU8(data);
174 inline s16 readS16(const u8 *data)
176 return (s16)readU16(data);
179 inline s32 readS32(const u8 *data)
181 return (s32)readU32(data);
184 inline s64 readS64(const u8 *data)
186 return (s64)readU64(data);
189 inline f32 readF1000(const u8 *data)
191 return (f32)readS32(data) / FIXEDPOINT_FACTOR;
194 inline f32 readF32(const u8 *data)
196 u32 u = readU32(data);
198 switch (g_serialize_f32_type) {
199 case FLOATTYPE_SYSTEM: {
200 f32 f;
201 memcpy(&f, &u, 4);
202 return f;
204 case FLOATTYPE_SLOW:
205 return u32Tof32Slow(u);
206 case FLOATTYPE_UNKNOWN: // First initialization
207 g_serialize_f32_type = getFloatSerializationType();
208 return readF32(data);
210 throw SerializationError("readF32: Unreachable code");
213 inline video::SColor readARGB8(const u8 *data)
215 video::SColor p(readU32(data));
216 return p;
219 inline v2s16 readV2S16(const u8 *data)
221 v2s16 p;
222 p.X = readS16(&data[0]);
223 p.Y = readS16(&data[2]);
224 return p;
227 inline v3s16 readV3S16(const u8 *data)
229 v3s16 p;
230 p.X = readS16(&data[0]);
231 p.Y = readS16(&data[2]);
232 p.Z = readS16(&data[4]);
233 return p;
236 inline v2s32 readV2S32(const u8 *data)
238 v2s32 p;
239 p.X = readS32(&data[0]);
240 p.Y = readS32(&data[4]);
241 return p;
244 inline v3s32 readV3S32(const u8 *data)
246 v3s32 p;
247 p.X = readS32(&data[0]);
248 p.Y = readS32(&data[4]);
249 p.Z = readS32(&data[8]);
250 return p;
253 inline v3f readV3F1000(const u8 *data)
255 v3f p;
256 p.X = readF1000(&data[0]);
257 p.Y = readF1000(&data[4]);
258 p.Z = readF1000(&data[8]);
259 return p;
262 inline v2f readV2F32(const u8 *data)
264 v2f p;
265 p.X = readF32(&data[0]);
266 p.Y = readF32(&data[4]);
267 return p;
270 inline v3f readV3F32(const u8 *data)
272 v3f p;
273 p.X = readF32(&data[0]);
274 p.Y = readF32(&data[4]);
275 p.Z = readF32(&data[8]);
276 return p;
279 /////////////// write routines ////////////////
281 inline void writeU8(u8 *data, u8 i)
283 data[0] = (i >> 0) & 0xFF;
286 inline void writeS8(u8 *data, s8 i)
288 writeU8(data, (u8)i);
291 inline void writeS16(u8 *data, s16 i)
293 writeU16(data, (u16)i);
296 inline void writeS32(u8 *data, s32 i)
298 writeU32(data, (u32)i);
301 inline void writeS64(u8 *data, s64 i)
303 writeU64(data, (u64)i);
306 inline void writeF1000(u8 *data, f32 i)
308 assert(i >= F1000_MIN && i <= F1000_MAX);
309 writeS32(data, i * FIXEDPOINT_FACTOR);
312 inline void writeF32(u8 *data, f32 i)
314 switch (g_serialize_f32_type) {
315 case FLOATTYPE_SYSTEM: {
316 u32 u;
317 memcpy(&u, &i, 4);
318 return writeU32(data, u);
320 case FLOATTYPE_SLOW:
321 return writeU32(data, f32Tou32Slow(i));
322 case FLOATTYPE_UNKNOWN: // First initialization
323 g_serialize_f32_type = getFloatSerializationType();
324 return writeF32(data, i);
326 throw SerializationError("writeF32: Unreachable code");
329 inline void writeARGB8(u8 *data, video::SColor p)
331 writeU32(data, p.color);
334 inline void writeV2S16(u8 *data, v2s16 p)
336 writeS16(&data[0], p.X);
337 writeS16(&data[2], p.Y);
340 inline void writeV3S16(u8 *data, v3s16 p)
342 writeS16(&data[0], p.X);
343 writeS16(&data[2], p.Y);
344 writeS16(&data[4], p.Z);
347 inline void writeV2S32(u8 *data, v2s32 p)
349 writeS32(&data[0], p.X);
350 writeS32(&data[4], p.Y);
353 inline void writeV3S32(u8 *data, v3s32 p)
355 writeS32(&data[0], p.X);
356 writeS32(&data[4], p.Y);
357 writeS32(&data[8], p.Z);
360 inline void writeV3F1000(u8 *data, v3f p)
362 writeF1000(&data[0], p.X);
363 writeF1000(&data[4], p.Y);
364 writeF1000(&data[8], p.Z);
367 inline void writeV2F32(u8 *data, v2f p)
369 writeF32(&data[0], p.X);
370 writeF32(&data[4], p.Y);
373 inline void writeV3F32(u8 *data, v3f p)
375 writeF32(&data[0], p.X);
376 writeF32(&data[4], p.Y);
377 writeF32(&data[8], p.Z);
380 ////
381 //// Iostream wrapper for data read/write
382 ////
384 #define MAKE_STREAM_READ_FXN(T, N, S) \
385 inline T read ## N(std::istream &is) \
387 char buf[S] = {0}; \
388 is.read(buf, sizeof(buf)); \
389 return read ## N((u8 *)buf); \
392 #define MAKE_STREAM_WRITE_FXN(T, N, S) \
393 inline void write ## N(std::ostream &os, T val) \
395 char buf[S]; \
396 write ## N((u8 *)buf, val); \
397 os.write(buf, sizeof(buf)); \
400 MAKE_STREAM_READ_FXN(u8, U8, 1);
401 MAKE_STREAM_READ_FXN(u16, U16, 2);
402 MAKE_STREAM_READ_FXN(u32, U32, 4);
403 MAKE_STREAM_READ_FXN(u64, U64, 8);
404 MAKE_STREAM_READ_FXN(s8, S8, 1);
405 MAKE_STREAM_READ_FXN(s16, S16, 2);
406 MAKE_STREAM_READ_FXN(s32, S32, 4);
407 MAKE_STREAM_READ_FXN(s64, S64, 8);
408 MAKE_STREAM_READ_FXN(f32, F1000, 4);
409 MAKE_STREAM_READ_FXN(f32, F32, 4);
410 MAKE_STREAM_READ_FXN(v2s16, V2S16, 4);
411 MAKE_STREAM_READ_FXN(v3s16, V3S16, 6);
412 MAKE_STREAM_READ_FXN(v2s32, V2S32, 8);
413 MAKE_STREAM_READ_FXN(v3s32, V3S32, 12);
414 MAKE_STREAM_READ_FXN(v3f, V3F1000, 12);
415 MAKE_STREAM_READ_FXN(v2f, V2F32, 8);
416 MAKE_STREAM_READ_FXN(v3f, V3F32, 12);
417 MAKE_STREAM_READ_FXN(video::SColor, ARGB8, 4);
419 MAKE_STREAM_WRITE_FXN(u8, U8, 1);
420 MAKE_STREAM_WRITE_FXN(u16, U16, 2);
421 MAKE_STREAM_WRITE_FXN(u32, U32, 4);
422 MAKE_STREAM_WRITE_FXN(u64, U64, 8);
423 MAKE_STREAM_WRITE_FXN(s8, S8, 1);
424 MAKE_STREAM_WRITE_FXN(s16, S16, 2);
425 MAKE_STREAM_WRITE_FXN(s32, S32, 4);
426 MAKE_STREAM_WRITE_FXN(s64, S64, 8);
427 MAKE_STREAM_WRITE_FXN(f32, F1000, 4);
428 MAKE_STREAM_WRITE_FXN(f32, F32, 4);
429 MAKE_STREAM_WRITE_FXN(v2s16, V2S16, 4);
430 MAKE_STREAM_WRITE_FXN(v3s16, V3S16, 6);
431 MAKE_STREAM_WRITE_FXN(v2s32, V2S32, 8);
432 MAKE_STREAM_WRITE_FXN(v3s32, V3S32, 12);
433 MAKE_STREAM_WRITE_FXN(v3f, V3F1000, 12);
434 MAKE_STREAM_WRITE_FXN(v2f, V2F32, 8);
435 MAKE_STREAM_WRITE_FXN(v3f, V3F32, 12);
436 MAKE_STREAM_WRITE_FXN(video::SColor, ARGB8, 4);
438 ////
439 //// More serialization stuff
440 ////
442 // Creates a string with the length as the first two bytes
443 std::string serializeString16(const std::string &plain);
445 // Reads a string with the length as the first two bytes
446 std::string deSerializeString16(std::istream &is);
448 // Creates a string with the length as the first four bytes
449 std::string serializeString32(const std::string &plain);
451 // Reads a string with the length as the first four bytes
452 std::string deSerializeString32(std::istream &is);
454 // Creates a string encoded in JSON format (almost equivalent to a C string literal)
455 std::string serializeJsonString(const std::string &plain);
457 // Reads a string encoded in JSON format
458 std::string deSerializeJsonString(std::istream &is);
460 // If the string contains spaces, quotes or control characters, encodes as JSON.
461 // Else returns the string unmodified.
462 std::string serializeJsonStringIfNeeded(const std::string &s);
464 // Parses a string serialized by serializeJsonStringIfNeeded.
465 std::string deSerializeJsonStringIfNeeded(std::istream &is);