forked from lrusso/Emulatrix
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Emulatrix_GameBoy.htm
executable file
·686 lines (606 loc) · 30.3 KB
/
Emulatrix_GameBoy.htm
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
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Emulatrix</title>
<link rel="stylesheet" href="Emulatrix.css">
</head>
<body>
<script>
var container_width;
var container_height;
var download_running = false;
var download_Checker;
var download_Filename;
var download_LastSize = -1;
var download_LastSizeRepeated = 0;
var upload_running = false;
var upload_Checker;
var upload_OrigSize = -1;
var upload_OrigSizeRepeated = 0;
var settings_file = "";
var settings_Checker;
function loadRomIntoVD()
{
// CREATING THE ROM FILE IN THE VIRTUAL DRIVE
var dataView=new Uint8Array(parent.ROMDATA);
FS.createDataFile("/","game.gb",dataView,true,false);
// CREATING THE FOLDERS IN THE VIRTUAL DRIVE IN ORDER TO SAVE THE CONFIG FILE
FS.createFolder("/home/web_user", "retroarch", true, true);
FS.createFolder("/home/web_user/retroarch", "userdata", true, true);
// SETTING THE VARIABLE FOR EMPTY VALUE IN THE CONFIG FILE. USING RETROARCH WEB IN MAC HAS A BUG THAT
// ANY NULL KEY IS FIRED AS THE COMMAND KEY. THAT'S WHY IS SCROLL_BACK INSTEAD OF NUL. THERE IS
// ANOTHER BUG IN MAC WITH THE COMMAND KEY, THAT IS FIRED AS THE TURBO BUTTON, NO MATTER THE CONFIG.
var emptyValue = "scroll_lock";
// SETTING THE DEFAULT FOLDER WHEN BROWSING THE VIRTUAL DRIVE
settings_file += "rgui_browser_directory = /\n";
// SETTING THE KEY FOR THE START AND SELECT BUTTON
settings_file += "input_player1_start = s\n";
settings_file += "input_player1_start_btn = s\n";
settings_file += "input_player1_select = a\n";
settings_file += "input_player1_select_btn = a\n";
// SETTING THE KEYS FOR THE BUTTONS A, B
settings_file += "input_player1_a = x\n";
settings_file += "input_player1_b = z\n";
// SETTING THE KEY FOR SOUND TOGGLE
settings_file += "input_audio_mute = f9\n";
// SETTING THE KEY FOR RELOADING THE GAME
settings_file += "input_reset = f10\n";
// SETTING THE KEYS FOR UNWANTED FUNCTIONS
settings_file += "input_toggle_fast_forward = " + emptyValue + "\n";
settings_file += "input_hold_fast_forward = " + emptyValue + "\n";
settings_file += "input_toggle_slowmotion = " + emptyValue + "\n";
settings_file += "input_hold_slowmotion = " + emptyValue + "\n";
settings_file += "input_save_state = " + emptyValue + "\n";
settings_file += "input_load_state = " + emptyValue + "\n";
settings_file += "input_toggle_fullscreen = " + emptyValue + "\n";
settings_file += "input_exit_emulator = " + emptyValue + "\n";
settings_file += "input_state_slot_increase = " + emptyValue + "\n";
settings_file += "input_state_slot_decrease = " + emptyValue + "\n";
settings_file += "input_rewind = " + emptyValue + "\n";
settings_file += "input_movie_record_toggle = " + emptyValue + "\n";
settings_file += "input_pause_toggle = " + emptyValue + "\n";
settings_file += "input_frame_advance = " + emptyValue + "\n";
settings_file += "input_shader_next = " + emptyValue + "\n";
settings_file += "input_shader_prev = " + emptyValue + "\n";
settings_file += "input_cheat_index_plus = " + emptyValue + "\n";
settings_file += "input_cheat_index_minus = " + emptyValue + "\n";
settings_file += "input_cheat_toggle = " + emptyValue + "\n";
settings_file += "input_screenshot = " + emptyValue + "\n";
settings_file += "input_osk_toggle = " + emptyValue + "\n";
settings_file += "input_netplay_game_watch = " + emptyValue + "\n";
//settings_file += "input_enable_hotkey = " + emptyValue + "\n";
settings_file += "input_volume_up = " + emptyValue + "\n";
settings_file += "input_volume_down = " + emptyValue + "\n";
settings_file += "input_overlay_next = " + emptyValue + "\n";
settings_file += "input_disk_eject_toggle = " + emptyValue + "\n";
settings_file += "input_disk_next = " + emptyValue + "\n";
settings_file += "input_disk_prev = " + emptyValue + "\n";
settings_file += "input_grab_mouse_toggle = " + emptyValue + "\n";
settings_file += "input_game_focus_toggle = " + emptyValue + "\n";
settings_file += "input_menu_toggle = " + emptyValue + "\n";
settings_file += "input_recording_toggle = " + emptyValue + "\n";
settings_file += "input_streaming_toggle = " + emptyValue + "\n";
// PLAYER 1
settings_file += "input_player1_x = " + emptyValue + "\n";
settings_file += "input_player1_y = " + emptyValue + "\n";
settings_file += "input_player1_l = " + emptyValue + "\n";
settings_file += "input_player1_l2 = " + emptyValue + "\n";
settings_file += "input_player1_l3 = " + emptyValue + "\n";
settings_file += "input_player1_r = " + emptyValue + "\n";
settings_file += "input_player1_r2 = " + emptyValue + "\n";
settings_file += "input_player1_r3 = " + emptyValue + "\n";
settings_file += "input_player1_l_x_plus = " + emptyValue + "\n";
settings_file += "input_player1_l_x_minus = " + emptyValue + "\n";
settings_file += "input_player1_l_y_plus = " + emptyValue + "\n";
settings_file += "input_player1_l_y_minus = " + emptyValue + "\n";
settings_file += "input_player1_r_x_plus = " + emptyValue + "\n";
settings_file += "input_player1_r_x_minus = " + emptyValue + "\n";
settings_file += "input_player1_r_y_plus = " + emptyValue + "\n";
settings_file += "input_player1_r_y_minus = " + emptyValue + "\n";
settings_file += "input_player1_gun_trigger = " + emptyValue + "\n";
settings_file += "input_player1_gun_offscreen_shot = " + emptyValue + "\n";
settings_file += "input_player1_gun_aux_a = " + emptyValue + "\n";
settings_file += "input_player1_gun_aux_b = " + emptyValue + "\n";
settings_file += "input_player1_gun_aux_c = " + emptyValue + "\n";
settings_file += "input_player1_gun_start = " + emptyValue + "\n";
settings_file += "input_player1_gun_select = " + emptyValue + "\n";
settings_file += "input_player1_gun_dpad_up = " + emptyValue + "\n";
settings_file += "input_player1_gun_dpad_down = " + emptyValue + "\n";
settings_file += "input_player1_gun_dpad_left = " + emptyValue + "\n";
settings_file += "input_player1_gun_dpad_right = " + emptyValue + "\n";
settings_file += "input_player1_turbo = " + emptyValue + "\n";
// PLAYER 2
settings_file += "input_player2_up = " + emptyValue + "\n";
settings_file += "input_player2_down = " + emptyValue + "\n";
settings_file += "input_player2_left = " + emptyValue + "\n";
settings_file += "input_player2_right = " + emptyValue + "\n";
settings_file += "input_player2_start = " + emptyValue + "\n";
settings_file += "input_player2_select = " + emptyValue + "\n";
settings_file += "input_player2_a = " + emptyValue + "\n";
settings_file += "input_player2_b = " + emptyValue + "\n";
settings_file += "input_player2_x = " + emptyValue + "\n";
settings_file += "input_player2_y = " + emptyValue + "\n";
settings_file += "input_player2_l = " + emptyValue + "\n";
settings_file += "input_player2_l2 = " + emptyValue + "\n";
settings_file += "input_player2_l3 = " + emptyValue + "\n";
settings_file += "input_player2_r = " + emptyValue + "\n";
settings_file += "input_player2_r2 = " + emptyValue + "\n";
settings_file += "input_player2_r3 = " + emptyValue + "\n";
settings_file += "input_player2_l_x_plus = " + emptyValue + "\n";
settings_file += "input_player2_l_x_minus = " + emptyValue + "\n";
settings_file += "input_player2_l_y_plus = " + emptyValue + "\n";
settings_file += "input_player2_l_y_minus = " + emptyValue + "\n";
settings_file += "input_player2_r_x_plus = " + emptyValue + "\n";
settings_file += "input_player2_r_x_minus = " + emptyValue + "\n";
settings_file += "input_player2_r_y_plus = " + emptyValue + "\n";
settings_file += "input_player2_r_y_minus = " + emptyValue + "\n";
settings_file += "input_player2_gun_trigger = " + emptyValue + "\n";
settings_file += "input_player2_gun_offscreen_shot = " + emptyValue + "\n";
settings_file += "input_player2_gun_aux_a = " + emptyValue + "\n";
settings_file += "input_player2_gun_aux_b = " + emptyValue + "\n";
settings_file += "input_player2_gun_aux_c = " + emptyValue + "\n";
settings_file += "input_player2_gun_start = " + emptyValue + "\n";
settings_file += "input_player2_gun_select = " + emptyValue + "\n";
settings_file += "input_player2_gun_dpad_up = " + emptyValue + "\n";
settings_file += "input_player2_gun_dpad_down = " + emptyValue + "\n";
settings_file += "input_player2_gun_dpad_left = " + emptyValue + "\n";
settings_file += "input_player2_gun_dpad_right = " + emptyValue + "\n";
settings_file += "input_player2_turbo = " + emptyValue + "\n";
// PLAYER 3
settings_file += "input_player3_up = " + emptyValue + "\n";
settings_file += "input_player3_down = " + emptyValue + "\n";
settings_file += "input_player3_left = " + emptyValue + "\n";
settings_file += "input_player3_right = " + emptyValue + "\n";
settings_file += "input_player3_start = " + emptyValue + "\n";
settings_file += "input_player3_select = " + emptyValue + "\n";
settings_file += "input_player3_a = " + emptyValue + "\n";
settings_file += "input_player3_b = " + emptyValue + "\n";
settings_file += "input_player3_x = " + emptyValue + "\n";
settings_file += "input_player3_y = " + emptyValue + "\n";
settings_file += "input_player3_l = " + emptyValue + "\n";
settings_file += "input_player3_l2 = " + emptyValue + "\n";
settings_file += "input_player3_l3 = " + emptyValue + "\n";
settings_file += "input_player3_r = " + emptyValue + "\n";
settings_file += "input_player3_r2 = " + emptyValue + "\n";
settings_file += "input_player3_r3 = " + emptyValue + "\n";
settings_file += "input_player3_l_x_plus = " + emptyValue + "\n";
settings_file += "input_player3_l_x_minus = " + emptyValue + "\n";
settings_file += "input_player3_l_y_plus = " + emptyValue + "\n";
settings_file += "input_player3_l_y_minus = " + emptyValue + "\n";
settings_file += "input_player3_r_x_plus = " + emptyValue + "\n";
settings_file += "input_player3_r_x_minus = " + emptyValue + "\n";
settings_file += "input_player3_r_y_plus = " + emptyValue + "\n";
settings_file += "input_player3_r_y_minus = " + emptyValue + "\n";
settings_file += "input_player3_gun_trigger = " + emptyValue + "\n";
settings_file += "input_player3_gun_offscreen_shot = " + emptyValue + "\n";
settings_file += "input_player3_gun_aux_a = " + emptyValue + "\n";
settings_file += "input_player3_gun_aux_b = " + emptyValue + "\n";
settings_file += "input_player3_gun_aux_c = " + emptyValue + "\n";
settings_file += "input_player3_gun_start = " + emptyValue + "\n";
settings_file += "input_player3_gun_select = " + emptyValue + "\n";
settings_file += "input_player3_gun_dpad_up = " + emptyValue + "\n";
settings_file += "input_player3_gun_dpad_down = " + emptyValue + "\n";
settings_file += "input_player3_gun_dpad_left = " + emptyValue + "\n";
settings_file += "input_player3_gun_dpad_right = " + emptyValue + "\n";
settings_file += "input_player3_turbo = " + emptyValue + "\n";
// PLAYER 4
settings_file += "input_player4_up = " + emptyValue + "\n";
settings_file += "input_player4_down = " + emptyValue + "\n";
settings_file += "input_player4_left = " + emptyValue + "\n";
settings_file += "input_player4_right = " + emptyValue + "\n";
settings_file += "input_player4_start = " + emptyValue + "\n";
settings_file += "input_player4_select = " + emptyValue + "\n";
settings_file += "input_player4_a = " + emptyValue + "\n";
settings_file += "input_player4_b = " + emptyValue + "\n";
settings_file += "input_player4_x = " + emptyValue + "\n";
settings_file += "input_player4_y = " + emptyValue + "\n";
settings_file += "input_player4_l = " + emptyValue + "\n";
settings_file += "input_player4_l2 = " + emptyValue + "\n";
settings_file += "input_player4_l3 = " + emptyValue + "\n";
settings_file += "input_player4_r = " + emptyValue + "\n";
settings_file += "input_player4_r2 = " + emptyValue + "\n";
settings_file += "input_player4_r3 = " + emptyValue + "\n";
settings_file += "input_player4_l_x_plus = " + emptyValue + "\n";
settings_file += "input_player4_l_x_minus = " + emptyValue + "\n";
settings_file += "input_player4_l_y_plus = " + emptyValue + "\n";
settings_file += "input_player4_l_y_minus = " + emptyValue + "\n";
settings_file += "input_player4_r_x_plus = " + emptyValue + "\n";
settings_file += "input_player4_r_x_minus = " + emptyValue + "\n";
settings_file += "input_player4_r_y_plus = " + emptyValue + "\n";
settings_file += "input_player4_r_y_minus = " + emptyValue + "\n";
settings_file += "input_player4_gun_trigger = " + emptyValue + "\n";
settings_file += "input_player4_gun_offscreen_shot = " + emptyValue + "\n";
settings_file += "input_player4_gun_aux_a = " + emptyValue + "\n";
settings_file += "input_player4_gun_aux_b = " + emptyValue + "\n";
settings_file += "input_player4_gun_aux_c = " + emptyValue + "\n";
settings_file += "input_player4_gun_start = " + emptyValue + "\n";
settings_file += "input_player4_gun_select = " + emptyValue + "\n";
settings_file += "input_player4_gun_dpad_up = " + emptyValue + "\n";
settings_file += "input_player4_gun_dpad_down = " + emptyValue + "\n";
settings_file += "input_player4_gun_dpad_left = " + emptyValue + "\n";
settings_file += "input_player4_gun_dpad_right = " + emptyValue + "\n";
settings_file += "input_player4_turbo = " + emptyValue + "\n";
// PLAYER 5
settings_file += "input_player5_up = " + emptyValue + "\n";
settings_file += "input_player5_down = " + emptyValue + "\n";
settings_file += "input_player5_left = " + emptyValue + "\n";
settings_file += "input_player5_right = " + emptyValue + "\n";
settings_file += "input_player5_start = " + emptyValue + "\n";
settings_file += "input_player5_select = " + emptyValue + "\n";
settings_file += "input_player5_a = " + emptyValue + "\n";
settings_file += "input_player5_b = " + emptyValue + "\n";
settings_file += "input_player5_x = " + emptyValue + "\n";
settings_file += "input_player5_y = " + emptyValue + "\n";
settings_file += "input_player5_l = " + emptyValue + "\n";
settings_file += "input_player5_l2 = " + emptyValue + "\n";
settings_file += "input_player5_l3 = " + emptyValue + "\n";
settings_file += "input_player5_r = " + emptyValue + "\n";
settings_file += "input_player5_r2 = " + emptyValue + "\n";
settings_file += "input_player5_r3 = " + emptyValue + "\n";
settings_file += "input_player5_l_x_plus = " + emptyValue + "\n";
settings_file += "input_player5_l_x_minus = " + emptyValue + "\n";
settings_file += "input_player5_l_y_plus = " + emptyValue + "\n";
settings_file += "input_player5_l_y_minus = " + emptyValue + "\n";
settings_file += "input_player5_r_x_plus = " + emptyValue + "\n";
settings_file += "input_player5_r_x_minus = " + emptyValue + "\n";
settings_file += "input_player5_r_y_plus = " + emptyValue + "\n";
settings_file += "input_player5_r_y_minus = " + emptyValue + "\n";
settings_file += "input_player5_gun_trigger = " + emptyValue + "\n";
settings_file += "input_player5_gun_offscreen_shot = " + emptyValue + "\n";
settings_file += "input_player5_gun_aux_a = " + emptyValue + "\n";
settings_file += "input_player5_gun_aux_b = " + emptyValue + "\n";
settings_file += "input_player5_gun_aux_c = " + emptyValue + "\n";
settings_file += "input_player5_gun_start = " + emptyValue + "\n";
settings_file += "input_player5_gun_select = " + emptyValue + "\n";
settings_file += "input_player5_gun_dpad_up = " + emptyValue + "\n";
settings_file += "input_player5_gun_dpad_down = " + emptyValue + "\n";
settings_file += "input_player5_gun_dpad_left = " + emptyValue + "\n";
settings_file += "input_player5_gun_dpad_right = " + emptyValue + "\n";
settings_file += "input_player5_turbo = " + emptyValue + "\n";
// SETTING THE VIDEO CONFIGURATION
container_width = document.getElementById("container").offsetWidth;
container_height = document.getElementById("container").offsetHeight;
settings_file += "video_vsync = true\n";
settings_file += "video_scale = 1\n";
settings_file += "video_window_x = " + container_width + "\n";
settings_file += "video_window_y = " + container_height + "\n";
settings_file += "aspect_ratio_index = 23\n";
settings_file += "custom_viewport_width = " + container_width + "\n";
settings_file += "custom_viewport_height = " + container_height + "\n";
settings_file += "custom_viewport_x = 0\n";
settings_file += "custom_viewport_y = 0\n";
// SETTING THE AUDIO LATENCY
settings_file += "audio_latency = 128\n";
// HIDING THE NOTIFICATION MESSAGES
settings_file += "video_message_pos_x = -100\n";
settings_file += "video_message_pos_y = -100\n";
settings_file += "menu_enable_widgets = false\n";
// CREATING THE FILE WITH THE CONFIGURATION
FS.createDataFile("/home/web_user/retroarch/userdata", "retroarch.cfg", settings_file, true, true);
// WAITING A PRUDENTIAL TIME FOR THE RETROARCH.CFG FILE TO BE CREATED
settings_Checker = setInterval(checkSettingsFile, 1000);
}
function checkSettingsFile()
{
// SETTING THE VARIABLE TO KNOW IF THE EMULATOR MUST START
var mustStart = false;
try
{
// TRYING TO READ THE SETTINGS FILE
var myTempFile = FS.readFile("/home/web_user/retroarch/userdata/retroarch.cfg");
// CHECKING IF THE SETTINGS FILE HAS DATA
if (myTempFile.length==settings_file.length)
{
mustStart = true;
}
}
catch(err)
{
}
// CHECKING IF THE EMULATOR MUST START
if (mustStart==true)
{
// DISABLING THE SETTINGS FILE CHECKER
clearInterval(settings_Checker);
// STARTING THE EMULATOR
setTimeout(function()
{
startEmulator();
},1500);
}
}
function startEmulator()
{
// RUNNING THE EMULATOR WITH THE SELECTED ROM
Module.callMain(["-v","/game.gb"]);
// RESIZING THE CANVAS
document.getElementById("canvas").width = container_width;
document.getElementById("canvas").height = container_height;
setTimeout(function(){resizeEmulatorCanvas()},500);
setTimeout(function(){resizeEmulatorCanvas()},1000);
setTimeout(function(){resizeEmulatorCanvas()},1500);
}
function resizeEmulatorCanvas()
{
try
{
container_width = document.getElementById("container").offsetWidth;
container_height = document.getElementById("container").offsetHeight;
Module.setCanvasSize(container_width,container_height,true);
}
catch(err)
{
}
}
function toggleSound()
{
sendVirtualKey("keydown","F9")
setTimeout(function()
{
sendVirtualKey("keyup","F9");
}, 50);
}
function reloadROM()
{
sendVirtualKey("keydown","F10")
setTimeout(function()
{
sendVirtualKey("keyup","F10");
}, 50);
}
function downloadROMState()
{
try
{
// CHECKING IF THE DOWNLOADING AND UPLOADING PROCESS ARE NOT RUNNING
if (download_running==false && upload_running==false)
{
// SETTING THE DOWNLOADING PROCESS AS TRUE
download_running = true;
// SAVING THE STATE (TAKES A LOT OF SECONDS IN SOME CASES, EMSCRIPTED + LIBRETRO RELATED ISSUE)
Module._cmd_save_state();
// GETTING THE ROM FILENAME WITHOUT THE EXTENSION
download_Filename = parent.ROMNAME.replace(/\.[^/.]+$/, "");
// SETTING THE DEFAULT VALUES FOR THE VARIABLES THAT WILL CHECK THE DOWNLOADING PROCESS
download_LastSize = -1;
download_LastSizeRepeated = 0;
// RESTORING THE ORIGINAL LABEL TEXT
document.getElementsByClassName("gui_saving")[0].innerHTML = parent.STRING_SAVING;
// SHOWING THE 'SAVING, PLEASE WAIT...' LABEL
document.getElementsByClassName("gui_saving")[0].style.display = "block";
// IF RUNNING THE MOBILE APP, TELLING THE NAME THAT THE DOWNLOADED FILE WILL HAVE
console.log("FILENAME=" + download_Filename + ".state");
// STARTING THE TASK THAT WILL DOWNLOAD THE ROMSTATE AS A FILE
download_Checker = setInterval(download_CheckerCron, 1000);
}
}
catch(err)
{
}
}
function download_CheckerCron()
{
try
{
// TRYING TO READ THE ROMSTATE FILE
var myTempFile = FS.readFile("/home/web_user/retroarch/userdata/states/game.state");
// THE ROMSTATE FILE IS GOING TO BE WRITTEN WITH AN ASYNCHRONOUS TASK THAT WE CAN'T HANDLE IN ANY WAY.
// THIS IS THE REASON WHY WE NEED TO CHECK IF THE FILESIZE IS THE SAME AFTER SEVERAL CHECKINGS.
// CHECKING THE ROMSTATE FILE SIZE
if (myTempFile.length==download_LastSize)
{
// IF IT IS THE SAME, WE ADD 1 TO THE VARIABLE THAT WE USE FOR CHECKING THIS
download_LastSizeRepeated = download_LastSizeRepeated + 1;
}
else
{
// IF THE FILE SIZE IS NOT THE SAME, WE UPDATE A VARIABLE WITH THE LAST FILE SIZE THAT WE GOT
download_LastSize = myTempFile.length;
}
// IF THE FILE SIZE IS THE SAME AFTER SEVERAL CHECKINGS
if (download_LastSizeRepeated>parent.STATE_CHECK_TIMES)
{
// SETTING THE DEFAULT VALUES FOR THE VARIABLES THAT WE USED FOR CHECKING
download_LastSize = -1;
download_LastSizeRepeated = 0;
// DELETING THE ROMSTATE FILE FROM THE FS
FS.unlink("/home/web_user/retroarch/userdata/states/game.state");
// DOWNLOADING THE ROMSTATE AS A FILE
download_Blob(myTempFile, download_Filename + ".state", "application/octet-stream");
// UPDATING THE LABEL TEXT IN ORDER TO TELL THE USER THAT A FILE WAS DOWNLOADED
document.getElementsByClassName("gui_saving")[0].innerHTML = parent.STRING_SAVED;
// SETTING THE DOWNLOAD PROCESS AS FALSE
download_running = false;
setTimeout(function()
{
// CHECKING IF THE LABEL MUST BE HIDDEN
if (download_running==false)
{
// HIDING THE LABEL
document.getElementsByClassName("gui_saving")[0].style.display = "none";
// RESTORING THE ORIGINAL LABEL TEXT
document.getElementsByClassName("gui_saving")[0].innerHTML = parent.STRING_SAVING;
}
}, 3000);
// DISABLING THE DOWNLOADING TASK
clearInterval(download_Checker);
}
}
catch(err)
{
}
}
function download_Blob(data, fileName, mimeType)
{
var blob, url;
blob = new Blob([data], {type: mimeType});
url = window.URL.createObjectURL(blob);
download_URL(url, fileName);
}
function download_URL(data, fileName)
{
var a;
a = document.createElement("a");
a.href = data;
a.download = fileName;
document.body.appendChild(a);
a.style = "display: none";
a.click();
a.remove();
}
function uploadROMState()
{
// CHECKING IF THE UPLOADING AND DOWNLOADING PROCESS ARE NOT RUNNING
if (upload_running==false && download_running==false)
{
// HIDING THE 'SAVING, PLEASE WAIT...' LABEL
document.getElementsByClassName("gui_saving")[0].style.display = "none";
// ALLOWING THE USER TO SELECT THE ROMSTATE FILE FROM THE COMPUTER OR MOBILE DEVICE
document.getElementById("fileupload_uploader").click();
}
}
function upload_getFile(files)
{
try
{
// CHECKING THE FILE EXTENSION
var extension = files[0].name.split(".").pop().toLowerCase();
if (extension=="state")
{
// SETTING THE UPLOADING PROCESS AS TRUE
upload_running = true;
// READING THE SELECTED FILE
var filereader = new FileReader();
filereader.file_name = files[0].name;
filereader.onload = function()
{
// CHECKING IF THERE IS A PREVIOUS ROMSTATE FILE CREATED, IF SO, IT IS DELETED
try
{
var myTempFile = FS.readFile("/home/web_user/retroarch/userdata/states/game.state");
FS.unlink("/home/web_user/retroarch/userdata/states/game.state");
}
catch(err)
{
}
// CREATING THE ROMSTATE FILE
var dataView = new Uint8Array(this.result);
FS.createDataFile("/home/web_user/retroarch/userdata/states","game.state",dataView,true,true);
// SETTING THE DEFAULT VALUES FOR THE VARIABLES THAT WILL CHECK THE UPLOADING PROCESS
upload_OrigSize = dataView.length;
upload_OrigSizeRepeated = 0;
// CLEARING THE SELECTED FILE VALUE
document.getElementById("fileupload_uploader").value = null;
// SHOWING THE 'LOADING, PLEASE WAIT...' LABEL
document.getElementsByClassName("gui_loading")[0].style.display = "block";
// STARTING THE TASK THAT WILL UPLOAD THE ROMSTATE FILE
upload_Checker = setInterval(upload_CheckerCron, 1000);
};
filereader.readAsArrayBuffer(files[0]);
}
}
catch(err)
{
}
}
function upload_CheckerCron()
{
try
{
// TRYING TO READ THE ROMSTATE FILE
var myTempFile = FS.readFile("/home/web_user/retroarch/userdata/states/game.state");
// THE ROMSTATE FILE IS GOING TO BE WRITTEN WITH AN ASYNCHRONOUS TASK THAT WE CAN'T HANDLE IN ANY WAY.
// THIS IS THE REASON WHY WE NEED TO CHECK IF THE FILESIZE IS THE SAME AFTER SEVERAL CHECKINGS.
// CHECKING THE ROMSTATE FILE SIZE
if (myTempFile.length==upload_OrigSize)
{
// IF IT IS THE SAME, WE ADD 1 TO THE VARIABLE THAT WE USE FOR CHECKING THIS
upload_OrigSizeRepeated = upload_OrigSizeRepeated + 1;
}
// IF THE FILE SIZE IS THE SAME AFTER SEVERAL CHECKINGS
if (upload_OrigSizeRepeated>parent.STATE_CHECK_TIMES)
{
// SETTING THE DEFAULT VALUES FOR THE VARIABLES THAT WE USED FOR CHECKING
upload_OrigSize = -1;
upload_OrigSizeRepeated = 0;
// SETTING THE UPLOAD PROCESS AS FALSE
upload_running = false;
// LOADING THE STATE (TAKES A LOT OF SECONDS IN SOME CASES, EMSCRIPTED + LIBRETRO RELATED ISSUE)
Module._cmd_load_state();
// HIDING THE 'LOADING, PLEASE WAIT...' LABEL
document.getElementsByClassName("gui_loading")[0].style.display = "none";
// DISABLING THE UPLOADING TASK
clearInterval(upload_Checker);
}
}
catch(err)
{
}
}
window.addEventListener("resize", function()
{
// RESIZING CANVAS IF THE WINDOW HAS BEEN RESIZED
resizeEmulatorCanvas();
});
window.addEventListener("load", function()
{
// SHOWING THE CANVAS CONTAINER
document.getElementById("container").style.display="block";
// STARTING THE FILE SYSTEM
setTimeout(function(){startFileSystem()},1000);
// LOADING THE ROM
setTimeout(function(){loadRomIntoVD()},2000);
// SETTING FUNCTIONS THAT WILL BE CALLED WHEN THE USER CLICKS, DOUBLE CLICKS OR MOVES THE MOUSE IN ORDER TO SHOW THE SYSTEM ICONS
document.addEventListener("click", parent.goBackButtonResetIncrement, false);
document.addEventListener("dblclick", parent.goBackButtonResetIncrement, false);
document.addEventListener("mousemove", parent.goBackButtonResetIncrement, false);
// CHECKING IF IT IS A MOBILE DEVICE
if (isMobileDevice()==true)
{
// SHOWING THE VIRTUAL BUTTONS AND ASSIGNING TOUCHING EVENTS TO EACH ONE OF THEM
document.getElementsByClassName("gui_gameboy_keyselect")[0].style.display = "block";
document.getElementsByClassName("gui_gameboy_keyselect")[0].addEventListener("touchstart",function(event){sendVirtualKey("keydown","KeyA")});
document.getElementsByClassName("gui_gameboy_keyselect")[0].addEventListener("touchend",function(event){sendVirtualKey("keyup","KeyA")});
document.getElementsByClassName("gui_gameboy_keystart")[0].style.display = "block";
document.getElementsByClassName("gui_gameboy_keystart")[0].addEventListener("touchstart",function(event){sendVirtualKey("keydown","KeyS")});
document.getElementsByClassName("gui_gameboy_keystart")[0].addEventListener("touchend",function(event){sendVirtualKey("keyup","KeyS")});
document.getElementsByClassName("gui_gameboy_keya")[0].style.display = "block";
document.getElementsByClassName("gui_gameboy_keya")[0].addEventListener("touchstart",function(event){sendVirtualKey("keydown","KeyX")});
document.getElementsByClassName("gui_gameboy_keya")[0].addEventListener("touchend",function(event){sendVirtualKey("keyup","KeyX")});
document.getElementsByClassName("gui_gameboy_keyb")[0].style.display = "block";
document.getElementsByClassName("gui_gameboy_keyb")[0].addEventListener("touchstart",function(event){sendVirtualKey("keydown","KeyZ")});
document.getElementsByClassName("gui_gameboy_keyb")[0].addEventListener("touchend",function(event){sendVirtualKey("keyup","KeyZ")});
// SHOWING THE VIRTUAL JOYSTICK
document.getElementsByClassName("gui_joystick")[0].style.display = "block";
showVirtualJoystick();
}
});
window.onerror = function (msg, url, lineNo, columnNo, error)
{
// SOMETIMES THE LIBRETRO TAKES TOO MUCH TIME TO LOAD AND MAY THROUGH AN ERROR.
// THE CODE BELOW GIVES ANOTHER TRY TO RUN THE EMULATOR WITH THE SELECTED ROM.
setTimeout(function()
{
// RUNS THE EMULATOR WITH THE SELECTED ROM
Module.callMain(["-v","/game.gb"]);
// RESIZES THE CANVAS
document.getElementById("canvas").width = container_width;
document.getElementById("canvas").height = container_height;
}, 5000);
return true;
}
</script>
<div class="gui_pleasewait_title"><script>document.write(parent.STRING_STARTINGEMULATOR);</script></div><div class="pleasewait"><div class="lds-spinner"><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div></div></div>
<div id="container"><canvas id="canvas" oncontextmenu="event.preventDefault()"></canvas></div>
<div class="gui_joystick" id="joyDiv"></div>
<div class="gui_gameboy_keyselect" oncontextmenu="return false;">SELECT</div>
<div class="gui_gameboy_keystart" oncontextmenu="return false;">START</div>
<div class="gui_gameboy_keyb" oncontextmenu="return false;">B</div>
<div class="gui_gameboy_keya" oncontextmenu="return false;">A</div>
<div class="gui_loading"><script>document.write(parent.STRING_LOADING);</script></div>
<div class="gui_saving"><script>document.write(parent.STRING_SAVING);</script></div>
<input type="file" class="fileupload_uploader" id="fileupload_uploader" onchange="upload_getFile(event.target.files);" />
<script src="Emulatrix_GameBoy.js"></script>
</body>
</html>