FileUtils.ts 9.3 KB

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