GenCode_CSharp.js 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  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. customType;
  11. otherPkgName;
  12. constructor(member) {
  13. this.name = member.name;
  14. this.varName = member.varName;
  15. this.type = member.type;
  16. this.customType = false;
  17. }
  18. }
  19. let exportLangKV = new Map();
  20. /**UI的vm类名后缀*/
  21. let vmSuffix = "VM";
  22. function genCodeCS(handler) {
  23. let settings = 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 (!csharp_1.System.IO.Directory.Exists(exportCodePath)) {
  37. csharp_1.System.IO.Directory.CreateDirectory(exportCodePath);
  38. }
  39. let classCnt = classes.Count;
  40. let writer = new CodeWriter_1.default();
  41. // 一个个导出类导出
  42. for (let i = 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. }
  48. else {
  49. console.log("导出:" + classInfo.resName);
  50. }
  51. logClassInfo(classInfo);
  52. let resName = myCapitalize(classInfo.resName);
  53. let uiName = codePkgName + resName;
  54. let uiCodePath = `${exportCodePath}/${resName}`;
  55. //创建UI窗口的代码文件夹
  56. if (!csharp_1.System.IO.Directory.Exists(uiCodePath)) {
  57. csharp_1.System.IO.Directory.CreateDirectory(uiCodePath);
  58. }
  59. if (classInfo.res.exported) {
  60. // 有设置导出属性的才生成View和Ctrl代码
  61. genUIClassOnce(uiCodePath, codeTemplatesPath, codePkgName, resName, uiName, "View");
  62. }
  63. genUIClassOnce(uiCodePath, codeTemplatesPath, codePkgName, resName, uiName, vmSuffix);
  64. // 需要绑定的组件变量
  65. let genClassTypeMap = new Map();
  66. // 需要多语言转义的变量
  67. let langTypeMap = new Map();
  68. // TODO 该信息后续看是否需要生成脚本
  69. let otherPkgName = new Array();
  70. // 提前先获取一次生成组件变量存在引用的生成代码或者跨包的生成代码类.
  71. for (let index = 0; index < classInfo.members?.Count; index++) {
  72. const element = classInfo.members.get_Item(index);
  73. console.log("element.name:" + element.name + " varName:" + element.varName + " type:" + element.type);
  74. if (element.res) {
  75. let desc = handler.GetItemDesc(element.res);
  76. let superClassName = desc?.GetAttribute("extention");
  77. //fgui的组件.xml文件,如果是普通组件,则没有extention属性,否则extention会记录具体的组件类型
  78. // 是一个自定义组件
  79. console.log("element.res name:" + element.res.name + " owner.name:" + element.res.owner.name + " type:" + element.res.type
  80. + " super:" + superClassName);
  81. // 如果是当前的包那么直接发布
  82. // 如果是其他包。那么需要判断这个是否需要生成代码的,否则也是直接当成是component组件引用
  83. if (element.res.owner.name == handler.pkg.name) {
  84. // 判断是否需要导出生成代码类否则直接生成扩展类吧
  85. if (checkPackageItemIsGenCode(handler, element.res)) { //&& element.res.exported
  86. let className = `${codePkgName}${myCapitalize(element.res.name)}${vmSuffix}`;
  87. if (otherPkgName.indexOf(className) == -1) {
  88. otherPkgName.push(className);
  89. }
  90. }
  91. else {
  92. // 生成绑定ext类
  93. }
  94. }
  95. else {
  96. // 不在同一个包,需要读取目标路径的配置xml判断是否需要生成
  97. if (checkOtherPackageItemIsGenCode(element.res)) {
  98. // console.log("引用其他包的资源可以生成代码:" + element.res.owner.name + "." + element.res.name)
  99. let className = `${myCapitalize(element.res.owner.name)}${myCapitalize(element.res.name)}${vmSuffix}`;
  100. if (otherPkgName.indexOf(className) == -1) {
  101. otherPkgName.push(className);
  102. // writer.writeln('import { %s } from "../%s/%s";', className + "_" + element.res.owner.name, element.res.owner.name, className + "_" + element.res.owner.name)
  103. }
  104. }
  105. }
  106. }
  107. }
  108. // 组件变量
  109. for (let index = 0; index < classInfo.members?.Count; index++) {
  110. const element = classInfo.members.get_Item(index);
  111. if (element.res) {
  112. // 如果是当前的包那么直接发布
  113. // 如果是其他包。那么需要判断这个是否需要生成代码的,否则也是直接当成是component组件引用
  114. if (element.res.owner.name == handler.pkg.name) {
  115. // 判断是否需要导出生成代码类否则直接生成扩展类吧
  116. // 只有当这个组件资源里面勾选了生成代码 + exported才生成代码
  117. if (checkPackageItemIsGenCode(handler, element.res)) { // && element.res.exported
  118. // 有生成代码标记
  119. let gci = new GenClassInfo(element);
  120. gci.type = `${codePkgName}${myCapitalize(element.res.name)}${vmSuffix}`;
  121. gci.customType = true;
  122. genClassTypeMap.set(element.varName, gci);
  123. }
  124. // 通过判断在该组件是否勾选了导出,那么也需要生成一下ext类
  125. else {
  126. let desc = handler.GetItemDesc(element.res);
  127. let superClassName = desc?.GetAttribute("extention");
  128. console.log(`GetItemDesc name:${element.res.name} exported:${element.res.exported} ext:${superClassName}`);
  129. if (!superClassName) {
  130. // // null = GComponent
  131. // writer.writeln('public FairyGUI.GComponent %s;', element.varName)
  132. let gci = new GenClassInfo(element);
  133. gci.type = "GComponent";
  134. genClassTypeMap.set(element.varName, gci);
  135. // 如果有title的可以?
  136. if (checkPackageItemIsLang(handler, classInfo.res, element.name)) {
  137. gci = new GenClassInfo(element);
  138. gci.type = superClassName;
  139. langTypeMap.set(element.varName, gci);
  140. }
  141. }
  142. else {
  143. // writer.writeln('public FairyGUI.G%s %s;',superClassName, element.varName)
  144. let gci = new GenClassInfo(element);
  145. gci.type = "G" + superClassName;
  146. genClassTypeMap.set(element.varName, gci);
  147. if (checkPackageItemIsLang(handler, classInfo.res, element.name)) {
  148. langTypeMap.set(element.varName, gci);
  149. }
  150. }
  151. }
  152. }
  153. else {
  154. // 不在同一个包,需要读取目标路径的配置xml判断是否需要生成
  155. if (checkOtherPackageItemIsGenCode(element.res)) {
  156. // // console.log("引用其他包的资源可以生成代码:" + element.res.owner.name + "." + element.res.name)
  157. let gci = new GenClassInfo(element);
  158. gci.type = `${myCapitalize(element.res.owner.name)}${myCapitalize(element.res.name)}${vmSuffix}`;
  159. gci.customType = true;
  160. gci.otherPkgName = myCapitalize(element.res.owner.name);
  161. genClassTypeMap.set(element.varName, gci);
  162. }
  163. else {
  164. let superClassName = checkOtherPackageItemExtension(element.res);
  165. if (superClassName) {
  166. // writer.writeln('public FairyGUI.G%s %s;',superClassName, element.varName)
  167. let gci = new GenClassInfo(element);
  168. gci.type = "G" + superClassName;
  169. genClassTypeMap.set(element.varName, gci);
  170. if (checkPackageItemIsLang(handler, classInfo.res, element.name)) {
  171. langTypeMap.set(element.varName, gci);
  172. }
  173. }
  174. else {
  175. let gci = new GenClassInfo(element);
  176. gci.type = "GComponent";
  177. genClassTypeMap.set(element.varName, gci);
  178. if (checkPackageItemIsLang(handler, classInfo.res, element.name)) {
  179. langTypeMap.set(element.varName, gci);
  180. }
  181. }
  182. }
  183. }
  184. }
  185. else {
  186. // fgui 默认的组件变量即可
  187. // writer.writeln('public FairyGUI.%s %s;',element.type, element.varName)
  188. genClassTypeMap.set(element.varName, new GenClassInfo(element));
  189. if (checkPackageItemIsLang(handler, classInfo.res, element.name)) {
  190. langTypeMap.set(element.varName, new GenClassInfo(element));
  191. }
  192. }
  193. }
  194. if (classInfo.res.exported) {
  195. // 有设置导出属性的才生成Ctrl代码
  196. genUIClassOnce(uiCodePath, codeTemplatesPath, codePkgName, resName, uiName, "Ctrl", genClassTypeMap);
  197. }
  198. genViewModelClass(uiCodePath, codeTemplatesPath, codePkgName, resName, uiName, genClassTypeMap);
  199. }
  200. // 写一个binder类可以直接获取这个package导出的信息
  201. // 如果有些组件需要绑定的也可以绑定这个组件类型
  202. // let binderName = "GenMain_" + codePkgName;
  203. // writer.writeln('export class %s', binderName)
  204. // writer.startBlock()
  205. // writer.writeln()
  206. // writer.writeln('public static PackageName:string = "%s";', handler.pkg.name)
  207. // writer.writeln()
  208. // // 把这个包导出的类记录一下引用的类名
  209. // // bindall扩展类绑定
  210. // writer.writeln('public static bindAll()');
  211. // writer.startBlock();
  212. // // for (let i: number = 0; i < classCnt; i++) {
  213. // // let classInfo = classes.get_Item(i);
  214. // // writer.writeln('UIObjectFactory.SetPackageItemExtension(%s.URL, typeof(%s));', classInfo.className, classInfo.className);
  215. // // }
  216. // writer.endBlock(); //bindall
  217. // writer.endBlock()
  218. // writer.save(exportCodePath + '/' + binderName + '.ts');
  219. writer.reset();
  220. // 把该包多语言的kv保存下来
  221. if (exportLangKV.size > 0) {
  222. exportLangKV.forEach((v, k) => {
  223. writer.writeln("%s %s", k, v);
  224. });
  225. writer.save(exportCodePath + '/' + "lang_" + codePkgName + '.txt');
  226. }
  227. writer.reset();
  228. }
  229. exports.genCodeCS = genCodeCS;
  230. /**
  231. * 创建UI代码,已经存在则跳过
  232. * 不重复创建
  233. * classSuffix 类名后缀:View、ViewCtrl、ViewModel等
  234. */
  235. function genUIClassOnce(uiCodePath, codeTemplatesPath, pkgName, panelName, uiName, classSuffix, genClassTypeMap = null) {
  236. let uiClassName = uiName + classSuffix;
  237. let uiClassPath = `${uiCodePath}/${uiClassName}.cs`;
  238. if (csharp_1.System.IO.File.Exists(uiClassPath)) {
  239. console.log(`${uiClassName}已经存在,不重复创建`);
  240. // System.IO.File.Delete(uiClassPath);
  241. return;
  242. }
  243. let uiClassText = csharp_1.System.IO.File.ReadAllText(`${codeTemplatesPath}/Template${classSuffix}.cs`);
  244. uiClassText = uiClassText.replaceAll("<#pkgName>", pkgName);
  245. uiClassText = uiClassText.replaceAll("<#panelName>", panelName);
  246. uiClassText = uiClassText.replaceAll("<#UIName>", uiName);
  247. if (genClassTypeMap != null) {
  248. uiClassText = genButtonEvents(uiClassText, genClassTypeMap);
  249. }
  250. csharp_1.System.IO.File.WriteAllText(uiClassPath, uiClassText);
  251. }
  252. /**生成GButton的onClick注册代码*/
  253. function genButtonEvents(uiClassText, genClassTypeMap) {
  254. let uiEventsAdd = "";
  255. let uiEventsRemove = "";
  256. let uiEventsDefine = "";
  257. genClassTypeMap.forEach((v, k) => {
  258. if (v.type == "GButton") {
  259. let btnName = myCapitalize(v.name);
  260. let functionName = `OnClick${btnName}`;
  261. uiEventsAdd += ` VM.${btnName}.onClick.Add(${functionName});\n`;
  262. uiEventsRemove += ` VM.${btnName}.onClick.Remove(${functionName});\n`;
  263. uiEventsDefine += ` private void ${functionName}(EventContext context)
  264. {
  265. }\n`;
  266. }
  267. });
  268. uiClassText = uiClassText.replace("<#UIEventsAddArea>", uiEventsAdd);
  269. uiClassText = uiClassText.replace("<#UIEventsRemoveArea>", uiEventsRemove);
  270. uiClassText = uiClassText.replace("<#UIEventsDefineArea>", uiEventsDefine);
  271. return uiClassText;
  272. }
  273. /**
  274. * 生成ViewModel.Gen代码
  275. * 每次都会重新生成
  276. */
  277. function genViewModelClass(uiCodePath, codeTemplatesPath, pkgName, panelName, uiName, genClassTypeMap) {
  278. let vmClassName = `${uiName}${vmSuffix}.Gen`;
  279. let vmClassPath = `${uiCodePath}/${vmClassName}.cs`;
  280. if (csharp_1.System.IO.File.Exists(vmClassPath)) {
  281. //删除旧文件
  282. csharp_1.System.IO.File.Delete(vmClassPath);
  283. }
  284. let vmClassText = csharp_1.System.IO.File.ReadAllText(`${codeTemplatesPath}/Template${vmSuffix}.Gen.cs`);
  285. vmClassText = vmClassText.replaceAll("<#pkgName>", pkgName);
  286. vmClassText = vmClassText.replaceAll("<#panelName>", panelName);
  287. vmClassText = vmClassText.replaceAll("<#UIName>", uiName);
  288. // 开始绑定组件get
  289. let propertyDefine = "";
  290. let propertyAssign = "";
  291. genClassTypeMap.forEach((v, k) => {
  292. propertyDefine += ` public ${v.type} ${myCapitalize(v.name)} { get; private set; }\n`;
  293. let eTypeWithoutG = v.type.slice(1, v.type.length); // component组件名字去掉开头的'G'
  294. if (v.customType) {
  295. propertyAssign += ` ${myCapitalize(v.name)} = new ${v.type}(panel.GetChild("${v.name}").asCom);\n`;
  296. // propertyAssign += ` ${myCapitalize(v.name)} = (panel.GetChild("${v.superName}").asCom).GetChild("${v.name}").as${eTypeWithoutG};\n\r`;
  297. }
  298. else {
  299. if (v.type == "Controller") {
  300. propertyAssign += ` ${myCapitalize(v.name)} = panel.GetController("${v.name}");\n`;
  301. }
  302. else if (v.type == "Transition") {
  303. propertyAssign += ` ${myCapitalize(v.name)} = panel.GetTransition("${v.name}");\n`;
  304. }
  305. else {
  306. propertyAssign += ` ${myCapitalize(v.name)} = panel.GetChild("${v.name}") as ${v.type};\n`;
  307. }
  308. }
  309. });
  310. vmClassText = vmClassText.replace("<#PropertyDefineArea>", propertyDefine);
  311. vmClassText = vmClassText.replace("<#PropertyAssignArea>", propertyAssign);
  312. csharp_1.System.IO.File.WriteAllText(vmClassPath, vmClassText);
  313. }
  314. function genBindClass(handler, pkgItem) {
  315. let writer = new CodeWriter_1.default();
  316. // if (!checkPackageItemIsGenCode(handler, pkgItem)) {
  317. // return
  318. // } else {
  319. // console.log("导出:" + pkgItem.name)
  320. // }
  321. writer.reset();
  322. writer.writeln('import { FairyGUI } from \'csharp\';');
  323. writer.writeln('import { binder } from \'framework/common/NiceDecorator\';');
  324. writer.writeln('import { BaseUI, UILayer } from "../../../framework/ui/BaseUI";');
  325. writer.writeln();
  326. pkgItem = handler.pkg.GetItem(pkgItem.id);
  327. console.log(pkgItem.name + " - " + pkgItem.file);
  328. console.log(pkgItem.GetURL());
  329. let pkg = pkgItem.GetTrunk();
  330. // 提前先获取一次生成组件变量存在引用的生成代码或者跨包的生成代码类.
  331. for (let index = 0; index < pkg.children.Count; index++) {
  332. const child = pkg.children.get_Item(index);
  333. console.log("child : " + child.name);
  334. }
  335. }
  336. function checkPackageItemIsLang(handler, pkgItem, refComponentName) {
  337. // console.log("found lang = " + refComponentName)
  338. console.log(`checkPackageItemIsLang pkgItem.file:${pkgItem.file}`);
  339. let file = csharp_1.System.IO.File.ReadAllText(pkgItem.file);
  340. let xml = new csharp_1.FairyGUI.Utils.XML(file);
  341. let sd = xml?.GetNode("scriptData");
  342. if (!sd) {
  343. return false;
  344. }
  345. let eles = xml?.GetNode("displayList")?.Elements();
  346. for (let index = 0; index < eles?.Count; index++) {
  347. const element = eles.get_Item(index);
  348. if (element.GetAttribute("name") == refComponentName) {
  349. // 获取这个组件id
  350. let id = element.GetAttribute("id");
  351. // 然后跑去找这个配置的lang是否是1
  352. if (sd.GetAttribute("lang" + id) == "1") {
  353. let key = handler.pkg.name + "." + pkgItem.name + "." + refComponentName;
  354. // 保存一下这个多语言的key - text
  355. if (element.HasAttribute("text")) {
  356. exportLangKV.set(key, element.GetAttribute("text"));
  357. }
  358. else if (element.HasAttribute("url")) {
  359. exportLangKV.set(key, element.GetAttribute("url"));
  360. }
  361. else if (element.GetNode("Button")?.HasAttribute("title")) {
  362. exportLangKV.set(key, element.GetNode("Button").GetAttribute("title"));
  363. }
  364. else if (element.GetNode("Label")?.HasAttribute("title")) {
  365. exportLangKV.set(key, element.GetNode("Label").GetAttribute("title"));
  366. }
  367. return true;
  368. }
  369. else {
  370. return false;
  371. }
  372. }
  373. }
  374. return false;
  375. }
  376. /**
  377. * 判断包内Item是否有生成代码标记
  378. */
  379. function checkPackageItemIsGenCode(handler, pkgItem) {
  380. let xml = handler.GetScriptData(pkgItem);
  381. let gencode = xml?.GetAttribute("gencode" + pkgItem.id);
  382. if (gencode == "1") {
  383. return true;
  384. }
  385. return false;
  386. }
  387. // 例如一个组件里面选择了某个丢进来的组件勾选了生成代码,但是这个组件不生成实例化代码就用ext
  388. function checkComponentIsGenCode(handler, pkgItem, refComponentId) {
  389. let xml = handler.GetScriptData(pkgItem);
  390. let gencode = xml?.GetAttribute("gencode" + refComponentId);
  391. if (gencode == "1") {
  392. return true;
  393. }
  394. return false;
  395. }
  396. /**
  397. * 判断其他包内Item是否有生成代码标记
  398. */
  399. function checkOtherPackageItemIsGenCode(pkgItem) {
  400. let file = csharp_1.System.IO.File.ReadAllText(pkgItem.file);
  401. let xml = new csharp_1.FairyGUI.Utils.XML(file);
  402. let gencode = xml?.GetNode("scriptData")?.GetAttribute("gencode" + pkgItem.id);
  403. if (gencode == "1") {
  404. return true;
  405. }
  406. return false;
  407. }
  408. function checkOtherPackageItemExtension(pkgItem) {
  409. let file = csharp_1.System.IO.File.ReadAllText(pkgItem.file);
  410. let xml = new csharp_1.FairyGUI.Utils.XML(file);
  411. return xml?.GetAttribute("extention");
  412. }
  413. function logClassInfo(ci) {
  414. console.log(ci.superClassName + "." + ci.className + " resName=" + ci.resName + " resId=" + ci.resId);
  415. // for (let index = 0; index < ci.members?.Count; index++) {
  416. // const element = ci.members.get_Item(index);
  417. // console.log(" > " + element.name + " -v:" + element.varName + " -t:" + element.type )
  418. // if(element.res) {
  419. // console.log(" : res=" + element.res.name + " 包名:" + element.res.owner.name)
  420. // }
  421. // }
  422. for (let index = 0; index < ci.references?.Count; index++) {
  423. const element = ci.references.get_Item(index);
  424. console.log(" >> ref:" + element);
  425. }
  426. }
  427. /**
  428. * 字符串首字母转大写
  429. */
  430. function myCapitalize(str) {
  431. return str[0].toUpperCase() + str.slice(1, str.length);
  432. }