93 lines
2.5 KiB
JavaScript
93 lines
2.5 KiB
JavaScript
const decompress = require('decompress');
|
|
const fs = require('fs');
|
|
const jsonfile = require('jsonfile');
|
|
const blockRemaps = 'mappings/blocks.json';
|
|
let blocks;
|
|
jsonfile.readFile(blockRemaps, function (err, obj) {
|
|
if (err) console.error(err);
|
|
blocks = obj;
|
|
});
|
|
const itemRemaps = 'mappings/items.json';
|
|
let items;
|
|
jsonfile.readFile(itemRemaps, function (err, obj) {
|
|
if (err) console.error(err);
|
|
items = obj;
|
|
});
|
|
let toConvertName;
|
|
|
|
try {
|
|
toConvertName = process.argv[2];
|
|
if (fs.existsSync(`input/${toConvertName}.zip`)) {
|
|
convert(toConvertName);
|
|
} else {
|
|
console.log('Usage: npm start <pack name>');
|
|
}
|
|
} catch (err) {
|
|
console.log(err);
|
|
}
|
|
|
|
/**
|
|
* @param {string} input Name of resource pack zip file
|
|
*/
|
|
|
|
function convert(input) {
|
|
const extractedPath = `out/${input}/assets/minecraft/textures/`;
|
|
decompress(`input/${input}.zip`, `out/${input}`, {
|
|
filter: file => !file.path.endsWith('/'),
|
|
}).then(files => {
|
|
files.forEach(element => {
|
|
if (!element.path.includes('assets/minecraft/textures/')) {
|
|
return;
|
|
}
|
|
const texture = element.path.match(/([A-Za-z]*)\/([A-Za-z]*)\/([A-Za-z_0-9]*)\.(png\.mcmeta|png)/);
|
|
if (!texture) return;
|
|
let type = texture[1];
|
|
const subtype = texture[2];
|
|
const name = texture[3];
|
|
const extension = texture[4];
|
|
let rename;
|
|
if (type == 'textures') {
|
|
type = subtype;
|
|
}
|
|
if (type == 'items') {
|
|
rename = items[name];
|
|
}
|
|
if (type == 'blocks') {
|
|
rename = blocks[name];
|
|
}
|
|
const path1 = `${extractedPath}${type}/`;
|
|
if (rename) {
|
|
try {
|
|
fs.renameSync(`${path1}${name}.${extension}`, `${path1}${rename}.${extension}`);
|
|
console.log(`Renamed ${name}.${extension} to ${rename}.${extension}`);
|
|
} catch (error) {
|
|
console.log(error);
|
|
}
|
|
}
|
|
});
|
|
}).then(() => {
|
|
try {
|
|
fs.renameSync(`${extractedPath}blocks`, `${extractedPath}block`);
|
|
console.log(`Renamed ${extractedPath}blocks to ${extractedPath}block`);
|
|
} catch (error) {
|
|
console.log(error);
|
|
}
|
|
try {
|
|
fs.renameSync(`${extractedPath}items`, `${extractedPath}item`);
|
|
console.log(`Renamed ${extractedPath}items to ${extractedPath}item`);
|
|
} catch (error) {
|
|
console.log(error);
|
|
}
|
|
jsonfile.readFile(`out/${input}/pack.mcmeta`, function (err, obj) {
|
|
if (err) console.error(err);
|
|
const oldFormat = obj.pack.pack_format;
|
|
const newFormat = 15;
|
|
obj.pack.pack_format = newFormat;
|
|
jsonfile.writeFile(`out/${input}/pack.mcmeta`, obj, { spaces: 2 }, function (err) {
|
|
if (err) console.error(err);
|
|
console.log(`Pack format changed from ${oldFormat} to ${newFormat}`);
|
|
});
|
|
});
|
|
});
|
|
}
|