3 Copyright (C) 2010-2012 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 #ifndef UTIL_SERIALIZE_HEADER
21 #define UTIL_SERIALIZE_HEADER
23 #include "../irrlichttypes.h"
24 #include "../irrlichttypes_bloated.h"
25 #include "../irr_v2d.h"
26 #include "../irr_v3d.h"
29 #include "../exceptions.h"
32 inline void writeU64(u8 *data, u64 i)
34 data[0] = ((i>>56)&0xff);
35 data[1] = ((i>>48)&0xff);
36 data[2] = ((i>>40)&0xff);
37 data[3] = ((i>>32)&0xff);
38 data[4] = ((i>>24)&0xff);
39 data[5] = ((i>>16)&0xff);
40 data[6] = ((i>> 8)&0xff);
41 data[7] = ((i>> 0)&0xff);
44 inline void writeU32(u8 *data, u32 i)
46 data[0] = ((i>>24)&0xff);
47 data[1] = ((i>>16)&0xff);
48 data[2] = ((i>> 8)&0xff);
49 data[3] = ((i>> 0)&0xff);
52 inline void writeU16(u8 *data, u16 i)
54 data[0] = ((i>> 8)&0xff);
55 data[1] = ((i>> 0)&0xff);
58 inline void writeU8(u8 *data, u8 i)
60 data[0] = ((i>> 0)&0xff);
63 inline u64 readU64(u8 *data)
65 return ((u64)data[0]<<56) | ((u64)data[1]<<48)
66 | ((u64)data[2]<<40) | ((u64)data[3]<<32)
67 | ((u64)data[4]<<24) | ((u64)data[5]<<16)
68 | ((u64)data[6]<<8) | ((u64)data[7]<<0);
71 inline u32 readU32(u8 *data)
73 return (data[0]<<24) | (data[1]<<16) | (data[2]<<8) | (data[3]<<0);
76 inline u16 readU16(u8 *data)
78 return (data[0]<<8) | (data[1]<<0);
81 inline u8 readU8(u8 *data)
86 inline void writeS32(u8 *data, s32 i){
87 writeU32(data, (u32)i);
89 inline s32 readS32(u8 *data){
90 return (s32)readU32(data);
93 inline void writeS16(u8 *data, s16 i){
94 writeU16(data, (u16)i);
96 inline s16 readS16(u8 *data){
97 return (s16)readU16(data);
100 inline void writeS8(u8 *data, s8 i){
101 writeU8(data, (u8)i);
103 inline s8 readS8(u8 *data){
104 return (s8)readU8(data);
107 inline void writeF1000(u8 *data, f32 i){
108 writeS32(data, i*1000);
110 inline f32 readF1000(u8 *data){
111 return (f32)readS32(data)/1000.;
114 inline void writeV3S32(u8 *data, v3s32 p)
116 writeS32(&data[0], p.X);
117 writeS32(&data[4], p.Y);
118 writeS32(&data[8], p.Z);
120 inline v3s32 readV3S32(u8 *data)
123 p.X = readS32(&data[0]);
124 p.Y = readS32(&data[4]);
125 p.Z = readS32(&data[8]);
129 inline void writeV3F1000(u8 *data, v3f p)
131 writeF1000(&data[0], p.X);
132 writeF1000(&data[4], p.Y);
133 writeF1000(&data[8], p.Z);
135 inline v3f readV3F1000(u8 *data)
138 p.X = (float)readF1000(&data[0]);
139 p.Y = (float)readF1000(&data[4]);
140 p.Z = (float)readF1000(&data[8]);
144 inline void writeV2F1000(u8 *data, v2f p)
146 writeF1000(&data[0], p.X);
147 writeF1000(&data[4], p.Y);
149 inline v2f readV2F1000(u8 *data)
152 p.X = (float)readF1000(&data[0]);
153 p.Y = (float)readF1000(&data[4]);
157 inline void writeV2S16(u8 *data, v2s16 p)
159 writeS16(&data[0], p.X);
160 writeS16(&data[2], p.Y);
163 inline v2s16 readV2S16(u8 *data)
166 p.X = readS16(&data[0]);
167 p.Y = readS16(&data[2]);
171 inline void writeV2S32(u8 *data, v2s32 p)
173 writeS32(&data[0], p.X);
174 writeS32(&data[2], p.Y);
177 inline v2s32 readV2S32(u8 *data)
180 p.X = readS32(&data[0]);
181 p.Y = readS32(&data[2]);
185 inline void writeV3S16(u8 *data, v3s16 p)
187 writeS16(&data[0], p.X);
188 writeS16(&data[2], p.Y);
189 writeS16(&data[4], p.Z);
192 inline v3s16 readV3S16(u8 *data)
195 p.X = readS16(&data[0]);
196 p.Y = readS16(&data[2]);
197 p.Z = readS16(&data[4]);
201 inline void writeARGB8(u8 *data, video::SColor p)
203 writeU8(&data[0], p.getAlpha());
204 writeU8(&data[1], p.getRed());
205 writeU8(&data[2], p.getGreen());
206 writeU8(&data[3], p.getBlue());
209 inline video::SColor readARGB8(u8 *data)
212 p.setAlpha(readU8(&data[0]));
213 p.setRed(readU8(&data[1]));
214 p.setGreen(readU8(&data[2]));
215 p.setBlue(readU8(&data[3]));
220 The above stuff directly interfaced to iostream
223 inline void writeU8(std::ostream &os, u8 p)
226 writeU8((u8*)buf, p);
229 inline u8 readU8(std::istream &is)
233 return readU8((u8*)buf);
236 inline void writeU16(std::ostream &os, u16 p)
239 writeU16((u8*)buf, p);
242 inline u16 readU16(std::istream &is)
246 return readU16((u8*)buf);
249 inline void writeU32(std::ostream &os, u32 p)
252 writeU32((u8*)buf, p);
255 inline u32 readU32(std::istream &is)
259 return readU32((u8*)buf);
262 inline void writeS32(std::ostream &os, s32 p)
265 writeS32((u8*)buf, p);
268 inline s32 readS32(std::istream &is)
272 return readS32((u8*)buf);
275 inline void writeS16(std::ostream &os, s16 p)
278 writeS16((u8*)buf, p);
281 inline s16 readS16(std::istream &is)
285 return readS16((u8*)buf);
288 inline void writeS8(std::ostream &os, s8 p)
291 writeS8((u8*)buf, p);
294 inline s8 readS8(std::istream &is)
298 return readS8((u8*)buf);
301 inline void writeF1000(std::ostream &os, f32 p)
304 writeF1000((u8*)buf, p);
307 inline f32 readF1000(std::istream &is)
311 return readF1000((u8*)buf);
314 inline void writeV3F1000(std::ostream &os, v3f p)
317 writeV3F1000((u8*)buf, p);
320 inline v3f readV3F1000(std::istream &is)
324 return readV3F1000((u8*)buf);
327 inline void writeV2F1000(std::ostream &os, v2f p)
330 writeV2F1000((u8*)buf, p);
333 inline v2f readV2F1000(std::istream &is)
337 return readV2F1000((u8*)buf);
340 inline void writeV2S16(std::ostream &os, v2s16 p)
343 writeV2S16((u8*)buf, p);
346 inline v2s16 readV2S16(std::istream &is)
350 return readV2S16((u8*)buf);
353 inline void writeV3S16(std::ostream &os, v3s16 p)
356 writeV3S16((u8*)buf, p);
359 inline v3s16 readV3S16(std::istream &is)
363 return readV3S16((u8*)buf);
366 inline void writeARGB8(std::ostream &os, video::SColor p)
369 writeARGB8((u8*)buf, p);
373 inline video::SColor readARGB8(std::istream &is)
377 return readARGB8((u8*)buf);
381 More serialization stuff
384 // Creates a string with the length as the first two bytes
385 inline std::string serializeString(const std::string &plain)
387 //assert(plain.size() <= 65535);
388 if(plain.size() > 65535)
389 throw SerializationError("String too long for serializeString");
391 writeU16((u8*)&buf[0], plain.size());
398 // Creates a string with the length as the first two bytes from wide string
399 inline std::string serializeWideString(const std::wstring &plain)
401 //assert(plain.size() <= 65535);
402 if(plain.size() > 65535)
403 throw SerializationError("String too long for serializeString");
405 writeU16((u8*)buf, plain.size());
408 for(u32 i=0; i<plain.size(); i++)
410 writeU16((u8*)buf, plain[i]);
416 // Reads a string with the length as the first two bytes
417 inline std::string deSerializeString(std::istream &is)
422 throw SerializationError("deSerializeString: size not read");
423 u16 s_size = readU16((u8*)buf);
426 Buffer<char> buf2(s_size);
427 is.read(&buf2[0], s_size);
430 s.append(&buf2[0], s_size);
434 // Reads a wide string with the length as the first two bytes
435 inline std::wstring deSerializeWideString(std::istream &is)
440 throw SerializationError("deSerializeString: size not read");
441 u16 s_size = readU16((u8*)buf);
446 for(u32 i=0; i<s_size; i++)
449 wchar_t c16 = readU16((u8*)buf);
455 // Creates a string with the length as the first four bytes
456 inline std::string serializeLongString(const std::string &plain)
459 writeU32((u8*)&buf[0], plain.size());
466 // Reads a string with the length as the first four bytes
467 inline std::string deSerializeLongString(std::istream &is)
472 throw SerializationError("deSerializeLongString: size not read");
473 u32 s_size = readU32((u8*)buf);
476 Buffer<char> buf2(s_size);
477 is.read(&buf2[0], s_size);
480 s.append(&buf2[0], s_size);
484 // Creates a string encoded in JSON format (almost equivalent to a C string literal)
485 std::string serializeJsonString(const std::string &plain);
487 // Reads a string encoded in JSON format
488 std::string deSerializeJsonString(std::istream &is);