BaseTemplate.cs 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. using System;
  2. using System.Collections.Generic;
  3. using UnityEditor;
  4. using UnityEngine;
  5. namespace ET.PackageManager.Editor
  6. {
  7. public struct CreateVo
  8. {
  9. /// <summary>
  10. /// 只能是.txt 的模板
  11. /// 重写文件不需要模板
  12. /// </summary>
  13. public string TemplatePath;
  14. /// <summary>
  15. /// 文件保存的位置
  16. /// </summary>
  17. public string SavePath;
  18. public CreateVo(string templatePath, string savePath)
  19. {
  20. TemplatePath = templatePath;
  21. SavePath = savePath;
  22. }
  23. }
  24. public abstract class BaseTemplate
  25. {
  26. /// <summary>
  27. /// 这个模板动作的名称
  28. /// 作用于提示信息
  29. /// </summary>
  30. public virtual string EventName => "";
  31. protected CreateVo CreateVo;
  32. //常规新文件用字典
  33. protected Dictionary<string, string> ValueDic;
  34. public BaseTemplate(string authorName)
  35. {
  36. var dt = DateTime.Now;
  37. ValueDic = new Dictionary<string, string>();
  38. ValueDic["Author"] = authorName;
  39. ValueDic["CreateDate"] = $"{dt.Year}.{dt.Month}.{dt.Day}";
  40. }
  41. /// <summary>
  42. /// 是否覆盖 如果存在的情况下
  43. /// 写入新文件时可用
  44. /// </summary>
  45. public virtual bool Cover => true;
  46. /// <summary>
  47. /// 其他是否保留
  48. /// 重写文件时可用
  49. /// </summary>
  50. public virtual bool OtherRetain => true;
  51. /// <summary>
  52. /// 如果值已存在是否跳过
  53. /// 重写文件时可用
  54. /// </summary>
  55. public virtual bool ExistSkip => true;
  56. /// <summary>
  57. /// 完成后自动刷新
  58. /// </summary>
  59. public virtual bool AutoRefresh => true;
  60. /// <summary>
  61. /// 提示
  62. /// </summary>
  63. public virtual bool ShowTips => true;
  64. /// <summary>
  65. /// 根据模板创建一个新文件
  66. /// </summary>
  67. /// <returns></returns>
  68. protected bool CreateNewFile()
  69. {
  70. if (TemplateEngine.FileExists(CreateVo.SavePath))
  71. {
  72. if (!Cover)
  73. {
  74. if (ShowTips)
  75. {
  76. var code = $"文件已存在 当前选择已存在不覆盖 {CreateVo.SavePath}";
  77. UnityTipsHelper.ShowWarning(code);
  78. }
  79. return false;
  80. }
  81. }
  82. if (!TemplateEngine.CreateCodeFile(CreateVo.SavePath, CreateVo.TemplatePath, ValueDic))
  83. {
  84. UnityTipsHelper.Show(CreateVo.SavePath + "创建失败");
  85. TemplateEngine.TemplateBasePath = null;
  86. return false;
  87. }
  88. if (ShowTips)
  89. {
  90. UnityTipsHelper.Show($"{EventName} 处理完毕");
  91. }
  92. if (AutoRefresh)
  93. {
  94. AssetDatabase.Refresh();
  95. }
  96. return true;
  97. }
  98. /// <summary>
  99. /// 重写文件
  100. /// 指定区域内的都会被重写 不存在检查 是否重复的情况
  101. /// 这种使用一般是这块区域就是固定的区域不允许玩家乱写的
  102. /// </summary>
  103. /// <returns></returns>
  104. protected bool OverrideCodeFile()
  105. {
  106. if (!TemplateEngine.FileExists(CreateVo.SavePath))
  107. {
  108. if (ShowTips)
  109. {
  110. var code = $"文件不存在 无法重写 {CreateVo.SavePath}";
  111. UnityTipsHelper.ShowError(code);
  112. }
  113. return false;
  114. }
  115. if (!TemplateEngine.OverrideCodeFile(CreateVo.SavePath, ValueDic, OtherRetain))
  116. {
  117. UnityTipsHelper.ShowError(CreateVo.SavePath + "创建失败");
  118. TemplateEngine.TemplateBasePath = null;
  119. return false;
  120. }
  121. if (ShowTips)
  122. {
  123. UnityTipsHelper.Show($"{EventName} 处理完毕");
  124. }
  125. if (AutoRefresh)
  126. {
  127. AssetDatabase.Refresh();
  128. }
  129. return true;
  130. }
  131. /// <summary>
  132. /// 区域内重写文件
  133. /// 但是会检查 如果我要写的东西已经存在了就不写了
  134. /// 如: 我要提供一个方法给玩家 如果这个方法已经存在了就不检查了 否则我就会创建一个新的方法
  135. /// 使用此方法需要吧上面的 OverrideDic 赋值
  136. /// </summary>
  137. /// <param name="overrideDic">数据</param>
  138. /// <param name="cover">是否覆盖 则不需要检查是否有直接全部覆盖</param>
  139. /// <returns></returns>
  140. protected bool OverrideCheckCodeFile(Dictionary<string, List<Dictionary<string, string>>> overrideDic, bool cover = false)
  141. {
  142. if (!TemplateEngine.FileExists(CreateVo.SavePath))
  143. {
  144. if (ShowTips)
  145. {
  146. var code = $"文件不存在 无法重写 {CreateVo.SavePath}";
  147. UnityTipsHelper.ShowError(code);
  148. }
  149. return false;
  150. }
  151. if (overrideDic == null)
  152. {
  153. Debug.LogError("替换数据无值 无法操作 OverrideDic == null");
  154. return false;
  155. }
  156. if (overrideDic.Count >= 1)
  157. {
  158. if (!TemplateEngine.OverrideCheckCodeFile(CreateVo.SavePath, overrideDic, cover))
  159. {
  160. UnityTipsHelper.ShowError(CreateVo.SavePath + "创建失败");
  161. TemplateEngine.TemplateBasePath = null;
  162. return false;
  163. }
  164. }
  165. if (ShowTips)
  166. {
  167. UnityTipsHelper.Show($"{EventName} 处理完毕");
  168. }
  169. if (AutoRefresh)
  170. {
  171. AssetDatabase.Refresh();
  172. }
  173. return true;
  174. }
  175. }
  176. /// <summary>
  177. /// 这是一个案例 只需要new这个类 即可进行一系列模板操作
  178. /// 创建一个新文件时
  179. /// </summary>
  180. public class TestTemplate : BaseTemplate
  181. {
  182. public override string EventName => "测试案例一 创建新文件";
  183. public override bool Cover => false;
  184. public TestTemplate(string authorName, string moduleName, string pkgName, string resName) : base(authorName)
  185. {
  186. CreateVo = new CreateVo("Assets/.../Template/BasePanelTemplate.txt",
  187. $"Assets/../{moduleName}/UI/{resName}.cs");
  188. ValueDic["moduleName"] = moduleName;
  189. ValueDic["uiPkgName"] = pkgName;
  190. ValueDic["uiResName"] = resName;
  191. CreateNewFile();
  192. }
  193. }
  194. /// <summary>
  195. /// 这是一个案例 只需要new这个类 即可进行一系列模板操作
  196. /// 重写文件内容
  197. /// </summary>
  198. public class TestTemplate2 : BaseTemplate
  199. {
  200. public override string EventName => "测试案例二 重写文件内容";
  201. //public override bool OtherRetain => false;
  202. public TestTemplate2(string authorName, string moduleName, string pkgName, string resName) : base(authorName)
  203. {
  204. CreateVo = new CreateVo("Assets/.../Template/BasePanelTemplate.txt",
  205. $"Assets/../{moduleName}/UI/{resName}.cs");
  206. ValueDic["moduleName"] = moduleName;
  207. ValueDic["uiPkgName"] = pkgName;
  208. ValueDic["uiResName"] = resName;
  209. ValueDic["AA"] = "//我替换的东西"; //的相当于 标记为AA 这个范围内的所有东西都会被清空 替换我指定的内容
  210. OverrideCodeFile();
  211. }
  212. }
  213. }