Initial implementation of the web emulator

This commit is contained in:
ovosimpatico
2024-11-14 10:01:10 -03:00
parent 255070feeb
commit b20d3954c6
11 changed files with 568 additions and 4 deletions

View File

@@ -10,13 +10,17 @@ import sanitize from "sanitize";
import debugPrint from "./lib/debugprint.js";
import compression from "compression";
import { generateAsciiArt } from './lib/asciiart.js';
import { getEmulatorConfig, isEmulatorCompatible, isNonGameContent } from './lib/emulatorConfig.js';
import fetch from 'node-fetch';
let fileListPath = "./data/filelist.json";
let queryCountFile = "./data/queries.txt";
let categoryListPath = "./lib/categories.json"
let searchAlikesPath = './lib/searchalikes.json'
let nonGameTermsPath = './lib/nonGameTerms.json'
let categoryList = await FileHandler.parseJsonFile(categoryListPath);
global.searchAlikes = await FileHandler.parseJsonFile(searchAlikesPath)
let nonGameTerms = await FileHandler.parseJsonFile(nonGameTermsPath);
let crawlTime = 0;
let queryCount = 0;
let fileCount = 0;
@@ -101,7 +105,8 @@ let defaultOptions = {
queryCount: queryCount,
fileCount: fileCount,
termCount: search.miniSearch.termCount,
generateAsciiArt: generateAsciiArt
generateAsciiArt: generateAsciiArt,
isEmulatorCompatible: isEmulatorCompatible
};
function updateDefaults(){
@@ -216,6 +221,62 @@ app.get("/about", function (req, res) {
res.render(indexPage, buildOptions(page));
});
app.get("/play/:id", async function (req, res) {
// Block access if emulator is disabled
if (process.env.EMULATOR_ENABLED !== 'true') {
res.redirect('/');
return;
}
let fileId = parseInt(req.params.id);
let romFile = search.findIndex(fileId);
if (!romFile) {
res.redirect('/');
return;
}
let options = {
romFile: romFile,
emulatorConfig: getEmulatorConfig(romFile.category),
isNonGame: isNonGameContent(romFile.filename, nonGameTerms)
};
let page = "emulator";
options = buildOptions(page, options);
res.render(indexPage, options);
});
app.get("/proxy-rom/:id", async function (req, res) {
// Block access if emulator is disabled
if (process.env.EMULATOR_ENABLED !== 'true') {
res.status(403).send('Emulator feature is disabled');
return;
}
let fileId = parseInt(req.params.id);
let romFile = search.findIndex(fileId);
if (!romFile) {
res.status(404).send('ROM not found');
return;
}
try {
const response = await fetch(romFile.path);
const contentLength = response.headers.get('content-length');
res.setHeader('Content-Type', 'application/zip');
res.setHeader('Content-Length', contentLength);
res.setHeader('Content-Disposition', `attachment; filename="${romFile.filename}"`);
response.body.pipe(res);
} catch (error) {
console.error('Error proxying ROM:', error);
res.status(500).send('Error fetching ROM');
}
});
server.listen(process.env.PORT, process.env.BIND_ADDRESS);
server.on("listening", function () {
console.log(