You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

server.js 1.7KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. var emu = require("./js/emulate");
  2. var ipc = require("./js/ipc");
  3. var shell = require("./js/shell");
  4. var udev = require("./udev");
  5. var pathlib = require("path");
  6. var fs = require("fs");
  7. var mountdir = "/tmp/emuboy.mnt"
  8. process.on("unhandledRejection", evt => {
  9. console.trace(evt);
  10. process.exit(1);
  11. });
  12. var rompath = mountdir+"/roms";
  13. var games = [];
  14. function updateGames() {
  15. if (driveMounted) {
  16. var rx = /\.(gba|gbc|nds)$/;
  17. fs.readdir(rompath, (err, list) => {
  18. if (err)
  19. throw err;
  20. games = list.filter(x => rx.test(x));
  21. ipc.sendGameList(games);
  22. });
  23. } else {
  24. games = [];
  25. ipc.sendGameList(games);
  26. }
  27. }
  28. shell.safe("./scripts/keymap.sh");
  29. var driveMounted = false;
  30. async function onblock(evt) {
  31. if (evt.DEVTYPE !== "partition")
  32. return;
  33. if (evt.PARTN !== "1")
  34. return;
  35. if (evt.ID_BUS !== "usb")
  36. return;
  37. if (!evt.ACTION || evt.ACTION === "add") {
  38. await shell.safe("mkdir", "-p", mountdir);
  39. if (await shell("mountpoint", "-q", mountdir) === 0)
  40. await shell.safe("sudo", "umount", mountdir);
  41. await shell.safe(
  42. "sudo", "mount", "-o", "uid="+process.env.USER,
  43. evt.DEVNAME, mountdir);
  44. await shell.safe("mkdir", "-p", rompath);
  45. driveMounted = true;
  46. updateGames();
  47. } else if (evt.ACTION === "remove") {
  48. driveMounted = false;
  49. console.log("Unmounted drive", evt.DEVNAME);
  50. updateGames();
  51. }
  52. }
  53. udev.init();
  54. udev.monitor("block", onblock);
  55. udev.list("block", x => x.forEach(onblock));
  56. ipc.init(8085, () => {
  57. console.log("Server started");
  58. ipc.on("run", async function(name) {
  59. try {
  60. ipc.sendGameStart();
  61. await emu.run(pathlib.join(rompath, name));
  62. } catch (err) {
  63. ipc.error(err.toString());
  64. console.trace(err);
  65. }
  66. ipc.sendGameStopped();
  67. });
  68. ipc.on("connection", () => {
  69. updateGames();
  70. });
  71. });