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.
- 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 +115 -122
- package/{src → dist/src}/lib/cc/CCCamera.js +83 -93
- package/{src → dist/src}/lib/cc/CCCanvas.js +49 -54
- package/{src → dist/src}/lib/cc/CCColor.js +30 -32
- package/{src → dist/src}/lib/cc/CCComponent.js +39 -60
- package/{src → dist/src}/lib/cc/CCLabel.js +139 -146
- package/{src → dist/src}/lib/cc/CCNode.js +190 -256
- package/{src → dist/src}/lib/cc/CCObject.js +19 -23
- package/{src → dist/src}/lib/cc/CCPrefab.js +219 -242
- package/{src → dist/src}/lib/cc/CCRect.js +30 -32
- package/{src → dist/src}/lib/cc/CCRichText.js +38 -44
- package/{src → dist/src}/lib/cc/CCScene.js +32 -42
- package/dist/src/lib/cc/CCSceneAsset.js +242 -0
- package/{src → dist/src}/lib/cc/CCSize.js +22 -26
- package/{src → dist/src}/lib/cc/CCSprite.js +82 -94
- package/{src → dist/src}/lib/cc/CCTrs.js +49 -74
- package/{src → dist/src}/lib/cc/CCVec2.js +22 -26
- package/{src → dist/src}/lib/cc/CCVec3.js +26 -29
- package/{src → dist/src}/lib/cc/CCWidget.js +94 -98
- 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 +221 -285
- package/dist/src/lib/templates.js +17 -0
- package/dist/src/lib/utils.js +81 -0
- package/package.json +13 -6
- 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
package/bin/cocos2d-cli.js
DELETED
|
@@ -1,152 +0,0 @@
|
|
|
1
|
-
#!/usr/bin/env node
|
|
2
|
-
/**
|
|
3
|
-
* Cocos Creator CLI
|
|
4
|
-
* Command-line tools for AI to read and manipulate Cocos Creator 2.4.x project scenes
|
|
5
|
-
*/
|
|
6
|
-
|
|
7
|
-
const path = require('path');
|
|
8
|
-
|
|
9
|
-
// 命令映射
|
|
10
|
-
const commands = {
|
|
11
|
-
tree: '../src/commands/tree',
|
|
12
|
-
get: '../src/commands/get',
|
|
13
|
-
set: '../src/commands/set',
|
|
14
|
-
'set-component': '../src/commands/set-component',
|
|
15
|
-
add: '../src/commands/add',
|
|
16
|
-
'add-component': '../src/commands/add-component',
|
|
17
|
-
'remove-component': '../src/commands/remove-component',
|
|
18
|
-
'remove': '../src/commands/remove',
|
|
19
|
-
build: '../src/commands/build',
|
|
20
|
-
'create-prefab': '../src/commands/prefab-create',
|
|
21
|
-
'create-scene': '../src/commands/create-scene',
|
|
22
|
-
'screenshot': '../src/commands/screenshot'
|
|
23
|
-
};
|
|
24
|
-
|
|
25
|
-
// 帮助信息
|
|
26
|
-
function showHelp() {
|
|
27
|
-
console.log(`
|
|
28
|
-
Cocos Creator CLI - 场景/预制体操作工具集
|
|
29
|
-
|
|
30
|
-
用法:
|
|
31
|
-
cocos2d-cli <command> [options]
|
|
32
|
-
|
|
33
|
-
命令:
|
|
34
|
-
tree <场景.fire | 预制体.prefab> 查看节点树
|
|
35
|
-
get <场景.fire | 预制体.prefab> <节点路径> [属性名|组件类型] 获取节点或组件属性
|
|
36
|
-
set <场景.fire | 预制体.prefab> <节点路径> <属性名> <值> 修改节点属性
|
|
37
|
-
set-component <文件> <节点路径> <组件类型> <属性名> <值> 修改组件属性
|
|
38
|
-
add <场景.fire | 预制体.prefab> <父节点路径> <名称> 添加节点
|
|
39
|
-
add-component <文件> <节点路径> <类型> 给节点添加组件
|
|
40
|
-
remove-component <文件> <节点路径> <类型> 删除节点组件
|
|
41
|
-
remove <文件> <节点路径> 删除节点
|
|
42
|
-
build <项目目录> 构建组件映射
|
|
43
|
-
create-prefab [JSON文件路径] <输出.prefab> 创建预制体(不传JSON则创建默认)
|
|
44
|
-
create-scene [JSON文件路径] <输出.fire> 创建场景(不传JSON则创建默认)
|
|
45
|
-
screenshot <json文件> [选项] 渲染JSON并截图
|
|
46
|
-
|
|
47
|
-
节点路径格式:
|
|
48
|
-
Canvas - 根节点下的 Canvas
|
|
49
|
-
Canvas/MidNode - Canvas 下的 MidNode
|
|
50
|
-
Canvas/GameScene/NodeA - 多层嵌套路径
|
|
51
|
-
|
|
52
|
-
set 支持的节点属性:
|
|
53
|
-
name 节点名称
|
|
54
|
-
active 激活状态 (true/false)
|
|
55
|
-
x X 坐标
|
|
56
|
-
y Y 坐标
|
|
57
|
-
width 宽度
|
|
58
|
-
height 高度
|
|
59
|
-
anchorX 锚点 X (0-1)
|
|
60
|
-
anchorY 锚点 Y (0-1)
|
|
61
|
-
opacity 透明度 (0-255)
|
|
62
|
-
scaleX X 缩放
|
|
63
|
-
scaleY Y 缩放
|
|
64
|
-
rotation 旋转角度
|
|
65
|
-
|
|
66
|
-
set-component 示例:
|
|
67
|
-
cocos2d-cli set-component xxx.fire Canvas/Label Label string "Hello"
|
|
68
|
-
cocos2d-cli set-component xxx.fire Canvas/Label Label fontSize 32
|
|
69
|
-
cocos2d-cli set-component xxx.fire Canvas/Sprite Sprite sizeMode 0
|
|
70
|
-
|
|
71
|
-
add 支持的选项:
|
|
72
|
-
--x=<数值> 设置 X 坐标
|
|
73
|
-
--y=<数值> 设置 Y 坐标
|
|
74
|
-
--width=<数值> 设置宽度
|
|
75
|
-
--height=<数值> 设置高度
|
|
76
|
-
--scaleX=<数值> 设置 X 缩放
|
|
77
|
-
--scaleY=<数值> 设置 Y 缩放
|
|
78
|
-
--rotation=<角度> 设置旋转角度
|
|
79
|
-
--active=true/false 设置激活状态
|
|
80
|
-
--type=sprite/label/button 添加节点时指定组件类型
|
|
81
|
-
--string=<文字> Label 文字内容 (配合 --type=label)
|
|
82
|
-
--fontSize=<数值> Label 字体大小 (配合 --type=label)
|
|
83
|
-
|
|
84
|
-
JSON 格式:
|
|
85
|
-
详见 SKILL.md 完整文档。简要示例:
|
|
86
|
-
{
|
|
87
|
-
"name": "Node",
|
|
88
|
-
"width": 400, "height": 300,
|
|
89
|
-
"x": 0, "y": 0,
|
|
90
|
-
"anchorX": 0, "color": "#336699",
|
|
91
|
-
"components": [
|
|
92
|
-
{ "type": "label", "string": "Hello", "horizontalAlign": "left" },
|
|
93
|
-
{ "type": "richText", "string": "<color=#3cb034>绿色</color>" }
|
|
94
|
-
],
|
|
95
|
-
"children": [...]
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
提示:配合 anchorX + horizontalAlign 实现靠左/靠右布局(见 SKILL.md)
|
|
99
|
-
|
|
100
|
-
示例:
|
|
101
|
-
cocos2d-cli tree assets/main.fire
|
|
102
|
-
cocos2d-cli get assets/main.fire Canvas/MidNode
|
|
103
|
-
cocos2d-cli get assets/main.fire Canvas/MidNode x
|
|
104
|
-
cocos2d-cli get assets/main.fire Canvas/MidNode Label
|
|
105
|
-
cocos2d-cli set assets/main.fire Canvas/MidNode x 100
|
|
106
|
-
cocos2d-cli set assets/main.fire Canvas/MidNode width 200
|
|
107
|
-
cocos2d-cli set-component assets/main.fire Canvas/Label Label string "Hello World"
|
|
108
|
-
cocos2d-cli add assets/main.fire Canvas NewSprite --type=sprite --x=100
|
|
109
|
-
cocos2d-cli add-component assets/main.fire Canvas/MidNode label
|
|
110
|
-
cocos2d-cli remove-component assets/main.fire Canvas/MidNode label
|
|
111
|
-
cocos2d-cli remove assets/main.fire Canvas/MidNode
|
|
112
|
-
|
|
113
|
-
# 从 JSON 文件创建场景
|
|
114
|
-
cocos2d-cli create-scene scene.json assets/scene.fire
|
|
115
|
-
|
|
116
|
-
# 从 JSON 文件创建预制体
|
|
117
|
-
cocos2d-cli create-prefab panel.json assets/panel.prefab
|
|
118
|
-
|
|
119
|
-
# 创建默认预制体(不传JSON)
|
|
120
|
-
cocos2d-cli create-prefab assets/NewNode.prefab
|
|
121
|
-
|
|
122
|
-
# 截图
|
|
123
|
-
cocos2d-cli screenshot data.json
|
|
124
|
-
cocos2d-cli screenshot data.json -o ./screenshots --width 1080 --height 1920
|
|
125
|
-
`);
|
|
126
|
-
}
|
|
127
|
-
|
|
128
|
-
// 解析参数
|
|
129
|
-
const args = process.argv.slice(2);
|
|
130
|
-
|
|
131
|
-
if (args.length === 0 || args[0] === '--help' || args[0] === '-h' || args[0] === 'help') {
|
|
132
|
-
showHelp();
|
|
133
|
-
process.exit(0);
|
|
134
|
-
}
|
|
135
|
-
|
|
136
|
-
const commandName = args[0];
|
|
137
|
-
const commandPath = commands[commandName];
|
|
138
|
-
|
|
139
|
-
if (!commandPath) {
|
|
140
|
-
console.error(`未知命令: ${commandName}`);
|
|
141
|
-
console.error('运行 cocos2d-cli --help 查看可用命令');
|
|
142
|
-
process.exit(1);
|
|
143
|
-
}
|
|
144
|
-
|
|
145
|
-
// 加载并执行命令
|
|
146
|
-
try {
|
|
147
|
-
const command = require(commandPath);
|
|
148
|
-
command.run(args.slice(1));
|
|
149
|
-
} catch (err) {
|
|
150
|
-
console.error(`命令执行失败: ${err.message}`);
|
|
151
|
-
process.exit(1);
|
|
152
|
-
}
|
|
@@ -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 };
|