import-assets.cjs 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. // Mechanical asset preparation: preserve alpha, uniformly scale, pad, slice and record provenance.
  2. const fs=require('fs'),path=require('path');
  3. const sharp=require('C:/Users/EDY/.cache/codex-runtimes/codex-primary-runtime/dependencies/node/node_modules/sharp');
  4. const root=path.resolve(__dirname,'../..'),art=path.join(root,'Assets/BuffaloSiege/Art');
  5. const records=JSON.parse(fs.readFileSync(path.join(__dirname,'generation-records.json')));
  6. const manifests=[path.join(art,'asset-manifest.json'),path.join(root,'docs/ui-style-a/asset-manifest.json')];
  7. function bounds(data,w,h,threshold=0){let l=w,t=h,r=-1,b=-1;for(let y=0;y<h;y++)for(let x=0;x<w;x++)if(data[(y*w+x)*4+3]>threshold){l=Math.min(l,x);r=Math.max(r,x);t=Math.min(t,y);b=Math.max(b,y);}if(r<0)throw Error('Empty image');return {left:l,top:t,width:r-l+1,height:b-t+1};}
  8. async function alphaBounds(file){const {data,info}=await sharp(file).ensureAlpha().raw().toBuffer({resolveWithObject:true});return bounds(data,info.width,info.height);}
  9. async function update(){
  10. const docs=manifests.map(f=>JSON.parse(fs.readFileSync(f)));const report=[];
  11. fs.mkdirSync(path.join(__dirname,'sources'),{recursive:true});
  12. for(const rec of records){
  13. if(!rec.path)throw Error('Missing source '+rec.id);
  14. const source=path.join(__dirname,'sources',rec.id+'.png');if(!fs.existsSync(source))fs.copyFileSync(rec.path,source);
  15. if(rec.id.startsWith('animation.'))continue;
  16. const output=path.join(art,rec.id+'.png');let b,dimensions;
  17. if(rec.id.startsWith('illustration.')){
  18. // Crop an overscan composition at its native proportions; never squeeze the image.
  19. await sharp(source).extract(rec.crop).resize(688,152,{fit:'cover',position:'centre'}).flatten({background:'#241521'}).png().toFile(output);
  20. dimensions=[688,152];b={left:0,top:0,width:688,height:152};
  21. }else{
  22. const box=await alphaBounds(source),size=rec.id.startsWith('avatar.')||rec.id==='prop.build-pad'?256:512,pad=size/16;
  23. const content=await sharp(source).extract(box).resize(size-pad*2,size-pad*2,{fit:'inside'}).png().toBuffer();
  24. const meta=await sharp(content).metadata();
  25. await sharp({create:{width:size,height:size,channels:4,background:'#00000000'}}).composite([{input:content,left:Math.floor((size-meta.width)/2),top:Math.floor((size-meta.height)/2)}]).png().toFile(output);
  26. dimensions=[size,size];b=await alphaBounds(output);
  27. }
  28. const padding=[b.top,dimensions[0]-b.left-b.width,dimensions[1]-b.top-b.height,b.left];
  29. const shared=path.join(root,'docs/ui-style-a/assets/sprites',rec.id+'.png');fs.copyFileSync(output,shared);
  30. for(let i=0;i<docs.length;i++){
  31. const entry=docs[i].assets.find(a=>a.id===rec.id);if(!entry)throw Error('Unknown ID '+rec.id);
  32. for(const key of ['atlas','grid','cellIndex','sourceRect','uv'])delete entry[key];
  33. Object.assign(entry,{file:i===0?rec.id+'.png':'assets/sprites/'+rec.id+'.png',width:dimensions[0],height:dimensions[1],transparent:!rec.id.startsWith('illustration.'),transparentPadding:padding,pivot:[.5,.5],source:'docs/art-refresh-2026-10-29/sources/'+rec.id+'.png',sourceRoot:'project',revision:'2026-10-29'});
  34. }
  35. report.push({id:rec.id,width:dimensions[0],height:dimensions[1],padding});
  36. }
  37. for(let i=0;i<docs.length;i++){docs[i].status='unity-integrated';fs.writeFileSync(manifests[i],JSON.stringify(docs[i],null,2)+'\n');}
  38. fs.writeFileSync(path.join(__dirname,'asset-validation.json'),JSON.stringify(report,null,2)+'\n');
  39. console.log(JSON.stringify({imported:report.length,dimensionsAndPadding:report}));
  40. }
  41. update().catch(e=>{console.error(e);process.exit(1);});