GenCode_CSharp.js 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.genCodeCS = void 0;
  4. const csharp_1 = require("csharp");
  5. const CodeWriter_1 = require("./CodeWriter");
  6. class GenClassInfo {
  7. name;
  8. varName;
  9. type;
  10. isCustomType;
  11. otherPkgName;
  12. glistItemView;
  13. constructor(member) {
  14. this.name = member.name;
  15. this.varName = member.varName;
  16. this.type = member.type;
  17. this.isCustomType = false;
  18. }
  19. }
  20. let exportLangKV = new Map();
  21. /**UI的vm类名后缀*/
  22. let vmSuffix = "VM";
  23. let viewSuffix = "View";
  24. let uiLayerEnum = ["MapUI", "Normal", "Middle", "High", "Popup", "Toast", "Guide", "Effect"];
  25. function genCodeCS(handler) {
  26. let settings = handler.project.GetSettings("Publish").codeGeneration;
  27. let codePkgName = handler.ToFilename(handler.pkg.name); //convert chinese to pinyin, remove special chars etc.
  28. codePkgName = myCapitalize(codePkgName);
  29. //package代码的根目录
  30. let exportCodePath = `${handler.exportCodePath}/${codePkgName}`;
  31. //模板代码根目录
  32. let codeTemplatesPath = handler.project.assetsPath.replace("assets", "plugins/xgame_gen_code/Templates/cs");
  33. console.log(`CodePath:${exportCodePath} codePkgName:${codePkgName}`);
  34. console.log(`codeTemplatesPath:${codeTemplatesPath} getMemberByName:${settings.getMemberByName}`);
  35. //CollectClasses(stripeMemeber, stripeClass, fguiNamespace)
  36. let classes = handler.CollectClasses(settings.ignoreNoname, settings.ignoreNoname, null);
  37. // handler.SetupCodeFolder(exportCodePath, "cs"); //check if target folder exists, and delete old files
  38. //创建Package代码文件夹
  39. if (!csharp_1.System.IO.Directory.Exists(exportCodePath)) {
  40. csharp_1.System.IO.Directory.CreateDirectory(exportCodePath);
  41. }
  42. let classCnt = classes.Count;
  43. let writer = new CodeWriter_1.default();
  44. // 一个个导出类导出
  45. for (let i = 0; i < classCnt; i++) {
  46. let classInfo = classes.get_Item(i);
  47. if (!checkPackageItemIsGenCode(handler, classInfo.res)) {
  48. // console.warn("不需要导出:" + classInfo.resName)
  49. continue;
  50. }
  51. else {
  52. console.log("导出:" + classInfo.resName);
  53. }
  54. logClassInfo(classInfo);
  55. let resName = myCapitalize(classInfo.resName);
  56. let uiName = codePkgName + resName;
  57. let uiCodePath = `${exportCodePath}/${resName}`;
  58. //创建UI窗口的代码文件夹
  59. if (!csharp_1.System.IO.Directory.Exists(uiCodePath)) {
  60. csharp_1.System.IO.Directory.CreateDirectory(uiCodePath);
  61. }
  62. // if (classInfo.res.exported) {
  63. // 有设置导出属性的才生成View和Ctrl代码
  64. let uitype = getUIType(handler, classInfo.res);
  65. let templateCodePath = uitype == 0 ? `${codeTemplatesPath}/TemplatePanel.cs` : `${codeTemplatesPath}/TemplateNested.cs`;
  66. genUIClassOnce(uiCodePath, templateCodePath, codePkgName, resName, uiName, viewSuffix, (uiClassText) => {
  67. if (uitype == 0) {
  68. let uiLayer = getUILayer(handler, classInfo.res);
  69. let layerDefineStr = "";
  70. if (uiLayer != uiLayerEnum[1]) {
  71. // UILayer.Normal 不定义该变量
  72. layerDefineStr = `public static UILayer Layer => UILayer.${uiLayer};`;
  73. }
  74. uiClassText = uiClassText.replaceAll("<#UILayerDefineArea>", layerDefineStr);
  75. }
  76. return uiClassText;
  77. });
  78. if (uitype != 0) {
  79. // NestedView的ListItem扩展
  80. genListItemCode(uiCodePath, `${codeTemplatesPath}/TemplateNested.ListItem.cs`, uiName, uitype == 2);
  81. }
  82. // }
  83. // VM有需要的业务自己创建
  84. // genUIClassOnce(uiCodePath, codeTemplatesPath, codePkgName, resName, uiName, vmSuffix);
  85. // 需要绑定的组件变量
  86. let genClassTypeMap = new Map();
  87. // 需要多语言转义的变量
  88. let langTypeMap = new Map();
  89. // TODO 该信息后续看是否需要生成脚本
  90. let otherPkgName = new Array();
  91. // 提前先获取一次生成组件变量存在引用的生成代码或者跨包的生成代码类.
  92. for (let index = 0; index < classInfo.members?.Count; index++) {
  93. const element = classInfo.members.get_Item(index);
  94. console.log("element.name:" + element.name + " varName:" + element.varName + " type:" + element.type);
  95. if (element.res) {
  96. let desc = handler.GetItemDesc(element.res);
  97. let superClassName = desc?.GetAttribute("extention");
  98. //fgui的组件.xml文件,如果是普通组件,则没有extention属性,否则extention会记录具体的组件类型
  99. // 是一个自定义组件
  100. console.log("element.res name:" + element.res.name + " owner.name:" + element.res.owner.name + " type:" + element.res.type
  101. + " super:" + superClassName);
  102. // 如果是当前的包那么直接发布
  103. // 如果是其他包。那么需要判断这个是否需要生成代码的,否则也是直接当成是component组件引用
  104. if (element.res.owner.name == handler.pkg.name) {
  105. // 判断是否需要导出生成代码类否则直接生成扩展类吧
  106. if (checkPackageItemIsGenCode(handler, element.res)) { //&& element.res.exported
  107. let className = `${codePkgName}${myCapitalize(element.res.name)}${viewSuffix}`;
  108. if (otherPkgName.indexOf(className) == -1) {
  109. otherPkgName.push(className);
  110. }
  111. }
  112. else {
  113. // 生成绑定ext类
  114. }
  115. }
  116. else {
  117. // 不在同一个包,需要读取目标路径的配置xml判断是否需要生成
  118. if (checkOtherPackageItemIsGenCode(element.res)) {
  119. // console.log("引用其他包的资源可以生成代码:" + element.res.owner.name + "." + element.res.name)
  120. let className = `${myCapitalize(element.res.owner.name)}${myCapitalize(element.res.name)}${viewSuffix}`;
  121. if (otherPkgName.indexOf(className) == -1) {
  122. otherPkgName.push(className);
  123. // writer.writeln('import { %s } from "../%s/%s";', className + "_" + element.res.owner.name, element.res.owner.name, className + "_" + element.res.owner.name)
  124. }
  125. }
  126. }
  127. }
  128. }
  129. // 组件变量
  130. for (let index = 0; index < classInfo.members?.Count; index++) {
  131. const element = classInfo.members.get_Item(index);
  132. if (element.res) {
  133. // 如果是当前的包那么直接发布
  134. // 如果是其他包。那么需要判断这个是否需要生成代码的,否则也是直接当成是component组件引用
  135. if (element.res.owner.name == handler.pkg.name) {
  136. // 判断是否需要导出生成代码类否则直接生成扩展类吧
  137. // 只有当这个组件资源里面勾选了生成代码 + exported才生成代码
  138. if (checkPackageItemIsGenCode(handler, element.res)) { // && element.res.exported
  139. // 有生成代码标记
  140. let gci = new GenClassInfo(element);
  141. gci.type = `${codePkgName}${myCapitalize(element.res.name)}${viewSuffix}`;
  142. gci.isCustomType = true;
  143. genClassTypeMap.set(element.varName, gci);
  144. }
  145. // 通过判断在该组件是否勾选了导出,那么也需要生成一下ext类
  146. else {
  147. let desc = handler.GetItemDesc(element.res);
  148. let superClassName = desc?.GetAttribute("extention");
  149. console.log(`GetItemDesc name:${element.res.name} exported:${element.res.exported} ext:${superClassName}`);
  150. if (!superClassName) {
  151. // // null = GComponent
  152. // writer.writeln('public FairyGUI.GComponent %s;', element.varName)
  153. let gci = new GenClassInfo(element);
  154. gci.type = "GComponent";
  155. genClassTypeMap.set(element.varName, gci);
  156. // 如果有title的可以?
  157. if (checkPackageItemIsLang(handler, classInfo.res, element.name)) {
  158. gci = new GenClassInfo(element);
  159. gci.type = superClassName;
  160. langTypeMap.set(element.varName, gci);
  161. }
  162. }
  163. else {
  164. // writer.writeln('public FairyGUI.G%s %s;',superClassName, element.varName)
  165. let gci = new GenClassInfo(element);
  166. gci.type = "G" + superClassName;
  167. genClassTypeMap.set(element.varName, gci);
  168. if (checkPackageItemIsLang(handler, classInfo.res, element.name)) {
  169. langTypeMap.set(element.varName, gci);
  170. }
  171. }
  172. }
  173. }
  174. else {
  175. // 不在同一个包,需要读取目标路径的配置xml判断是否需要生成
  176. if (checkOtherPackageItemIsGenCode(element.res)) {
  177. // // console.log("引用其他包的资源可以生成代码:" + element.res.owner.name + "." + element.res.name)
  178. let gci = new GenClassInfo(element);
  179. gci.type = `${myCapitalize(element.res.owner.name)}${myCapitalize(element.res.name)}${viewSuffix}`;
  180. gci.isCustomType = true;
  181. gci.otherPkgName = myCapitalize(element.res.owner.name);
  182. genClassTypeMap.set(element.varName, gci);
  183. }
  184. else {
  185. let superClassName = checkOtherPackageItemExtension(element.res);
  186. if (superClassName) {
  187. // writer.writeln('public FairyGUI.G%s %s;',superClassName, element.varName)
  188. let gci = new GenClassInfo(element);
  189. gci.type = "G" + superClassName;
  190. genClassTypeMap.set(element.varName, gci);
  191. if (checkPackageItemIsLang(handler, classInfo.res, element.name)) {
  192. langTypeMap.set(element.varName, gci);
  193. }
  194. }
  195. else {
  196. let gci = new GenClassInfo(element);
  197. gci.type = "GComponent";
  198. genClassTypeMap.set(element.varName, gci);
  199. if (checkPackageItemIsLang(handler, classInfo.res, element.name)) {
  200. langTypeMap.set(element.varName, gci);
  201. }
  202. }
  203. }
  204. }
  205. }
  206. else {
  207. // fgui 默认的组件变量即可
  208. // writer.writeln('public FairyGUI.%s %s;',element.type, element.varName)
  209. let gci = new GenClassInfo(element);
  210. if (element.type == "GList") {
  211. gci.glistItemView = getGListItemView(classInfo.res, element.name);
  212. }
  213. genClassTypeMap.set(element.varName, gci);
  214. if (checkPackageItemIsLang(handler, classInfo.res, element.name)) {
  215. langTypeMap.set(element.varName, new GenClassInfo(element));
  216. }
  217. }
  218. }
  219. // if (classInfo.res.exported) {
  220. // 有设置导出属性的才生成Ctrl代码
  221. genUIClassOnce(uiCodePath, `${codeTemplatesPath}/TemplateCtrl.cs`, codePkgName, resName, uiName, "Ctrl", (uiClassText) => {
  222. if (genClassTypeMap != null) {
  223. uiClassText = genButtonEvents(uiClassText, genClassTypeMap);
  224. }
  225. return uiClassText;
  226. });
  227. // }
  228. genViewModelCode(uiCodePath, codeTemplatesPath, codePkgName, resName, uiName, genClassTypeMap);
  229. }
  230. // 写一个binder类可以直接获取这个package导出的信息
  231. // 如果有些组件需要绑定的也可以绑定这个组件类型
  232. // let binderName = "GenMain_" + codePkgName;
  233. // writer.writeln('export class %s', binderName)
  234. // writer.startBlock()
  235. // writer.writeln()
  236. // writer.writeln('public static PackageName:string = "%s";', handler.pkg.name)
  237. // writer.writeln()
  238. // // 把这个包导出的类记录一下引用的类名
  239. // // bindall扩展类绑定
  240. // writer.writeln('public static bindAll()');
  241. // writer.startBlock();
  242. // // for (let i: number = 0; i < classCnt; i++) {
  243. // // let classInfo = classes.get_Item(i);
  244. // // writer.writeln('UIObjectFactory.SetPackageItemExtension(%s.URL, typeof(%s));', classInfo.className, classInfo.className);
  245. // // }
  246. // writer.endBlock(); //bindall
  247. // writer.endBlock()
  248. // writer.save(exportCodePath + '/' + binderName + '.ts');
  249. writer.reset();
  250. // 把该包多语言的kv保存下来
  251. if (exportLangKV.size > 0) {
  252. exportLangKV.forEach((v, k) => {
  253. writer.writeln("%s %s", k, v);
  254. });
  255. writer.save(exportCodePath + '/' + "lang_" + codePkgName + '.txt');
  256. }
  257. writer.reset();
  258. }
  259. exports.genCodeCS = genCodeCS;
  260. /**
  261. * 创建UI代码,已经存在则跳过
  262. * 不重复创建
  263. * classSuffix 类名后缀:View、ViewCtrl、ViewModel等
  264. */
  265. function genUIClassOnce(uiCodePath, templateCodePath, pkgName, panelName, uiName, classSuffix, customText) {
  266. let uiClassName = uiName + classSuffix;
  267. let uiClassPath = `${uiCodePath}/${uiClassName}.cs`;
  268. if (csharp_1.System.IO.File.Exists(uiClassPath)) {
  269. console.log(`${uiClassName}已经存在,不重复创建`);
  270. // System.IO.File.Delete(uiClassPath);
  271. return;
  272. }
  273. let uiClassText = csharp_1.System.IO.File.ReadAllText(templateCodePath);
  274. uiClassText = uiClassText.replaceAll("<#pkgName>", pkgName);
  275. uiClassText = uiClassText.replaceAll("<#panelName>", panelName);
  276. uiClassText = uiClassText.replaceAll("<#UIName>", uiName);
  277. // uiClassText = uiClassText.replaceAll("<#UILayer>", uiName);
  278. if (customText != null) {
  279. uiClassText = customText(uiClassText);
  280. }
  281. csharp_1.System.IO.File.WriteAllText(uiClassPath, uiClassText);
  282. }
  283. /**生成GButton的onClick注册代码*/
  284. function genButtonEvents(uiClassText, genClassTypeMap) {
  285. let uiEventsAdd = "";
  286. let uiEventsRemove = "";
  287. let uiEventsDefine = "";
  288. genClassTypeMap.forEach((v, k) => {
  289. if (v.type == "GButton") {
  290. let btnName = myCapitalize(v.name);
  291. let functionName = `OnClick${btnName}`;
  292. uiEventsAdd += ` VM.${btnName}.onClick.Add(${functionName});\n`;
  293. uiEventsRemove += ` VM.${btnName}.onClick.Remove(${functionName});\n`;
  294. uiEventsDefine += ` private void ${functionName}(EventContext context)
  295. {
  296. }\n`;
  297. }
  298. });
  299. uiClassText = uiClassText.replace("<#UIEventsAddArea>", uiEventsAdd);
  300. uiClassText = uiClassText.replace("<#UIEventsRemoveArea>", uiEventsRemove);
  301. uiClassText = uiClassText.replace("<#UIEventsDefineArea>", uiEventsDefine);
  302. return uiClassText;
  303. }
  304. /**
  305. * 生成或者删除 NestedView的ListItem扩展代码
  306. */
  307. function genListItemCode(uiCodePath, templateCodePath, uiName, isListItem) {
  308. let uiClassName = uiName + viewSuffix;
  309. let uiClassPath = `${uiCodePath}/${uiClassName}.ListItem.cs`;
  310. if (csharp_1.System.IO.File.Exists(uiClassPath)) {
  311. if (!isListItem) {
  312. //UINested
  313. csharp_1.System.IO.File.Delete(uiClassPath);
  314. return;
  315. }
  316. console.log(`${uiClassName}已经存在,不重复创建`);
  317. // System.IO.File.Delete(uiClassPath);
  318. return;
  319. }
  320. else if (!isListItem) {
  321. return;
  322. }
  323. let uiClassText = csharp_1.System.IO.File.ReadAllText(templateCodePath);
  324. // uiClassText = uiClassText.replaceAll("<#pkgName>", pkgName);
  325. // uiClassText = uiClassText.replaceAll("<#panelName>", panelName);
  326. uiClassText = uiClassText.replaceAll("<#UIName>", uiName);
  327. csharp_1.System.IO.File.WriteAllText(uiClassPath, uiClassText);
  328. }
  329. /**
  330. * 生成ViewModel.Gen代码
  331. * 每次都会重新生成
  332. */
  333. function genViewModelCode(uiCodePath, codeTemplatesPath, pkgName, panelName, uiName, genClassTypeMap) {
  334. let vmClassName = `${uiName}${vmSuffix}.Gen`;
  335. let vmClassPath = `${uiCodePath}/${vmClassName}.cs`;
  336. if (csharp_1.System.IO.File.Exists(vmClassPath)) {
  337. //删除旧文件
  338. csharp_1.System.IO.File.Delete(vmClassPath);
  339. }
  340. let vmClassText = csharp_1.System.IO.File.ReadAllText(`${codeTemplatesPath}/Template${vmSuffix}.Gen.cs`);
  341. vmClassText = vmClassText.replaceAll("<#pkgName>", pkgName);
  342. vmClassText = vmClassText.replaceAll("<#panelName>", panelName);
  343. vmClassText = vmClassText.replaceAll("<#UIName>", uiName);
  344. // 开始绑定组件get
  345. let propertyDefine = "";
  346. let propertyAssign = "";
  347. genClassTypeMap.forEach((v, k) => {
  348. propertyDefine += ` public ${v.type} ${myCapitalize(v.name)} { get; private set; }\n`;
  349. //let eTypeWithoutG = v.type.slice(1,v.type.length); // component组件名字去掉开头的'G'
  350. if (v.isCustomType) {
  351. propertyAssign += ` ${myCapitalize(v.name)} = adapter.CreateNested<${v.type}>(new FguiNested(panel.GetChild("${v.name}").asCom), true);\n`;
  352. propertyAssign += ` AddChild(${myCapitalize(v.name)});\n`;
  353. // propertyAssign += ` ${myCapitalize(v.name)} = new ${v.type}(panel.GetChild("${v.name}").asCom);\n`;
  354. // propertyAssign += ` ${myCapitalize(v.name)} = (panel.GetChild("${v.superName}").asCom).GetChild("${v.name}").as${eTypeWithoutG};\n\r`;
  355. }
  356. else {
  357. if (v.type == "Controller") {
  358. propertyAssign += ` ${myCapitalize(v.name)} = panel.GetController("${v.name}");\n`;
  359. }
  360. else if (v.type == "Transition") {
  361. propertyAssign += ` ${myCapitalize(v.name)} = panel.GetTransition("${v.name}");\n`;
  362. }
  363. else {
  364. propertyAssign += ` ${myCapitalize(v.name)} = panel.GetChild("${v.name}") as ${v.type};\n`;
  365. if (v.glistItemView) {
  366. //是GList,注册ListItemView
  367. propertyAssign += ` ${myCapitalize(v.name)}.Init(typeof(${v.glistItemView}), adapter.CreateListItem);\n`;
  368. }
  369. }
  370. }
  371. });
  372. vmClassText = vmClassText.replace("<#PropertyDefineArea>", propertyDefine);
  373. vmClassText = vmClassText.replace("<#PropertyAssignArea>", propertyAssign);
  374. csharp_1.System.IO.File.WriteAllText(vmClassPath, vmClassText);
  375. }
  376. function getGListItemView(pkgItem, listName) {
  377. let file = csharp_1.System.IO.File.ReadAllText(pkgItem.file);
  378. let xml = new csharp_1.FairyGUI.Utils.XML(file);
  379. let displayListEles = xml?.GetNode("displayList").Elements("list");
  380. for (let index = 0; index < displayListEles?.Count; index++) {
  381. const element = displayListEles.get_Item(index);
  382. if (element.GetAttribute("name") != listName) {
  383. continue;
  384. }
  385. let defaultItemUrl = element.GetAttribute("defaultItem");
  386. if (defaultItemUrl) {
  387. let glistItem = csharp_1.FairyEditor.App.project.GetItemByURL(defaultItemUrl);
  388. if (glistItem) {
  389. return `${myCapitalize(glistItem.owner.name)}${myCapitalize(glistItem.name)}${viewSuffix}`;
  390. }
  391. }
  392. // let glistItemEles = element.Elements("item");
  393. // for(let glistItemIdx = 0; glistItemIdx < glistItemEles?.Count; glistItemIdx++) {
  394. // const glistItemEle = glistItemEles.get_Item(glistItemIdx);
  395. // const itemUrl = glistItemEle.GetAttribute("url");
  396. // let glistItem = FairyEditor.App.project.GetItemByURL(itemUrl);
  397. // if (glistItem) {
  398. // return `${myCapitalize(glistItem.owner.name)}${myCapitalize(glistItem.name)}${viewSuffix}`;
  399. // }
  400. // // console.log(`ListName:${listName} itemIdx:${glistItemIdx} url:${itemUrl} glistItem:${glistItem} owner:${glistItem?.owner} name:${glistItem?.name}`);
  401. // }
  402. }
  403. return "";
  404. }
  405. function checkPackageItemIsLang(handler, pkgItem, refComponentName) {
  406. // console.log("found lang = " + refComponentName)
  407. console.log(`checkPackageItemIsLang pkgItem.file:${pkgItem.file}`);
  408. let file = csharp_1.System.IO.File.ReadAllText(pkgItem.file);
  409. let xml = new csharp_1.FairyGUI.Utils.XML(file);
  410. let sd = xml?.GetNode("scriptData");
  411. if (!sd) {
  412. return false;
  413. }
  414. let eles = xml?.GetNode("displayList")?.Elements();
  415. for (let index = 0; index < eles?.Count; index++) {
  416. const element = eles.get_Item(index);
  417. if (element.GetAttribute("name") == refComponentName) {
  418. // 获取这个组件id
  419. let id = element.GetAttribute("id");
  420. // 然后跑去找这个配置的lang是否是1
  421. if (sd.GetAttribute("lang" + id) == "1") {
  422. let key = handler.pkg.name + "." + pkgItem.name + "." + refComponentName;
  423. // 保存一下这个多语言的key - text
  424. if (element.HasAttribute("text")) {
  425. exportLangKV.set(key, element.GetAttribute("text"));
  426. }
  427. else if (element.HasAttribute("url")) {
  428. exportLangKV.set(key, element.GetAttribute("url"));
  429. }
  430. else if (element.GetNode("Button")?.HasAttribute("title")) {
  431. exportLangKV.set(key, element.GetNode("Button").GetAttribute("title"));
  432. }
  433. else if (element.GetNode("Label")?.HasAttribute("title")) {
  434. exportLangKV.set(key, element.GetNode("Label").GetAttribute("title"));
  435. }
  436. return true;
  437. }
  438. else {
  439. return false;
  440. }
  441. }
  442. }
  443. return false;
  444. }
  445. /**
  446. * 判断包内Item是否有生成代码标记
  447. */
  448. function checkPackageItemIsGenCode(handler, pkgItem) {
  449. let xml = handler.GetScriptData(pkgItem);
  450. let gencode = xml?.GetAttribute("gencode" + pkgItem.id);
  451. if (gencode == "1") {
  452. return true;
  453. }
  454. return false;
  455. }
  456. // // 例如一个组件里面选择了某个丢进来的组件勾选了生成代码,但是这个组件不生成实例化代码就用ext
  457. // function checkComponentIsGenCode(handler: FairyEditor.PublishHandler, pkgItem: FairyEditor.FPackageItem, refComponentId: string) {
  458. // let xml = handler.GetScriptData(pkgItem)
  459. // let gencode = xml?.GetAttribute("gencode" + refComponentId)
  460. // if (gencode == "1") {
  461. // return true
  462. // }
  463. // return false
  464. // }
  465. /**
  466. * 判断其他包内Item是否有生成代码标记
  467. */
  468. function checkOtherPackageItemIsGenCode(pkgItem) {
  469. let file = csharp_1.System.IO.File.ReadAllText(pkgItem.file);
  470. let xml = new csharp_1.FairyGUI.Utils.XML(file);
  471. let gencode = xml?.GetNode("scriptData")?.GetAttribute("gencode" + pkgItem.id);
  472. if (gencode == "1") {
  473. return true;
  474. }
  475. return false;
  476. }
  477. function checkOtherPackageItemExtension(pkgItem) {
  478. let file = csharp_1.System.IO.File.ReadAllText(pkgItem.file);
  479. let xml = new csharp_1.FairyGUI.Utils.XML(file);
  480. return xml?.GetAttribute("extention");
  481. }
  482. /**
  483. * 获取UI类型
  484. * 0:UIPanel
  485. * 1:UINested
  486. * 2: ListItem
  487. */
  488. function getUIType(handler, pkgItem) {
  489. let xml = handler.GetScriptData(pkgItem);
  490. let genuitype = xml?.GetAttributeInt("genuitype" + pkgItem.id);
  491. return genuitype;
  492. }
  493. /**
  494. * 获取UI的UILayer
  495. * 默认为Normal
  496. */
  497. function getUILayer(handler, pkgItem) {
  498. let xml = handler.GetScriptData(pkgItem);
  499. let genuilayer = xml?.GetAttribute("genuilayer" + pkgItem.id);
  500. if (genuilayer == null || genuilayer == "")
  501. genuilayer = "1"; // 默认为Normal
  502. csharp_1.FairyEditor.App.consoleView.Log(`getUILayer Item:${pkgItem.name} genuilayer:${genuilayer}`);
  503. return uiLayerEnum[Number(genuilayer)];
  504. }
  505. function logClassInfo(ci) {
  506. csharp_1.FairyEditor.App.consoleView.Log(ci.superClassName + "." + ci.className + " resName=" + ci.resName + " resId=" + ci.resId);
  507. // for (let index = 0; index < ci.members?.Count; index++) {
  508. // const element = ci.members.get_Item(index);
  509. // console.log(" > " + element.name + " -v:" + element.varName + " -t:" + element.type )
  510. // if(element.res) {
  511. // console.log(" : res=" + element.res.name + " 包名:" + element.res.owner.name)
  512. // }
  513. // }
  514. for (let index = 0; index < ci.references?.Count; index++) {
  515. const element = ci.references.get_Item(index);
  516. console.log(" >> ref:" + element);
  517. }
  518. }
  519. /**
  520. * 字符串首字母转大写
  521. */
  522. function myCapitalize(str) {
  523. return str[0].toUpperCase() + str.slice(1, str.length);
  524. }