fix: new generator script and refactor

This commit is contained in:
trafficlunar 2024-12-22 15:58:02 +00:00
parent 3e00029466
commit 7f52cabfbd
13 changed files with 219 additions and 42 deletions

54
generator/scripts/data.js Normal file
View file

@ -0,0 +1,54 @@
const fs = require("fs");
const path = require("path");
const { getAverageColor } = require("fast-average-color-node");
const minecraftData = require("minecraft-data");
const versionRegex = require("../data/versions.json");
const INPUT = path.join(__dirname, "../blocks/");
const OUTPUT_DIR = path.join(__dirname, "../output/");
const OUTPUT = path.join(OUTPUT_DIR, "data.json");
const VERSION_DATA = minecraftData("1.13.2");
if (!fs.existsSync(OUTPUT_DIR)) fs.mkdirSync(OUTPUT_DIR);
const data = {};
(async () => {
const files = fs.readdirSync(INPUT);
for (const file of files) {
const filePath = path.join(INPUT, file);
const fileName = file.slice(0, -4);
const color = await getAverageColor(filePath);
const nameRegex = ["_top", "_side", "_front", "_back"];
const pattern = new RegExp(nameRegex.join("|"), "g");
const blockName = fileName.replace(pattern, "");
function getDataBlockProperty(property) {
return VERSION_DATA.blocksByName[blockName] ? VERSION_DATA.blocksByName[blockName][property] : "REPLACE_ME_REPLACE_ME_REPLACE_ME_REPLACE_ME";
}
function getVersion() {
for (const key of Object.keys(versionRegex)) {
if (blockName.includes(key)) {
return versionRegex[key];
}
}
return "REPLACE_ME_REPLACE_ME_REPLACE_ME_REPLACE_ME";
}
data[fileName] = {
name: VERSION_DATA.blocksByName[fileName] ? VERSION_DATA.blocksByName[fileName].displayName : "REPLACE_ME_REPLACE_ME_REPLACE_ME_REPLACE_ME",
version: getVersion(),
id: [getDataBlockProperty("name"), getDataBlockProperty("id")],
color: [color.value[0], color.value[1], color.value[2], color.value[3]],
};
}
fs.writeFileSync(OUTPUT, JSON.stringify(data, null, 4));
console.log("Done!");
})();

View file

@ -0,0 +1,2 @@
#!/bin/bash
rm ../blocks/*

View file

@ -0,0 +1,33 @@
const fs = require("fs");
const path = require("path");
const regex = require("../data/regex.json");
const INPUT = path.join(__dirname, "../blocks/");
function isBlacklisted(fileName) {
if (!fileName.endsWith(".png")) return true;
return regex.some((pattern) => {
const regex = new RegExp(pattern);
return regex.test(fileName);
});
}
fs.readdir(INPUT, (err, files) => {
if (err) throw err;
files.forEach((file) => {
const filePath = path.join(INPUT, file);
if (isBlacklisted(file)) {
fs.unlink(filePath, (err) => {
if (err) {
console.error(`Error deleting file ${file}:`, err);
} else {
console.log(`Deleted file: ${file}`);
}
});
}
});
});

View file

@ -0,0 +1,27 @@
const fs = require("fs");
const path = require("path");
const sharp = require("sharp");
const INPUT = path.join(__dirname, "../blocks/");
(async () => {
const files = fs.readdirSync(INPUT);
for (const file of files) {
const filePath = path.join(INPUT, file);
const image = sharp(filePath);
const metadata = await image.metadata();
if (metadata.height > 16) {
await image
.extract({ top: 0, left: 0, width: 16, height: 16 })
.resize(16, 16)
.toBuffer()
.then((buffer) => {
fs.writeFileSync(filePath, buffer);
});
}
}
console.log("Done!");
})();

View file

@ -0,0 +1,15 @@
const fs = require("fs");
const path = require("path");
const spritesheet = require("spritesheet-js");
const INPUT = path.join(__dirname, "../blocks/");
const OUTPUT = path.join(__dirname, "../output/");
if (!fs.existsSync(OUTPUT)) fs.mkdirSync(OUTPUT);
spritesheet(path.join(INPUT, "*.png"), { format: "pixi.js", path: OUTPUT }, function (err) {
if (err) throw err;
console.log("Done!");
});