Debian Codegraph @

terps/tads/tads2/fio.c

raw symbols SCIP
1 #ifdef RCSID
2 static char RCSid[] =
3 "$Header: d:/cvsroot/tads/TADS2/FIO.C,v 1.4 1999/07/11 00:46:29 MJRoberts Exp $";
4 #endif
5
6 /*
7 * Copyright (c) 1992, 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 fio.c - file i/o functions
15 Function
16 file i/o: read game, write game, save game, restore game
17 Notes
18 none
19 Modified
20 04/11/99 CNebel - Include appctx.h, not trd.h.
21 10/23/97 CNebel - removed now-obsolete Think C hack.
22 04/02/92 MJRoberts - creation
23 */
24
25 #include <stdlib.h>
26 #include <string.h>
27
28 #include "os.h"
29 #include "std.h"
30 #include "appctx.h"
31 #include "mch.h"
32 #include "mcm.h"
33 #include "mcl.h"
34 #include "tok.h"
35 #include "obj.h"
36 #include "voc.h"
37 #include "fio.h"
38 #include "dat.h"
39 #include "prs.h"
40 #include "linf.h"
41 #include "cmap.h"
42
43
44 /* compare a resource string */
45 /* int fioisrsc(uchar *filbuf, char *refnam); */
46 #define fioisrsc(filbuf, refnam) \
47 (((filbuf)[0] == strlen(refnam)) && \
48 !memcmp(filbuf+1, refnam, (size_t)((filbuf)[0])))
49
50 /* callback to load an object on demand */
51 void OS_LOADDS fioldobj(void *ctx0, mclhd handle, uchar *ptr, ushort siz)
52 {
53 fiolcxdef *ctx = (fiolcxdef *)ctx0;
54 ulong seekpos = (ulong)handle;
55 osfildef *fp = ctx->fiolcxfp;
56 char buf[7];
57 errcxdef *ec = ctx->fiolcxerr;
58 uint rdsiz;
59
60 /* figure out what type of object is to be loaded */
61 osfseek(fp, seekpos + ctx->fiolcxst, OSFSK_SET);
62 if (osfrb(fp, buf, 7)) errsig(ec, ERR_LDGAM);
63 switch(buf[0])
64 {
65 case TOKSTFUNC:
66 rdsiz = osrp2(buf + 3);
67 break;
68
69 case TOKSTOBJ:
70 rdsiz = osrp2(buf + 5);
71 break;
72
73 case TOKSTFWDOBJ:
74 case TOKSTFWDFN:
75 default:
76 errsig(ec, ERR_UNKOTYP);
77 }
78
79 if (siz < rdsiz) errsig(ec, ERR_LDBIG);
80 if (osfrb(fp, ptr, rdsiz)) errsig(ec, ERR_LDGAM);
81 if (ctx->fiolcxflg & FIOFCRYPT)
82 fioxor(ptr, rdsiz, ctx->fiolcxseed, ctx->fiolcxinc);
83 }
84
85 /* shut down load-on-demand subsystem (close load file) */
86 void fiorcls(fiolcxdef *ctx)
87 {
88 if (ctx != 0 && ctx->fiolcxfp != 0)
89 {
90 /* close the file */
91 osfcls(ctx->fiolcxfp);
92
93 /* forget the file object */
94 ctx->fiolcxfp = 0;
95 }
96 }
97
98 /*
99 * Read an HTMLRES resource map
100 */
101 static void fiordhtml(errcxdef *ec, osfildef *fp, appctxdef *appctx,
102 int resfileno, const char *resfilename)
103 {
104 uchar buf[256];
105
106 /*
107 * resource map - if the host system is interested, tell it about it
108 */
109 if (appctx != 0)
110 {
111 ulong entry_cnt;
112 ulong i;
113
114 /* read the index table header */
115 if (osfrb(fp, buf, 8))
116 errsig1(ec, ERR_RDRSC, ERRTSTR,
117 errstr(ec, resfilename, strlen(resfilename)));
118
119 /* get the number of entries in the table */
120 entry_cnt = osrp4(buf);
121
122 /* read the index entries */
123 for (i = 0 ; i < entry_cnt ; ++i)
124 {
125 ulong res_ofs;
126 ulong res_siz;
127 ushort res_namsiz;
128
129 /* read this entry */
130 if (osfrb(fp, buf, 10))
131 errsig1(ec, ERR_RDRSC, ERRTSTR,
132 errstr(ec, resfilename, strlen(resfilename)));
133
134 /* get the entry header */
135 res_ofs = osrp4(buf);
136 res_siz = osrp4(buf + 4);
137 res_namsiz = osrp2(buf + 8);
138
139 /* read this entry's name */
140 if (osfrb(fp, buf, res_namsiz))
141 errsig1(ec, ERR_RDRSC, ERRTSTR,
142 errstr(ec, resfilename, strlen(resfilename)));
143
144 /* tell the host system about this entry */
145 if (appctx->add_resource)
146 (*appctx->add_resource)(appctx->add_resource_ctx,
147 res_ofs, res_siz,
148 (char *)buf,
149 (size_t)res_namsiz,
150 resfileno);
151 }
152
153 /* tell the host system where the resources start */
154 if (appctx->set_resmap_seek != 0)
155 {
156 long pos = osfpos(fp);
157 (*appctx->set_resmap_seek)(appctx->set_resmap_seek_ctx,
158 pos, resfileno);
159 }
160 }
161 }
162
163 /*
164 * Read an external resource file. This is a limited version of the
165 * general file reader that can only read resource files, not full game
166 * files.
167 */
168 static void fiordrscext(errcxdef *ec, osfildef *fp, appctxdef *appctx,
169 int resfileno, char *resfilename)
170 {
171 uchar buf[TOKNAMMAX + 50];
172 unsigned long endpos;
173 unsigned long startofs;
174
175 /* note the starting offset */
176 startofs = osfpos(fp);
177
178 /* check file and version headers, and get flags and timestamp */
179 if (osfrb(fp, buf, (int)(sizeof(FIOFILHDR) + sizeof(FIOVSNHDR) + 2)))
180 errsig1(ec, ERR_RDRSC, ERRTSTR,
181 errstr(ec, resfilename, strlen(resfilename)));
182 if (memcmp(buf, FIOFILHDRRSC, (size_t)sizeof(FIOFILHDRRSC)))
183 errsig1(ec, ERR_BADHDRRSC, ERRTSTR,
184 errstr(ec, resfilename, strlen(resfilename)));
185 if (memcmp(buf + sizeof(FIOFILHDR), FIOVSNHDR,
186 (size_t)sizeof(FIOVSNHDR))
187 && memcmp(buf + sizeof(FIOFILHDR), FIOVSNHDR2,
188 (size_t)sizeof(FIOVSNHDR2))
189 && memcmp(buf + sizeof(FIOFILHDR), FIOVSNHDR3,
190 (size_t)sizeof(FIOVSNHDR3)))
191 errsig(ec, ERR_BADVSN);
192 if (osfrb(fp, buf, (size_t)26))
193 errsig1(ec, ERR_RDRSC, ERRTSTR,
194 errstr(ec, resfilename, strlen(resfilename)));
195
196 /* now read resources from the file */
197 for (;;)
198 {
199 /* read resource type and next-resource pointer */
200 if (osfrb(fp, buf, 1)
201 || osfrb(fp, buf + 1, (int)(buf[0] + 4)))
202 errsig1(ec, ERR_RDRSC, ERRTSTR,
203 errstr(ec, resfilename, strlen(resfilename)));
204 endpos = osrp4(buf + 1 + buf[0]);
205
206 /* check the resource type */
207 if (fioisrsc(buf, "HTMLRES"))
208 {
209 /* read the HTML resource map */
210 fiordhtml(ec, fp, appctx, resfileno, resfilename);
211
212 /*
213 * skip the resources - they're entirely for the host
214 * application's use
215 */
216 osfseek(fp, endpos + startofs, OSFSK_SET);
217 }
218 else if (fioisrsc(buf, "$EOF"))
219 {
220 /* we're done reading the file */
221 break;
222 }
223 else
224 errsig(ec, ERR_UNKRSC);
225 }
226 }
227
228 /*
229 * read a game from a binary file
230 *
231 * flags:
232 * &1 ==> run preinit
233 * &2 ==> preload objects
234 */
235 static void fiord1(mcmcxdef *mctx, voccxdef *vctx, tokcxdef *tctx,
236 osfildef *fp, const char *fname,
237 fiolcxdef *setupctx, ulong startofs,
238 objnum *preinit, uint *flagp, tokpdef *path,
239 uchar **fmtsp, uint *fmtlp, uint *pcntptr, int flags,
240 appctxdef *appctx, char *argv0)
241 {
242 int i;
243 int siz;
244 uchar buf[TOKNAMMAX + 50];
245 errcxdef *ec = vctx->voccxerr;
246 ulong endpos;
247 int obj;
248 ulong curpos;
249 runxdef *ex;
250 ulong eof_reset = 0; /* reset here at EOF if non-zero */
251 #if 0 // XFCNs are obsolete
252 int xfcns_done = FALSE; /* already loaded XFCNs */
253 #endif
254 ulong xfcn_pos = 0; /* location of XFCN's if preloadable */
255 uint xor_seed = 17; /* seed value for fioxor */
256 uint xor_inc = 29; /* increment value for fioxor */
257
258 /* set up loader callback context */
259 setupctx->fiolcxfp = fp;
260 setupctx->fiolcxerr = ec;
261 setupctx->fiolcxst = startofs;
262 setupctx->fiolcxseed = xor_seed;
263 setupctx->fiolcxinc = xor_inc;
264
265 /* check file and version headers, and get flags and timestamp */
266 if (osfrb(fp, buf, (int)(sizeof(FIOFILHDR) + sizeof(FIOVSNHDR) + 2)))
267 errsig(ec, ERR_RDGAM);
268 if (memcmp(buf, FIOFILHDR, (size_t)sizeof(FIOFILHDR)))
269 errsig(ec, ERR_BADHDR);
270 if (memcmp(buf + sizeof(FIOFILHDR), FIOVSNHDR,
271 (size_t)sizeof(FIOVSNHDR))
272 && memcmp(buf + sizeof(FIOFILHDR), FIOVSNHDR2,
273 (size_t)sizeof(FIOVSNHDR2))
274 && memcmp(buf + sizeof(FIOFILHDR), FIOVSNHDR3,
275 (size_t)sizeof(FIOVSNHDR3)))
276 errsig(ec, ERR_BADVSN);
277 if (osfrb(fp, vctx->voccxtim, (size_t)26)) errsig(ec, ERR_RDGAM);
278
279 /*
280 * if the game wasn't compiled with 2.2 or later, make a note,
281 * because we need to ignore certain property flags (due to a bug in
282 * the old compiler)
283 */
284 if (memcmp(buf + sizeof(FIOFILHDR), FIOVSNHDR2,
285 (size_t)sizeof(FIOVSNHDR2)) == 0
286 || memcmp(buf + sizeof(FIOFILHDR), FIOVSNHDR3,
287 (size_t)sizeof(FIOVSNHDR3)) == 0)
288 mctx->mcmcxflg |= MCMCXF_NO_PRP_DEL;
289
290 setupctx->fiolcxflg =
291 *flagp = osrp2(buf + sizeof(FIOFILHDR) + sizeof(FIOVSNHDR));
292
293 /* now read resources from the file */
294 for (;;)
295 {
296 /* read resource type and next-resource pointer */
297 if (osfrb(fp, buf, 1)
298 || osfrb(fp, buf + 1, (int)(buf[0] + 4)))
299 errsig(ec, ERR_RDGAM);
300 endpos = osrp4(buf + 1 + buf[0]);
301
302 if (fioisrsc(buf, "OBJ"))
303 {
304 /* skip regular objects if fast-load records are included */
305 if (*flagp & FIOFFAST)
306 {
307 osfseek(fp, endpos + startofs, OSFSK_SET);
308 continue;
309 }
310
311 curpos = osfpos(fp) - startofs;
312 while (curpos != endpos)
313 {
314 /* read type and object number */
315 if (osfrb(fp, buf, 3)) errsig(ec, ERR_RDGAM);
316 obj = osrp2(buf+1);
317
318 switch(buf[0])
319 {
320 case TOKSTFUNC:
321 case TOKSTOBJ:
322 if (osfrb(fp, buf + 3, 4)) errsig(ec, ERR_RDGAM);
323 mcmrsrv(mctx, (ushort)osrp2(buf + 3), (mcmon)obj,
324 (mclhd)curpos);
325 curpos += osrp2(buf + 5) + 7;
326
327 /* load object if preloading */
328 if (flags & 2)
329 {
330 (void)mcmlck(mctx, (mcmon)obj);
331 mcmunlck(mctx, (mcmon)obj);
332 }
333
334 /* seek past this object */
335 osfseek(fp, curpos + startofs, OSFSK_SET);
336 break;
337
338 case TOKSTFWDOBJ:
339 case TOKSTFWDFN:
340 {
341 ushort siz;
342 uchar *p;
343
344 if (osfrb(fp, buf+3, 2)) errsig(ec, ERR_RDGAM);
345 siz = osrp2(buf+3);
346 p = mcmalonum(mctx, siz, (mcmon)obj);
347 if (osfrb(fp, p, siz)) errsig(ec, ERR_RDGAM);
348 mcmunlck(mctx, (mcmon)obj);
349 curpos += 5 + siz;
350 break;
351 }
352
353 case TOKSTEXTERN:
354 if (!vctx->voccxrun->runcxext)
355 errsig(ec, ERR_UNXEXT);
356 ex = &vctx->voccxrun->runcxext[obj];
357
358 if (osfrb(fp, buf + 3, 1)
359 || osfrb(fp, ex->runxnam, (int)buf[3]))
360 errsig(ec, ERR_RDGAM);
361 ex->runxnam[buf[3]] = '\0';
362 curpos += buf[3] + 4;
363 break;
364
365 default:
366 errsig(ec, ERR_UNKOTYP);
367 }
368 }
369 }
370 else if (fioisrsc(buf, "FST"))
371 {
372 uchar *p;
373 uchar *bufp;
374 ulong siz;
375
376 if (!(*flagp & FIOFFAST))
377 {
378 osfseek(fp, endpos + startofs, OSFSK_SET);
379 continue;
380 }
381
382 curpos = osfpos(fp) - startofs;
383 siz = endpos - curpos;
384 if (siz && siz < OSMALMAX
385 && (bufp = p = (uchar *)osmalloc((size_t)siz)) != 0)
386 {
387 uchar *p1;
388 ulong siz2;
389 uint sizcur;
390
391 for (p1 = p, siz2 = siz ; siz2 ; siz2 -= sizcur, p1 += sizcur)
392 {
393 sizcur = (siz2 > (uint)0xffff ? (uint)0xffff : siz2);
394 if (osfrb(fp, p1, sizcur)) errsig(ec, ERR_RDGAM);
395 }
396
397 while (siz)
398 {
399 obj = osrp2(p + 1);
400 switch(*p)
401 {
402 case TOKSTFUNC:
403 case TOKSTOBJ:
404 mcmrsrv(mctx, (ushort)osrp2(p + 3), (mcmon)obj,
405 (mclhd)osrp4(p + 7));
406 p += 11;
407 siz -= 11;
408
409 /* preload object if desired */
410 if (flags & 2)
411 {
412 (void)mcmlck(mctx, (mcmon)obj);
413 mcmunlck(mctx, (mcmon)obj);
414 }
415 break;
416
417 case TOKSTEXTERN:
418 if (!vctx->voccxrun->runcxext)
419 errsig(ec, ERR_UNXEXT);
420 ex = &vctx->voccxrun->runcxext[obj];
421
422 memcpy(ex->runxnam, p + 4, (size_t)p[3]);
423 ex->runxnam[p[3]] = '\0';
424 siz -= p[3] + 4;
425 p += p[3] + 4;
426 break;
427
428 default:
429 errsig(ec, ERR_UNKOTYP);
430 }
431 }
432
433 /* done with temporary block; free it */
434 osfree(bufp);
435 osfseek(fp, endpos + startofs, OSFSK_SET);
436 }
437 else
438 {
439 while (curpos != endpos)
440 {
441 if (osfrb(fp, buf, 3)) errsig(ec, ERR_RDGAM);
442 obj = osrp2(buf + 1);
443 switch(buf[0])
444 {
445 case TOKSTFUNC:
446 case TOKSTOBJ:
447 if (osfrb(fp, buf + 3, 8)) errsig(ec, ERR_RDGAM);
448 mcmrsrv(mctx, (ushort)osrp2(buf + 3), (mcmon)obj,
449 (mclhd)osrp4(buf + 7));
450 curpos += 11;
451
452 /* preload object if desired */
453 if (flags & 2)
454 {
455 (void)mcmlck(mctx, (mcmon)obj);
456 mcmunlck(mctx, (mcmon)obj);
457 osfseek(fp, curpos + startofs, OSFSK_SET);
458 }
459 break;
460
461 case TOKSTEXTERN:
462 if (!vctx->voccxrun->runcxext)
463 errsig(ec, ERR_UNXEXT);
464 ex = &vctx->voccxrun->runcxext[obj];
465
466 if (osfrb(fp, buf + 3, 1)
467 || osfrb(fp, ex->runxnam, (int)buf[3]))
468 errsig(ec, ERR_RDGAM);
469 ex->runxnam[buf[3]] = '\0';
470 curpos += buf[3] + 4;
471 break;
472
473 default:
474 errsig(ec, ERR_UNKOTYP);
475 }
476 }
477 }
478
479 /* if we can preload xfcn's, do so now */
480 if (xfcn_pos)
481 {
482 eof_reset = endpos; /* remember to return here when done */
483 osfseek(fp, xfcn_pos, OSFSK_SET); /* go to xfcn's */
484 }
485 }
486 else if (fioisrsc(buf, "XFCN"))
487 {
488 if (!vctx->voccxrun->runcxext) errsig(ec, ERR_UNXEXT);
489
490 /* read length and name of resource */
491 if (osfrb(fp, buf, 3) || osfrb(fp, buf + 3, (int)buf[2]))
492 errsig(ec, ERR_RDGAM);
493 siz = osrp2(buf);
494
495 #if 0
496 /*
497 * external functions are now obsolete - do not load
498 */
499
500 /* look for an external function with the same name */
501 for (i = vctx->voccxrun->runcxexc, ex = vctx->voccxrun->runcxext
502 ; i ; ++ex, --i)
503 {
504 j = strlen(ex->runxnam);
505 if (j == buf[2] && !memcmp(buf + 3, ex->runxnam, (size_t)j))
506 break;
507 }
508
509 /* if we found an external function of this name, load it */
510 if (i && !xfcns_done)
511 {
512 /* load the function */
513 ex->runxptr = os_exfld(fp, (unsigned)siz);
514 }
515 else
516 {
517 /* this XFCN isn't used; don't bother loading it */
518 osfseek(fp, endpos + startofs, OSFSK_SET);
519 }
520 #else
521 /* external functions are obsolete; simply skip the data */
522 osfseek(fp, endpos + startofs, OSFSK_SET);
523 #endif
524 }
525 else if (fioisrsc(buf, "HTMLRES"))
526 {
527 /* read the resources */
528 fiordhtml(ec, fp, appctx, 0, fname);
529
530 /*
531 * skip the resources - they're entirely for the host
532 * application's use
533 */
534 osfseek(fp, endpos + startofs, OSFSK_SET);
535 }
536 else if (fioisrsc(buf, "INH"))
537 {
538 uchar *p;
539 uchar *bufp;
540 ulong siz;
541
542 /* do it in a single file read, if we can, for speed */
543 curpos = osfpos(fp) - startofs;
544 siz = endpos - curpos;
545 if (siz && siz < OSMALMAX
546 && (bufp = p = (uchar *)osmalloc((size_t)siz)) != 0)
547 {
548 uchar *p1;
549 ulong siz2;
550 uint sizcur;
551
552 for (p1 = p, siz2 = siz ; siz2 ; siz2 -= sizcur, p1 += sizcur)
553 {
554 sizcur = (siz2 > (uint)0xffff ? (uint)0xffff : siz2);
555 if (osfrb(fp, p1, sizcur)) errsig(ec, ERR_RDGAM);
556 }
557
558 while (siz)
559 {
560 i = osrp2(p + 7);
561 obj = osrp2(p + 1);
562
563 vociadd(vctx, (objnum)obj, (objnum)osrp2(p+3), i,
564 (objnum *)(p + 9), p[0] | VOCIFXLAT);
565 vocinh(vctx, obj)->vociilc = osrp2(p + 5);
566
567 p += 9 + (2 * i);
568 siz -= 9 + (2 * i);
569 }
570
571 /* done with temporary block; free it */
572 osfree(bufp);
573 }
574 else
575 {
576 while (curpos != endpos)
577 {
578 if (osfrb(fp, buf, 9)) errsig(ec, ERR_RDGAM);
579 i = osrp2(buf + 7); /* get number of superclasses */
580 obj = osrp2(buf + 1); /* get object number */
581 if (i && osfrb(fp, buf + 9, 2 * i)) errsig(ec, ERR_RDGAM);
582
583 vociadd(vctx, (objnum)obj, (objnum)osrp2(buf+3),
584 i, (objnum *)(buf + 9), buf[0] | VOCIFXLAT);
585 vocinh(vctx, obj)->vociilc = osrp2(buf + 5);
586
587 curpos += 9 + (2 * i);
588 }
589 }
590 }
591 else if (fioisrsc(buf, "REQ"))
592 {
593 curpos = osfpos(fp) - startofs;
594 siz = endpos - curpos;
595
596 if (osfrb(fp, buf, (uint)siz)) errsig(ec, ERR_RDGAM);
597 vctx->voccxme = vctx->voccxme_init = osrp2(buf);
598 vctx->voccxvtk = osrp2(buf+2);
599 vctx->voccxstr = osrp2(buf+4);
600 vctx->voccxnum = osrp2(buf+6);
601 vctx->voccxprd = osrp2(buf+8);
602 vctx->voccxvag = osrp2(buf+10);
603 vctx->voccxini = osrp2(buf+12);
604 vctx->voccxpre = osrp2(buf+14);
605 vctx->voccxper = osrp2(buf+16);
606
607 /* if we have a cmdPrompt function, read it */
608 if (siz >= 20)
609 vctx->voccxprom = osrp2(buf + 18);
610 else
611 vctx->voccxprom = MCMONINV;
612
613 /* if we have the NLS functions, read them */
614 if (siz >= 26)
615 {
616 vctx->voccxpdis = osrp2(buf + 20);
617 vctx->voccxper2 = osrp2(buf + 22);
618 vctx->voccxpdef = osrp2(buf + 24);
619 }
620 else
621 {
622 /* the new NLS functions aren't defined in this file */
623 vctx->voccxpdis = MCMONINV;
624 vctx->voccxper2 = MCMONINV;
625 vctx->voccxpdef = MCMONINV;
626 }
627
628 /* test for parseAskobj separately, as it was added later */
629 if (siz >= 28)
630 vctx->voccxpask = osrp2(buf + 26);
631 else
632 vctx->voccxpask = MCMONINV;
633
634 /* test for preparseCmd separately - it's another late comer */
635 if (siz >= 30)
636 vctx->voccxppc = osrp2(buf + 28);
637 else
638 vctx->voccxppc = MCMONINV;
639
640 /* check for parseAskobjActor separately - another late comer */
641 if (siz >= 32)
642 vctx->voccxpask2 = osrp2(buf + 30);
643 else
644 vctx->voccxpask2 = MCMONINV;
645
646 /* if we have parseErrorParam, read it as well */
647 if (siz >= 34)
648 {
649 vctx->voccxperp = osrp2(buf + 32);
650 }
651 else
652 {
653 /* parseErrorParam isn't defined in this file */
654 vctx->voccxperp = MCMONINV;
655 }
656
657 /*
658 * if we have commandAfterRead and initRestore, read them as
659 * well
660 */
661 if (siz >= 38)
662 {
663 vctx->voccxpostprom = osrp2(buf + 34);
664 vctx->voccxinitrestore = osrp2(buf + 36);
665 }
666 else
667 {
668 /* these new functions aren't defined in this game */
669 vctx->voccxpostprom = MCMONINV;
670 vctx->voccxinitrestore = MCMONINV;
671 }
672
673 /* check for and read parseUnknownVerb, parseNounPhrase */
674 if (siz >= 42)
675 {
676 vctx->voccxpuv = osrp2(buf + 38);
677 vctx->voccxpnp = osrp2(buf + 40);
678 }
679 else
680 {
681 vctx->voccxpuv = MCMONINV;
682 vctx->voccxpnp = MCMONINV;
683 }
684
685 /* check for postAction, endCommand */
686 if (siz >= 48)
687 {
688 vctx->voccxpostact = osrp2(buf + 42);
689 vctx->voccxendcmd = osrp2(buf + 44);
690 vctx->voccxprecmd = osrp2(buf + 46);
691 }
692 else
693 {
694 vctx->voccxpostact = MCMONINV;
695 vctx->voccxendcmd = MCMONINV;
696 vctx->voccxprecmd = MCMONINV;
697 }
698
699 /* check for parseAskobjIndirect */
700 if (siz >= 50)
701 vctx->voccxpask3 = osrp2(buf + 48);
702 else
703 vctx->voccxpask3 = MCMONINV;
704
705 /* check for preparseExt and parseDefaultExt */
706 if (siz >= 54)
707 {
708 vctx->voccxpre2 = osrp2(buf + 50);
709 vctx->voccxpdef2 = osrp2(buf + 52);
710 }
711 else
712 {
713 vctx->voccxpre2 = MCMONINV;
714 vctx->voccxpdef2 = MCMONINV;
715 }
716 }
717 else if (fioisrsc(buf, "VOC"))
718 {
719 uchar *p;
720 uchar *bufp;
721 ulong siz;
722 int len1;
723 int len2;
724
725 /* do it in a single file read, if we can, for speed */
726 curpos = osfpos(fp) - startofs;
727 siz = endpos - curpos;
728 if (siz && siz < OSMALMAX
729 && (bufp = p = (uchar *)osmalloc((size_t)siz)) != 0)
730 {
731 uchar *p1;
732 ulong siz2;
733 uint sizcur;
734
735 for (p1 = p, siz2 = siz ; siz2 ; siz2 -= sizcur, p1 += sizcur)
736 {
737 sizcur = (siz2 > (uint)0xffff ? (uint)0xffff : siz2);
738 if (osfrb(fp, p1, sizcur)) errsig(ec, ERR_RDGAM);
739 }
740
741 while (siz)
742 {
743 len1 = osrp2(p);
744 len2 = osrp2(p + 2);
745 if (*flagp & FIOFCRYPT)
746 fioxor(p + 10, (uint)(len1 + len2),
747 xor_seed, xor_inc);
748 vocadd2(vctx, (prpnum)osrp2(p+4), (objnum)osrp2(p+6),
749 osrp2(p+8), p + 10, len1,
750 (len2 ? p + 10 + len1 : (uchar*)0), len2);
751
752 p += 10 + len1 + len2;
753 siz -= 10 + len1 + len2;
754 }
755
756 /* done with the temporary block; free it up */
757 osfree(bufp);
758 }
759 else
760 {
761 /* can't do it in one file read; do it the slow way */
762 while (curpos != endpos)
763 {
764 if (osfrb(fp, buf, 10)
765 || osfrb(fp, buf + 10,
766 (len1 = osrp2(buf)) + (len2 = osrp2(buf + 2))))
767 errsig(ec, ERR_RDGAM);
768
769 if (*flagp & FIOFCRYPT)
770 fioxor(buf + 10, (uint)(len1 + len2),
771 xor_seed, xor_inc);
772 vocadd2(vctx, (prpnum)osrp2(buf+4), (objnum)osrp2(buf+6),
773 osrp2(buf+8), buf + 10, len1,
774 (len2 ? buf + 10 + len1 : (uchar*)0), len2);
775 curpos += 10 + len1 + len2;
776 }
777 }
778 }
779 else if (fioisrsc(buf, "FMTSTR"))
780 {
781 uchar *fmts;
782 uint fmtl;
783
784 if (osfrb(fp, buf, 2)) errsig(ec, ERR_RDGAM);
785 fmtl = osrp2(buf);
786 fmts = mchalo(vctx->voccxerr, fmtl, "fiord1");
787 if (osfrb(fp, fmts, fmtl)) errsig(ec, ERR_RDGAM);
788 if (*flagp & FIOFCRYPT) fioxor(fmts, fmtl, xor_seed, xor_inc);
789 tiosetfmt(vctx->voccxtio, vctx->voccxrun, fmts, fmtl);
790
791 if (fmtsp) *fmtsp = fmts;
792 if (fmtlp) *fmtlp = fmtl;
793 }
794 else if (fioisrsc(buf, "CMPD"))
795 {
796 if (osfrb(fp, buf, 2)) errsig(ec, ERR_RDGAM);
797 vctx->voccxcpl = osrp2(buf);
798 vctx->voccxcpp = (char *)mchalo(vctx->voccxerr,
799 vctx->voccxcpl, "fiord1");
800 if (osfrb(fp, vctx->voccxcpp, (uint)vctx->voccxcpl))
801 errsig(ec, ERR_RDGAM);
802 if (*flagp & FIOFCRYPT)
803 fioxor((uchar *)vctx->voccxcpp, (uint)vctx->voccxcpl,
804 xor_seed, xor_inc);
805 }
806 else if (fioisrsc(buf, "SPECWORD"))
807 {
808 if (osfrb(fp, buf, 2)) errsig(ec, ERR_RDGAM);
809 vctx->voccxspl = osrp2(buf);
810 vctx->voccxspp = (char *)mchalo(vctx->voccxerr,
811 vctx->voccxspl, "fiord1");
812 if (osfrb(fp, vctx->voccxspp, (uint)vctx->voccxspl))
813 errsig(ec, ERR_RDGAM);
814 if (*flagp & FIOFCRYPT)
815 fioxor((uchar *)vctx->voccxspp, (uint)vctx->voccxspl,
816 xor_seed, xor_inc);
817 }
818 else if (fioisrsc(buf, "SYMTAB"))
819 {
820 tokthdef *symtab;
821
822 /* if there's no debugger context, don't bother with this */
823 if (!vctx->voccxrun->runcxdbg)
824 {
825 osfseek(fp, endpos + startofs, OSFSK_SET);
826 continue;
827 }
828
829 if (!(symtab = vctx->voccxrun->runcxdbg->dbgcxtab))
830 {
831 symtab = (tokthdef *)mchalo(ec, sizeof(tokthdef),
832 "fiord:symtab");
833 tokthini(ec, mctx, (toktdef *)symtab);
834 vctx->voccxrun->runcxdbg->dbgcxtab = symtab;
835 }
836
837 /* read symbols until we find a zero-length symbol */
838 for (;;)
839 {
840 int hash;
841
842 if (osfrb(fp, buf, 4)) errsig(ec, ERR_RDGAM);
843 if (buf[0] == 0) break;
844 if (osfrb(fp, buf + 4, (int)buf[0])) errsig(ec, ERR_RDGAM);
845 buf[4 + buf[0]] = '\0';
846 hash = tokhsh((char *)buf + 4);
847
848 (*symtab->tokthsc.toktfadd)((toktdef *)symtab,
849 (char *)buf + 4,
850 (int)buf[0], (int)buf[1],
851 osrp2(buf + 2), hash);
852 }
853 }
854 else if (fioisrsc(buf, "SRC"))
855 {
856 /* skip source file id's if there's no debugger context */
857 if (vctx->voccxrun->runcxdbg == 0)
858 {
859 osfseek(fp, endpos + startofs, OSFSK_SET);
860 continue;
861 }
862
863 while ((osfpos(fp) - startofs) != endpos)
864 {
865 /* the only thing we know how to read is linfdef's */
866 if (linfload(fp, vctx->voccxrun->runcxdbg, ec, path))
867 errsig(ec, ERR_RDGAM);
868 }
869 }
870 else if (fioisrsc(buf, "SRC2"))
871 {
872 /*
873 * this is simply a marker indicating that we have new-style
874 * (line-number-based) source debugging information in the
875 * file -- set the new-style debug info flag
876 */
877 if (vctx->voccxrun->runcxdbg != 0)
878 vctx->voccxrun->runcxdbg->dbgcxflg |= DBGCXFLIN2;
879
880 /* the contents are empty - skip the block */
881 osfseek(fp, endpos + startofs, OSFSK_SET);
882 }
883 else if (fioisrsc(buf, "PREINIT"))
884 {
885 if (osfrb(fp, buf, 2)) errsig(ec, ERR_RDGAM);
886 *preinit = osrp2(buf);
887 }
888 else if (fioisrsc(buf, "ERRMSG"))
889 {
890 errini(ec, fp);
891 osfseek(fp, endpos + startofs, OSFSK_SET);
892 }
893 else if (fioisrsc(buf, "EXTCNT"))
894 {
895 uchar *p;
896 ushort len;
897 ulong siz;
898
899 curpos = osfpos(fp) - startofs;
900 siz = endpos - curpos;
901 if (osfrb(fp, buf, 2)) errsig(ec, ERR_RDGAM);
902 i = osrp2(buf);
903
904 len = i * sizeof(runxdef);
905 p = mchalo(ec, len, "fiord:runxdef");
906 memset(p, 0, (size_t)len);
907
908 vctx->voccxrun->runcxext = (runxdef *)p;
909 vctx->voccxrun->runcxexc = i;
910
911 /* see if start-of-XFCN information is present */
912 if (siz >= 6)
913 {
914 /* get location of first XFCN, and seek there */
915 if (osfrb(fp, buf, 4)) errsig(ec, ERR_RDGAM);
916 xfcn_pos = osrp4(buf);
917 }
918
919 /* seek past this resource */
920 osfseek(fp, endpos + startofs, OSFSK_SET);
921 }
922 else if (fioisrsc(buf, "PRPCNT"))
923 {
924 if (osfrb(fp, buf, 2)) errsig(ec, ERR_RDGAM);
925 if (pcntptr) *pcntptr = osrp2(buf);
926 }
927 else if (fioisrsc(buf, "TADSPP") && tctx != 0)
928 {
929 tok_read_defines(tctx, fp, ec);
930 }
931 else if (fioisrsc(buf, "XSI"))
932 {
933 if (osfrb(fp, buf, 2)) errsig(ec, ERR_RDGAM);
934 setupctx->fiolcxseed = xor_seed = buf[0];
935 setupctx->fiolcxinc = xor_inc = buf[1];
936 osfseek(fp, endpos + startofs, OSFSK_SET);
937 }
938 else if (fioisrsc(buf, "CHRSET"))
939 {
940 size_t len;
941
942 /* read the character set ID and LDESC */
943 if (osfrb(fp, buf, 6)
944 || (len = osrp2(buf+4)) > CMAP_LDESC_MAX_LEN
945 || osfrb(fp, buf+6, len))
946 errsig(ec, ERR_RDGAM);
947
948 /* establish this character set mapping */
949 buf[4] = '\0';
950 cmap_set_game_charset(ec, (char *)buf, (char *)buf + 6, argv0);
951 }
952 else if (fioisrsc(buf, "$EOF"))
953 {
954 if (eof_reset)
955 {
956 osfseek(fp, eof_reset, OSFSK_SET); /* back after EXTCNT */
957 eof_reset = 0; /* really done at next EOF */
958 #if 0 // XFCNs are obsolete
959 xfcns_done = TRUE; /* don't do XFCNs again */
960 #endif
961 }
962 else
963 break;
964 }
965 else
966 errsig(ec, ERR_UNKRSC);
967 }
968 }
969
970 /* read binary file */
971 void fiord(mcmcxdef *mctx, voccxdef *vctx, tokcxdef *tctx, char *fname,
972 char *exename, fiolcxdef *setupctx, objnum *preinit, uint *flagp,
973 tokpdef *path, uchar **fmtsp, uint *fmtlp, uint *pcntptr,
974 int flags, struct appctxdef *appctx, char *argv0)
975 {
976 osfildef *fp;
977 ulong startofs;
978 char *display_fname;
979
980 /* presume there will be no need to run preinit */
981 *preinit = MCMONINV;
982
983 /*
984 * get the display filename - use the real filename if one is
985 * provided, otherwise use the name of the executable file itself
986 */
987 display_fname = (fname != 0 ? fname : exename);
988
989 /* save the filename in G_os_gamename */
990 if (display_fname != 0)
991 {
992 size_t copylen;
993
994 /* limit the copy to the buffer size */
995 if ((copylen = strlen(display_fname)) > sizeof(G_os_gamename) - 1)
996 copylen = sizeof(G_os_gamename) - 1;
997
998 /* save it */
999 memcpy(G_os_gamename, display_fname, copylen);
1000 G_os_gamename[copylen] = '\0';
1001 }
1002 else
1003 G_os_gamename[0] = '\0';
1004
1005 /* open the file and read and check file header */
1006 fp = (fname != 0 ? osfoprb(fname, OSFTGAME)
1007 : os_exeseek(exename, "TGAM"));
1008 if (fp == 0)
1009 errsig(vctx->voccxerr, ERR_OPRGAM);
1010
1011 /*
1012 * we've identified the .GAM file source - tell the host system
1013 * about it, if it's interested
1014 */
1015 if (appctx != 0 && appctx->set_game_name != 0)
1016 (*appctx->set_game_name)(appctx->set_game_name_ctx, display_fname);
1017
1018 /* remember starting location in file */
1019 startofs = osfpos(fp);
1020
1021 ERRBEGIN(vctx->voccxerr)
1022
1023 /*
1024 * Read the game file. Note that the .GAM file always has resource
1025 * file number zero.
1026 */
1027 fiord1(mctx, vctx, tctx, fp, display_fname,
1028 setupctx, startofs, preinit, flagp, path,
1029 fmtsp, fmtlp, pcntptr, flags, appctx, argv0);
1030
1031 /*
1032 * If the host system accepts additional resource files, look for
1033 * additional resource files. These are files in the same directory
1034 * as the .GAM file, with the .GAM suffix replaced by suffixes from
1035 *. RS0 to .RS9.
1036 */
1037 if (appctx != 0 && appctx->add_resfile != 0)
1038 {
1039 char suffix_lc[4];
1040 char suffix_uc[4];
1041 int i;
1042 char *base_name;
1043
1044 /* use the game or executable filename, as appropriate */
1045 base_name = display_fname;
1046
1047 /* build the initial suffixes - try both upper- and lower-case */
1048 suffix_uc[0] = 'R';
1049 suffix_uc[1] = 'S';
1050 suffix_uc[3] = '\0';
1051 suffix_lc[0] = 'r';
1052 suffix_lc[1] = 's';
1053 suffix_lc[3] = '\0';
1054
1055 /* loop through each possible suffix (.RS0 through .RS9) */
1056 for (i = 0 ; i < 9 ; ++i)
1057 {
1058 char resname[OSFNMAX];
1059 osfildef *fpres;
1060 int resfileno;
1061
1062 /*
1063 * Build the next resource filename. If there's an explicit
1064 * resource path, use it, otherwise use the same directory
1065 * that contains the .GAM file.
1066 */
1067 if (appctx->ext_res_path != 0)
1068 {
1069 /*
1070 * There's an explicit resource path - append the root
1071 * (filename-only, minus path) portion of the .GAM file
1072 * name to the resource path.
1073 */
1074 os_build_full_path(resname, sizeof(resname),
1075 appctx->ext_res_path,
1076 os_get_root_name(base_name));
1077 }
1078 else
1079 {
1080 /*
1081 * there's no resource path - use the entire .GAM
1082 * filename, including directory, so that we look in the
1083 * same directory that contains the .GAM file
1084 */
1085 if (base_name != 0)
1086 strcpy(resname, base_name);
1087 else
1088 resname[0] = '\0';
1089 }
1090
1091 /* add the current extension (replacing any current extension) */
1092 os_remext(resname);
1093 suffix_lc[2] = suffix_uc[2] = '0' + i;
1094 os_addext(resname, suffix_lc);
1095
1096 /* try opening the file */
1097 fpres = osfoprb(resname, OSFTGAME);
1098
1099 /* if that didn't work, try the upper-case name */
1100 if (fpres == 0)
1101 {
1102 /* replace the suffix with the upper-case version */
1103 os_remext(resname);
1104 os_addext(resname, suffix_uc);
1105
1106 /* try again with the new name */
1107 fpres = osfoprb(resname, OSFTGAME);
1108 }
1109
1110 /* if we opened it successfully, read it */
1111 if (fpres != 0)
1112 {
1113 /* tell the host system about it */
1114 resfileno = (*appctx->add_resfile)
1115 (appctx->add_resfile_ctx, resname);
1116
1117 /* read the file */
1118 fiordrscext(vctx->voccxerr, fpres, appctx,
1119 resfileno, resname);
1120
1121 /* we're done with the file, so close it */
1122 osfcls(fpres);
1123 }
1124 }
1125 }
1126
1127 ERRCLEAN(vctx->voccxerr)
1128 /* if an error occurs during read, clean up by closing the file */
1129 osfcls(fp);
1130 ERRENDCLN(vctx->voccxerr);
1131 }
1132
1133 /* save game header */
1134 #define FIOSAVHDR "TADS2 save\012\015\032"
1135
1136 /* save game header prefix - .GAM file information */
1137 #define FIOSAVHDR_PREFIX "TADS2 save/g\012\015\032"
1138
1139 /*
1140 * Saved game format version string - note that the length of the
1141 * version string must be fixed, so when this is updated, it must be
1142 * updated to another string of the same length. This should be updated
1143 * whenever a change is made to the format that can't be otherwise
1144 * detected from the data stream in the saved game file.
1145 */
1146 #define FIOSAVVSN "v2.2.1"
1147
1148 /* old saved game format version strings */
1149 #define FIOSAVVSN1 "v2.2.0"
1150
1151 /* read fuse/daemon/alarm record */
1152 static int fiorfda(osfildef *fp, vocddef *p, uint cnt)
1153 {
1154 vocddef *q;
1155 uint i;
1156 uchar buf[14];
1157
1158 /* start by clearing out entire record */
1159 for (i = 0, q = p ; i < cnt ; ++q, ++i)
1160 q->vocdfn = MCMONINV;
1161
1162 /* now restore all the records from the file */
1163 for (;;)
1164 {
1165 /* read a record, and quit if it's the last one */
1166 if (osfrb(fp, buf, 13)) return(TRUE);
1167 if ((i = osrp2(buf)) == 0xffff) return(FALSE);
1168
1169 /* restore this record */
1170 q = p + i;
1171 q->vocdfn = osrp2(buf+2);
1172 q->vocdarg.runstyp = buf[4];
1173 switch(buf[4])
1174 {
1175 case DAT_NUMBER:
1176 q->vocdarg.runsv.runsvnum = osrp4s(buf+5);
1177 break;
1178 case DAT_OBJECT:
1179 case DAT_FNADDR:
1180 q->vocdarg.runsv.runsvobj = osrp2(buf+5);
1181 break;
1182 case DAT_PROPNUM:
1183 q->vocdarg.runsv.runsvprp = osrp2(buf+5);
1184 break;
1185 }
1186 q->vocdprp = osrp2(buf+9);
1187 q->vocdtim = osrp2(buf+11);
1188 }
1189 }
1190
1191 /*
1192 * Look in a saved game file to determine if it has information on which
1193 * GAM file created it. If the GAM file information is available, this
1194 * routine returns true and stores the game file name in the given
1195 * buffer; if the information isn't available, we'll return false.
1196 */
1197 int fiorso_getgame(char *saved_file, char *fnamebuf, size_t buflen)
1198 {
1199 osfildef *fp;
1200 uint namelen;
1201 char buf[sizeof(FIOSAVHDR_PREFIX) + 2];
1202
1203 /* open the input file */
1204 if (!(fp = osfoprb(saved_file, OSFTSAVE)))
1205 return FALSE;
1206
1207 /* read the prefix header and check */
1208 if (osfrb(fp, buf, (int)(sizeof(FIOSAVHDR_PREFIX) + 2))
1209 || memcmp(buf, FIOSAVHDR_PREFIX, sizeof(FIOSAVHDR_PREFIX)) != 0)
1210 {
1211 /*
1212 * there's no game file information - close the file and
1213 * indicate that we have no information
1214 */
1215 osfcls(fp);
1216 return FALSE;
1217 }
1218
1219 /* get the length of the filename */
1220 namelen = osrp2(buf + sizeof(FIOSAVHDR_PREFIX));
1221 if (namelen > buflen - 1)
1222 namelen = buflen - 1;
1223
1224 /* read the filename */
1225 if (osfrb(fp, fnamebuf, namelen))
1226 {
1227 osfcls(fp);
1228 return FALSE;
1229 }
1230
1231 /* null-terminate the string */
1232 fnamebuf[namelen] = '\0';
1233
1234 /* done with the file */
1235 osfcls(fp);
1236
1237 /* indicate that we found the information */
1238 return TRUE;
1239 }
1240
1241 /* restore game: returns TRUE on failure */
1242 int fiorso(voccxdef *vctx, char *fname)
1243 {
1244 osfildef *fp;
1245 objnum obj;
1246 uchar *p;
1247 uchar *mut;
1248 uint mutsiz;
1249 uint oldmutsiz;
1250 int propcnt;
1251 mcmcxdef *mctx = vctx->voccxmem;
1252 uchar buf[sizeof(FIOSAVHDR) + sizeof(FIOSAVVSN)];
1253 ushort newsiz;
1254 int err = FALSE;
1255 char timestamp[26];
1256 int version = 0; /* version ID - 0 = current version */
1257 int result;
1258
1259 /* presume success */
1260 result = FIORSO_SUCCESS;
1261
1262 /* open the input file */
1263 if (!(fp = osfoprb(fname, OSFTSAVE)))
1264 return FIORSO_FILE_NOT_FOUND;
1265
1266 /* check for a prefix header - if it's there, skip it */
1267 if (!osfrb(fp, buf, (int)(sizeof(FIOSAVHDR_PREFIX) + 2))
1268 && memcmp(buf, FIOSAVHDR_PREFIX, sizeof(FIOSAVHDR_PREFIX)) == 0)
1269 {
1270 ulong skip_len;
1271
1272 /*
1273 * The prefix header is present - skip it. The 2-byte value
1274 * following the header is the length of the prefix data block
1275 * (not including the header), so simply skip the additional
1276 * number of bytes specified.
1277 */
1278 skip_len = (ulong)osrp2(buf + sizeof(FIOSAVHDR_PREFIX));
1279 osfseek(fp, skip_len, OSFSK_CUR);
1280 }
1281 else
1282 {
1283 /*
1284 * there's no prefix header - seek back to the start of the file
1285 * and read the standard header information
1286 */
1287 osfseek(fp, 0, OSFSK_SET);
1288 }
1289
1290
1291 /* read headers and check */
1292 if (osfrb(fp, buf, (int)(sizeof(FIOSAVHDR) + sizeof(FIOSAVVSN)))
1293 || memcmp(buf, FIOSAVHDR, (size_t)sizeof(FIOSAVHDR)))
1294 {
1295 /* it's not a saved game file */
1296 result = FIORSO_NOT_SAVE_FILE;
1297 goto ret_error;
1298 }
1299
1300 /* check the version string */
1301 if (memcmp(buf + sizeof(FIOSAVHDR), FIOSAVVSN,
1302 (size_t)sizeof(FIOSAVVSN)) == 0)
1303 {
1304 /* it's the current version */
1305 version = 0;
1306 }
1307 else if (memcmp(buf + sizeof(FIOSAVHDR), FIOSAVVSN1,
1308 (size_t)sizeof(FIOSAVVSN1)) == 0)
1309 {
1310 /* it's old version #1 */
1311 version = 1;
1312 }
1313 else
1314 {
1315 /*
1316 * this isn't a recognized version - the file must have been
1317 * saved by a newer version of the system, so we can't assume we
1318 * will be able to parse the format
1319 */
1320 result = FIORSO_BAD_FMT_VSN;
1321 goto ret_error;
1322 }
1323
1324 /*
1325 * Read timestamp and check - the game must have been saved by the
1326 * same .GAM file that we are now running, because the .SAV file is
1327 * written entirely in terms of the contents of the .GAM file; any
1328 * change in the .GAM file invalidates the .SAV file.
1329 */
1330 if (osfrb(fp, timestamp, 26)
1331 || memcmp(timestamp, vctx->voccxtim, (size_t)26))
1332 {
1333 result = FIORSO_BAD_GAME_VSN;
1334 goto ret_error;
1335 }
1336
1337 /* first revert every object to original (post-compilation) state */
1338 vocrevert(vctx);
1339
1340 /*
1341 * the most common error from here on is simply a file read error,
1342 * so presume that this is what will happen; if we are successful or
1343 * encounter a different error, we'll change the status at that
1344 * point
1345 */
1346 result = FIORSO_READ_ERROR;
1347
1348 /* go through file and load changed objects */
1349 for (;;)
1350 {
1351 /* get the header */
1352 if (osfrb(fp, buf, 7))
1353 goto ret_error;
1354
1355 /* get the object number from the header, and stop if we're done */
1356 obj = osrp2(buf+1);
1357 if (obj == MCMONINV)
1358 break;
1359
1360 /* if the object was dynamically allocated, recreate it */
1361 if (buf[0] == 1)
1362 {
1363 int sccnt;
1364 objnum sc;
1365
1366 /* create the object */
1367 mutsiz = osrp2(buf + 3);
1368 p = mcmalonum(mctx, (ushort)mutsiz, (mcmon)obj);
1369
1370 /* read the object's contents */
1371 if (osfrb(fp, p, mutsiz))
1372 goto ret_error;
1373
1374 /* get the superclass data (at most one superclass) */
1375 sccnt = objnsc(p);
1376 if (sccnt) sc = osrp2(objsc(p));
1377
1378 /* create inheritance records for the object */
1379 vociadd(vctx, obj, MCMONINV, sccnt, &sc, VOCIFNEW | VOCIFVOC);
1380
1381 #if 0
1382 {
1383 int wrdcnt;
1384
1385 /* read the object's vocabulary and add it back */
1386 if (osfrb(fp, buf, 2))
1387 goto ret_error;
1388 wrdcnt = osrp2(buf);
1389 while (wrdcnt--)
1390 {
1391 int len1;
1392 int len2;
1393 char wrd[80];
1394
1395 /* read the header */
1396 if (osfrb(fp, buf, 6))
1397 goto ret_error;
1398 len1 = osrp2(buf+2);
1399 len2 = osrp2(buf+4);
1400
1401 /* read the word text */
1402 if (osfrb(fp, wrd, len1 + len2))
1403 goto ret_error;
1404
1405 /* add the word */
1406 vocadd2(vctx, buf[0], obj, buf[1], wrd, len1,
1407 wrd + len1, len2);
1408 }
1409 }
1410 #endif
1411
1412 }
1413 else
1414 {
1415 /* get the remaining data from the header */
1416 propcnt = osrp2(buf + 3);
1417 mutsiz = osrp2(buf + 5);
1418
1419 /* expand object if it's not big enough for mutsiz */
1420 p = mcmlck(mctx, (mcmon)obj);
1421 oldmutsiz = mcmobjsiz(mctx, (mcmon)obj) - objrst(p);
1422 if (oldmutsiz < mutsiz)
1423 {
1424 newsiz = mutsiz - oldmutsiz;
1425 p = (uchar *)objexp(mctx, obj, &newsiz);
1426 }
1427
1428 /* reset statistics, and read mutable part from file */
1429 mut = p + objrst(p);
1430 objsnp(p, propcnt);
1431 objsfree(p, mutsiz + objrst(p));
1432 if (osfrb(fp, mut, mutsiz))
1433 err = TRUE;
1434
1435 /* reset ignore flags as needed */
1436 objsetign(mctx, obj);
1437 }
1438
1439 /* touch and unlock the object */
1440 mcmtch(mctx, (mcmon)obj);
1441 mcmunlck(mctx, (mcmon)obj);
1442 if (err)
1443 goto ret_error;
1444 }
1445
1446 /* read fuses/daemons/alarms */
1447 if (fiorfda(fp, vctx->voccxdmn, vctx->voccxdmc)
1448 || fiorfda(fp, vctx->voccxfus, vctx->voccxfuc)
1449 || fiorfda(fp, vctx->voccxalm, vctx->voccxalc))
1450 goto ret_error;
1451
1452 /* read the dynamically added and deleted vocabulary */
1453 for (;;)
1454 {
1455 int len1;
1456 int len2;
1457 char wrd[80];
1458 int flags;
1459 int typ;
1460
1461 /* read the header */
1462 if (osfrb(fp, buf, 8))
1463 goto ret_error;
1464
1465 typ = buf[0];
1466 flags = buf[1];
1467 len1 = osrp2(buf+2);
1468 len2 = osrp2(buf+4);
1469 obj = osrp2(buf+6);
1470
1471 /* check to see if this is the end marker */
1472 if (obj == MCMONINV) break;
1473
1474 /* read the word text */
1475 if (osfrb(fp, wrd+2, len1))
1476 goto ret_error;
1477 if (len2)
1478 {
1479 wrd[len1 + 2] = ' ';
1480 if (osfrb(fp, &wrd[len1 + 3], len2))
1481 goto ret_error;
1482 oswp2(wrd, len1 + len2 + 3);
1483 }
1484 else
1485 oswp2(wrd, len1 + 2);
1486
1487 /* add or delete the word as appropriate */
1488 if (flags & VOCFDEL)
1489 vocdel1(vctx, obj, (char *)wrd, (prpnum)typ, FALSE, FALSE, FALSE);
1490 else
1491 vocadd2(vctx, buf[0], obj, buf[1], (uchar *)wrd+2, len1,
1492 (uchar *)wrd+len1, len2);
1493 }
1494
1495 /*
1496 * the following was added in save format version "v2.2.1", so skip
1497 * it if the save version is older than that
1498 */
1499 if (version != 1)
1500 {
1501 /* read the current "Me" object */
1502 if (osfrb(fp, buf, 2))
1503 goto ret_error;
1504 vctx->voccxme = osrp2(buf);
1505 }
1506
1507 /* done - close file and return success indication */
1508 osfcls(fp);
1509 return FIORSO_SUCCESS;
1510
1511 /* come here on failure - close file and return error indication */
1512 ret_error:
1513 osfcls(fp);
1514 return result;
1515 }
1516
1517 /* write fuse/daemon/alarm block */
1518 static int fiowfda(osfildef *fp, vocddef *p, uint cnt)
1519 {
1520 uchar buf[14];
1521 uint i;
1522
1523 for (i = 0 ; i < cnt ; ++i, ++p)
1524 {
1525 if (p->vocdfn == MCMONINV) continue; /* not set - ignore */
1526
1527 oswp2(buf, i); /* element in array to be set */
1528 oswp2(buf+2, p->vocdfn); /* object number for function/target */
1529 buf[4] = p->vocdarg.runstyp; /* type of argument */
1530 switch(buf[4])
1531 {
1532 case DAT_NUMBER:
1533 oswp4s(buf+5, p->vocdarg.runsv.runsvnum);
1534 break;
1535 case DAT_OBJECT:
1536 case DAT_FNADDR:
1537 oswp2(buf+5, p->vocdarg.runsv.runsvobj);
1538 break;
1539 case DAT_PROPNUM:
1540 oswp2(buf+5, p->vocdarg.runsv.runsvprp);
1541 break;
1542 }
1543 oswp2(buf+9, p->vocdprp);
1544 oswp2(buf+11, p->vocdtim);
1545
1546 /* write this record to file */
1547 if (osfwb(fp, buf, 13)) return(TRUE);
1548 }
1549
1550 /* write end record - -1 for array element number */
1551 oswp2(buf, 0xffff);
1552 return(osfwb(fp, buf, 13));
1553 }
1554
1555 /* context for vocabulary saver callback function */
1556 struct fiosav_cb_ctx
1557 {
1558 int err;
1559 osfildef *fp;
1560 };
1561
1562 #ifdef NEVER
1563 /*
1564 * callback for vocabulary saver - called by voc_iterate for each word
1565 * defined for a particular object, allowing us to write all the words
1566 * attached to a dynamically allocated object to the save file
1567 */
1568 static void fiosav_cb(struct fiosav_cb_ctx *ctx,
1569 vocdef *voc, vocwdef *vocw)
1570 {
1571 char buf[10];
1572
1573 /* write the part of speech, flags, and word lengths */
1574 buf[0] = vocw->vocwtyp;
1575 buf[1] = vocw->vocwflg;
1576 oswp2(buf+2, voc->voclen);
1577 oswp2(buf+4, voc->vocln2);
1578 if (osfwb(ctx->fp, buf, 6)) ctx->err = TRUE;
1579
1580 /* write the words */
1581 if (osfwb(ctx->fp, voc->voctxt, voc->voclen + voc->vocln2))
1582 ctx->err = TRUE;
1583 }
1584 #endif
1585
1586 /*
1587 * Callback for vocabulary saver - called by voc_iterate for every
1588 * word. We'll write the word if it was dynamically added or deleted,
1589 * so that we can restore that status when the game is restored.
1590 */
1591 static void fiosav_voc_cb(void *ctx0, vocdef *voc, vocwdef *vocw)
1592 {
1593 struct fiosav_cb_ctx *ctx = (struct fiosav_cb_ctx *)ctx0;
1594 char buf[10];
1595
1596 /* if the word was dynamically allocated or deleted, save it */
1597 if ((vocw->vocwflg & VOCFNEW) || (vocw->vocwflg & VOCFDEL))
1598 {
1599 /* write the header information */
1600 buf[0] = vocw->vocwtyp;
1601 buf[1] = vocw->vocwflg;
1602 oswp2(buf+2, voc->voclen);
1603 oswp2(buf+4, voc->vocln2);
1604 oswp2(buf+6, vocw->vocwobj);
1605 if (osfwb(ctx->fp, buf, 8)) ctx->err = TRUE;
1606
1607 /* write the words */
1608 if (osfwb(ctx->fp, voc->voctxt, voc->voclen + voc->vocln2))
1609 ctx->err = TRUE;
1610 }
1611 }
1612
1613
1614 /* save game; returns TRUE on failure */
1615 int fiosav(voccxdef *vctx, char *fname, char *game_fname)
1616 {
1617 osfildef *fp;
1618 vocidef ***vpg;
1619 vocidef **v;
1620 int i;
1621 int j;
1622 objnum obj;
1623 uchar *p;
1624 uchar *mut;
1625 uint mutsiz;
1626 int propcnt;
1627 mcmcxdef *mctx = vctx->voccxmem;
1628 uchar buf[8];
1629 int err = FALSE;
1630 struct fiosav_cb_ctx fnctx;
1631
1632 /* open the output file */
1633 if ((fp = osfopwb(fname, OSFTSAVE)) == 0)
1634 return TRUE;
1635
1636 /*
1637 * If we have game file information, save the game file information
1638 * with the saved game file. This lets the player start the
1639 * run-time and restore the game by specifying only the saved game
1640 * file.
1641 */
1642 if (game_fname != 0)
1643 {
1644 size_t len;
1645
1646 /* write the prefix header */
1647 len = strlen(game_fname);
1648 oswp2(buf, len);
1649 if (osfwb(fp, FIOSAVHDR_PREFIX, (int)sizeof(FIOSAVHDR_PREFIX))
1650 || osfwb(fp, buf, 2)
1651 || osfwb(fp, game_fname, (int)len))
1652 goto ret_error;
1653 }
1654
1655 /* write save game header and timestamp */
1656 if (osfwb(fp, FIOSAVHDR, (int)sizeof(FIOSAVHDR))
1657 || osfwb(fp, FIOSAVVSN, (int)sizeof(FIOSAVVSN))
1658 || osfwb(fp, vctx->voccxtim, 26))
1659 goto ret_error;
1660
1661 /* go through each object, and write if it's been changed */
1662 for (vpg = vctx->voccxinh, i = 0 ; i < VOCINHMAX ; ++vpg, ++i)
1663 {
1664 if (!*vpg) continue;
1665 for (v = *vpg, obj = (i << 8), j = 0 ; j < 256 ; ++v, ++obj, ++j)
1666 {
1667 if (*v != 0)
1668 {
1669 /* write object if it's dirty */
1670 if (mcmobjdirty(mctx, (mcmon)obj))
1671 {
1672 p = mcmlck(mctx, (mcmon)obj);
1673 mut = p + objrst(p);
1674 propcnt = objnprop(p);
1675 mutsiz = objfree(p) - objrst(p);
1676 if ((objflg(p) & OBJFINDEX) != 0)
1677 mutsiz += propcnt * 4;
1678
1679 /*
1680 * If the object was dynamically allocated, write
1681 * the whole object. Otherwise, write just the
1682 * mutable part.
1683 */
1684 if ((*v)->vociflg & VOCIFNEW)
1685 {
1686 /* indicate that the object is dynamic */
1687 buf[0] = 1;
1688 oswp2(buf + 1, obj);
1689
1690 /* write the entire object */
1691 mutsiz = objfree(p);
1692 oswp2(buf + 3, mutsiz);
1693 if (osfwb(fp, buf, 7)
1694 || osfwb(fp, p, mutsiz))
1695 err = TRUE;
1696
1697 #ifdef NEVER
1698 {
1699 int wrdcnt;
1700
1701 /* count the words, and write the count */
1702 voc_count(vctx, obj, 0, &wrdcnt, (int *)0);
1703 oswp2(buf, wrdcnt);
1704 if (osfwb(fp, buf, 2))
1705 err = TRUE;
1706
1707 /* write the words */
1708 fnctx.err = 0;
1709 fnctx.fp = fp;
1710 voc_iterate(vctx, obj, fiosav_cb, &fnctx);
1711 if (fnctx.err != 0)
1712 err = TRUE;
1713 }
1714 #endif
1715 }
1716 else if (mutsiz)
1717 {
1718 /* write number of properties, size of mut, and mut */
1719 buf[0] = 0; /* indicate that the object is static */
1720 oswp2(buf + 1, obj);
1721 oswp2(buf + 3, propcnt);
1722 oswp2(buf + 5, mutsiz);
1723 if (osfwb(fp, buf, 7)
1724 || osfwb(fp, mut, mutsiz))
1725 err = TRUE;
1726 }
1727
1728 mcmunlck(mctx, (mcmon)obj);
1729 if (err != 0)
1730 goto ret_error;
1731 }
1732 }
1733 }
1734 }
1735
1736 /* write end-of-objects indication */
1737 buf[0] = 0;
1738 oswp2(buf + 1, MCMONINV);
1739 oswp4(buf + 3, 0);
1740 if (osfwb(fp, buf, 7))
1741 goto ret_error;
1742
1743 /* write fuses/daemons/alarms */
1744 if (fiowfda(fp, vctx->voccxdmn, vctx->voccxdmc)
1745 || fiowfda(fp, vctx->voccxfus, vctx->voccxfuc)
1746 || fiowfda(fp, vctx->voccxalm, vctx->voccxalc))
1747 goto ret_error;
1748
1749 /* write run-time vocabulary additions and deletions */
1750 fnctx.fp = fp;
1751 fnctx.err = 0;
1752 voc_iterate(vctx, MCMONINV, fiosav_voc_cb, &fnctx);
1753 if (fnctx.err)
1754 goto ret_error;
1755
1756 /* write end marker for vocabulary additions and deletions */
1757 oswp2(buf+6, MCMONINV);
1758 if (osfwb(fp, buf, 8))
1759 goto ret_error;
1760
1761 /* write the current "Me" object */
1762 oswp2(buf, vctx->voccxme);
1763 if (osfwb(fp, buf, 2))
1764 goto ret_error;
1765
1766 /* done - close file and return success indication */
1767 osfcls(fp);
1768 os_settype(fname, OSFTSAVE);
1769 return FALSE;
1770
1771 /* come here on failure - close file and return error indication */
1772 ret_error:
1773 osfcls(fp);
1774 return TRUE;
1775 }