From 0784446a61ab3205221c5cad69ebb54e068dc8ef Mon Sep 17 00:00:00 2001
From: I-Have-An-Issue <34550332+I-Have-An-Issue@users.noreply.github.com>
Date: Mon, 20 Feb 2023 04:12:51 -0500
Subject: [PATCH 1/3] Document, better endpoints
---
README.md | 96 ++++++++++++++++++++++++++++++++++++
src/lib/classes/RenderJob.js | 10 ++++
src/routes/render/asset.js | 26 ++++------
src/routes/render/game.js | 14 ++++--
src/routes/render/texture.js | 11 +----
src/routes/render/user.js | 47 +++++++++---------
6 files changed, 149 insertions(+), 55 deletions(-)
diff --git a/README.md b/README.md
index 1a74536..632247f 100644
--- a/README.md
+++ b/README.md
@@ -7,3 +7,99 @@ It comes preloaded with some Lua scripts made by kinery and jackd900.
You **will** have to replace/modify these scripts when implementing for your own projects.
Set your desired settings in `.env.example`, then rename it to `.env`.
+
+## Routes
+
+### GET /render/asset/:id
+
+
+200 OK
+
+```
+iVBORw0KGgoAAAANSUhEUgAAAtAAAALQCAYAAAC...
+```
+
+
+
+### GET /render/asset/3d/:id
+
+
+200 OK
+
+```json
+{
+ "camera": {
+ "position": { "x": 0, "y": 0, "z": 0 },
+ "direction": { "x": 0, "y": 0, "z": 0 }
+ },
+ "AABB": {
+ "min": { "x": 0, "y": 0, "z": 0 },
+ "max": { "x": 0, "y": 0, "z": 0 }
+ },
+ "files": {
+ "scene.obj": { "content": "..." },
+ "scene.mtl": { "content": "..." },
+ "Handle1Tex.png": { "content": "..." }
+ }
+}
+```
+
+
+
+### GET /render/texture/:id
+
+
+200 OK
+
+```
+iVBORw0KGgoAAAANSUhEUgAAAtAAAALQCAYAAAC...
+```
+
+
+
+### GET /render/user/headshot/:id
+
+
+200 OK
+
+```
+iVBORw0KGgoAAAANSUhEUgAAAtAAAALQCAYAAAC...
+```
+
+
+
+### GET /render/user/bodyshot/:id
+
+
+200 OK
+
+```
+iVBORw0KGgoAAAANSUhEUgAAAtAAAALQCAYAAAC...
+```
+
+
+
+### GET /render/user/3d/:id
+
+
+200 OK
+
+```json
+{
+ "camera": {
+ "position": { "x": 0, "y": 0, "z": 0 },
+ "direction": { "x": 0, "y": 0, "z": 0 }
+ },
+ "AABB": {
+ "min": { "x": 0, "y": 0, "z": 0 },
+ "max": { "x": 0, "y": 0, "z": 0 }
+ },
+ "files": {
+ "scene.obj": { "content": "..." },
+ "scene.mtl": { "content": "..." },
+ "Handle1Tex.png": { "content": "..." }
+ }
+}
+```
+
+
diff --git a/src/lib/classes/RenderJob.js b/src/lib/classes/RenderJob.js
index 0439098..78f0139 100644
--- a/src/lib/classes/RenderJob.js
+++ b/src/lib/classes/RenderJob.js
@@ -44,6 +44,8 @@ class RenderJob extends Job {
logger.info(`${chalk.gray(`${chalk.gray(`[${this.id}]`)}`)} Headshot RenderJob finished for ${id}`)
+ this.Stop()
+
if (!result) return false
return result[0]?.OpenJobExResult?.LuaValue[0]?.value
}
@@ -84,6 +86,8 @@ class RenderJob extends Job {
if (three_d) logger.info(`${chalk.gray(`${chalk.gray(`[${this.id}]`)}`)} 3D Bodyshot RenderJob finished for ${id}`)
else logger.info(`${chalk.gray(`${chalk.gray(`[${this.id}]`)}`)} Bodyshot RenderJob finished for ${id}`)
+ this.Stop()
+
if (!result) return false
return result[0]?.OpenJobExResult?.LuaValue[0]?.value
}
@@ -125,6 +129,8 @@ class RenderJob extends Job {
if (three_d) logger.info(`${chalk.gray(`${chalk.gray(`[${this.id}]`)}`)} 3D Asset RenderJob finished for ${id}`)
else logger.info(`${chalk.gray(`${chalk.gray(`[${this.id}]`)}`)} Asset RenderJob finished for ${id}`)
+ this.Stop()
+
if (!result) return false
return result[0]?.OpenJobExResult?.LuaValue[0]?.value
}
@@ -166,6 +172,8 @@ class RenderJob extends Job {
logger.info(`${chalk.gray(`${chalk.gray(`[${this.id}]`)}`)} Place RenderJob finished for ${id}`)
+ this.Stop()
+
if (!result) return false
return result[0]?.OpenJobExResult?.LuaValue[0]?.value
}
@@ -204,6 +212,8 @@ class RenderJob extends Job {
logger.info(`[${this.id}] Headshot RenderJob finished for ${id}`)
+ this.Stop()
+
if (!result) return false
return result[0]?.OpenJobExResult?.LuaValue[0]?.value
}
diff --git a/src/routes/render/asset.js b/src/routes/render/asset.js
index 9fc500e..24aea09 100644
--- a/src/routes/render/asset.js
+++ b/src/routes/render/asset.js
@@ -6,27 +6,21 @@ const RenderJob = require("../../lib/classes/RenderJob.js")
app.get("/:id", async (request, response) => {
const { params, query } = request
const job = new RenderJob()
- let body = {}
const asset = await job.RenderAsset(params.id).catch((_) => _)
- if (asset?.message) {
- job.Stop()
- return response.status(500).json({ error: asset.message })
- }
- body.asset = asset
+ if (asset?.message) return response.status(500).json({ error: asset.message })
- if (query.three_d) {
- const three_d = await job.RenderAsset(params.id, true).catch((_) => _)
- if (three_d?.message) {
- job.Stop()
- return response.status(500).json({ error: three_d.message })
- }
- body.three_d = three_d
- }
+ return response.end(asset)
+})
- job.Stop()
+app.get("/:id/3d", async (request, response) => {
+ const { params, query } = request
+ const job = new RenderJob()
- return response.json(body)
+ const three_d = await job.RenderAsset(params.id, true).catch((_) => _)
+ if (three_d?.message) return response.status(500).json({ error: three_d.message })
+
+ return response.json(JSON.parse(three_d))
})
module.exports = app
diff --git a/src/routes/render/game.js b/src/routes/render/game.js
index 7616772..5962c24 100644
--- a/src/routes/render/game.js
+++ b/src/routes/render/game.js
@@ -3,12 +3,16 @@ const app = express.Router()
const RenderJob = require("../../lib/classes/RenderJob.js")
-app.get("/:id", async (request, response) => {
- const job = new RenderJob()
- const result = await job.RenderPlace(request.params.id, process.env.RENDER_BASE64).catch((_) => _)
+// app.get("/:id", async (request, response) => {
+// const job = new RenderJob()
+// const result = await job.RenderPlace(request.params.id, process.env.RENDER_BASE64).catch((_) => _)
- if (result?.message) return response.status(500).json({ error: result.message })
- else return response.end(result)
+// if (result?.message) return response.status(500).json({ error: result.message })
+// else return response.end(result)
+// })
+
+app.get("*", (request, response) => {
+ response.status(501).end("Not Implemented")
})
module.exports = app
diff --git a/src/routes/render/texture.js b/src/routes/render/texture.js
index 5d1e7ad..6bd0fc7 100644
--- a/src/routes/render/texture.js
+++ b/src/routes/render/texture.js
@@ -6,18 +6,11 @@ const RenderJob = require("../../lib/classes/RenderJob.js")
app.get("/:id", async (request, response) => {
const { params, query } = request
const job = new RenderJob()
- let body = {}
const texture = await job.RenderTexture(params.id).catch((_) => _)
- if (texture?.message) {
- job.Stop()
- return response.status(500).json({ error: texture.message })
- }
- body.texture = texture
+ if (texture?.message) return response.status(500).json({ error: texture.message })
- job.Stop()
-
- return response.json(body)
+ return response.end(texture)
})
module.exports = app
diff --git a/src/routes/render/user.js b/src/routes/render/user.js
index 76f7376..b799dc9 100644
--- a/src/routes/render/user.js
+++ b/src/routes/render/user.js
@@ -3,37 +3,34 @@ const app = express.Router()
const RenderJob = require("../../lib/classes/RenderJob.js")
-app.get("/:id", async (request, response) => {
- const { params, query } = request
+app.get("/:id/bodyshot", async (request, response) => {
+ const { params } = request
const job = new RenderJob()
- let body = {}
-
- const headshot = await job.RenderHeadshot(params.id).catch((_) => _)
- if (headshot?.message) {
- job.Stop()
- return response.status(500).json({ error: headshot.message })
- }
- body.headshot = headshot
const bodyshot = await job.RenderBodyshot(params.id).catch((_) => _)
- if (bodyshot?.message) {
- job.Stop()
- return response.status(500).json({ error: bodyshot.message })
- }
- body.bodyshot = bodyshot
+ if (bodyshot?.message) return response.status(500).json({ error: bodyshot.message })
- if (query.three_d) {
- const three_d = await job.RenderBodyshot(params.id, true).catch((_) => _)
- if (three_d?.message) {
- job.Stop()
- return response.status(500).json({ error: three_d.message })
- }
- body.three_d = three_d
- }
+ return response.end(bodyshot)
+})
- job.Stop()
+app.get("/:id/headshot", async (request, response) => {
+ const { params } = request
+ const job = new RenderJob()
- return response.json(body)
+ const headshot = await job.RenderHeadshot(params.id).catch((_) => _)
+ if (headshot?.message) return response.status(500).json({ error: headshot.message })
+
+ return response.end(headshot)
+})
+
+app.get("/:id/3d", async (request, response) => {
+ const { params, query } = request
+ const job = new RenderJob()
+
+ const three_d = await job.RenderBodyshot(params.id, true).catch((_) => _)
+ if (three_d?.message) return response.status(500).json({ error: three_d.message })
+
+ return response.json(JSON.parse(three_d))
})
module.exports = app
From 173a3cc271a5b8987b94dd8ce865ce291cf329a7 Mon Sep 17 00:00:00 2001
From: I-Have-An-Issue <34550332+I-Have-An-Issue@users.noreply.github.com>
Date: Mon, 20 Feb 2023 04:24:49 -0500
Subject: [PATCH 2/3] Gameserver should get a random UDP port
---
src/lib/classes/GameJob.js | 7 +++++--
1 file changed, 5 insertions(+), 2 deletions(-)
diff --git a/src/lib/classes/GameJob.js b/src/lib/classes/GameJob.js
index ef5a24c..1cc4528 100644
--- a/src/lib/classes/GameJob.js
+++ b/src/lib/classes/GameJob.js
@@ -3,6 +3,7 @@ const { readFile } = require("fs/promises")
const Job = require("./Job.js")
const logger = require("../logger.js")
+const randport = require("../randport.js")
class GameJob extends Job {
constructor() {
@@ -19,6 +20,8 @@ class GameJob extends Job {
logger.info(`[${this.id}] GameJob started for ${id}`)
+ const port = await randport.udp()
+
this.OpenJobEx({
name: this.id,
script: await readFile(__dirname + "/../../lua/gameserver.lua", { encoding: "utf-8" }),
@@ -30,12 +33,12 @@ class GameJob extends Job {
{ type: "LUA_TSTRING", value: process.env.BASE_URL },
{ type: "LUA_TNUMBER", value: id },
- { type: "LUA_TNUMBER", value: 50001 },
+ { type: "LUA_TNUMBER", value: port },
],
},
}).catch((e) => reject(e))
- resolve()
+ resolve(port)
})
}
From 7da5877d58ee609a3fd1db57bdcd6a262f51d56d Mon Sep 17 00:00:00 2001
From: I-Have-An-Issue <34550332+I-Have-An-Issue@users.noreply.github.com>
Date: Mon, 20 Feb 2023 04:25:40 -0500
Subject: [PATCH 3/3] randport.udp() should get a random port between 60k-50k
---
src/lib/randport.js | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/src/lib/randport.js b/src/lib/randport.js
index 23bd478..34dfa5f 100644
--- a/src/lib/randport.js
+++ b/src/lib/randport.js
@@ -14,7 +14,7 @@ exports.tcp = () => {
exports.udp = () => {
return new Promise((resolve) => {
const server = dgram.createSocket()
- server.bind(0, () => {
+ server.bind(Math.random() * (60_000 - 50_000) + 50_000, () => {
const port = server.address().port
server.close((err) => resolve(port))
})