cocos2d-cli 1.6.3 → 1.6.5
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/bin/cocos2d-cli.js +152 -152
- package/data/script_map.json +25 -25
- package/package.json +33 -33
- package/src/commands/add-component.js +112 -112
- package/src/commands/add.js +177 -177
- package/src/commands/build.js +78 -78
- package/src/commands/create-scene.js +181 -181
- package/src/commands/get.js +108 -108
- package/src/commands/prefab-create.js +110 -110
- package/src/commands/remove-component.js +110 -110
- package/src/commands/remove.js +99 -99
- package/src/commands/screenshot.js +108 -103
- package/src/commands/set-component.js +119 -119
- package/src/commands/set.js +107 -107
- package/src/commands/tree.js +28 -28
- package/src/lib/cc/CCButton.js +122 -122
- package/src/lib/cc/CCCamera.js +93 -93
- package/src/lib/cc/CCCanvas.js +54 -54
- package/src/lib/cc/CCColor.js +32 -32
- package/src/lib/cc/CCComponent.js +60 -60
- package/src/lib/cc/CCLabel.js +146 -146
- package/src/lib/cc/CCNode.js +255 -255
- package/src/lib/cc/CCObject.js +23 -23
- package/src/lib/cc/CCPrefab.js +242 -242
- package/src/lib/cc/CCRect.js +32 -32
- package/src/lib/cc/CCRichText.js +44 -44
- package/src/lib/cc/CCScene.js +42 -42
- package/src/lib/cc/CCSceneAsset.js +302 -302
- package/src/lib/cc/CCSize.js +26 -26
- package/src/lib/cc/CCSprite.js +94 -94
- package/src/lib/cc/CCTrs.js +74 -74
- package/src/lib/cc/CCVec2.js +26 -26
- package/src/lib/cc/CCVec3.js +29 -29
- package/src/lib/cc/CCWidget.js +98 -98
- package/src/lib/cc/index.js +42 -42
- package/src/lib/fire-utils.js +373 -373
- package/src/lib/json-parser.js +185 -185
- package/src/lib/node-utils.js +395 -395
- package/src/lib/screenshot/index.html +29 -29
- package/src/lib/screenshot-core.js +285 -286
- package/src/lib/templates.js +49 -49
- package/src/lib/utils.js +202 -202
|
@@ -1,103 +1,108 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* screenshot 命令
|
|
3
|
-
* 渲染 JSON 数据并截图
|
|
4
|
-
*/
|
|
5
|
-
|
|
6
|
-
const path = require('path');
|
|
7
|
-
const { takeScreenshot } = require('../lib/screenshot-core');
|
|
8
|
-
|
|
9
|
-
function showHelp() {
|
|
10
|
-
console.log(`
|
|
11
|
-
用法:
|
|
12
|
-
cocos2d-cli screenshot <json文件> [选项]
|
|
13
|
-
|
|
14
|
-
选项:
|
|
15
|
-
-o, --output <目录> 输出目录,默认当前目录
|
|
16
|
-
--width <数值> 视口宽度,默认 750(不支持简写)
|
|
17
|
-
--height <数值> 视口高度,默认 1334(不支持简写)
|
|
18
|
-
--full-page 全页截图(默认)
|
|
19
|
-
--no-full-page 仅视口截图
|
|
20
|
-
--debug-bounds 叠加节点边界框和名称,方便定位布局问题
|
|
21
|
-
--timeout <毫秒> 页面加载超时,默认 30000
|
|
22
|
-
--wait <毫秒> 截图前等待时间,默认 1000
|
|
23
|
-
|
|
24
|
-
示例:
|
|
25
|
-
cocos2d-cli screenshot data.json
|
|
26
|
-
cocos2d-cli screenshot data.json -o ./screenshots
|
|
27
|
-
cocos2d-cli screenshot data.json --width 1080 --height 1920
|
|
28
|
-
cocos2d-cli screenshot data.json --debug-bounds
|
|
29
|
-
cocos2d-cli screenshot data.json --no-full-page
|
|
30
|
-
`);
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
function parseArgs(args) {
|
|
34
|
-
const options = {
|
|
35
|
-
jsonPath: null,
|
|
36
|
-
outputDir: '.',
|
|
37
|
-
viewport: { width: 750, height: 1334 },
|
|
38
|
-
fullPage: true,
|
|
39
|
-
debugBounds: false,
|
|
40
|
-
timeout: 30000,
|
|
41
|
-
waitTime: 1000
|
|
42
|
-
};
|
|
43
|
-
|
|
44
|
-
let i = 0;
|
|
45
|
-
while (i < args.length) {
|
|
46
|
-
const arg = args[i];
|
|
47
|
-
|
|
48
|
-
if (arg === '--help' || arg === '-h') {
|
|
49
|
-
showHelp();
|
|
50
|
-
process.exit(0);
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
if (arg === '-o' || arg === '--output') {
|
|
54
|
-
options.outputDir = args[++i];
|
|
55
|
-
} else if (arg === '--width') {
|
|
56
|
-
options.viewport.width = parseInt(args[++i], 10);
|
|
57
|
-
} else if (arg === '--height') {
|
|
58
|
-
options.viewport.height = parseInt(args[++i], 10);
|
|
59
|
-
} else if (arg === '--full-page') {
|
|
60
|
-
options.fullPage = true;
|
|
61
|
-
} else if (arg === '--no-full-page') {
|
|
62
|
-
options.fullPage = false;
|
|
63
|
-
} else if (arg === '--debug-bounds') {
|
|
64
|
-
options.debugBounds = true;
|
|
65
|
-
} else if (arg === '--timeout') {
|
|
66
|
-
options.timeout = parseInt(args[++i], 10);
|
|
67
|
-
} else if (arg === '--wait') {
|
|
68
|
-
options.waitTime = parseInt(args[++i], 10);
|
|
69
|
-
} else if (!arg.startsWith('-')) {
|
|
70
|
-
options.jsonPath = arg;
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
i++;
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
return options;
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
async function run(args) {
|
|
80
|
-
const options = parseArgs(args);
|
|
81
|
-
|
|
82
|
-
if (!options.jsonPath) {
|
|
83
|
-
console.error('错误: 请指定 JSON 文件路径');
|
|
84
|
-
showHelp();
|
|
85
|
-
process.exit(1);
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
options.jsonPath = path.resolve(options.jsonPath);
|
|
89
|
-
options.outputDir = path.resolve(options.outputDir);
|
|
90
|
-
|
|
91
|
-
try {
|
|
92
|
-
await takeScreenshot(options);
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
1
|
+
/**
|
|
2
|
+
* screenshot 命令
|
|
3
|
+
* 渲染 JSON 数据并截图
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
const path = require('path');
|
|
7
|
+
const { takeScreenshot } = require('../lib/screenshot-core');
|
|
8
|
+
|
|
9
|
+
function showHelp() {
|
|
10
|
+
console.log(`
|
|
11
|
+
用法:
|
|
12
|
+
cocos2d-cli screenshot <json文件> [选项]
|
|
13
|
+
|
|
14
|
+
选项:
|
|
15
|
+
-o, --output <目录> 输出目录,默认当前目录
|
|
16
|
+
--width <数值> 视口宽度,默认 750(不支持简写)
|
|
17
|
+
--height <数值> 视口高度,默认 1334(不支持简写)
|
|
18
|
+
--full-page 全页截图(默认)
|
|
19
|
+
--no-full-page 仅视口截图
|
|
20
|
+
--debug-bounds 叠加节点边界框和名称,方便定位布局问题
|
|
21
|
+
--timeout <毫秒> 页面加载超时,默认 30000
|
|
22
|
+
--wait <毫秒> 截图前等待时间,默认 1000
|
|
23
|
+
|
|
24
|
+
示例:
|
|
25
|
+
cocos2d-cli screenshot data.json
|
|
26
|
+
cocos2d-cli screenshot data.json -o ./screenshots
|
|
27
|
+
cocos2d-cli screenshot data.json --width 1080 --height 1920
|
|
28
|
+
cocos2d-cli screenshot data.json --debug-bounds
|
|
29
|
+
cocos2d-cli screenshot data.json --no-full-page
|
|
30
|
+
`);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
function parseArgs(args) {
|
|
34
|
+
const options = {
|
|
35
|
+
jsonPath: null,
|
|
36
|
+
outputDir: '.',
|
|
37
|
+
viewport: { width: 750, height: 1334 },
|
|
38
|
+
fullPage: true,
|
|
39
|
+
debugBounds: false,
|
|
40
|
+
timeout: 30000,
|
|
41
|
+
waitTime: 1000
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
let i = 0;
|
|
45
|
+
while (i < args.length) {
|
|
46
|
+
const arg = args[i];
|
|
47
|
+
|
|
48
|
+
if (arg === '--help' || arg === '-h') {
|
|
49
|
+
showHelp();
|
|
50
|
+
process.exit(0);
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
if (arg === '-o' || arg === '--output') {
|
|
54
|
+
options.outputDir = args[++i];
|
|
55
|
+
} else if (arg === '--width') {
|
|
56
|
+
options.viewport.width = parseInt(args[++i], 10);
|
|
57
|
+
} else if (arg === '--height') {
|
|
58
|
+
options.viewport.height = parseInt(args[++i], 10);
|
|
59
|
+
} else if (arg === '--full-page') {
|
|
60
|
+
options.fullPage = true;
|
|
61
|
+
} else if (arg === '--no-full-page') {
|
|
62
|
+
options.fullPage = false;
|
|
63
|
+
} else if (arg === '--debug-bounds') {
|
|
64
|
+
options.debugBounds = true;
|
|
65
|
+
} else if (arg === '--timeout') {
|
|
66
|
+
options.timeout = parseInt(args[++i], 10);
|
|
67
|
+
} else if (arg === '--wait') {
|
|
68
|
+
options.waitTime = parseInt(args[++i], 10);
|
|
69
|
+
} else if (!arg.startsWith('-')) {
|
|
70
|
+
options.jsonPath = arg;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
i++;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
return options;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
async function run(args) {
|
|
80
|
+
const options = parseArgs(args);
|
|
81
|
+
|
|
82
|
+
if (!options.jsonPath) {
|
|
83
|
+
console.error('错误: 请指定 JSON 文件路径');
|
|
84
|
+
showHelp();
|
|
85
|
+
process.exit(1);
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
options.jsonPath = path.resolve(options.jsonPath);
|
|
89
|
+
options.outputDir = path.resolve(options.outputDir);
|
|
90
|
+
|
|
91
|
+
try {
|
|
92
|
+
const result = await takeScreenshot(options);
|
|
93
|
+
const filename = path.basename(result.screenshotPath);
|
|
94
|
+
console.log(JSON.stringify({
|
|
95
|
+
success: true,
|
|
96
|
+
filename: filename
|
|
97
|
+
}));
|
|
98
|
+
} catch (error) {
|
|
99
|
+
console.log(JSON.stringify({
|
|
100
|
+
success: false,
|
|
101
|
+
error: error.message,
|
|
102
|
+
logDir: error.logDir || null
|
|
103
|
+
}));
|
|
104
|
+
process.exit(1);
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
module.exports = { run };
|
|
@@ -1,119 +1,119 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* set-component 命令 - 设置组件属性
|
|
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
|
-
/**
|
|
37
|
-
* 查找组件
|
|
38
|
-
*/
|
|
39
|
-
function findComponent(node, compType) {
|
|
40
|
-
if (!node._components) return null;
|
|
41
|
-
|
|
42
|
-
const type = compType.toLowerCase();
|
|
43
|
-
const typeName = 'cc.' + type.charAt(0).toUpperCase() + type.slice(1);
|
|
44
|
-
|
|
45
|
-
return node._components.find(c => c.__type__ === typeName);
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
function run(args) {
|
|
49
|
-
if (args.length < 4) {
|
|
50
|
-
console.log(JSON.stringify({
|
|
51
|
-
error: '用法: cocos2d-cli set-component <场景.fire|预制体.prefab> <节点路径> <组件类型> <属性名> <值>'
|
|
52
|
-
}));
|
|
53
|
-
return;
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
const filePath = args[0];
|
|
57
|
-
const nodePath = args[1];
|
|
58
|
-
const compType = args[2];
|
|
59
|
-
const prop = args[3];
|
|
60
|
-
const value = args[4];
|
|
61
|
-
|
|
62
|
-
const ext = path.extname(filePath).toLowerCase();
|
|
63
|
-
|
|
64
|
-
try {
|
|
65
|
-
let root;
|
|
66
|
-
let asset;
|
|
67
|
-
const json = JSON.parse(fs.readFileSync(filePath, 'utf8'));
|
|
68
|
-
|
|
69
|
-
if (ext === '.fire') {
|
|
70
|
-
asset = CCSceneAsset.fromJSON(json);
|
|
71
|
-
root = asset._scene;
|
|
72
|
-
} else if (ext === '.prefab') {
|
|
73
|
-
asset = CCPrefab.fromJSON(json);
|
|
74
|
-
root = asset._root;
|
|
75
|
-
} else {
|
|
76
|
-
console.log(JSON.stringify({ error: '不支持的文件类型,仅支持 .fire 和 .prefab' }));
|
|
77
|
-
return;
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
const node = findNode(root, nodePath);
|
|
81
|
-
|
|
82
|
-
if (!node) {
|
|
83
|
-
console.log(JSON.stringify({ error: `节点不存在: ${nodePath}` }));
|
|
84
|
-
return;
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
const comp = findComponent(node, compType);
|
|
88
|
-
|
|
89
|
-
if (!comp) {
|
|
90
|
-
console.log(JSON.stringify({ error: `组件不存在: ${compType}` }));
|
|
91
|
-
return;
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
// 调用组件的 setProp 方法
|
|
95
|
-
if (typeof comp.setProp !== 'function') {
|
|
96
|
-
console.log(JSON.stringify({ error: '组件不支持 setProp 方法' }));
|
|
97
|
-
return;
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
// 构造属性对象并调用 setProp
|
|
101
|
-
const props = { [prop]: value };
|
|
102
|
-
comp.setProp(props);
|
|
103
|
-
|
|
104
|
-
// 保存
|
|
105
|
-
const data = asset.toJSON();
|
|
106
|
-
fs.writeFileSync(filePath, JSON.stringify(data, null, 2), 'utf8');
|
|
107
|
-
|
|
108
|
-
// 输出节点树
|
|
109
|
-
const scriptMap = loadScriptMap(filePath);
|
|
110
|
-
const prefab = isPrefab(data);
|
|
111
|
-
const startIndex = prefab ? 0 : 1;
|
|
112
|
-
console.log(buildTree(data, scriptMap, startIndex).trim());
|
|
113
|
-
|
|
114
|
-
} catch (err) {
|
|
115
|
-
console.log(JSON.stringify({ error: err.message }));
|
|
116
|
-
}
|
|
117
|
-
}
|
|
118
|
-
|
|
119
|
-
module.exports = { run };
|
|
1
|
+
/**
|
|
2
|
+
* set-component 命令 - 设置组件属性
|
|
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
|
+
/**
|
|
37
|
+
* 查找组件
|
|
38
|
+
*/
|
|
39
|
+
function findComponent(node, compType) {
|
|
40
|
+
if (!node._components) return null;
|
|
41
|
+
|
|
42
|
+
const type = compType.toLowerCase();
|
|
43
|
+
const typeName = 'cc.' + type.charAt(0).toUpperCase() + type.slice(1);
|
|
44
|
+
|
|
45
|
+
return node._components.find(c => c.__type__ === typeName);
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
function run(args) {
|
|
49
|
+
if (args.length < 4) {
|
|
50
|
+
console.log(JSON.stringify({
|
|
51
|
+
error: '用法: cocos2d-cli set-component <场景.fire|预制体.prefab> <节点路径> <组件类型> <属性名> <值>'
|
|
52
|
+
}));
|
|
53
|
+
return;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
const filePath = args[0];
|
|
57
|
+
const nodePath = args[1];
|
|
58
|
+
const compType = args[2];
|
|
59
|
+
const prop = args[3];
|
|
60
|
+
const value = args[4];
|
|
61
|
+
|
|
62
|
+
const ext = path.extname(filePath).toLowerCase();
|
|
63
|
+
|
|
64
|
+
try {
|
|
65
|
+
let root;
|
|
66
|
+
let asset;
|
|
67
|
+
const json = JSON.parse(fs.readFileSync(filePath, 'utf8'));
|
|
68
|
+
|
|
69
|
+
if (ext === '.fire') {
|
|
70
|
+
asset = CCSceneAsset.fromJSON(json);
|
|
71
|
+
root = asset._scene;
|
|
72
|
+
} else if (ext === '.prefab') {
|
|
73
|
+
asset = CCPrefab.fromJSON(json);
|
|
74
|
+
root = asset._root;
|
|
75
|
+
} else {
|
|
76
|
+
console.log(JSON.stringify({ error: '不支持的文件类型,仅支持 .fire 和 .prefab' }));
|
|
77
|
+
return;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
const node = findNode(root, nodePath);
|
|
81
|
+
|
|
82
|
+
if (!node) {
|
|
83
|
+
console.log(JSON.stringify({ error: `节点不存在: ${nodePath}` }));
|
|
84
|
+
return;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
const comp = findComponent(node, compType);
|
|
88
|
+
|
|
89
|
+
if (!comp) {
|
|
90
|
+
console.log(JSON.stringify({ error: `组件不存在: ${compType}` }));
|
|
91
|
+
return;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
// 调用组件的 setProp 方法
|
|
95
|
+
if (typeof comp.setProp !== 'function') {
|
|
96
|
+
console.log(JSON.stringify({ error: '组件不支持 setProp 方法' }));
|
|
97
|
+
return;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
// 构造属性对象并调用 setProp
|
|
101
|
+
const props = { [prop]: value };
|
|
102
|
+
comp.setProp(props);
|
|
103
|
+
|
|
104
|
+
// 保存
|
|
105
|
+
const data = asset.toJSON();
|
|
106
|
+
fs.writeFileSync(filePath, JSON.stringify(data, null, 2), 'utf8');
|
|
107
|
+
|
|
108
|
+
// 输出节点树
|
|
109
|
+
const scriptMap = loadScriptMap(filePath);
|
|
110
|
+
const prefab = isPrefab(data);
|
|
111
|
+
const startIndex = prefab ? 0 : 1;
|
|
112
|
+
console.log(buildTree(data, scriptMap, startIndex).trim());
|
|
113
|
+
|
|
114
|
+
} catch (err) {
|
|
115
|
+
console.log(JSON.stringify({ error: err.message }));
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
module.exports = { run };
|
package/src/commands/set.js
CHANGED
|
@@ -1,107 +1,107 @@
|
|
|
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
|
+
/**
|
|
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 };
|