cocos2d-cli 1.6.4 → 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.
Files changed (42) hide show
  1. package/bin/cocos2d-cli.js +152 -152
  2. package/data/script_map.json +25 -25
  3. package/package.json +33 -33
  4. package/src/commands/add-component.js +112 -112
  5. package/src/commands/add.js +177 -177
  6. package/src/commands/build.js +78 -78
  7. package/src/commands/create-scene.js +181 -181
  8. package/src/commands/get.js +108 -108
  9. package/src/commands/prefab-create.js +110 -110
  10. package/src/commands/remove-component.js +110 -110
  11. package/src/commands/remove.js +99 -99
  12. package/src/commands/screenshot.js +108 -108
  13. package/src/commands/set-component.js +119 -119
  14. package/src/commands/set.js +107 -107
  15. package/src/commands/tree.js +28 -28
  16. package/src/lib/cc/CCButton.js +122 -122
  17. package/src/lib/cc/CCCamera.js +93 -93
  18. package/src/lib/cc/CCCanvas.js +54 -54
  19. package/src/lib/cc/CCColor.js +32 -32
  20. package/src/lib/cc/CCComponent.js +60 -60
  21. package/src/lib/cc/CCLabel.js +146 -146
  22. package/src/lib/cc/CCNode.js +255 -255
  23. package/src/lib/cc/CCObject.js +23 -23
  24. package/src/lib/cc/CCPrefab.js +242 -242
  25. package/src/lib/cc/CCRect.js +32 -32
  26. package/src/lib/cc/CCRichText.js +44 -44
  27. package/src/lib/cc/CCScene.js +42 -42
  28. package/src/lib/cc/CCSceneAsset.js +302 -302
  29. package/src/lib/cc/CCSize.js +26 -26
  30. package/src/lib/cc/CCSprite.js +94 -94
  31. package/src/lib/cc/CCTrs.js +74 -74
  32. package/src/lib/cc/CCVec2.js +26 -26
  33. package/src/lib/cc/CCVec3.js +29 -29
  34. package/src/lib/cc/CCWidget.js +98 -98
  35. package/src/lib/cc/index.js +42 -42
  36. package/src/lib/fire-utils.js +373 -373
  37. package/src/lib/json-parser.js +185 -185
  38. package/src/lib/node-utils.js +395 -395
  39. package/src/lib/screenshot/index.html +29 -29
  40. package/src/lib/screenshot-core.js +285 -286
  41. package/src/lib/templates.js +49 -49
  42. package/src/lib/utils.js +202 -202
package/src/lib/utils.js CHANGED
@@ -1,202 +1,202 @@
1
- /**
2
- * 公共工具模块
3
- * 提供颜色解析、UUID 生成等通用功能
4
- */
5
-
6
- /**
7
- * 解析颜色字符串 #RRGGBB 或 #RRGGBBAA
8
- * @param {string} colorStr - 颜色字符串
9
- * @returns {object|null} - 颜色对象 {r, g, b, a} 或 null
10
- */
11
- function parseColor(colorStr) {
12
- if (!colorStr || typeof colorStr !== 'string') return null;
13
-
14
- let hex = colorStr.replace('#', '');
15
- if (hex.length === 6) {
16
- return {
17
- r: parseInt(hex.substring(0, 2), 16),
18
- g: parseInt(hex.substring(2, 4), 16),
19
- b: parseInt(hex.substring(4, 6), 16),
20
- a: 255
21
- };
22
- } else if (hex.length === 8) {
23
- return {
24
- r: parseInt(hex.substring(0, 2), 16),
25
- g: parseInt(hex.substring(2, 4), 16),
26
- b: parseInt(hex.substring(4, 6), 16),
27
- a: parseInt(hex.substring(6, 8), 16)
28
- };
29
- }
30
- return null;
31
- }
32
-
33
- /**
34
- * 解析颜色字符串并返回 cc.Color 格式
35
- * @param {string} colorStr - 颜色字符串
36
- * @returns {object|null} - cc.Color 对象或 null
37
- */
38
- function parseColorToCcColor(colorStr) {
39
- const color = parseColor(colorStr);
40
- if (!color) return null;
41
- return { "__type__": "cc.Color", ...color };
42
- }
43
-
44
- /**
45
- * 将 cc.Color 对象转为 #RRGGBB 字符串
46
- * @param {object} color - cc.Color 对象
47
- * @returns {string} - #RRGGBB 格式字符串
48
- */
49
- function colorToHex(color) {
50
- if (!color) return '#ffffff';
51
- const r = (color.r || 0).toString(16).padStart(2, '0');
52
- const g = (color.g || 0).toString(16).padStart(2, '0');
53
- const b = (color.b || 0).toString(16).padStart(2, '0');
54
- return `#${r}${g}${b}`;
55
- }
56
-
57
- /**
58
- * 生成 UUID
59
- * @returns {string} - UUID 字符串
60
- */
61
- function generateUUID() {
62
- return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
63
- const r = Math.random() * 16 | 0;
64
- const v = c === 'x' ? r : (r & 0x3 | 0x8);
65
- return v.toString(16);
66
- });
67
- }
68
-
69
- /**
70
- * 将标准 UUID 压缩为 Cocos Creator 格式(22位 base64)
71
- * @param {string} uuid - 标准 UUID(如 "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx")
72
- * @returns {string} - 压缩后的 UUID(如 "a5esZu+45LA5mBpvttspPD")
73
- */
74
- function compressUUID(uuid) {
75
- if (!uuid) return '';
76
- // 去掉横线
77
- const hex = uuid.replace(/-/g, '');
78
- if (hex.length !== 32) return uuid; // 不是标准格式,直接返回
79
-
80
- // 转 base64
81
- const chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
82
- let result = '';
83
- for (let i = 0; i < hex.length; i += 3) {
84
- const byte1 = parseInt(hex.substr(i, 2), 16);
85
- let byte2 = 0, byte3 = 0;
86
- let group = 1;
87
-
88
- if (i + 2 < hex.length) {
89
- byte2 = parseInt(hex.substr(i + 2, 2), 16);
90
- group = 2;
91
- }
92
- if (i + 4 < hex.length) {
93
- byte3 = parseInt(hex.substr(i + 4, 2), 16);
94
- group = 3;
95
- }
96
-
97
- // 由于我们是每3个字符(1.5字节)处理,需要重新计算
98
- }
99
-
100
- // 更简单的方式:将32个十六进制字符转为16字节,再base64编码
101
- const bytes = [];
102
- for (let i = 0; i < hex.length; i += 2) {
103
- bytes.push(parseInt(hex.substr(i, 2), 16));
104
- }
105
-
106
- // Base64 编码
107
- for (let i = 0; i < bytes.length; i += 3) {
108
- const b1 = bytes[i];
109
- const b2 = bytes[i + 1] || 0;
110
- const b3 = bytes[i + 2] || 0;
111
-
112
- result += chars[(b1 >> 2) & 0x3f];
113
- result += chars[((b1 & 0x03) << 4) | ((b2 >> 4) & 0x0f)];
114
- result += chars[((b2 & 0x0f) << 2) | ((b3 >> 6) & 0x03)];
115
- result += chars[b3 & 0x3f];
116
- }
117
-
118
- // 返回前22个字符(去掉末尾填充)
119
- return result.substring(0, 22);
120
- }
121
-
122
- /**
123
- * 生成压缩格式的 UUID(Cocos Creator 节点使用)
124
- * @returns {string} - 压缩后的 UUID(22位)
125
- */
126
- function generateCompressedUUID() {
127
- return compressUUID(generateUUID());
128
- }
129
-
130
- /**
131
- * 生成 Cocos Creator 组件 ID(22位 base64 格式)
132
- * @returns {string} - 组件 ID 字符串
133
- */
134
- function generateId() {
135
- const chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
136
- let result = '';
137
- for (let i = 0; i < 22; i++) {
138
- result += chars.charAt(Math.floor(Math.random() * chars.length));
139
- }
140
- return result;
141
- }
142
-
143
- /**
144
- * 解析命令行选项
145
- * @param {string[]} args - 命令行参数数组
146
- * @param {number} startIndex - 开始解析的索引
147
- * @returns {object} - 选项对象
148
- */
149
- function parseOptions(args, startIndex = 0) {
150
- const options = {};
151
- for (let i = startIndex; i < args.length; i++) {
152
- const arg = args[i];
153
- if (arg.startsWith('--')) {
154
- const eqIndex = arg.indexOf('=');
155
- if (eqIndex > 2) {
156
- const key = arg.substring(2, eqIndex);
157
- const value = arg.substring(eqIndex + 1);
158
- options[key] = value;
159
- }
160
- }
161
- }
162
- return options;
163
- }
164
-
165
- /**
166
- * 输出 JSON 格式结果
167
- * @param {object} data - 要输出的数据
168
- */
169
- function outputJson(data) {
170
- console.log(JSON.stringify(data));
171
- }
172
-
173
- /**
174
- * 输出错误信息
175
- * @param {string} message - 错误信息
176
- * @param {object} extra - 额外信息
177
- */
178
- function outputError(message, extra = {}) {
179
- console.log(JSON.stringify({ error: message, ...extra }));
180
- }
181
-
182
- /**
183
- * 输出成功信息
184
- * @param {object} data - 成功数据
185
- */
186
- function outputSuccess(data) {
187
- console.log(JSON.stringify({ success: true, ...data }));
188
- }
189
-
190
- module.exports = {
191
- parseColor,
192
- parseColorToCcColor,
193
- colorToHex,
194
- generateUUID,
195
- compressUUID,
196
- generateCompressedUUID,
197
- generateId,
198
- parseOptions,
199
- outputJson,
200
- outputError,
201
- outputSuccess
202
- };
1
+ /**
2
+ * 公共工具模块
3
+ * 提供颜色解析、UUID 生成等通用功能
4
+ */
5
+
6
+ /**
7
+ * 解析颜色字符串 #RRGGBB 或 #RRGGBBAA
8
+ * @param {string} colorStr - 颜色字符串
9
+ * @returns {object|null} - 颜色对象 {r, g, b, a} 或 null
10
+ */
11
+ function parseColor(colorStr) {
12
+ if (!colorStr || typeof colorStr !== 'string') return null;
13
+
14
+ let hex = colorStr.replace('#', '');
15
+ if (hex.length === 6) {
16
+ return {
17
+ r: parseInt(hex.substring(0, 2), 16),
18
+ g: parseInt(hex.substring(2, 4), 16),
19
+ b: parseInt(hex.substring(4, 6), 16),
20
+ a: 255
21
+ };
22
+ } else if (hex.length === 8) {
23
+ return {
24
+ r: parseInt(hex.substring(0, 2), 16),
25
+ g: parseInt(hex.substring(2, 4), 16),
26
+ b: parseInt(hex.substring(4, 6), 16),
27
+ a: parseInt(hex.substring(6, 8), 16)
28
+ };
29
+ }
30
+ return null;
31
+ }
32
+
33
+ /**
34
+ * 解析颜色字符串并返回 cc.Color 格式
35
+ * @param {string} colorStr - 颜色字符串
36
+ * @returns {object|null} - cc.Color 对象或 null
37
+ */
38
+ function parseColorToCcColor(colorStr) {
39
+ const color = parseColor(colorStr);
40
+ if (!color) return null;
41
+ return { "__type__": "cc.Color", ...color };
42
+ }
43
+
44
+ /**
45
+ * 将 cc.Color 对象转为 #RRGGBB 字符串
46
+ * @param {object} color - cc.Color 对象
47
+ * @returns {string} - #RRGGBB 格式字符串
48
+ */
49
+ function colorToHex(color) {
50
+ if (!color) return '#ffffff';
51
+ const r = (color.r || 0).toString(16).padStart(2, '0');
52
+ const g = (color.g || 0).toString(16).padStart(2, '0');
53
+ const b = (color.b || 0).toString(16).padStart(2, '0');
54
+ return `#${r}${g}${b}`;
55
+ }
56
+
57
+ /**
58
+ * 生成 UUID
59
+ * @returns {string} - UUID 字符串
60
+ */
61
+ function generateUUID() {
62
+ return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
63
+ const r = Math.random() * 16 | 0;
64
+ const v = c === 'x' ? r : (r & 0x3 | 0x8);
65
+ return v.toString(16);
66
+ });
67
+ }
68
+
69
+ /**
70
+ * 将标准 UUID 压缩为 Cocos Creator 格式(22位 base64)
71
+ * @param {string} uuid - 标准 UUID(如 "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx")
72
+ * @returns {string} - 压缩后的 UUID(如 "a5esZu+45LA5mBpvttspPD")
73
+ */
74
+ function compressUUID(uuid) {
75
+ if (!uuid) return '';
76
+ // 去掉横线
77
+ const hex = uuid.replace(/-/g, '');
78
+ if (hex.length !== 32) return uuid; // 不是标准格式,直接返回
79
+
80
+ // 转 base64
81
+ const chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
82
+ let result = '';
83
+ for (let i = 0; i < hex.length; i += 3) {
84
+ const byte1 = parseInt(hex.substr(i, 2), 16);
85
+ let byte2 = 0, byte3 = 0;
86
+ let group = 1;
87
+
88
+ if (i + 2 < hex.length) {
89
+ byte2 = parseInt(hex.substr(i + 2, 2), 16);
90
+ group = 2;
91
+ }
92
+ if (i + 4 < hex.length) {
93
+ byte3 = parseInt(hex.substr(i + 4, 2), 16);
94
+ group = 3;
95
+ }
96
+
97
+ // 由于我们是每3个字符(1.5字节)处理,需要重新计算
98
+ }
99
+
100
+ // 更简单的方式:将32个十六进制字符转为16字节,再base64编码
101
+ const bytes = [];
102
+ for (let i = 0; i < hex.length; i += 2) {
103
+ bytes.push(parseInt(hex.substr(i, 2), 16));
104
+ }
105
+
106
+ // Base64 编码
107
+ for (let i = 0; i < bytes.length; i += 3) {
108
+ const b1 = bytes[i];
109
+ const b2 = bytes[i + 1] || 0;
110
+ const b3 = bytes[i + 2] || 0;
111
+
112
+ result += chars[(b1 >> 2) & 0x3f];
113
+ result += chars[((b1 & 0x03) << 4) | ((b2 >> 4) & 0x0f)];
114
+ result += chars[((b2 & 0x0f) << 2) | ((b3 >> 6) & 0x03)];
115
+ result += chars[b3 & 0x3f];
116
+ }
117
+
118
+ // 返回前22个字符(去掉末尾填充)
119
+ return result.substring(0, 22);
120
+ }
121
+
122
+ /**
123
+ * 生成压缩格式的 UUID(Cocos Creator 节点使用)
124
+ * @returns {string} - 压缩后的 UUID(22位)
125
+ */
126
+ function generateCompressedUUID() {
127
+ return compressUUID(generateUUID());
128
+ }
129
+
130
+ /**
131
+ * 生成 Cocos Creator 组件 ID(22位 base64 格式)
132
+ * @returns {string} - 组件 ID 字符串
133
+ */
134
+ function generateId() {
135
+ const chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
136
+ let result = '';
137
+ for (let i = 0; i < 22; i++) {
138
+ result += chars.charAt(Math.floor(Math.random() * chars.length));
139
+ }
140
+ return result;
141
+ }
142
+
143
+ /**
144
+ * 解析命令行选项
145
+ * @param {string[]} args - 命令行参数数组
146
+ * @param {number} startIndex - 开始解析的索引
147
+ * @returns {object} - 选项对象
148
+ */
149
+ function parseOptions(args, startIndex = 0) {
150
+ const options = {};
151
+ for (let i = startIndex; i < args.length; i++) {
152
+ const arg = args[i];
153
+ if (arg.startsWith('--')) {
154
+ const eqIndex = arg.indexOf('=');
155
+ if (eqIndex > 2) {
156
+ const key = arg.substring(2, eqIndex);
157
+ const value = arg.substring(eqIndex + 1);
158
+ options[key] = value;
159
+ }
160
+ }
161
+ }
162
+ return options;
163
+ }
164
+
165
+ /**
166
+ * 输出 JSON 格式结果
167
+ * @param {object} data - 要输出的数据
168
+ */
169
+ function outputJson(data) {
170
+ console.log(JSON.stringify(data));
171
+ }
172
+
173
+ /**
174
+ * 输出错误信息
175
+ * @param {string} message - 错误信息
176
+ * @param {object} extra - 额外信息
177
+ */
178
+ function outputError(message, extra = {}) {
179
+ console.log(JSON.stringify({ error: message, ...extra }));
180
+ }
181
+
182
+ /**
183
+ * 输出成功信息
184
+ * @param {object} data - 成功数据
185
+ */
186
+ function outputSuccess(data) {
187
+ console.log(JSON.stringify({ success: true, ...data }));
188
+ }
189
+
190
+ module.exports = {
191
+ parseColor,
192
+ parseColorToCcColor,
193
+ colorToHex,
194
+ generateUUID,
195
+ compressUUID,
196
+ generateCompressedUUID,
197
+ generateId,
198
+ parseOptions,
199
+ outputJson,
200
+ outputError,
201
+ outputSuccess
202
+ };