-
Notifications
You must be signed in to change notification settings - Fork 0
/
background.js
223 lines (190 loc) · 4.21 KB
/
background.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
const scripts = [
"constants.js",
"js/util.js",
"js/core.js",
"js/arm.js",
"js/thumb.js",
"js/mmu.js",
"js/io.js",
"js/audio.js",
"js/video.js",
"js/video/proxy.js",
"js/video/software.js",
"js/irq.js",
"js/keypad.js",
"js/sio.js",
"js/savedata.js",
"js/gpio.js",
"js/gba.js",
"resources/xhr.js",
"indexeddb/Tools.js",
"indexeddb/Migrate.js",
"indexeddb/Controller.js",
];
const GBA_KEYS = {
KEYCODE_LEFT: 37,
KEYCODE_UP: 38,
KEYCODE_RIGHT: 39,
KEYCODE_DOWN: 40,
KEYCODE_START: 13,
KEYCODE_SELECT: 220,
KEYCODE_A: 90,
KEYCODE_B: 88,
KEYCODE_L: 65,
KEYCODE_R: 83,
}
var MY_KEYBOARD_MAP = {
37: 37,
38: 38,
39: 39,
40: 40,
13: 13,
220: 220,
90: 90,
88: 88,
65: 65,
83: 83,
}
async function updateKeyBoard() {
keys = (await chrome.storage.local.get('gba_key_map'))['gba_key_map'] || GBA_KEYS
MY_KEYBOARD_MAP = getMapKey(keys)
}
function getMapKey(map) {
const obj = {}
Object.entries(map).forEach(([key, value]) => {
obj[value] = GBA_KEYS[key]
})
return obj
}
// 读取
updateKeyBoard()
importScripts(...scripts);
let connection = null;
let currentRom = null;
const gbaStorage = new GbaController();
// connection handler
chrome.runtime.onConnect.addListener(function (port) {
port.onDisconnect.addListener(function () {
connection = null;
gba.pause();
gbaStorage.updateMemo(
currentRom.md5,
GbaController.DEFAULT_ROM_UNAME,
gba.mmu.save.buffer
);
});
port.onMessage.addListener(async function ({ type }) {
if (type === "reqFrame") {
currentRom && connection &&
connection.postMessage({
type: "screen",
data: Array.from(gba.video.renderPath.pixelData.data)
});
}
});
connection = port;
if (currentRom) {
gba.keypad.registerHandlers();
gba.runStable();
}
});
// message handler
chrome.runtime.onMessage.addListener(async function (
{ type },
sender,
sendResponse
) {
// 插rom卡
if (type === "RomChange") {
await getCurrentRom();
if (currentRom) {
const memo = await getMemoCard(currentRom.md5);
// 加载 memo
memo.arrayBuffer &&
runCommands.push(() => {
gba.setSavedata(memo.arrayBuffer);
});
}
gba.pause();
gba.reset();
loadRomFromArrayBuffer(currentRom.arrayBuffer);
} else if (type === "SaveMemo") {
// cun
await gbaStorage.updateMemo(
currentRom.md5,
GbaController.DEFAULT_ROM_UNAME,
gba.mmu.save.buffer
);
sendResponse();
} else if (type === "UpdateKeyBoard") {
// 按键映射更新
updateKeyBoard()
}
});
var gba;
var runCommands = [];
var debug = null;
async function main() {
try {
gba = new GameBoyAdvance();
gba.keypad.eatInput = true;
gba.setLogger(function (level, error) {
console.log(error);
gba.pause();
});
} catch (exception) {
console.log("exceptionz", exception);
gba = null;
}
if (gba && FileReader) {
var canvas = new OffscreenCanvas(240, 160);
gba.setCanvas(canvas);
gba.logLevel = gba.LOG_ERROR;
loadRom("resources/bios.bin", function (bios) {
gba.setBios(bios);
});
await getCurrentRom();
if (currentRom) {
const memo = await getMemoCard(currentRom.md5);
// 加载 memo
memo.arrayBuffer &&
runCommands.push(() => {
gba.setSavedata(memo.arrayBuffer);
});
loadRomFromArrayBuffer(currentRom.arrayBuffer);
}
}
}
function loadRomFromArrayBuffer(buf) {
gba.loadRomFromFile(buf, function (result) {
if (result) {
for (var i = 0; i < runCommands.length; ++i) {
runCommands[i]();
}
runCommands = [];
} else {
console.log("testLoad fail");
}
});
}
/**
* 获取当前rom
*/
async function getCurrentRom() {
const rKey = (await chrome.storage.local.get(ROM_STORAGE_KEY))[
ROM_STORAGE_KEY
];
if (!rKey) {
currentRom = null;
gba.reset();
return;
}
// 从 indexeddb 中读取 rom 的 arrayBuffer
const rom = await gbaStorage.queryRomByMd5(rKey);
currentRom = rom;
return rom;
}
async function getMemoCard(rKey, uName = GbaController.DEFAULT_ROM_UNAME) {
return await gbaStorage.queryMemoByRomUser(rKey, uName);
}
main();