FileUtils.js 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. const puerts_1 = require("puerts");
  4. const FairyEditor = CS.FairyEditor;
  5. const System = CS.System;
  6. const App = FairyEditor.App;
  7. class FileUtils {
  8. /**获取选中目标数量 */
  9. static getSelectedTargetCount() {
  10. let items = App.activeDoc.inspectingTargets;
  11. let index = 0;
  12. try {
  13. while ((items.get_Item(index) != null)) {
  14. index++;
  15. }
  16. }
  17. catch (e) {
  18. return index;
  19. }
  20. return index;
  21. }
  22. /**获取UBB Url文本 */
  23. static getUBBUrl(url, titile) {
  24. return `[url=` + `${url}]${titile}[/url]`;
  25. }
  26. /**UBB file文本 */
  27. static getUBBFile(data) {
  28. let a = `[url=${FileUtils.getFilePath(data)}]`;
  29. let b = this.getAbsPackageItemPath(data);
  30. let c = "[/url]";
  31. return a + b + c;
  32. }
  33. /**获取packageItem的File路径 不带后缀 */
  34. static getFilePath(data) {
  35. return `file://${FileUtils.getAbsPackageItemPath(data)}${FileUtils.getAssetType(data)}`;
  36. }
  37. static getHttpPath(http, title) {
  38. let a = `[url=${http}]`;
  39. let b = title;
  40. let c = "[/url]";
  41. return a + b + c;
  42. }
  43. /**获取packageItem的绝对路径 不带后缀 */
  44. static getAbsPackageItemPath(data) {
  45. return `${App.project.basePath}\\assets\\${data.owner.name}${data.path}${data.name}`;
  46. }
  47. /**获取资源类型的后缀名 */
  48. static getAssetType(asset) {
  49. switch (asset.type) {
  50. case FairyEditor.FPackageItemType.IMAGE:
  51. return ".png";
  52. case FairyEditor.FPackageItemType.ATLAS:
  53. return ".jpg";
  54. case FairyEditor.FPackageItemType.SPINE:
  55. return ".json";
  56. }
  57. }
  58. /**拼接fgui url */
  59. static joinUrl(pkg, res) {
  60. return `ui://${pkg}/${res}`;
  61. }
  62. /**创建泛型List */
  63. static createGenericList(t) {
  64. let List = (0, puerts_1.$generic)(System.Collections.Generic.List$1, t);
  65. let list = new List();
  66. return list;
  67. }
  68. /**创建泛型字典 */
  69. static createGenericDictionary(t, k) {
  70. let Dictionary = (0, puerts_1.$generic)(System.Collections.Generic.Dictionary$2, t, k);
  71. let dictionary = new Dictionary();
  72. return dictionary;
  73. }
  74. //#region IO
  75. /**加载Json文件 */
  76. static loadJson(path) {
  77. if (System.IO.File.Exists(path) == false) {
  78. return null;
  79. }
  80. let f = System.IO.File.ReadAllText(path);
  81. return JSON.parse(f);
  82. }
  83. /**保存Json文件 */
  84. static writerJson(path, msg) {
  85. let config = JSON.stringify(msg);
  86. System.IO.File.WriteAllText(path, config);
  87. }
  88. /**是否清理了依赖 */
  89. static isClearOnPublish(item, map, quertIndex) {
  90. let xml = CS.FairyEditor.XMLExtension.Load(item.file);
  91. let rootElements = xml.Elements();
  92. for (let index = 0; index < rootElements.Count; index++) {
  93. let child = rootElements.get_Item(index);
  94. if (child.name != 'displayList')
  95. continue;
  96. let childElements = child.Elements();
  97. for (let index = 0; index < childElements.Count; index++) {
  98. const element = childElements.get_Item(index);
  99. if (element.name == 'loader' || element.name == 'loader3D') {
  100. if (element.GetAttribute('clearOnPublish'))
  101. continue;
  102. let url = element.GetAttribute('url');
  103. if (!url)
  104. continue;
  105. //没清理依赖
  106. let packageItem = App.project.GetItemByURL(url);
  107. if (!packageItem) {
  108. App.consoleView.Log(`${FileUtils.getUBBUrl(item.GetURL(), item.name)} 引用了失效资源 ${url}`);
  109. }
  110. else if (!map.has(packageItem.owner.name)) {
  111. if (packageItem.type == FairyEditor.FPackageItemType.COMPONENT) {
  112. quertIndex = this.isClearOnPublish(packageItem, map, quertIndex);
  113. }
  114. map.set(packageItem.owner.name, packageItem.owner);
  115. quertIndex += 1;
  116. }
  117. }
  118. else {
  119. let src = element.GetAttribute("src");
  120. if (!src) {
  121. //系统资源 不需要依赖
  122. continue;
  123. }
  124. let pkg = element.GetAttribute("pkg");
  125. if (!pkg) {
  126. //自己依赖包的资源 所以不需要pkg
  127. pkg = item.owner.id;
  128. }
  129. let url = `ui://${pkg}${src}`;
  130. if (src == null)
  131. App.consoleView.Log(element.name);
  132. let packageItem = App.project.GetItemByURL(url);
  133. if (!packageItem) {
  134. App.consoleView.Log(`${FileUtils.getUBBUrl(item.GetURL(), item.name)} 引用了失效资源 ${url}`);
  135. }
  136. else if (!map.has(packageItem.owner.name)) {
  137. if (packageItem.type == FairyEditor.FPackageItemType.COMPONENT) {
  138. quertIndex = this.isClearOnPublish(packageItem, map, quertIndex);
  139. }
  140. map.set(packageItem.owner.name, packageItem.owner);
  141. quertIndex += 1;
  142. }
  143. }
  144. }
  145. }
  146. return quertIndex;
  147. }
  148. /**获取清理依赖后的资源 */
  149. static getClearOnPublishPackageItem(item, map, quertIndex) {
  150. let xml = CS.FairyEditor.XMLExtension.Load(item.file);
  151. let rootElements = xml.Elements();
  152. for (let index = 0; index < rootElements.Count; index++) {
  153. let child = rootElements.get_Item(index);
  154. if (child.name != 'displayList')
  155. continue;
  156. let childElements = child.Elements();
  157. for (let index = 0; index < childElements.Count; index++) {
  158. const element = childElements.get_Item(index);
  159. if (element.name == 'loader' || element.name == 'loader3D') {
  160. if (element.GetAttribute('clearOnPublish'))
  161. continue;
  162. let url = element.GetAttribute('url');
  163. if (!url)
  164. continue;
  165. //没清理依赖
  166. let packageItem = App.project.GetItemByURL(url);
  167. if (!packageItem) {
  168. App.consoleView.Log(`${FileUtils.getUBBUrl(item.GetURL(), item.name)} 引用了失效资源 ${url}`);
  169. }
  170. else if (!map.has(packageItem.name)) {
  171. if (packageItem.type == FairyEditor.FPackageItemType.COMPONENT) {
  172. quertIndex = this.getClearOnPublishPackageItem(packageItem, map, quertIndex);
  173. }
  174. map.set(packageItem.owner.name, packageItem);
  175. quertIndex += 1;
  176. }
  177. }
  178. else {
  179. let src = element.GetAttribute("src");
  180. if (!src) {
  181. //系统资源 不需要依赖
  182. continue;
  183. }
  184. let pkg = element.GetAttribute("pkg");
  185. if (!pkg) {
  186. //自己依赖包的资源 所以不需要pkg
  187. pkg = item.owner.id;
  188. }
  189. let url = `ui://${pkg}${src}`;
  190. if (src == null)
  191. App.consoleView.Log(element.name);
  192. let packageItem = App.project.GetItemByURL(url);
  193. if (!packageItem) {
  194. App.consoleView.Log(`${FileUtils.getUBBUrl(item.GetURL(), item.name)} 引用了失效资源 ${url}`);
  195. }
  196. else if (!map.has(packageItem.name)) {
  197. if (packageItem.type == FairyEditor.FPackageItemType.COMPONENT) {
  198. quertIndex = this.getClearOnPublishPackageItem(packageItem, map, quertIndex);
  199. }
  200. map.set(packageItem.name, packageItem);
  201. quertIndex += 1;
  202. }
  203. }
  204. }
  205. }
  206. return quertIndex;
  207. }
  208. /**生成模板值 */
  209. static getTemplateVars(keys, nums) {
  210. let dic = FileUtils.createGenericDictionary(System.String, System.String);
  211. keys.forEach((key, index) => {
  212. dic.set_Item(key, nums[index]);
  213. }, this);
  214. return dic;
  215. }
  216. /**所有packageItem累加数量*/
  217. static getAllPackageItemsCount() {
  218. let count = 0;
  219. let pkgs = App.project.allPackages;
  220. pkgs.ForEach(element => {
  221. count += element.items.Count;
  222. });
  223. return count;
  224. }
  225. /**睡眠 delay: 毫秒*/
  226. static sleep(delay) {
  227. return new Promise((resolve) => {
  228. setTimeout(() => { resolve(null); }, delay);
  229. });
  230. }
  231. }
  232. exports.default = FileUtils;