comparison xrogue/state.c @ 133:e6179860cb76

Import XRogue 8.0 from the Roguelike Restoration Project (r1490)
author John "Elwin" Edwards
date Tue, 21 Apr 2015 08:55:20 -0400
parents
children cfa9d1609b78
comparison
equal deleted inserted replaced
124:d10fc4a065ac 133:e6179860cb76
1 /*
2 state.c - Portable Rogue Save State Code
3
4 Copyright (C) 2000 Nicholas J. Kisseberth
5
6 Redistribution and use in source and binary forms, with or without
7 modification, are permitted provided that the following conditions
8 are met:
9 1. Redistributions of source code must retain the above copyright
10 notice, this list of conditions and the following disclaimer.
11 2. Redistributions in binary form must reproduce the above copyright
12 notice, this list of conditions and the following disclaimer in the
13 documentation and/or other materials provided with the distribution.
14 3. Neither the name(s) of the author(s) nor the names of other contributors
15 may be used to endorse or promote products derived from this software
16 without specific prior written permission.
17
18 THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) AND CONTRIBUTORS ``AS IS'' AND
19 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR(S) OR CONTRIBUTORS BE LIABLE
22 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 SUCH DAMAGE.
29 */
30
31 #define RSXR_STATS 0xABCD0001
32 #define RSXR_THING 0xABCD0002
33 #define RSXR_OBJECT 0xABCD0003
34 #define RSXR_MAGICITEMS 0xABCD0004
35 #define RSXR_KNOWS 0xABCD0005
36 #define RSXR_GUESSES 0xABCD0006
37 #define RSXR_OBJECTLIST 0xABCD0007
38 #define RSXR_BAGOBJECT 0xABCD0008
39 #define RSXR_MONSTERLIST 0xABCD0009
40 #define RSXR_MONSTERSTATS 0xABCD000A
41 #define RSXR_MONSTERS 0xABCD000B
42 #define RSXR_TRAP 0xABCD000C
43 #define RSXR_WINDOW 0xABCD000D
44 #define RSXR_DAEMONS 0xABCD000E
45 #define RSXR_IWEAPS 0xABCD000F
46 #define RSXR_IARMOR 0xABCD0010
47 #define RSXR_SPELLS 0xABCD0011
48 #define RSXR_ILIST 0xABCD0012
49 #define RSXR_HLIST 0xABCD0013
50 #define RSXR_DEATHTYPE 0xABCD0014
51 #define RSXR_CTYPES 0XABCD0015
52 #define RSXR_COORDLIST 0XABCD0016
53 #define RSXR_ROOMS 0XABCD0017
54
55 #if defined(_WIN32)
56 #include <Windows.h>
57 #include <Lmcons.h>
58 #include <process.h>
59 #include <shlobj.h>
60 #include <Shlwapi.h>
61 #undef VOID
62 #undef MOUSE_MOVED
63 #elif defined(__DJGPP__)
64 #include <process.h>
65 #else
66 #include <pwd.h>
67 #include <sys/utsname.h>
68 #include <unistd.h>
69 #endif
70
71 #include <stdlib.h>
72 #include <curses.h>
73 #include <sys/stat.h>
74 #include <stdio.h>
75 #include <stdarg.h>
76 #include <assert.h>
77 #include <fcntl.h>
78 #include <limits.h>
79 #include <time.h>
80 #include <signal.h>
81 #include "rogue.h"
82 #include "mach_dep.h"
83
84
85 #define READSTAT ((format_error == 0) && (read_error == 0))
86 #define WRITESTAT (write_error == 0)
87
88 int read_error = FALSE;
89 int write_error = FALSE;
90 int format_error = FALSE;
91
92 int save_debug = FALSE;
93 #define DBG(x) {if (save_debug) rsPrintf x;}
94
95 int
96 rsPrintf(char *fmt, ...)
97 {
98 va_list ap;
99
100 va_start(ap, fmt);
101 vfprintf(stderr,fmt, ap);
102 va_end(ap);
103
104 return(0);
105 }
106
107
108 void *
109 get_list_item(struct linked_list *l, int i)
110 {
111 int count = 0;
112
113 while(l != NULL)
114 {
115 if (count == i)
116 return(l->l_data);
117
118 l = l->l_next;
119
120 count++;
121 }
122
123 return(NULL);
124 }
125
126 rs_write(FILE *savef, void *ptr, size_t size)
127 {
128 size_t i = 0;
129
130 if (!write_error)
131 i = ENCWRITE(ptr,size,savef);
132 if (i != size)
133 write_error = TRUE;
134
135 assert(write_error == 0);
136 return(WRITESTAT);
137 }
138
139 int end_of_file = FALSE;
140
141 rs_read(int inf, void *ptr, size_t size)
142 {
143 int actual;
144 end_of_file =FALSE;
145 if (!read_error && !format_error)
146 {
147 actual = ENCREAD(ptr, size, inf);
148
149 if ((actual == 0) && (size != 0))
150 end_of_file = TRUE;
151 }
152
153 if (read_error){
154 printf("read error has occurred. restore short-circuited.\n");abort();}
155 if (format_error)
156 {printf("game format invalid. restore short-circuited.\n");abort();}
157
158 return(READSTAT);
159 }
160
161 int big_endian = 0;
162
163 rs_write_uint(FILE *savef, unsigned int c)
164 {
165 char bytes[4];
166 char *buf = (char *) &c;
167
168 if (big_endian)
169 {
170 bytes[3] = buf[0];
171 bytes[2] = buf[1];
172 bytes[1] = buf[2];
173 bytes[0] = buf[3];
174 buf = bytes;
175 }
176
177 rs_write(savef, buf, 4);
178
179 return(WRITESTAT);
180 }
181
182 rs_write_int(FILE *savef, int c)
183 {
184 char bytes[4];
185 char *buf = (char *) &c;
186
187 if (big_endian)
188 {
189 bytes[3] = buf[0];
190 bytes[2] = buf[1];
191 bytes[1] = buf[2];
192 bytes[0] = buf[3];
193 buf = bytes;
194 }
195
196 rs_write(savef, buf, 4);
197
198 return(WRITESTAT);
199 }
200
201 rs_write_ulong(FILE *savef, unsigned long c)
202 {
203 char bytes[4];
204 char *buf = (char *)&c;
205
206 if (big_endian)
207 {
208 bytes[3] = buf[0];
209 bytes[2] = buf[1];
210 bytes[1] = buf[2];
211 bytes[0] = buf[3];
212 buf = bytes;
213 }
214
215 rs_write(savef, buf, 4);
216
217 return(WRITESTAT);
218 }
219
220 rs_write_long(FILE *savef, long c)
221 {
222 char bytes[4];
223 char *buf = (char *)&c;
224
225 if (big_endian)
226 {
227 bytes[3] = buf[0];
228 bytes[2] = buf[1];
229 bytes[1] = buf[2];
230 bytes[0] = buf[3];
231 buf = bytes;
232 }
233
234 rs_write(savef, buf, 4);
235
236 return(WRITESTAT);
237 }
238
239 rs_write_boolean(FILE *savef, bool c)
240 {
241 char buf;
242
243 if (c == 0)
244 buf = 0;
245 else
246 buf = 1;
247
248 rs_write(savef, &buf, 1);
249
250 return(WRITESTAT);
251 }
252
253 rs_read_int(int inf, int *i)
254 {
255 char bytes[4];
256 int input;
257 char *buf = (char *)&input;
258
259 rs_read(inf, &input, 4);
260
261 if (big_endian)
262 {
263 bytes[3] = buf[0];
264 bytes[2] = buf[1];
265 bytes[1] = buf[2];
266 bytes[0] = buf[3];
267 buf = bytes;
268 }
269
270 *i = *((int *) buf);
271
272 return(READSTAT);
273 }
274
275 rs_read_uint(int inf, unsigned int *i)
276 {
277 char bytes[4];
278 int input;
279 char *buf = (char *)&input;
280
281 rs_read(inf, &input, 4);
282
283 if (big_endian)
284 {
285 bytes[3] = buf[0];
286 bytes[2] = buf[1];
287 bytes[1] = buf[2];
288 bytes[0] = buf[3];
289 buf = bytes;
290 }
291
292 *i = *((int *) buf);
293
294 return(READSTAT);
295 }
296
297 rs_read_ulong(int inf, unsigned long *i)
298 {
299 char bytes[4];
300 unsigned long input;
301 char *buf = (char *) &input;
302
303 rs_read(inf, &input, 4);
304
305 if (big_endian)
306 {
307 bytes[3] = buf[0];
308 bytes[2] = buf[1];
309 bytes[1] = buf[2];
310 bytes[0] = buf[3];
311 buf = bytes;
312 }
313
314 *i = *((unsigned long *) buf);
315 return(READSTAT);
316 }
317
318 rs_read_long(int inf, long *i)
319 {
320 char bytes[4];
321 long input;
322 char *buf = (char *) &input;
323
324 rs_read(inf, &input, 4);
325
326 if (big_endian)
327 {
328 bytes[3] = buf[0];
329 bytes[2] = buf[1];
330 bytes[1] = buf[2];
331 bytes[0] = buf[3];
332 buf = bytes;
333 }
334
335 *i = *((long *) buf);
336 return(READSTAT);
337 }
338
339 rs_read_boolean(int inf, bool *i)
340 {
341 char buf;
342
343 rs_read(inf, &buf, 1);
344
345 *i = buf;
346
347 return(READSTAT);
348 }
349
350 rs_write_ints(FILE *savef, int *c, int count)
351 {
352 int n=0;
353
354 rs_write_int(savef,count);
355
356 for(n=0;n<count;n++)
357 rs_write_int(savef,c[n]);
358
359 return(WRITESTAT);
360 }
361
362 rs_write_short(FILE *savef, short c)
363 {
364 char bytes[2];
365 char *buf = (char *) &c;
366
367 if (big_endian)
368 {
369 bytes[1] = buf[0];
370 bytes[0] = buf[1];
371 buf = bytes;
372 }
373
374 rs_write(savef, buf, 2);
375
376 return(WRITESTAT);
377 }
378
379 rs_read_short(int inf, short *s)
380 {
381 char bytes[2];
382 short input;
383 char *buf = (char *)&input;
384
385 rs_read(inf, &input, 2);
386
387 if (big_endian)