forked from haithun/CoD4X17a_testing
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscr_vm_fs.c
462 lines (356 loc) · 10.3 KB
/
scr_vm_fs.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
/*
===========================================================================
Copyright (C) 2010-2013 Ninja and TheKelm of the IceOps-Team
Copyright (C) 1999-2005 Id Software, Inc.
This file is part of CoD4X17a-Server source code.
CoD4X17a-Server source code is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
CoD4X17a-Server source code is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>
===========================================================================
*/
#include "q_shared.h"
#include "qcommon_io.h"
#include "filesystem.h"
#include "scr_vm.h"
#include "cvar.h"
#include <string.h>
static int scr_fopencount;
static scr_fileHandle_t scr_fsh[MAX_SCRIPT_FILEHANDLES];
/*
==============
Scr_FS_CloseFile
If the FILE pointer is an open pak file, leave it open.
For some reason, other dll's can't just cal fclose()
on files returned by FS_FOpenFile...
==============
*/
qboolean Scr_FS_CloseFile( scr_fileHandle_t* f ) {
// we didn't find it as a pak, so close it as a unique file
if (f->fh) {
fclose (f->fh);
Com_Memset( f, 0, sizeof( scr_fileHandle_t ));
return qtrue;
}
Com_Memset( f, 0, sizeof( scr_fileHandle_t ));
return qfalse;
}
/*
========================================================================================
Handle based file calls for virtual machines
========================================================================================
*/
/*
=============
FS_FreeFile
=============
*/
/*
void Scr_FS_FreeFile( void *buffer ) {
if ( !buffer ) {
Com_Error( ERR_FATAL, "FS_FreeFile( NULL )" );
}
*fs_loadStack = (*fs_loadStack)-1;
free( buffer );
}
*/
/*
=================
Scr_FS_ReadLine
Custom function that only reads single lines
Properly handles line reads
=================
*/
int Scr_FS_ReadLine( void *buffer, int len, fileHandle_t f ) {
char *read;
char *buf;
if ( !fs_searchpaths ) {
Com_Error( ERR_FATAL, "Filesystem call made without initialization\n" );
}
if(f > MAX_SCRIPT_FILEHANDLES || f < 1){
Scr_Error("Scr_FS_ReadLine: Out of range filehandle\n");
return 0;
}
buf = buffer;
*buf = 0;
read = fgets (buf, len, scr_fsh[f -1].fh);
if (read == NULL) { //Error
if(feof(scr_fsh[f -1].fh))
return 0;
Com_PrintScriptRuntimeWarning("Scr_FS_ReadLine: couldn't read line");
return -1;
}
return 1;
}
qboolean Scr_FS_AlreadyOpened( char* qpath, char* filename, size_t fnamelen){
int i = 0;
char qpathbuf[MAX_OSPATH];
Q_strncpyz(qpathbuf, qpath, sizeof(qpathbuf));
FS_ConvertPath(qpathbuf);
do{
Q_strncpyz(filename, &qpathbuf[i], fnamelen);
i = Q_strichr(&qpathbuf[i], '/');
i += 1;
}while(i > 0);
for(i = 0; i < MAX_SCRIPT_FILEHANDLES; i++){
if(!Q_stricmp(filename, scr_fsh[i].filename)){
Com_PrintScriptRuntimeWarning("Script_FileOpen: Tried to open a file with the same name two times: %s\n", filename);
*filename = 0;
return qtrue;
}
}
return qfalse;
}
/*
===========
Scr_FS_FOpenFile
===========
*/
qboolean Scr_FS_FOpenFile( const char *filename, fsMode_t mode, scr_fileHandle_t* f ) {
char *ospath;
f->fh = NULL;
if ( !fs_searchpaths ) {
Com_Error( ERR_FATAL, "Filesystem call made without initialization" );
}
// search homepath
ospath = FS_BuildOSPath( fs_homepath->string, "", filename );
// remove trailing slash
if((ospath[strlen(ospath)-1]) == '/')
ospath[strlen(ospath)-1] = '\0';
if ( fs_debug->boolean ) {
Com_Printf( "Scr_FS_FOpenFile (fs_homepath): %s\n", ospath );
}
switch(mode){
case FS_READ:
f->fh = fopen( ospath, "rt" );
break;
case FS_WRITE:
if( !FS_CreatePath( ospath )) {
f->fh = fopen( ospath, "wt" );
}
break;
case FS_APPEND:
if( !FS_CreatePath( ospath )) {
f->fh = fopen( ospath, "at" );
}
break;
default:
Scr_Error("Scr_FS_FOpenFile: bad mode.\n");
return qfalse;
}
if (!f->fh){
// NOTE TTimo on non *nix systems, fs_homepath == fs_basepath, might want to avoid
if (Q_stricmp(fs_homepath->string,fs_basepath->string)){
// search basepath
ospath = FS_BuildOSPath( fs_basepath->string, "", filename);
// remove trailing slash
if((ospath[strlen(ospath)-1]) == '/')
ospath[strlen(ospath)-1] = '\0';
if ( fs_debug->boolean ){
Com_Printf( "Scr_FS_FOpenFile (fs_basepath): %s\n", ospath );
}
switch(mode){
case FS_READ:
f->fh = fopen( ospath, "rt" );
break;
case FS_WRITE:
if( !FS_CreatePath( ospath )) {
f->fh = fopen( ospath, "wt" );
}
break;
case FS_APPEND:
if( !FS_CreatePath( ospath )) {
f->fh = fopen( ospath, "at" );
}
break;
default:
return qfalse;
}
}
}
if ( f->fh ) {
f->baseOffset = ftell(f->fh);
fseek(f->fh,0,SEEK_END);
f->fileSize = ftell(f->fh);
fseek(f->fh,0,SEEK_SET);
return qtrue;
}
return qfalse;
}
fileHandle_t Scr_OpenScriptFile( char* qpath, scr_fileHandleType_t ft, fsMode_t mode){
int i;
char filename[MAX_QPATH];
if ( !fs_searchpaths ) {
Com_Error( ERR_FATAL, "Filesystem call made without initialization" );
}
if(Scr_FS_AlreadyOpened(qpath, filename, sizeof(filename))){
return 0;
}
for(i=0; i < MAX_SCRIPT_FILEHANDLES; i++){
if(!scr_fsh[i].fh){
if(!Scr_FS_FOpenFile(qpath, mode, &scr_fsh[i])){
return 0;
}
scr_fsh[i].type = ft;
Q_strncpyz(scr_fsh[i].filename, filename, MAX_QPATH);
scr_fopencount++;
return i+1;
}
}
Com_PrintScriptRuntimeWarning("Scr_OpenScriptFile: Exceeded limit of %i opened files\n", MAX_SCRIPT_FILEHANDLES);
return 0;
}
qboolean Scr_CloseScriptFile( fileHandle_t fh){
if ( !fs_searchpaths ) {
Com_Error( ERR_FATAL, "Filesystem call made without initialization" );
}
if(fh > MAX_SCRIPT_FILEHANDLES || fh < 1){
Scr_Error("Scr_CloseScriptFile: Out of range filehandle\n");
return qfalse;
}
switch(scr_fsh[fh -1].type){
case SCR_FH_FILE:
if(!Scr_FS_CloseFile(&scr_fsh[fh -1])){
return qfalse;
}
break;
case SCR_FH_PARALIST:
//ToDo Additional handling for memcached files
if(!Scr_FS_CloseFile(&scr_fsh[fh -1])){
return qfalse;
}
break;
case SCR_FH_INDEXPARALIST:
//ToDo Additional handling for memcached files
if(!Scr_FS_CloseFile(&scr_fsh[fh -1])){
return qfalse;
}
}
scr_fopencount--;
return qtrue;
}
/*
=================
FS_Read
Properly handles partial reads
=================
*/
int Scr_FS_Read( void *buffer, int len, fileHandle_t f ) {
int block, remaining;
int read;
byte *buf;
int tries;
if ( !fs_searchpaths ) {
Com_Error( ERR_FATAL, "Filesystem call made without initialization" );
}
if(f > MAX_SCRIPT_FILEHANDLES || f < 1){
Scr_Error("Scr_FS_Read: Out of range filehandle\n");
return 0;
}
buf = (byte *)buffer;
remaining = len;
tries = 0;
while (remaining) {
block = remaining;
read = fread (buf, 1, block, scr_fsh[f -1].fh);
if (read == 0) {
// we might have been trying to read from a CD, which
// sometimes returns a 0 read on windows
if (!tries) {
tries = 1;
} else {
return len-remaining; //Com_Error (ERR_FATAL, "FS_Read: 0 bytes read");
}
}
if (read == -1) {
Scr_Error ("Scr_FS_Read: -1 bytes read");
}
remaining -= read;
buf += read;
}
return len;
}
/*
=================
Scr_FS_Write
Properly handles partial writes
=================
*/
int Scr_FS_Write( const void *buffer, int len, fileHandle_t h ) {
int block, remaining;
int written;
byte *buf;
int tries;
FILE *f;
if ( !fs_searchpaths ) {
Com_Error( ERR_FATAL, "Filesystem call made without initialization" );
}
if(h > MAX_SCRIPT_FILEHANDLES || h < 1){
Scr_Error("Scr_FS_Write: Out of range filehandle\n");
return 0;
}
f = scr_fsh[h -1].fh;
buf = (byte *)buffer;
remaining = len;
tries = 0;
while (remaining) {
block = remaining;
written = fwrite (buf, 1, block, f);
if (written == 0) {
if (!tries) {
tries = 1;
} else {
Com_Printf( "Scr_FS_Write: 0 bytes written\n" );
return 0;
}
}
if (written == -1) {
Com_Printf( "Scr_FS_Write: -1 bytes written\n" );
return 0;
}
remaining -= written;
buf += written;
}
return len;
}
#define PK3_SEEK_BUFFER_SIZE 65536
/*
=================
Scr_FS_Seek
=================
*/
int Scr_FS_Seek( fileHandle_t f, long offset, int origin ) {
int _origin;
if ( !fs_searchpaths ) {
Com_Error( ERR_FATAL, "Filesystem call made without initialization" );
return -1;
}
if(f > MAX_SCRIPT_FILEHANDLES || f < 1){
Scr_Error("Scr_FS_Seek: Out of range filehandle\n");
return -1;
}
FILE *file;
file = scr_fsh[f -1].fh;
switch( origin ) {
case FS_SEEK_CUR:
_origin = SEEK_CUR;
break;
case FS_SEEK_END:
_origin = SEEK_END;
break;
case FS_SEEK_SET:
_origin = SEEK_SET;
break;
default:
_origin = SEEK_CUR;
Scr_Error( "Bad origin in Scr_FS_Seek" );
break;
}
return fseek( file, offset, _origin );
}