Debian Codegraph @

terps/glulxe/unixautosave.c

raw symbols SCIP
1 /* unixautosave.c: Unix-specific autosave code for Glulxe.
2 Designed by Andrew Plotkin <[email protected]>
3 http://eblong.com/zarf/glulx/index.html
4 */
5
6 #include <stdio.h>
7 #include <string.h>
8 #include "glulxe.h"
9 #include "unixstrt.h"
10 #include "gi_dispa.h"
11 #include "gi_blorb.h"
12 #include "glkstart.h" /* This comes with the Glk library. */
13
14 /* The following code only makes sense when compiled with a Glk library which offers autosave/autorestore hooks. */
15
16 #ifdef GLKUNIX_AUTOSAVE_FEATURES
17
18 char *pref_autosavedir = ".";
19 char *pref_autosavename = "autosave";
20 int pref_autosave_skiparrange = FALSE;
21
22 /* This is only needed for autorestore. (Defined in glkop.c.) */
23 extern gidispatch_rock_t glulxe_classtable_register_existing(void *obj, glui32 objclass, glui32 dispid);
24
25 /* This structure contains VM state which is not stored in a normal save file, but which is needed for an autorestore.
26
27 (The reason it's not stored in a normal save file is that it's useless unless you serialize the entire Glk state along with the VM. Glulx normally doesn't do that, but for an autosave, we do.)
28 */
29
30 typedef struct extra_glk_obj_id_entry_struct {
31 glui32 objclass;
32 glui32 tag;
33 glui32 dispid;
34 } extra_glk_obj_id_entry_t;
35
36 typedef struct extra_glulx_accel_entry_struct {
37 glui32 index;
38 glui32 addr;
39 } extra_glulx_accel_entry_t;
40
41 typedef struct extra_glulx_accel_param_struct {
42 glui32 param;
43 } extra_glulx_accel_param_t;
44
45 typedef struct extra_state_data_struct {
46 glui32 active;
47 glui32 protectstart, protectend;
48 glui32 iosys_mode, iosys_rock;
49 glui32 stringtable;
50 glui32 accel_param_count;
51 extra_glulx_accel_param_t *accel_params;
52 glui32 accel_func_count;
53 extra_glulx_accel_entry_t *accel_funcs;
54 glui32 gamefiletag;
55 glui32 id_map_list_count;
56 extra_glk_obj_id_entry_t *id_map_list;
57 } extra_state_data_t;
58
59 static void stash_extra_state(extra_state_data_t *state);
60 static void recover_extra_state(extra_state_data_t *state);
61
62 static extra_state_data_t *extra_state_data_alloc(void);
63 static void extra_state_data_free(extra_state_data_t *);
64 static int extra_state_serialize(glkunix_serialize_context_t, void *);
65 static int extra_state_serialize_accel_param(glkunix_serialize_context_t, void *);
66 static int extra_state_serialize_accel_func(glkunix_serialize_context_t, void *);
67 static int extra_state_serialize_obj_id_entry(glkunix_serialize_context_t, void *);
68 static int extra_state_unserialize(glkunix_unserialize_context_t, void *);
69 static int extra_state_unserialize_accel_param(glkunix_unserialize_context_t, void *);
70 static int extra_state_unserialize_accel_func(glkunix_unserialize_context_t, void *);
71 static int extra_state_unserialize_obj_id_entry(glkunix_unserialize_context_t, void *);
72
73 static char *game_signature = NULL;
74 static char *autosave_basepath = NULL;
75
76 /* Take a chunk of data (the first 64 bytes of the game file, which makes a good signature) and convert it to a hex string. This will be used as part of the filename for autosave.
77 */
78 void glkunix_set_autosave_signature(unsigned char *buf, glui32 len)
79 {
80 static char *hexdigits = "0123456789abcdef";
81
82 game_signature = glulx_malloc(2*len+1);
83 for (int ix=0; ix<len; ix++) {
84 unsigned char ch = buf[ix];
85 game_signature[2*ix] = hexdigits[(ch >> 4) & 0x0F];
86 game_signature[2*ix+1] = hexdigits[(ch) & 0x0F];
87 }
88 game_signature[2*len] = '\0';
89 }
90
91 /* Construct the pathname for autosaving this game. Returns a statically allocated string; the caller should append a filename suffix to that.
92 This looks at the autosavedir and autosavename preferences. If autosavename contains a "#" character, the game signature is substituted.
93 */
94 static char *get_autosave_basepath(void)
95 {
96 if (autosave_basepath == NULL) {
97 /* First time through we figure out where to save. */
98 char *basename = pref_autosavename;
99 char *hashpos = strchr(pref_autosavename, '#');
100 if (hashpos && game_signature) {
101 /* Substitute the game signature for the hash. */
102 int len = strlen(pref_autosavename) - 1 + strlen(game_signature) + 1;
103 int pos = (hashpos - pref_autosavename);
104 basename = glulx_malloc(len);
105 strncpy(basename, pref_autosavename, pos);
106 strcpy(basename+pos, game_signature);
107 strcat(basename, pref_autosavename+pos+1);
108 if (strlen(basename) != len-1) {
109 /* shouldn't happen */
110 fatal_error("autosavename interpolation came out wrong");
111 return NULL;
112 }
113 }
114
115 int buflen = strlen(pref_autosavedir) + 1 + strlen(basename) + 1;
116 autosave_basepath = glulx_malloc(buflen);
117 sprintf(autosave_basepath, "%s/%s", pref_autosavedir, basename);
118
119 if (basename != pref_autosavename)
120 glulx_free(basename);
121 }
122
123 return autosave_basepath;
124 }
125
126 /* Backtrack through the current opcode (at prevpc), and figure out whether its input arguments are on the stack or not. This will be important when setting up the saved VM state for restarting its opcode.
127
128 The opmodes argument must be an array int[3]. Returns TRUE on success.
129 */
130 static int parse_partial_operand(int *opmodes)
131 {
132 glui32 addr = prevpc;
133
134 /* Fetch the opcode number. */
135 glui32 opcode = Mem1(addr);
136 addr++;
137 if (opcode & 0x80) {
138 /* More than one-byte opcode. */
139 if (opcode & 0x40) {
140 /* Four-byte opcode */
141 opcode &= 0x3F;
142 opcode = (opcode << 8) | Mem1(addr);
143 addr++;
144 opcode = (opcode << 8) | Mem1(addr);
145 addr++;
146 opcode = (opcode << 8) | Mem1(addr);
147 addr++;
148 }
149 else {
150 /* Two-byte opcode */
151 opcode &= 0x7F;
152 opcode = (opcode << 8) | Mem1(addr);
153 addr++;
154 }
155 }
156
157 if (opcode != 0x130) { /* op_glk */
158 /* Error: parsed wrong opcode */
159 return FALSE;
160 }
161
162 /* @glk has operands LLS. */
163 opmodes[0] = Mem1(addr) & 0x0F;
164 opmodes[1] = (Mem1(addr) >> 4) & 0x0F;
165 opmodes[2] = Mem1(addr+1) & 0x0F;
166
167 return TRUE;
168 }
169
170 void glkunix_do_autosave(glui32 selector, glui32 arg0, glui32 arg1, glui32 arg2)
171 {
172 char *basepath = get_autosave_basepath();
173 if (!basepath)
174 return;
175 /* Space for the base plus a file suffix. */
176 char *pathname = glulx_malloc(strlen(basepath) + 16);
177 if (!pathname)
178 return;
179
180 /* When the save file is autorestored, the VM will restart the @glk opcode. That means that the Glk argument (the event structure address) must be waiting on the stack. Possibly also the @glk opcode's operands -- these might or might not have come off the stack. */
181 int res;
182 int opmodes[3];
183 res = parse_partial_operand(opmodes);
184 if (!res) {
185 glulx_free(pathname);
186 return;
187 }
188
189 sprintf(pathname, "%s.glksave", basepath);
190 strid_t savefile = glkunix_stream_open_pathname_gen(pathname, TRUE, FALSE, 1);
191 if (!savefile) {
192 glulx_free(pathname);
193 return;
194 }
195
196 /* Push all the necessary arguments for the @glk opcode. */
197 glui32 origstackptr = stackptr;
198 int stackvals = 0;
199
200 if (selector == 0x00C0) {
201 /* The event structure address: */
202 stackvals++;
203 if (stackptr+4 > stacksize)
204 fatal_error("Stack overflow in autosave callstub.");
205 StkW4(stackptr, arg0); /* eventaddr */
206 stackptr += 4;
207 }
208 else if (selector == 0x0062) {
209 /* The three arguments: */
210 stackvals++;
211 if (stackptr+4 > stacksize)
212 fatal_error("Stack overflow in autosave callstub.");
213 StkW4(stackptr, arg2); /* rock */
214 stackptr += 4;
215 stackvals++;
216 if (stackptr+4 > stacksize)
217 fatal_error("Stack overflow in autosave callstub.");
218 StkW4(stackptr, arg1); /* fmode */
219 stackptr += 4;
220 stackvals++;
221 if (stackptr+4 > stacksize)
222 fatal_error("Stack overflow in autosave callstub.");
223 StkW4(stackptr, arg0); /* usage */
224 stackptr += 4;
225 }
226 else {
227 fatal_error("Autosave with unrecognized glk selector.");
228 }
229
230 if (opmodes[1] == 8) {
231 /* The number of Glk arguments (1): */
232 stackvals++;
233 if (stackptr+4 > stacksize)
234 fatal_error("Stack overflow in autosave callstub.");
235 StkW4(stackptr, 1);
236 stackptr += 4;
237 }
238 if (opmodes[0] == 8) {
239 /* The Glk call selector (0x00C0 or 0x0062): */
240 stackvals++;
241 if (stackptr+4 > stacksize)
242 fatal_error("Stack overflow in autosave callstub.");
243 StkW4(stackptr, selector);
244 stackptr += 4;
245 }
246
247 /* Push a temporary callstub which contains the *last* PC -- the address of the @glk(select) invocation. */
248 if (stackptr+16 > stacksize)
249 fatal_error("Stack overflow in autosave callstub.");
250 StkW4(stackptr+0, 0);
251 StkW4(stackptr+4, 0);
252 StkW4(stackptr+8, prevpc);
253 StkW4(stackptr+12, frameptr);
254 stackptr += 16;
255
256 res = perform_save(savefile);
257
258 stackptr -= 16; // discard the temporary callstub
259 stackptr -= 4 * stackvals; // discard the temporary arguments
260 if (origstackptr != stackptr)
261 fatal_error("Stack pointer mismatch in autosave");
262
263 glk_stream_close(savefile, NULL);
264 savefile = NULL;
265
266 if (res) {
267 glulx_free(pathname);
268 return;
269 }
270
271 extra_state_data_t *extra_state = extra_state_data_alloc();
272 if (!extra_state) {
273 glulx_free(pathname);
274 return;
275 }
276 stash_extra_state(extra_state);
277
278 sprintf(pathname, "%s.json", basepath);
279 strid_t jsavefile = glkunix_stream_open_pathname_gen(pathname, TRUE, FALSE, 1);
280 if (!jsavefile) {
281 extra_state_data_free(extra_state);
282 glulx_free(pathname);
283 return;
284 }
285
286 glkunix_save_library_state(jsavefile, jsavefile, extra_state_serialize, extra_state);
287
288 glk_stream_close(jsavefile, NULL);
289 jsavefile = NULL;
290
291 extra_state_data_free(extra_state);
292 extra_state = NULL;
293
294 /* We could write those files to temporary paths and then rename them into place. That would be safer. */
295
296 glulx_free(pathname);
297 pathname = NULL;
298 }
299
300 int glkunix_do_autorestore()
301 {
302 char *basepath = get_autosave_basepath();
303 if (!basepath)
304 return FALSE;
305 /* Space for the base plus a file suffix. */
306 char *pathname = glulx_malloc(strlen(basepath) + 16);
307 if (!pathname)
308 return FALSE;
309
310 extra_state_data_t *extra_state = extra_state_data_alloc();
311 if (!extra_state) {
312 glulx_free(pathname);
313 return FALSE;
314 }
315
316 sprintf(pathname, "%s.json", basepath);
317 strid_t jsavefile = glkunix_stream_open_pathname_gen(pathname, FALSE, FALSE, 1);
318 if (!jsavefile) {
319 extra_state_data_free(extra_state);
320 glulx_free(pathname);
321 return FALSE;
322 }
323
324 glkunix_library_state_t library_state = glkunix_load_library_state(jsavefile, extra_state_unserialize, extra_state);
325
326 glk_stream_close(jsavefile, NULL);
327 jsavefile = NULL;
328
329 if (!library_state) {
330 extra_state_data_free(extra_state);
331 glulx_free(pathname);
332 return FALSE;
333 }
334
335 sprintf(pathname, "%s.glksave", basepath);
336 strid_t savefile = glkunix_stream_open_pathname_gen(pathname, FALSE, FALSE, 1);
337 if (!savefile) {
338 glkunix_library_state_free(library_state);
339 extra_state_data_free(extra_state);
340 glulx_free(pathname);
341 return FALSE;
342 }
343
344 glui32 res = perform_restore(savefile, TRUE);
345 glk_stream_close(savefile, NULL);
346 savefile = NULL;
347
348 if (res) {
349 glkunix_library_state_free(library_state);
350 extra_state_data_free(extra_state);
351 glulx_free(pathname);
352 return FALSE;
353 }
354
355 pop_callstub(0);
356 /* This should leave the PC on the @glk opcode that executed glk_select or glk_fileref_create_by_prompt. */
357
358 /* Annoyingly, the update_from_library_state we're about to do will close the currently-open gamefile. We'll recover it immediately, in recover_extra_state(). */
359
360 res = glkunix_update_from_library_state(library_state);
361 if (!res) {
362 glkunix_library_state_free(library_state);
363 extra_state_data_free(extra_state);
364 glulx_free(pathname);
365 return FALSE;
366 }
367
368 recover_extra_state(extra_state);
369
370 /* Clean up. */
371
372 glkunix_library_state_free(library_state);
373 library_state = NULL;
374
375 extra_state_data_free(extra_state);
376 extra_state = NULL;
377
378 glulx_free(pathname);
379 pathname = NULL;
380
381 return TRUE;
382 }
383
384 static glui32 tmp_accel_func_count;
385 static glui32 tmp_accel_func_size;
386 static extra_glulx_accel_entry_t *tmp_accel_funcs;
387
388 static void stash_one_accel_func(glui32 index, glui32 addr)
389 {
390 if (tmp_accel_func_count >= tmp_accel_func_size) {
391 if (tmp_accel_funcs == NULL) {
392 tmp_accel_func_size = 4;
393 tmp_accel_funcs = glulx_malloc(tmp_accel_func_size * sizeof(extra_glulx_accel_entry_t));
394 }
395 else {
396 tmp_accel_func_size = 2 * tmp_accel_func_count + 4;
397 tmp_accel_funcs = glulx_realloc(tmp_accel_funcs, tmp_accel_func_size * sizeof(extra_glulx_accel_entry_t));
398 }
399 }
400
401 tmp_accel_funcs[tmp_accel_func_count].index = index;
402 tmp_accel_funcs[tmp_accel_func_count].addr = addr;
403 tmp_accel_func_count++;
404 }
405
406 /* Copy extra chunks of the VM state into the (static) extra_state object. This is information needed by autosave, but not included in the regular save process.
407 */
408 static void stash_extra_state(extra_state_data_t *state)
409 {
410 glui32 count;
411
412 state->active = TRUE;
413
414 state->protectstart = protectstart;
415 state->protectend = protectend;
416 stream_get_iosys(&state->iosys_mode, &state->iosys_rock);
417 state->stringtable = stream_get_table();
418
419 count = accel_get_param_count();
420 state->accel_param_count = count;
421 state->accel_params = glulx_malloc(count * sizeof(extra_glulx_accel_param_t));
422 for (int ix=0; ix<count; ix++) {
423 state->accel_params[ix].param = accel_get_param(ix);
424 }
425
426 tmp_accel_func_count = 0;
427 tmp_accel_func_size = 0;
428 tmp_accel_funcs = NULL;
429
430 accel_iterate_funcs(&stash_one_accel_func);
431
432 state->accel_func_count = tmp_accel_func_count;
433 state->accel_funcs = tmp_accel_funcs;
434 tmp_accel_funcs = NULL;
435
436 if (gamefile) {
437 state->gamefiletag = glkunix_stream_get_updatetag(gamefile);
438 }
439
440 count = 0;
441 for (winid_t tmpwin = glk_window_iterate(NULL, NULL); tmpwin; tmpwin = glk_window_iterate(tmpwin, NULL))
442 count++;
443 for (strid_t tmpstr = glk_stream_iterate(NULL, NULL); tmpstr; tmpstr = glk_stream_iterate(tmpstr, NULL))
444 count++;
445 for (frefid_t tmpfref = glk_fileref_iterate(NULL, NULL); tmpfref; tmpfref = glk_fileref_iterate(tmpfref, NULL))
446 count++;
447
448 state->id_map_list_count = count;
449 if (count) {
450 state->id_map_list = glulx_malloc(count * sizeof(extra_glk_obj_id_entry_t));
451 }
452
453 glui32 ix = 0;
454 for (winid_t tmpwin = glk_window_iterate(NULL, NULL); tmpwin; tmpwin = glk_window_iterate(tmpwin, NULL)) {
455 state->id_map_list[ix].objclass = gidisp_Class_Window;
456 state->id_map_list[ix].tag = glkunix_window_get_updatetag(tmpwin);
457 state->id_map_list[ix].dispid = find_id_for_window(tmpwin);
458 ix++;
459 }
460 for (strid_t tmpstr = glk_stream_iterate(NULL, NULL); tmpstr; tmpstr = glk_stream_iterate(tmpstr, NULL)) {
461 state->id_map_list[ix].objclass = gidisp_Class_Stream;
462 state->id_map_list[ix].tag = glkunix_stream_get_updatetag(tmpstr);
463 state->id_map_list[ix].dispid = find_id_for_stream(tmpstr);
464 ix++;
465 }
466 for (frefid_t tmpfref = glk_fileref_iterate(NULL, NULL); tmpfref; tmpfref = glk_fileref_iterate(tmpfref, NULL)) {
467 state->id_map_list[ix].objclass = gidisp_Class_Fileref;
468 state->id_map_list[ix].tag = glkunix_fileref_get_updatetag(tmpfref);
469 state->id_map_list[ix].dispid = find_id_for_fileref(tmpfref);
470 ix++;
471 }
472
473 if (ix != state->id_map_list_count)
474 fatal_error("stash_extra_state: Glk object count mismatch");
475 }
476
477 /* Copy chunks of VM state out of the extra_state object.
478 */
479 static void recover_extra_state(extra_state_data_t *state)
480 {
481 if (!state->active) {
482 return;
483 }
484
485 protectstart = state->protectstart;
486 protectend = state->protectend;
487 stream_set_iosys(state->iosys_mode, state->iosys_rock);
488 stream_set_table(state->stringtable);
489
490 if (state->accel_params) {
491 for (int ix=0; ix<state->accel_param_count; ix++) {
492 accel_set_param(ix, state->accel_params[ix].param);
493 }
494 }
495
496 if (state->accel_funcs) {
497 for (int ix=0; ix<state->accel_func_count; ix++) {
498 accel_set_func(state->accel_funcs[ix].index, state->accel_funcs[ix].addr);
499 }
500 }
501
502 if (state->id_map_list) {
503 for (int ix=0; ix<state->id_map_list_count; ix++) {
504 extra_glk_obj_id_entry_t *entry = &state->id_map_list[ix];
505
506 switch (entry->objclass) {
507
508 case gidisp_Class_Window: {
509 winid_t win = glkunix_window_find_by_updatetag(entry->tag);
510 if (!win) {
511 nonfatal_warning_i("Could not find window for tag", entry->tag);
512 break;
513 }
514 glkunix_window_set_dispatch_rock(win, glulxe_classtable_register_existing(win, entry->objclass, entry->dispid));
515 }
516 break;
517
518 case gidisp_Class_Stream: {
519 strid_t str = glkunix_stream_find_by_updatetag(entry->tag);
520 if (!str) {
521 nonfatal_warning_i("Could not find stream for tag", entry->tag);
522 break;
523 }
524 glkunix_stream_set_dispatch_rock(str, glulxe_classtable_register_existing(str, entry->objclass, entry->dispid));
525 }
526 break;
527
528 case gidisp_Class_Fileref: {
529 frefid_t fref = glkunix_fileref_find_by_updatetag(entry->tag);
530 if (!fref) {
531 nonfatal_warning_i("Could not find fileref for tag", entry->tag);
532 break;
533 }
534 glkunix_fileref_set_dispatch_rock(fref, glulxe_classtable_register_existing(fref, entry->objclass, entry->dispid));
535 }
536 break;
537
538 }
539 }
540 }
541
542 if (state->gamefiletag) {
543 gamefile = glkunix_stream_find_by_updatetag(state->gamefiletag);
544
545 if (giblorb_get_resource_map()) {
546 /* It's inefficient to throw away the blorb chunk map, which we just loaded, and then recreate it. Oh well. */
547 giblorb_err_t err;
548 err = giblorb_unset_resource_map();
549 if (err)
550 fatal_error("Unable to clear blorb map");
551 err = giblorb_set_resource_map(gamefile);
552 if (err)
553 fatal_error("Unable to reset blorb map");
554 }
555 }
556 }
557
558 static extra_state_data_t *extra_state_data_alloc()
559 {
560 extra_state_data_t *state = glulx_malloc(sizeof(extra_state_data_t));
561 if (!state)
562 return NULL;
563
564 /* Everything gets initialized to zero/null */
565 memset(state, 0, sizeof(extra_state_data_t));
566 state->active = FALSE;
567 state->accel_param_count = 0;
568 state->accel_params = NULL;
569 state->accel_func_count = 0;
570 state->accel_funcs = NULL;
571 state->id_map_list_count = 0;
572 state->id_map_list = NULL;
573
574 return state;
575 }
576
577 static void extra_state_data_free(extra_state_data_t *state)
578 {
579 if (state->accel_params) {
580 glulx_free(state->accel_params);
581 state->accel_params = NULL;
582 }
583 if (state->accel_funcs) {
584 glulx_free(state->accel_funcs);
585 state->accel_funcs = NULL;
586 }
587 if (state->id_map_list) {
588 glulx_free(state->id_map_list);
589 state->id_map_list = NULL;
590 }
591 state->active = FALSE;
592
593 glulx_free(state);
594 }
595
596 static int extra_state_serialize(glkunix_serialize_context_t ctx, void *rock)
597 {
598 extra_state_data_t *state = rock;
599
600 if (state->active) {
601 glkunix_serialize_uint32(ctx, "glulx_extra_state", 1);
602
603 glkunix_serialize_uint32(ctx, "glulx_protectstart", state->protectstart);
604 glkunix_serialize_uint32(ctx, "glulx_protectend", state->protectend);
605 glkunix_serialize_uint32(ctx, "glulx_iosys_mode", state->iosys_mode);
606 glkunix_serialize_uint32(ctx, "glulx_iosys_rock", state->iosys_rock);
607 glkunix_serialize_uint32(ctx, "glulx_stringtable", state->stringtable);
608
609 if (state->accel_params) {
610 glkunix_serialize_object_list(ctx, "glulx_accel_params", extra_state_serialize_accel_param, state->accel_param_count, sizeof(extra_glulx_accel_param_t), state->accel_params);
611 }
612
613 if (state->accel_funcs) {
614 glkunix_serialize_object_list(ctx, "glulx_accel_funcs", extra_state_serialize_accel_func, state->accel_func_count, sizeof(extra_glulx_accel_entry_t), state->accel_funcs);
615 }
616
617 glkunix_serialize_uint32(ctx, "glulx_gamefiletag", state->gamefiletag);
618
619 if (state->id_map_list) {
620 glkunix_serialize_object_list(ctx, "glulx_id_map_list", extra_state_serialize_obj_id_entry, state->id_map_list_count, sizeof(extra_glk_obj_id_entry_t), state->id_map_list);
621 }
622
623 }
624
625 return TRUE;
626 }
627
628 static int extra_state_serialize_accel_param(glkunix_serialize_context_t ctx, void *rock)
629 {
630 extra_glulx_accel_param_t *param = rock;
631 glkunix_serialize_uint32(ctx, "param", param->param);
632 return TRUE;
633 }
634
635 static int extra_state_serialize_accel_func(glkunix_serialize_context_t ctx, void *rock)
636 {
637 extra_glulx_accel_entry_t *entry = rock;
638 glkunix_serialize_uint32(ctx, "index", entry->index);
639 glkunix_serialize_uint32(ctx, "addr", entry->addr);
640 return TRUE;
641 }
642
643 static int extra_state_serialize_obj_id_entry(glkunix_serialize_context_t ctx, void *rock)
644 {
645 extra_glk_obj_id_entry_t *obj = rock;
646 glkunix_serialize_uint32(ctx, "objclass", obj->objclass);
647 glkunix_serialize_uint32(ctx, "tag", obj->tag);
648 glkunix_serialize_uint32(ctx, "dispid", obj->dispid);
649 return TRUE;
650 }
651
652 static int extra_state_unserialize(glkunix_unserialize_context_t ctx, void *rock)
653 {
654 extra_state_data_t *state = (extra_state_data_t *)rock;
655
656 glui32 val;
657
658 if (!glkunix_unserialize_uint32(ctx, "glulx_extra_state", &val))
659 return FALSE;
660 if (!val)
661 return FALSE;
662
663 glkunix_unserialize_uint32(ctx, "glulx_protectstart", &state->protectstart);
664 glkunix_unserialize_uint32(ctx, "glulx_protectend", &state->protectend);
665 glkunix_unserialize_uint32(ctx, "glulx_iosys_mode", &state->iosys_mode);
666 glkunix_unserialize_uint32(ctx, "glulx_iosys_rock", &state->iosys_rock);
667 glkunix_unserialize_uint32(ctx, "glulx_stringtable", &state->stringtable);
668
669 glkunix_unserialize_context_t array;
670 int count;
671
672 if (glkunix_unserialize_list(ctx, "glulx_accel_params", &array, &count)) {
673 if (count) {
674 state->accel_param_count = count;
675 state->accel_params = glulx_malloc(count * sizeof(extra_glulx_accel_param_t));
676 memset(state->accel_params, 0, count * sizeof(extra_glulx_accel_param_t));
677 if (!glkunix_unserialize_object_list_entries(array, extra_state_unserialize_accel_param, count, sizeof(extra_glulx_accel_param_t), state->accel_params))
678 return FALSE;
679 }
680 }
681
682 if (glkunix_unserialize_list(ctx, "glulx_accel_funcs", &array, &count)) {
683 if (count) {
684 state->accel_func_count = count;
685 state->accel_funcs = glulx_malloc(count * sizeof(extra_glulx_accel_entry_t));
686 memset(state->accel_funcs, 0, count * sizeof(extra_glulx_accel_entry_t));
687 if (!glkunix_unserialize_object_list_entries(array, extra_state_unserialize_accel_func, count, sizeof(extra_glulx_accel_entry_t), state->accel_funcs))
688 return FALSE;
689 }
690 }
691
692 glkunix_unserialize_uint32(ctx, "glulx_gamefiletag", &state->gamefiletag);
693
694 if (glkunix_unserialize_list(ctx, "glulx_id_map_list", &array, &count)) {
695 if (count) {
696 state->id_map_list_count = count;
697 state->id_map_list = glulx_malloc(count * sizeof(extra_glk_obj_id_entry_t));
698 memset(state->id_map_list, 0, count * sizeof(extra_glk_obj_id_entry_t));
699 if (!glkunix_unserialize_object_list_entries(array, extra_state_unserialize_obj_id_entry, count, sizeof(extra_glk_obj_id_entry_t), state->id_map_list))
700 return FALSE;
701 }
702 }
703
704 state->active = TRUE;
705
706 return TRUE;
707 }
708
709 static int extra_state_unserialize_accel_param(glkunix_unserialize_context_t ctx, void *rock)
710 {
711 extra_glulx_accel_param_t *param = rock;
712 glkunix_unserialize_uint32(ctx, "param", &param->param);
713 return TRUE;
714 }
715
716 static int extra_state_unserialize_accel_func(glkunix_unserialize_context_t ctx, void *rock)
717 {
718 extra_glulx_accel_entry_t *entry = rock;
719 glkunix_unserialize_uint32(ctx, "index", &entry->index);
720 glkunix_unserialize_uint32(ctx, "addr", &entry->addr);
721 return TRUE;
722 }
723
724 static int extra_state_unserialize_obj_id_entry(glkunix_unserialize_context_t ctx, void *rock)
725 {
726 extra_glk_obj_id_entry_t *obj = rock;
727 glkunix_unserialize_uint32(ctx, "objclass", &obj->objclass);
728 glkunix_unserialize_uint32(ctx, "tag", &obj->tag);
729 glkunix_unserialize_uint32(ctx, "dispid", &obj->dispid);
730 return TRUE;
731 }
732
733 #endif /* GLKUNIX_AUTOSAVE_FEATURES */