-
Notifications
You must be signed in to change notification settings - Fork 5
/
app.js
160 lines (139 loc) · 4.84 KB
/
app.js
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
const fs = require('fs'),
plist = require('plist'),
gm = require('gm').subClass({ imageMagick: true });
const file = process.argv.splice(2).toString();
let frames, textureRect, frame, img_name, output_name, coordinate_x, coordinate_y, img_width, img_height, rotated, buf, img_arr = [],
splitIdx = 0, maxSplitIdx;
if (!file) {
console.error('缺少命令行参数!');
return;
}
if (file.indexOf(".plist") > -1) {
getSpritePlist(file);
} else if (file.indexOf(".json") > -1) {
getSpriteJson(file);
} else {
console.log('暂不支持该文件格式!');
};
async function getSpritePlist(path) {
let json = plist.parse(fs.readFileSync(file, 'utf8'));
if (json && json['frames'] && json['metadata']) {
frames = json['frames'];
img_name = json['metadata']["textureFileName"];
} else {
console.error('plist文件结构不符合要求!');
return;
}
let version = json['metadata']['format'];
for (let item in frames) {
frame = frames[item];
output_name = item.replace(".png", "");
switch (version) {
case 1:
textureRect = transStr(frame['frame']);
rotated = false;
break;
case 2:
textureRect = transStr(frame['frame']);
rotated = frame['rotated'];
break;
case 3:
textureRect = transStr(frame['textureRect']);
rotated = frame['textureRotated'];
break;
default:
textureRect = transStr(frame['textureRect']);
break;
}
coordinate_x = textureRect[0];
coordinate_y = textureRect[1];
img_width = rotated ? textureRect[3] : textureRect[2];
img_height = rotated ? textureRect[2] : textureRect[3];
img_arr.push({
'img_name': img_name,
'output_name': output_name,
"coordinate_x": coordinate_x,
"coordinate_y": coordinate_y,
"img_width": img_width,
"img_height": img_height,
"rotated": rotated,
});
}
maxSplitIdx = img_arr.length;
for (let i = 0; i < maxSplitIdx; i++) {
let img = img_arr[i];
await spriteSpliter(img.img_name, img.output_name, img.img_width, img.img_height, img.coordinate_x, img.coordinate_y, img.rotated);
}
};
async function getSpriteJson(path) {
let json = JSON.parse(fs.readFileSync(file, 'utf8'));
if (json && json['frames'] && json['file']) {
frames = json['frames'];
img_name = json['file'];
} else if (json && json['frames'] && json['images']) {
frames = json['frames'];
img_name = json['images'][0];
} else {
console.error('json文件结构不符合要求!');
return;
}
console.log(img_name);
if (frames instanceof Array) {
frames.forEach((frame,index) => {
img_arr.push({
'img_name': img_name,
'output_name': index,
"coordinate_x": frame[0],
"coordinate_y": frame[1],
"img_width": frame[2],
"img_height": frame[3],
"rotated": false,
});
})
} else {
for (let item in frames) {
frame = frames[item]
output_name = item;
coordinate_x = frame['x'];
coordinate_y = frame['y'];
img_width = frame['w'];
img_height = frame['h'];
rotated = frames['rotated'];
img_arr.push({
'img_name': img_name,
'output_name': output_name,
"coordinate_x": coordinate_x,
"coordinate_y": coordinate_y,
"img_width": img_width,
"img_height": img_height,
"rotated": rotated,
});
}
}
maxSplitIdx = img_arr.length;
for (let i = 0; i < maxSplitIdx; i++) {
let img = img_arr[i];
await spriteSpliter(img.img_name, img.output_name, img.img_width, img.img_height, img.coordinate_x, img.coordinate_y, img.rotated);
}
}
function transStr(string) {//"{{41,52},{11,19}}"=>["41","52","11","19"]
return string.replace(/{|}/g, "").split(",");
}
async function spriteSpliter(img_name, output_name, width, height, x, y, rotated) {
buf = gm(`./src/${img_name}`)
.crop(width, height, x, y);
if (rotated) {
buf = buf.rotate("transparent", -90);
}
buf.write(`./output/${output_name}.png`, function (err) {
if (!err) {
splitIdx++;
console.log(`${splitIdx}/${maxSplitIdx}=>${output_name}.png`);
if (splitIdx == maxSplitIdx) {
console.log("文件裁剪完成!");
}
} else {
console.log(err.message || "出错了!");
}
});
};