-
Notifications
You must be signed in to change notification settings - Fork 0
/
a_vfile.cpp
254 lines (209 loc) · 6.22 KB
/
a_vfile.cpp
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
// ****************************************************************
// * PCP *
// * Copyright (C)1999 Dark Nova Software (DNS) *
// * All Rights Reserved. *
// * *
// * File: a_vfile.cpp *
// * Author: vecna *
// * Portability: All systems *
// * Description: Virtual-file interface. Packfile support. *
// ****************************************************************
#include "xerxes.h"
// *****
// TODO: Make vfile mem chunk desc for a file handle the actual file name.
// *****
/***********8
**********
*******
*********
HELLO, REWRITE ME.
OR AT LEAST FIX ME UP
FOR THE LOVE OF ALL THAT IS HOLY
********
*******
*********
*********
*******/
// ***************************** Data *****************************
mountstruct pack[3]; // packfile structs
char filesmounted=0; // Number of VRG files to check.
char headertag[]={ 'V','R','G','P','A','C','K',0 };
// ***************************** Code *****************************
int Exist(char *fname)
{
FILE *tempf;
tempf=fopen(fname,"rb");
if (tempf)
{
fclose(tempf);
return 1;
}
else return 0;
}
void DecryptHeader()
{
char lastvalue, precodebyte, *ptr;
ptr=(char *) pack[filesmounted].files;
lastvalue = *ptr;
ptr++;
while (ptr < (char *) (int) pack[filesmounted].files + (int) pack[filesmounted].numfiles*100)
{
precodebyte = *ptr;
(*ptr) -= lastvalue;
lastvalue = precodebyte;
ptr++;
}
}
void MountVFile(char *fname)
{
char buffer[10];
if (!(pack[filesmounted].vhandle = fopen(fname,"rb")))
err("*error* Unable to mount %s; file not found. \n",fname);
// Read pack header
memset(&buffer, 0, 10);
fread(&buffer, 1, 7, pack[filesmounted].vhandle);
if (strcmp(buffer,headertag))
err("*error* %s is not a valid packfile. \n",fname);
fread(&buffer, 1, 1, pack[filesmounted].vhandle);
if (buffer[0]!=1)
err("*error* %s is an incompatible packfile version. (ver reported: %d) \n",fname,buffer[0]);
fread(&pack[filesmounted].numfiles, 1, 4, pack[filesmounted].vhandle);
memcpy(pack[filesmounted].mountname, fname, strlen(fname)+1);
// Allocate memory for headers and read them in.
pack[filesmounted].files = (struct filestruct *) new byte[pack[filesmounted].numfiles*100];
fread(pack[filesmounted].files, pack[filesmounted].numfiles, 100, pack[filesmounted].vhandle);
DecryptHeader();
filesmounted++;
}
VFILE *vopen(char *fname)
{
VFILE *tmp;
char rf=0,vf=0;
int i, j=0;
// All files using V* are read-only. To write a file, use regular i/o.
// First we'll see if a real file exists, then we'll check for one in VFiles,
// if we don't find one in VFile or it's overridable then a real file will
// be used. That's the general logic progression.
if (Exist(fname)) rf=1;
// Search the VFiles.
for (i=filesmounted-1; i>=0; i--)
{
for (j=0; j<pack[i].numfiles; j++)
if (!strcasecmp(fname,(char *) pack[i].files[j].fname)) { vf=1; break; }
if (vf) break;
}
if (!vf && !rf) return 0;
tmp = new VFILE;
if (vf && rf)
{
if (pack[i].files[j].override) vf=0;
else rf=0;
}
if (vf)
{
tmp -> fp=pack[i].vhandle;
tmp -> s=1;
tmp -> v=i;
tmp -> i=j;
pack[i].files[j].curofs=0;
fseek(tmp -> fp, pack[i].files[j].packofs, 0);
pack[i].curofs=pack[i].files[j].packofs;
return tmp;
}
tmp -> fp=fopen(fname,"rb");
tmp -> s=0; tmp -> v=0; tmp -> i=0;
return tmp;
}
void vread(void *dest, int len, VFILE *f)
{
// This is fairly simple.. Just make sure our filepointer is at the right
// place, then do a straight fread.
if (f -> s)
{
if (pack[f -> v].curofs != (pack[f -> v].files[f -> i].packofs + pack[f -> v].files[f -> i].curofs))
fseek(f -> fp, pack[f -> v].files[f -> i].curofs+pack[f -> v].files[f -> i].packofs, 0);
pack[f -> v].files[f -> i].curofs += len;
pack[f -> v].curofs+=len;
}
fread(dest, 1, len, f -> fp);
}
void vclose(VFILE *f)
{
if (!f) return;
if (!f -> s) fclose(f -> fp);
f -> fp=0;
delete f;
}
int filesize(VFILE *f)
{
int oldpos, tmp;
// Filesize for Vfiles is real simple.
if (f -> s) return pack[f -> v].files[f -> i].size;
// It's a bit more complex for external files.
oldpos=ftell(f -> fp);
fseek(f -> fp, 0, 2);
tmp=ftell(f -> fp);
fseek(f -> fp, oldpos, 0);
return tmp;
}
void vseek(VFILE *f, int offset, int origin)
{
if (!f->s)
{
fseek(f -> fp, offset, origin);
return;
}
switch(origin)
{
case 0: pack[f->v].files[f->i].curofs=offset;
fseek(f->fp, offset+pack[f->v].files[f->i].packofs, 0);
return;
case 1: pack[f->v].files[f->i].curofs+=offset;
fseek(f->fp, offset, 1);
return;
case 2: pack[f->v].files[f->i].curofs = pack[f->v].files[f->i].size-offset;
fseek(f->fp, pack[f->v].files[f->i].curofs + pack[f->v].files[f->i].packofs, 0);
return;
}
}
void _vscanf(VFILE *f, char *format, char *dest)
{
fscanf(f -> fp, format, dest);
if (f -> s)
pack[f -> v].files[f -> i].curofs = ftell(f -> fp) - pack[f -> v].files[f -> i].packofs;
}
void _vscanf(VFILE *f, char *format, int *dest)
{
fscanf(f -> fp, format, dest);
if (f -> s)
pack[f -> v].files[f -> i].curofs = ftell(f -> fp) - pack[f -> v].files[f -> i].packofs;
}
char vgetc(VFILE *f)
{
char c;
vread(&c, 1, f);
return c;
}
word vgetw(VFILE *f)
{
word c;
vread((char *) &c, 2, f);
return c;
}
void vgets(char *str, int len, VFILE *f)
{
if (f->s)
{
if (pack[f->v].curofs != (pack[f->v].files[f->i].packofs + pack[f->v].files[f->i].curofs))
fseek(f->fp, pack[f->v].files[f->i].curofs+pack[f->v].files[f->i].packofs, 0);
pack[f->v].files[f->i].curofs+=len;
pack[f->v].curofs+=len;
}
fgets(str, len, f->fp);
}
int vtell(VFILE* f)
{
if (!f->s)
return ftell(f->fp);
return pack[f->v].files[f->i].curofs;
}