-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy patharchdep.cpp
More file actions
557 lines (481 loc) · 11.9 KB
/
Copy patharchdep.cpp
File metadata and controls
557 lines (481 loc) · 11.9 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
/*
YAPE - Yet Another Plus/4 Emulator
The program emulates the Commodore 264 family of 8 bit microcomputers
This program is free software, you are welcome to distribute it,
and/or modify it under certain conditions. For more information,
read 'Copying'.
(c) 2000, 2001, 2005, 2007 Attila Gr�z
(c) 2005 VENESZ Roland
*/
#include "archdep.h"
#include <cstdio>
#ifdef _WIN32
typedef struct IUnknown IUnknown;
#include <windows.h>
// fixes a missing export in SDL2 for VS 2015
#if _MSC_VER >= 1900
FILE _iob[] = { *stdin, *stdout, *stderr };
extern "C" FILE * __cdecl __iob_func(void)
{
return _iob;
}
#endif
#endif
// forward declarations
static void flipMaxFps(void *v);
unsigned int tick_50hz, tick_vsync, fps;
static unsigned int framesShown = 50;
static const unsigned int maxFpsValues[] = { 100, 60, 50, 25, 10 };
static unsigned int maxFps = 100;
static unsigned int maxFpsIndex = 0;
static unsigned int targetFps;
static unsigned int fpsInterval;
rvar_t archDepSettings[] = {
{ "Maximum framerate", "MaxFrameRate", flipMaxFps, &maxFps, RVAR_INT, NULL },
{ "", "", NULL, NULL, RVAR_NULL, NULL }
};
static void flipMaxFps(void *v)
{
maxFpsIndex = (maxFpsIndex + 1) % (sizeof(maxFpsValues) / sizeof(maxFpsValues[0]));
maxFps = maxFpsValues[maxFpsIndex];
}
/* functions for Windows */
#if defined(_WIN32)
static HANDLE handle;
static WIN32_FIND_DATA rec;
static char temp[512];
static BOOL showDriveLetters = FALSE;
static DWORD driveBitFields;
static UINT currentDriveIndex;
static char currentDrive[MAX_PATH];
/* file management */
void ad_exit_drive_selector()
{
showDriveLetters = FALSE;
}
static int ad_get_next_drive()
{
do {
driveBitFields >>= 1;
currentDriveIndex++;
} while (!(driveBitFields & 1) && driveBitFields != 0);
if (driveBitFields) {
sprintf(currentDrive, "%c:/", 'A' + currentDriveIndex);
return 1;
} else {
return 0;
}
}
static void ad_get_first_drive()
{
currentDriveIndex = 0;
driveBitFields = GetLogicalDrives();
while (!(driveBitFields & 1) && driveBitFields != 0) {
driveBitFields >>= 1;
currentDriveIndex++;
}
sprintf(currentDrive, "%c:/", 'A' + currentDriveIndex);
}
int ad_get_curr_dir(char *pathstring)
{
return GetCurrentDirectory(MAX_PATH, pathstring);
}
int ad_set_curr_dir(char *path)
{
char origPath[MAX_PATH];
char cp[MAX_PATH];
GetCurrentDirectory(MAX_PATH, origPath);
BOOL retval = SetCurrentDirectory(path);
if (retval) {
if (!strcmp(path, "..")) {
GetCurrentDirectory(MAX_PATH, cp);
// root reached?
if (!strcmp(cp, origPath)) {
showDriveLetters = TRUE;
} else {
showDriveLetters = FALSE;
}
}
}
return retval;
}
int ad_find_first_file(const char *filefilter)
{
if (showDriveLetters) {
ad_get_first_drive();
return 1;
} else {
handle = FindFirstFile( filefilter, &rec);
if (handle != INVALID_HANDLE_VALUE) return 1;
return 0;
}
}
char *ad_return_current_filename(void)
{
if (showDriveLetters) {
return currentDrive;
} else {
return (char *) rec.cFileName;
}
}
UI_FileTypes ad_return_current_filetype(void)
{
if (showDriveLetters)
return FT_VOLUME;
else if (rec.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY )
return FT_DIR;
else
return FT_FILE;
}
unsigned int ad_get_current_filesize(void)
{
return (unsigned int) rec.nFileSizeLow;
}
int ad_find_next_file(void)
{
if (showDriveLetters) {
return ad_get_next_drive();
} else {
return FindNextFile( handle, &rec);
}
}
int ad_find_file_close(void)
{
if (showDriveLetters)
return 1;
return FindClose(handle);
}
int ad_makedirs(char *path)
{
strcpy(temp,path);
strcat(temp, "/yape");
CreateDirectory(temp, NULL);
return 1;
}
#endif /* end of Windows functions */
#if defined(_WIN32) || defined(__EMSCRIPTEN__)
static Uint64 timeelapsed;
static Uint64 nextFrameTime;
void ad_vsync_init(unsigned int targetfps)
{
timeelapsed = SDL_GetTicks64();
targetFps = targetfps;
fpsInterval = 1000 / targetFps;
if (targetfps > maxFps) {
maxFpsIndex = 0;
maxFps = maxFpsValues[maxFpsIndex];
}
nextFrameTime = timeelapsed + (1000 / maxFps);
}
bool ad_vsync(bool sync)
{
Uint64 time_limit = timeelapsed + fpsInterval;
timeelapsed = SDL_GetTicks64();
if (sync) {
if (time_limit > timeelapsed) {
Uint32 nr10ms = Uint32((time_limit - timeelapsed) / 10) * 10;
SDL_Delay(nr10ms);
timeelapsed = SDL_GetTicks64();
while (time_limit > timeelapsed) {
SDL_Delay(0);
timeelapsed = SDL_GetTicks64();
}
}
}
if (nextFrameTime > timeelapsed) {
return false;
} else {
nextFrameTime = timeelapsed + (1000 / maxFps);
framesShown++;
return true;
}
}
unsigned int ad_get_fps(unsigned int &framesDrawn)
{
static unsigned int speed = targetFps * 2;
static Uint64 g_TotElapsed = SDL_GetTicks64();
static unsigned int g_TotFrames = 0;
const unsigned int measureIntervalMsec = 2000 - (targetFps - 50) * 40;
Uint64 now = SDL_GetTicks64();
if (now - g_TotElapsed >= measureIntervalMsec) {
// compute FPS over the interval
speed = g_TotFrames + 1;
// reset interval
g_TotElapsed = now;
g_TotFrames = 0;
framesDrawn = framesShown * targetFps / 100;
framesShown = 0;
} else
g_TotFrames++;
return speed;
}
int ad_change_target_speed(int change)
{
fpsInterval += change;
if (fpsInterval <= 0) fpsInterval = 1;
else if (fpsInterval > 2000) fpsInterval = 2000;
return 2000 / fpsInterval;
}
#endif
#if !defined(_WIN32)
/* ---------- UNIX ---------- */
#include <stdio.h>
#include <unistd.h>
#include <glob.h>
#include <dirent.h>
#include <ctype.h>
#include <string.h>
#include <stdlib.h>
#include <signal.h>
#include <sys/stat.h>
#include <sys/types.h>
#define INVALID_HANDLE_VALUE 0x00
glob_t gl;
unsigned int gl_curr;
unsigned int gl_currsize;
char temp[512];
char sem_vsync, sem_fps;
void ad_exit_drive_selector()
{
//
}
int ad_get_curr_dir(char *pathstring, size_t size)
{
char *p = getcwd(pathstring, size);
return 1;
}
int ad_get_curr_dir(char *pathstring)
{
size_t size = 512;
getcwd(pathstring, size);
return !!pathstring;
}
int ad_set_curr_dir(char *path)
{
if (chdir(path) == 0) return 1;
return 0;
}
int ad_find_first_file(const char *filefilter)
{
// We will search in the current working directory.
if (glob(filefilter, 0, NULL, &gl) != 0) return 0;
gl_curr = 0; gl_currsize = 0;
return 1;
}
char *ad_return_current_filename(void)
{
// fprintf(stderr, "File: %s parsed\n", gl.gl_pathv[gl_curr]);
if (!gl.gl_pathc)
return 0;
return (char *) gl.gl_pathv[gl_curr];
}
UI_FileTypes ad_return_current_filetype(void)
{
// Probably too kludgy but I dunno Unix so who cares...
DIR *dirp;
if (gl.gl_pathv && (dirp = opendir(gl.gl_pathv[gl_curr])) != NULL) {
closedir(dirp);
return FT_DIR;
} else
return FT_FILE;
}
unsigned int ad_get_current_filesize(void)
{
struct stat buf;
// Size cache!
if (gl_currsize != 0) return gl_currsize;
// If the file size actually is 0, subsequent calls to this
// function will stat the file over and over again. I don't care.
if (stat(gl.gl_pathv[gl_curr], &buf) == 0) {
return (gl_currsize = (unsigned int) buf.st_size);
} else return 0;
}
int ad_find_next_file(void)
{
if (++gl_curr >= gl.gl_pathc) {
// No more matches: we don't need the glob any more.
//globfree(&gl);
return 0;
}
gl_currsize = 0;
return 1;
}
int ad_find_file_close(void)
{
return 1;
}
int ad_makedirs(char *path)
{
// Possible buffer overflow fixed.
strncpy(temp, path, 512);
if (strlen(temp) > 506) return 0;
strcat(temp, "/yape");
mkdir(temp, 0777);
return 1;
}
#endif
#if !defined(_WIN32) && !defined(__EMSCRIPTEN__)
void _ad_vsync_sigalrm_handler(int signal)
{
sem_vsync = 0x01;
// Refresh FPS every 3 seconds. To avoid race condition, do
// it in the next call if tick_vsync is locked.
if (++tick_50hz >= 3*20 && sem_fps != 0x00) {
fps = (int) (targetFps * ((double) tick_vsync / (double) tick_50hz));
tick_vsync = 0;
tick_50hz = 0;
}
}
void ad_vsync_init(unsigned int targetfps)
{
struct sigaction ac;
// Initialization
sem_vsync = 0x01;
sem_fps = 0x01;
tick_vsync = 0;
tick_50hz = 0;
fps = targetfps;
targetFps = targetfps;
ac.sa_handler = _ad_vsync_sigalrm_handler;
sigemptyset(&ac.sa_mask);
ac.sa_flags = 0;
if (sigaction(SIGALRM, &ac, NULL) == 0) ualarm(100, 1000000 / targetfps);
}
bool ad_vsync(bool sync)
{
if (sync) {
while (sem_vsync == 0x00) usleep(VSYNC_LATENCY);
sem_vsync = 0x00;
}
sem_fps = 0x00;
tick_vsync++;
sem_fps = 0x01;
return true;
}
unsigned int ad_get_fps(unsigned int &framesDrawn)
{
framesDrawn = fps;
return fps << 1;
}
int ad_change_target_speed(int change)
{
if (int(targetFps) - change > 0)
targetFps -= change;
return targetFps * 2;
}
#endif
#if defined(__EMSCRIPTEN__) || defined(ZIP_SUPPORT)
#pragma comment(lib, "zlibstat.lib")
#include "zlib/unzip.h"
bool zipOpen(const char *zipName, unsigned char **buffer, unsigned int &bufferSize, unsigned int &fileType)
{
static unsigned int storedFileSize = 0;
unzFile zipFile = unzOpen(zipName);
fileType = 0;
if (zipFile) {
if (unzGoToFirstFile(zipFile) == UNZ_OK) {
unz_file_info zipfileinfo;
char zippedName[512];
if (unzOpenCurrentFile(zipFile) == UNZ_OK) {
int result = unzGetCurrentFileInfo(zipFile, &zipfileinfo, zippedName, 512, NULL, 0, NULL, 0);
fprintf(stderr, "First file in ZIP: %s\n", zippedName);
unsigned int fsize = zipfileinfo.uncompressed_size;
char *ext = strrchr(zippedName, '.');
if (ext) {
if (fsize > storedFileSize) {
if (*buffer) {
delete[] *buffer;
*buffer = NULL;
}
*buffer = new unsigned char[fsize];
storedFileSize = fsize;
}
if (*buffer) {
if (!strcmp(ext, ".tap") || !strcmp(ext, ".TAP"))
fileType = 2;
else
fileType = 1;
bufferSize = fsize;
unzReadCurrentFile(zipFile, *buffer, fsize);
unzCloseCurrentFile(zipFile);
unzClose(zipFile);
}
return true;
}
}
}
}
return false;
}
#else
bool zipOpen(const char *zipName, unsigned char **buffer, unsigned int &bufferSize, unsigned int &fileType)
{
return false;
}
#endif
void unzipFiles(const char* zipFilename, const char* OutDir)
{
#ifdef __EMSCRIPTEN__
unzFile zipfile = unzOpen(zipFilename);
if (zipfile == NULL) {
fprintf(stderr, "Error opening ZIP file %s\n", zipFilename);
return;
}
// Get the total number of files in the ZIP archive
unz_global_info global_info;
if (unzGetGlobalInfo(zipfile, &global_info) != UNZ_OK) {
fprintf(stderr, "Error getting global info for %s\n", zipFilename);
unzClose(zipfile);
return;
}
// Loop through each file in the ZIP archive
for (unsigned int i = 0; i < global_info.number_entry; i++) {
// Get file info
unz_file_info file_info;
char filename_in_zip[256];
if (unzGetCurrentFileInfo(zipfile, &file_info, filename_in_zip, sizeof(filename_in_zip), NULL, 0, NULL, 0) != UNZ_OK) {
fprintf(stderr, "Error getting file info for entry %d\n", i);
unzClose(zipfile);
return;
}
// Construct the full output path for the file
char output_path[512];
snprintf(output_path, sizeof(output_path), "%s/%s", OutDir, filename_in_zip);
// Open the file for extraction
if (unzOpenCurrentFile(zipfile) != UNZ_OK) {
fprintf(stderr, "Error opening file %s\n", filename_in_zip);
unzClose(zipfile);
return;
}
// Create the directory if necessary
FILE* out_file = fopen(output_path, "wb");
if (out_file == NULL) {
fprintf(stderr, "Error creating file %s\n", output_path);
unzClose(zipfile);
return;
}
// Allocate buffer for reading the file content
unsigned char buffer[4096];
int bytes_read = 0;
// Extract the file content
while ((bytes_read = unzReadCurrentFile(zipfile, buffer, sizeof(buffer))) > 0) {
fwrite(buffer, 1, bytes_read, out_file);
}
if (bytes_read < 0) {
fprintf(stderr, "Error reading file %s\n", filename_in_zip);
}
fclose(out_file);
unzCloseCurrentFile(zipfile);
// Move to the next file in the ZIP archive
if (i < global_info.number_entry - 1) {
if (unzGoToNextFile(zipfile) != UNZ_OK) {
fprintf(stderr, "Error moving to next file in ZIP archive\n");
unzClose(zipfile);
return;
}
}
}
// Close the ZIP file
unzClose(zipfile);
#endif
}