cocos2d-cli 1.1.2 → 1.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,139 @@
1
+ /**
2
+ * 公共工具模块
3
+ * 提供颜色解析、UUID 生成等通用功能
4
+ */
5
+
6
+ /**
7
+ * 解析颜色字符串 #RRGGBB 或 #RRGGBBAA
8
+ * @param {string} colorStr - 颜色字符串
9
+ * @returns {object|null} - 颜色对象 {r, g, b, a} 或 null
10
+ */
11
+ function parseColor(colorStr) {
12
+ if (!colorStr || typeof colorStr !== 'string') return null;
13
+
14
+ let hex = colorStr.replace('#', '');
15
+ if (hex.length === 6) {
16
+ return {
17
+ r: parseInt(hex.substring(0, 2), 16),
18
+ g: parseInt(hex.substring(2, 4), 16),
19
+ b: parseInt(hex.substring(4, 6), 16),
20
+ a: 255
21
+ };
22
+ } else if (hex.length === 8) {
23
+ return {
24
+ r: parseInt(hex.substring(0, 2), 16),
25
+ g: parseInt(hex.substring(2, 4), 16),
26
+ b: parseInt(hex.substring(4, 6), 16),
27
+ a: parseInt(hex.substring(6, 8), 16)
28
+ };
29
+ }
30
+ return null;
31
+ }
32
+
33
+ /**
34
+ * 解析颜色字符串并返回 cc.Color 格式
35
+ * @param {string} colorStr - 颜色字符串
36
+ * @returns {object|null} - cc.Color 对象或 null
37
+ */
38
+ function parseColorToCcColor(colorStr) {
39
+ const color = parseColor(colorStr);
40
+ if (!color) return null;
41
+ return { "__type__": "cc.Color", ...color };
42
+ }
43
+
44
+ /**
45
+ * 将 cc.Color 对象转为 #RRGGBB 字符串
46
+ * @param {object} color - cc.Color 对象
47
+ * @returns {string} - #RRGGBB 格式字符串
48
+ */
49
+ function colorToHex(color) {
50
+ if (!color) return '#ffffff';
51
+ const r = (color.r || 0).toString(16).padStart(2, '0');
52
+ const g = (color.g || 0).toString(16).padStart(2, '0');
53
+ const b = (color.b || 0).toString(16).padStart(2, '0');
54
+ return `#${r}${g}${b}`;
55
+ }
56
+
57
+ /**
58
+ * 生成 UUID
59
+ * @returns {string} - UUID 字符串
60
+ */
61
+ function generateUUID() {
62
+ return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
63
+ const r = Math.random() * 16 | 0;
64
+ const v = c === 'x' ? r : (r & 0x3 | 0x8);
65
+ return v.toString(16);
66
+ });
67
+ }
68
+
69
+ /**
70
+ * 生成 Cocos Creator 组件 ID(22位 base64 格式)
71
+ * @returns {string} - 组件 ID 字符串
72
+ */
73
+ function generateId() {
74
+ const chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
75
+ let result = '';
76
+ for (let i = 0; i < 22; i++) {
77
+ result += chars.charAt(Math.floor(Math.random() * chars.length));
78
+ }
79
+ return result;
80
+ }
81
+
82
+ /**
83
+ * 解析命令行选项
84
+ * @param {string[]} args - 命令行参数数组
85
+ * @param {number} startIndex - 开始解析的索引
86
+ * @returns {object} - 选项对象
87
+ */
88
+ function parseOptions(args, startIndex = 0) {
89
+ const options = {};
90
+ for (let i = startIndex; i < args.length; i++) {
91
+ const arg = args[i];
92
+ if (arg.startsWith('--')) {
93
+ const eqIndex = arg.indexOf('=');
94
+ if (eqIndex > 2) {
95
+ const key = arg.substring(2, eqIndex);
96
+ const value = arg.substring(eqIndex + 1);
97
+ options[key] = value;
98
+ }
99
+ }
100
+ }
101
+ return options;
102
+ }
103
+
104
+ /**
105
+ * 输出 JSON 格式结果
106
+ * @param {object} data - 要输出的数据
107
+ */
108
+ function outputJson(data) {
109
+ console.log(JSON.stringify(data));
110
+ }
111
+
112
+ /**
113
+ * 输出错误信息
114
+ * @param {string} message - 错误信息
115
+ * @param {object} extra - 额外信息
116
+ */
117
+ function outputError(message, extra = {}) {
118
+ console.log(JSON.stringify({ error: message, ...extra }));
119
+ }
120
+
121
+ /**
122
+ * 输出成功信息
123
+ * @param {object} data - 成功数据
124
+ */
125
+ function outputSuccess(data) {
126
+ console.log(JSON.stringify({ success: true, ...data }));
127
+ }
128
+
129
+ module.exports = {
130
+ parseColor,
131
+ parseColorToCcColor,
132
+ colorToHex,
133
+ generateUUID,
134
+ generateId,
135
+ parseOptions,
136
+ outputJson,
137
+ outputError,
138
+ outputSuccess
139
+ };
@@ -1,74 +0,0 @@
1
- /**
2
- * delete 命令 - 删除节点
3
- */
4
-
5
- const { loadScene, saveScene, collectNodeAndChildren, rebuildReferences, refreshEditor, loadScriptMap, buildTree } = require('../lib/fire-utils');
6
-
7
- function run(args) {
8
- if (args.length < 2) {
9
- console.log(JSON.stringify({ error: '用法: cocos2.4 delete <场景文件路径> <节点索引>' }));
10
- return;
11
- }
12
-
13
- const scenePath = args[0];
14
- const nodeRef = args[1];
15
-
16
- // 节点必须使用数字索引
17
- if (!/^\d+$/.test(nodeRef)) {
18
- console.log(JSON.stringify({ error: '节点索引必须是数字,请先用 tree 命令查看节点索引' }));
19
- return;
20
- }
21
-
22
- try {
23
- const data = loadScene(scenePath);
24
-
25
- const nodeIndex = parseInt(nodeRef);
26
-
27
- if (!data[nodeIndex]) {
28
- console.log(JSON.stringify({ error: `无效的节点索引: ${nodeRef}` }));
29
- return;
30
- }
31
-
32
- if (nodeIndex <= 1) {
33
- console.log(JSON.stringify({ error: '不能删除根节点' }));
34
- return;
35
- }
36
-
37
- const node = data[nodeIndex];
38
-
39
- // 收集所有需要删除的索引(节点 + 子节点 + 组件)
40
- const indicesToDelete = collectNodeAndChildren(data, nodeIndex);
41
-
42
- // 从父节点的 _children 中移除引用
43
- if (node._parent) {
44
- const parentIndex = node._parent.__id__;
45
- const parent = data[parentIndex];
46
- if (parent && parent._children) {
47
- parent._children = parent._children.filter(c => c.__id__ !== nodeIndex);
48
- }
49
- }
50
-
51
- // 重建引用(更新所有 __id__)
52
- rebuildReferences(data, indicesToDelete);
53
-
54
- // 真正删除元素(从大到小排序,避免索引错乱)
55
- const sortedIndices = Array.from(indicesToDelete).sort((a, b) => b - a);
56
- for (const idx of sortedIndices) {
57
- data.splice(idx, 1);
58
- }
59
-
60
- // 保存场景
61
- saveScene(scenePath, data);
62
-
63
- // 触发编辑器刷新
64
- refreshEditor(scenePath);
65
-
66
- // 返回删除后的节点树
67
- const scriptMap = loadScriptMap(scenePath);
68
- console.log(buildTree(data, scriptMap, 1));
69
- } catch (err) {
70
- console.log(JSON.stringify({ error: err.message }));
71
- }
72
- }
73
-
74
- module.exports = { run };
@@ -1,63 +0,0 @@
1
- /**
2
- * remove-component 命令 - 删除节点上的组件
3
- */
4
-
5
- const { loadScene, saveScene } = require('../lib/fire-utils');
6
-
7
- function run(args) {
8
- if (args.length < 2) {
9
- console.log('用法: cocos2.4 remove-component <场景文件路径> <组件索引>');
10
- return;
11
- }
12
-
13
- const scenePath = args[0];
14
- const compIndex = parseInt(args[1]);
15
-
16
- if (isNaN(compIndex)) {
17
- console.log('错误: 组件索引必须是数字');
18
- return;
19
- }
20
-
21
- try {
22
- const data = loadScene(scenePath);
23
-
24
- // 检查组件数据是否存在
25
- const compData = data[compIndex];
26
- if (!compData) {
27
- console.log(`错误: 组件索引 ${compIndex} 不存在`);
28
- return;
29
- }
30
-
31
- const compType = compData.__type__;
32
- const nodeId = compData.node?.__id__;
33
-
34
- if (nodeId === undefined) {
35
- console.log(`错误: 组件 ${compIndex} 没有关联的节点`);
36
- return;
37
- }
38
-
39
- const node = data[nodeId];
40
- if (!node) {
41
- console.log(`错误: 组件关联的节点 ${nodeId} 不存在`);
42
- return;
43
- }
44
-
45
- // 从节点的 _components 中移除引用
46
- if (node._components) {
47
- node._components = node._components.filter(c => c.__id__ !== compIndex);
48
- }
49
-
50
- // 从数组中删除组件(设为 null,保持索引)
51
- data[compIndex] = null;
52
-
53
- // 保存场景
54
- saveScene(scenePath, data);
55
-
56
- console.log(`已删除组件: ${compType} (索引: ${compIndex})`);
57
- console.log(`从节点: ${node._name || '(unnamed)'} (索引: ${nodeId})`);
58
- } catch (err) {
59
- console.log(`错误: ${err.message}`);
60
- }
61
- }
62
-
63
- module.exports = { run };
@@ -1,404 +0,0 @@
1
- /**
2
- * Cocos Creator 2.4.x 组件模板
3
- * 从编辑器导出的默认组件属性
4
- */
5
-
6
- function generateId() {
7
- const chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
8
- let result = '';
9
- for (let i = 0; i < 22; i++) {
10
- result += chars.charAt(Math.floor(Math.random() * chars.length));
11
- }
12
- return result;
13
- }
14
-
15
- const DEFAULT_MATERIAL = { "__uuid__": "eca5d2f2-8ef6-41c2-bbe6-f9c79d09c432" };
16
- const DEFAULT_SPRITE_FRAME = { "__uuid__": "8cdb44ac-a3f6-449f-b354-7cd48cf84061" };
17
- const SPLASH_SPRITE_FRAME = { "__uuid__": "a23235d1-15db-4b95-8439-a2e005bfff91" };
18
- const BUTTON_NORMAL_SPRITE = { "__uuid__": "f0048c10-f03e-4c97-b9d3-3506e1d58952" };
19
- const BUTTON_PRESSED_SPRITE = { "__uuid__": "e9ec654c-97a2-4787-9325-e6a10375219a" };
20
- const BUTTON_DISABLED_SPRITE = { "__uuid__": "29158224-f8dd-4661-a796-1ffab537140e" };
21
- const PARTICLE_FILE = { "__uuid__": "b2687ac4-099e-403c-a192-ff477686f4f5" };
22
- const PARTICLE_SPRITE = { "__uuid__": "472df5d3-35e7-4184-9e6c-7f41bee65ee3" };
23
-
24
- const Components = {
25
- sprite: (nodeId) => ({
26
- "__type__": "cc.Sprite",
27
- "_name": "",
28
- "_objFlags": 0,
29
- "node": { "__id__": nodeId },
30
- "_enabled": true,
31
- "_materials": [DEFAULT_MATERIAL],
32
- "_srcBlendFactor": 770,
33
- "_dstBlendFactor": 771,
34
- "_spriteFrame": SPLASH_SPRITE_FRAME,
35
- "_type": 0,
36
- "_sizeMode": 0,
37
- "_fillType": 0,
38
- "_fillCenter": { "__type__": "cc.Vec2", "x": 0, "y": 0 },
39
- "_fillStart": 0,
40
- "_fillRange": 0,
41
- "_isTrimmedMode": true,
42
- "_atlas": null,
43
- "_id": generateId()
44
- }),
45
-
46
- label: (nodeId) => ({
47
- "__type__": "cc.Label",
48
- "_name": "",
49
- "_objFlags": 0,
50
- "node": { "__id__": nodeId },
51
- "_enabled": true,
52
- "_materials": [DEFAULT_MATERIAL],
53
- "_srcBlendFactor": 770,
54
- "_dstBlendFactor": 771,
55
- "_string": "Label",
56
- "_N$string": "Label",
57
- "_fontSize": 40,
58
- "_lineHeight": 40,
59
- "_enableWrapText": true,
60
- "_N$file": null,
61
- "_isSystemFontUsed": true,
62
- "_spacingX": 0,
63
- "_batchAsBitmap": false,
64
- "_styleFlags": 0,
65
- "_underlineHeight": 0,
66
- "_N$horizontalAlign": 1,
67
- "_N$verticalAlign": 1,
68
- "_N$fontFamily": "Arial",
69
- "_N$overflow": 0,
70
- "_N$cacheMode": 0,
71
- "_id": generateId()
72
- }),
73
-
74
- button: (nodeId) => ({
75
- "__type__": "cc.Button",
76
- "_name": "",
77
- "_objFlags": 0,
78
- "node": { "__id__": nodeId },
79
- "_enabled": true,
80
- "_normalMaterial": null,
81
- "_grayMaterial": null,
82
- "duration": 0.1,
83
- "zoomScale": 1.2,
84
- "clickEvents": [],
85
- "_N$interactable": true,
86
- "_N$enableAutoGrayEffect": false,
87
- "_N$transition": 3,
88
- "transition": 3,
89
- "_N$normalColor": {
90
- "__type__": "cc.Color",
91
- "r": 255,
92
- "g": 255,
93
- "b": 255,
94
- "a": 255
95
- },
96
- "_N$pressedColor": {
97
- "__type__": "cc.Color",
98
- "r": 200,
99
- "g": 200,
100
- "b": 200,
101
- "a": 255
102
- },
103
- "pressedColor": {
104
- "__type__": "cc.Color",
105
- "r": 200,
106
- "g": 200,
107
- "b": 200,
108
- "a": 255
109
- },
110
- "_N$hoverColor": {
111
- "__type__": "cc.Color",
112
- "r": 255,
113
- "g": 255,
114
- "b": 255,
115
- "a": 255
116
- },
117
- "hoverColor": {
118
- "__type__": "cc.Color",
119
- "r": 255,
120
- "g": 255,
121
- "b": 255,
122
- "a": 255
123
- },
124
- "_N$disabledColor": {
125
- "__type__": "cc.Color",
126
- "r": 120,
127
- "g": 120,
128
- "b": 120,
129
- "a": 200
130
- },
131
- "_N$normalSprite": BUTTON_NORMAL_SPRITE,
132
- "_N$pressedSprite": BUTTON_PRESSED_SPRITE,
133
- "pressedSprite": BUTTON_PRESSED_SPRITE,
134
- "_N$hoverSprite": BUTTON_NORMAL_SPRITE,
135
- "hoverSprite": BUTTON_NORMAL_SPRITE,
136
- "_N$disabledSprite": BUTTON_DISABLED_SPRITE,
137
- "_N$target": null,
138
- "_id": generateId()
139
- }),
140
-
141
- layout: (nodeId) => ({
142
- "__type__": "cc.Layout",
143
- "_name": "",
144
- "_objFlags": 0,
145
- "node": { "__id__": nodeId },
146
- "_enabled": true,
147
- "_layoutSize": {
148
- "__type__": "cc.Size",
149
- "width": 200,
150
- "height": 150
151
- },
152
- "_resize": 1,
153
- "_N$layoutType": 2,
154
- "_N$cellSize": {
155
- "__type__": "cc.Size",
156
- "width": 40,
157
- "height": 40
158
- },
159
- "_N$startAxis": 0,
160
- "_N$paddingLeft": 0,
161
- "_N$paddingRight": 0,
162
- "_N$paddingTop": 0,
163
- "_N$paddingBottom": 0,
164
- "_N$spacingX": 0,
165
- "_N$spacingY": 0,
166
- "_N$verticalDirection": 1,
167
- "_N$horizontalDirection": 0,
168
- "_N$affectedByScale": false,
169
- "_id": generateId()
170
- }),
171
-
172
- widget: (nodeId) => ({
173
- "__type__": "cc.Widget",
174
- "_name": "",
175
- "_objFlags": 0,
176
- "node": { "__id__": nodeId },
177
- "_enabled": true,
178
- "alignMode": 1,
179
- "_target": null,
180
- "_alignFlags": 0,
181
- "_left": 0,
182
- "_right": 0,
183
- "_top": 0,
184
- "_bottom": 0,
185
- "_verticalCenter": 0,
186
- "_horizontalCenter": 0,
187
- "_isAbsLeft": true,
188
- "_isAbsRight": true,
189
- "_isAbsTop": true,
190
- "_isAbsBottom": true,
191
- "_isAbsHorizontalCenter": true,
192
- "_isAbsVerticalCenter": true,
193
- "_originalWidth": 0,
194
- "_originalHeight": 0,
195
- "_id": generateId()
196
- }),
197
-
198
- particleSystem: (nodeId) => ({
199
- "__type__": "cc.ParticleSystem",
200
- "_name": "",
201
- "_objFlags": 0,
202
- "node": { "__id__": nodeId },
203
- "_enabled": true,
204
- "_materials": [DEFAULT_MATERIAL],
205
- "_srcBlendFactor": 770,
206
- "_dstBlendFactor": 1,
207
- "_custom": false,
208
- "_file": PARTICLE_FILE,
209
- "_spriteFrame": PARTICLE_SPRITE,
210
- "_texture": null,
211
- "_stopped": true,
212
- "playOnLoad": true,
213
- "autoRemoveOnFinish": false,
214
- "totalParticles": 200,
215
- "duration": -1,
216
- "emissionRate": 999.999985098839,
217
- "life": 0.20000000298023224,
218
- "lifeVar": 0.5,
219
- "_startColor": {
220
- "__type__": "cc.Color",
221
- "r": 202,
222
- "g": 200,
223
- "b": 86,
224
- "a": 163
225
- },
226
- "_startColorVar": {
227
- "__type__": "cc.Color",
228
- "r": 229,
229
- "g": 255,
230
- "b": 173,
231
- "a": 198
232
- },
233
- "_endColor": {
234
- "__type__": "cc.Color",
235
- "r": 173,
236
- "g": 161,
237
- "b": 19,
238
- "a": 214
239
- },
240
- "_endColorVar": {
241
- "__type__": "cc.Color",
242
- "r": 107,
243
- "g": 249,
244
- "b": 249,
245
- "a": 188
246
- },
247
- "angle": 360,
248
- "angleVar": 360,
249
- "startSize": 3.369999885559082,
250
- "startSizeVar": 50,
251
- "endSize": 30.31999969482422,
252
- "endSizeVar": 0,
253
- "startSpin": -47.369998931884766,
254
- "startSpinVar": 0,
255
- "endSpin": -47.369998931884766,
256
- "endSpinVar": -142.11000061035156,
257
- "sourcePos": {
258
- "__type__": "cc.Vec2",
259
- "x": 0,
260
- "y": 0
261
- },
262
- "posVar": {
263
- "__type__": "cc.Vec2",
264
- "x": 7,
265
- "y": 7
266
- },
267
- "_positionType": 1,
268
- "positionType": 1,
269
- "emitterMode": 0,
270
- "gravity": {
271
- "__type__": "cc.Vec2",
272
- "x": 0.25,
273
- "y": 0.8600000143051147
274
- },
275
- "speed": 0,
276
- "speedVar": 190.7899932861328,
277
- "tangentialAccel": -92.11000061035156,
278
- "tangentialAccelVar": 65.79000091552734,
279
- "radialAccel": -671.0499877929688,
280
- "radialAccelVar": 65.79000091552734,
281
- "rotationIsDir": false,
282
- "startRadius": 0,
283
- "startRadiusVar": 0,
284
- "endRadius": 0,
285
- "endRadiusVar": 0,
286
- "rotatePerS": 0,
287
- "rotatePerSVar": 0,
288
- "_N$preview": true,
289
- "_id": generateId()
290
- }),
291
-
292
- camera: (nodeId) => ({
293
- "__type__": "cc.Camera",
294
- "_name": "",
295
- "_objFlags": 0,
296
- "node": { "__id__": nodeId },
297
- "_enabled": true,
298
- "_cullingMask": 4294967295,
299
- "_clearFlags": 7,
300
- "_backgroundColor": {
301
- "__type__": "cc.Color",
302
- "r": 0,
303
- "g": 0,
304
- "b": 0,
305
- "a": 255
306
- },
307
- "_depth": -1,
308
- "_zoomRatio": 1,
309
- "_targetTexture": null,
310
- "_fov": 60,
311
- "_orthoSize": 10,
312
- "_nearClip": 1,
313
- "_farClip": 4096,
314
- "_ortho": true,
315
- "_rect": {
316
- "__type__": "cc.Rect",
317
- "x": 0,
318
- "y": 0,
319
- "width": 1,
320
- "height": 1
321
- },
322
- "_renderStages": 1,
323
- "_alignWithScreen": true,
324
- "_id": generateId()
325
- }),
326
-
327
- canvas: (nodeId) => ({
328
- "__type__": "cc.Canvas",
329
- "_name": "",
330
- "_objFlags": 0,
331
- "node": { "__id__": nodeId },
332
- "_enabled": true,
333
- "_designResolution": {
334
- "__type__": "cc.Size",
335
- "width": 960,
336
- "height": 640
337
- },
338
- "_fitWidth": false,
339
- "_fitHeight": true,
340
- "_id": generateId()
341
- })
342
- };
343
-
344
- /**
345
- * 创建默认节点数据
346
- */
347
- function createNodeData(name, parentId, options = {}) {
348
- return {
349
- "__type__": "cc.Node",
350
- "_name": name,
351
- "_objFlags": 0,
352
- "_parent": { "__id__": parentId },
353
- "_children": [],
354
- "_active": options.active !== false,
355
- "_components": [],
356
- "_prefab": null,
357
- "_opacity": 255,
358
- "_color": {
359
- "__type__": "cc.Color",
360
- "r": 255,
361
- "g": 255,
362
- "b": 255,
363
- "a": 255
364
- },
365
- "_contentSize": {
366
- "__type__": "cc.Size",
367
- "width": options.width || 0,
368
- "height": options.height || 0
369
- },
370
- "_anchorPoint": {
371
- "__type__": "cc.Vec2",
372
- "x": 0.5,
373
- "y": 0.5
374
- },
375
- "_trs": {
376
- "__type__": "TypedArray",
377
- "ctor": "Float64Array",
378
- "array": [
379
- options.x || 0,
380
- options.y || 0,
381
- 0, 0, 0, 0, 1,
382
- 1, 1, 1
383
- ]
384
- },
385
- "_eulerAngles": {
386
- "__type__": "cc.Vec3",
387
- "x": 0,
388
- "y": 0,
389
- "z": 0
390
- },
391
- "_skewX": 0,
392
- "_skewY": 0,
393
- "_is3DNode": false,
394
- "_groupIndex": 0,
395
- "groupIndex": 0,
396
- "_id": generateId()
397
- };
398
- }
399
-
400
- module.exports = {
401
- Components,
402
- generateId,
403
- createNodeData
404
- };