-
Notifications
You must be signed in to change notification settings - Fork 2
/
vm_file.c
307 lines (254 loc) · 6.68 KB
/
vm_file.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
#include "shared/shared.h"
#include "vm.h"
#include "vm_string.h"
#include "vm_file.h"
#include "g_file.h"
#include "game.h"
#ifndef KMQUAKE2_ENGINE_MOD
typedef size_t fileHandle_t;
typedef enum
{
FS_READ,
FS_WRITE,
FS_APPEND
} fsMode_t;
static FILE *OpenFile(qcvm_t *vm, const char *name, const fsMode_t mode)
{
return fopen(qcvm_temp_format(vm, "%s%s", vm->path, name), mode == FS_READ ? "rb" : mode == FS_APPEND ? "ab" : "wb");
}
#endif
static void QC_LoadFile(qcvm_t *vm)
{
const char *name = qcvm_argv_string(vm, 0);
void *buffer = NULL;
int32_t len = 0;
#ifdef KMQUAKE2_ENGINE_MOD
len = gi.LoadFile((char *)name, &buffer);
#else
FILE *handle = OpenFile(vm, name, FS_READ);
if (handle)
{
fseek(handle, 0, SEEK_END);
len = ftell(handle);
fseek(handle, 0, SEEK_SET);
buffer = qcvm_alloc(vm, len);
fread(buffer, sizeof(char), len, handle);
fclose(handle);
}
else
len = -1;
#endif
if (len >= 1)
qcvm_set_global_str(vm, GLOBAL_PARM1, (const char *)buffer, len, true);
else
qcvm_set_global_str(vm, GLOBAL_PARM1, "", 0, false);
qcvm_return_int32(vm, len);
#ifdef KMQUAKE2_ENGINE_MOD
if (buffer)
gi.FreeFile(buffer);
#else
if (buffer)
qcvm_mem_free(vm, buffer);
#endif
}
static void CloseFile(qcvm_t *vm, void *ptr)
{
#ifdef KMQUAKE2_ENGINE_MOD
gi.CloseFile((fileHandle_t)ptr);
#else
fclose((FILE *)ptr);
#endif
}
static qcvm_handle_descriptor_t fileHandle_descriptor =
{
.free = CloseFile
};
static void QC_OpenFile(qcvm_t *vm)
{
const char *name = qcvm_argv_string(vm, 0);
const fsMode_t mode = qcvm_argv_int32(vm, 2);
int32_t len;
int32_t qhandle;
#ifdef KMQUAKE2_ENGINE_MOD
fileHandle_t handle;
len = gi.OpenFile(name, &handle, mode);
if (len != -1)
qhandle = qcvm_handle_alloc(vm, (void *)(size_t)handle, &fileHandle_descriptor);
#else
FILE *handle = OpenFile(vm, name, mode);
if (handle)
{
fseek(handle, 0, SEEK_END);
len = ftell(handle);
fseek(handle, 0, SEEK_SET);
qhandle = qcvm_handle_alloc(vm, handle, &fileHandle_descriptor);
}
else
len = -1;
#endif
if (len != -1)
qcvm_set_global_typed_value(int32_t, vm, GLOBAL_PARM1, qhandle);
qcvm_return_int32(vm, len);
}
static void QC_OpenCompressedFile(qcvm_t *vm)
{
#ifdef KMQUAKE2_ENGINE_MOD
const char *zipName = qcvm_argv_string(vm, 0);
const char *name = qcvm_argv_string(vm, 1);
const fsMode_t mode = qcvm_argv_int32(vm, 3);
int32_t len;
fileHandle_t handle;
len = gi.OpenCompressedFile(zipName, name, &handle, mode);
if (len != -1)
{
int32_t qhandle = qcvm_handle_alloc(vm, (void *)(size_t)handle, &fileHandle_descriptor);
qcvm_set_global_typed_value(fileHandle_t, vm, GLOBAL_PARM1, qhandle);
}
qcvm_return_int32(vm, len);
#else
qcvm_return_int32(vm, -1);
#endif
}
static void QC_FRead(qcvm_t *vm)
{
const qcvm_pointer_t pointer = qcvm_argv_pointer(vm, 0);
const int32_t size = qcvm_argv_int32(vm, 1);
const fileHandle_t *handle = qcvm_argv_handle(fileHandle_t, vm, 2);
int32_t read;
void *address;
if (!qcvm_resolve_pointer(vm, pointer, false, size, &address))
qcvm_error(vm, "bad pointer");
#ifdef KMQUAKE2_ENGINE_MOD
read = gi.FRead(address, size, (fileHandle_t)handle);
#else
read = fread(address, size, 1, (FILE *)handle);
#endif
qcvm_return_int32(vm, read);
}
static void QC_FReadString(qcvm_t *vm)
{
int32_t size = qcvm_argv_int32(vm, 1);
const fileHandle_t *handle = qcvm_argv_handle(fileHandle_t, vm, 2);
int32_t read = 0;
if (size == -1)
{
}
else if (size)
{
char *buffer = qcvm_temp_buffer(vm, size);
#ifdef KMQUAKE2_ENGINE_MOD
read = gi.FRead(buffer, size, (fileHandle_t)handle);
#else
read = fread(buffer, size, 1, (FILE *)handle);
#endif
// trim trailing zeroes
while (size && !buffer[size - 1])
size--;
qcvm_set_global_str(vm, GLOBAL_PARM0, buffer, size, true);
}
qcvm_return_int32(vm, read);
}
static void QC_FWrite(qcvm_t *vm)
{
const qcvm_pointer_t pointer = qcvm_argv_pointer(vm, 0);
const int32_t size = qcvm_argv_int32(vm, 1);
const fileHandle_t *handle = qcvm_argv_handle(fileHandle_t, vm, 2);
int32_t written;
void *address;
if (!qcvm_resolve_pointer(vm, pointer, false, size, &address))
qcvm_error(vm, "bad pointer");
#ifdef KMQUAKE2_ENGINE_MOD
written = gi.FWrite(address, size, (fileHandle_t)handle);
#else
written = fwrite(address, size, 1, (FILE *)handle);
#endif
qcvm_return_int32(vm, written);
}
static void QC_CreatePath(qcvm_t *vm)
{
const char *name = qcvm_argv_string(vm, 0);
name = qcvm_cpp_absolute_path(vm, name);
#ifdef KMQUAKE2_ENGINE_MOD
gi.CreatePath((char *)name);
#else
mkdir(name);
#endif
}
static void QC_GameDir(qcvm_t *vm)
{
#ifdef KMQUAKE2_ENGINE_MOD
qcvm_return_string(vm, gi.FS_GameDir());
#else
const cvar_t *game_var = gi.cvar("game", "", 0);
qcvm_return_string(vm, qcvm_temp_format(vm, "./%s", *game_var->string ? game_var->string : BASEDIR));
#endif
}
static void QC_SaveGameDir(qcvm_t *vm)
{
#ifdef KMQUAKE2_ENGINE_MOD
qcvm_return_string(vm, gi.FS_SaveGameDir());
#else
QC_GameDir(vm);
#endif
}
typedef struct
{
char **entries;
int32_t num;
} qc_file_list_t;
static void QC_file_list_get(qcvm_t *vm)
{
const qc_file_list_t *list = qcvm_argv_handle(qc_file_list_t, vm, 0);
const int32_t index = qcvm_argv_int32(vm, 1);
if (index < 0 || index >= list->num)
qcvm_error(vm, "overrun file list");
qcvm_return_string(vm, list->entries[index]);
}
static void QC_file_list_length(qcvm_t *vm)
{
const qc_file_list_t *list = qcvm_argv_handle(qc_file_list_t, vm, 0);
qcvm_return_int32(vm, list->num);
}
static void QC_file_list_free(qcvm_t *vm, void *ptr)
{
qc_file_list_t *list = (qc_file_list_t *)ptr;
#ifdef KMQUAKE2_ENGINE_MOD
gi.FreeFileList(list->entries, list->num);
#else
for (int32_t i = 0; i < list->num; i++)
qcvm_mem_free(vm, list->entries[i]);
#endif
qcvm_mem_free(vm, list);
}
static qcvm_handle_descriptor_t file_list_descriptor =
{
.free = QC_file_list_free
};
static void QC_GetFileList(qcvm_t *vm)
{
const char *path = qcvm_argv_string(vm, 0);
const char *ext = qcvm_argv_string(vm, 1);
qc_file_list_t *list = (qc_file_list_t *)qcvm_alloc(vm, sizeof(qc_file_list_t));
#ifdef KMQUAKE2_ENGINE_MOD
list->entries = gi.GetFileList(path, ext, &list->num);
#else
list->entries = qcvm_get_file_list(vm, path, ext, &list->num);
#endif
qcvm_return_handle(vm, list, &file_list_descriptor);
}
void qcvm_init_file_builtins(qcvm_t *vm)
{
qcvm_register_builtin(LoadFile);
qcvm_register_builtin(OpenFile);
qcvm_register_builtin(OpenCompressedFile);
qcvm_register_builtin(FRead);
qcvm_register_builtin(FWrite);
qcvm_register_builtin(FReadString);
//qcvm_register_builtin(FWriteString);
qcvm_register_builtin(CreatePath);
qcvm_register_builtin(GameDir);
qcvm_register_builtin(SaveGameDir);
qcvm_register_builtin(GetFileList);
qcvm_register_builtin(file_list_get);
qcvm_register_builtin(file_list_length);
}