-
Notifications
You must be signed in to change notification settings - Fork 0
/
SkinRender.js
69 lines (56 loc) · 2.19 KB
/
SkinRender.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
const fs = require("fs");
const path = require("path");
const { Skin, ColorCode } = require("teeworlds-utilities")
const fastify = require("fastify")({
logger: true,
});
const SKIN_PATH = process.env.SKIN_PATH || path.join(__dirname, "skins");
const OUTPUT_PATH = process.env.OUTPUT_PATH || path.join(__dirname, "render");
const BASEURL = `https://${process.env.BASEURL || '127.0.0.1'}/api/skins/render/`;
class SkinManager {
static async loadSkin(skinPath) {
const skin = new Skin();
return await skin.load(skinPath);
}
static async saveGameskinPart(skin, body, outPath) {
if (body !== null) {
try {
skin.colorBody(new ColorCode(body))
} catch (err) {
// console.log(err)
}
}
skin
.render()
.saveRenderAs(outPath);
}
}
// Define the API endpoint
fastify.get("/api/render", async (request, reply) => {
try {
const { skin: skinName, body = null} = request.query;
if (!skinName) {
return { err: "not_skin" };
}
const skinPath = path.join(SKIN_PATH, `${skinName}.png`);
if (!fs.existsSync(skinPath)) {
return { err: "skin not found" };
}
const outSkinName = `${skinName}_${body}.png`;
const outSkinPath = path.join(OUTPUT_PATH, outSkinName);
if (fs.existsSync(outSkinPath)) {
return { url: `${BASEURL}${outSkinName.replace(" ", "%20")}` };
}
const skin = await SkinManager.loadSkin(skinPath);
await SkinManager.saveGameskinPart(skin, body, outSkinPath);
reply.type("application/json").code(200);
return { url: `${BASEURL}${outSkinName.replace(" ", "%20")}` };
} catch (error) {
console.error("Error:", error);
}
});
// Start the server
fastify.listen({ host: "0.0.0.0", port: Number(process.env.PORT || 1541) }, (err, address) => {
if (err) throw err;
console.log(address);
});