1
0
mirror of https://github.com/tobspr/shapez.io.git synced 2025-06-13 13:04:03 +00:00

Fix #1446 by modifying gulp/atlas2json.js

This commit is contained in:
Jasper Meggitt 2022-06-19 07:53:15 -04:00
parent 6a8e519c0a
commit 3714439add

View File

@ -4,21 +4,35 @@ const { readFileSync, readdirSync, writeFileSync } = require("fs");
const suffixToScale = { const suffixToScale = {
lq: "0.25", lq: "0.25",
mq: "0.5", mq: "0.5",
hq: "0.75" hq: "0.75",
}; };
function convert(srcDir) { function convert(srcDir) {
const full = resolve(srcDir); const fullPath = resolve(srcDir);
const srcFiles = readdirSync(full) const srcFiles = readdirSync(fullPath)
.filter(n => n.endsWith(".atlas")) .filter(n => n.endsWith(".atlas"))
.map(n => join(full, n)); .map(n => join(fullPath, n));
for (const atlas of srcFiles) { for (const atlas of srcFiles) {
console.log(`Processing: ${atlas}`); console.log(`Processing: ${atlas}`);
// Sections for different atlas images are broken up via an extra line break
const atlasSections = readFileSync(atlas, "utf-8")
.trim()
.split("\n\n");
console.log("Found " + atlasSections.length + " sections!");
// Perform the conversion for each section
for (const section of atlasSections) {
preformConversion(fullPath, section);
}
}
}
function preformConversion(pathPrefix, atlasData) {
// Read all text, split it into line array // Read all text, split it into line array
// and filter all empty lines // and filter all empty lines
const lines = readFileSync(atlas, "utf-8") const lines = atlasData
.split("\n") .split("\n")
.filter(n => n.trim()); .filter(n => n.trim());
@ -29,7 +43,7 @@ function convert(srcDir) {
// Read all metadata (supports only one page) // Read all metadata (supports only one page)
while (true) { while (true) {
const kv = lines.shift().split(":"); const kv = lines.shift().split(":");
if (kv.length != 2) { if (kv.length !== 2) {
lines.unshift(kv[0]); lines.unshift(kv[0]);
break; break;
} }
@ -45,7 +59,7 @@ function convert(srcDir) {
for (const line of lines) { for (const line of lines) {
if (!line.startsWith(" ")) { if (!line.startsWith(" ")) {
// New frame, convert previous if it exists // New frame, convert previous if it exists
if (current != null) { if (current !== null) {
let { name, rotate, xy, size, orig, offset, index } = current; let { name, rotate, xy, size, orig, offset, index } = current;
// Convert to arrays because Node.js doesn't // Convert to arrays because Node.js doesn't
@ -65,7 +79,7 @@ function convert(srcDir) {
x: xy[0], x: xy[0],
y: xy[1], y: xy[1],
w: size[0], w: size[0],
h: size[1] h: size[1],
}, },
// Whether image was rotated // Whether image was rotated
@ -77,19 +91,19 @@ function convert(srcDir) {
x: offset[0], x: offset[0],
y: (orig[1] - size[1]) - offset[1], y: (orig[1] - size[1]) - offset[1],
w: size[0], w: size[0],
h: size[1] h: size[1],
}, },
sourceSize: { sourceSize: {
w: orig[0], w: orig[0],
h: orig[1] h: orig[1],
} },
} };
} }
// Simple object that will hold other metadata // Simple object that will hold other metadata
current = { current = {
name: line name: line,
}; };
} else { } else {
// Read and set current image metadata // Read and set current image metadata
@ -99,7 +113,7 @@ function convert(srcDir) {
} }
const atlasSize = srcMeta.size.split(",").map(v => Number(v)); const atlasSize = srcMeta.size.split(",").map(v => Number(v));
const atlasScale = suffixToScale[atlas.match(/_(\w+)\.atlas$/)[1]]; const atlasScale = suffixToScale[image.match(/_([a-z]+)\d*\.png$/)[1]];
const result = JSON.stringify({ const result = JSON.stringify({
frames, frames,
@ -108,16 +122,17 @@ function convert(srcDir) {
format: srcMeta.format, format: srcMeta.format,
size: { size: {
w: atlasSize[0], w: atlasSize[0],
h: atlasSize[1] h: atlasSize[1],
},
scale: atlasScale.toString(),
}, },
scale: atlasScale.toString()
}
}); });
writeFileSync(atlas.replace(".atlas", ".json"), result, {
encoding: "utf-8" const dstFile = join(pathPrefix, image.replace(".png", ".json"));
writeFileSync(dstFile, result, {
encoding: "utf-8",
}); });
}
} }
if (require.main == module) { if (require.main == module) {