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.
@@ -1,129 +1,13 @@
1
1
  /**
2
- * get 命令 - 获取节点信息
3
- * 返回与编辑器 Inspector 面板一致的所有属性
2
+ * get 命令 - 获取节点信息
4
3
  */
5
4
 
6
5
  const { loadScene, buildMaps, findNodeIndex } = require('../lib/fire-utils');
7
-
8
- /**
9
- * 将 _color 对象转为 #RRGGBB 字符串
10
- */
11
- function colorToHex(color) {
12
- if (!color) return '#ffffff';
13
- const r = (color.r || 0).toString(16).padStart(2, '0');
14
- const g = (color.g || 0).toString(16).padStart(2, '0');
15
- const b = (color.b || 0).toString(16).padStart(2, '0');
16
- return `#${r}${g}${b}`;
17
- }
18
-
19
- /**
20
- * 提取组件的关键属性(对应 Inspector 面板显示)
21
- */
22
- function extractComponentProps(comp) {
23
- if (!comp) return null;
24
- const type = comp.__type__;
25
-
26
- // 只在 disabled 时才输出 enabled 字段
27
- const base = comp._enabled ? { type } : { type, enabled: false };
28
-
29
- // 清理对象中的 __type__ 字段
30
- const clean = (obj) => {
31
- if (!obj || typeof obj !== 'object') return obj;
32
- if (Array.isArray(obj)) return obj.map(clean);
33
- const result = {};
34
- for (const [k, v] of Object.entries(obj)) {
35
- if (k === '__type__') continue;
36
- result[k] = typeof v === 'object' ? clean(v) : v;
37
- }
38
- return result;
39
- };
40
-
41
- switch (type) {
42
- case 'cc.Sprite':
43
- return {
44
- ...base,
45
- spriteFrame: comp._spriteFrame?.__uuid__ || null,
46
- sizeMode: ['CUSTOM', 'TRIMMED', 'RAW'][comp._sizeMode] || comp._sizeMode,
47
- spriteType: ['SIMPLE', 'SLICED', 'TILED', 'FILLED', 'MESH'][comp._type] || comp._type,
48
- trim: comp._isTrimmedMode
49
- };
50
- case 'cc.Label':
51
- return {
52
- ...base,
53
- string: comp._string,
54
- fontSize: comp._fontSize,
55
- lineHeight: comp._lineHeight,
56
- horizontalAlign: ['LEFT', 'CENTER', 'RIGHT'][comp._N$horizontalAlign] || comp._N$horizontalAlign,
57
- verticalAlign: ['TOP', 'CENTER', 'BOTTOM'][comp._N$verticalAlign] || comp._N$verticalAlign,
58
- overflow: ['NONE', 'CLAMP', 'SHRINK', 'RESIZE_HEIGHT'][comp._N$overflow] || comp._N$overflow,
59
- fontFamily: comp._N$fontFamily,
60
- enableWrapText: comp._enableWrapText
61
- };
62
- case 'cc.Button':
63
- return {
64
- ...base,
65
- interactable: comp._N$interactable,
66
- transition: ['NONE', 'COLOR', 'SPRITE', 'SCALE'][comp._N$transition] || comp._N$transition,
67
- zoomScale: comp.zoomScale,
68
- duration: comp.duration
69
- };
70
- case 'cc.Widget':
71
- return {
72
- ...base,
73
- alignMode: ['ONCE', 'ON_WINDOW_RESIZE', 'ALWAYS'][comp.alignMode] || comp.alignMode,
74
- left: comp._left,
75
- right: comp._right,
76
- top: comp._top,
77
- bottom: comp._bottom
78
- };
79
- case 'cc.Layout':
80
- return {
81
- ...base,
82
- layoutType: ['NONE', 'HORIZONTAL', 'VERTICAL', 'GRID'][comp._N$layoutType] || comp._N$layoutType,
83
- spacingX: comp._N$spacingX,
84
- spacingY: comp._N$spacingY,
85
- paddingLeft: comp._N$paddingLeft,
86
- paddingRight: comp._N$paddingRight,
87
- paddingTop: comp._N$paddingTop,
88
- paddingBottom: comp._N$paddingBottom
89
- };
90
- case 'cc.Canvas':
91
- return {
92
- ...base,
93
- designResolution: clean(comp._designResolution),
94
- fitWidth: comp._fitWidth,
95
- fitHeight: comp._fitHeight
96
- };
97
- case 'cc.Camera':
98
- return {
99
- ...base,
100
- depth: comp._depth,
101
- zoomRatio: comp._zoomRatio,
102
- ortho: comp._ortho,
103
- cullingMask: comp._cullingMask
104
- };
105
- case 'cc.ParticleSystem':
106
- return {
107
- ...base,
108
- playOnLoad: comp.playOnLoad,
109
- totalParticles: comp.totalParticles,
110
- duration: comp.duration
111
- };
112
- default:
113
- // 未知组件:返回去掉内部字段的原始属性
114
- const result = { ...base };
115
- for (const key of Object.keys(comp)) {
116
- if (!key.startsWith('_') && key !== '__type__') {
117
- result[key] = comp[key];
118
- }
119
- }
120
- return result;
121
- }
122
- }
6
+ const { getNodeState } = require('../lib/node-utils');
123
7
 
124
8
  function run(args) {
125
9
  if (args.length < 2) {
126
- console.log(JSON.stringify({ error: '用法: cocos2.4 get <场景.fire | 预制体.prefab> <节点索引|名称>' }));
10
+ console.log(JSON.stringify({ error: '用法: cocos2d-cli get <场景.fire | 预制体.prefab> <节点索引|名称>' }));
127
11
  return;
128
12
  }
129
13
 
@@ -134,7 +18,6 @@ function run(args) {
134
18
  const data = loadScene(scenePath);
135
19
  const { indexMap } = buildMaps(data);
136
20
 
137
- // 查找节点
138
21
  const idx = findNodeIndex(data, indexMap, nodeRef);
139
22
 
140
23
  if (idx === null || idx < 0 || idx >= data.length) {
@@ -148,43 +31,11 @@ function run(args) {
148
31
  return;
149
32
  }
150
33
 
151
- // _trs 数组解析变换属性
152
- // 格式: [x, y, z, qx, qy, qz, qw, scaleX, scaleY, scaleZ]
153
- const trs = node._trs?.array || [0,0,0, 0,0,0,1, 1,1,1];
154
-
155
- // 组件详细信息
156
- const components = (node._components || []).map(ref => {
157
- const comp = data[ref.__id__];
158
- return extractComponentProps(comp);
159
- });
160
-
161
- // 子节点名称
162
- const children = (node._children || []).map(ref => data[ref.__id__]?._name || '(unknown)');
163
-
164
- const info = indexMap[idx] || {};
165
-
166
- // 精简 JSON 输出
167
- const result = {
168
- name: info.name,
169
- active: node._active,
170
- position: { x: trs[0], y: trs[1] },
171
- rotation: node._eulerAngles?.z ?? 0,
172
- scale: { x: trs[7], y: trs[8] },
173
- anchor: { x: node._anchorPoint?.x ?? 0.5, y: node._anchorPoint?.y ?? 0.5 },
174
- size: { w: node._contentSize?.width ?? 0, h: node._contentSize?.height ?? 0 },
175
- color: colorToHex(node._color),
176
- opacity: node._opacity ?? 255,
177
- group: node._groupIndex ?? 0
178
- };
179
-
180
- if (children.length > 0) result.children = children;
181
- if (components.length > 0) result.components = components;
182
-
183
- console.log(JSON.stringify(result));
34
+ console.log(JSON.stringify(getNodeState(data, node, idx)));
184
35
 
185
36
  } catch (err) {
186
37
  console.log(JSON.stringify({ error: err.message }));
187
38
  }
188
39
  }
189
40
 
190
- module.exports = { run };
41
+ module.exports = { run };