-
Notifications
You must be signed in to change notification settings - Fork 1
/
id.c
381 lines (311 loc) · 7.26 KB
/
id.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
/* Sarien - A Sierra AGI resource interpreter engine
* Copyright (C) 1999-2003 Stuart George and Claudio Matsuoka
*
* $Id: id.c,v 1.48 2003/07/13 14:27:33 cmatsuoka Exp $
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; see docs/COPYING for further details.
*/
#ifndef PALMOS
#include <stdio.h>
#include <string.h>
#include "sarien.h"
#include "agi.h"
#include "opcodes.h"
/*
* Determine what AGI v2 system to emulate, these are the major version
* to emulate, thus 2.915 comes under 2.917, 2.4xxx is 2.440, etc.
*
* 0x2089
* 0x2272
* 0x2440
* 0x2917
* 0x2936
*/
int setup_v2_game(int ver, UINT32 crc);
int setup_v3_game(int ver, UINT32 crc);
int v4id_game (UINT32 crc);
#ifdef USE_COMMAND_LINE
void list_games ()
{
FILE *f;
char *c, *t, buf[256];
UINT32 id, ver;
int min, maj, i = 0;
if ((f = fopen (get_config_file(), "r")) == NULL) {
printf ("Configuration file not found.\n");
return;
}
printf (
"Game# AGI ver. Title CRC\n"
"------ ---------- ---------------------------------------- -------\n"
);
while (!feof (f)) {
fgets (buf, 256, f);
c = strchr (buf, '#');
if (c) *c = 0;
/* Remove spaces at end of line */
if (strlen (buf)) {
for (c = buf + strlen (buf) - 1;
*c == ' ' || *c == '\t'; *c-- = 0) {}
}
t = strtok (buf, " \t\r\n");
if (t == NULL)
continue;
id = strtoul (t, NULL, 0);
t = strtok (NULL, " \t\r\n");
if (t == NULL)
continue;
ver = strtoul (t, NULL, 0);
maj = (ver >> 12) & 0xf;
min = ver & 0xfff;
t = strtok (NULL, "\n\r");
if (maj == 2) {
printf ("[%3d] %x.%03x %-40.40s 0x%05x\n",
++i, maj, min, t, id);
} else {
printf ("[%3d] %x.002.%03x %-40.40s 0x%05x\n",
++i, maj, min, t, id);
}
}
fclose (f);
}
#endif
UINT32 match_crc (UINT32 crc, char *path, char *name, int len)
{
FILE *f;
char *c, *t, buf[256];
UINT32 id, ver;
if ((f = fopen (path, "r")) == NULL)
return 0;
while (!feof (f)) {
if ( !fgets (buf, 256, f) )
return 0;
if (strstr(buf, "# End of sarien.conf") != NULL)
break;
c = strchr (buf, '#');
if (c) *c = 0;
/* Remove spaces at end of line */
if (strlen (buf)) {
for (c = buf + strlen (buf) - 1;
*c == ' ' || *c == '\t'; *c-- = 0) {}
}
t = strtok (buf, " \t\r\n");
if (t == NULL)
continue;
id = strtoul (t, NULL, 0);
t = strtok (NULL, " \t\r\n");
if (t == NULL)
continue;
ver = strtoul (t, NULL, 0);
t = strtok (NULL, "\n\r");
for (; *t == ' ' || *t == '\t'; t++);
if (id == crc) {
/* Now we must check options enclosed in brackets
* like [A] for Amiga
*/
if (*t == '[') {
while (*t != ']') {
switch (*t++) {
case 'A':
opt.amiga = TRUE;
break;
case 'a':
opt.agds = TRUE;
break;
#ifdef USE_MOUSE
case 'm':
opt.agimouse = TRUE;
break;
#endif
}
}
t++;
for (; *t == ' ' || *t == '\t'; t++) {}
}
strncpy (name, t, len);
fclose (f);
#ifdef DREAMCAST
strcpy(g_gamename, t);
#endif
return ver;
}
}
fclose (f);
return 0;
}
static UINT32 match_version (UINT32 crc)
{
int ver;
char name[80];
if ((ver = match_crc(crc, get_config_file(), name, 80)) > 0)
report ("AGI game detected: %s\n\n", name);
return ver;
}
int v2id_game ()
{
#ifdef __MPW__
return err_OK; /* FIXME! */
#else
int y, ver;
UINT32 len, c, crc;
UINT8 *buff;
FILE *fp;
char *fn[] = { "viewdir", "logdir", "picdir", "snddir",
"words.tok", "object", "" };
buff = malloc (8192);
for (crc = y = 0; fn[y][0]; y++) {
char *path = fixpath (NO_GAMEDIR, fn[y]);
if ((fp = fopen (path, "rb")) != NULL) {
for (len = 1; len > 0; ) {
memset (buff, 0, 8192);
len = fread (buff, 1, 8000, fp);
for (c = 0; c < len; c++)
crc += *(buff + c);
}
fclose (fp);
}
}
free (buff);
report ("Computed CRC: 0x%05x\n", crc);
ver = match_version (crc);
game.crc = crc;
game.ver = ver;
_D (_D_WARN "game.ver = 0x%x", game.ver);
agi_set_release (ver);
return setup_v2_game(ver, crc);
#endif
}
/*
* Currently, there is no known difference between v3.002.098 -> v3.002.149
* So version emulated;
*
* 0x0086,
* 0x0149
*/
int v3id_game ()
{
int ec = err_OK, y, ver;
UINT32 len, c, crc;
UINT8 *buff;
FILE *fp;
char *path, *fn[] = { "words.tok", "object", "" };
buff = malloc (8192);
for (crc = 0, y = 0; fn[y][0] != 0x0; y++) {
path = fixpath (NO_GAMEDIR, fn[y]);
if ((fp = fopen (path, "rb")) != NULL) {
len = 1;
while (len > 0) {
memset (buff, 0, 8192);
len = fread (buff, 1, 8000, fp);
for (c = 0; c < len; c++)
crc += *(buff + c);
}
fclose(fp);
}
}
/* now do the directory file */
path = fixpath (GAMEDIR, DIR_);
if ((fp = fopen(path, "rb")) != NULL) {
for (len = 1; len > 0; ) {
memset (buff, 0, 8192);
len = fread (buff, 1, 8000, fp);
for (c = 0; c < len; c++)
crc += *(buff + c);
}
fclose (fp);
}
free (buff);
report ("Computed CRC: 0x%05x\n", crc);
ver = match_version (crc);
game.crc = crc;
game.ver = ver;
agi_set_release (ver);
ec = setup_v3_game(ver, crc);
return ec;
}
#ifdef PALMOS
int v4id_game (UINT32 ver)
{
int ec = err_OK;
game.crc = 0;
game.ver = ver;
agi_set_release (ver);
switch ((ver>>12)&0xFF) {
case 2:
ec = setup_v2_game (ver, 0);
break;
case 3:
ec = setup_v3_game (ver, 0);
break;
}
return ec;
}
#endif
/**
*
*/
int setup_v2_game (int ver, UINT32 crc)
{
int ec=err_OK;
if (ver == 0) {
report ("Unknown v2 Sierra game: %08x\n\n", crc);
agi_set_release (0x2917);
}
/* setup the differences in the opcodes and other bits in the
* AGI v2 specs
*/
if (opt.emuversion)
agi_set_release (opt.emuversion);
if (opt.agds)
agi_set_release (0x2440);/* ALL AGDS games built for 2.440 */
switch(agi_get_release ()) {
case 0x2089:
logic_names_cmd[0x86].num_args = 0; /* quit: 0 args */
logic_names_cmd[0x97].num_args = 3; /* print.at: 3 args */
logic_names_cmd[0x98].num_args = 3; /* print.at.v: 3 args*/
break;
case 0x2272:
/* KQ3 0x88673 (2.272) requires print.at with 4 arguments */
break;
case 0x2440:
break;
case 0x2917:
break;
case 0x2936:
break;
default:
report ("** Cannot setup for unknown version\n");
ec = err_UnknownAGIVersion;
break;
}
return ec;
}
/**
*
*/
int setup_v3_game (int ver, UINT32 crc)
{
int ec = err_OK;
if (ver == 0) {
report ("Unknown v3 Sierra game: %08x\n\n", crc);
agi_set_release (ver = 0x3149);
}
if (opt.emuversion)
agi_set_release (ver = opt.emuversion);
switch(ver) {
case 0x3086:
logic_names_cmd[0xad].num_args = 1; /* 173 : 1 args */
break;
case 0x3149:
logic_names_cmd[0xad].num_args = 0; /* 173 : 0 args */
break;
default:
report ("Error: cannot setup for unknown version\n");
ec = err_UnknownAGIVersion;
break;
}
return ec;
}
#endif