-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathtwstoini.c
More file actions
538 lines (475 loc) · 13.5 KB
/
twstoini.c
File metadata and controls
538 lines (475 loc) · 13.5 KB
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
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <stdarg.h>
#include <string.h>
#include <limits.h>
#include <errno.h>
#define TRUE 1
#define FALSE 0
/* The default locations of everything.
*/
#ifdef _WIN32
#define DIRSEP '\\'
#define DEFAULT_DATA_DIR ".\\data"
#define DEFAULT_SETS_DIR ".\\sets"
#define DEFAULT_SAVE_DIR ".\\save"
#else
#define DIRSEP '/'
#define DEFAULT_DATA_DIR "/usr/local/share/tworld/data"
#define DEFAULT_SETS_DIR "/usr/local/share/tworld/sets"
#define DEFAULT_SAVE_DIR "~/.tworld"
#endif
/* The various file signatures.
*/
static unsigned long const tws_sig = 0x999B3335;
static unsigned long const dat_sig = 0x0002AAAC;
static unsigned long const dac_sig = 0x656C6966;
/*
* General-purpose functions
*/
#define memerrexit() (warn("out of memory"), exit(EXIT_FAILURE))
#define xalloc(p, n) (((p) = realloc((p), (n))) || (memerrexit(), NULL))
typedef struct fileinfo {
char const *name;
FILE *fp;
} fileinfo;
static int warn(char const *fmt, ...)
{
va_list args;
va_start(args, fmt);
vfprintf(stderr, fmt, args);
fputc('\n', stderr);
va_end(args);
return FALSE;
}
static int warnfile(fileinfo const *file, char const *alt)
{
if (alt) {
if (errno)
warn("%s: %s", file->name, strerror(errno));
else
warn("%s: %s", file->name, alt);
}
return FALSE;
}
static int fileopen(fileinfo *file, char const *name, char const *mode,
char const *msg)
{
file->name = name;
file->fp = fopen(name, mode);
if (!file->fp)
return warnfile(file, msg);
return TRUE;
}
static int fileskip(fileinfo *file, int offset, char const *msg)
{
return fseek(file->fp, offset, SEEK_CUR) ? warnfile(file, msg) : TRUE;
}
static int fileread(fileinfo *file, void *buf, unsigned long size,
char const *msg)
{
return fread(buf, size, 1, file->fp) == 1 ? TRUE : warnfile(file, msg);
}
static int filereadint8(fileinfo *file, unsigned int *val, char const *msg)
{
int byte;
if ((byte = fgetc(file->fp)) != EOF) {
*val = (unsigned char)byte;
return TRUE;
}
return warnfile(file, msg);
}
static int filereadint16(fileinfo *file, unsigned int *val, char const *msg)
{
int byte;
if ((byte = fgetc(file->fp)) != EOF) {
*val = byte & 0xFFU;
if ((byte = fgetc(file->fp)) != EOF) {
*val |= (byte & 0xFFU) << 8;
return TRUE;
}
}
return warnfile(file, msg);
}
static int filereadint32(fileinfo *file, unsigned long *val, char const *msg)
{
int byte;
if ((byte = fgetc(file->fp)) != EOF) {
*val = byte & 0xFFUL;
if ((byte = fgetc(file->fp)) != EOF) {
*val |= (byte & 0xFFUL) << 8;
if ((byte = fgetc(file->fp)) != EOF) {
*val |= (byte & 0xFFUL) << 16;
if ((byte = fgetc(file->fp)) != EOF) {
*val |= (byte & 0xFFUL) << 24;
return TRUE;
}
}
}
}
return warnfile(file, msg);
}
static void fileclose(fileinfo *file, char const *msg)
{
if (fclose(file->fp))
warnfile(file, msg);
file->fp = NULL;
}
/*
* Functions for reading solution file data
*/
/* A single solution.
*/
typedef struct solution {
unsigned int number; /* level number */
unsigned char passwd[16]; /* level password */
unsigned long time; /* solution time */
unsigned long score; /* score for the level */
} solution;
/* A set of solutions.
*/
typedef struct solutions {
int count; /* total number of solutions */
solution *solutions; /* all the solutions */
int currentlevel; /* most recently visited level */
char setname[256]; /* the name of the data file */
} solutions;
/* Parse the given file and return a filled-in solutions struct.
*/
static int readsolutions(char const *infilename, solutions *ss)
{
fileinfo file;
solution *s;
unsigned long dword;
unsigned int word, byte;
ss->count = 0;
ss->solutions = NULL;
ss->currentlevel = 0;
*ss->setname = '\0';
if (!fileopen(&file, infilename, "rb", "couldn't open"))
return FALSE;
/* The first four bytes of a .tws file contain the signature.
*/
if (!filereadint32(&file, &dword, "not a solution file"))
return FALSE;
if (dword != tws_sig)
return warnfile(&file, "not a .tws file");
/* The next byte contains the ruleset.
*/
if (!filereadint8(&file, &byte, "not a solution file"))
return FALSE;
if (byte != 2)
warnfile(&file, "does not use the MS ruleset");
/* The next two bytes contain the current level number.
*/
if (!filereadint16(&file, &word, "not a solution file"))
return FALSE;
ss->currentlevel = word;
/* The next byte contains the size of the remaining header.
*/
if (!filereadint8(&file, &byte, "not a solution file"))
return FALSE;
if (byte)
if (!fileskip(&file, byte, "not a solution file"))
return FALSE;
/* After that come the individual solutions. The first four bytes
* of every solution is a number (little-endian) that gives the
* size of the solution data, not including the four bytes just
* read. Note that it is permitted for the size to be zero, in
* which case no further data follows for that individual
* solution. Otherwise, the first two bytes of the solution data
* is the number (again, little-endian) of the level, and the next
* four bytes are the level password. If the size is greater than
* six, then the solution time is stored as an int32 starting at
* the sixteenth byte.
*/
for (;;) {
if (!filereadint32(&file, &dword, NULL))
break;
if (dword == 0)
continue;
if (dword < 6)
return warnfile(&file, "invalid solution data");
++ss->count;
xalloc(ss->solutions, ss->count * sizeof *ss->solutions);
s = ss->solutions + ss->count - 1;
if (!filereadint16(&file, &s->number, "invalid solution file"))
return FALSE;
if (!fileread(&file, s->passwd, 4, "invalid solution file"))
return FALSE;
dword -= 6;
s->passwd[4] = '\0';
s->time = 0;
s->score = 0;
if (dword >= 10) {
if (!fileskip(&file, 6, "invalid solution file"))
return FALSE;
if (!filereadint32(&file, &s->time, "invalid solution file"))
return FALSE;
dword -= 10;
if (!s->number && !*s->passwd) {
--ss->count;
if (dword > 0 && dword < 256)
if (!fileread(&file, ss->setname, dword,
"invalid solution file"))
return FALSE;
ss->setname[dword] = '\0';
dword = 0;
}
if (!fileskip(&file, dword, "invalid solution file"))
return FALSE;
}
}
fileclose(&file, NULL);
return TRUE;
}
/* Free all memory allocated by the solutions struct.
*/
static void freesolutions(solutions *ss)
{
free(ss->solutions);
ss->solutions = NULL;
ss->count = 0;
}
/*
* Functions for reading level set files
*/
/* A single level.
*/
typedef struct level {
unsigned int number; /* level number */
unsigned int time; /* level time */
} level;
/* A set of levels.
*/
typedef struct levels {
int count; /* total number of levels */
unsigned int lastnumber; /* number of the last level */
level *levels; /* all the levels */
} levels;
/* Parse the given file and return a filled-in levels struct.
*/
static int readlevels(char const *infilename, levels *ls)
{
fileinfo file;
level *l;
unsigned long dword;
unsigned int word;
ls->count = 0;
ls->lastnumber = 0;
ls->levels = NULL;
if (!fileopen(&file, infilename, "rb", "couldn't open"))
return FALSE;
/* The first four bytes of a level set file contain the signature.
*/
if (!filereadint32(&file, &dword, "not a level set file"))
return FALSE;
if ((dword & 0x00FFFFFF) != dat_sig)
return warnfile(&file, "not a level set file");
/* The next two bytes contain the number of the last level.
*/
if (!filereadint16(&file, &ls->lastnumber, "not a solution file"))
return FALSE;
/* After that come the individual levels. The first two bytes of
* every level is a number (little-endian) that gives the size of
* the level data, not including the four bytes just read. The
* next two bytes provide the level's number, and the next two
* bytes provide the level's time limit, in seconds (or zero if
* the level has no time limit).
*/
for (;;) {
if (!filereadint16(&file, &word, NULL))
break;
if (word < 4)
return warnfile(&file, "invalid level set file");
++ls->count;
xalloc(ls->levels, ls->count * sizeof *ls->levels);
l = ls->levels + ls->count - 1;
if (!filereadint16(&file, &l->number, "invalid level set file"))
return FALSE;
if (!filereadint16(&file, &l->time, "invalid level set file"))
return FALSE;
if (!fileskip(&file, word - 4, "invalid level set file"))
return FALSE;
}
fileclose(&file, NULL);
return TRUE;
}
/* Free all memory allocated by the levels struct.
*/
static void freelevels(levels *ss)
{
free(ss->levels);
ss->levels = NULL;
ss->count = 0;
}
/*
* Top-level functions
*/
static char datadir[4096] = DEFAULT_DATA_DIR;
static char setsdir[4096] = DEFAULT_SETS_DIR;
static char savedir[4096] = DEFAULT_SAVE_DIR;
static int findlevelsandsolutions(char const *filename,
levels *ls, solutions *ss)
{
fileinfo file;
char buf[8192];
char const *homedir;
unsigned long dword;
int n;
homedir = getenv("HOME");
if (!homedir || !*homedir)
homedir = ".";
if (*datadir == '~') {
snprintf(buf, sizeof buf, "%s", datadir + 1);
snprintf(datadir, sizeof datadir, "%s%s", homedir, buf);
}
if (*setsdir == '~') {
snprintf(buf, sizeof buf, "%s", setsdir + 1);
snprintf(setsdir, sizeof setsdir, "%s%s", homedir, buf);
}
if (*savedir == '~') {
snprintf(buf, sizeof buf, "%s", savedir + 1);
snprintf(savedir, sizeof savedir, "%s%s", homedir, buf);
}
if (strchr(filename, DIRSEP))
snprintf(buf, sizeof buf, "%s", filename);
else
snprintf(buf, sizeof buf, "%s%c%s", setsdir, DIRSEP, filename);
if (!fileopen(&file, buf, "rb", NULL)) {
if (!strchr(filename, DIRSEP)) {
snprintf(buf, sizeof buf, "%s", filename);
if (fileopen(&file, filename, "rb", NULL))
goto okay;
}
return warnfile(&file, "couldn't open");
}
okay:
if (!filereadint32(&file, &dword, "invalid level set file"))
return FALSE;
if (dword == dac_sig) {
n = snprintf(buf, sizeof buf, "%s%c", datadir, DIRSEP);
if (fscanf(file.fp, "=%255s", buf + n) != 1)
return warnfile(&file, "invalid configuration file");
if (!fileopen(&file, buf, "rb", "couldn't open"))
return FALSE;
}
fileclose(&file, NULL);
if (!readlevels(buf, ls))
return FALSE;
n = strlen(filename);
if (filename[n - 4] == '.' && tolower(filename[n - 3]) == 'd'
&& tolower(filename[n - 2]) == 'a'
&& tolower(filename[n - 1]) == 't')
n -= 4;
if (strchr(filename, DIRSEP))
snprintf(buf, sizeof buf, "%.*s.tws", n, filename);
else
snprintf(buf, sizeof buf, "%s%c%.*s.tws", savedir, DIRSEP,
n, filename);
if (!readsolutions(buf, ss))
return FALSE;
return TRUE;
}
static void calculatetimesandscores(solutions *ss, levels const *ls)
{
int const tickspersec = 20;
solution *s;
int m, n;
for (m = 0, s = ss->solutions ; m < ss->count ; ++m, ++s) {
if (!s->time) {
s->score = 0;
continue;
}
for (n = 0 ; n < ls->count ; ++n)
if (ls->levels[n].number == s->number)
break;
if (n == ls->count) {
warn("found solution for nonexistent level %d", s->number);
continue;
}
s->score = s->number * 500;
if (ls->levels[n].time) {
s->time = (ls->levels[n].time * tickspersec - s->time);
s->time = (s->time + tickspersec - 1) / tickspersec;
s->score += 10 * s->time;
} else {
s->time = 0;
}
}
}
static void displaysolutioninfo(solutions *ss, char const *infilename)
{
int total, n;
if (!ss->count) {
printf("; (empty)\n");
return;
}
if (infilename)
printf("; %s\n\n", infilename);
printf("[Chip's Challenge]\n");
if (ss->currentlevel > 0)
printf("Current Level=%d\n", ss->currentlevel);
total = 0;
for (n = 0 ; n < ss->count ; ++n) {
printf("Level%u=%s",
ss->solutions[n].number, ss->solutions[n].passwd);
if (ss->solutions[n].score)
printf(",%lu,%lu", ss->solutions[n].time, ss->solutions[n].score);
putchar('\n');
total += ss->solutions[n].score;
}
printf("Highest Level=%d\n", n);
printf("Current Score=%d\n", total);
}
static void yowzitch(FILE *out)
{
fprintf(out, "Usage: twstoini [-DLS DIR] SETNAME\n"
" -L DIR Read level sets from DIR [default=%s]\n"
" -D DIR Read data files from DIR [default=%s]\n"
" -S DIR Read solution files from DIR [default=%s]\n",
DEFAULT_SETS_DIR, DEFAULT_DATA_DIR, DEFAULT_SAVE_DIR);
}
int main(int argc, char *argv[])
{
char const *setname = NULL;
levels ls;
solutions ss;
int n;
if (argc == 1 || !strcmp(argv[1], "-h") || !strcmp(argv[1], "--help")) {
yowzitch(stdout);
return EXIT_SUCCESS;
}
for (n = 1 ; n < argc ; ++n) {
if (argv[n][0] == '-') {
switch (argv[n][1]) {
case 'D':
strcpy(datadir, argv[n][2] ? &argv[n][2] : argv[++n]);
break;
case 'L':
strcpy(setsdir, argv[n][2] ? &argv[n][2] : argv[++n]);
break;
case 'S':
strcpy(savedir, argv[n][2] ? &argv[n][2] : argv[++n]);
break;
default:
yowzitch(stderr);
return EXIT_FAILURE;
}
} else {
if (setname) {
yowzitch(stderr);
return EXIT_FAILURE;
}
setname = argv[n];
}
}
if (!findlevelsandsolutions(setname, &ls, &ss))
return EXIT_FAILURE;
calculatetimesandscores(&ss, &ls);
displaysolutioninfo(&ss, setname);
freelevels(&ls);
freesolutions(&ss);
return EXIT_SUCCESS;
}