forked from chocolatiers/doom-utilities
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jwadlink.c
622 lines (486 loc) · 11.3 KB
/
jwadlink.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
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
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
#define VERSION "1.2"
/*
=============================================================================
WADLINK
by John Carmack
=============================================================================
*/
#include "cmdlib.h"
#include "scriplib.h"
#include "lzlib.h"
#define MAXPATH 1025
//================
//
// wad file types
//
//================
typedef struct
{
long filepos;
long size;
char name[8];
} lumpinfo_t;
typedef struct
{
char identification[4];
long numlumps;
long infotableofs;
} wadinfo_t;
//==========================================================================
char destpath[MAXPATH];
wadinfo_t newwad; // the file being written out
lumpinfo_t *newlumps;
int newhandle;
lumpinfo_t *lump_p; // wadlumps[lumpon]
int lumpon; // current lump
char loadwadpath[MAXPATH];
char lumppath[MAXPATH];
int datahandle; // can be an individual file or a WAD
wadinfo_t loadwad; // a WAD file to look for lumps in
lumpinfo_t *wadlumps;
lumpinfo_t *wadlump_p; // info on lump to copy
boolean inwadfile;
boolean outputnamed;
char sourcepath[MAXPATH];
char defaultdestpath[MAXPATH];
int lumpscopied; // number of copy operations done
//============================================================================
/*
================
=
= CheckIfCompressed
=
= Checks the script file to see if the lump should be compressed, and then
= sets the high bit of the first character of the name if it should.
=
================
*/
void CheckIfCompressed(void)
{
// check for (LZSS) token
if (TokenAvailable())
{
GetToken(false);
if (!strcmpi(token, "(LZSS)"))
{
lump_p->name[0] |= 0x80;
} else {
Error ("Unrocognized token after lump: %s\n",token);
}
}
}
/*
================
=
= OpenLump
=
= If in a wad file, lseeks to the start of the lump
=
= If a separate file, sets inputhandle to the open file
=
================
*/
void OpenLump (void)
{
int i;
char lumpname[9];
if (inwadfile)
{
lumpname[8] = 0;
strncpy (lumpname, lump_p->name, 8);
//
// set wadlump_p to the lumpinfo in the open wad file
//
wadlump_p = wadlumps;
for (i=0 ; i<loadwad.numlumps ; i++, wadlump_p++)
if (strncmp( lump_p->name , wadlump_p->name, 8) == 0)
break;
if (i == loadwad.numlumps)
Error ("lump %s is not in wad file %s\n",lumpname,lumppath);
strcpy (lumppath, loadwadpath);
strcat (lumppath, " : ");
strcat (lumppath, lumpname);
}
else
{
//
// open the file on disk
//
datahandle = SafeOpenRead (lumppath);
}
}
/*
=================
=
= CopyLump
=
=================
*/
void CopyLump (void)
{
long size;
byte *buffer;
byte *compressed_buffer;
int compressed_size;
if (inwadfile)
{
lseek (datahandle, LittleLong (wadlump_p->filepos) ,SEEK_SET);
size = LittleLong (wadlump_p->size);
}
else
size = filelength (datahandle);
printf ("%4i = %s (%lu bytes)",lumpon,lumppath,size);
lump_p->filepos = LittleLong (tell(newhandle));
lump_p->size = LittleLong (size);
buffer = malloc (size);
SafeRead (datahandle, buffer, size);
// compress the lump if necessary
if (lump_p->name[0] & 0x80)
{
compressed_buffer = encode(buffer, size, &compressed_size);
free(buffer);
size = compressed_size;
buffer = compressed_buffer;
printf(" (%d compressed)", size);
}
SafeWrite (newhandle, buffer, size);
free (buffer);
printf("\n");
while (tell(newhandle)&3)
SafeWrite (newhandle,buffer,1);
if (!inwadfile)
close (datahandle);
lumpon++;
lump_p++;
lumpscopied++;
}
/*
=============================================================================
SCRIPT COMMANDS
=============================================================================
*/
/*
=================
=
= CmdCloseWad
=
=================
*/
void CmdCloseWad (void)
{
if (!inwadfile)
Error ("$CLOSEWAD issued without an open wad file\n");
close (datahandle);
free (wadlumps);
inwadfile = false;
}
/*
=================
=
= CmdOpenWad
=
=================
*/
void CmdOpenWad (void)
{
if (inwadfile)
CmdCloseWad ();
//
// get and qualify wad file name
//
GetToken (false);
strcpy (loadwadpath, token);
DefaultExtension (loadwadpath, ".wad");
DefaultPath (loadwadpath, sourcepath);
//
// open it and read in header / lump directory
//
datahandle = SafeOpenRead (loadwadpath);
SafeRead (datahandle,&loadwad,sizeof(loadwad));
loadwad.numlumps = LittleLong (loadwad.numlumps);
loadwad.infotableofs = LittleLong (loadwad.infotableofs);
if (strncmp(loadwad.identification,"IWAD",4))
Error ("Wad file %s doesn't have IWAD id\n",lumppath);
lseek (datahandle , loadwad.infotableofs , SEEK_SET);
wadlumps = malloc ( loadwad.numlumps*sizeof(lumpinfo_t) );
SafeRead (datahandle, wadlumps , loadwad.numlumps*sizeof(lumpinfo_t));
inwadfile = true;
}
/*
=================
=
= CmdShootWad
=
= Copies every lump in a wad file into the output
=
=================
*/
void CmdShootWad (void)
{
int i;
int compressed = 0;
CmdOpenWad ();
CheckIfCompressed();
if (lump_p->name[0] & 0x80) compressed = 1;
for (i=0 ; i<loadwad.numlumps ; i++)
{
strncpy (lump_p->name , wadlumps[i].name , 8);
OpenLump ();
if (compressed) lump_p->name[0] |= 0x80;
CopyLump ();
}
CmdCloseWad ();
}
/*
===================
=
= CmdLabel
=
===================
*/
void CmdLabel (void)
{
GetToken (false);
lump_p->filepos = 0;
lump_p->size = 0;
strupr (token);
strncpy( lump_p->name, token, 8);
printf ("%4i is LABEL: %s\n",lumpon,token);
lumpon++;
lump_p++;
}
/*
=====================
=
= CmdWadName
=
=====================
*/
void CmdWadName (char *name)
{
if (outputnamed)
Error ("Tried to rename the wad file");
//
// get the output filename
//
strcpy (destpath,name);
DefaultPath (destpath, defaultdestpath);
DefaultExtension (destpath, ".wad");
printf ("Output wad file: %s\n",destpath);
newhandle = SafeOpenWrite (destpath);
newwad.numlumps = 0;
strncpy (newwad.identification, "IWAD", 4);
//
// allocate space for the lump directory
//
newlumps = malloc (0xfff0);
lump_p = newlumps;
lumpon = 0;
//
// position the file pointer to begin writing data
//
// leave space in the data file for the header
lseek (newhandle,sizeof(newwad),SEEK_SET);
outputnamed = true;
inwadfile = false;
}
//==========================================================================
/*
================
=
= CheckCommands
=
================
*/
boolean CheckCommands (void)
{
if (token[0] != '$')
return false;
//
// link commands
//
if (!strcmpi(token,"$WADNAME") )
{
GetToken (false);
CmdWadName (token);
return true;
}
if (!strcmpi(token,"$OPENWAD") )
{
CmdOpenWad ();
return true;
}
if (!strcmpi(token,"$SHOOTWAD") )
{
CmdShootWad ();
return true;
}
if (!strcmpi(token,"$CLOSEWAD") )
{
CmdCloseWad ();
return true;
}
if (!strcmpi(token,"$LABEL") )
{
CmdLabel ();
return true;
}
Error ("Unrocognized command %s\n",token);
return false;
}
/*
=============================================================================
MAIN LOOP
=============================================================================
*/
/*
===================
=
= ProcessScript
=
===================
*/
void ProcessScript (void)
{
lumpscopied = 0;
outputnamed = false;
while (1)
{
//
// skip ahead to next command or lump name
//
GetToken (true);
if (endofscript)
break; // all done
if ( CheckCommands () )
continue; // linker command, not data lump
if (!outputnamed)
CmdWadName ("wadlink.wad"); // use default wad name
//
// get the lump name and lump pathname
// if the lump doesn't have an extension, default to .LMP
//
strcpy (lumppath,token);
DefaultPath (lumppath,sourcepath);
DefaultExtension (lumppath,".lmp");
ExtractFileBase (lumppath,lump_p->name);
OpenLump (); // Find the lump data, either in a seperate file or in the comp file
CheckIfCompressed(); // Checks whether to compress the lump
CopyLump (); // copy the lump to the output wad
}
}
/*
===================
=
= WriteDirectory
=
===================
*/
void WriteDirectory (void)
{
//
// write lumpinfo table
//
newwad.numlumps = LittleLong (lumpon);
newwad.infotableofs = LittleLong (tell(newhandle));
write (newhandle,newlumps, lumpon*sizeof(lumpinfo_t));
//
// write wadinfo
//
lseek (newhandle,0,SEEK_SET);
write (newhandle,&newwad,sizeof(newwad));
close (newhandle);
}
/*
===================
=
= main
=
===================
*/
int main (int argc, char **argv)
{
int l;
int parmnum,parmsleft;
char scriptfilename[MAXPATH];
printf ("\nWADLINK "VERSION" by John Carmack, copyright (c) 1992 Id Software\n");
parmsleft = argc;
//
// check for help
//
if (CheckParm ("?"))
{
printf (
"Usage: wadlink [-b] [-source path] [-dest path] [-script scriptfile]\n\n"
"-source path To place the source for the files in another directory\n"
"-dest path To place the linked wad file in another directory\n"
"-script file The script name defaults to WADLINK.WL if not specified\n"
);
exit (1);
}
//
// get source directory for data files
//
parmnum = CheckParm ("source");
if (parmnum)
{
strcpy (sourcepath,argv[parmnum+1]);
parmsleft -= 2;
}
else
{
getcwd (sourcepath,MAXPATH);
}
if (sourcepath[strlen(sourcepath)-1] != PATHSEPERATOR)
{
l = strlen(sourcepath);
sourcepath[l] = PATHSEPERATOR;
sourcepath[l+1] = 0;
}
printf ("Source directory_____: %s\n",sourcepath);
//
// get destination directory for link file
//
parmnum = CheckParm ("dest");
if (parmnum)
{
strcpy (defaultdestpath,argv[parmnum+1]);
parmsleft -= 2;
}
else
{
getcwd (defaultdestpath,MAXPATH);
}
if (defaultdestpath[strlen(defaultdestpath)-1] != PATHSEPERATOR)
{
l = strlen(defaultdestpath);
defaultdestpath[l] = PATHSEPERATOR;
defaultdestpath[l+1] = 0;
}
printf ("Destination directory: %s\n",defaultdestpath);
//
// get script file
//
parmnum = CheckParm ("script");
if (parmnum)
{
strcpy (scriptfilename,argv[parmnum+1]);
parmsleft -= 2;
}
else
{
getcwd (scriptfilename,MAXPATH);
strcat (scriptfilename,"/wadlink.wl");
}
printf ("Script file__________: %s\n",scriptfilename);
LoadScriptFile (scriptfilename);
if (parmsleft != 1)
Error ("Improper parameters. WADLINK -? for help.\n");
//
// start doing stuff
//
ProcessScript ();
WriteDirectory ();
printf ("\n%u lumps copied.\n",lumpscopied);
return 0;
}