22 lines
3.9 KiB
JavaScript
22 lines
3.9 KiB
JavaScript
import {mkdir,readFile,writeFile,rename,lstat} from 'node:fs/promises';
|
|
import {createHash} from 'node:crypto';import {resolve,dirname} from 'node:path';import {fileURLToPath} from 'node:url';
|
|
|
|
// Native Minecraft 1.21.8 inventory renders. Existing custom/item sprites are
|
|
// preserved. Pin the author revision so a later game update cannot change them.
|
|
const revision='99090baaa21639c63f503fd77c60c7084dd9af1a',repository='Owen1212055/mc-assets';
|
|
const root=resolve(dirname(fileURLToPath(import.meta.url)),'..'),destination=resolve(root,'website/web/assets/minecraft-items'),manifestPath=resolve(root,'tools/manifests/native-item-icons-1.21.8.json');
|
|
const hash=(algorithm,bytes)=>createHash(algorithm).update(bytes).digest('hex');
|
|
const exists=async path=>{try{const stat=await lstat(path);if(!stat.isFile()||stat.isSymbolicLink())throw Error('Expected a regular icon file');return true;}catch(error){if(error.code==='ENOENT')return false;throw error;}};
|
|
const response=await fetch(`https://api.github.com/repos/${repository}/git/trees/${revision}?recursive=1`,{headers:{accept:'application/vnd.github+json'}});if(!response.ok)throw Error('Native icon catalogue unavailable: '+response.status);const tree=await response.json();if(tree.truncated)throw Error('Incomplete native icon catalogue');
|
|
const entries=tree.tree.filter(row=>row.type==='blob'&&/^assets\/[A-Z0-9_]+\.png$/.test(row.path));if(entries.length<1300||entries.length>1500)throw Error('Unexpected native item catalogue');
|
|
let manifest={version:1,minecraftVersion:'1.21.8',repository,revision,source:`https://github.com/${repository}/tree/${revision}/assets`,attribution:'Minecraft textures and native game renders belong to Mojang/Microsoft; native renders provided by Owen1212055/mc-assets. DuneRealms is an unofficial fan project.',icons:{}};
|
|
if(await exists(manifestPath)){manifest=JSON.parse(await readFile(manifestPath,'utf8'));if(manifest.revision!==revision)throw Error('Icon provenance revision changed');}
|
|
const pending=[];let preserved=0;
|
|
for(const row of entries){const id=row.path.slice(7,-4).toLowerCase(),target=resolve(destination,id+'.png');if(await exists(target)){if(manifest.icons[id]&&hash('sha256',await readFile(target))!==manifest.icons[id].sha256)throw Error('Previously imported icon changed: '+id);preserved++;}else pending.push({...row,id,target});}
|
|
console.log(JSON.stringify({minecraftVersion:'1.21.8',preserved,missing:pending.length,apply:process.argv.includes('--apply')}));
|
|
if(process.argv.includes('--apply')){
|
|
await mkdir(destination,{recursive:true});await mkdir(dirname(manifestPath),{recursive:true});
|
|
let imported=0;
|
|
async function worker(){for(;;){const row=pending.shift();if(!row)return;const url=`https://raw.githubusercontent.com/${repository}/${revision}/${row.path}`,r=await fetch(url);if(!r.ok)throw Error('Native icon download failed: '+row.id+' '+r.status);const bytes=Buffer.from(await r.arrayBuffer());if(bytes.length!==row.size||bytes.length>1048576||!bytes.subarray(0,8).equals(Buffer.from([137,80,78,71,13,10,26,10]))||bytes.readUInt32BE(16)!==256||bytes.readUInt32BE(20)!==256)throw Error('Invalid native PNG: '+row.id);if(hash('sha1',Buffer.concat([Buffer.from('blob '+bytes.length+'\0'),bytes]))!==row.sha)throw Error('Native icon did not match its pinned Git object');await writeFile(row.target+'.importing',bytes,{flag:'wx'});await rename(row.target+'.importing',row.target);manifest.icons[row.id]={source:row.path,gitBlob:row.sha,sha256:hash('sha256',bytes)};if(++imported%100===0)console.log(JSON.stringify({imported}));}}
|
|
const results=await Promise.allSettled(Array.from({length:4},worker));manifest.icons=Object.fromEntries(Object.entries(manifest.icons).sort(([a],[b])=>a.localeCompare(b)));await writeFile(manifestPath,JSON.stringify(manifest,null,2)+'\n');for(const result of results)if(result.status==='rejected')throw result.reason;console.log(JSON.stringify({imported,recorded:Object.keys(manifest.icons).length}));
|
|
}
|

Staff