Merge additive Java pack assets and reject source collisions
This commit is contained in:
parent
9a12d82fa2
commit
afd40037dd
2 changed files with 66 additions and 8 deletions
|
|
@ -24,8 +24,45 @@ function addEntry(entries,name,bytes){
|
|||
entries.set(name,bytes);
|
||||
}
|
||||
|
||||
export function centralPackEntries(base,planetEntries=textureEntries){
|
||||
const merged=new Map(base);
|
||||
export function mergePackAssets(base,sources){
|
||||
const merged=new Map(base),conflicts=[];
|
||||
for(const source of sources){
|
||||
for(const [name,bytes] of source){
|
||||
if(!/^assets\/[a-z0-9_.-]+\/[a-z0-9_./-]+$/.test(name)||name.split('/').some(part=>!part||part==='.'||part==='..'))
|
||||
throw Error('Invalid resource asset path: '+name);
|
||||
if(!merged.has(name)){
|
||||
merged.set(name,bytes);
|
||||
continue;
|
||||
}
|
||||
const mapping=/^assets\/[^/]+\/(?:lang\/[^/]+\.json|sounds\.json)$/.test(name);
|
||||
const list=/^assets\/[^/]+\/font\/.+\.json$/.test(name)?'providers':
|
||||
/^assets\/[^/]+\/atlases\/[^/]+\.json$/.test(name)?'sources':null;
|
||||
if(!mapping&&!list){
|
||||
conflicts.push(name);
|
||||
continue;
|
||||
}
|
||||
const before=JSON.parse(merged.get(name)),after=JSON.parse(bytes);
|
||||
if(!before||typeof before!=='object'||Array.isArray(before)||!after||typeof after!=='object'||Array.isArray(after))
|
||||
throw Error('Expected a JSON object in '+name);
|
||||
if(mapping){
|
||||
const duplicates=Object.keys(after).filter(key=>Object.hasOwn(before,key));
|
||||
conflicts.push(...duplicates.map(key=>name+'#'+key));
|
||||
const additions=Object.fromEntries(Object.entries(after).filter(([key])=>!Object.hasOwn(before,key)));
|
||||
merged.set(name,json({...before,...additions}));
|
||||
}else{
|
||||
if(!Array.isArray(before[list])||!Array.isArray(after[list]))throw Error('Invalid '+list+' in '+name);
|
||||
const additionalKeys=Object.keys(after).filter(key=>key!==list);
|
||||
if(additionalKeys.length)throw Error('Unexpected fields in '+name+': '+additionalKeys.join(', '));
|
||||
merged.set(name,json({...before,[list]:[...before[list],...after[list]]}));
|
||||
}
|
||||
}
|
||||
}
|
||||
if(conflicts.length)throw Error('Conflicting pack assets:\n'+[...new Set(conflicts)].sort().join('\n'));
|
||||
return merged;
|
||||
}
|
||||
|
||||
export function centralPackEntries(base,planetEntries=textureEntries,assetSources=[]){
|
||||
const merged=mergePackAssets(base,assetSources);
|
||||
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');
|
||||
|
|
@ -85,9 +122,9 @@ function readDirectory(root,dir=root,entries=new Map()){
|
|||
return entries;
|
||||
}
|
||||
|
||||
export function buildCentralPack(baseDir,output,version){
|
||||
export function buildCentralPack(baseDir,output,version,assetDirectories=[]){
|
||||
if(!/^[1-9][0-9]*$/.test(version))throw Error('A positive integer release version is required');
|
||||
const entries=centralPackEntries(readDirectory(baseDir));
|
||||
const entries=centralPackEntries(readDirectory(baseDir),textureEntries,assetDirectories.map(dir=>readDirectory(dir)));
|
||||
if(entries.size>65535)throw Error('The central pack exceeds the ZIP entry limit');
|
||||
const metadata=JSON.parse(entries.get('pack.mcmeta'));
|
||||
metadata.pack.description='DuneRealms Central Resource Pack v'+version;
|
||||
|
|
@ -106,7 +143,7 @@ export function buildCentralPack(baseDir,output,version){
|
|||
}
|
||||
|
||||
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));
|
||||
const [base,output,version,...assetDirectories]=process.argv.slice(2);
|
||||
if(!base||!output||!version)throw Error('Usage: node build-central-resource-pack.mjs <extracted-artwork> <output> <version> [namespaced-asset-source ...]');
|
||||
console.log(JSON.stringify(buildCentralPack(base,output,version,assetDirectories),null,2));
|
||||
}
|
||||
|
|
|
|||
|
|
@ -7,7 +7,7 @@ import os from 'node:os';
|
|||
import path from 'node:path';
|
||||
import {build,profiles,textureEntries,protectedTextures,zip} from './build-planet-texture-packs.mjs';
|
||||
import {sandPalettes} from './flat-sand.mjs';
|
||||
import {centralPackEntries} from './build-central-resource-pack.mjs';
|
||||
import {centralPackEntries,mergePackAssets} from './build-central-resource-pack.mjs';
|
||||
|
||||
function chunks(png,type){const found=[];for(let i=8;i<png.length;){const n=png.readUInt32BE(i);if(png.subarray(i+4,i+8).toString()===type)found.push(png.subarray(i+8,i+8+n));i+=12+n;}return Buffer.concat(found);}
|
||||
test('every sand palette has identical grain, darker spice, and zero shader relief',()=>{
|
||||
|
|
@ -137,3 +137,24 @@ test('central generated licence text has identical ZIP bytes for Windows and LF
|
|||
assert.equal(windows.get('licenses/planet-materials/'+name).toString('utf8'),text);
|
||||
}
|
||||
});
|
||||
|
||||
test('source assets combine only recognized additive JSON and report every collision',()=>{
|
||||
const lang='assets/dunerealms/lang/en_us.json';
|
||||
const font='assets/dunerealms/font/menu.json';
|
||||
const sounds='assets/dunerealms/sounds.json';
|
||||
const atlas='assets/minecraft/atlases/blocks.json';
|
||||
const texture='assets/dunerealms/textures/gui/logo.png';
|
||||
const inputs=rows=>new Map(Object.entries(rows).map(([name,value])=>[name,Buffer.from(JSON.stringify(value))]));
|
||||
const first=inputs({[lang]:{dune:'Dune'},[font]:{providers:[{type:'space'}]},[sounds]:{spice:{sounds:['dust']}},
|
||||
[atlas]:{sources:[{type:'single'}]},[texture]:'pixels'});
|
||||
const second=inputs({[lang]:{spice:'Spice'},[font]:{providers:[{type:'bitmap'}]},[sounds]:{menu:{sounds:['open']}},
|
||||
[atlas]:{sources:[{type:'directory'}]}});
|
||||
const merged=mergePackAssets(first,[second]);
|
||||
assert.deepEqual(JSON.parse(merged.get(lang)),{dune:'Dune',spice:'Spice'});
|
||||
assert.deepEqual(JSON.parse(merged.get(font)).providers,[{type:'space'},{type:'bitmap'}]);
|
||||
assert.deepEqual(JSON.parse(merged.get(atlas)).sources,[{type:'single'},{type:'directory'}]);
|
||||
assert.deepEqual(Object.keys(JSON.parse(merged.get(sounds))),['spice','menu']);
|
||||
assert.throws(()=>mergePackAssets(first,[inputs({[lang]:{dune:'Again'},[texture]:'pixels'})]),
|
||||
error=>error.message.includes(lang+'#dune')&&error.message.includes(texture));
|
||||
assert.throws(()=>mergePackAssets(first,[inputs({'assets/dr_arrakis/../logo.png':'bad'})]),/Invalid resource asset path/);
|
||||
});
|
||||
|
|
|
|||
Loading…
Reference in a new issue

Staff