Skip to content

Instantly share code, notes, and snippets.

@ChrisVilches
Last active October 23, 2018 02:31
Show Gist options
  • Save ChrisVilches/66e9ca4a122c1e1d39a8af539853aaf1 to your computer and use it in GitHub Desktop.
Save ChrisVilches/66e9ca4a122c1e1d39a8af539853aaf1 to your computer and use it in GitHub Desktop.
Mednafen Console GUI
const fs = require("fs");
const path = require ("path");
const readline = require('readline');
const { spawn } = require('child_process');
let ROMS_FOLDER = "C:/Users/.../PSX";
let MEDNAFEN_HOME = "C:/Users/.../Mednafen";
// Initialize the line reader
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
const cueList = fromDir(ROMS_FOLDER, '.cue');
function fromDir(startPath,filter){
let result = [];
if (!fs.existsSync(startPath)){
console.log("No dir ", startPath);
return [];
}
let files = fs.readdirSync(startPath);
for(let i=0;i<files.length;i++){
let filename = path.join(startPath, files[i]);
let stat = fs.lstatSync(filename);
if (stat.isDirectory()){
fromDir(filename,filter);
}
else if (filename.indexOf(filter)>=0) {
result.push(files[i]);
}
}
return result;
}
function showMenu(){
let extensionRegex = new RegExp(/\.cue$/);
// Print the menu
for(let i=0; i<cueList.length; i++){
let menuItem = `(${i}) ${cueList[i]}`.replace(extensionRegex, "");
console.log(menuItem);
}
}
function startMednafen(fullRomPath){
const ls = spawn(`${MEDNAFEN_HOME}/mednafen.exe`, [fullRomPath]);
ls.stdout.on('data', (data) => {
console.log(`stdout: ${data}`);
});
ls.stderr.on('data', (data) => {
console.log(`stderr: ${data}`);
});
ls.on('close', (code) => {
console.log(`child process exited with code ${code}`);
showMenu();
chooseGame();
});
}
function chooseGame(){
function error(){
console.log("Error");
chooseGame();
}
// Ask which game to play
rl.question("Choose → ", (answer) => {
if(typeof answer === "undefined") return error();
answer = answer.trim();
if(answer.length === 0) return error();
answer = Number(answer);
if(typeof cueList[answer] === "undefined") return error();
let fullRomPath = ROMS_FOLDER + "/" + cueList[answer];
startMednafen(fullRomPath);
});
}
showMenu();
chooseGame();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment