MSE-MC/packgen.js

219 lines
8.0 KiB
JavaScript
Raw Permalink Normal View History

2024-04-21 02:59:22 +12:00
#!/usr/bin/env node
const MSE = require("mse-translator");
const https = require("https");
const fs = require("fs");
const archiver = require("archiver");
var argv = require("yargs/yargs")(process.argv.slice(2))
.usage("\nMSE Minecraft language-pack generator \n\nUsage: $0 [options]")
.help("help")
.alias("help", "h")
.version("version", "1.0.1")
.alias("version", "V")
.options({
descriptivefile: {
alias: "f",
description: 'descriptive output E.g. "en_gb-v1.19.1-en_MSE"',
requiresArg: false,
required: false,
type: "boolean",
},
lang: {
alias: "l",
description: "<language> base language for pack generation",
requiresArg: true,
required: false,
type: "string",
},
2024-09-02 22:16:34 +12:00
verid: {
alias: "v",
description: "<version-id> base version for pack generation",
requiresArg: true,
required: false,
type: "string",
},
releaseOnly: {
alias: "R",
description: "use only the *latest* release version",
requiresArg: false,
required: false,
type: "boolean",
},
2024-04-21 02:59:22 +12:00
manifest: {
alias: "m",
description: "<url> custom manifest endpoint",
requiresArg: true,
required: false,
type: "string",
},
resources: {
alias: "r",
description: "<url> custom resources endpoint",
requiresArg: true,
required: false,
type: "string",
},
2024-09-02 22:16:34 +12:00
debug: {
alias: "d",
description: "verbose debug output",
requiresArg: false,
required: false,
type: "boolean",
},
2024-04-21 02:59:22 +12:00
})
.parse();
2024-04-01 20:48:59 +13:00
2024-09-02 22:16:34 +12:00
if (argv.debug) {console.log(argv)}
if (argv.verid && argv.releaseOnly) {
console.log('Ignoreing specified version and using latest release. (remove "releaseOnly" to use specified version)')
argv.verid = undefined
}
2024-04-16 21:11:55 +12:00
const urlReq = function (reqUrl, options, cb) {
2024-04-21 02:59:22 +12:00
if (typeof options === "function") {
2024-04-19 21:46:39 +12:00
cb = options;
options = {};
} // incase no options passed in
https
.get(reqUrl, (res) => {
2024-04-21 02:59:22 +12:00
let body = "";
res.on("data", (chunk) => {
2024-04-19 21:46:39 +12:00
body += chunk;
});
2024-04-21 02:59:22 +12:00
res.on("end", () => {
2024-04-19 21:46:39 +12:00
try {
cb(body, res);
} catch (error) {
console.error(error.message);
}
});
})
2024-04-21 02:59:22 +12:00
.on("error", (error) => {
2024-04-19 21:46:39 +12:00
console.error(error.message);
});
2024-04-16 21:11:55 +12:00
};
2024-04-01 20:48:59 +13:00
2024-04-21 02:59:22 +12:00
let piston =
argv.manifest ||
"https://piston-meta.mojang.com/mc/game/version_manifest_v2.json";
2024-04-01 20:48:59 +13:00
2024-04-16 21:11:55 +12:00
urlReq(piston, function (body, res) {
2024-04-19 21:46:39 +12:00
json = JSON.parse(body);
2024-09-02 22:16:34 +12:00
var version;
if (argv.releaseOnly) {
version = json.versions.filter(v => v.type === 'release')[0].id // latest type ("release") version
} else if (argv.verid) {
if (json.versions.filter(v => v.id === argv.verid ).length >= 1) { // specified ("argv.verid") version
console.log("Found metadata for",argv.verid)
version = argv.verid
} else {
console.log("No metadata found for",argv.verid,"are you sure this is a valid version id?")
process.exit(1);
}
} else {
version = json.versions[0].id; // latest type ("any") version
}
2024-04-21 02:59:22 +12:00
let resources =
argv.resources || "https://resources.download.minecraft.net/";
let baselang = argv.lang || "en_gb";
let filename =
(argv.descriptivefile && `${baselang}-v${version}-en_MSE`) || // descriptive file name
"Maoist-Standard-English"; // default
2024-04-19 21:46:39 +12:00
let mcmeta_template = [
{
text: `§cen_MSE§r\n`,
},
{
text: `§8(§f${baselang}§8)-<§f${version}§8>`,
},
];
2024-04-19 21:41:02 +12:00
2024-04-19 21:46:39 +12:00
console.log(`Using base language "${baselang}" from version (${version})`);
2024-04-19 21:41:02 +12:00
2024-09-02 22:16:34 +12:00
if (argv.debug) {console.log(version,json.versions.filter(v => v.id === version )[0].url)}
urlReq(json.versions.filter(v => v.id === version )[0].url, function (body, res) {
2024-04-19 21:46:39 +12:00
json = JSON.parse(body);
urlReq(json.assetIndex.url, function (body, res) {
json = JSON.parse(body);
2024-09-02 22:16:34 +12:00
if (json.objects[`minecraft/lang/${baselang}.json`] === undefined) {
console.log("This version has no/outdated language file (only ≥1.13 )")
process.exit(1);
}
2024-04-19 21:46:39 +12:00
hash = json.objects[`minecraft/lang/${baselang}.json`].hash;
urlReq(
resources + `${hash.substring(0, 2)}/${hash}`,
function (body, res) {
pack = JSON.parse(body);
var totals = {
total: Object.keys(pack).length,
translated: 0,
};
console.log(`Translating ${totals.total} strings`);
Object.keys(pack).forEach(function (key) {
var translated = MSE.translate(
pack[key]
2024-04-21 02:59:22 +12:00
.replace(/\%s/gi, "%_") //mc patch
.replace(/\$s/gi, "$_"),
2024-04-19 21:46:39 +12:00
)
2024-04-21 02:59:22 +12:00
.replace(/\%_/gi, "%s") //mc patch back
.replace(/\$_/gi, "$s");
2024-04-19 21:46:39 +12:00
if (translated != pack[key]) {
totals.translated = totals.translated + 1;
}
pack[key] = translated;
});
console.log(
`Translated ${totals.translated} out of ${totals.total} strings`,
);
2024-04-19 21:41:02 +12:00
2024-04-21 02:59:22 +12:00
var mcmeta = __dirname + "/template/pack.mcmeta";
2024-04-19 21:46:39 +12:00
var mcmeta_data = JSON.parse(fs.readFileSync(mcmeta));
2024-09-02 22:16:34 +12:00
var mcmeta_data_old = fs.readFileSync(mcmeta);
2024-04-19 21:46:39 +12:00
mcmeta_data.pack.description = mcmeta_template;
2024-04-01 20:48:59 +13:00
2024-04-19 21:46:39 +12:00
fs.writeFileSync(mcmeta, JSON.stringify(mcmeta_data));
fs.writeFile(
__dirname +
2024-04-21 02:59:22 +12:00
"/template/assets/minecraft/lang/en_mse.json",
2024-04-19 21:46:39 +12:00
JSON.stringify(pack, null, 4),
2024-04-21 02:59:22 +12:00
"utf8",
2024-04-19 21:46:39 +12:00
function (err) {
if (err) throw err;
console.log(
2024-04-21 02:59:22 +12:00
"en_mse.json generated, creating pack and compressing.",
2024-04-19 21:46:39 +12:00
);
2024-09-02 22:16:34 +12:00
// Make sure resourcepacks folder exists
if (!fs.existsSync(__dirname +"/resourcepacks/")) {fs.mkdirSync(__dirname +"/resourcepacks/")}
2024-04-19 21:46:39 +12:00
const output = fs.createWriteStream(
2024-09-02 22:16:34 +12:00
__dirname + `/resourcepacks/${filename}.zip`,
2024-04-19 21:46:39 +12:00
);
2024-04-21 02:59:22 +12:00
const archive = archiver("zip", {
2024-04-19 21:46:39 +12:00
zlib: { level: 9 }, // Sets the compression level.
});
2024-04-21 02:59:22 +12:00
archive.on("error", function (err) {
2024-04-19 21:46:39 +12:00
throw err;
});
2024-04-21 02:59:22 +12:00
output.on("close", function () {
2024-04-19 21:46:39 +12:00
console.log(
2024-04-21 02:59:22 +12:00
"language-pack archive generated with a filesize of " +
2024-04-19 21:46:39 +12:00
archive.pointer() +
2024-04-21 02:59:22 +12:00
" bytes" +
2024-09-02 22:16:34 +12:00
`\n(./resourcepacks/${filename}.zip)`,
2024-04-19 21:46:39 +12:00
);
});
archive.pipe(output);
2024-04-21 02:59:22 +12:00
archive.directory("template/", false);
2024-04-19 21:46:39 +12:00
archive.finalize();
},
);
},
);
});
2024-04-01 20:48:59 +13:00
});
});