cocos的plist拆成小图

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
// cocos plist 动画转 拆分成小图
function p2m(farr){
for(let f of farr){
fs.readFile(bpath + f, (err,bu)=>{
if(err){
console.log(err);
return;
}
let jn = xml.xml2js(bu, {compact: true});
console.log(jn);
let imgObj = sharp(bpath + jn.plist.dict.dict[1].string[0]._text);
console.log("basefile");
for(let i=0;i<jn.plist.dict.dict[0].key.length;i++){
let pos = jn.plist.dict.dict[0].dict[i].string[0]._text;
pos = pos.substr(2, pos.length-4).replace("},{",",").split(",");
console.log(jn.plist.dict.dict[0].key[i]._text, pos);

try{
if("true" in jn.plist.dict.dict[0].dict[i]){
imgObj.clone().extract({ left: pos[0]|0, top: pos[1]|0, width: pos[3]|0, height: pos[2]|0 }).rotate(-90).toFile(bpath + "m/"+ jn.plist.dict.dict[0].key[i]._text, (err, info)=>{
if(err) console.log("error", err);
});
}else{
imgObj.clone().extract({ left: pos[0]|0, top: pos[1]|0, width: pos[2]|0, height: pos[3]|0 }).toFile(bpath + "m/"+ jn.plist.dict.dict[0].key[i]._text);
}
}catch(e){
console.error("save", pos[0], pos[1], pos[2], pos[3], e);
}
}
});
}
}