Mercurial > hg > early-roguelike
comparison xrogue/state.c @ 142:6b5fbd7c3ece
Merge arogue7 and xrogue trees.
| author | John "Elwin" Edwards |
|---|---|
| date | Tue, 12 May 2015 21:39:39 -0400 |
| parents | cc5148bdf345 |
| children | 7faf4568c295 |
comparison
equal
deleted
inserted
replaced
| 132:66b0263af424 | 142:6b5fbd7c3ece |
|---|---|
| 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 <string.h> | |
| 73 #include <curses.h> | |
| 74 #include <sys/stat.h> | |
| 75 #include <stdio.h> | |
| 76 #include <stdarg.h> | |
| 77 #include <assert.h> | |
| 78 #include <fcntl.h> | |
| 79 #include <limits.h> | |
| 80 #include <time.h> | |
| 81 #include <signal.h> | |
| 82 #include "rogue.h" | |
| 83 #include "mach_dep.h" | |
| 84 | |
| 85 | |
| 86 #define READSTAT ((format_error == 0) && (read_error == 0)) | |
| 87 #define WRITESTAT (write_error == 0) | |
| 88 | |
| 89 int read_error = FALSE; | |
| 90 int write_error = FALSE; | |
| 91 int format_error = FALSE; | |
| 92 | |
| 93 int save_debug = FALSE; | |
| 94 #define DBG(x) {if (save_debug) rsPrintf x;} | |
| 95 | |
| 96 int | |
| 97 rsPrintf(char *fmt, ...) | |
| 98 { | |
| 99 va_list ap; | |
| 100 | |
| 101 va_start(ap, fmt); | |
| 102 vfprintf(stderr,fmt, ap); | |
| 103 va_end(ap); | |
| 104 | |
| 105 return(0); | |
| 106 } | |
| 107 | |
| 108 | |
| 109 void * | |
| 110 get_list_item(struct linked_list *l, int i) | |
| 111 { | |
| 112 int count = 0; | |
| 113 | |
| 114 while(l != NULL) | |
| 115 { | |
| 116 if (count == i) | |
| 117 return(l->l_data); | |
| 118 | |
| 119 l = l->l_next; | |
| 120 | |
| 121 count++; | |
| 122 } | |
| 123 | |
| 124 return(NULL); | |
| 125 } | |
| 126 | |
| 127 rs_write(FILE *savef, void *ptr, size_t size) | |
| 128 { | |
| 129 size_t i = 0; | |
| 130 | |
| 131 if (!write_error) | |
| 132 i = ENCWRITE(ptr,size,savef); | |
| 133 if (i != size) | |
| 134 write_error = TRUE; | |
| 135 | |
| 136 assert(write_error == 0); | |
| 137 return(WRITESTAT); | |
| 138 } | |
| 139 | |
| 140 int end_of_file = FALSE; | |
| 141 | |
| 142 rs_read(int inf, void *ptr, size_t size) | |
| 143 { | |
| 144 int actual; | |
| 145 end_of_file =FALSE; | |
| 146 if (!read_error && !format_error) | |
| 147 { | |
| 148 actual = ENCREAD(ptr, size, inf); | |
| 149 | |
| 150 if ((actual == 0) && (size != 0)) | |
| 151 end_of_file = TRUE; | |
| 152 } | |
| 153 | |
| 154 if (read_error){ | |
| 155 printf("read error has occurred. restore short-circuited.\n");abort();} | |
| 156 if (format_error) | |
| 157 {printf("game format invalid. restore short-circuited.\n");abort();} | |
| 158 | |
| 159 return(READSTAT); | |
| 160 } | |
| 161 | |
| 162 int big_endian = 0; | |
| 163 | |
| 164 rs_write_uint(FILE *savef, unsigned int c) | |
| 165 { | |
| 166 char bytes[4]; | |
| 167 char *buf = (char *) &c; | |
| 168 | |
| 169 if (big_endian) | |
| 170 { | |
| 171 bytes[3] = buf[0]; | |
| 172 bytes[2] = buf[1]; | |
| 173 bytes[1] = buf[2]; | |
| 174 bytes[0] = buf[3]; | |
| 175 buf = bytes; | |
| 176 } | |
| 177 | |
| 178 rs_write(savef, buf, 4); | |
| 179 | |
| 180 return(WRITESTAT); | |
| 181 } | |
| 182 | |
| 183 rs_write_int(FILE *savef, int c) | |
| 184 { | |
| 185 char bytes[4]; | |
| 186 char *buf = (char *) &c; | |
| 187 | |
| 188 if (big_endian) | |
| 189 { | |
| 190 bytes[3] = buf[0]; | |
| 191 bytes[2] = buf[1]; | |
| 192 bytes[1] = buf[2]; | |
| 193 bytes[0] = buf[3]; | |
| 194 buf = bytes; | |
| 195 } | |
| 196 | |
| 197 rs_write(savef, buf, 4); | |
| 198 | |
| 199 return(WRITESTAT); | |
| 200 } | |
| 201 | |
| 202 rs_write_ulong(FILE *savef, unsigned long c) | |
| 203 { | |
| 204 char bytes[4]; | |
| 205 char *buf = (char *)&c; | |
| 206 | |
| 207 if (big_endian) | |
| 208 { | |
| 209 bytes[3] = buf[0]; | |
| 210 bytes[2] = buf[1]; | |
| 211 bytes[1] = buf[2]; | |
| 212 bytes[0] = buf[3]; | |
| 213 buf = bytes; | |
| 214 } | |
| 215 | |
| 216 rs_write(savef, buf, 4); | |
| 217 | |
| 218 return(WRITESTAT); | |
| 219 } | |
| 220 | |
| 221 rs_write_long(FILE *savef, long c) | |
| 222 { | |
| 223 char bytes[4]; | |
| 224 char *buf = (char *)&c; | |
| 225 | |
| 226 if (big_endian) | |
| 227 { | |
| 228 bytes[3] = buf[0]; | |
| 229 bytes[2] = buf[1]; | |
| 230 bytes[1] = buf[2]; | |
| 231 bytes[0] = buf[3]; | |
| 232 buf = bytes; | |
| 233 } | |
| 234 | |
| 235 rs_write(savef, buf, 4); | |
| 236 | |
| 237 return(WRITESTAT); | |
| 238 } | |
| 239 | |
| 240 rs_write_boolean(FILE *savef, bool c) | |
| 241 { | |
| 242 char buf; | |
| 243 | |
| 244 if (c == 0) | |
| 245 buf = 0; | |
| 246 else | |
| 247 buf = 1; | |
| 248 | |
| 249 rs_write(savef, &buf, 1); | |
| 250 | |
| 251 return(WRITESTAT); | |
| 252 } | |
| 253 | |
| 254 rs_read_int(int inf, int *i) | |
| 255 { | |
| 256 char bytes[4]; | |
| 257 int input; | |
| 258 char *buf = (char *)&input; | |
| 259 | |
| 260 rs_read(inf, &input, 4); | |
| 261 | |
| 262 if (big_endian) | |
| 263 { | |
| 264 bytes[3] = buf[0]; | |
| 265 bytes[2] = buf[1]; | |
| 266 bytes[1] = buf[2]; | |
| 267 bytes[0] = buf[3]; | |
| 268 buf = bytes; | |
| 269 } | |
| 270 | |
| 271 *i = *((int *) buf); | |
| 272 | |
| 273 return(READSTAT); | |
| 274 } | |
| 275 | |
| 276 rs_read_uint(int inf, unsigned int *i) | |
| 277 { | |
| 278 char bytes[4]; | |
| 279 int input; | |
| 280 char *buf = (char *)&input; | |
| 281 | |
| 282 rs_read(inf, &input, 4); | |
| 283 | |
| 284 if (big_endian) | |
| 285 { | |
| 286 bytes[3] = buf[0]; | |
| 287 bytes[2] = buf[1]; | |
| 288 bytes[1] = buf[2]; | |
| 289 bytes[0] = buf[3]; | |
| 290 buf = bytes; | |
| 291 } | |
| 292 | |
| 293 *i = *((int *) buf); | |
| 294 | |
| 295 return(READSTAT); | |
| 296 } | |
| 297 | |
| 298 rs_read_ulong(int inf, unsigned long *i) | |
| 299 { | |
| 300 char bytes[4]; | |
| 301 unsigned long input; | |
| 302 char *buf = (char *) &input; | |
| 303 | |
| 304 rs_read(inf, &input, 4); | |
| 305 | |
| 306 if (big_endian) | |
| 307 { | |
| 308 bytes[3] = buf[0]; | |
| 309 bytes[2] = buf[1]; | |
| 310 bytes[1] = buf[2]; | |
| 311 bytes[0] = buf[3]; | |
| 312 buf = bytes; | |
| 313 } | |
| 314 | |
| 315 *i = *((unsigned long *) buf); | |
| 316 return(READSTAT); | |
| 317 } | |
| 318 | |
| 319 rs_read_long(int inf, long *i) | |
| 320 { | |
| 321 char bytes[4]; | |
| 322 long input; | |
| 323 char *buf = (char *) &input; | |
| 324 | |
| 325 rs_read(inf, &input, 4); | |
| 326 | |
| 327 if (big_endian) | |
| 328 { | |
| 329 bytes[3] = buf[0]; | |
| 330 bytes[2] = buf[1]; | |
| 331 bytes[1] = buf[2]; | |
| 332 bytes[0] = buf[3]; | |
| 333 buf = bytes; | |
| 334 } | |
| 335 | |
| 336 *i = *((long *) buf); | |
| 337 return(READSTAT); | |
| 338 } | |
| 339 | |
| 340 rs_read_boolean(int inf, bool *i) | |
| 341 { | |
| 342 char buf; | |
| 343 | |
| 344 rs_read(inf, &buf, 1); | |
| 345 | |
| 346 *i = buf; | |
| 347 | |
| 348 return(READSTAT); | |
| 349 } | |
| 350 | |
| 351 rs_write_ints(FILE *savef, int *c, int count) | |
| 352 { | |
| 353 int n=0; | |
| 354 | |
| 355 rs_write_int(savef,count); | |
| 356 | |
| 357 for(n=0;n<count;n++) | |
| 358 rs_write_int(savef,c[n]); | |
| 359 | |
| 360 return(WRITESTAT); | |
| 361 } | |
| 362 | |
| 363 rs_write_short(FILE *savef, short c) | |
| 364 { | |
| 365 char bytes[2]; | |
| 366 char *buf = (char *) &c; | |
| 367 | |
| 368 if (big_endian) | |
| 369 { | |
| 370 bytes[1] = buf[0]; | |
| 371 bytes[0] = buf[1]; | |
| 372 buf = bytes; | |
| 373 } | |
| 374 | |
| 375 rs_write(savef, buf, 2); | |
| 376 | |
| 377 return(WRITESTAT); | |
| 378 } | |
| 379 | |
| 380 rs_read_short(int inf, short *s) | |
| 381 { | |
| 382 char bytes[2]; | |
| 383 short input; | |
| 384 char *buf = (char *)&input; | |
| 385 | |
| 386 rs_read(inf, &input, 2); | |
| 387 |
