forked from unimatrix/web
Build one pack containing independently addressable world textures
This commit is contained in:
parent
8f3d91aedc
commit
81dd062f3c
1 changed files with 60 additions and 0 deletions
60
tools/build-central-resource-pack.mjs
Normal file
60
tools/build-central-resource-pack.mjs
Normal file
|
|
@ -0,0 +1,60 @@
|
|||
/** 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 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);
|
||||
}
|
||||
}
|
||||
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));
|
||||
}
|
||||
Loading…
Reference in a new issue

Staff