diff options
Diffstat (limited to 'apps/plugins/xworld/serializer.c')
-rw-r--r-- | apps/plugins/xworld/serializer.c | 141 |
1 files changed, 141 insertions, 0 deletions
diff --git a/apps/plugins/xworld/serializer.c b/apps/plugins/xworld/serializer.c new file mode 100644 index 0000000000..5268c2784c --- /dev/null +++ b/apps/plugins/xworld/serializer.c | |||
@@ -0,0 +1,141 @@ | |||
1 | /*************************************************************************** | ||
2 | * __________ __ ___. | ||
3 | * Open \______ \ ____ ____ | | _\_ |__ _______ ___ | ||
4 | * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / | ||
5 | * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < | ||
6 | * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ | ||
7 | * \/ \/ \/ \/ \/ | ||
8 | * $Id$ | ||
9 | * | ||
10 | * Copyright (C) 2014 Franklin Wei, Benjamin Brown | ||
11 | * Copyright (C) 2004 Gregory Montoir | ||
12 | * | ||
13 | * This program is free software; you can redistribute it and/or | ||
14 | * modify it under the terms of the GNU General Public License | ||
15 | * as published by the Free Software Foundation; either version 2 | ||
16 | * of the License, or (at your option) any later version. | ||
17 | * | ||
18 | * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY | ||
19 | * KIND, either express or implied. | ||
20 | * | ||
21 | ***************************************************************************/ | ||
22 | |||
23 | #include "serializer.h" | ||
24 | #include "file.h" | ||
25 | |||
26 | |||
27 | void ser_create(struct Serializer* c, File *stream, enum Mode mode, uint8_t *ptrBlock, uint16_t saveVer) | ||
28 | { | ||
29 | c->_stream = stream; | ||
30 | c->_mode = mode; | ||
31 | c->_ptrBlock = ptrBlock; | ||
32 | c->_saveVer = saveVer; | ||
33 | } | ||
34 | |||
35 | void ser_saveOrLoadEntries(struct Serializer* c, struct Entry *entry) { | ||
36 | debug(DBG_SER, "ser_saveOrLoadEntries() _mode=%d", c->_mode); | ||
37 | c->_bytesCount = 0; | ||
38 | switch (c->_mode) { | ||
39 | case SM_SAVE: | ||
40 | ser_saveEntries(c, entry); | ||
41 | break; | ||
42 | case SM_LOAD: | ||
43 | ser_loadEntries(c, entry); | ||
44 | break; | ||
45 | } | ||
46 | debug(DBG_SER, "ser_saveOrLoadEntries() _bytesCount=%d", c->_bytesCount); | ||
47 | } | ||
48 | |||
49 | void ser_saveEntries(struct Serializer* c, struct Entry *entry) { | ||
50 | debug(DBG_SER, "ser_saveEntries()"); | ||
51 | for (; entry->type != SET_END; ++entry) { | ||
52 | if (entry->maxVer == CUR_VER) { | ||
53 | switch (entry->type) { | ||
54 | case SET_INT: | ||
55 | ser_saveInt(c, entry->size, entry->data); | ||
56 | c->_bytesCount += entry->size; | ||
57 | break; | ||
58 | case SET_ARRAY: | ||
59 | if (entry->size == SES_INT8) { | ||
60 | file_write(c->_stream, entry->data, entry->n); | ||
61 | c->_bytesCount += entry->n; | ||
62 | } else { | ||
63 | uint8_t *p = (uint8_t *)entry->data; | ||
64 | for (int i = 0; i < entry->n; ++i) { | ||
65 | ser_saveInt(c, entry->size, p); | ||
66 | p += entry->size; | ||
67 | c->_bytesCount += entry->size; | ||
68 | } | ||
69 | } | ||
70 | break; | ||
71 | case SET_PTR: | ||
72 | file_writeUint32BE(c->_stream, *(uint8_t **)(entry->data) - c->_ptrBlock); | ||
73 | c->_bytesCount += 4; | ||
74 | break; | ||
75 | case SET_END: | ||
76 | break; | ||
77 | } | ||
78 | } | ||
79 | } | ||
80 | } | ||
81 | |||
82 | void ser_loadEntries(struct Serializer* c, struct Entry *entry) { | ||
83 | debug(DBG_SER, "ser_loadEntries()"); | ||
84 | for (; entry->type != SET_END; ++entry) { | ||
85 | if (c->_saveVer >= entry->minVer && c->_saveVer <= entry->maxVer) { | ||
86 | switch (entry->type) { | ||
87 | case SET_INT: | ||
88 | ser_loadInt(c, entry->size, entry->data); | ||
89 | c->_bytesCount += entry->size; | ||
90 | break; | ||
91 | case SET_ARRAY: | ||
92 | if (entry->size == SES_INT8) { | ||
93 | file_read(c->_stream, entry->data, entry->n); | ||
94 | c->_bytesCount += entry->n; | ||
95 | } else { | ||
96 | uint8_t *p = (uint8_t *)entry->data; | ||
97 | for (int i = 0; i < entry->n; ++i) { | ||
98 | ser_loadInt(c, entry->size, p); | ||
99 | p += entry->size; | ||
100 | c->_bytesCount += entry->size; | ||
101 | } | ||
102 | } | ||
103 | break; | ||
104 | case SET_PTR: | ||
105 | *(uint8_t **)(entry->data) = c->_ptrBlock + file_readUint32BE(c->_stream); | ||
106 | c->_bytesCount += 4; | ||
107 | break; | ||
108 | case SET_END: | ||
109 | break; | ||
110 | } | ||
111 | } | ||
112 | } | ||
113 | } | ||
114 | |||
115 | void ser_saveInt(struct Serializer* c, uint8_t es, void *p) { | ||
116 | switch (es) { | ||
117 | case 1: | ||
118 | file_writeByte(c->_stream, *(uint8_t *)p); | ||
119 | break; | ||
120 | case 2: | ||
121 | file_writeUint16BE(c->_stream, *(uint16_t *)p); | ||
122 | break; | ||
123 | case 4: | ||
124 | file_writeUint32BE(c->_stream, *(uint32_t *)p); | ||
125 | break; | ||
126 | } | ||
127 | } | ||
128 | |||
129 | void ser_loadInt(struct Serializer* c, uint8_t es, void *p) { | ||
130 | switch (es) { | ||
131 | case 1: | ||
132 | *(uint8_t *)p = file_readByte(c->_stream); | ||
133 | break; | ||
134 | case 2: | ||
135 | *(uint16_t *)p = file_readUint16BE(c->_stream); | ||
136 | break; | ||
137 | case 4: | ||
138 | *(uint32_t *)p = file_readUint32BE(c->_stream); | ||
139 | break; | ||
140 | } | ||
141 | } | ||