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/example.env b/example.env index f19971d..795de3a 100644 --- a/example.env +++ b/example.env @@ -1,9 +1,11 @@ +PORT= RCCSERVICE= ARBITER_TOKEN= BASE_URL=https://crapblox.cf -RENDER_FORMAT=PNG +ARBITER_TOKEN= +ARBITER_KEY= RENDER_USER_WIDTH=420 RENDER_USER_HEIGHT=420 diff --git a/src/index.js b/src/index.js index eff1167..b9d6f0b 100644 --- a/src/index.js +++ b/src/index.js @@ -6,13 +6,11 @@ const logger = require("./lib/logger.js") if (process.platform == "linux") logger.warn("Game hosting might not be fully compatible with Linux") -global.games = new Map() - -setInterval(() => { - global.games.forEach(async (value, key) => { - if (!(await value.Running())) value.Stop() - }) -}, 15000) +app.use(({ query }, response, next) => { + if (!query.key) return response.status(400).json({ error: "Missing key in query" }) + if (query.key !== process.env.ARBITER_KEY) return response.status(403).json({ error: "Incorrect key in query" }) + next() +}) app.use("/game/start", require("./routes/game/start.js")) app.use("/game/stop", require("./routes/game/stop.js")) @@ -22,17 +20,19 @@ app.use("/game/status", require("./routes/game/status.js")) app.use("/game/execute", require("./routes/game/execute.js")) app.use("/render/asset", require("./routes/render/asset.js")) -//app.use("/render/game", require("./routes/render/game.js")) -//app.use("/render/texture", require("./routes/render/texture.js")) +app.use("/render/game", require("./routes/render/game.js")) +app.use("/render/texture", require("./routes/render/texture.js")) app.use("/render/user", require("./routes/render/user.js")) app.use("/render/texture", require("./routes/render/texture.js")) app.use("*", require("./routes/index.js")) -app.listen(process.env.PORT || 64989, () => { - logger.boot(`Listening on http://127.0.0.1:${process.env.PORT || 64989}/`) -}) +process.on("uncaughtException", (err) => logger.error(err.message)) +global.games = new Map() +setInterval(() => { + global.games.forEach(async (value, key) => { + if (!(await value.Running())) value.Stop() + }) +}, 15000) -process.on("uncaughtException", (err) => { - logger.error(err.message) -}) +app.listen(process.env.PORT, () => logger.boot(`Listening on http://127.0.0.1:${process.env.PORT}/`)) diff --git a/src/lib/classes/GameJob.js b/src/lib/classes/GameJob.js index 8025e38..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() { @@ -11,10 +12,6 @@ class GameJob extends Job { StartGame(id) { return new Promise(async (resolve, reject) => { - const response = await axios(`${process.env.BASE_URL}/API/Game/${id}?t=${process.env.ARBITER_TOKEN}`).catch((_) => reject(_)) - const { server_token, server_port, server_owner_id } = response.data - - this.id = server_token this.placeId = id const started = await this.Start() @@ -23,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" }), @@ -34,13 +33,12 @@ class GameJob extends Job { { type: "LUA_TSTRING", value: process.env.BASE_URL }, { type: "LUA_TNUMBER", value: id }, - { type: "LUA_TNUMBER", value: server_port }, - { type: "LUA_TNUMBER", value: server_owner_id }, + { type: "LUA_TNUMBER", value: port }, ], }, }).catch((e) => reject(e)) - resolve() + resolve(port) }) } diff --git a/src/lib/classes/Job.js b/src/lib/classes/Job.js index 54778b2..4757f84 100644 --- a/src/lib/classes/Job.js +++ b/src/lib/classes/Job.js @@ -5,7 +5,7 @@ const RCCService = require("./RCCService.js") const logger = require("../logger.js") class Job extends RCCService { - constructor({ id = randomUUID(), expirationInSeconds = 10, category = 0, cores = 1 } = {}) { + constructor({ id = randomUUID(), expirationInSeconds = 60, category = 0, cores = 1 } = {}) { super() this.id = id this.expirationInSeconds = expirationInSeconds diff --git a/src/lib/classes/RenderJob.js b/src/lib/classes/RenderJob.js index 4da4cf9..0d14dd3 100644 --- a/src/lib/classes/RenderJob.js +++ b/src/lib/classes/RenderJob.js @@ -1,5 +1,5 @@ const { readFile } = require("fs/promises") -const chalk = require('chalk') +const chalk = require("chalk") const Job = require("./Job.js") const logger = require("../logger.js") @@ -31,7 +31,7 @@ class RenderJob extends Job { { type: "LUA_TSTRING", value: this.id }, { type: "LUA_TSTRING", value: "Headshot" }, - { type: "LUA_TSTRING", value: process.env.RENDER_FORMAT }, + { type: "LUA_TSTRING", value: "PNG" }, { type: "LUA_TNUMBER", value: process.env.RENDER_USER_WIDTH }, { type: "LUA_TNUMBER", value: process.env.RENDER_USER_HEIGHT }, @@ -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 } @@ -70,7 +72,7 @@ class RenderJob extends Job { { type: "LUA_TSTRING", value: this.id }, { type: "LUA_TSTRING", value: "Bodyshot" }, - { type: "LUA_TSTRING", value: three_d ? "OBJ" : process.env.RENDER_FORMAT }, + { type: "LUA_TSTRING", value: three_d ? "OBJ" : "PNG" }, { type: "LUA_TNUMBER", value: process.env.RENDER_USER_WIDTH }, { type: "LUA_TNUMBER", value: process.env.RENDER_USER_HEIGHT }, @@ -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 } @@ -110,7 +114,7 @@ class RenderJob extends Job { { type: "LUA_TSTRING", value: this.id }, { type: "LUA_TSTRING", value: "Asset" }, - { type: "LUA_TSTRING", value: three_d ? "OBJ" : process.env.RENDER_FORMAT }, + { type: "LUA_TSTRING", value: three_d ? "OBJ" : "PNG" }, { type: "LUA_TNUMBER", value: process.env.RENDER_ASSET_WIDTH }, { type: "LUA_TNUMBER", value: process.env.RENDER_ASSET_HEIGHT }, @@ -125,16 +129,13 @@ 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 } async RenderPlace(id) { - const response = await axios(`${process.env.BASE_URL}/API/Game/${id}?t=${process.env.ARBITER_TOKEN}`).catch((_) => reject(_)) - const { server_token, server_port, server_owner_id } = response.data - - this.id = server_token - const running = this.started if (!running) { const started = await this.Start() @@ -153,19 +154,22 @@ class RenderJob extends Job { { type: "LUA_TSTRING", value: this.id }, { type: "LUA_TSTRING", value: "Place" }, - { type: "LUA_TSTRING", value: process.env.RENDER_FORMAT }, + { type: "LUA_TSTRING", value: "PNG" }, { type: "LUA_TNUMBER", value: process.env.RENDER_PLACE_WIDTH }, { type: "LUA_TNUMBER", value: process.env.RENDER_PLACE_HEIGHT }, { type: "LUA_TSTRING", value: process.env.BASE_URL }, { type: "LUA_TNUMBER", value: id }, + { type: "LUA_TSTRING", value: process.env.ARBITER_TOKEN }, ], }, }).catch((e) => false) 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 } @@ -191,7 +195,7 @@ class RenderJob extends Job { { type: "LUA_TSTRING", value: this.id }, { type: "LUA_TSTRING", value: "Texture" }, - { type: "LUA_TSTRING", value: process.env.RENDER_FORMAT }, + { type: "LUA_TSTRING", value: "PNG" }, { type: "LUA_TNUMBER", value: process.env.RENDER_USER_WIDTH }, { type: "LUA_TNUMBER", value: process.env.RENDER_USER_HEIGHT }, @@ -204,6 +208,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/lib/randport.js b/src/lib/randport.js index 23bd478..0da19fa 100644 --- a/src/lib/randport.js +++ b/src/lib/randport.js @@ -13,8 +13,8 @@ exports.tcp = () => { exports.udp = () => { return new Promise((resolve) => { - const server = dgram.createSocket() - server.bind(0, () => { + const server = dgram.createSocket("udp4") + server.bind(Math.random() * (60_000 - 50_000) + 50_000, () => { const port = server.address().port server.close((err) => resolve(port)) }) diff --git a/src/lua/gameserver.lua b/src/lua/gameserver.lua index ce3ea7c..e866833 100644 --- a/src/lua/gameserver.lua +++ b/src/lua/gameserver.lua @@ -1,13 +1,10 @@ -local jobId, type, baseUrl, placeId, port, owner = ... +local jobId, type, baseUrl, placeId, port = ... ------------------- UTILITY FUNCTIONS -------------------------- - function waitForChild(parent, childName) while true do local child = parent:findFirstChild(childName) - if child then - return child - end + if child then return child end parent.ChildAdded:wait() end end @@ -66,20 +63,16 @@ pcall(function() settings().Diagnostics:LegacyScriptMode() end) -----------------------------------START GAME SHARED SCRIPT------------------------------ -local assetId = placeId - local scriptContext = game:GetService("ScriptContext") pcall(function() scriptContext:AddStarterScript(37801172) end) scriptContext.ScriptsDisabled = true -game:SetPlaceID(assetId, false) +game:SetPlaceID(placeId, false) game:GetService("ChangeHistoryService"):SetEnabled(false) -game:SetCreatorId(owner, Enum.CreatorType.User) -game:GetService("HttpService").HttpEnabled = true local ns = game:GetService("NetworkServer") -if baseUrl~=nil then +if baseUrl ~= nil then pcall(function() game:GetService("Players"):SetAbuseReportUrl(baseUrl .. "/AbuseReport/InGameChatHandler.ashx") end) pcall(function() game:GetService("ScriptInformationProvider"):SetAssetUrl(baseUrl .. "/Asset/") end) pcall(function() game:GetService("ContentProvider"):SetBaseUrl(baseUrl .. "/") end) @@ -99,29 +92,16 @@ end settings().Diagnostics.LuaRamLimit = 0 game:GetService("Players").PlayerAdded:connect(function(player) - keepAlive() print("Player " .. player.userId .. " added") - - player.CharacterAdded:connect(function(c) - game:GetObjects("rbxasset://fonts/characterCameraScript.rbxmx")[1].Parent = c - game:GetObjects("rbxasset://fonts/characterControlScript.rbxmx")[1].Parent = c - - for i,v in pairs(c:GetChildren()) do - print(v.Name) - end - - print(c.Animate.Source) - end) end) game:GetService("Players").PlayerRemoving:connect(function(player) - keepAlive(player) print("Player " .. player.userId .. " leaving") end) -if placeId~=nil and baseUrl~=nil then +if placeId ~= nil and baseUrl ~= nil then wait() - game:Load(baseUrl .. "/thumbs/staticimage?r=" .. jobId) + game:Load(baseUrl .. "/asset/?id=" .. placeId) end ------------------------------ RENEW GAME JOB SERVICE ------------------------------- @@ -144,4 +124,6 @@ ns:Start(port) scriptContext:SetTimeout(10) scriptContext.ScriptsDisabled = false +------------------------------END START GAME SHARED SCRIPT-------------------------- + game:GetService("RunService"):Run() diff --git a/src/routes/game/execute.js b/src/routes/game/execute.js index d740057..36e84a8 100644 --- a/src/routes/game/execute.js +++ b/src/routes/game/execute.js @@ -6,8 +6,8 @@ const GameJob = require("../../lib/classes/GameJob.js") app.use(express.json()) -app.post("/:token", async (request, response) => { - const game = global.games.get(request.params.token) +app.post("/:id", async (request, response) => { + const game = global.games.get(request.params.id) if (!game) return response.status(404).json({ error: "Game is not running" }) const { script } = request.body diff --git a/src/routes/game/renew.js b/src/routes/game/renew.js index ca3f580..68e6538 100644 --- a/src/routes/game/renew.js +++ b/src/routes/game/renew.js @@ -3,8 +3,8 @@ const app = express.Router() const GameJob = require("../../lib/classes/GameJob.js") -app.get("/:token/:expire", async (request, response) => { - const game = global.games.get(request.params.token) +app.get("/:id/:expire", async (request, response) => { + const game = global.games.get(request.params.id) if (!game) return response.status(404).json({ error: "Game is not running" }) await game.RenewLease(request.params.expire) diff --git a/src/routes/game/running.js b/src/routes/game/running.js index a33b8df..a1f97a0 100644 --- a/src/routes/game/running.js +++ b/src/routes/game/running.js @@ -3,8 +3,8 @@ const app = express.Router() const GameJob = require("../../lib/classes/GameJob.js") -app.get("/:token", async (request, response) => { - const game = global.games.get(request.params.token) +app.get("/:id", async (request, response) => { + const game = global.games.get(request.params.id) if (!game) return response.json(false) const running = await game.Running() diff --git a/src/routes/game/start.js b/src/routes/game/start.js index 2bcc564..0049888 100644 --- a/src/routes/game/start.js +++ b/src/routes/game/start.js @@ -3,26 +3,16 @@ const app = express.Router() const GameJob = require("../../lib/classes/GameJob.js") -function getGameById(id) { - let game - - global.games.forEach((value, key) => { - if (value.placeId == id) game = value - }) - - return game -} - app.get("/:id", async (request, response) => { - const game = global.games.get(getGameById(request.params.id)?.id) + const game = global.games.get(request.params.id) if (game) return response.status(400).json({ error: "Game is running" }) const job = new GameJob() const result = await job.StartGame(request.params.id).catch((_) => _) - global.games.set(job.id, job) + global.games.set(request.params.id, job) job.proc.once("exit", () => { - global.games.delete(job.id) + global.games.delete(request.params.id) }) return response.json({ success: true }) diff --git a/src/routes/game/status.js b/src/routes/game/status.js index 915bf71..7c4aeed 100644 --- a/src/routes/game/status.js +++ b/src/routes/game/status.js @@ -3,8 +3,8 @@ const app = express.Router() const GameJob = require("../../lib/classes/GameJob.js") -app.get("/:token", async (request, response) => { - const game = global.games.get(request.params.token) +app.get("/:id", async (request, response) => { + const game = global.games.get(request.params.id) if (!game) return response.status(404).json({ error: "Game is not running" }) const status = await game.GetStatus() diff --git a/src/routes/game/stop.js b/src/routes/game/stop.js index b82cd49..be676b5 100644 --- a/src/routes/game/stop.js +++ b/src/routes/game/stop.js @@ -3,8 +3,8 @@ const app = express.Router() const GameJob = require("../../lib/classes/GameJob.js") -app.get("/:token", async (request, response) => { - const game = global.games.get(request.params.token) +app.get("/:id", async (request, response) => { + const game = global.games.get(request.params.id) if (!game) return response.status(404).json({ error: "Game is not running" }) game.Stop() 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..a2e4c51 100644 --- a/src/routes/render/game.js +++ b/src/routes/render/game.js @@ -4,11 +4,13 @@ const app = express.Router() const RenderJob = require("../../lib/classes/RenderJob.js") app.get("/:id", async (request, response) => { + const { params, query } = request 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) + const game = await job.RenderPlace(params.id).catch((_) => _) + if (game?.message) return response.status(500).json({ error: game.message }) + + return response.end(game) }) 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