-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathh5-texture-unpacker.js
3 lines (2 loc) · 2.51 KB
/
h5-texture-unpacker.js
1
2
3
#!/usr/bin/env node
var e=require("xpath"),r=require("@xmldom/xmldom").DOMParser;var t=require("sharp"),a=require("path"),n=require("fs"),i=require("yargs"),o=require("winston"),s=(new Date).toISOString(),f=o.format.printf((function(e){return s+"-"+e.level+": "+JSON.stringify(e.message,null,4)+"\n"})),l=o.createLogger({transports:[new o.transports.Console({format:o.format.combine(o.format.colorize(),f)})]});a.join(__dirname,"../");var m,u,p,h=i.usage("Usage: $0 -j [jsonFile] -o [outputDir]").option("imageFile",{alias:"i",demandOption:!0,default:"",describe:"The input texture image",type:"string"}).option("atlasFile",{alias:"a",demandOption:!0,describe:"The input atlas",type:"string"}).option("outputDir",{alias:"o",demandOption:!0,describe:"The output folder",type:"string"}).option("verbose",{alias:"v",default:!1,describe:"Show log messages",type:"boolean"}).help("h").alias("h","help").epilog("copyright Azerion 2021").argv,c=a.join(h.i),g=a.join(h.a),d=a.join(h.o,"/"),y=n.readFileSync(g),w=[];l.level=!0===h.verbose?"info":"warn",n.existsSync(c)||(l.error("Could not find image at specified path!"),process.exit(1)),n.existsSync(d)||n.mkdirSync(d),g.match(/\.json$/)?(m=y,u=JSON.parse(m.toString()),p=[],u.hasOwnProperty("frames")&&Array.isArray(u.frames)&&u.frames.forEach((function(e){p.push({name:e.filename,w:e.frame.w,h:e.frame.h,x:e.frame.x,y:e.frame.y})})),null===(w=p)&&(w=function(e){var r=JSON.parse(e.toString()),t=[];return r.hasOwnProperty("frames")&&!Array.isArray(r.frames)&&Object.keys(r.frames).forEach((function(e){var a=r.frames[e];t.push({name:e,w:a.frame.w,h:a.frame.h,x:a.frame.x,y:a.frame.y})})),t}(y)),null===w&&(l.error("Could not get Hash nor Array data from json file: "+g),process.exit(1))):g.match(/\.xml$/)?w=function(t){var a=(new r).parseFromString(t.toString()),n=e.select("//SubTexture",a),i=[];return n.length>0&&n.forEach((function(e){i.push({name:e.getAttribute("name"),w:parseInt(e.getAttribute("width")),h:parseInt(e.getAttribute("height")),x:parseInt(e.getAttribute("x")),y:parseInt(e.getAttribute("y"))})})),i}(y):(l.error("Unknown file format: "+g),process.exit(1));var x=0;0===w.length&&(l.error("No data found: "+g),process.exit(1)),w.forEach((function(e){0!==e.w&&0!==e.h?t(c).extract({width:e.w,height:e.h,left:e.x,top:e.y}).toFile(a.join(d,"/",e.name+".png")).then((function(r){l.info("File: "+e.name+" written"),x++})).catch((function(r){l.info("Unable to write file , "+e.name+":, "+r)})):l.warn("Unable to write file , "+e.name+": Source width/height is 0")})),console.log("Done writing "+x+" images to "+d);