2023-06-22 16:00:13 +00:00
|
|
|
class EJS_GameManager {
|
|
|
|
constructor(Module) {
|
|
|
|
this.Module = Module;
|
|
|
|
this.FS = this.Module.FS;
|
|
|
|
this.functions = {
|
|
|
|
restart: this.Module.cwrap('system_restart', '', []),
|
|
|
|
getStateInfo: this.Module.cwrap('get_state_info', 'string', []), //these names are dumb
|
|
|
|
saveStateInfo: this.Module.cwrap('save_state_info', 'null', []),
|
|
|
|
loadState: this.Module.cwrap('load_state', 'number', ['string', 'number']),
|
2023-06-23 16:33:20 +00:00
|
|
|
screenshot: this.Module.cwrap('cmd_take_screenshot', '', []),
|
|
|
|
simulateInput: this.Module.cwrap('simulate_input', 'null', ['number', 'number', 'number']),
|
2023-06-27 16:54:35 +00:00
|
|
|
toggleMainLoop: this.Module.cwrap('toggleMainLoop', 'null', ['number']),
|
2023-06-28 18:27:06 +00:00
|
|
|
getCoreOptions: this.Module.cwrap('get_core_options', 'string', []),
|
2023-06-29 15:35:25 +00:00
|
|
|
setVariable: this.Module.cwrap('set_variable', 'null', ['string', 'string']),
|
|
|
|
setCheat: this.Module.cwrap('set_cheat', 'null', ['number', 'number', 'string']),
|
2023-06-29 16:49:48 +00:00
|
|
|
resetCheat: this.Module.cwrap('reset_cheat', 'null', []),
|
2023-07-01 16:46:52 +00:00
|
|
|
toggleShader: this.Module.cwrap('shader_enable', 'null', ['number']),
|
|
|
|
getDiskCount: this.Module.cwrap('get_disk_count', 'number', []),
|
|
|
|
getCurrentDisk: this.Module.cwrap('get_current_disk', 'number', []),
|
2023-07-01 21:21:48 +00:00
|
|
|
setCurrentDisk: this.Module.cwrap('set_current_disk', 'null', ['number']),
|
2023-07-03 14:31:38 +00:00
|
|
|
getSaveFilePath: this.Module.cwrap('save_file_path', 'string', []),
|
|
|
|
saveSaveFiles: this.Module.cwrap('cmd_savefiles', '', []),
|
|
|
|
supportsStates: this.Module.cwrap('supports_states', 'number', [])
|
2023-06-22 16:00:13 +00:00
|
|
|
}
|
2023-06-28 17:57:02 +00:00
|
|
|
this.mkdir("/home");
|
|
|
|
this.mkdir("/home/web_user");
|
|
|
|
this.mkdir("/home/web_user/retroarch");
|
|
|
|
this.mkdir("/home/web_user/retroarch/userdata");
|
|
|
|
|
2023-06-29 21:26:30 +00:00
|
|
|
//this.FS.writeFile("/home/web_user/retroarch/userdata/retroarch.cfg", this.getRetroArchCfg());
|
2023-06-29 16:49:48 +00:00
|
|
|
|
|
|
|
this.initShaders();
|
2023-06-28 17:57:02 +00:00
|
|
|
}
|
|
|
|
mkdir(path) {
|
|
|
|
try {
|
|
|
|
this.FS.mkdir(path);
|
|
|
|
} catch(e) {}
|
|
|
|
}
|
|
|
|
getRetroArchCfg() {
|
2023-06-30 17:42:51 +00:00
|
|
|
return "\n";
|
2023-06-22 16:00:13 +00:00
|
|
|
}
|
2023-06-29 16:49:48 +00:00
|
|
|
initShaders() {
|
|
|
|
if (!window.EJS_SHADERS) return;
|
|
|
|
this.mkdir("/shader");
|
|
|
|
for (const shader in window.EJS_SHADERS) {
|
|
|
|
this.FS.writeFile('/shader/'+shader, window.EJS_SHADERS[shader]);
|
|
|
|
}
|
|
|
|
}
|
2023-06-22 16:00:13 +00:00
|
|
|
restart() {
|
|
|
|
this.functions.restart();
|
|
|
|
}
|
|
|
|
getState() {
|
|
|
|
return new Promise(async (resolve, reject) => {
|
|
|
|
const stateInfo = (await this.getStateInfo()).split('|')
|
|
|
|
let state;
|
|
|
|
let size = stateInfo[0] >> 0;
|
|
|
|
if (size > 0) {
|
|
|
|
state = new Uint8Array(size);
|
|
|
|
let start = stateInfo[1] >> 0;
|
|
|
|
for (let i=0; i<size; i++) state[i] = this.Module.getValue(start + i);
|
|
|
|
}
|
|
|
|
resolve(state);
|
|
|
|
})
|
|
|
|
}
|
|
|
|
getStateInfo() {
|
|
|
|
this.functions.saveStateInfo();
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
let a;
|
|
|
|
let b = setInterval(() => {
|
|
|
|
a = this.functions.getStateInfo();
|
|
|
|
if (a) {
|
|
|
|
clearInterval(b);
|
|
|
|
resolve(a);
|
|
|
|
}
|
|
|
|
}, 50)
|
|
|
|
});
|
|
|
|
}
|
|
|
|
loadState(state) {
|
|
|
|
try {
|
|
|
|
this.FS.unlink('game.state');
|
|
|
|
} catch(e){}
|
|
|
|
this.FS.writeFile('/game.state', state);
|
|
|
|
this.functions.loadState("game.state", 0);
|
|
|
|
setTimeout(() => {
|
|
|
|
this.FS.unlink('game.state');
|
|
|
|
}, 5000)
|
|
|
|
}
|
|
|
|
screenshot() {
|
|
|
|
this.functions.screenshot();
|
|
|
|
return this.FS.readFile('screenshot.png');
|
|
|
|
}
|
|
|
|
quickSave() {
|
|
|
|
(async () => {
|
|
|
|
let slot = 0;
|
|
|
|
let name = slot + '-quick.state';
|
|
|
|
try {
|
|
|
|
this.FS.unlink(name);
|
|
|
|
} catch (e) {}
|
|
|
|
let data = await this.getState();
|
|
|
|
this.FS.writeFile('/'+name, data);
|
|
|
|
})();
|
|
|
|
}
|
|
|
|
quickLoad() {
|
|
|
|
(async () => {
|
|
|
|
let slot = 0;
|
|
|
|
let name = slot + '-quick.state';
|
|
|
|
this.functions.loadState(name, 0);
|
|
|
|
})();
|
|
|
|
}
|
2023-06-23 16:33:20 +00:00
|
|
|
simulateInput(player, index, value) {
|
|
|
|
this.functions.simulateInput(player, index, value);
|
|
|
|
}
|
|
|
|
toggleMainLoop(playing) {
|
|
|
|
this.functions.toggleMainLoop(playing);
|
|
|
|
}
|
2023-06-27 16:54:35 +00:00
|
|
|
getCoreOptions() {
|
|
|
|
return this.functions.getCoreOptions();
|
|
|
|
}
|
2023-06-28 18:27:06 +00:00
|
|
|
setVariable(option, value) {
|
|
|
|
this.functions.setVariable(option, value);
|
|
|
|
}
|
2023-06-29 15:35:25 +00:00
|
|
|
setCheat(index, enabled, code) {
|
|
|
|
this.functions.setCheat(index, enabled, code);
|
|
|
|
}
|
|
|
|
resetCheat() {
|
|
|
|
this.functions.resetCheat();
|
|
|
|
}
|
2023-06-29 16:49:48 +00:00
|
|
|
toggleShader(active) {
|
|
|
|
this.functions.toggleShader(active);
|
|
|
|
}
|
2023-07-01 16:46:52 +00:00
|
|
|
getDiskCount() {
|
|
|
|
return this.functions.getDiskCount();
|
|
|
|
}
|
|
|
|
getCurrentDisk() {
|
|
|
|
return this.functions.getCurrentDisk();
|
|
|
|
}
|
|
|
|
setCurrentDisk(disk) {
|
|
|
|
this.functions.setCurrentDisk(disk);
|
|
|
|
}
|
2023-07-03 14:31:38 +00:00
|
|
|
getSaveFilePath() {
|
|
|
|
return this.functions.getSaveFilePath();
|
|
|
|
}
|
|
|
|
saveSaveFiles() {
|
|
|
|
this.functions.saveSaveFiles();
|
|
|
|
}
|
|
|
|
supportsStates() {
|
|
|
|
return !!this.functions.supportsStates();
|
2023-07-01 21:21:48 +00:00
|
|
|
}
|
2023-06-22 16:00:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
window.EJS_GameManager = EJS_GameManager;
|