MC/tools/build-central-resource-pack.mjs

70 lines
3.4 KiB
JavaScript

/** Combine the shared network artwork with independently addressable planet textures. */
import fs from 'node:fs';
import path from 'node:path';
import {createHash} from 'node:crypto';
import {fileURLToPath} from 'node:url';
import {profiles,textureEntries,zip} from './build-planet-texture-packs.mjs';
const texturePrefix='assets/minecraft/textures/';
const packPrefix='assets/dunerealms/textures/planets/';
const cubeItems=['stone','sand','red_sand','cobblestone','dirt','oak_planks'];
const json=value=>Buffer.from(JSON.stringify(value));
const hash=(algorithm,bytes)=>createHash(algorithm).update(bytes).digest('hex');
export function centralPackEntries(base){
const merged=new Map(base);
if(!merged.has('pack.mcmeta'))throw Error('The base must be an extracted network resource pack');
const packInfo=JSON.parse(merged.get('pack.mcmeta'));
if(!packInfo.pack)throw Error('The network pack metadata is invalid');
for(const planet of Object.keys(profiles)){
for(const [name,bytes] of textureEntries(planet)){
if(name==='pack.mcmeta')continue;
const target=name.startsWith(texturePrefix)
?packPrefix+planet+'/'+name.slice(texturePrefix.length)
:'licenses/planet-materials/'+name;
if(merged.has(target)&&!merged.get(target).equals(bytes))throw Error('Conflicting pack entry: '+target);
merged.set(target,bytes);
}
for(const material of cubeItems){
const id=planet+'/'+material;
if(!merged.has(packPrefix+planet+'/block/'+material+'.png'))throw Error('Missing planet item texture: '+id);
merged.set('assets/dunerealms/items/planets/'+id+'.json',json({model:{type:'minecraft:model',model:'dunerealms:item/planets/'+id}}));
merged.set('assets/dunerealms/models/item/planets/'+id+'.json',json({
parent:'minecraft:block/cube_all',textures:{all:'dunerealms:planets/'+planet+'/block/'+material}
}));
}
}
return merged;
}
function readDirectory(root,dir=root,entries=new Map()){
for(const entry of fs.readdirSync(dir,{withFileTypes:true})){
const absolute=path.join(dir,entry.name);
if(entry.isDirectory())readDirectory(root,absolute,entries);
else if(entry.isFile())entries.set(path.relative(root,absolute).split(path.sep).join('/'),fs.readFileSync(absolute));
else throw Error('Unsupported pack entry: '+entry.name);
}
return entries;
}
export function buildCentralPack(baseDir,output,version){
if(!/^[1-9][0-9]*$/.test(version))throw Error('A positive integer release version is required');
const entries=centralPackEntries(readDirectory(baseDir));
if(entries.size>65535)throw Error('The central pack exceeds the ZIP entry limit');
const bytes=zip(entries),filename='DuneRealms-Central-v'+version+'.zip';
const manifest={
filename,url:'https://www.dunerealms.com/downloads/'+filename,
sha1:hash('sha1',bytes),sha256:hash('sha256',bytes),
bytes:bytes.length,profiles:Object.fromEntries(Object.entries(profiles).map(([id,profile])=>[id,profile.aliases]))
};
fs.mkdirSync(output,{recursive:true});
fs.writeFileSync(path.join(output,filename),bytes);
fs.writeFileSync(path.join(output,'central-pack-manifest.json'),JSON.stringify(manifest,null,2)+'\n');
return manifest;
}
if(process.argv[1]&&path.resolve(process.argv[1])===fileURLToPath(import.meta.url)){
const [base,output,version]=process.argv.slice(2);
if(!base||!output||!version)throw Error('Usage: node build-central-resource-pack.mjs <extracted-artwork> <output> <version>');
console.log(JSON.stringify(buildCentralPack(base,output,version),null,2));
}