cocos2d-cli 1.6.4 → 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.
- package/data/script_map.json +25 -25
- package/dist/bin/cocos2d-cli.js +64 -0
- package/dist/src/commands/add-component.js +3 -0
- package/dist/src/commands/add.js +3 -0
- package/dist/src/commands/build.js +6 -0
- package/dist/src/commands/create-scene.js +3 -0
- package/dist/src/commands/get.js +3 -0
- package/dist/src/commands/prefab-create.js +109 -0
- package/dist/src/commands/remove-component.js +3 -0
- package/dist/src/commands/remove.js +3 -0
- package/dist/src/commands/screenshot.js +41 -0
- package/dist/src/commands/set-component.js +3 -0
- package/dist/src/commands/set.js +3 -0
- package/dist/src/commands/tree.js +24 -0
- package/{src → dist/src}/lib/cc/CCButton.js +26 -33
- package/{src → dist/src}/lib/cc/CCCamera.js +20 -30
- package/{src → dist/src}/lib/cc/CCCanvas.js +10 -15
- package/{src → dist/src}/lib/cc/CCColor.js +6 -8
- package/{src → dist/src}/lib/cc/CCComponent.js +8 -29
- package/{src → dist/src}/lib/cc/CCLabel.js +44 -51
- package/{src → dist/src}/lib/cc/CCNode.js +52 -118
- package/{src → dist/src}/lib/cc/CCObject.js +4 -8
- package/{src → dist/src}/lib/cc/CCPrefab.js +77 -100
- package/{src → dist/src}/lib/cc/CCRect.js +6 -8
- package/{src → dist/src}/lib/cc/CCRichText.js +10 -16
- package/{src → dist/src}/lib/cc/CCScene.js +3 -13
- package/dist/src/lib/cc/CCSceneAsset.js +242 -0
- package/{src → dist/src}/lib/cc/CCSize.js +4 -8
- package/{src → dist/src}/lib/cc/CCSprite.js +21 -33
- package/{src → dist/src}/lib/cc/CCTrs.js +4 -29
- package/{src → dist/src}/lib/cc/CCVec2.js +4 -8
- package/{src → dist/src}/lib/cc/CCVec3.js +5 -8
- package/{src → dist/src}/lib/cc/CCWidget.js +20 -24
- package/dist/src/lib/fire-utils.js +86 -0
- package/dist/src/lib/json-parser.js +114 -0
- package/dist/src/lib/node-utils.js +131 -0
- package/{src → dist/src}/lib/screenshot-core.js +54 -119
- package/dist/src/lib/templates.js +17 -0
- package/dist/src/lib/utils.js +81 -0
- package/package.json +40 -33
- package/bin/cocos2d-cli.js +0 -152
- package/src/commands/add-component.js +0 -112
- package/src/commands/add.js +0 -177
- package/src/commands/build.js +0 -78
- package/src/commands/create-scene.js +0 -181
- package/src/commands/get.js +0 -108
- package/src/commands/prefab-create.js +0 -111
- package/src/commands/remove-component.js +0 -111
- package/src/commands/remove.js +0 -99
- package/src/commands/screenshot.js +0 -108
- package/src/commands/set-component.js +0 -119
- package/src/commands/set.js +0 -107
- package/src/commands/tree.js +0 -29
- package/src/lib/cc/CCSceneAsset.js +0 -303
- package/src/lib/cc/index.js +0 -42
- package/src/lib/fire-utils.js +0 -374
- package/src/lib/json-parser.js +0 -185
- package/src/lib/node-utils.js +0 -395
- package/src/lib/screenshot/favicon.ico +0 -0
- package/src/lib/screenshot/index.html +0 -30
- package/src/lib/templates.js +0 -49
- package/src/lib/utils.js +0 -202
|
@@ -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;
|
package/src/lib/cc/index.js
DELETED
|
@@ -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
|
-
};
|
package/src/lib/fire-utils.js
DELETED
|
@@ -1,374 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Fire/Prefab 文件工具模块
|
|
3
|
-
* 提供场景/预制体文件的读写和编辑器交互功能
|
|
4
|
-
*/
|
|
5
|
-
|
|
6
|
-
const fs = require('fs');
|
|
7
|
-
const path = require('path');
|
|
8
|
-
const crypto = require('crypto');
|
|
9
|
-
|
|
10
|
-
/**
|
|
11
|
-
* 检测是否为预制体文件
|
|
12
|
-
*/
|
|
13
|
-
function isPrefab(data) {
|
|
14
|
-
return data[0]?.__type__ === 'cc.Prefab';
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
/**
|
|
18
|
-
* 加载场景/预制体文件
|
|
19
|
-
*/
|
|
20
|
-
function loadScene(scenePath) {
|
|
21
|
-
if (!fs.existsSync(scenePath)) {
|
|
22
|
-
throw new Error(`文件不存在: ${scenePath}`);
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
const content = fs.readFileSync(scenePath, 'utf8');
|
|
26
|
-
return JSON.parse(content);
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
/**
|
|
30
|
-
* 保存场景/预制体文件
|
|
31
|
-
*/
|
|
32
|
-
function saveScene(scenePath, data) {
|
|
33
|
-
fs.writeFileSync(scenePath, JSON.stringify(data, null, 2), 'utf8');
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
/**
|
|
37
|
-
* 构建 ID 和索引映射
|
|
38
|
-
*/
|
|
39
|
-
function buildMaps(data) {
|
|
40
|
-
const idMap = {};
|
|
41
|
-
const indexMap = {};
|
|
42
|
-
const prefab = isPrefab(data);
|
|
43
|
-
|
|
44
|
-
function traverse(nodeIndex, parentPath = '') {
|
|
45
|
-
const node = data[nodeIndex];
|
|
46
|
-
if (!node) return;
|
|
47
|
-
|
|
48
|
-
if (!node.__type__?.startsWith('cc.Node') && node.__type__ !== 'cc.Scene') {
|
|
49
|
-
return;
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
const nodeId = node._id;
|
|
53
|
-
if (nodeId) {
|
|
54
|
-
idMap[nodeId] = nodeIndex;
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
const nodeName = node._name || '(unnamed)';
|
|
58
|
-
const nodePath = parentPath ? `${parentPath}/${nodeName}` : nodeName;
|
|
59
|
-
|
|
60
|
-
indexMap[nodeIndex] = {
|
|
61
|
-
_id: nodeId,
|
|
62
|
-
name: nodeName,
|
|
63
|
-
path: nodePath,
|
|
64
|
-
type: node.__type__
|
|
65
|
-
};
|
|
66
|
-
|
|
67
|
-
if (node._children) {
|
|
68
|
-
node._children.forEach(childRef => {
|
|
69
|
-
traverse(childRef.__id__, nodePath);
|
|
70
|
-
});
|
|
71
|
-
}
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
traverse(1);
|
|
75
|
-
|
|
76
|
-
return { idMap, indexMap, prefab };
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
/**
|
|
80
|
-
* 查找节点索引
|
|
81
|
-
*/
|
|
82
|
-
function findNodeIndex(data, indexMap, nodeRef) {
|
|
83
|
-
if (/^\d+$/.test(nodeRef)) {
|
|
84
|
-
return parseInt(nodeRef);
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
for (const [idx, info] of Object.entries(indexMap)) {
|
|
88
|
-
if (info.name === nodeRef || info.path === nodeRef || info.path.endsWith('/' + nodeRef)) {
|
|
89
|
-
return parseInt(idx);
|
|
90
|
-
}
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
return null;
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
/**
|
|
97
|
-
* 重建所有 __id__ 引用
|
|
98
|
-
*/
|
|
99
|
-
function rebuildReferences(data, deletedIndices) {
|
|
100
|
-
const indexMap = {};
|
|
101
|
-
let newIndex = 0;
|
|
102
|
-
for (let oldIndex = 0; oldIndex < data.length; oldIndex++) {
|
|
103
|
-
if (!deletedIndices.has(oldIndex)) {
|
|
104
|
-
indexMap[oldIndex] = newIndex;
|
|
105
|
-
newIndex++;
|
|
106
|
-
}
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
function updateRef(obj) {
|
|
110
|
-
if (!obj || typeof obj !== 'object') return;
|
|
111
|
-
|
|
112
|
-
if (obj.__id__ !== undefined) {
|
|
113
|
-
const oldId = obj.__id__;
|
|
114
|
-
if (indexMap[oldId] !== undefined) {
|
|
115
|
-
obj.__id__ = indexMap[oldId];
|
|
116
|
-
}
|
|
117
|
-
} else {
|
|
118
|
-
for (const key of Object.keys(obj)) {
|
|
119
|
-
updateRef(obj[key]);
|
|
120
|
-
}
|
|
121
|
-
}
|
|
122
|
-
}
|
|
123
|
-
|
|
124
|
-
for (const item of data) {
|
|
125
|
-
updateRef(item);
|
|
126
|
-
}
|
|
127
|
-
|
|
128
|
-
return indexMap;
|
|
129
|
-
}
|
|
130
|
-
|
|
131
|
-
/**
|
|
132
|
-
* 检查 CLI Helper 插件状态
|
|
133
|
-
*/
|
|
134
|
-
function checkPluginStatus() {
|
|
135
|
-
const http = require('http');
|
|
136
|
-
|
|
137
|
-
return new Promise((resolve) => {
|
|
138
|
-
const req = http.request({
|
|
139
|
-
hostname: 'localhost',
|
|
140
|
-
port: 7455,
|
|
141
|
-
path: '/status',
|
|
142
|
-
method: 'GET'
|
|
143
|
-
}, (res) => {
|
|
144
|
-
let data = '';
|
|
145
|
-
res.on('data', chunk => data += chunk);
|
|
146
|
-
res.on('end', () => {
|
|
147
|
-
try {
|
|
148
|
-
resolve(JSON.parse(data));
|
|
149
|
-
} catch (e) {
|
|
150
|
-
resolve(null);
|
|
151
|
-
}
|
|
152
|
-
});
|
|
153
|
-
});
|
|
154
|
-
|
|
155
|
-
req.on('error', () => resolve(null));
|
|
156
|
-
req.setTimeout(3000, () => { req.destroy(); resolve(null); });
|
|
157
|
-
req.end();
|
|
158
|
-
});
|
|
159
|
-
}
|
|
160
|
-
|
|
161
|
-
/**
|
|
162
|
-
* 触发编辑器刷新
|
|
163
|
-
*/
|
|
164
|
-
function refreshEditor(scenePath) {
|
|
165
|
-
if (!scenePath) return;
|
|
166
|
-
|
|
167
|
-
const http = require('http');
|
|
168
|
-
|
|
169
|
-
const assetsPath = path.dirname(scenePath);
|
|
170
|
-
const projectPath = path.dirname(assetsPath);
|
|
171
|
-
const relativePath = path.relative(projectPath, scenePath).replace(/\\/g, '/');
|
|
172
|
-
const targetSceneUrl = 'db://' + relativePath.replace(/^assets\//, 'assets/');
|
|
173
|
-
|
|
174
|
-
const getCurrentScene = () => {
|
|
175
|
-
return new Promise((resolve) => {
|
|
176
|
-
const req = http.request({
|
|
177
|
-
hostname: 'localhost',
|
|
178
|
-
port: 7455,
|
|
179
|
-
path: '/current-scene',
|
|
180
|
-
method: 'GET'
|
|
181
|
-
}, (res) => {
|
|
182
|
-
let data = '';
|
|
183
|
-
res.on('data', chunk => data += chunk);
|
|
184
|
-
res.on('end', () => {
|
|
185
|
-
try {
|
|
186
|
-
resolve(JSON.parse(data).sceneUrl || null);
|
|
187
|
-
} catch (e) {
|
|
188
|
-
resolve(null);
|
|
189
|
-
}
|
|
190
|
-
});
|
|
191
|
-
});
|
|
192
|
-
req.on('error', () => resolve(null));
|
|
193
|
-
req.setTimeout(3000, () => { req.destroy(); resolve(null); });
|
|
194
|
-
req.end();
|
|
195
|
-
});
|
|
196
|
-
};
|
|
197
|
-
|
|
198
|
-
const sendRefreshRequest = (sceneUrl) => {
|
|
199
|
-
const postData = sceneUrl ? JSON.stringify({ sceneUrl }) : '';
|
|
200
|
-
const req = http.request({
|
|
201
|
-
hostname: 'localhost',
|
|
202
|
-
port: 7455,
|
|
203
|
-
path: '/refresh',
|
|
204
|
-
method: 'POST',
|
|
205
|
-
headers: {
|
|
206
|
-
'Content-Type': 'application/json',
|
|
207
|
-
'Content-Length': Buffer.byteLength(postData)
|
|
208
|
-
}
|
|
209
|
-
}, () => {});
|
|
210
|
-
req.on('error', () => {});
|
|
211
|
-
if (postData) req.write(postData);
|
|
212
|
-
req.end();
|
|
213
|
-
};
|
|
214
|
-
|
|
215
|
-
getCurrentScene().then(currentSceneUrl => {
|
|
216
|
-
sendRefreshRequest(currentSceneUrl === targetSceneUrl ? targetSceneUrl : null);
|
|
217
|
-
});
|
|
218
|
-
}
|
|
219
|
-
|
|
220
|
-
/**
|
|
221
|
-
* 安装 CLI Helper 插件
|
|
222
|
-
*/
|
|
223
|
-
function installPlugin(scenePath) {
|
|
224
|
-
try {
|
|
225
|
-
const assetsPath = path.dirname(scenePath);
|
|
226
|
-
const projectPath = path.dirname(assetsPath);
|
|
227
|
-
const packagesPath = path.join(projectPath, 'packages');
|
|
228
|
-
const pluginPath = path.join(packagesPath, 'cocos2d-cli-helper');
|
|
229
|
-
|
|
230
|
-
if (fs.existsSync(pluginPath)) return true;
|
|
231
|
-
|
|
232
|
-
if (!fs.existsSync(packagesPath)) {
|
|
233
|
-
fs.mkdirSync(packagesPath, { recursive: true });
|
|
234
|
-
}
|
|
235
|
-
|
|
236
|
-
const cliPluginPath = path.join(__dirname, '..', '..', 'editor-plugin');
|
|
237
|
-
|
|
238
|
-
if (!fs.existsSync(cliPluginPath)) return false;
|
|
239
|
-
|
|
240
|
-
fs.cpSync(cliPluginPath, pluginPath, { recursive: true });
|
|
241
|
-
return true;
|
|
242
|
-
} catch (e) {
|
|
243
|
-
return false;
|
|
244
|
-
}
|
|
245
|
-
}
|
|
246
|
-
|
|
247
|
-
/**
|
|
248
|
-
* 加载脚本映射
|
|
249
|
-
*/
|
|
250
|
-
function loadScriptMap(scenePath) {
|
|
251
|
-
const projectPath = path.dirname(path.dirname(scenePath));
|
|
252
|
-
const mapPath = path.join(projectPath, 'data', 'script_map.json');
|
|
253
|
-
try {
|
|
254
|
-
if (fs.existsSync(mapPath)) {
|
|
255
|
-
return JSON.parse(fs.readFileSync(mapPath, 'utf-8'));
|
|
256
|
-
}
|
|
257
|
-
} catch (e) {}
|
|
258
|
-
return {};
|
|
259
|
-
}
|
|
260
|
-
|
|
261
|
-
/**
|
|
262
|
-
* 生成 fileId(用于 PrefabInfo)
|
|
263
|
-
*/
|
|
264
|
-
function generateFileId() {
|
|
265
|
-
const chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
|
|
266
|
-
let result = '';
|
|
267
|
-
for (let i = 0; i < 22; i++) {
|
|
268
|
-
result += chars.charAt(Math.floor(Math.random() * chars.length));
|
|
269
|
-
}
|
|
270
|
-
return result;
|
|
271
|
-
}
|
|
272
|
-
|
|
273
|
-
/**
|
|
274
|
-
* 获取预制体根节点索引
|
|
275
|
-
*/
|
|
276
|
-
function getPrefabRootIndex(data) {
|
|
277
|
-
if (!isPrefab(data)) return null;
|
|
278
|
-
return 1;
|
|
279
|
-
}
|
|
280
|
-
|
|
281
|
-
/**
|
|
282
|
-
* 生成 UUID
|
|
283
|
-
*/
|
|
284
|
-
function generateUUID() {
|
|
285
|
-
return crypto.randomUUID();
|
|
286
|
-
}
|
|
287
|
-
|
|
288
|
-
/**
|
|
289
|
-
* 生成预制体的 meta 对象
|
|
290
|
-
* @param {string} uuid - 可选的 UUID,不传则自动生成
|
|
291
|
-
* @returns {object}
|
|
292
|
-
*/
|
|
293
|
-
function createPrefabMeta(uuid) {
|
|
294
|
-
return {
|
|
295
|
-
ver: '1.3.2',
|
|
296
|
-
uuid: uuid || generateUUID(),
|
|
297
|
-
importer: 'prefab',
|
|
298
|
-
optimizationPolicy: 'AUTO',
|
|
299
|
-
asyncLoadAssets: false,
|
|
300
|
-
readonly: false,
|
|
301
|
-
subMetas: {}
|
|
302
|
-
};
|
|
303
|
-
}
|
|
304
|
-
|
|
305
|
-
/**
|
|
306
|
-
* 生成场景的 meta 对象
|
|
307
|
-
* @param {string} uuid - 可选的 UUID,不传则自动生成
|
|
308
|
-
* @returns {object}
|
|
309
|
-
*/
|
|
310
|
-
function createSceneMeta(uuid) {
|
|
311
|
-
return {
|
|
312
|
-
ver: '1.3.2',
|
|
313
|
-
uuid: uuid || generateUUID(),
|
|
314
|
-
importer: 'scene',
|
|
315
|
-
asyncLoadAssets: false,
|
|
316
|
-
autoReleaseAssets: false,
|
|
317
|
-
subMetas: {}
|
|
318
|
-
};
|
|
319
|
-
}
|
|
320
|
-
|
|
321
|
-
/**
|
|
322
|
-
* 保存 meta 文件
|
|
323
|
-
* @param {string} filePath - 资源文件路径(.prefab 或 .fire)
|
|
324
|
-
* @param {object} meta - meta 对象
|
|
325
|
-
*/
|
|
326
|
-
function saveMetaFile(filePath, meta) {
|
|
327
|
-
const metaPath = filePath + '.meta';
|
|
328
|
-
fs.writeFileSync(metaPath, JSON.stringify(meta, null, 2), 'utf8');
|
|
329
|
-
return metaPath;
|
|
330
|
-
}
|
|
331
|
-
|
|
332
|
-
/**
|
|
333
|
-
* 加载 meta 文件
|
|
334
|
-
* @param {string} filePath - 资源文件路径
|
|
335
|
-
* @returns {object|null}
|
|
336
|
-
*/
|
|
337
|
-
function loadMetaFile(filePath) {
|
|
338
|
-
const metaPath = filePath + '.meta';
|
|
339
|
-
try {
|
|
340
|
-
if (fs.existsSync(metaPath)) {
|
|
341
|
-
return JSON.parse(fs.readFileSync(metaPath, 'utf8'));
|
|
342
|
-
}
|
|
343
|
-
} catch (e) {}
|
|
344
|
-
return null;
|
|
345
|
-
}
|
|
346
|
-
|
|
347
|
-
module.exports = {
|
|
348
|
-
// 文件操作
|
|
349
|
-
loadScene,
|
|
350
|
-
saveScene,
|
|
351
|
-
isPrefab,
|
|
352
|
-
|
|
353
|
-
// 索引映射
|
|
354
|
-
buildMaps,
|
|
355
|
-
findNodeIndex,
|
|
356
|
-
rebuildReferences,
|
|
357
|
-
|
|
358
|
-
// 编辑器交互
|
|
359
|
-
refreshEditor,
|
|
360
|
-
installPlugin,
|
|
361
|
-
checkPluginStatus,
|
|
362
|
-
|
|
363
|
-
// Meta 文件管理
|
|
364
|
-
generateUUID,
|
|
365
|
-
createPrefabMeta,
|
|
366
|
-
createSceneMeta,
|
|
367
|
-
saveMetaFile,
|
|
368
|
-
loadMetaFile,
|
|
369
|
-
|
|
370
|
-
// 工具函数
|
|
371
|
-
loadScriptMap,
|
|
372
|
-
generateFileId,
|
|
373
|
-
getPrefabRootIndex
|
|
374
|
-
};
|