Debian Codegraph @

terps/scare/scserial.c

raw symbols SCIP
1 /* vi: set ts=2 shiftwidth=2 expandtab:
2 *
3 * Copyright (C) 2003-2008 Simon Baldwin and Mark J. Tilford
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of version 2 of the GNU General Public License
7 * as published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
17 * USA
18 */
19
20 /*
21 * Module notes:
22 *
23 * o ...
24 */
25
26 #include <assert.h>
27 #include <errno.h>
28 #include <setjmp.h>
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <string.h>
32
33 #include "zlib.h"
34 #include "scare.h"
35 #include "scprotos.h"
36 #include "scgamest.h"
37
38
39 /* Assorted definitions and constants. */
40 static const sc_char NEWLINE = '\n';
41 static const sc_char CARRIAGE_RETURN = '\r';
42 static const sc_char NUL = '\0';
43
44 enum { BUFFER_SIZE = 4096 };
45
46 /* Output buffer. */
47 static sc_byte *ser_buffer = NULL;
48 static sc_int ser_buffer_length = 0;
49
50 /* Callback and opaque pointer for use by output functions. */
51 static sc_write_callbackref_t ser_callback = NULL;
52 static void *ser_opaque = NULL;
53
54
55 /*
56 * ser_flush()
57 * ser_buffer_character()
58 *
59 * Flush pending buffer contents; add a character to the buffer.
60 */
61 static void
62 ser_flush (sc_bool is_final)
63 {
64 static sc_bool initialized = FALSE;
65 static sc_byte *out_buffer = NULL;
66 static sc_int out_buffer_size = 0;
67 static z_stream stream;
68
69 sc_int status;
70
71 /* If this is an initial call, initialize deflation. */
72 if (!initialized)
73 {
74 /* Allocate an initial output buffer. */
75 out_buffer_size = BUFFER_SIZE;
76 out_buffer = sc_malloc (out_buffer_size);
77
78 /* Initialize Zlib deflation functions. */
79 stream.next_out = out_buffer;
80 stream.avail_out = out_buffer_size;
81 stream.next_in = ser_buffer;
82 stream.avail_in = 0;
83
84 stream.zalloc = Z_NULL;
85 stream.zfree = Z_NULL;
86 stream.opaque = Z_NULL;
87
88 status = deflateInit (&stream, Z_DEFAULT_COMPRESSION);
89 if (status != Z_OK)
90 {
91 sc_error ("ser_flush: deflateInit: error %ld\n", status);
92 ser_buffer_length = 0;
93
94 sc_free (out_buffer);
95 out_buffer = NULL;
96 out_buffer_size = 0;
97 return;
98 }
99
100 initialized = TRUE;
101 }
102
103 /* Deflate data from the current output buffer. */
104 stream.next_in = ser_buffer;
105 stream.avail_in = ser_buffer_length;
106
107 /* Loop while deflate output is pending and buffer not emptied. */
108 while (TRUE)
109 {
110 sc_int in_bytes, out_bytes;
111
112 /* Compress stream data, with finish if this is the final flush. */
113 if (is_final)
114 status = deflate (&stream, Z_FINISH);
115 else
116 status = deflate (&stream, Z_NO_FLUSH);
117 if (status != Z_STREAM_END && status != Z_OK)
118 {
119 sc_error ("ser_flush: deflate: error %ld\n", status);
120 ser_buffer_length = 0;
121
122 sc_free (out_buffer);
123 out_buffer = NULL;
124 out_buffer_size = 0;
125 initialized = FALSE;
126 return;
127 }
128
129 /* Calculate bytes used, and output. */
130 in_bytes = ser_buffer_length - stream.avail_in;
131 out_bytes = out_buffer_size - stream.avail_out;
132
133 /* See if compressed data is available. */
134 if (out_bytes > 0)
135 {
136 /* Write it to save file output through the callback. */
137 ser_callback (ser_opaque, out_buffer, out_bytes);
138
139 /* Reset deflation stream for available space. */
140 stream.next_out = out_buffer;
141 stream.avail_out = out_buffer_size;
142 }
143
144 /* Remove consumed data from the input buffer. */
145 if (in_bytes > 0)
146 {
147 /* Move any unused data, and reduce length. */
148 memmove (ser_buffer,
149 ser_buffer + in_bytes, ser_buffer_length - in_bytes);
150 ser_buffer_length -= in_bytes;
151
152 /* Reset deflation stream for consumed data. */
153 stream.next_in = ser_buffer;
154 stream.avail_in = ser_buffer_length;
155 }
156
157 /* If final flush, wait until deflate indicates finished. */
158 if (is_final && status == Z_OK)
159 continue;
160
161 /* If data was consumed or produced, break. */
162 if (out_bytes > 0 || in_bytes > 0)
163 break;
164 }
165
166 /* If this was a final call, clean up. */
167 if (is_final)
168 {
169 /* Compression completed. */
170 status = deflateEnd (&stream);
171 if (status != Z_OK)
172 sc_error ("ser_flush: warning: deflateEnd: error %ld\n", status);
173
174 if (ser_buffer_length != 0)
175 {
176 sc_error ("ser_flush: warning: deflate missed data\n");
177 ser_buffer_length = 0;
178 }
179
180 /* Free the allocated compression buffer. */
181 sc_free (ser_buffer);
182 ser_buffer = NULL;
183
184 /*
185 * Free output buffer, and reset flag for reinitialization on the next
186 * call.
187 */
188 sc_free (out_buffer);
189 out_buffer = NULL;
190 out_buffer_size = 0;
191 initialized = FALSE;
192 }
193 }
194
195 static void
196 ser_buffer_character (sc_char character)
197 {
198 /* Allocate the buffer if not yet done. */
199 if (!ser_buffer)
200 {
201 assert (ser_buffer_length == 0);
202 ser_buffer = sc_malloc (BUFFER_SIZE);
203 }
204
205 /* Add to the buffer, with intermediate flush if filled. */
206 ser_buffer[ser_buffer_length++] = character;
207 if (ser_buffer_length == BUFFER_SIZE)
208 ser_flush (FALSE);
209 }
210
211
212 /*
213 * ser_buffer_buffer()
214 * ser_buffer_string()
215 * ser_buffer_int()
216 * ser_buffer_int_special()
217 * ser_buffer_uint()
218 * ser_buffer_boolean()
219 *
220 * Buffer a buffer, a string, an unsigned and signed integer, and a boolean.
221 */
222 static void
223 ser_buffer_buffer (const sc_char *buffer, sc_int length)
224 {
225 sc_int index_;
226
227 /* Add each character to the buffer. */
228 for (index_ = 0; index_ < length; index_++)
229 ser_buffer_character (buffer[index_]);
230 }
231
232 static void
233 ser_buffer_string (const sc_char *string)
234 {
235 /* Buffer string, followed by DOS style end-of-line. */
236 ser_buffer_buffer (string, strlen (string));
237 ser_buffer_character (CARRIAGE_RETURN);
238 ser_buffer_character (NEWLINE);
239 }
240
241 static void
242 ser_buffer_int (sc_int value)
243 {
244 sc_char buffer[32];
245
246 /* Convert to a string and buffer that. */
247 sprintf (buffer, "%ld", value);
248 ser_buffer_string (buffer);
249 }
250
251 static void
252 ser_buffer_int_special (sc_int value)
253 {
254 sc_char buffer[32];
255
256 /* Weirdo formatting for compatibility. */
257 sprintf (buffer, "% ld ", value);
258 ser_buffer_string (buffer);
259 }
260
261 static void
262 ser_buffer_uint (sc_uint value)
263 {
264 sc_char buffer[32];
265
266 /* Convert to a string and buffer that. */
267 sprintf (buffer, "%lu", value);
268 ser_buffer_string (buffer);
269 }
270
271 static void
272 ser_buffer_boolean (sc_bool boolean)
273 {
274 /* Write a 1 for TRUE, 0 for FALSE. */
275 ser_buffer_string (boolean ? "1" : "0");
276 }
277
278
279 /*
280 * ser_save_game()
281 *
282 * Serialize a game and save its state using the given callback and opaque.
283 */
284 void
285 ser_save_game (sc_gameref_t game,
286 sc_write_callbackref_t callback, void *opaque)
287 {
288 const sc_var_setref_t vars = gs_get_vars (game);
289 const sc_prop_setref_t bundle = gs_get_bundle (game);
290 sc_vartype_t vt_key[3];
291 sc_int index_, var_count;
292 assert (callback);
293
294 /* Store the callback and opaque references, for writer functions. */
295 ser_callback = callback;
296 ser_opaque = opaque;
297
298 /* Write the game name. */
299 vt_key[0].string = "Globals";
300 vt_key[1].string = "GameName";
301 ser_buffer_string (prop_get_string (bundle, "S<-ss", vt_key));
302
303 /* Write the counts of rooms, objects, etc. */
304 ser_buffer_int (gs_room_count (game));
305 ser_buffer_int (gs_object_count (game));
306 ser_buffer_int (gs_task_count (game));
307 ser_buffer_int (gs_event_count (game));
308 ser_buffer_int (gs_npc_count (game));
309
310 /* Write the score and player information. */
311 ser_buffer_int (game->score);
312 ser_buffer_int (gs_playerroom (game) + 1);
313 ser_buffer_int (gs_playerparent (game));
314 ser_buffer_int (gs_playerposition (game));
315
316 /* Write player gender. */
317 vt_key[0].string = "Globals";
318 vt_key[1].string = "PlayerGender";
319 ser_buffer_int (prop_get_integer (bundle, "I<-ss", vt_key));
320
321 /*
322 * Write encumbrance details. The player limits are constant for a given
323 * game, and can be extracted from properties. The current sizes and
324 * weights can also be recalculated from held objects, so we don't maintain
325 * them in the game. We can write constants here, then, and ignore
326 * the values on restoring. Note however that if the Adrift Runner is
327 * relying on these values, this may give it problems with one of our saved
328 * games.
329 */
330 ser_buffer_int (90);
331 ser_buffer_int (0);
332 ser_buffer_int (90);
333 ser_buffer_int (0);
334
335 /* Save rooms information. */
336 for (index_ = 0; index_ < gs_room_count (game); index_++)
337 ser_buffer_boolean (gs_room_seen (game, index_));
338
339 /* Save objects information. */
340 for (index_ = 0; index_ < gs_object_count (game); index_++)
341 {
342 ser_buffer_int (gs_object_position (game, index_));
343 ser_buffer_boolean (gs_object_seen (game, index_));
344 ser_buffer_int (gs_object_parent (game, index_));
345 if (gs_object_openness (game, index_) != 0)
346 ser_buffer_int (gs_object_openness (game, index_));
347
348 if (gs_object_state (game, index_) != 0)
349 ser_buffer_int (gs_object_state (game, index_));
350
351 ser_buffer_boolean (gs_object_unmoved (game, index_));
352 }
353
354 /* Save tasks information. */
355 for (index_ = 0; index_ < gs_task_count (game); index_++)
356 {
357 ser_buffer_boolean (gs_task_done (game, index_));
358 ser_buffer_boolean (gs_task_scored (game, index_));
359 }
360
361 /* Save events information. */
362 for (index_ = 0; index_ < gs_event_count (game); index_++)
363 {
364 sc_int startertype, task;
365
366 /* Get starter task, if any. */
367 vt_key[0].string = "Events";
368 vt_key[1].integer = index_;
369 vt_key[2].string = "StarterType";
370 startertype = prop_get_integer (bundle, "I<-sis", vt_key);
371 if (startertype == 3)
372 {
373 vt_key[2].string = "TaskNum";
374 task = prop_get_integer (bundle, "I<-sis", vt_key);
375 }
376 else
377 task = 0;
378
379 /* Save event details. */
380 ser_buffer_int (gs_event_time (game, index_));
381 ser_buffer_int (task);
382 ser_buffer_int (gs_event_state (game, index_) - 1);
383 if (task > 0)
384 ser_buffer_boolean (gs_task_done (game, task - 1));
385 else
386 ser_buffer_boolean (FALSE);
387 }
388
389 /* Save NPCs information. */
390 for (index_ = 0; index_ < gs_npc_count (game); index_++)
391 {
392 sc_int walk;
393
394 ser_buffer_int (gs_npc_location (game, index_));
395 ser_buffer_boolean (gs_npc_seen (game, index_));
396 for (walk = 0; walk < gs_npc_walkstep_count (game, index_); walk++)
397 ser_buffer_int_special (gs_npc_walkstep (game, index_, walk));
398 }
399
400 /* Save each variable. */
401 vt_key[0].string = "Variables";
402 var_count = prop_get_child_count (bundle, "I<-s", vt_key);
403
404 for (index_ = 0; index_ < var_count; index_++)
405 {
406 const sc_char *name;
407 sc_int var_type;
408
409 vt_key[1].integer = index_;
410
411 vt_key[2].string = "Name";
412 name = prop_get_string (bundle, "S<-sis", vt_key);
413 vt_key[2].string = "Type";
414 var_type = prop_get_integer (bundle, "I<-sis", vt_key);
415
416 switch (var_type)
417 {
418 case TAFVAR_NUMERIC:
419 ser_buffer_int (var_get_integer (vars, name));
420 break;
421
422 case TAFVAR_STRING:
423 ser_buffer_string (var_get_string (vars, name));
424 break;
425
426 default:
427 sc_fatal ("ser_save_game: unknown variable type, %ld\n", var_type);
428 }
429 }
430
431 /* Save timing information. */
432 ser_buffer_uint (var_get_elapsed_seconds (vars));
433
434 /* Save turns count. */
435 ser_buffer_uint ((sc_uint) game->turns);
436
437 /*
438 * Flush the last buffer contents, and drop the callback and opaque
439 * references.
440 */
441 ser_flush (TRUE);
442 ser_callback = NULL;
443 ser_opaque = NULL;
444 }
445
446
447 /*
448 * ser_save_game_prompted()
449 *
450 * Serialize a game and save its state, requesting a save stream from
451 * the user.
452 */
453 sc_bool
454 ser_save_game_prompted (sc_gameref_t game)
455 {
456 void *opaque;
457
458 /*
459 * Open an output stream, and if successful, save a game using the opaque
460 * value returned.
461 */
462 opaque = if_open_saved_game (TRUE);
463 if (opaque)
464 {
465 ser_save_game (game, if_write_saved_game, opaque);
466 if_close_saved_game (opaque);
467 return TRUE;
468 }
469
470 return FALSE;
471 }
472
473
474 /* TAS input file line counter. */
475 static sc_tafref_t ser_tas = NULL;
476 static sc_int ser_tasline = 0;
477
478 /* Restore error jump buffer. */
479 static jmp_buf ser_tas_error;
480
481 /*
482 * ser_get_string()
483 * ser_get_int()
484 * ser_get_uint()
485 * ser_get_boolean()
486 *
487 * Wrapper round obtaining the next TAS file line, with variants to convert
488 * the line content into an appropriate type.
489 */
490 static const sc_char *
491 ser_get_string (void)
492 {
493 const sc_char *string;
494
495 /* Get the next line, and complain if absent. */
496 string = taf_next_line (ser_tas);
497 if (!string)
498 {
499 sc_error ("ser_get_string: out of TAS data at line %ld\n", ser_tasline);
500 longjmp (ser_tas_error, 1);
501 }
502
503 ser_tasline++;
504 return string;
505 }
506
507 static sc_int
508 ser_get_int (void)
509 {
510 const sc_char *string;
511 sc_int value;
512
513 /* Get line, and scan for a single integer; return it. */
514 string = ser_get_string ();
515 if (sscanf (string, "%ld", &value) != 1)
516 {
517 sc_error ("ser_get_int:"
518 " invalid integer at line %ld\n", ser_tasline - 1);
519 longjmp (ser_tas_error, 1);
520 }
521
522 return value;
523 }
524
525 static sc_uint
526 ser_get_uint (void)
527 {
528 const sc_char *string;
529 sc_uint value;
530
531 /* Get line, and scan for a single integer; return it. */
532 string = ser_get_string ();
533 if (sscanf (string, "%lu", &value) != 1)
534 {
535 sc_error ("ser_get_uint:"
536 " invalid integer at line %ld\n", ser_tasline - 1);
537 longjmp (ser_tas_error, 1);
538 }
539
540 return value;
541 }
542
543 static sc_bool
544 ser_get_boolean (void)
545 {
546 const sc_char *string;
547 sc_uint value;
548
549 /*
550 * Get line, and scan for a single integer; check it's a valid-looking flag,
551 * and return it.
552 */
553 string = ser_get_string ();
554 if (sscanf (string, "%lu", &value) != 1)
555 {
556 sc_error ("ser_get_boolean:"
557 " invalid boolean at line %ld\n", ser_tasline - 1);
558 longjmp (ser_tas_error, 1);
559 }
560 if (value != 0 && value != 1)
561 {
562 sc_error ("ser_get_boolean:"
563 " warning: suspect boolean at line %ld\n", ser_tasline - 1);
564 }
565
566 return value != 0;
567 }
568
569
570 /*
571 * ser_load_game()
572 *
573 * Load a serialized game into the given game by repeated calls to the
574 * callback() function.
575 */
576 sc_bool
577 ser_load_game (sc_gameref_t game,
578 sc_read_callbackref_t callback, void *opaque)
579 {
580 static sc_var_setref_t new_vars; /* For setjmp safety */
581 static sc_gameref_t new_game; /* For setjmp safety */
582
583 const sc_filterref_t filter = gs_get_filter (game);
584 const sc_prop_setref_t bundle = gs_get_bundle (game);
585 sc_vartype_t vt_key[3];
586 sc_int index_, var_count;
587 const sc_char *gamename;
588
589 /* Create a TAF (TAS) reference from callbacks, for reader functions. */
590 ser_tas = taf_create_tas (callback, opaque);
591 if (!ser_tas)
592 return FALSE;
593
594 /* Reset line counter for error messages. */
595 ser_tasline = 1;
596
597 new_game = NULL;
598 new_vars = NULL;
599
600 /* Set up error handling jump buffer, and handle errors. */
601 if (setjmp (ser_tas_error) != 0)
602 {
603 /* Destroy any temporary game and variables. */
604 if (new_game)
605 gs_destroy (new_game);
606 if (new_vars)
607 var_destroy (new_vars);
608
609 /* Destroy the TAF (TAS) file and return fail status. */
610 taf_destroy (ser_tas);
611 ser_tas = NULL;
612 return FALSE;
613 }
614
615 /*
616 * Read the game name, and compare with the one in the game. Fail if
617 * they don't match exactly. A tighter check than this would perhaps be
618 * preferable, say, something based on the TAF file header, but this isn't
619 * in the save file format.
620 */
621 vt_key[0].string = "Globals";
622 vt_key[1].string = "GameName";
623 gamename = prop_get_string (bundle, "S<-ss", vt_key);
624 if (strcmp (ser_get_string (), gamename) != 0)
625 longjmp (ser_tas_error, 1);
626
627 /* Read and verify the counts in the saved game. */
628 if (ser_get_int () != gs_room_count (game)
629 || ser_get_int () != gs_object_count (game)
630 || ser_get_int () != gs_task_count (game)
631 || ser_get_int () != gs_event_count (game)
632 || ser_get_int () != gs_npc_count (game))
633 longjmp (ser_tas_error, 1);
634
635 /* Create a variables set and game to restore into. */
636 new_vars = var_create (bundle);
637 new_game = gs_create (new_vars, bundle, filter);
638 var_register_game (new_vars, new_game);
639
640 /* All set to load TAF (TAS) data into the new game. */
641
642 /* Restore the score and player information. */
643 new_game->score = ser_get_int ();
644 gs_set_playerroom (new_game, ser_get_int () - 1);
645 gs_set_playerparent (new_game, ser_get_int ());
646 gs_set_playerposition (new_game, ser_get_int ());
647
648 /* Skip player gender. */
649 (void) ser_get_int ();
650
651 /* Skip encumbrance details, not currently maintained by the game. */
652 (void) ser_get_int ();
653 (void) ser_get_int ();
654 (void) ser_get_int ();
655 (void) ser_get_int ();
656
657 /* Restore rooms information. */
658 for (index_ = 0; index_ < gs_room_count (new_game); index_++)
659 gs_set_room_seen (new_game, index_, ser_get_boolean ());
660
661 /* Restore objects information. */
662 for (index_ = 0; index_ < gs_object_count (new_game); index_++)
663 {
664 sc_int openable, currentstate;
665
666 /* Bypass mutators for position and parent. Fix later? */
667 new_game->objects[index_].position = ser_get_int ();
668 gs_set_object_seen (new_game, index_, ser_get_boolean ());
669 new_game->objects[index_].parent = ser_get_int ();
670
671 vt_key[0].string = "Objects";
672 vt_key[1].integer = index_;
673 vt_key[2].string = "Openable";
674 openable = prop_get_integer (bundle, "I<-sis", vt_key);
675 gs_set_object_openness (new_game, index_,
676 openable != 0 ? ser_get_int () : 0);
677
678 vt_key[2].string = "CurrentState";
679 currentstate = prop_get_integer (bundle, "I<-sis", vt_key);
680 gs_set_object_state (new_game, index_,
681 currentstate != 0 ? ser_get_int () : 0);
682
683 gs_set_object_unmoved (new_game, index_, ser_get_boolean ());
684 }
685
686 /* Restore tasks information. */
687 for (index_ = 0; index_ < gs_task_count (new_game); index_++)
688 {
689 gs_set_task_done (new_game, index_, ser_get_boolean ());
690 gs_set_task_scored (new_game, index_, ser_get_boolean ());
691 }
692
693 /* Restore events information. */
694 for (index_ = 0; index_ < gs_event_count (new_game); index_++)
695 {
696 sc_int startertype, task;
697
698 /* Restore first event details. */
699 gs_set_event_time (new_game, index_, ser_get_int ());
700 task = ser_get_int ();
701 gs_set_event_state (new_game, index_, ser_get_int () + 1);
702
703 /* Verify and restore the starter task, if any. */
704 if (task > 0)
705 {
706 vt_key[0].string = "Events";
707 vt_key[1].integer = index_;
708 vt_key[2].string = "StarterType";
709 startertype = prop_get_integer (bundle, "I<-sis", vt_key);
710 if (startertype != 3)
711 longjmp (ser_tas_error, 1);
712
713 /* Restore task state. */
714 gs_set_task_done (new_game, task - 1, ser_get_boolean ());
715 }
716 else
717 (void) ser_get_boolean ();
718 }
719
720 /* Restore NPCs information. */
721 for (index_ = 0; index_ < gs_npc_count (new_game); index_++)
722 {
723 sc_int walk;
724
725 gs_set_npc_location (new_game, index_, ser_get_int ());
726 gs_set_npc_seen (new_game, index_, ser_get_boolean ());
727 for (walk = 0; walk < gs_npc_walkstep_count (new_game, index_); walk++)
728 gs_set_npc_walkstep (new_game, index_, walk, ser_get_int ());
729 }
730
731 /* Restore each variable. */
732 vt_key[0].string = "Variables";
733 var_count = prop_get_child_count (bundle, "I<-s", vt_key);
734
735 for (index_ = 0; index_ < var_count; index_++)
736 {
737 const sc_char *name;
738 sc_int var_type;
739
740 vt_key[1].integer = index_;
741
742 vt_key[2].string = "Name";
743 name = prop_get_string (bundle, "S<-sis", vt_key);
744 vt_key[2].string = "Type";
745 var_type = prop_get_integer (bundle, "I<-sis", vt_key);
746
747 switch (var_type)
748 {
749 case TAFVAR_NUMERIC:
750 var_put_integer (new_vars, name, ser_get_int ());
751 break;
752
753 case TAFVAR_STRING:
754 var_put_string (new_vars, name, ser_get_string ());
755 break;
756
757 default:
758 sc_fatal ("ser_load_game: unknown variable type, %ld\n", var_type);
759 }
760 }
761
762 /* Restore timing information. */
763 var_set_elapsed_seconds (new_vars, ser_get_uint ());
764
765 /* Restore turns count. */
766 new_game->turns = (sc_int) ser_get_uint ();
767
768 /*
769 * Resources tweak -- set requested to match those in the current game
770 * so that they remain unchanged by the gs_copy() of new_game onto
771 * game. This way, both the requested and the active resources in the
772 * game are unchanged by restore.
773 */
774 new_game->requested_sound = game->requested_sound;
775 new_game->requested_graphic = game->requested_graphic;
776
777 /*
778 * Quitter tweak -- set the quit jump buffer in the new game to be the
779 * same as the current one, so that it remains unchanged by gs_copy(). The
780 * one in the new game is still the unset one from gs_create().
781 */
782 memcpy (&new_game->quitter, &game->quitter, sizeof (game->quitter));
783
784 /*
785 * If we got this far, we successfully restored the game from the file.
786 * As our final act, copy the new game onto the old one.
787 */
788 new_game->temporary = game->temporary;
789 new_game->undo = game->undo;
790 gs_copy (game, new_game);
791
792 /* Done with the temporary game and variables. */
793 gs_destroy (new_game);
794 var_destroy (new_vars);
795
796 /* Done with TAF (TAS) file; destroy it and return successfully. */
797 taf_destroy (ser_tas);
798 ser_tas = NULL;
799 return TRUE;
800 }
801
802
803 /*
804 * ser_load_game_prompted()
805 *
806 * Load a serialized game into the given game, requesting a restore
807 * stream from the user.
808 */
809 sc_bool
810 ser_load_game_prompted (sc_gameref_t game)
811 {
812 void *opaque;
813
814 /*
815 * Open an input stream, and if successful, try to load a game using
816 * the opaque value returned and the saved game callback.
817 */
818 opaque = if_open_saved_game (FALSE);
819 if (opaque)
820 {
821 sc_bool status;
822
823 status = ser_load_game (game, if_read_saved_game, opaque);
824 if_close_saved_game (opaque);
825 return status;
826 }
827
828 return FALSE;
829 }