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,112 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* add-component 命令 - 添加组件
|
|
3
|
-
*/
|
|
4
|
-
|
|
5
|
-
const path = require('path');
|
|
6
|
-
const fs = require('fs');
|
|
7
|
-
const { CCSceneAsset, CCPrefab, CCCanvas, CCWidget, CCSprite, CCLabel, CCButton, CCCamera } = require('../lib/cc');
|
|
8
|
-
const { buildTree } = require('../lib/node-utils');
|
|
9
|
-
const { loadScriptMap, isPrefab } = require('../lib/fire-utils');
|
|
10
|
-
|
|
11
|
-
/**
|
|
12
|
-
* 创建组件
|
|
13
|
-
*/
|
|
14
|
-
function createComponent(type) {
|
|
15
|
-
switch (type.toLowerCase()) {
|
|
16
|
-
case 'canvas': return new CCCanvas();
|
|
17
|
-
case 'widget': return new CCWidget();
|
|
18
|
-
case 'sprite': return new CCSprite();
|
|
19
|
-
case 'label': return new CCLabel();
|
|
20
|
-
case 'button': return new CCButton();
|
|
21
|
-
case 'camera': return new CCCamera();
|
|
22
|
-
default: return null;
|
|
23
|
-
}
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
/**
|
|
27
|
-
* 查找节点
|
|
28
|
-
*/
|
|
29
|
-
function findNode(root, path) {
|
|
30
|
-
if (!path) return root;
|
|
31
|
-
|
|
32
|
-
const parts = path.split('/').filter(p => p);
|
|
33
|
-
if (parts.length === 0) return root;
|
|
34
|
-
|
|
35
|
-
let current = root;
|
|
36
|
-
|
|
37
|
-
if (parts[0] === root._name) {
|
|
38
|
-
parts.shift();
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
for (const part of parts) {
|
|
42
|
-
if (!current._children || current._children.length === 0) return null;
|
|
43
|
-
const found = current._children.find(c => c._name === part);
|
|
44
|
-
if (!found) return null;
|
|
45
|
-
current = found;
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
return current;
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
function run(args) {
|
|
52
|
-
if (args.length < 3) {
|
|
53
|
-
console.log(JSON.stringify({ error: '用法: cocos2d-cli add-component <场景.fire|预制体.prefab> <节点路径> <组件类型>' }));
|
|
54
|
-
return;
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
const filePath = args[0];
|
|
58
|
-
const nodePath = args[1];
|
|
59
|
-
const compType = args[2];
|
|
60
|
-
|
|
61
|
-
const ext = path.extname(filePath).toLowerCase();
|
|
62
|
-
|
|
63
|
-
try {
|
|
64
|
-
let root;
|
|
65
|
-
let asset;
|
|
66
|
-
const json = JSON.parse(fs.readFileSync(filePath, 'utf8'));
|
|
67
|
-
|
|
68
|
-
if (ext === '.fire') {
|
|
69
|
-
asset = CCSceneAsset.fromJSON(json);
|
|
70
|
-
root = asset._scene;
|
|
71
|
-
} else if (ext === '.prefab') {
|
|
72
|
-
asset = CCPrefab.fromJSON(json);
|
|
73
|
-
root = asset._root;
|
|
74
|
-
} else {
|
|
75
|
-
console.log(JSON.stringify({ error: '不支持的文件类型,仅支持 .fire 和 .prefab' }));
|
|
76
|
-
return;
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
const node = findNode(root, nodePath);
|
|
80
|
-
|
|
81
|
-
if (!node) {
|
|
82
|
-
console.log(JSON.stringify({ error: `节点不存在: ${nodePath}` }));
|
|
83
|
-
return;
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
// 创建组件
|
|
87
|
-
const comp = createComponent(compType);
|
|
88
|
-
if (!comp) {
|
|
89
|
-
console.log(JSON.stringify({ error: `不支持的组件类型: ${compType}` }));
|
|
90
|
-
return;
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
comp.node = node;
|
|
94
|
-
node._components = node._components || [];
|
|
95
|
-
node._components.push(comp);
|
|
96
|
-
|
|
97
|
-
// 保存
|
|
98
|
-
const data = asset.toJSON();
|
|
99
|
-
fs.writeFileSync(filePath, JSON.stringify(data, null, 2), 'utf8');
|
|
100
|
-
|
|
101
|
-
// 输出节点树
|
|
102
|
-
const scriptMap = loadScriptMap(filePath);
|
|
103
|
-
const prefab = isPrefab(data);
|
|
104
|
-
const startIndex = prefab ? 0 : 1;
|
|
105
|
-
console.log(buildTree(data, scriptMap, startIndex).trim());
|
|
106
|
-
|
|
107
|
-
} catch (err) {
|
|
108
|
-
console.log(JSON.stringify({ error: err.message }));
|
|
109
|
-
}
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
module.exports = { run };
|
package/src/commands/add.js
DELETED
|
@@ -1,177 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* add 命令 - 添加节点
|
|
3
|
-
*/
|
|
4
|
-
|
|
5
|
-
const path = require('path');
|
|
6
|
-
const fs = require('fs');
|
|
7
|
-
const { CCNode, CCSceneAsset, CCPrefab, CCCanvas, CCWidget, CCSprite, CCLabel, CCButton } = require('../lib/cc');
|
|
8
|
-
const { buildTree } = require('../lib/node-utils');
|
|
9
|
-
const { loadScriptMap, isPrefab } = require('../lib/fire-utils');
|
|
10
|
-
const { generateCompressedUUID } = require('../lib/utils');
|
|
11
|
-
|
|
12
|
-
/**
|
|
13
|
-
* 解析命令行选项
|
|
14
|
-
*/
|
|
15
|
-
function parseOptions(args, startIndex) {
|
|
16
|
-
const options = {};
|
|
17
|
-
for (let i = startIndex; i < args.length; i++) {
|
|
18
|
-
const arg = args[i];
|
|
19
|
-
if (arg.startsWith('--')) {
|
|
20
|
-
const eqIndex = arg.indexOf('=');
|
|
21
|
-
if (eqIndex > 0) {
|
|
22
|
-
const key = arg.substring(2, eqIndex);
|
|
23
|
-
let value = arg.substring(eqIndex + 1);
|
|
24
|
-
if (!isNaN(value) && value !== '') {
|
|
25
|
-
value = parseFloat(value);
|
|
26
|
-
}
|
|
27
|
-
options[key] = value;
|
|
28
|
-
}
|
|
29
|
-
}
|
|
30
|
-
}
|
|
31
|
-
return options;
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
/**
|
|
35
|
-
* 创建组件
|
|
36
|
-
*/
|
|
37
|
-
function createComponent(type) {
|
|
38
|
-
switch (type.toLowerCase()) {
|
|
39
|
-
case 'canvas': return new CCCanvas();
|
|
40
|
-
case 'widget': return new CCWidget();
|
|
41
|
-
case 'sprite': return new CCSprite();
|
|
42
|
-
case 'label': return new CCLabel();
|
|
43
|
-
case 'button': return new CCButton();
|
|
44
|
-
default: return null;
|
|
45
|
-
}
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
/**
|
|
49
|
-
* 查找节点
|
|
50
|
-
*/
|
|
51
|
-
function findNode(root, path) {
|
|
52
|
-
if (!path) return root;
|
|
53
|
-
|
|
54
|
-
const parts = path.split('/').filter(p => p);
|
|
55
|
-
if (parts.length === 0) return root;
|
|
56
|
-
|
|
57
|
-
let current = root;
|
|
58
|
-
|
|
59
|
-
// 如果路径以根节点名称开始,跳过
|
|
60
|
-
if (parts[0] === root._name) {
|
|
61
|
-
parts.shift();
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
for (const part of parts) {
|
|
65
|
-
if (!current._children || current._children.length === 0) return null;
|
|
66
|
-
const found = current._children.find(c => c._name === part);
|
|
67
|
-
if (!found) return null;
|
|
68
|
-
current = found;
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
return current;
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
function run(args) {
|
|
75
|
-
if (args.length < 3) {
|
|
76
|
-
console.log(JSON.stringify({ error: '用法: cocos2d-cli add <场景.fire|预制体.prefab> <父节点路径> <节点名称> [--type=组件类型] [--x=N] [--y=N]' }));
|
|
77
|
-
return;
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
const filePath = args[0];
|
|
81
|
-
const parentPath = args[1];
|
|
82
|
-
const nodeName = args[2];
|
|
83
|
-
const options = parseOptions(args, 3);
|
|
84
|
-
|
|
85
|
-
const ext = path.extname(filePath).toLowerCase();
|
|
86
|
-
|
|
87
|
-
try {
|
|
88
|
-
let root;
|
|
89
|
-
let asset;
|
|
90
|
-
const json = JSON.parse(fs.readFileSync(filePath, 'utf8'));
|
|
91
|
-
|
|
92
|
-
if (ext === '.fire') {
|
|
93
|
-
asset = CCSceneAsset.fromJSON(json);
|
|
94
|
-
root = asset._scene;
|
|
95
|
-
} else if (ext === '.prefab') {
|
|
96
|
-
asset = CCPrefab.fromJSON(json);
|
|
97
|
-
root = asset._root;
|
|
98
|
-
} else {
|
|
99
|
-
console.log(JSON.stringify({ error: '不支持的文件类型,仅支持 .fire 和 .prefab' }));
|
|
100
|
-
return;
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
const parent = findNode(root, parentPath);
|
|
104
|
-
|
|
105
|
-
if (!parent) {
|
|
106
|
-
console.log(JSON.stringify({ error: `父节点不存在: ${parentPath}` }));
|
|
107
|
-
return;
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
// 创建节点
|
|
111
|
-
const node = new CCNode(nodeName);
|
|
112
|
-
|
|
113
|
-
// 应用属性
|
|
114
|
-
if (options.x !== undefined) node._trs.array[0] = parseFloat(options.x);
|
|
115
|
-
if (options.y !== undefined) node._trs.array[1] = parseFloat(options.y);
|
|
116
|
-
if (options.width !== undefined) node._contentSize.width = parseFloat(options.width);
|
|
117
|
-
if (options.height !== undefined) node._contentSize.height = parseFloat(options.height);
|
|
118
|
-
if (options.scaleX !== undefined) node._trs.array[7] = parseFloat(options.scaleX);
|
|
119
|
-
if (options.scaleY !== undefined) node._trs.array[8] = parseFloat(options.scaleY);
|
|
120
|
-
if (options.rotation !== undefined) {
|
|
121
|
-
node._trs.array[5] = parseFloat(options.rotation) * Math.PI / 180;
|
|
122
|
-
node._eulerAngles.z = parseFloat(options.rotation);
|
|
123
|
-
}
|
|
124
|
-
if (options.active !== undefined) {
|
|
125
|
-
node._active = options.active !== 'false' && options.active !== false;
|
|
126
|
-
}
|
|
127
|
-
|
|
128
|
-
// 添加组件
|
|
129
|
-
if (options.type) {
|
|
130
|
-
const comp = createComponent(options.type);
|
|
131
|
-
if (comp) {
|
|
132
|
-
comp.node = node;
|
|
133
|
-
node._components = [comp];
|
|
134
|
-
|
|
135
|
-
// 组件特殊属性
|
|
136
|
-
if (options.type.toLowerCase() === 'label') {
|
|
137
|
-
if (options.string) {
|
|
138
|
-
comp._string = options.string;
|
|
139
|
-
comp['_N$string'] = options.string;
|
|
140
|
-
}
|
|
141
|
-
if (options.fontSize) {
|
|
142
|
-
comp._fontSize = parseInt(options.fontSize);
|
|
143
|
-
comp._lineHeight = parseInt(options.fontSize);
|
|
144
|
-
}
|
|
145
|
-
}
|
|
146
|
-
}
|
|
147
|
-
}
|
|
148
|
-
|
|
149
|
-
// 添加节点到父节点
|
|
150
|
-
parent._children = parent._children || [];
|
|
151
|
-
parent._children.push(node);
|
|
152
|
-
node._parent = parent;
|
|
153
|
-
|
|
154
|
-
// 场景节点需要 _id,预制体需要 PrefabInfo
|
|
155
|
-
if (ext === '.fire') {
|
|
156
|
-
node._id = generateCompressedUUID();
|
|
157
|
-
} else if (ext === '.prefab') {
|
|
158
|
-
const { CCPrefabInfo } = require('../lib/cc');
|
|
159
|
-
node._prefab = new CCPrefabInfo();
|
|
160
|
-
}
|
|
161
|
-
|
|
162
|
-
// 保存
|
|
163
|
-
const data = asset.toJSON();
|
|
164
|
-
fs.writeFileSync(filePath, JSON.stringify(data, null, 2), 'utf8');
|
|
165
|
-
|
|
166
|
-
// 输出节点树
|
|
167
|
-
const scriptMap = loadScriptMap(filePath);
|
|
168
|
-
const prefab = isPrefab(data);
|
|
169
|
-
const startIndex = prefab ? 0 : 1;
|
|
170
|
-
console.log(buildTree(data, scriptMap, startIndex).trim());
|
|
171
|
-
|
|
172
|
-
} catch (err) {
|
|
173
|
-
console.log(JSON.stringify({ error: err.message }));
|
|
174
|
-
}
|
|
175
|
-
}
|
|
176
|
-
|
|
177
|
-
module.exports = { run };
|
package/src/commands/build.js
DELETED
|
@@ -1,78 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* build 命令 - 构建组件映射
|
|
3
|
-
*/
|
|
4
|
-
|
|
5
|
-
const fs = require('fs');
|
|
6
|
-
const path = require('path');
|
|
7
|
-
|
|
8
|
-
function run(args) {
|
|
9
|
-
if (args.length < 1) {
|
|
10
|
-
console.log(JSON.stringify({ error: '用法: cocos2d-cli build <项目目录>' }));
|
|
11
|
-
return;
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
const projectDir = args[0];
|
|
15
|
-
const importsDir = path.join(projectDir, 'library', 'imports');
|
|
16
|
-
const outputFile = path.join(__dirname, '../../data/script_map.json');
|
|
17
|
-
|
|
18
|
-
if (!fs.existsSync(importsDir)) {
|
|
19
|
-
console.log(JSON.stringify({ error: `imports 目录不存在: ${importsDir}` }));
|
|
20
|
-
return;
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
const scriptMap = {};
|
|
24
|
-
let processedCount = 0;
|
|
25
|
-
|
|
26
|
-
function scanDirectory(dir) {
|
|
27
|
-
const items = fs.readdirSync(dir);
|
|
28
|
-
|
|
29
|
-
for (const item of items) {
|
|
30
|
-
const fullPath = path.join(dir, item);
|
|
31
|
-
const stat = fs.statSync(fullPath);
|
|
32
|
-
|
|
33
|
-
if (stat.isDirectory()) {
|
|
34
|
-
scanDirectory(fullPath);
|
|
35
|
-
} else if (item.endsWith('.js')) {
|
|
36
|
-
try {
|
|
37
|
-
const content = fs.readFileSync(fullPath, 'utf8');
|
|
38
|
-
|
|
39
|
-
// 查找 cc._RF.push 调用
|
|
40
|
-
const match = content.match(/cc\._RF\.push\(module,\s*['"]([^'"]+)['"],\s*['"]([^'"]+)['"]\)/);
|
|
41
|
-
|
|
42
|
-
if (match) {
|
|
43
|
-
const hash = match[1];
|
|
44
|
-
const className = match[2];
|
|
45
|
-
|
|
46
|
-
// 只存储脚本相关的哈希(不以 'cc.' 开头的)
|
|
47
|
-
if (!className.startsWith('cc.')) {
|
|
48
|
-
scriptMap[hash] = className;
|
|
49
|
-
processedCount++;
|
|
50
|
-
}
|
|
51
|
-
}
|
|
52
|
-
} catch (err) {
|
|
53
|
-
// 忽略读取错误
|
|
54
|
-
}
|
|
55
|
-
}
|
|
56
|
-
}
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
scanDirectory(importsDir);
|
|
60
|
-
|
|
61
|
-
// 确保输出目录存在
|
|
62
|
-
const outputDir = path.dirname(outputFile);
|
|
63
|
-
if (!fs.existsSync(outputDir)) {
|
|
64
|
-
fs.mkdirSync(outputDir, { recursive: true });
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
// 写入输出文件
|
|
68
|
-
fs.writeFileSync(outputFile, JSON.stringify(scriptMap, null, 2), 'utf8');
|
|
69
|
-
|
|
70
|
-
console.log(JSON.stringify({
|
|
71
|
-
success: true,
|
|
72
|
-
count: Object.keys(scriptMap).length,
|
|
73
|
-
outputFile,
|
|
74
|
-
message: `构建完成,共 ${Object.keys(scriptMap).length} 个脚本映射`
|
|
75
|
-
}, null, 2));
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
module.exports = { run };
|
|
@@ -1,181 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* create-scene 命令 - 创建场景文件
|
|
3
|
-
*/
|
|
4
|
-
|
|
5
|
-
const fs = require('fs');
|
|
6
|
-
const path = require('path');
|
|
7
|
-
const { CCNode, CCScene, CCSceneAsset, CCCanvas, CCWidget, CCCamera } = require('../lib/cc');
|
|
8
|
-
const { buildTree } = require('../lib/node-utils');
|
|
9
|
-
const { loadScriptMap, createSceneMeta, saveMetaFile } = require('../lib/fire-utils');
|
|
10
|
-
const { generateUUID, generateCompressedUUID } = require('../lib/utils');
|
|
11
|
-
const { fromJSON } = require('../lib/json-parser');
|
|
12
|
-
|
|
13
|
-
/**
|
|
14
|
-
* 从项目配置读取设计分辨率
|
|
15
|
-
*/
|
|
16
|
-
function getDesignResolution(outputPath) {
|
|
17
|
-
const defaultResolution = { width: 960, height: 640 };
|
|
18
|
-
|
|
19
|
-
let currentDir = path.dirname(path.resolve(outputPath));
|
|
20
|
-
while (currentDir !== path.dirname(currentDir)) {
|
|
21
|
-
const settingsPath = path.join(currentDir, 'settings', 'project.json');
|
|
22
|
-
if (fs.existsSync(settingsPath)) {
|
|
23
|
-
try {
|
|
24
|
-
const config = JSON.parse(fs.readFileSync(settingsPath, 'utf8'));
|
|
25
|
-
if (config['design-resolution-width'] && config['design-resolution-height']) {
|
|
26
|
-
return {
|
|
27
|
-
width: config['design-resolution-width'],
|
|
28
|
-
height: config['design-resolution-height']
|
|
29
|
-
};
|
|
30
|
-
}
|
|
31
|
-
} catch (e) {}
|
|
32
|
-
}
|
|
33
|
-
currentDir = path.dirname(currentDir);
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
return defaultResolution;
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
/**
|
|
40
|
-
* 从 meta 文件读取 uuid
|
|
41
|
-
*/
|
|
42
|
-
function readUuidFromMeta(scenePath) {
|
|
43
|
-
const metaPath = scenePath + '.meta';
|
|
44
|
-
if (fs.existsSync(metaPath)) {
|
|
45
|
-
try {
|
|
46
|
-
const meta = JSON.parse(fs.readFileSync(metaPath, 'utf8'));
|
|
47
|
-
return meta.uuid || null;
|
|
48
|
-
} catch (e) {
|
|
49
|
-
return null;
|
|
50
|
-
}
|
|
51
|
-
}
|
|
52
|
-
return null;
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
/**
|
|
56
|
-
* 为节点树设置 _id
|
|
57
|
-
*/
|
|
58
|
-
function setNodeId(node) {
|
|
59
|
-
node._id = generateCompressedUUID();
|
|
60
|
-
if (node._children) {
|
|
61
|
-
node._children.forEach(child => setNodeId(child));
|
|
62
|
-
}
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
/**
|
|
66
|
-
* 创建默认场景结构(Canvas + Main Camera)
|
|
67
|
-
*/
|
|
68
|
-
function createDefaultScene(asset, resolution) {
|
|
69
|
-
const { width, height } = resolution;
|
|
70
|
-
const scene = asset._scene;
|
|
71
|
-
|
|
72
|
-
// 创建 Canvas 节点
|
|
73
|
-
const canvas = new CCNode('Canvas');
|
|
74
|
-
canvas._contentSize.width = width;
|
|
75
|
-
canvas._contentSize.height = height;
|
|
76
|
-
canvas._anchorPoint.x = 0.5;
|
|
77
|
-
canvas._anchorPoint.y = 0.5;
|
|
78
|
-
canvas._trs.array[0] = width / 2;
|
|
79
|
-
canvas._trs.array[1] = height / 2;
|
|
80
|
-
canvas._is3DNode = false;
|
|
81
|
-
canvas._id = generateCompressedUUID();
|
|
82
|
-
|
|
83
|
-
// Canvas 组件
|
|
84
|
-
const canvasComp = new CCCanvas();
|
|
85
|
-
canvasComp._designResolution.set(width, height);
|
|
86
|
-
canvas.addChild(canvasComp);
|
|
87
|
-
|
|
88
|
-
// Widget 组件
|
|
89
|
-
const widget = new CCWidget();
|
|
90
|
-
widget.node = canvas;
|
|
91
|
-
|
|
92
|
-
canvas._components = [canvasComp, widget];
|
|
93
|
-
canvas._parent = scene;
|
|
94
|
-
scene._children = [canvas];
|
|
95
|
-
|
|
96
|
-
// 创建 Main Camera 节点
|
|
97
|
-
const camera = new CCNode('Main Camera');
|
|
98
|
-
camera._id = generateCompressedUUID();
|
|
99
|
-
|
|
100
|
-
// Camera 组件
|
|
101
|
-
const cameraComp = new CCCamera();
|
|
102
|
-
cameraComp.node = camera;
|
|
103
|
-
|
|
104
|
-
camera._components = [cameraComp];
|
|
105
|
-
camera._parent = canvas;
|
|
106
|
-
canvas._children = [camera];
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
function run(args) {
|
|
110
|
-
if (args.length < 1) {
|
|
111
|
-
console.log(JSON.stringify({ error: '用法: cocos2d-cli create-scene [JSON文件路径] <输出路径.fire>' }));
|
|
112
|
-
console.log(JSON.stringify({ hint: '不传 JSON 则创建默认场景(含 Canvas 和 Main Camera)' }));
|
|
113
|
-
return;
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
let jsonPath = null;
|
|
117
|
-
let outputPath;
|
|
118
|
-
|
|
119
|
-
if (args.length === 1) {
|
|
120
|
-
outputPath = args[0];
|
|
121
|
-
} else {
|
|
122
|
-
jsonPath = args[0];
|
|
123
|
-
outputPath = args[1];
|
|
124
|
-
}
|
|
125
|
-
|
|
126
|
-
const sceneName = path.basename(outputPath, '.fire');
|
|
127
|
-
|
|
128
|
-
try {
|
|
129
|
-
// 创建场景
|
|
130
|
-
const asset = new CCSceneAsset();
|
|
131
|
-
const scene = new CCScene(sceneName);
|
|
132
|
-
asset._scene = scene;
|
|
133
|
-
scene._children = [];
|
|
134
|
-
|
|
135
|
-
// 从 JSON 创建
|
|
136
|
-
if (jsonPath && fs.existsSync(jsonPath)) {
|
|
137
|
-
const input = fs.readFileSync(jsonPath, 'utf8');
|
|
138
|
-
const nodeDef = JSON.parse(input.replace(/^\uFEFF/, '').trim());
|
|
139
|
-
|
|
140
|
-
const nodes = Array.isArray(nodeDef) ? nodeDef : [nodeDef];
|
|
141
|
-
for (const def of nodes) {
|
|
142
|
-
const node = fromJSON(def);
|
|
143
|
-
setNodeId(node);
|
|
144
|
-
node._parent = scene;
|
|
145
|
-
scene._children.push(node);
|
|
146
|
-
}
|
|
147
|
-
} else {
|
|
148
|
-
// 没有 JSON,创建默认场景结构
|
|
149
|
-
const resolution = getDesignResolution(outputPath);
|
|
150
|
-
createDefaultScene(asset, resolution);
|
|
151
|
-
}
|
|
152
|
-
|
|
153
|
-
// 确保目录存在
|
|
154
|
-
const dir = path.dirname(outputPath);
|
|
155
|
-
if (!fs.existsSync(dir)) {
|
|
156
|
-
fs.mkdirSync(dir, { recursive: true });
|
|
157
|
-
}
|
|
158
|
-
|
|
159
|
-
// 设置 Scene._id
|
|
160
|
-
const uuid = readUuidFromMeta(outputPath);
|
|
161
|
-
scene._id = uuid || generateUUID();
|
|
162
|
-
|
|
163
|
-
// 保存
|
|
164
|
-
const data = asset.toJSON();
|
|
165
|
-
fs.writeFileSync(outputPath, JSON.stringify(data, null, 2), 'utf8');
|
|
166
|
-
|
|
167
|
-
// 生成并保存 meta 文件(如果不存在)
|
|
168
|
-
if (!uuid) {
|
|
169
|
-
saveMetaFile(outputPath, createSceneMeta(scene._id));
|
|
170
|
-
}
|
|
171
|
-
|
|
172
|
-
// 输出 tree
|
|
173
|
-
const scriptMap = loadScriptMap(outputPath);
|
|
174
|
-
console.log(buildTree(data, scriptMap, 1).trim());
|
|
175
|
-
|
|
176
|
-
} catch (err) {
|
|
177
|
-
console.log(JSON.stringify({ error: err.message }));
|
|
178
|
-
}
|
|
179
|
-
}
|
|
180
|
-
|
|
181
|
-
module.exports = { run };
|
package/src/commands/get.js
DELETED
|
@@ -1,108 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* get 命令 - 获取节点或组件属性
|
|
3
|
-
*/
|
|
4
|
-
|
|
5
|
-
const path = require('path');
|
|
6
|
-
const fs = require('fs');
|
|
7
|
-
const { CCSceneAsset, CCPrefab } = require('../lib/cc');
|
|
8
|
-
|
|
9
|
-
/**
|
|
10
|
-
* 查找节点
|
|
11
|
-
*/
|
|
12
|
-
function findNode(root, nodePath) {
|
|
13
|
-
if (!nodePath) return root;
|
|
14
|
-
|
|
15
|
-
const parts = nodePath.split('/').filter(p => p);
|
|
16
|
-
if (parts.length === 0) return root;
|
|
17
|
-
|
|
18
|
-
let current = root;
|
|
19
|
-
|
|
20
|
-
if (parts[0] === root._name) {
|
|
21
|
-
parts.shift();
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
for (const part of parts) {
|
|
25
|
-
if (!current._children || current._children.length === 0) return null;
|
|
26
|
-
const found = current._children.find(c => c._name === part);
|
|
27
|
-
if (!found) return null;
|
|
28
|
-
current = found;
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
return current;
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
/**
|
|
35
|
-
* 查找组件
|
|
36
|
-
*/
|
|
37
|
-
function findComponent(node, compType) {
|
|
38
|
-
if (!node._components) return null;
|
|
39
|
-
|
|
40
|
-
const type = compType.toLowerCase();
|
|
41
|
-
const typeName = 'cc.' + type.charAt(0).toUpperCase() + type.slice(1);
|
|
42
|
-
|
|
43
|
-
return node._components.find(c => c.__type__ === typeName);
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
function run(args) {
|
|
47
|
-
if (args.length < 2) {
|
|
48
|
-
console.log(JSON.stringify({ error: '用法: cocos2d-cli get <场景.fire|预制体.prefab> <节点路径> [属性名|组件类型]' }));
|
|
49
|
-
return;
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
const filePath = args[0];
|
|
53
|
-
const nodePath = args[1];
|
|
54
|
-
const propOrComp = args[2];
|
|
55
|
-
|
|
56
|
-
const ext = path.extname(filePath).toLowerCase();
|
|
57
|
-
|
|
58
|
-
try {
|
|
59
|
-
let root;
|
|
60
|
-
const json = JSON.parse(fs.readFileSync(filePath, 'utf8'));
|
|
61
|
-
|
|
62
|
-
if (ext === '.fire') {
|
|
63
|
-
const asset = CCSceneAsset.fromJSON(json);
|
|
64
|
-
root = asset._scene;
|
|
65
|
-
} else if (ext === '.prefab') {
|
|
66
|
-
const asset = CCPrefab.fromJSON(json);
|
|
67
|
-
root = asset._root;
|
|
68
|
-
} else {
|
|
69
|
-
console.log(JSON.stringify({ error: '不支持的文件类型,仅支持 .fire 和 .prefab' }));
|
|
70
|
-
return;
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
const node = findNode(root, nodePath);
|
|
74
|
-
|
|
75
|
-
if (!node) {
|
|
76
|
-
console.log(JSON.stringify({ error: `节点不存在: ${nodePath}` }));
|
|
77
|
-
return;
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
// 没有指定属性或组件,返回节点所有属性
|
|
81
|
-
if (!propOrComp) {
|
|
82
|
-
const props = node.getProp ? node.getProp() : {};
|
|
83
|
-
console.log(JSON.stringify(props, null, 2));
|
|
84
|
-
return;
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
// 检查是否是组件类型
|
|
88
|
-
const comp = findComponent(node, propOrComp);
|
|
89
|
-
if (comp) {
|
|
90
|
-
const props = comp.getProp ? comp.getProp() : {};
|
|
91
|
-
console.log(JSON.stringify(props, null, 2));
|
|
92
|
-
return;
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
// 返回节点的单个属性
|
|
96
|
-
const nodeProps = node.getProp ? node.getProp() : {};
|
|
97
|
-
if (nodeProps[propOrComp] !== undefined) {
|
|
98
|
-
console.log(JSON.stringify({ [propOrComp]: nodeProps[propOrComp] }, null, 2));
|
|
99
|
-
} else {
|
|
100
|
-
console.log(JSON.stringify({ error: `属性不存在: ${propOrComp}` }));
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
} catch (err) {
|
|
104
|
-
console.log(JSON.stringify({ error: err.message }));
|
|
105
|
-
}
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
module.exports = { run };
|