-
Notifications
You must be signed in to change notification settings - Fork 1
/
file_commands.cpp
493 lines (353 loc) · 11.2 KB
/
file_commands.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
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
// -----------------------------------------------------------------
// FILE OPERATIONS
// -----------------------------------------------------------------
case C_FILE_EXISTS:
{ // Return 1 if name exists, else 0.
if(argument_num < 3) {
QWrite("ERROR: Not enough parameters");
break;
}
if(fdbExists(String_trim_quotes(argument[2])))
QWrite("1");
else
QWrite("-1");
}
break;
case C_FILE_READ:
{ // Read val from file fwatcher/mdb/name
if(argument_num < 4) {
QWrite("ERROR: Not enough parameters");
break;
}
global.option_error_output |= OPTION_ERROR_ARRAY_LOCAL;
QWrite(fdbGet(String_trim_quotes(argument[2]), String_trim_quotes(argument[3])));
}
break;
case C_FILE_WRITE:
{ // Write var=val to file fwatcher/mdb/name
if(argument_num < 5) {
QWrite("ERROR: Not enough parameters");
break;
}
global.option_error_output |= OPTION_ERROR_ARRAY_LOCAL;
if(fdbPut(String_trim_quotes(argument[2]), String_trim_quotes(argument[3]), argument[4].text, false))
QWrite("1");
else
QWrite("-1");
}
break;
case C_FILE_QWRITE:
{ // Write var=val to file fwatcher/mdb/name without checking if it already exists
if(argument_num < 5) {
QWrite("ERROR: Not enough parameters");
break;
}
if(fdbPutQ(String_trim_quotes(argument[2]), String_trim_quotes(argument[3]), argument[4].text))
QWrite("1");
else
QWrite("-1");
}
break;
case C_FILE_AWRITE:
{ // Append val to var in file fwatcher/mdb/name
if(argument_num < 5) {
QWrite("ERROR: Not enough parameters");
break;
}
global.option_error_output |= OPTION_ERROR_ARRAY_LOCAL;
if(fdbPut(String_trim_quotes(argument[2]), String_trim_quotes(argument[3]), argument[4].text, true))
QWrite("1");
else
QWrite("-1");
}
break;
case C_FILE_VARS:
{ // Return array of vars in file fwatcher/mdb/name
if(argument_num < 3) {
QWrite("ERROR: Not enough parameters");
break;
}
char *res = fdbVars(String_trim_quotes(argument[2]));
QWrite(res);
delete[] res;
}
break;
case C_FILE_READVARS:
{ // Return all vars from file fwatcher/mdb/name
if(argument_num < 3) {
QWrite("ERROR: Not enough parameters");
break;
}
char *res = fdbReadvars(String_trim_quotes(argument[2]));
QWrite(res);
delete[] res;
}
break;
case C_FILE_REMOVE:
{ // Remove a val from file fwatcher/mdb/name
if(argument_num < 4) {
QWrite("ERROR: Not enough parameters");
break;
}
if(fdbRemove(String_trim_quotes(argument[2]), String_trim_quotes(argument[3])))
QWrite("1");
else
QWrite("-1");
}
break;
case C_FILE_DELETE:
{ // Remove file fwatcher/mdb/name
if(argument_num < 3) {
QWrite("ERROR: Not enough parameters");
break;
}
if(fdbDelete(String_trim_quotes(argument[2])))
QWrite("1");
else
QWrite("-1");
}
break;
case C_FILE_WGET:
{ // Get a http/ftp file with wget
// Execute wget, outputing to a named pipe
// Not very clean but it works...
if(argument_num < 3) {
QWrite("ERROR: Not enough parameters");
break;
}
if((GetTickCount() - global.lastWget) < WGET_MINWAIT) {
// Return -1 if user is calling wget too often
QWrite("-1");
break;
}
// wget should always return an empty file if an error occurs so no handling should be required for it
// TODO: maybe should return some sort of error message for OFP...
HANDLE pipe;
if(global.nomap)
// Create a named pipe for wget to write into
pipe = CreateNamedPipe(file.filename, PIPE_ACCESS_DUPLEX, PIPE_TYPE_BYTE | PIPE_READMODE_BYTE | PIPE_WAIT,
PIPE_UNLIMITED_INSTANCES, PIPEBUFSIZE, PIPEBUFSIZE, NMPWAIT_USE_DEFAULT_WAIT, NULL);
char *cline = new char[256+strlen(file.filename)+argument[2].length];
sprintf(cline, "-q --tries=1 --output-document=\"%s\" --timeout=3 --user-agent=fwatch/%.2f %s", file.filename, SCRIPT_VERSION, argument[2].text);
// Win23 API bloat
STARTUPINFO si;
PROCESS_INFORMATION pi;
ZeroMemory(&si, sizeof(si));
si.cb = sizeof(si);
si.dwFlags = STARTF_USESHOWWINDOW;
si.wShowWindow = SW_HIDE;
ZeroMemory(&pi, sizeof(pi));
// Execute wget
CreateProcess("fwatch/data/wget.exe", cline, NULL, NULL, TRUE, HIGH_PRIORITY_CLASS, NULL, NULL, &si, &pi);
delete[] cline;
global.lastWget = GetTickCount();
if(global.nomap) {
// Wait for wget to do its thing
char *buf = new char[PIPEBUFSIZE+1];
if(!buf)
break;
DWORD st, foo = 1, foo2;
do {
// Check exit code
GetExitCodeProcess(pi.hProcess, &st);
Sleep(5); // Lets not eat all cpu cycles
// Read named pipe and output to the out pipe for OFP
ReadFile(pipe, buf, PIPEBUFSIZE, &foo, NULL);
//buf[foo] = 0x00;
if(foo)
WriteFile(global.out, buf, foo, &foo2, NULL);
// Wait until the process has exited and no more data in pipe
} while(st == STILL_ACTIVE || foo);
WriteFile(global.out, "\0", 1, &foo2, NULL);
CloseHandle(pipe);
delete[] buf;
} else {
DWORD st;
do {
// Check exit code
GetExitCodeProcess(pi.hProcess, &st);
Sleep(10); // Lets not eat all cpu cycles
// Wait until the process has exited
} while(st == STILL_ACTIVE);
}
}
break;
case C_FILE_DXDLL:
{ // Check if dxdll is in OFP folder
FILE *f = fopen("d3d8.dll", "r");
if (f) {
fclose(f);
QWrite("true");
} else
QWrite("false");
}
break;
case C_FILE_READ2:
{ // Read variable from selected file (any path)
if (argument_num < 4) {
QWrite(":file read2 ERROR - not enough parameters");
break;
}
global.option_error_output |= OPTION_ERROR_ARRAY_LOCAL;
fdbGet2(String_trim_quotes(argument[2]), String_trim_quotes(argument[3]));
}
break;
case C_FILE_MODLIST:
{ // Return list of modfolders in the game folder
int base = 0;
int pointer = 0;
char username[30] = "";
switch(global_exe_version[global.exe_index]) {
case VER_196 : base=0x7DD184; break;
case VER_199 : base=0x7CC144; break;
case VER_201 : base=global.exe_address+0x714C10; break;
}
if (base) {
ReadProcessMemory(phandle, (LPVOID)base, &pointer, 4, &stBytes);
ReadProcessMemory(phandle, (LPVOID)(pointer+0x8), &username, 30, &stBytes);
}
WIN32_FIND_DATA fd;
HANDLE hFind = INVALID_HANDLE_VALUE;
hFind = FindFirstFile("*", &fd);
if (hFind != INVALID_HANDLE_VALUE) {
StringDynamic ModNames, ModID, ModCfg;
StringDynamic_init(ModNames);
StringDynamic_init(ModID);
StringDynamic_init(ModCfg);
char sub_folder[][16] = {
"addons",
"bin",
"campaigns",
"dta",
"worlds",
"Missions",
"MPMissions",
"Templates",
"SPTemplates",
"MissionsUsers",
"MPMissionsUsers"
};
int sub_folder_num = sizeof(sub_folder) / sizeof(sub_folder[0]);
do {
if (strcmp(fd.cFileName,".")==0 || strcmp(fd.cFileName,"..")==0)
continue;
// skip non-folders and "Res" folder
if ((fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)==0 || strcmpi("Res",fd.cFileName)==0)
continue;
char path2[MAX_PATH] = "";
// Check if current directory contains at least one of four required sub-folders
for (int i=0; i<sub_folder_num; i++) {
sprintf(path2, "%s\\%s", fd.cFileName, sub_folder[i]);
DWORD attributes = GetFileAttributes(path2);
if (attributes != -1 && attributes & FILE_ATTRIBUTE_DIRECTORY) {
StringDynamic_append(ModNames, "]+[\"");
StringDynamic_appendq(ModNames, fd.cFileName);
StringDynamic_append(ModNames, "\"");
StringDynamic_append(ModID, "]+[{");
StringDynamic_append(ModCfg, "]+[[");
sprintf(path2, "%s\\__gs_id", fd.cFileName);
FILE *f = fopen(path2, "r");
if (f) {
char data_buffer[256] = "";
fread(data_buffer, sizeof(char), 255, f);
String data = {data_buffer, strlen(data_buffer)};
String item;
int index = 0;
size_t data_pos = 0;
while ((item = String_tokenize(data,";",data_pos,OPTION_NONE)).length > 0) {
switch (index) {
case 0 : StringDynamic_appends(ModID, item); break;
case 3 : StringDynamic_appendf(ModCfg, "{%s},", item.text); break;
case 4 : {
if (strcmpi(item.text,"0") == 0)
StringDynamic_append(ModCfg, "false");
else
if (strcmpi(item.text,"1") == 0)
StringDynamic_append(ModCfg, "true");
else
StringDynamic_appendf(ModCfg, "%s", item.text); break;
} break;
default : StringDynamic_appendf(ModCfg, "%s,", item.text); break;
}
index++;
}
fclose(f);
} else
StringDynamic_append(ModCfg, "0,[],{},0");
StringDynamic_append(ModID, "}");
StringDynamic_append(ModCfg, "]");
break;
}
}
} while (FindNextFile(hFind, &fd));
FindClose(hFind);
double bytes = 0;
char custom_filename[256] = "";
char path_to_user[256] = "";
// Check if profile is using a custom face
sprintf(path_to_user, "Users\\%s\\UserInfo.cfg", username);
FILE *f = fopen(path_to_user,"r");
if (f) {
fseek(f, 0, SEEK_END);
int fsize = ftell(f);
fseek(f, 0, SEEK_SET);
char *settings = new char[fsize+1];
int result = fread(settings, 1, fsize, f);
settings[result] = '\0';
// Check custom face file size
if (strstr(settings, "face=\"Custom\"")) {
sprintf(path_to_user, "Users\\%s\\face.paa", username);
hFind = FindFirstFile(path_to_user, &fd);
if (hFind != INVALID_HANDLE_VALUE) {
if (fd.nFileSizeLow > bytes && fd.nFileSizeLow <= 102400) {
strncpy(custom_filename, fd.cFileName, 255);
bytes = fd.nFileSizeLow;
}
FindClose(hFind);
} else {
sprintf(path_to_user, "Users\\%s\\face.jpg", username);
hFind = FindFirstFile(path_to_user, &fd);
if (hFind != INVALID_HANDLE_VALUE)
if (fd.nFileSizeLow > bytes && fd.nFileSizeLow <= 102400) {
strncpy(custom_filename, fd.cFileName, 255);
bytes = fd.nFileSizeLow;
}
FindClose(hFind);
}
}
delete[] settings;
fclose(f);
}
// Check custom sounds file sizes
sprintf(path_to_user, "Users\\%s\\sounds\\*.*", username);
hFind = FindFirstFile(path_to_user, &fd);
if (hFind != INVALID_HANDLE_VALUE) {
do {
if (fd.cFileName[0] == '.')
continue;
if (fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
continue;
if (fd.nFileSizeLow > bytes && fd.nFileSizeLow <= 51200) {
bytes = fd.nFileSizeLow;
strncpy(custom_filename, fd.cFileName, 255);
}
} while (FindNextFile(hFind, &fd) != 0);
FindClose(hFind);
}
// Output result
QWrite_err(FWERROR_NONE, 0);
QWritef("[%s],[%s],[%s],\"%s", ModNames.text, ModID.text, ModCfg.text, custom_filename);
FileSize size = DivideBytes(bytes);
unsigned int hash = FNV_BASIS;
hash = fnv1a_hash(hash, ModCfg.text, ModCfg.length, OPTION_NONE);
QWritef("\",[%f,%f,%f],\"%u\",\"%s\"]", size.bytes, size.kilobytes, size.megabytes, hash, username);
StringDynamic_end(ModNames);
StringDynamic_end(ModID);
StringDynamic_end(ModCfg);
} else {
QWrite_err(FWERROR_WINAPI, 2, GetLastError(), username);
QWrite("[],[]]");
return;
}
}
break;