]> git.lizzy.rs Git - dragonfireclient.git/blob - src/util/serialize.h
Omnicleanup: header cleanup, add ModApiUtil shared between game and mainmenu
[dragonfireclient.git] / src / util / serialize.h
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 #ifndef UTIL_SERIALIZE_HEADER
21 #define UTIL_SERIALIZE_HEADER
22
23 #include "../irrlichttypes_bloated.h"
24 #include <iostream>
25 #include <string>
26
27 inline void writeU64(u8 *data, u64 i)
28 {
29         data[0] = ((i>>56)&0xff);
30         data[1] = ((i>>48)&0xff);
31         data[2] = ((i>>40)&0xff);
32         data[3] = ((i>>32)&0xff);
33         data[4] = ((i>>24)&0xff);
34         data[5] = ((i>>16)&0xff);
35         data[6] = ((i>> 8)&0xff);
36         data[7] = ((i>> 0)&0xff);
37 }
38
39 inline void writeU32(u8 *data, u32 i)
40 {
41         data[0] = ((i>>24)&0xff);
42         data[1] = ((i>>16)&0xff);
43         data[2] = ((i>> 8)&0xff);
44         data[3] = ((i>> 0)&0xff);
45 }
46
47 inline void writeU16(u8 *data, u16 i)
48 {
49         data[0] = ((i>> 8)&0xff);
50         data[1] = ((i>> 0)&0xff);
51 }
52
53 inline void writeU8(u8 *data, u8 i)
54 {
55         data[0] = ((i>> 0)&0xff);
56 }
57
58 inline u64 readU64(const u8 *data)
59 {
60         return ((u64)data[0]<<56) | ((u64)data[1]<<48)
61                 | ((u64)data[2]<<40) | ((u64)data[3]<<32)
62                 | ((u64)data[4]<<24) | ((u64)data[5]<<16)
63                 | ((u64)data[6]<<8) | ((u64)data[7]<<0);
64 }
65
66 inline u32 readU32(const u8 *data)
67 {
68         return (data[0]<<24) | (data[1]<<16) | (data[2]<<8) | (data[3]<<0);
69 }
70
71 inline u16 readU16(const u8 *data)
72 {
73         return (data[0]<<8) | (data[1]<<0);
74 }
75
76 inline u8 readU8(const u8 *data)
77 {
78         return (data[0]<<0);
79 }
80
81 inline void writeS32(u8 *data, s32 i){
82         writeU32(data, (u32)i);
83 }
84 inline s32 readS32(const u8 *data){
85         return (s32)readU32(data);
86 }
87
88 inline void writeS16(u8 *data, s16 i){
89         writeU16(data, (u16)i);
90 }
91 inline s16 readS16(const u8 *data){
92         return (s16)readU16(data);
93 }
94
95 inline void writeS8(u8 *data, s8 i){
96         writeU8(data, (u8)i);
97 }
98 inline s8 readS8(const u8 *data){
99         return (s8)readU8(data);
100 }
101
102 inline void writeF1000(u8 *data, f32 i){
103         writeS32(data, i*1000);
104 }
105 inline f32 readF1000(const u8 *data){
106         return (f32)readS32(data)/1000.;
107 }
108
109 inline void writeV3S32(u8 *data, v3s32 p)
110 {
111         writeS32(&data[0], p.X);
112         writeS32(&data[4], p.Y);
113         writeS32(&data[8], p.Z);
114 }
115 inline v3s32 readV3S32(const u8 *data)
116 {
117         v3s32 p;
118         p.X = readS32(&data[0]);
119         p.Y = readS32(&data[4]);
120         p.Z = readS32(&data[8]);
121         return p;
122 }
123
124 inline void writeV3F1000(u8 *data, v3f p)
125 {
126         writeF1000(&data[0], p.X);
127         writeF1000(&data[4], p.Y);
128         writeF1000(&data[8], p.Z);
129 }
130 inline v3f readV3F1000(const u8 *data)
131 {
132         v3f p;
133         p.X = (float)readF1000(&data[0]);
134         p.Y = (float)readF1000(&data[4]);
135         p.Z = (float)readF1000(&data[8]);
136         return p;
137 }
138
139 inline void writeV2F1000(u8 *data, v2f p)
140 {
141         writeF1000(&data[0], p.X);
142         writeF1000(&data[4], p.Y);
143 }
144 inline v2f readV2F1000(const u8 *data)
145 {
146         v2f p;
147         p.X = (float)readF1000(&data[0]);
148         p.Y = (float)readF1000(&data[4]);
149         return p;
150 }
151
152 inline void writeV2S16(u8 *data, v2s16 p)
153 {
154         writeS16(&data[0], p.X);
155         writeS16(&data[2], p.Y);
156 }
157
158 inline v2s16 readV2S16(const u8 *data)
159 {
160         v2s16 p;
161         p.X = readS16(&data[0]);
162         p.Y = readS16(&data[2]);
163         return p;
164 }
165
166 inline void writeV2S32(u8 *data, v2s32 p)
167 {
168         writeS32(&data[0], p.X);
169         writeS32(&data[2], p.Y);
170 }
171
172 inline v2s32 readV2S32(const u8 *data)
173 {
174         v2s32 p;
175         p.X = readS32(&data[0]);
176         p.Y = readS32(&data[2]);
177         return p;
178 }
179
180 inline void writeV3S16(u8 *data, v3s16 p)
181 {
182         writeS16(&data[0], p.X);
183         writeS16(&data[2], p.Y);
184         writeS16(&data[4], p.Z);
185 }
186
187 inline v3s16 readV3S16(const u8 *data)
188 {
189         v3s16 p;
190         p.X = readS16(&data[0]);
191         p.Y = readS16(&data[2]);
192         p.Z = readS16(&data[4]);
193         return p;
194 }
195
196 inline void writeARGB8(u8 *data, video::SColor p)
197 {
198         writeU8(&data[0], p.getAlpha());
199         writeU8(&data[1], p.getRed());
200         writeU8(&data[2], p.getGreen());
201         writeU8(&data[3], p.getBlue());
202 }
203
204 inline video::SColor readARGB8(const u8 *data)
205 {
206         video::SColor p(
207                 readU8(&data[0]),
208                 readU8(&data[1]),
209                 readU8(&data[2]),
210                 readU8(&data[3])
211         );
212         return p;
213 }
214
215 /*
216         The above stuff directly interfaced to iostream
217 */
218
219 inline void writeU8(std::ostream &os, u8 p)
220 {
221         char buf[1] = {0};
222         writeU8((u8*)buf, p);
223         os.write(buf, 1);
224 }
225 inline u8 readU8(std::istream &is)
226 {
227         char buf[1] = {0};
228         is.read(buf, 1);
229         return readU8((u8*)buf);
230 }
231
232 inline void writeU16(std::ostream &os, u16 p)
233 {
234         char buf[2] = {0};
235         writeU16((u8*)buf, p);
236         os.write(buf, 2);
237 }
238 inline u16 readU16(std::istream &is)
239 {
240         char buf[2] = {0};
241         is.read(buf, 2);
242         return readU16((u8*)buf);
243 }
244
245 inline void writeU32(std::ostream &os, u32 p)
246 {
247         char buf[4] = {0};
248         writeU32((u8*)buf, p);
249         os.write(buf, 4);
250 }
251 inline u32 readU32(std::istream &is)
252 {
253         char buf[4] = {0};
254         is.read(buf, 4);
255         return readU32((u8*)buf);
256 }
257
258 inline void writeS32(std::ostream &os, s32 p)
259 {
260         char buf[4] = {0};
261         writeS32((u8*)buf, p);
262         os.write(buf, 4);
263 }
264 inline s32 readS32(std::istream &is)
265 {
266         char buf[4] = {0};
267         is.read(buf, 4);
268         return readS32((u8*)buf);
269 }
270
271 inline void writeS16(std::ostream &os, s16 p)
272 {
273         char buf[2] = {0};
274         writeS16((u8*)buf, p);
275         os.write(buf, 2);
276 }
277 inline s16 readS16(std::istream &is)
278 {
279         char buf[2] = {0};
280         is.read(buf, 2);
281         return readS16((u8*)buf);
282 }
283
284 inline void writeS8(std::ostream &os, s8 p)
285 {
286         char buf[1] = {0};
287         writeS8((u8*)buf, p);
288         os.write(buf, 1);
289 }
290 inline s8 readS8(std::istream &is)
291 {
292         char buf[1] = {0};
293         is.read(buf, 1);
294         return readS8((u8*)buf);
295 }
296
297 inline void writeF1000(std::ostream &os, f32 p)
298 {
299         char buf[4] = {0};
300         writeF1000((u8*)buf, p);
301         os.write(buf, 4);
302 }
303 inline f32 readF1000(std::istream &is)
304 {
305         char buf[4] = {0};
306         is.read(buf, 4);
307         return readF1000((u8*)buf);
308 }
309
310 inline void writeV3F1000(std::ostream &os, v3f p)
311 {
312         char buf[12];
313         writeV3F1000((u8*)buf, p);
314         os.write(buf, 12);
315 }
316 inline v3f readV3F1000(std::istream &is)
317 {
318         char buf[12];
319         is.read(buf, 12);
320         return readV3F1000((u8*)buf);
321 }
322
323 inline void writeV2F1000(std::ostream &os, v2f p)
324 {
325         char buf[8] = {0};
326         writeV2F1000((u8*)buf, p);
327         os.write(buf, 8);
328 }
329 inline v2f readV2F1000(std::istream &is)
330 {
331         char buf[8] = {0};
332         is.read(buf, 8);
333         return readV2F1000((u8*)buf);
334 }
335
336 inline void writeV2S16(std::ostream &os, v2s16 p)
337 {
338         char buf[4] = {0};
339         writeV2S16((u8*)buf, p);
340         os.write(buf, 4);
341 }
342 inline v2s16 readV2S16(std::istream &is)
343 {
344         char buf[4] = {0};
345         is.read(buf, 4);
346         return readV2S16((u8*)buf);
347 }
348
349 inline void writeV3S16(std::ostream &os, v3s16 p)
350 {
351         char buf[6] = {0};
352         writeV3S16((u8*)buf, p);
353         os.write(buf, 6);
354 }
355 inline v3s16 readV3S16(std::istream &is)
356 {
357         char buf[6] = {0};
358         is.read(buf, 6);
359         return readV3S16((u8*)buf);
360 }
361
362 inline void writeARGB8(std::ostream &os, video::SColor p)
363 {
364         char buf[4] = {0};
365         writeARGB8((u8*)buf, p);
366         os.write(buf, 4);
367 }
368
369 inline video::SColor readARGB8(std::istream &is)
370 {
371         char buf[4] = {0};
372         is.read(buf, 4);
373         return readARGB8((u8*)buf);
374 }
375
376 /*
377         More serialization stuff
378 */
379
380 // Creates a string with the length as the first two bytes
381 std::string serializeString(const std::string &plain);
382
383 // Creates a string with the length as the first two bytes from wide string
384 std::string serializeWideString(const std::wstring &plain);
385
386 // Reads a string with the length as the first two bytes
387 std::string deSerializeString(std::istream &is);
388
389 // Reads a wide string with the length as the first two bytes
390 std::wstring deSerializeWideString(std::istream &is);
391
392 // Creates a string with the length as the first four bytes
393 std::string serializeLongString(const std::string &plain);
394
395 // Reads a string with the length as the first four bytes
396 std::string deSerializeLongString(std::istream &is);
397
398 // Creates a string encoded in JSON format (almost equivalent to a C string literal)
399 std::string serializeJsonString(const std::string &plain);
400
401 // Reads a string encoded in JSON format
402 std::string deSerializeJsonString(std::istream &is);
403
404 #endif
405