-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
745 lines (604 loc) · 24.5 KB
/
index.js
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
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
import links from './links.js';
import { readSaveData } from './pmd-save.js?v=2';
import { KEY_INDICES, initSettingsView, isSettingsMenuOpen, keyboardMappings, loadKeyBindings } from './settings.js';
const CLEAN_US_SHA1 = '5fa96ca8d8dd6405d6cd2bad73ed68bc73a9d152';
const CLEAN_EU_SHA1 = 'c838a5adf1ed32d2da8454976e5b1a1aa189c139';
class UserError extends Error { }
class HttpStatusError extends Error {
constructor(message, statusCode) {
super(message);
this.statusCode = statusCode;
}
}
async function downloadPatch(url) {
console.log(`Downloading patch '${url}'...`)
let result;
try {
result = await fetch(url);
} catch (e) {
throw new Error(`Failed to download patch, please check your internet connection.`);
}
if (!result.ok) {
throw new HttpStatusError(`Failed to fetch patch '${url} (code ${result.status})'`, result.status);
}
const reader = result.body.getReader();
const contentLength = parseInt(result.headers.get('Content-Length'));
const contentLengthMb = contentLength / 1024 / 1024;
console.log(`Patch size: ${contentLengthMb.toFixed(2)} MB`);
const patch = new Uint8Array(contentLength);
let offset = 0;
while (true) {
const { done, value } = await reader.read();
if (done) break;
patch.set(value, offset);
offset += value.length;
const progress = offset / contentLength;
console.log(`Download progress: ${(progress * 100).toFixed(2)}% `);
}
return { patch };
}
const worker = new Worker('patch-worker.js');
let invocationId = 0;
function applyPatch(romBytes, patchBytes) {
console.log('Applying the patch...');
const currentInvocationId = ++invocationId;
return new Promise((resolve, reject) => {
worker.onmessage = function (e) {
const result = e.data;
const { invocationId: resultInvocationId, data } = result;
if (resultInvocationId === currentInvocationId) {
resolve(data);
}
};
worker.onerror = function (error) {
reject(error);
};
worker.postMessage({ invocationId: currentInvocationId, romBytes, patchBytes });
});
}
async function isRomClean(romSha1, romRegion) {
const expectedSha1 = getCleanSha1ForRegion(romRegion);
return expectedSha1 === romSha1;
}
async function cleanRom(rom, romRegion, romSha1) {
try {
const { patch } = await downloadPatch(`patches/${romRegion}/from/${romSha1.toUpperCase()}.xdelta`);
return await applyPatch(rom, patch);
}
catch (e) {
if (e instanceof HttpStatusError && e.statusCode == 404) {
// An unsupported dump was provided if no patch was found
throw new UserError(`The provided ROM is incompatible. Please try again with a clean ROM. (Checksum of the provided ROM: "${romSha1}")`);
} else {
throw e;
}
}
}
async function ensureExpectedRegion(romData, romRegion, expectedRegion) {
console.log(`ROM region: ${romRegion}, expected region: ${expectedRegion} `);
if (romRegion !== expectedRegion) {
const { patch } = await downloadPatch(`patches/${romRegion}-to-${expectedRegion}.xdelta`);
return await applyPatch(romData, patch);
} else {
return romData;
}
}
function isSaveDataGarbage(saveData) {
// The game sometimes saves 0xff garbage data for some reason
return saveData.length < 4 || (saveData[0] == 0xff && saveData[1] == 0xff && saveData[2] == 0xff && saveData[3] == 0xff);
}
function getAndCheckRomRegion(rom) {
// Read gamecode (see http://problemkaputt.de/gbatek.htm#dscartridgeheader)
const gameCode = String.fromCharCode(...rom.slice(0xC, 0xC + 4));
if (gameCode === 'C2SE') { // E (US, "English")
return 'us';
} else if (gameCode === 'C2SP') { // E (Europe)
return 'eu';
} else if (gameCode === 'C2SJ') { // J (Japan)
return 'jp';
} else {
throw new UserError('The provided ROM is not an Explorers of Sky ROM.');
}
}
async function selectAndReadFileAsArrayBuffer() {
return new Promise((resolve, reject) => {
const inputElement = document.createElement('input');
inputElement.type = 'file';
inputElement.addEventListener('change', async (event) => {
const file = event.target.files[0];
if (file) {
const reader = new FileReader();
reader.onload = function (e) {
resolve(e.target.result);
};
reader.onerror = function (e) {
reject(new Error("Error reading file: " + e));
};
reader.readAsArrayBuffer(file);
} else {
resolve(null);
}
});
inputElement.click();
});
}
function saveFile(bytes, name) {
const link = document.createElement('a');
link.href = createUrlFromBytes(bytes);
link.download = name;
link.click();
URL.revokeObjectURL(link.href);
}
function createUrlFromBytes(bytes) {
const blob = new Blob([bytes], { type: 'application/octet-stream' });
return URL.createObjectURL(blob);
}
function getCleanSha1ForRegion(region) {
return region === 'us' ? CLEAN_US_SHA1 : CLEAN_EU_SHA1;
}
function getFileNameFromUrl(url) {
const lastSegment = url.includes('/') ? url.split('/').pop() : url;
// Return the file name without extension
return lastSegment.includes('.')
? lastSegment.substr(0, lastSegment.lastIndexOf('.'))
: lastSegment;
}
function reportError(error) {
let text = '';
if (error.message && error.message.includes('not implemented')) {
// "Not implemented" error from the VCDiff library
text = 'This patch is not supported. Please ask the ROM hack author to provide a compatible patch and include the error details below:<br><br>';
} else if (!(error instanceof UserError)) {
text = 'An error occured.<br><br>';
}
for (const errorElem of document.querySelectorAll('.error')) {
errorElem.innerHTML = `${text}${error}`;
}
}
function removeError() {
for (const errorElem of document.querySelectorAll('.error')) {
errorElem.innerHTML = '';
}
}
async function loadSaveData(gameId) {
return localforage.getItem('save-' + gameId);
}
async function loadPlayer(url, gameId, name) {
// Patch desmond's keymap
desmondPatch();
const background = document.querySelector('.background');
background.classList.add('hidden');
const playerContainer = document.getElementById('player-container');
playerContainer.classList.remove('hidden');
const saveData = await loadSaveData(gameId);
const player = document.getElementById('player');
player.loadURL(url, () => {
// Load the save data
if (saveData) {
Module.HEAPU8.set(saveData, Module._savGetPointer(saveData.length))
Module._savUpdateChangeFlag();
console.log('Loaded save data');
globalThis.config.frameSkip = 1;
globalThis.config.powerSave = false;
}
});
document.title = `${name} - EoS Hack Player`;
document.getElementById('settings').addEventListener('click', () => {
const settings = document.getElementById('settings-menu');
settings.classList.remove('hidden');
});
document.getElementById('back').addEventListener('click', () => {
window.location.reload();
});
document.getElementById('save-backup').addEventListener('click', () => {
const size = Module._savGetSize();
if (size == 0) {
alert('No save data found, please save your game first.');
return;
}
const ptr = Module._savGetPointer(0);
const buffer = new Uint8Array(size);
buffer.set(Module.HEAPU8.subarray(ptr, ptr + size));
if (isSaveDataGarbage(buffer)) {
alert('No save data found, please save your game first.');
return;
}
saveFile(buffer, name + '.sav');
});
const toggleFastForwardElem = document.getElementById('toggle-fastforward');
toggleFastForwardElem.addEventListener('click', () => {
toggleFastForward();
});
window.addEventListener('beforeunload', (event) => {
const message = "Are you sure you want to exit? You might lose unsaved progress.";
event.returnValue = message;
return message;
});
let previousSaveFlag = 0;
setInterval(() => {
// Logic adapted from desmond.js `checkSaveGame` function
const saveFlag = Module._savUpdateChangeFlag();
if (saveFlag == 0 && previousSaveFlag == 1) {
console.log('Save detected');
const size = Module._savGetSize();
const ptr = Module._savGetPointer(0);
const buffer = new Uint8Array(size);
buffer.set(Module.HEAPU8.subarray(ptr, ptr + size));
// The game sometimes saves 0xff garbage data for some reason
if (size > 0 && !isSaveDataGarbage(buffer)) {
// desmond.js auto-loads a save file with this naming convention
localforage.setItem('save-' + gameId, buffer).then(() => {
console.log('Game saved.');
});
}
}
previousSaveFlag = saveFlag;
}, 1000);
}
async function createPatchSelect(links) {
const select = document.getElementById('patch-select');
select.innerHTML = '';
for (const [i, link] of links.entries()) {
const option = document.createElement('option');
option.value = i;
option.innerText = link.name;
select.appendChild(option);
}
}
async function patchRom(rom, link, region, validationSha1, button) {
const patchUrl = link.patch;
const patchRegion = link.region ?? 'us';
let patch;
if (patchUrl) {
button.innerText = 'Downloading patch (1/3)...';
const result = await downloadPatch(patchUrl, region);
patch = result.patch;
}
let patchedRom = rom.data;
let expectedSha1;
if (patchUrl) {
button.innerText = 'Patching (2/3)...';
const romInExpectedRegion = await ensureExpectedRegion(rom.data, rom.region, patchRegion);
expectedSha1 = getCleanSha1ForRegion(patchRegion);
console.log(`Validating checksum against clean SHA-1 "${expectedSha1}"`);
button.innerText = 'Patching (3/3)...';
console.log('Applying the ROM hack patch...');
patchedRom = await applyPatch(romInExpectedRegion, patch);
}
if (validationSha1) {
button.innerText = 'Validating...';
console.log(`Validating checksum against user - provided validation SHA-1 "${expectedSha1}"`);
const patchedRomSha1 = await sha1(patchedRom);
if (patchedRomSha1 !== validationSha1.toLowerCase()) {
throw new Error(`Failed to patch ROM(checksum mismatch: ${patchedRomSha1})`);
}
}
return patchedRom;
}
async function readRomFile(file) {
const data = await new Promise((resolve, reject) => {
const reader = new FileReader();
reader.onload = evt => resolve(new Uint8Array(evt.target.result));
reader.onerror = error => reject(error);
reader.readAsArrayBuffer(file);
});
const rom = {
name: file.name,
data
};
console.log(`Loaded ROM: ${rom.name} `);
return rom;
}
async function cacheRom(file) {
console.log('Caching ROM...');
await localforage.setItem('saved-rom', file);
// Delete the legacy cache key if present
localforage.removeItem('cached-rom').catch(console.error);
}
async function clearCachedRom() {
console.log('Clearing cached ROM...');
await localforage.removeItem('saved-rom');
}
async function loadCachedRom() {
const rom = await localforage.getItem('saved-rom');
if (!rom) return null;
const inputWrapper = document.getElementById('rom-input-wrapper');
const loadedRomElem = document.getElementById('loaded-rom');
const removeRomElem = document.getElementById('remove-rom');
document.getElementById('rom-name').textContent = rom.name;
inputWrapper.classList.add('hidden');
loadedRomElem.classList.remove('hidden');
removeRomElem.classList.remove('hidden');
document.getElementById('play-button').disabled = false;
document.getElementById('save-button').disabled = false;
return rom;
}
async function sha1(arrayBuffer) {
const hashBuffer = await crypto.subtle.digest('SHA-1', arrayBuffer);
const hashArray = Array.from(new Uint8Array(hashBuffer));
const hashHex = hashArray.map(b => b.toString(16).padStart(2, '0')).join('');
return hashHex;
}
document.addEventListener('DOMContentLoaded', () => {
createPatchSelect(links);
const inputWrapper = document.getElementById('rom-input-wrapper');
const loadedRomElem = document.getElementById('loaded-rom');
const fileInput = document.getElementById('rom-file');
const removeRomButton = document.getElementById('remove-rom');
const playButton = document.getElementById('play-button');
const saveButton = document.getElementById('save-button');
const inProgressButton = document.getElementById('in-progress');
const params = new URLSearchParams(window.location.search);
const region = params.get('region') || 'us';
const validationSha1 = params.get('sha1');
let romFile = null;
fileInput.addEventListener('change', async () => {
if (!fileInput.files
|| !fileInput.files.length
|| !fileInput.files[0].name.endsWith('.nds')
) {
return;
}
fileInput.disabled = true;
const file = fileInput.files[0];
const romNameElem = document.getElementById('rom-name');
romNameElem.textContent = "Loading...";
inputWrapper.classList.add('hidden');
loadedRomElem.classList.remove('hidden');
removeRomButton.classList.add('hidden');
try {
romFile = await readRomFile(file);
const romRegion = getAndCheckRomRegion(romFile.data);
romFile.region = romRegion;
let romSha1 = await sha1(romFile.data);
if (!await isRomClean(romSha1, romRegion)) {
romNameElem.textContent = "Cleaning ROM...";
romFile.data = await cleanRom(romFile.data, romRegion, romSha1);
romSha1 = await sha1(romFile.data);
}
if (romSha1 !== getCleanSha1ForRegion(romRegion)) {
throw new Error(`Failed to clean rom(checksum mismatch: ${romSha1})`);
}
await cacheRom(romFile);
romNameElem.textContent = romFile.name;
playButton.disabled = false;
saveButton.disabled = false;
removeRomButton.classList.remove('hidden');
} catch (e) {
reportError(e);
console.error(e);
romFile = null;
fileInput.value = '';
fileInput.disabled = false;
inputWrapper.classList.remove('hidden');
loadedRomElem.classList.add('hidden');
}
});
removeRomButton.addEventListener('click', () => {
romFile = null;
fileInput.value = '';
fileInput.disabled = false;
inputWrapper.classList.remove('hidden');
loadedRomElem.classList.add('hidden');
playButton.disabled = true;
saveButton.disabled = true;
clearCachedRom();
});
const onClick = async (shouldPlay) => {
playButton.classList.add('hidden');
saveButton.classList.add('hidden');
inProgressButton.classList.remove('hidden');
removeError();
try {
const patchIndex = parseInt(document.getElementById('patch-select').value);
const link = links[patchIndex];
const patchedRom = await patchRom(romFile, link, region, validationSha1, inProgressButton);
if (shouldPlay) {
const url = createUrlFromBytes(patchedRom);
await loadPlayer(url, link.id, link.name);
} else {
saveFile(patchedRom, link.name + '.nds');
}
} catch (e) {
reportError(e);
console.error(e);
} finally {
playButton.classList.remove('hidden');
saveButton.classList.remove('hidden');
inProgressButton.classList.add('hidden');
}
};
playButton.addEventListener('click', async () => {
await onClick(true);
});
saveButton.addEventListener('click', async () => {
await onClick(false);
});
loadKeyBindings();
initSettingsView();
document.getElementById('fullscreen').addEventListener('click', () => {
const player = document.getElementById('player-container');
if (player.webkitRequestFullscreen) {
player.webkitRequestFullscreen();
} else if (player.requestFullscreen) {
player.requestFullscreen();
}
});
document.getElementById('patch-select').addEventListener('change', async () => {
const patch = selectedHack();
if (patch) {
localStorage.setItem('last-patch', patch.id);
}
await updateHackInfo();
});
const lastPatch = localStorage.getItem('last-patch');
if (lastPatch) {
const patchIndex = links.findIndex(link => link.id === lastPatch);
if (patchIndex >= 0) {
document.getElementById('patch-select').value = patchIndex;
}
}
loadCachedRom().then(file => {
if (file) {
romFile = file;
}
});
updateHackInfo(); // Update the hack info on page load
document.getElementById('load-save').addEventListener('click', async () => {
const file = await selectAndReadFileAsArrayBuffer();
if (!file) return;
const selectedPatchIndex = parseInt(document.getElementById('patch-select').value);
const selectedPatch = links[selectedPatchIndex];
try {
if (isSaveDataGarbage(file)) {
throw new Error('The provided save file is invalid.');
}
const saveData = readSaveData(file);
console.log('Read save data:', saveData);
const saveDataBuffer = new Uint8Array(file);
await localforage.setItem('save-' + selectedPatch.id, saveDataBuffer);
console.log('Stored save data.');
await updateHackInfo();
} catch (e) {
reportError(e);
}
});
document.getElementById('export-save').addEventListener('click', async () => {
const selectedPatch = selectedHack();
const saveDataBuffer = await loadSaveData(selectedPatch.id);
if (!saveDataBuffer || isSaveDataGarbage(saveDataBuffer)) {
alert('No save data found, please save your game first.');
return;
}
saveFile(saveDataBuffer, selectedPatch.name + '.sav');
});
document.getElementById('delete-save').addEventListener('click', async () => {
const selectedPatch = selectedHack();
let text = `Are you sure you want to delete the save data for ${selectedPatch.name}? Deleted save data cannot be recovered.`;
if (selectedPatch.id === 'strungupbysketches' || selectedPatch.id === 'fantariem') {
text += '\n\nDeleting the save data will reset ALL your game progress. To trigger an in-game event, you will need to start the game and delete your save data under Other - Delete Save Data instead.';
}
if (confirm(text)) {
await localforage.removeItem('save-' + selectedPatch.id);
await updateHackInfo();
}
});
});
function selectedHack() {
const patchIndex = parseInt(document.getElementById('patch-select').value);
return links[patchIndex];
}
async function updateHackInfo() {
const selectedPatch = selectedHack();
document.getElementById('hack-info').classList.remove('hidden');
const hackNameElement = document.getElementById('hack-name');
const hackAuthorElement = document.getElementById('hack-author');
const hackLinkElement = document.getElementById('hack-link');
const noHackLinkElement = document.getElementById('no-hack-link');
const exportSaveButton = document.getElementById('export-save');
const deleteSaveButton = document.getElementById('delete-save');
hackNameElement.textContent = selectedPatch.name;
hackAuthorElement.textContent = 'by ' + selectedPatch.author;
const saveInfoElement = document.getElementById('save-info');
const noSaveElement = document.getElementById('no-save');
saveInfoElement.classList.add('hidden');
noSaveElement.classList.add('hidden');
exportSaveButton.classList.add('hidden');
deleteSaveButton.classList.add('hidden');
if (selectedPatch.page) {
hackLinkElement.href = selectedPatch.page;
hackLinkElement.classList.remove('hidden');
noHackLinkElement.classList.add('hidden');
} else {
hackLinkElement.classList.add('hidden');
noHackLinkElement.classList.remove('hidden');
}
// Update save info
const saveDataBuffer = await loadSaveData(selectedPatch.id);
saveInfoElement.classList.remove('hidden');
if (saveDataBuffer && !isSaveDataGarbage(saveDataBuffer)) {
try {
const saveData = readSaveData(saveDataBuffer);
console.log('Read save data:', saveData);
noSaveElement.classList.add('hidden');
saveInfoElement.classList.remove('hidden');
document.getElementById('save-hero-name').textContent = saveData.heroName || 'No hero name';
document.getElementById('save-team-name').textContent = saveData.teamName || 'No team name';
const playTimeInSeconds = saveData.playTimeInSeconds || 0;
let hours = Math.floor(playTimeInSeconds / 3600);
if (hours < 10) hours = '0' + hours;
let minutes = Math.floor((playTimeInSeconds % 3600) / 60);
if (minutes < 10) minutes = '0' + minutes;
let seconds = Math.floor(playTimeInSeconds % 60);
if (seconds < 10) seconds = '0' + seconds;
document.getElementById('save-playtime').textContent = `${hours}:${minutes}:${seconds}`;
const adventures = saveData.numberOfAdventures || 0;
document.getElementById('save-adventures').textContent = adventures + ' adventure' + (adventures === 1 ? '' : 's');
exportSaveButton.classList.remove('hidden');
deleteSaveButton.classList.remove('hidden');
} catch (e) {
reportError(e);
}
} else {
noSaveElement.classList.remove('hidden');
saveInfoElement.classList.add('hidden');
}
}
document.addEventListener('fullscreenchange', onFullScreenChange);
document.addEventListener('webkitfullscreenchange', onFullScreenChange);
function onFullScreenChange() {
if (document.fullscreenElement) {
document.querySelector('.controls').classList.add('hidden');
} else {
document.querySelector('.controls').classList.remove('hidden');
}
}
function toggleFastForward() {
const elem = document.getElementById('toggle-fastforward');
const isFastForward = globalThis.config.frameSkip === 2;
if (isFastForward) {
globalThis.config.frameSkip = 1;
elem.querySelector('span').innerHTML = 'fast_forward';
} else {
globalThis.config.frameSkip = 2;
elem.querySelector('span').innerHTML = 'play_arrow';
}
}
// Replace Desmond's keymap
function desmondPatch() {
window.onkeydown = window.onkeyup = (e) => {
// Copied from Desmond library code
if (!emuIsRunning || isSettingsMenuOpen()) {
return;
}
var isDown = (e.type === "keydown");
var k = convertKey(e.key);
if (isDown) {
if (e.key === keyboardMappings[KEY_INDICES['save-state']]) {
config.createSaveStateNextFrame = true;
e.preventDefault();
} else if (e.key === keyboardMappings[KEY_INDICES['load-state']]) {
config.loadSaveState = true;
e.preventDefault();
} else if (e.key === keyboardMappings[KEY_INDICES['fast-forward']]) {
toggleFastForward();
e.preventDefault();
}
}
if (k >= 0 && k <= 13) {
emuKeyState[k] = isDown;
e.preventDefault();
}
if (e.keyCode == 27) {
uiSwitchTo('menu');
}
}
}
function convertKey(keyCode) {
for (var i = 0; i < keyboardMappings.length; i++) {
if (keyCode == keyboardMappings[i]) {
return i;
}
}
return -1;
}