cocos2d-cli 1.6.5 → 2.0.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.
Files changed (61) hide show
  1. package/dist/bin/cocos2d-cli.js +64 -0
  2. package/dist/src/commands/add-component.js +3 -0
  3. package/dist/src/commands/add.js +3 -0
  4. package/dist/src/commands/build.js +6 -0
  5. package/dist/src/commands/create-scene.js +3 -0
  6. package/dist/src/commands/get.js +3 -0
  7. package/dist/src/commands/prefab-create.js +109 -0
  8. package/dist/src/commands/remove-component.js +3 -0
  9. package/dist/src/commands/remove.js +3 -0
  10. package/dist/src/commands/screenshot.js +41 -0
  11. package/dist/src/commands/set-component.js +3 -0
  12. package/dist/src/commands/set.js +3 -0
  13. package/dist/src/commands/tree.js +24 -0
  14. package/{src → dist/src}/lib/cc/CCButton.js +115 -122
  15. package/{src → dist/src}/lib/cc/CCCamera.js +83 -93
  16. package/{src → dist/src}/lib/cc/CCCanvas.js +49 -54
  17. package/{src → dist/src}/lib/cc/CCColor.js +30 -32
  18. package/{src → dist/src}/lib/cc/CCComponent.js +39 -60
  19. package/{src → dist/src}/lib/cc/CCLabel.js +139 -146
  20. package/{src → dist/src}/lib/cc/CCNode.js +190 -256
  21. package/{src → dist/src}/lib/cc/CCObject.js +19 -23
  22. package/{src → dist/src}/lib/cc/CCPrefab.js +219 -242
  23. package/{src → dist/src}/lib/cc/CCRect.js +30 -32
  24. package/{src → dist/src}/lib/cc/CCRichText.js +38 -44
  25. package/{src → dist/src}/lib/cc/CCScene.js +32 -42
  26. package/dist/src/lib/cc/CCSceneAsset.js +242 -0
  27. package/{src → dist/src}/lib/cc/CCSize.js +22 -26
  28. package/{src → dist/src}/lib/cc/CCSprite.js +82 -94
  29. package/{src → dist/src}/lib/cc/CCTrs.js +49 -74
  30. package/{src → dist/src}/lib/cc/CCVec2.js +22 -26
  31. package/{src → dist/src}/lib/cc/CCVec3.js +26 -29
  32. package/{src → dist/src}/lib/cc/CCWidget.js +94 -98
  33. package/dist/src/lib/fire-utils.js +86 -0
  34. package/dist/src/lib/json-parser.js +114 -0
  35. package/dist/src/lib/node-utils.js +131 -0
  36. package/{src → dist/src}/lib/screenshot-core.js +221 -285
  37. package/dist/src/lib/templates.js +17 -0
  38. package/dist/src/lib/utils.js +81 -0
  39. package/package.json +13 -6
  40. package/bin/cocos2d-cli.js +0 -152
  41. package/src/commands/add-component.js +0 -112
  42. package/src/commands/add.js +0 -177
  43. package/src/commands/build.js +0 -78
  44. package/src/commands/create-scene.js +0 -181
  45. package/src/commands/get.js +0 -108
  46. package/src/commands/prefab-create.js +0 -111
  47. package/src/commands/remove-component.js +0 -111
  48. package/src/commands/remove.js +0 -99
  49. package/src/commands/screenshot.js +0 -108
  50. package/src/commands/set-component.js +0 -119
  51. package/src/commands/set.js +0 -107
  52. package/src/commands/tree.js +0 -29
  53. package/src/lib/cc/CCSceneAsset.js +0 -303
  54. package/src/lib/cc/index.js +0 -42
  55. package/src/lib/fire-utils.js +0 -374
  56. package/src/lib/json-parser.js +0 -185
  57. package/src/lib/node-utils.js +0 -395
  58. package/src/lib/screenshot/favicon.ico +0 -0
  59. package/src/lib/screenshot/index.html +0 -30
  60. package/src/lib/templates.js +0 -49
  61. package/src/lib/utils.js +0 -202
@@ -1,107 +0,0 @@
1
- /**
2
- * set 命令 - 设置节点属性
3
- */
4
-
5
- const path = require('path');
6
- const fs = require('fs');
7
- const { CCSceneAsset, CCPrefab } = require('../lib/cc');
8
- const { buildTree } = require('../lib/node-utils');
9
- const { loadScriptMap, isPrefab } = require('../lib/fire-utils');
10
-
11
- /**
12
- * 查找节点
13
- */
14
- function findNode(root, nodePath) {
15
- if (!nodePath) return root;
16
-
17
- const parts = nodePath.split('/').filter(p => p);
18
- if (parts.length === 0) return root;
19
-
20
- let current = root;
21
-
22
- if (parts[0] === root._name) {
23
- parts.shift();
24
- }
25
-
26
- for (const part of parts) {
27
- if (!current._children || current._children.length === 0) return null;
28
- const found = current._children.find(c => c._name === part);
29
- if (!found) return null;
30
- current = found;
31
- }
32
-
33
- return current;
34
- }
35
-
36
- function run(args) {
37
- if (args.length < 4) {
38
- console.log(JSON.stringify({ error: '用法: cocos2d-cli set <场景.fire|预制体.prefab> <节点路径> <属性名> <值>' }));
39
- return;
40
- }
41
-
42
- const filePath = args[0];
43
- const nodePath = args[1];
44
- const prop = args[2];
45
- const value = args[3];
46
-
47
- const ext = path.extname(filePath).toLowerCase();
48
-
49
- try {
50
- let root;
51
- let asset;
52
- const json = JSON.parse(fs.readFileSync(filePath, 'utf8'));
53
-
54
- if (ext === '.fire') {
55
- asset = CCSceneAsset.fromJSON(json);
56
- root = asset._scene;
57
- } else if (ext === '.prefab') {
58
- asset = CCPrefab.fromJSON(json);
59
- root = asset._root;
60
- } else {
61
- console.log(JSON.stringify({ error: '不支持的文件类型,仅支持 .fire 和 .prefab' }));
62
- return;
63
- }
64
-
65
- const node = findNode(root, nodePath);
66
-
67
- if (!node) {
68
- console.log(JSON.stringify({ error: `节点不存在: ${nodePath}` }));
69
- return;
70
- }
71
-
72
- // 调用节点的 setProp 方法
73
- if (typeof node.setProp !== 'function') {
74
- console.log(JSON.stringify({ error: '节点不支持 setProp 方法' }));
75
- return;
76
- }
77
-
78
- // 转换数值类型
79
- let parsedValue = value;
80
- if (!isNaN(value) && value !== '') {
81
- parsedValue = parseFloat(value);
82
- } else if (value === 'true') {
83
- parsedValue = true;
84
- } else if (value === 'false') {
85
- parsedValue = false;
86
- }
87
-
88
- // 构造属性对象并调用 setProp
89
- const props = { [prop]: parsedValue };
90
- node.setProp(props);
91
-
92
- // 保存
93
- const data = asset.toJSON();
94
- fs.writeFileSync(filePath, JSON.stringify(data, null, 2), 'utf8');
95
-
96
- // 输出节点树
97
- const scriptMap = loadScriptMap(filePath);
98
- const prefab = isPrefab(data);
99
- const startIndex = prefab ? 0 : 1;
100
- console.log(buildTree(data, scriptMap, startIndex).trim());
101
-
102
- } catch (err) {
103
- console.log(JSON.stringify({ error: err.message }));
104
- }
105
- }
106
-
107
- module.exports = { run };
@@ -1,29 +0,0 @@
1
- /**
2
- * tree 命令 - 查看节点树(支持场景和预制体)
3
- */
4
-
5
- const { loadScene, loadScriptMap, isPrefab } = require('../lib/fire-utils');
6
- const { buildTree } = require('../lib/node-utils');
7
-
8
- function run(args) {
9
- const filePath = args[0];
10
-
11
- if (!filePath) {
12
- console.log(JSON.stringify({ error: '用法: cocos2d-cli tree <场景.fire | 预制体.prefab>' }));
13
- return;
14
- }
15
-
16
- try {
17
- const data = loadScene(filePath);
18
- const scriptMap = loadScriptMap(filePath);
19
- const prefab = isPrefab(data);
20
-
21
- // Prefab 从索引 0 开始,Scene 从索引 1 开始
22
- const startIndex = prefab ? 0 : 1;
23
- console.log(buildTree(data, scriptMap, startIndex).trim());
24
- } catch (err) {
25
- console.log(JSON.stringify({ error: err.message }));
26
- }
27
- }
28
-
29
- module.exports = { run };
@@ -1,303 +0,0 @@
1
- const CCObject = require('./CCObject');
2
- const CCScene = require('./CCScene');
3
- const CCNode = require('./CCNode');
4
- const CCCanvas = require('./CCCanvas');
5
- const CCWidget = require('./CCWidget');
6
- const CCSprite = require('./CCSprite');
7
- const CCLabel = require('./CCLabel');
8
- const CCButton = require('./CCButton');
9
- const CCCamera = require('./CCCamera');
10
- const { generateCompressedUUID } = require('../utils');
11
-
12
- /**
13
- * Cocos Creator 场景资源类
14
- * 场景文件的容器,包含 CCScene 根节点
15
- */
16
- class CCSceneAsset extends CCObject {
17
- constructor() {
18
- super('');
19
- this.__type__ = 'cc.SceneAsset';
20
-
21
- this._native = '';
22
- this._scene = null; // CCScene 引用
23
- }
24
-
25
- /**
26
- * 获取场景根节点
27
- */
28
- getScene() {
29
- return this._scene;
30
- }
31
-
32
- /**
33
- * 通过路径查找节点
34
- */
35
- findNode(path) {
36
- if (!this._scene) return null;
37
- return this._scene.findChild(path);
38
- }
39
-
40
- /**
41
- * 添加节点到场景
42
- */
43
- addNode(node) {
44
- if (!this._scene) return null;
45
-
46
- node._parent = this._scene;
47
- if (!this._scene._children) this._scene._children = [];
48
- this._scene._children.push(node);
49
-
50
- // 场景中的节点需要 _id
51
- node._id = generateCompressedUUID();
52
-
53
- return node;
54
- }
55
-
56
- /**
57
- * 删除节点
58
- */
59
- removeNode(node) {
60
- if (!node || !node._parent) return false;
61
-
62
- const idx = node._parent._children.indexOf(node);
63
- if (idx > -1) {
64
- node._parent._children.splice(idx, 1);
65
- return true;
66
- }
67
- return false;
68
- }
69
-
70
- /**
71
- * 添加组件到节点
72
- */
73
- addComponent(node, component) {
74
- if (!node._components) node._components = [];
75
- component.node = node;
76
- node._components.push(component);
77
- return component;
78
- }
79
-
80
- /**
81
- * 从 JSON 解析
82
- */
83
- static fromJSON(json) {
84
- const asset = new CCSceneAsset();
85
- const objects = [];
86
-
87
- // 第一遍:创建所有对象
88
- json.forEach((item, index) => {
89
- objects[index] = createObject(item);
90
- });
91
-
92
- // 第二遍:建立引用关系
93
- json.forEach((item, index) => {
94
- setupReferences(objects[index], item, objects);
95
- });
96
-
97
- asset._scene = objects[1];
98
- return asset;
99
- }
100
-
101
- /**
102
- * 序列化为 JSON
103
- * 顺序:SceneAsset → Scene → 节点树(节点 → 子节点 → 组件)
104
- */
105
- toJSON() {
106
- const result = [];
107
- const indexMap = new Map();
108
-
109
- // 0: SceneAsset 头
110
- indexMap.set(this, 0);
111
- result.push({
112
- __type__: 'cc.SceneAsset',
113
- _name: '',
114
- _objFlags: 0,
115
- _native: '',
116
- scene: { __id__: 1 }
117
- });
118
-
119
- // 1: Scene
120
- indexMap.set(this._scene, 1);
121
- result.push(null); // 占位
122
-
123
- // 递归处理节点
124
- const processNode = (node) => {
125
- if (!node) return;
126
-
127
- // 添加节点
128
- indexMap.set(node, result.length);
129
- result.push(null); // 占位
130
-
131
- // 递归处理子节点
132
- if (node._children) {
133
- node._children.forEach(child => processNode(child));
134
- }
135
-
136
- // 添加组件
137
- if (node._components) {
138
- node._components.forEach(comp => {
139
- indexMap.set(comp, result.length);
140
- result.push(componentToJSON(comp, indexMap));
141
- });
142
- }
143
-
144
- // 生成节点 JSON
145
- result[indexMap.get(node)] = {
146
- __type__: 'cc.Node',
147
- _name: node._name,
148
- _objFlags: node._objFlags,
149
- _parent: node._parent ? { __id__: indexMap.get(node._parent) } : null,
150
- _children: (node._children || []).map(c => ({ __id__: indexMap.get(c) })),
151
- _active: node._active,
152
- _components: (node._components || []).map(c => ({ __id__: indexMap.get(c) })),
153
- _prefab: null,
154
- _opacity: node._opacity,
155
- _color: node._color.toJSON(),
156
- _contentSize: node._contentSize.toJSON(),
157
- _anchorPoint: node._anchorPoint.toJSON(),
158
- _trs: node._trs.toJSON(),
159
- _eulerAngles: node._eulerAngles.toJSON(),
160
- _skewX: node._skewX,
161
- _skewY: node._skewY,
162
- _is3DNode: node._is3DNode,
163
- _groupIndex: node._groupIndex,
164
- groupIndex: node.groupIndex,
165
- _id: node._id || ''
166
- };
167
- };
168
-
169
- // 处理 Scene 的子节点
170
- if (this._scene && this._scene._children) {
171
- this._scene._children.forEach(child => processNode(child));
172
- }
173
-
174
- // 填充 Scene
175
- result[1] = {
176
- __type__: 'cc.Scene',
177
- _objFlags: this._scene._objFlags,
178
- _parent: null,
179
- _children: (this._scene._children || []).map(c => ({ __id__: indexMap.get(c) })),
180
- _active: this._scene._active,
181
- _components: [],
182
- _prefab: null,
183
- _opacity: this._scene._opacity,
184
- _color: this._scene._color.toJSON(),
185
- _contentSize: this._scene._contentSize.toJSON(),
186
- _anchorPoint: this._scene._anchorPoint.toJSON(),
187
- _trs: this._scene._trs.toJSON(),
188
- _is3DNode: this._scene._is3DNode,
189
- _groupIndex: this._scene._groupIndex,
190
- groupIndex: this._scene.groupIndex,
191
- autoReleaseAssets: this._scene.autoReleaseAssets || false,
192
- _id: this._scene._id || ''
193
- };
194
-
195
- return result;
196
- }
197
- }
198
-
199
- function componentToJSON(comp, indexMap) {
200
- const json = {
201
- __type__: comp.__type__,
202
- _name: comp._name || '',
203
- _objFlags: comp._objFlags || 0,
204
- node: { __id__: indexMap.get(comp.node) },
205
- _enabled: comp._enabled !== false
206
- };
207
-
208
- for (const key of Object.keys(comp)) {
209
- if (['__type__', '_name', '_objFlags', 'node', '_enabled', '_id'].includes(key)) continue;
210
- const val = comp[key];
211
- if (val === undefined) continue;
212
- json[key] = val && typeof val.toJSON === 'function' ? val.toJSON() : val;
213
- }
214
-
215
- json._id = comp._id || '';
216
-
217
- return json;
218
- }
219
-
220
- function createObject(item) {
221
- const type = item.__type__;
222
-
223
- if (type === 'cc.SceneAsset') return null; // 跳过,由 fromJSON 处理
224
- if (type === 'cc.Scene') {
225
- const scene = new CCScene();
226
- scene._id = item._id || '';
227
- if (item._active !== undefined) scene._active = item._active;
228
- if (item.autoReleaseAssets !== undefined) scene.autoReleaseAssets = item.autoReleaseAssets;
229
- return scene;
230
- }
231
-
232
- if (type === 'cc.Node') {
233
- const node = new CCNode(item._name || 'Node');
234
- node._id = item._id || '';
235
- copyNodeProps(node, item);
236
- return node;
237
- }
238
-
239
- // 组件 - 创建类实例
240
- const comp = createComponentInstance(type);
241
- if (comp) {
242
- copyComponentProps(comp, item);
243
- return comp;
244
- }
245
-
246
- // 未知类型,返回普通对象
247
- const obj = { __type__: type };
248
- for (const key of Object.keys(item)) {
249
- obj[key] = item[key];
250
- }
251
- return obj;
252
- }
253
-
254
- function createComponentInstance(type) {
255
- switch (type) {
256
- case 'cc.Canvas': return new CCCanvas();
257
- case 'cc.Widget': return new CCWidget();
258
- case 'cc.Sprite': return new CCSprite();
259
- case 'cc.Label': return new CCLabel();
260
- case 'cc.Button': return new CCButton();
261
- case 'cc.Camera': return new CCCamera();
262
- default: return null;
263
- }
264
- }
265
-
266
- function copyComponentProps(comp, item) {
267
- for (const key of Object.keys(item)) {
268
- if (['__type__', '_name', '_objFlags', 'node', '_enabled', '_id'].includes(key)) continue;
269
- comp[key] = item[key];
270
- }
271
- if (item._id) comp._id = item._id;
272
- }
273
-
274
- function copyNodeProps(node, item) {
275
- if (item._active !== undefined) node._active = item._active;
276
- if (item._opacity !== undefined) node._opacity = item._opacity;
277
- if (item._is3DNode !== undefined) node._is3DNode = item._is3DNode;
278
- if (item._groupIndex !== undefined) node._groupIndex = item._groupIndex;
279
- if (item.groupIndex !== undefined) node.groupIndex = item.groupIndex;
280
- if (item._skewX !== undefined) node._skewX = item._skewX;
281
- if (item._skewY !== undefined) node._skewY = item._skewY;
282
-
283
- if (item._color) node._color.set(item._color.r, item._color.g, item._color.b, item._color.a);
284
- if (item._contentSize) node._contentSize.set(item._contentSize.width, item._contentSize.height);
285
- if (item._anchorPoint) node._anchorPoint.set(item._anchorPoint.x, item._anchorPoint.y);
286
- if (item._trs?.array) item._trs.array.forEach((v, i) => node._trs.array[i] = v);
287
- if (item._eulerAngles) node._eulerAngles.set(item._eulerAngles.x, item._eulerAngles.y, item._eulerAngles.z);
288
- }
289
-
290
- function setupReferences(obj, item, objects) {
291
- if (!obj) return;
292
-
293
- if (obj.__type__ === 'cc.Scene' || obj.__type__ === 'cc.Node') {
294
- if (item._parent) obj._parent = objects[item._parent.__id__];
295
- if (item._children) obj._children = item._children.map(c => objects[c.__id__]).filter(Boolean);
296
- if (item._components) {
297
- obj._components = item._components.map(c => objects[c.__id__]).filter(Boolean);
298
- obj._components.forEach(c => { if (c) c.node = obj; });
299
- }
300
- }
301
- }
302
-
303
- module.exports = CCSceneAsset;
@@ -1,42 +0,0 @@
1
- const CCObject = require('./CCObject');
2
- const CCNode = require('./CCNode');
3
- const CCScene = require('./CCScene');
4
- const CCSceneAsset = require('./CCSceneAsset');
5
- const { CCPrefab, CCPrefabInfo } = require('./CCPrefab');
6
- const CCColor = require('./CCColor');
7
- const CCSize = require('./CCSize');
8
- const CCVec2 = require('./CCVec2');
9
- const CCVec3 = require('./CCVec3');
10
- const CCTrs = require('./CCTrs');
11
- const CCRect = require('./CCRect');
12
- const CCComponent = require('./CCComponent');
13
- const CCCanvas = require('./CCCanvas');
14
- const CCWidget = require('./CCWidget');
15
- const CCCamera = require('./CCCamera');
16
- const CCSprite = require('./CCSprite');
17
- const CCLabel = require('./CCLabel');
18
- const CCButton = require('./CCButton');
19
- const CCRichText = require('./CCRichText');
20
-
21
- module.exports = {
22
- CCObject,
23
- CCNode,
24
- CCScene,
25
- CCSceneAsset,
26
- CCPrefab,
27
- CCPrefabInfo,
28
- CCColor,
29
- CCSize,
30
- CCVec2,
31
- CCVec3,
32
- CCTrs,
33
- CCRect,
34
- CCComponent,
35
- CCCanvas,
36
- CCWidget,
37
- CCCamera,
38
- CCSprite,
39
- CCLabel,
40
- CCButton,
41
- CCRichText
42
- };