MC/tools/build-planet-texture-packs.mjs
2026-11-27 23:43:05 +00:00

78 lines
6.2 KiB
JavaScript

/** Deterministic vanilla-material layers; no GUI, models, entities or Dune namespace. */
import fs from 'node:fs';
import path from 'node:path';
import {fileURLToPath} from 'node:url';
import {createHash} from 'node:crypto';
import {deflateRawSync} from 'node:zlib';
import {sandEntries} from './flat-sand.mjs';
const root=path.resolve(path.dirname(fileURLToPath(import.meta.url)),'..');
const art=path.join(root,'DuneRealmsGUI/planet-artwork');
const source=path.join(art,'pixel-perfection-ce');
export const profiles=JSON.parse(fs.readFileSync(path.join(art,'profiles.json'),'utf8'));
const prefix='assets/minecraft/textures/';
// The pinned Network Artwork v1 uses these vanilla carrier faces for spice.
export const protectedTextures=new Set(['dried_kelp_bottom','dried_kelp_side','dried_kelp_top']);
const table=Array.from({length:256},(_,n)=>{for(let i=0;i<8;i++)n=(n&1)?0xedb88320^(n>>>1):n>>>1;return n>>>0;});
const crc32=bytes=>{let n=0xffffffff;for(const b of bytes)n=table[(n^b)&255]^(n>>>8);return (n^0xffffffff)>>>0;};
const digest=(algorithm,bytes)=>createHash(algorithm).update(bytes).digest('hex');
export function zip(entries){
const locals=[],directory=[];let offset=0;
for(const [name,data] of [...entries].sort(([a],[b])=>a<b?-1:a>b?1:0)){
if(!/^[a-zA-Z0-9_./-]+$/.test(name)||name.startsWith('/')||name.split('/').includes('..'))throw Error('Unsafe ZIP path');
const key=Buffer.from(name),body=deflateRawSync(data,{level:9}),crc=crc32(data);
const local=Buffer.alloc(30);local.writeUInt32LE(0x04034b50);local.writeUInt16LE(20,4);local.writeUInt16LE(8,8);local.writeUInt16LE(0x5d77,12);local.writeUInt32LE(crc,14);local.writeUInt32LE(body.length,18);local.writeUInt32LE(data.length,22);local.writeUInt16LE(key.length,26);
locals.push(local,key,body);
const central=Buffer.alloc(46);central.writeUInt32LE(0x02014b50);central.writeUInt16LE(20,4);central.writeUInt16LE(20,6);central.writeUInt16LE(8,10);central.writeUInt16LE(0x5d77,14);central.writeUInt32LE(crc,16);central.writeUInt32LE(body.length,20);central.writeUInt32LE(data.length,24);central.writeUInt16LE(key.length,28);central.writeUInt32LE(offset,42);directory.push(central,key);
offset+=local.length+key.length+body.length;
}
const central=Buffer.concat(directory),end=Buffer.alloc(22);end.writeUInt32LE(0x06054b50);end.writeUInt16LE(entries.size,8);end.writeUInt16LE(entries.size,10);end.writeUInt32LE(central.length,12);end.writeUInt32LE(offset,16);
return Buffer.concat([...locals,central,end]);
}
export function textureEntries(planet){
const profile=profiles[planet];if(!profile)throw Error('Unknown planet');
const entries=new Map();
for(const kind of ['block','item'])for(const name of fs.readdirSync(path.join(source,kind)).sort()){
if(kind==='block'&&protectedTextures.has(name.replace(/\.png(?:\.mcmeta)?$/,'')))continue;
if(!/^[a-z0-9_-]+\.png(?:\.mcmeta)?$/.test(name))throw Error('Unexpected upstream file: '+name);
const bytes=fs.readFileSync(path.join(source,kind,name));
if(name.endsWith('.mcmeta')){JSON.parse(bytes);if(!fs.existsSync(path.join(source,kind,name.slice(0,-7))))continue;}
else if(bytes.subarray(0,8).toString('hex')!=='89504e470d0a1a0a')throw Error('Invalid PNG');
entries.set(prefix+kind+'/'+name,bytes);
}
for(const [target,original] of Object.entries(profile.blocks)){
if(protectedTextures.has(target))throw Error('Cannot replace a Dune carrier texture');
const key=prefix+'block/'+target+'.png',src=path.join(source,'block',original+'.png');
entries.set(key,fs.readFileSync(src));entries.delete(key+'.mcmeta');
if(fs.existsSync(src+'.mcmeta'))entries.set(key+'.mcmeta',fs.readFileSync(src+'.mcmeta'));
}
for(const [name,bytes] of sandEntries(planet)){entries.set(name,bytes);entries.delete(name+'.mcmeta');}
entries.set('LICENSE-Sand-Luanti.txt',fs.readFileSync(path.join(art,'flat-sand/LICENSE.txt')));
entries.set('CREDITS-Sand.txt',fs.readFileSync(path.join(art,'flat-sand/CREDITS.txt')));
entries.set('pack.mcmeta',Buffer.from(JSON.stringify({pack:{pack_format:64,supported_formats:[64,97],min_format:64,max_format:97,description:'DuneRealms '+profile.name+' · '+profile.feel}})));
entries.set('LICENSE-Pixel-Perfection-CE.md',fs.readFileSync(path.join(source,'LICENSE.md')));
entries.set('CREDITS.txt',Buffer.from('Pixel Perfection CE by XSSheep and community contributors (Athemis, StonePendant, freejusticehere, Stingraych, Nova_Wostra, lazerl0rd).\nhttps://github.com/Athemis/PixelPerfectionCE\nSource commit: 28e38cab7c1f03f86364ef704f705bebcc13cb3d\nTextures and DuneRealms material remappings: CC-BY-SA-4.0. https://creativecommons.org/licenses/by-sa/4.0/\nDuneRealms changes: block/item-only extraction, modern pack metadata, named material remappings; original pixels preserved. No endorsement implied.\n'));
return entries;
}
export function build(output){
fs.mkdirSync(output,{recursive:true});const manifest={version:2,sourceCommit:'28e38cab7c1f03f86364ef704f705bebcc13cb3d',license:'CC-BY-SA-4.0',packId:'59f34b30-6a86-5e58-a813-6e776f726c64',packs:{}};
for(const [planet,profile] of Object.entries(profiles)){
const entries=textureEntries(planet),bytes=zip(entries),filename=`DuneRealms-Materials-${planet}-v2.zip`;
fs.writeFileSync(path.join(output,filename),bytes);
manifest.packs[planet]={name:profile.name,filename,url:'https://www.dunerealms.com/downloads/'+filename,sha1:digest('sha1',bytes),sha256:digest('sha256',bytes),bytes:bytes.length,entries:entries.size,aliases:profile.aliases};
}
fs.writeFileSync(path.join(output,'manifest.json'),JSON.stringify(manifest,null,2)+'\n');
const yaml=['# Enable after publishing every pinned ZIP from planet-artwork/release-v1/.',
'# Java material layer only. Geyser/Bedrock requires its own converted pack.',
'enabled: false','', '# Optional exact world-name mappings, e.g. world: arrakis','worlds: {}','','packs:'];
for(const [planet,p] of Object.entries(manifest.packs))yaml.push(` ${planet}:`,` name: ${JSON.stringify(p.name)}`,` url: ${p.url}`,` sha1: ${p.sha1}`,` worlds: ${JSON.stringify(p.aliases)}`);
fs.writeFileSync(path.join(output,'planet-textures.yml'),yaml.join('\n')+'\n');
return manifest;
}
if(process.argv[1]&&path.resolve(process.argv[1])===fileURLToPath(import.meta.url)){
const output=path.resolve(process.argv[2]||path.join(art,'release-v1'));
console.log(JSON.stringify(build(output),null,2));
}