diff --git a/start.bat b/dev.bat similarity index 100% rename from start.bat rename to dev.bat diff --git a/start.sh b/dev.sh similarity index 100% rename from start.sh rename to dev.sh diff --git a/prod.bat b/prod.bat new file mode 100644 index 0000000..f82489d --- /dev/null +++ b/prod.bat @@ -0,0 +1,2 @@ +set RCCSERVICE_PATH=C:\\RCCService\\ +npm run start \ No newline at end of file diff --git a/src/lib/classes/RCCService.js b/src/lib/classes/RCCService.js index e45da9f..0a755b0 100644 --- a/src/lib/classes/RCCService.js +++ b/src/lib/classes/RCCService.js @@ -1,5 +1,6 @@ const EventEmitter = require("events") const child_process = require("child_process") +const waitPort = require("wait-port") const logger = require("../../lib/logger.js") class RCCService extends EventEmitter { @@ -17,23 +18,31 @@ class RCCService extends EventEmitter { } else { this.proc = child_process.spawn("wine", ["RCCService.exe", "-Console", "-PlaceId:-1", `-Port`, this.port], options) } - this.proc.once("spawn", () => { - logger.info(`Spawned RCCService instance on port ${this.port}`) - resolve(this.proc) + + this.proc.once("spawn", async () => { + logger.info(`[${this.port}] RCCService instance spawned`) + const { open } = await waitPort({ host: "127.0.0.1", port: this.port, timeout: 5000, output: "silent" }).catch((e) => console.log(e)) + if (!open || this.proc.exitCode !== null) { + logger.error(`[${this.port}] RCCService could not listen, exiting`) + this.Close() + return resolve(false) + } + + return resolve(true) }) + this.proc.once("exit", () => { this.proc = null - logger.info(`An RCCService instance has closed on port ${this.port}`) + logger.info(`[${this.port}] RCCService instance exited`) }) } catch (_) { - logger.error(_) - reject(_) + resolve(false) } }) } Stop(signal = "SIGTERM") { - if (!this.proc) throw new Error("Process is not running") + if (!this.proc) return return this.proc.kill(signal) } } diff --git a/src/lua/fullbody.lua b/src/lua/bodyshot.lua similarity index 100% rename from src/lua/fullbody.lua rename to src/lua/bodyshot.lua diff --git a/src/routes/render/asset.js b/src/routes/render/asset.js index 263b50f..47f39c6 100644 --- a/src/routes/render/asset.js +++ b/src/routes/render/asset.js @@ -1,4 +1,3 @@ -const waitPort = require("wait-port") const { readFileSync } = require("fs") const { randomUUID } = require("crypto") const logger = require("../../lib/logger.js") @@ -7,40 +6,40 @@ const Job = require("../../lib/classes/Job.js") const express = require("express") const app = express.Router() -app.get("/", async (request, response) => { +app.get("/:id", async (request, response) => { let tempPort = 64990 const job = new Job(randomUUID(), tempPort) - await job.Start() - - const { open } = await waitPort({ host: "127.0.0.1", port: tempPort, timeout: 5000, output: "silent" }) - if (!open) { - logger.warn("Job could not be started because port is already taken. Closing job..") - job.Close() - return response.json(false) - } + const started = await job.Start() + if (!started) return response.status(500).json({ error: "RCCService failed to start" }) + logger.info(`[${job.id}] Job opened`) await job.CreateClient() - const result = await job.Open({ - name: job.id, - script: readFileSync(__dirname + "/../lua/xml.lua", { encoding: "utf-8" }), - arguments: { - LuaValue: [ - { type: "LUA_TSTRING", value: job.id }, - { type: "LUA_TSTRING", value: "XML" }, - { type: "LUA_TSTRING", value: "PNG" }, + const result = await job + .Open({ + name: job.id, + script: readFileSync(__dirname + "/../../lua/xml.lua", { encoding: "utf-8" }), + arguments: { + LuaValue: [ + { type: "LUA_TSTRING", value: job.id }, + { type: "LUA_TSTRING", value: "XML" }, + { type: "LUA_TSTRING", value: "PNG" }, - // change this to 1920x when we finish the arbiter - { type: "LUA_TNUMBER", value: "420" }, - { type: "LUA_TNUMBER", value: "420" }, + // change this to 1920x when we finish the arbiter + { type: "LUA_TNUMBER", value: "420" }, + { type: "LUA_TNUMBER", value: "420" }, - { type: "LUA_TSTRING", value: "https://economy.ittblox.gay" }, - { type: "LUA_TNUMBER", value: "2555" }, - ], - }, - }) + { type: "LUA_TSTRING", value: "https://economy.ittblox.gay" }, + { type: "LUA_TNUMBER", value: request.params.id }, + ], + }, + }) + .catch((e) => false) + logger.info(`[${job.id}] Job closed`) await job.Stop() - return response.end(Buffer.from(result[0].OpenJobExResult.LuaValue[0].value, "base64")) + + if (result) return response.end(Buffer.from(result[0]?.OpenJobExResult?.LuaValue[0]?.value, "base64")) + else return response.status(500).end() }) module.exports = app diff --git a/src/routes/render/avatar.js b/src/routes/render/avatar.js index 44c9b1d..c3c161f 100644 --- a/src/routes/render/avatar.js +++ b/src/routes/render/avatar.js @@ -1,4 +1,3 @@ -const waitPort = require("wait-port") const { readFileSync } = require("fs") const { randomUUID } = require("crypto") const logger = require("../../lib/logger.js") @@ -7,40 +6,40 @@ const Job = require("../../lib/classes/Job.js") const express = require("express") const app = express.Router() -app.get("/", async (request, response) => { +app.get("/:id", async (request, response) => { let tempPort = 64990 const job = new Job(randomUUID(), tempPort) - await job.Start() - - const { open } = await waitPort({ host: "127.0.0.1", port: tempPort, timeout: 5000, output: "silent" }) - if (!open) { - logger.warn("Job could not be started because port is already taken. Closing job..") - job.Close() - return response.json(false) - } + const started = await job.Start() + if (!started) return response.status(500).json({ error: "RCCService failed to start" }) + logger.info(`[${job.id}] Job opened`) await job.CreateClient() - const result = await job.Open({ - name: job.id, - script: readFileSync(__dirname + "/../lua/headshot.lua", { encoding: "utf-8" }), - arguments: { - LuaValue: [ - { type: "LUA_TSTRING", value: job.id }, - { type: "LUA_TSTRING", value: "Headshot" }, - { type: "LUA_TSTRING", value: "PNG" }, + const result = await job + .Open({ + name: job.id, + script: readFileSync(__dirname + "/../../lua/bodyshot.lua", { encoding: "utf-8" }), + arguments: { + LuaValue: [ + { type: "LUA_TSTRING", value: job.id }, + { type: "LUA_TSTRING", value: "Bodyshot" }, + { type: "LUA_TSTRING", value: "PNG" }, - // change this to 1920x when we finish the arbiter - { type: "LUA_TNUMBER", value: "420" }, - { type: "LUA_TNUMBER", value: "420" }, + // change this to 1920x when we finish the arbiter + { type: "LUA_TNUMBER", value: "420" }, + { type: "LUA_TNUMBER", value: "420" }, - { type: "LUA_TSTRING", value: "https://economy.ittblox.gay" }, - { type: "LUA_TNUMBER", value: "1" }, - ], - }, - }) + { type: "LUA_TSTRING", value: "https://economy.ittblox.gay" }, + { type: "LUA_TNUMBER", value: request.params.id }, + ], + }, + }) + .catch((e) => false) + logger.info(`[${job.id}] Job closed`) await job.Stop() - return response.end(Buffer.from(result[0].OpenJobExResult.LuaValue[0].value, "base64")) + + if (result) return response.end(Buffer.from(result[0]?.OpenJobExResult?.LuaValue[0]?.value, "base64")) + else return response.status(500).end() }) module.exports = app