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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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. var driveMounted = false;
  29. async function onblock(evt) {
  30. if (evt.DEVTYPE !== "partition")
  31. return;
  32. if (evt.PARTN !== "1")
  33. return;
  34. if (evt.ID_BUS !== "usb")
  35. return;
  36. if (!evt.ACTION || evt.ACTION === "add") {
  37. await shell.safe("mkdir", "-p", mountdir);
  38. if (await shell("mountpoint", "-q", mountdir) === 0)
  39. await shell.safe("sudo", "umount", mountdir);
  40. await shell.safe(
  41. "sudo", "mount", "-o", "uid="+process.env.USER,
  42. evt.DEVNAME, mountdir);
  43. await shell.safe("mkdir", "-p", rompath);
  44. driveMounted = true;
  45. updateGames();
  46. } else if (evt.ACTION === "remove") {
  47. driveMounted = false;
  48. console.log("Unmounted drive", evt.DEVNAME);
  49. updateGames();
  50. }
  51. }
  52. udev.init();
  53. udev.monitor("block", onblock);
  54. udev.list("block", x => x.forEach(onblock));
  55. ipc.init(8085, () => {
  56. console.log("Server started");
  57. ipc.on("run", async function(name) {
  58. try {
  59. ipc.sendGameStart();
  60. await emu.run(pathlib.join(rompath, name));
  61. } catch (err) {
  62. ipc.error(err.toString());
  63. console.trace(err);
  64. }
  65. ipc.sendGameStopped();
  66. });
  67. ipc.on("connection", () => {
  68. updateGames();
  69. });
  70. });