[go: up one dir, main page]

Menu

[r7]: / engine / loadfile.c  Maximize  Restore  History

Download this file

605 lines (483 with data), 10.7 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
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
//
// File loader
//
#include <allegro.h>
#include <stdio.h>
#include <fcntl.h>
#include <string.h>
#include <sys/stat.h>
#include "ithelib.h"
#include "loadfile.h"
#include "oscli.h"
#include "tdgui.h"
// Defines
// Macro to make a directory using the right API version
#ifdef _WIN32
#define MAKE_DIR(a) mkdir(a)
#else
#define MAKE_DIR(a) mkdir(a,S_IRUSR|S_IWUSR|S_IXUSR);
#endif
// Variables
char savegamedest[1024];
// Functions
extern int check_rar_file(char *filename);
extern void scan_rar_file(char *filename);
#ifdef _WIN32
static void winslash(char *name);
#endif
static int blank_savegame(const char *name, int attrib, void *irrelevant);
static int copy_savegame(const char *name, int attrib, void *irrelevant);
// Code
// user can choose a datafile
int data_choose(char *path,char *path_file,int size)
{
char *home;
int i,len;
int k=0;
memset(path,0,size); // Blank it
// home=getenv("HOME");
// if(!home)
home=".";
strcpy(path_file,home);
strcat(path_file,"/");
// if(!file_select_ex("load game (gamedata.ini)",path_file,"ini",size,0,0))
if(!billfile_select("load game (gamedata.ini)",path_file,"ini"))
return 0;
else
{
len = strlen(path_file);
strslash(path_file); // Ensure slashes are correct
// Find last slash
for (i=0;i<len;i++)
{
if (path_file[i]=='/')
{
k=i;
}
}
i=0;
// Copy everything up to the last slash
while(i<=k)
{
path[i]=path_file[i];
i++;
}
return 1;
}
}
char *loadfile(char *srcname, char *destname)
{
// Normalise filename
strcpy(destname,srcname);
strlwr(destname);
strslash(destname);
// Check it
if(iexist(destname))
return destname;
return NULL;
}
int fileexists(char *name)
{
//char fn[1024];
if(!name)
ithe_panic("fileexists: NULL filename given",NULL);
return(iexist(name));
//if(strchr(name,'#'))
// return(exists(name));
//ilog_quiet("F-exists\n");
//return((int)loadfile(name,fn));
}
int filelen(char *fname)
{
char fn[1024];
if(!fname)
ithe_panic("filelen: NULL filename given",NULL);
if(strchr(fname,'#'))
return file_size(fname);
if(loadfile(fname,fn))
return file_size(fn);
// Die
ithe_panic("filelen: file does not exist",fname);
return 0;
}
/*
* Convert slashes to windows format
*/
#ifdef _WIN32
void winslash(char *name)
{
char *a;
do {
a=strchr(name,'/');
if(a)
*a='\\';
// *a='|';
} while(a);
}
#endif
/*
* Convert a path from Absolute to Relative format
*/
void clean_path(char *fn)
{
char filename[1024];
char pathname[1024];
char *ptr;
int len;
strcpy(pathname,"/");
strcat(pathname,projectname);
strcat(pathname,"/");
strcpy(filename,"/");
strcat(filename,fn);
strslash(pathname);
strslash(filename);
strlwr(pathname);
strlwr(filename);
// OK, everything should be ready.
ptr = strstr(filename,pathname);
if(!ptr)
return; // It's clean
// It's not clean. Clean it.
ptr += strlen(pathname);
ptr--; // Remove initial '/'
len = (int)(ptr-filename);
strcpy(filename,&fn[len]);
strcpy(fn,filename);
}
/*
* .WAD manipulation functions. Assumes a 40-byte signature in front of the
* data and works around this. If you strip the first 40 bytes of the
* file away you have a true .WAD file that can load into DEU.
*/
void StartWad(IFILE *ofp)
{
iputl_i(0x44415750,ofp);// PWAD
iputl_i(0L,ofp);// Entries, patched later
iputl_i(0L,ofp);// Offset, ditto
}
/*
* Append the directory to the file to make a true .WAD
*/
void FinishWad(IFILE *ofp, WadEntry entries[])
{
unsigned int directory,num;
directory = itell(ofp);
// Write the entries
for(num=0;entries[num].name;num++)
{
iputl_i(entries[num].start-40,ofp); // Cancel out the signature
iputl_i(entries[num+1].start-entries[num].start,ofp);
iwrite((unsigned char *)entries[num].name,8,ofp);
}
// Back up the WAD and write the initial entries. Since each IRE file has
// a 40-byte signature of it's own, we don't start from 4 like DEU does.
iseek(ofp,44,SEEK_SET);
iputl_i(num,ofp);
iputl_i(directory-40,ofp); // -40 to cancel the signature
// Finish
iclose(ofp);
}
int GetWadEntry(IFILE *ifp, char *name)
{
unsigned int sig,num,off,ctr;
char buf[]="nothing!";
iseek(ifp,40,SEEK_SET);
sig = igetl_i(ifp);
if(sig != 0x44415750)
return 0;
num = igetl_i(ifp);
off = igetl_i(ifp);
iseek(ifp,off+40,SEEK_SET); // Cancel out the signature
for(ctr=0;ctr<num;ctr++)
{
off = igetl_i(ifp);
igetl_i(ifp); // skip length
iread((unsigned char *)buf,8,ifp);
if(!strcmp(buf,name))
{
iseek(ifp,off+40,SEEK_SET); // Cancel out the signature
return 1;
}
}
return 0;
}
// Work out savegame dir (and save it)
char *getsavegamedir(int savegame)
{
char temp[256];
char *home;
char *out;
out=strLocalBuf(); // Get local string (with garbage collection)
#ifdef __SOMEUNIX__
home=getenv("HOME");
if(!home)
return NULL;
sprintf(temp,"%s/.iresaves/%s/slot%02d/",home,projectname,savegame);
#else
sprintf(temp,"savegame\\%s\\slot%02d\\",projectname,savegame);
#ifndef _WIN32
strslash(temp);
#endif
#endif
strcpy(out,temp);
return out;
}
// File mangling for mapfiles, work out the full path and filename
char *makemapname(int number, int savegame, char *extension)
{
char *temp,*t2;
temp=strLocalBuf();
// Savegame 0 means the actual level data (master copy) not a savegame,
// (savegames are modified copies of the original).
if(!savegame)
sprintf(temp,"%s%s%04d%s",projectdir,projectname,number,extension);
else
{
t2=getsavegamedir(savegame);
if(!t2)
return NULL;
sprintf(temp,"%s%s%04d%s",t2,projectname,number,extension);
#ifdef _WIN32
strwinslash(temp);
#else
strslash(temp);
#endif
}
return temp;
}
// Make savegame dir (somewhat involved)
int makesavegamedir(int savegame, int erase)
{
char temp[512];
char *home;
#ifdef __SOMEUNIX__
home=getenv("HOME");
if(!home)
return 0;
sprintf(temp,"%s/.iresaves",home);
MAKE_DIR(temp);
sprintf(temp,"%s/.iresaves/%s",home,projectname);
MAKE_DIR(temp);
sprintf(temp,"%s/.iresaves/%s/slot%02d",home,projectname,savegame);
MAKE_DIR(temp);
// Empty it
if(erase)
{
// sprintf(temp,"rm -f %s/.iresaves/%s/slot%02d/*",home,projectname,savegame);
// system(temp);
sprintf(temp,"%s/.iresaves/%s/slot%02d/*",home,projectname,savegame);
for_each_file_ex(temp,0,FA_DIREC,blank_savegame,0);
}
#else
MAKE_DIR("savegame");
sprintf(temp,"savegame\\%s",projectname);
#ifndef _WIN32
strslash(temp);
#endif
MAKE_DIR(temp);
sprintf(temp,"savegame\\%s\\slot%02d",projectname,savegame);
#ifndef _WIN32
strslash(temp);
#endif
MAKE_DIR(temp);
// Empty it
if(erase)
{
sprintf(temp,"savegame\\%s\\slot%02d\\*.*",projectname,savegame);
// for_each_file_ex(temp,FA_RDONLY|FA_ARCH,0,blank_savegame,0);
for_each_file_ex(temp,0,FA_DIREC,blank_savegame,0);
}
#endif
return 1;
}
/*
* Allegro callback function
*/
static int blank_savegame(const char *name, int attrib, void *irrelevant)
{
ilog_quiet("Erase: %s\n",name);
remove(name);
return 0;
}
void copysavegamedir(int src, int dest)
{
char *srcdir;
char *destdir;
destdir=getsavegamedir(dest);
strcpy(savegamedest,destdir); // Copy destination to a global
srcdir=getsavegamedir(src);
strcat(srcdir,"*.*"); // Seek all files
ilog_quiet("Try to copy: %s %s\n",srcdir,savegamedest);
//for_each_file_ex(srcdir,FA_RDONLY|FA_ARCH,0,copy_savegame,0);
for_each_file_ex(srcdir,0,FA_DIREC,copy_savegame,0);
}
/*
* Allegro callback function to copy a single file to a certain directory
*/
static int copy_savegame(const char *name, int attrib, void *irrelevant)
{
FILE *fi,*fo;
char fname[512];
unsigned char buf[4096]; // 4k read buffer
int bytes,total;
fi=fopen(name,"rb");
if(!fi)
{
ilog_quiet("COPY: sourcename: can't open '%s'\n",name);
return 0;
}
// Assemble path - get_filename is an allegro support function to strip
// the path and return a bare filename
strcpy(fname,savegamedest);
strcat(fname,get_filename(name));
fo=fopen(fname,"wb");
if(!fo)
{
fclose(fi);
ilog_quiet("COPY: destname: can't create '%s'\n",fname);
return 0;
}
total=0;
do
{
bytes=fread(buf,1,4096,fi);
total+=bytes;
fwrite(buf,1,bytes,fo);
} while(bytes==4096);
fclose(fi);
fclose(fo);
ilog_quiet("COPY: '%s' to '%s' : %d bytes copied\n",name,fname,total);
return 0;
}
/*
* Read and write RLE sprite files. Average colour weighting can be
* stored in the file as well.
*/
void save_rle_sprite(RLE_SPRITE* spr, char *fname, long avgcol)
{
FILE *fp;
fp = fopen(fname,"wb");
if(!fp)
{
// Couldn't write file. Try building the path first.
makepath(fname);
fp = fopen(fname,"wb");
if(!fp)
return;
}
putw(spr->size, fp);
putw(spr->color_depth, fp);
putw(spr->w, fp);
putw(spr->h, fp);
fwrite(spr->dat, 1, spr->size, fp);
putw(avgcol, fp);
fclose(fp);
}
/*
* Load an RLE-compressed sprite, optionally get the average colour weight
*/
RLE_SPRITE* load_rle_sprite(char *fname, long *avgcol)
{
RLE_SPRITE* spr = 0;
int sz = 0;
FILE *fp;
fp = fopen(fname,"rb");
if(!fp)
return NULL;
sz = getw(fp);
if(sz < 1)
return 0;
spr = calloc(1,sizeof(RLE_SPRITE) + sz);
if(!spr)
{
// out of memory, but skip rest of data in file
fseek(fp, sz + 12, SEEK_SET);
return 0;
}
spr->color_depth = getw(fp);
spr->w = getw(fp);
spr->h = getw(fp);
spr->size = sz;
fread(spr->dat, 1, sz, fp);
// Get average pixel colour, if a pointer is given
if(avgcol)
*avgcol=getw(fp);
fclose(fp);
return spr;
}
#ifdef _WIN32
void makepath(char *fname)
{
char buffer[1024];
char *p,*pp;
strcpy(buffer,fname);
winslash(buffer); // Normalise slashes
p=strrchr(buffer,'\\');
if(p)
*p=0; // Chop off the filename
MAKE_DIR(buffer);
pp=&buffer[0];
do
{
p=strchr(pp,'\\');
if(p)
{
*p=0; // Pop the slash out
MAKE_DIR(buffer);
*p='\\'; // Put it back
pp=++p; // Look for the next one?
}
} while(p);
}
#else
void makepath(char *fname)
{
char buffer[1024];
char *p,*pp;
strcpy(buffer,fname);
strslash(buffer); // Normalise slashes
p=strrchr(buffer,'/');
if(p)
*p=0; // Chop off the filename
MAKE_DIR(buffer);
pp=&buffer[0];
do
{
p=strchr(pp,'/');
if(p)
{
*p=0; // Pop the slash out
MAKE_DIR(buffer);
*p='/'; // Put it back
pp=++p; // Look for the next one?
}
} while(p);
}
#endif
/*
// copy a file from an absolute path to a relative path
void cpfile(char *local_path, char *abs_path, char *file_name)
{
char cmd[1024];
char tmp1[1024];
char tmp2[1024];
FILE *fd;
strcpy(tmp1,abs_path);
strcat(tmp1,file_name);
strcpy(tmp2,local_path);
strcat(tmp2,file_name);
// Can we copy it?
if(access(tmp1,R_OK))
return;
// BUG: This checks if it exists, not if it can be created
// if(access(tmp2,W_OK))
// return;
sprintf(cmd,"cp -f %s %s > /dev/null",tmp1,tmp2);
fd = fopen(tmp2,"r");
if (!fd)
system(cmd);
else
fclose(fd);
// printf("%s\n",cmd);
}
*/