GenCode_CSharp.ts 26 KB

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