| 1 | #ifdef RCSID |
| 2 | static char RCSid[] = |
| 3 | "$Header$"; |
| 4 | #endif |
| 5 | |
| 6 | /* |
| 7 | * Copyright (c) 1999, 2002 Michael J. Roberts. All Rights Reserved. |
| 8 | * |
| 9 | * Please see the accompanying license file, LICENSE.TXT, for information |
| 10 | * on using and copying this software. |
| 11 | */ |
| 12 | /* |
| 13 | Name |
| 14 | vmsave.cpp - T3 save/restore state |
| 15 | Function |
| 16 | |
| 17 | Notes |
| 18 | |
| 19 | Modified |
| 20 | 08/02/99 MJRoberts - Creation |
| 21 | */ |
| 22 | |
| 23 | /* |
| 24 | * Saved game header structure: |
| 25 | * |
| 26 | *. <17 bytes> - signature, "T3-state-vXXX\015\012\032", where XXX is |
| 27 | *. the hex version number |
| 28 | *. UINT4 - stream size, counting from the timestamp on |
| 29 | *. UINT4 - CRC32 checksum of stream, from the timestamp on |
| 30 | *. <24 bytes> - timestamp of the game file, to ensure that this saved |
| 31 | *. state file is only applied to the correct image file |
| 32 | *. UINT2 - length of filename in bytes |
| 33 | *. <? bytes> - image file name; length given by preceding UINT2 |
| 34 | *. UINT2 - number of bytes in metadata table |
| 35 | *. <? bytes> - metadata table |
| 36 | *. <? bytes> - object stream data |
| 37 | * |
| 38 | * The metadata table contains any number of name/value string pairs. |
| 39 | * These are arbitrary values that allow the game to store descriptive |
| 40 | * information about the saved game that the interpreter and other tools |
| 41 | * can display to the user, to jog the user's memory when reviewing a |
| 42 | * collection of saved game files. The format of the table is: |
| 43 | * |
| 44 | *. UINT2 - number of string/value pairs |
| 45 | *. <? bytes> - first pair |
| 46 | *. <? bytes> - second pair |
| 47 | *. ...etc... |
| 48 | * |
| 49 | * Each pair has this format: |
| 50 | * |
| 51 | *. UINT2 - length of the name string |
| 52 | *. UINT2 - length of the value string |
| 53 | *. <? bytes> - name string |
| 54 | *. <? bytes> - value string |
| 55 | */ |
| 56 | |
| 57 | #include "t3std.h" |
| 58 | #include "os.h" |
| 59 | #include "vmglob.h" |
| 60 | #include "vmsave.h" |
| 61 | #include "vmfile.h" |
| 62 | #include "vmimage.h" |
| 63 | #include "vmobj.h" |
| 64 | #include "vmrun.h" |
| 65 | #include "vmstack.h" |
| 66 | #include "vmundo.h" |
| 67 | #include "vmmeta.h" |
| 68 | #include "vmcrc.h" |
| 69 | #include "vmlookup.h" |
| 70 | #include "vmstr.h" |
| 71 | |
| 72 | |
| 73 | /* ------------------------------------------------------------------------ */ |
| 74 | /* |
| 75 | * Saved state signature. This signature is tied to a specific format |
| 76 | * version; it should be changed whenever the format version is modified |
| 77 | * so that it is incompatible with older versions. |
| 78 | * |
| 79 | * Note that incompatible changes to intrinsic class serialization formats |
| 80 | * will require updating the version number. |
| 81 | * |
| 82 | * Incompatible changes to the format are not a particularly big deal. |
| 83 | * Saved states tend to be local to a particular machine, since they're |
| 84 | * mostly used to suspend sessions for later resumption and to "branch" |
| 85 | * the state evolution (i.e., to allow playing a game from a particular |
| 86 | * point, then returning later to that same point to play again, but doing |
| 87 | * different things this time; this is used particularly to save "good" |
| 88 | * positions as a precaution against later getting into unwinnable |
| 89 | * states). |
| 90 | */ |
| 91 | #define VMSAVEFILE_SIG "T3-state-v000A\015\012\032" |
| 92 | |
| 93 | |
| 94 | /* ------------------------------------------------------------------------ */ |
| 95 | /* |
| 96 | * Compute the checksum of the contents of a file stream. We'll compute |
| 97 | * the checksum of the given file, starting at the current seek position |
| 98 | * and running for the requested number of bytes. |
| 99 | */ |
| 100 | static unsigned long compute_checksum(CVmFile *fp, unsigned long len) |
| 101 | { |
| 102 | CVmCRC32 crc; |
| 103 | |
| 104 | /* read the file and compute the CRC value for its contents */ |
| 105 | while (len != 0) |
| 106 | { |
| 107 | char buf[256]; |
| 108 | size_t cur_len; |
| 109 | |
| 110 | /* figure out how much we can load from the file */ |
| 111 | cur_len = sizeof(buf); |
| 112 | if (cur_len > len) |
| 113 | cur_len = (size_t)len; |
| 114 | |
| 115 | /* load the data from the file */ |
| 116 | fp->read_bytes(buf, cur_len); |
| 117 | |
| 118 | /* deduct the amount we read from the overall file length remaining */ |
| 119 | len -= cur_len; |
| 120 | |
| 121 | /* scan this block into the checksum */ |
| 122 | crc.scan_bytes(buf, cur_len); |
| 123 | } |
| 124 | |
| 125 | /* return the computed value */ |
| 126 | return crc.get_crc_val(); |
| 127 | } |
| 128 | |
| 129 | |
| 130 | /* ------------------------------------------------------------------------ */ |
| 131 | /* |
| 132 | * Callback for enumerating the LookupTable entries for the metadata table |
| 133 | * while saving a game |
| 134 | */ |
| 135 | struct metatab_saver |
| 136 | { |
| 137 | metatab_saver(CVmFile *fp) |
| 138 | { |
| 139 | this->fp = fp; |
| 140 | this->cnt = 0; |
| 141 | } |
| 142 | |
| 143 | /* number of name/value pairs we've written so far */ |
| 144 | int cnt; |
| 145 | |
| 146 | /* file we're writing to */ |
| 147 | CVmFile *fp; |
| 148 | |
| 149 | /* LookupTable::for_each callback */ |
| 150 | static void cb(VMG_ const vm_val_t *key, const vm_val_t *val, void *ctx) |
| 151 | { |
| 152 | /* the context is our 'this' pointer */ |
| 153 | metatab_saver *self = (metatab_saver *)ctx; |
| 154 | CVmFile *fp = self->fp; |
| 155 | |
| 156 | /* both the key and value must be strings for us to save them */ |
| 157 | const char *keystr = key->get_as_string(vmg0_); |
| 158 | const char *valstr = val->get_as_string(vmg0_); |
| 159 | if (keystr != 0 && valstr != 0) |
| 160 | { |
| 161 | size_t len; |
| 162 | |
| 163 | /* save the key string */ |
| 164 | fp->write_uint2(len = vmb_get_len(keystr)); |
| 165 | fp->write_bytes(keystr + VMB_LEN, len); |
| 166 | |
| 167 | /* save the value string */ |
| 168 | fp->write_uint2(len = vmb_get_len(valstr)); |
| 169 | fp->write_bytes(valstr + VMB_LEN, len); |
| 170 | |
| 171 | /* count the pair */ |
| 172 | self->cnt += 1; |
| 173 | } |
| 174 | } |
| 175 | }; |
| 176 | |
| 177 | |
| 178 | /* ------------------------------------------------------------------------ */ |
| 179 | /* |
| 180 | * Save VM state to a file |
| 181 | */ |
| 182 | void CVmSaveFile::save(VMG_ CVmFile *fp, CVmObjLookupTable *metatab) |
| 183 | { |
| 184 | /* write the signature */ |
| 185 | fp->write_bytes(VMSAVEFILE_SIG, sizeof(VMSAVEFILE_SIG)-1); |
| 186 | |
| 187 | /* note the seek position of the start of the file header */ |
| 188 | long startpos = fp->get_pos(); |
| 189 | |
| 190 | /* write a placeholder for the stream size and checksum */ |
| 191 | fp->write_uint4(0); |
| 192 | fp->write_uint4(0); |
| 193 | |
| 194 | /* write the image file's timestamp */ |
| 195 | fp->write_bytes(G_image_loader->get_timestamp(), 24); |
| 196 | |
| 197 | /* get the image filename */ |
| 198 | const char *fname = G_image_loader->get_filename(); |
| 199 | size_t fname_len = strlen(fname); |
| 200 | |
| 201 | /* |
| 202 | * write the image filename, so we can figure out what image file to |
| 203 | * load if we start the interpreter specifying only the saved state |
| 204 | * to be restored |
| 205 | */ |
| 206 | fp->write_uint2(fname_len); |
| 207 | fp->write_bytes(fname, fname_len); |
| 208 | |
| 209 | /* if there's a metadata table, write it out */ |
| 210 | if (metatab != 0) |
| 211 | { |
| 212 | /* write a placeholder for the length and entry count */ |
| 213 | long metapos = fp->get_pos(); |
| 214 | fp->write_uint2(0); |
| 215 | fp->write_uint2(0); |
| 216 | |
| 217 | /* run through the table and write out the entries */ |
| 218 | metatab_saver ctx(fp); |
| 219 | metatab->for_each(vmg_ &ctx.cb, &ctx); |
| 220 | |
| 221 | /* go back and fix up the table length and entry count */ |
| 222 | long endpos = fp->get_pos(); |
| 223 | fp->set_pos(metapos); |
| 224 | fp->write_uint2((int)(endpos - metapos - 2)); |
| 225 | fp->write_uint2(ctx.cnt); |
| 226 | |
| 227 | /* it's an error if this table exceeds 64k */ |
| 228 | if (endpos - metapos > 0xffff) |
| 229 | err_throw(VMERR_DESC_TAB_OVERFLOW); |
| 230 | |
| 231 | /* seek back to the end of the table */ |
| 232 | fp->set_pos(endpos); |
| 233 | } |
| 234 | else |
| 235 | { |
| 236 | /* there's no metadata table - just write a zero-byte length */ |
| 237 | fp->write_uint2(0); |
| 238 | } |
| 239 | |
| 240 | /* save all modified object state */ |
| 241 | G_obj_table->save(vmg_ fp); |
| 242 | |
| 243 | /* save the synthesized exports */ |
| 244 | G_image_loader->save_synth_exports(vmg_ fp); |
| 245 | |
| 246 | /* remember where the file ends */ |
| 247 | long endpos = fp->get_pos(); |
| 248 | |
| 249 | /* |
| 250 | * compute the size of the data stream - this includes everything |
| 251 | * after the size/checksum fields |
| 252 | */ |
| 253 | unsigned long datasize = endpos - startpos - 8; |
| 254 | |
| 255 | /* |
| 256 | * seek back to just after the size/checksum header - this is the |
| 257 | * start of the section of the file for which we must compute the |
| 258 | * checksum |
| 259 | */ |
| 260 | fp->set_pos(startpos + 8); |
| 261 | |
| 262 | /* compute the checksum */ |
| 263 | unsigned long crcval = compute_checksum(fp, datasize); |
| 264 | |
| 265 | /* |
| 266 | * seek back to the size/checksum header, and fill in those fields now |
| 267 | * that we know their values |
| 268 | */ |
| 269 | fp->set_pos(startpos); |
| 270 | fp->write_uint4(datasize); |
| 271 | fp->write_uint4(crcval); |
| 272 | |
| 273 | /* seek back to the end of the file */ |
| 274 | fp->set_pos(endpos); |
| 275 | } |
| 276 | |
| 277 | /* ------------------------------------------------------------------------ */ |
| 278 | /* |
| 279 | * Given a saved state file, get the name of the image file that was |
| 280 | * loaded when the state file was created. |
| 281 | */ |
| 282 | int CVmSaveFile::restore_get_image(osfildef *fp, |
| 283 | char *fname_buf, size_t fname_buf_len) |
| 284 | { |
| 285 | /* read the signature, size/checksum, and timestamp fields */ |
| 286 | char buf[128]; |
| 287 | if (osfrb(fp, buf, sizeof(VMSAVEFILE_SIG)-1 + 8 + 24)) |
| 288 | return VMERR_READ_FILE; |
| 289 | |
| 290 | /* check the signature */ |
| 291 | if (memcmp(buf, VMSAVEFILE_SIG, sizeof(VMSAVEFILE_SIG)-1) != 0) |
| 292 | return VMERR_NOT_SAVED_STATE; |
| 293 | |
| 294 | /* read the length of the image file name */ |
| 295 | if (osfrb(fp, buf, 2)) |
| 296 | return VMERR_READ_FILE; |
| 297 | |
| 298 | /* get the length from the buffer */ |
| 299 | size_t len = osrp2(buf); |
| 300 | |
| 301 | /* if it won't fit in the buffer, return an error */ |
| 302 | if (len + 1 > fname_buf_len) |
| 303 | return VMERR_READ_FILE; |
| 304 | |
| 305 | /* read the name into the caller's buffer */ |
| 306 | if (osfrb(fp, fname_buf, len)) |
| 307 | return VMERR_READ_FILE; |
| 308 | |
| 309 | /* null-terminate the name */ |
| 310 | fname_buf[len] = '\0'; |
| 311 | |
| 312 | /* success */ |
| 313 | return 0; |
| 314 | } |
| 315 | |
| 316 | /* ------------------------------------------------------------------------ */ |
| 317 | /* |
| 318 | * Restore VM state from a file. Returns zero on success, non-zero on |
| 319 | * error. |
| 320 | */ |
| 321 | int CVmSaveFile::restore(VMG_ CVmFile *fp) |
| 322 | { |
| 323 | /* we don't have a fixup table yet (the object loader will create one) */ |
| 324 | CVmObjFixup *fixups = 0; |
| 325 | |
| 326 | /* read the file's signature */ |
| 327 | char buf[128]; |
| 328 | fp->read_bytes(buf, sizeof(VMSAVEFILE_SIG)-1); |
| 329 | |
| 330 | /* check the signature */ |
| 331 | if (memcmp(buf, VMSAVEFILE_SIG, sizeof(VMSAVEFILE_SIG)-1) != 0) |
| 332 | return VMERR_NOT_SAVED_STATE; |
| 333 | |
| 334 | /* read the size/checksum fields */ |
| 335 | unsigned long datasize = fp->read_uint4(); |
| 336 | unsigned long old_crcval = fp->read_uint4(); |
| 337 | |
| 338 | /* note the starting position of the datastream */ |
| 339 | long startpos = fp->get_pos(); |
| 340 | |
| 341 | /* compute the checksum of the file data */ |
| 342 | unsigned long new_crcval = compute_checksum(fp, datasize); |
| 343 | |
| 344 | /* |
| 345 | * if the checksum we computed doesn't match the one stored in the |
| 346 | * file, the file is corrupted |
| 347 | */ |
| 348 | if (new_crcval != old_crcval) |
| 349 | return VMERR_BAD_SAVED_STATE; |
| 350 | |
| 351 | /* seek back to the starting position */ |
| 352 | fp->set_pos(startpos); |
| 353 | |
| 354 | /* check the timestamp */ |
| 355 | fp->read_bytes(buf, 24); |
| 356 | if (memcmp(buf, G_image_loader->get_timestamp(), 24) != 0) |
| 357 | return VMERR_WRONG_SAVED_STATE; |
| 358 | |
| 359 | /* |
| 360 | * skip the image filename - since we already have an image file |
| 361 | * loaded, this information is of no use to us here (it's only |
| 362 | * useful when we want to restore a saved state before we know what |
| 363 | * the image file is) |
| 364 | */ |
| 365 | fp->set_pos_from_cur(fp->read_int2()); |
| 366 | |
| 367 | /* |
| 368 | * skip the metadata table - it's provided for browsing tools, and we |
| 369 | * have no use for it ourselves |
| 370 | */ |
| 371 | fp->set_pos_from_cur(fp->read_int2()); |
| 372 | |
| 373 | /* |
| 374 | * discard all undo information - any undo information we currently |
| 375 | * have obviously can't be applied to the restored state |
| 376 | */ |
| 377 | G_undo->drop_undo(vmg0_); |
| 378 | |
| 379 | /* |
| 380 | * Disable garbage collection while restoring. This is necessary |
| 381 | * because there are possible intermediate states where we have |
| 382 | * restored some of the objects but not all of them, so objects that |
| 383 | * are reachable from the fully restored state won't necessarily appear |
| 384 | * to be reachable from all possible intermediate states. |
| 385 | */ |
| 386 | int old_gc_enabled = G_obj_table->enable_gc(vmg_ FALSE); |
| 387 | |
| 388 | int err = 0; |
| 389 | err_try |
| 390 | { |
| 391 | /* forget any IntrinsicClass instances we created at startup */ |
| 392 | G_meta_table->forget_intrinsic_class_instances(vmg0_); |
| 393 | |
| 394 | /* load the object data from the file */ |
| 395 | if ((err = G_obj_table->restore(vmg_ fp, &fixups)) != 0) |
| 396 | goto read_done; |
| 397 | |
| 398 | /* load the synthesized exports from the file */ |
| 399 | err = G_image_loader->restore_synth_exports(vmg_ fp, fixups); |
| 400 | if (err != 0) |
| 401 | goto read_done; |
| 402 | |
| 403 | /* |
| 404 | * re-link to the exports and synthesized exports loaded from the |
| 405 | * saved session |
| 406 | */ |
| 407 | G_image_loader->do_dynamic_link(vmg0_); |
| 408 | |
| 409 | /* create any missing IntrinsicClass instances */ |
| 410 | G_meta_table->create_intrinsic_class_instances(vmg0_); |
| 411 | |
| 412 | /* perform any requested post-load object initializations */ |
| 413 | G_obj_table->do_all_post_load_init(vmg0_); |
| 414 | |
| 415 | read_done: ; |
| 416 | } |
| 417 | err_catch(exc) |
| 418 | { |
| 419 | /* remember the error code */ |
| 420 | err = exc->get_error_code(); |
| 421 | } |
| 422 | err_end; |
| 423 | |
| 424 | /* we're done with the fixup table, so delete it if we created one */ |
| 425 | if (fixups != 0) |
| 426 | delete fixups; |
| 427 | |
| 428 | /* restore the garbage collector's enabled state */ |
| 429 | G_obj_table->enable_gc(vmg_ old_gc_enabled); |
| 430 | |
| 431 | /* if any error occurred, throw the error */ |
| 432 | if (err != 0) |
| 433 | err_throw(err); |
| 434 | |
| 435 | /* |
| 436 | * explicitly run garbage collection, since any dynamic objects that |
| 437 | * were reachable before the restore only through non-transient |
| 438 | * references will no longer be reachable, all of the non-transient |
| 439 | * references having been replaced now |
| 440 | */ |
| 441 | G_obj_table->gc_full(vmg0_); |
| 442 | |
| 443 | /* success */ |
| 444 | return 0; |
| 445 | } |
| 446 | |
| 447 | /* ------------------------------------------------------------------------ */ |
| 448 | /* |
| 449 | * Reset to initial image file state |
| 450 | */ |
| 451 | void CVmSaveFile::reset(VMG0_) |
| 452 | { |
| 453 | /* |
| 454 | * discard undo information, since it applies only to the current VM |
| 455 | * state and obviously is no longer relevant after we reset to the |
| 456 | * initial state |
| 457 | */ |
| 458 | G_undo->drop_undo(vmg0_); |
| 459 | |
| 460 | /* |
| 461 | * discard all synthesized exports, since we want to dynamically link |
| 462 | * to the base image file state |
| 463 | */ |
| 464 | G_image_loader->discard_synth_exports(); |
| 465 | |
| 466 | /* forget any IntrinsicClass instances we created at startup */ |
| 467 | G_meta_table->forget_intrinsic_class_instances(vmg0_); |
| 468 | |
| 469 | /* reset all objects to initial image file load state */ |
| 470 | G_obj_table->reset_to_image(vmg0_); |
| 471 | |
| 472 | /* |
| 473 | * forget the previous dynamic linking information and relink to the |
| 474 | * image file again - this will ensure that any objects created after |
| 475 | * load are properly re-created now |
| 476 | */ |
| 477 | G_image_loader->do_dynamic_link(vmg0_); |
| 478 | |
| 479 | /* create any missing IntrinsicClass instances */ |
| 480 | G_meta_table->create_intrinsic_class_instances(vmg0_); |
| 481 | |
| 482 | /* perform any requested post-load object initializations */ |
| 483 | G_obj_table->do_all_post_load_init(vmg0_); |
| 484 | |
| 485 | /* |
| 486 | * explicitly run garbage collection to clean up dynamic objects that |
| 487 | * are no longer reachable from the initial state |
| 488 | */ |
| 489 | G_obj_table->gc_full(vmg0_); |
| 490 | |
| 491 | /* run the static initializers */ |
| 492 | G_image_loader->run_static_init(vmg0_); |
| 493 | } |
| 494 |