node-tree.js 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. 'use strict';
  2. const fs = require('fire-fs');
  3. const path = require('fire-path');
  4. const util = require('util');
  5. const configUtil = require('./config-util');
  6. const projectPath = Editor.Project.path;
  7. const adb = Editor.assetdb;
  8. var outputRelativePath;
  9. var outputFullPath;
  10. var codeNodeInit = '%s: %s;';
  11. var codeAssign = 'this.%s = this.%s.getChildByName("%s");';
  12. var codeAssign2 = 'this.%s = %s.getChildByName("%s");';
  13. var codeBody =
  14. `const { ccclass } = cc._decorator;
  15. @ccclass
  16. export default class %s extends cc.Component {
  17. %s
  18. onLoad () {
  19. %s
  20. }
  21. }
  22. `;
  23. var getWarnMsg = function (warnStr) {
  24. return {
  25. type: 'warning',
  26. buttons: ['OK'],
  27. titile: 'warning',
  28. message: warnStr,
  29. defaultId: 0,
  30. noLink: true
  31. };
  32. }
  33. /**获取控件真正的名字:现在的控件名后缀有特定功能,去掉后缀才是控件真正名字 */
  34. var getRealName = function (name) {
  35. let index = name.indexOf('__');
  36. if (index == -1) {
  37. return name;
  38. }
  39. name = name.substr(0, index);
  40. return name;
  41. }
  42. var getAutoUIName = function (url) {
  43. return 'auto_' + path.basenameNoExt(url);
  44. }
  45. module.exports = {
  46. init() {
  47. configUtil.initCfg((data) => {
  48. outputRelativePath = data.nodeOutputPath;
  49. outputFullPath = path.join(projectPath, outputRelativePath);
  50. });
  51. },
  52. dealFolder(assetInfo) {
  53. let url = assetInfo.url;
  54. if (!fs.existsSync(outputFullPath)) {
  55. fs.mkdirsSync(outputFullPath);
  56. }
  57. let moduleName = path.basenameNoExt(url);
  58. let moduleFolder = path.join(outputFullPath, moduleName);
  59. if (!fs.existsSync(moduleFolder)) {
  60. fs.mkdirsSync(moduleFolder);
  61. }
  62. Editor.log('dealFolder : ' + url);
  63. Editor.assetdb.queryAssets(url + '/**\/*', ['scene', 'prefab'],
  64. (err, results) => {
  65. results.forEach((r) => {
  66. if (err) {
  67. Editor.log('ERROR:' + err);
  68. } else {
  69. Editor.log('url:' + r.url);
  70. this.dealPrefab(r);
  71. }
  72. });
  73. });
  74. },
  75. dealPrefab(assetInfo) {
  76. if (!fs.existsSync(outputFullPath)) {
  77. fs.mkdirsSync(outputFullPath);
  78. }
  79. let url = assetInfo.url;
  80. //获取文件夹名称
  81. let moduleName = path.basenameNoExt(path.dirname(url));
  82. Editor.log('prefabFunc moduleName:', moduleName);
  83. //创建对应父文件夹
  84. let moduleFolder = path.join(outputFullPath, moduleName);
  85. if (!fs.existsSync(moduleFolder)) {
  86. fs.mkdirsSync(moduleFolder);
  87. }
  88. //生成对应的ts文件
  89. let uiName = getAutoUIName(url);
  90. let exportUIPath = `db://${outputRelativePath}/${moduleName}/${uiName}.ts`;
  91. let nameList = {};
  92. let sameNameList = [];
  93. let declareStr = '';
  94. let nodeInitStr = '';
  95. let assignStr = '';
  96. let json = fs.readJsonSync(assetInfo.path);
  97. let baseNode = json[0];
  98. let rootNode = json[1];
  99. let rootType = rootNode['__type__'];
  100. let rootName = rootNode['_name'];
  101. let isScene = baseNode['__type__'] == 'cc.SceneAsset';
  102. if (isScene) {
  103. let children = rootNode['_children'];
  104. if (!children || children.length == 0) {
  105. Editor.Dialog.messageBox(getWarnMsg('这是一个空的场景'));
  106. return;
  107. }
  108. assignStr += `\t\tlet parent = this.node.getParent();\n`;
  109. } else {
  110. nodeInitStr += '\t' + util.format(codeNodeInit, rootName, rootType) + '\n';
  111. assignStr += `\t\tthis.${rootName} = this.node\n`;
  112. }
  113. let outputFunc;
  114. outputFunc = function (root, nodeInfo, isScene) {
  115. var name = nodeInfo['_name'];
  116. let type = nodeInfo['__type__'];
  117. let parent = nodeInfo['_parent'];
  118. let parentId = parent ? parent['__id__'] : null;
  119. let parentName = 'node';
  120. let formatCodeAssign = codeAssign;
  121. if (parentId) {
  122. let parentInfo = root[parentId];
  123. parentName = parentInfo['_name'];
  124. if (isScene && parentName == undefined) {
  125. parentName = 'parent';
  126. formatCodeAssign = codeAssign2;
  127. }
  128. }
  129. if (name != rootName && name.indexOf(' ') == -1) {
  130. //同名控件检查
  131. if (nameList[name] == undefined) {
  132. nameList[name] = true;
  133. let realName = getRealName(name);
  134. let parentRealName = getRealName(parentName);
  135. nodeInitStr += '\t' + util.format(codeNodeInit, realName, type) + '\n';
  136. assignStr += '\t\t' + util.format(formatCodeAssign, realName, parentRealName, name) + '\n';
  137. } else {
  138. if (!sameNameList.hasOwnProperty(name)) {
  139. sameNameList.push(name);
  140. }
  141. }
  142. }
  143. let children = nodeInfo['_children'];
  144. if (!children || children == []) return;
  145. for (const childInfo in children) {
  146. if (children.hasOwnProperty(childInfo)) {
  147. const element = children[childInfo];
  148. let childID = element['__id__'];
  149. let childNode = root[childID];
  150. outputFunc(root, childNode, isScene);
  151. }
  152. }
  153. }
  154. outputFunc(json, rootNode, isScene);
  155. if (sameNameList.length > 0) {
  156. let warn = sameNameList.join('\n');
  157. Editor.log('warn ::' + warn);
  158. Editor.Dialog.messageBox(getWarnMsg(`输出中有控件命名重复: \n${warn}\n请修改`));
  159. }
  160. let urlStr = `\n\tpublic static URL:string = "${url}"\n`;
  161. declareStr = nodeInitStr + urlStr;
  162. let exportCode = util.format(codeBody, uiName, declareStr, assignStr);
  163. if (adb.exists(exportUIPath)) {
  164. adb.saveExists(exportUIPath, exportCode);
  165. } else {
  166. adb.create(exportUIPath, exportCode);
  167. }
  168. }
  169. }