123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240 |
- import { $generic } from 'puerts';
- const FairyEditor = CS.FairyEditor
- const System = CS.System
- const App = FairyEditor.App;
- export default class FileUtils {
- /**获取选中目标数量 */
- static getSelectedTargetCount(): number {
- let items = App.activeDoc.inspectingTargets;
- let index = 0;
- try {
- while ((items.get_Item(index) != null)) {
- index++;
- }
- } catch (e) {
- return index
- }
- return index
- }
- /**获取UBB Url文本 */
- static getUBBUrl(url: string, titile: string) {
- return `[url=` + `${url}]${titile}[/url]`
- }
- /**UBB file文本 */
- static getUBBFile(data: CS.FairyEditor.FPackageItem) {
- let a = `[url=${FileUtils.getFilePath(data)}]`
- let b = this.getAbsPackageItemPath(data)
- let c = "[/url]"
- return a + b + c
- }
- /**获取packageItem的File路径 不带后缀 */
- static getFilePath(data: CS.FairyEditor.FPackageItem) {
- return `file://${FileUtils.getAbsPackageItemPath(data)}${FileUtils.getAssetType(data)}`
- }
- static getHttpPath(http: string, title: string) {
- let a = `[url=${http}]`
- let b = title
- let c = "[/url]"
- return a + b + c
- }
- /**获取packageItem的绝对路径 不带后缀 */
- static getAbsPackageItemPath(data: CS.FairyEditor.FPackageItem) {
- return `${App.project.basePath}\\assets\\${data.owner.name}${data.path}${data.name}`
- }
- /**获取资源类型的后缀名 */
- static getAssetType(asset: CS.FairyEditor.FPackageItem) {
- switch (asset.type) {
- case FairyEditor.FPackageItemType.IMAGE:
- return ".png";
- case FairyEditor.FPackageItemType.ATLAS:
- return ".jpg";
- case FairyEditor.FPackageItemType.SPINE:
- return ".json"
- }
- }
- /**拼接fgui url */
- static joinUrl(pkg: string, res: string) {
- return `ui://${pkg}/${res}`;
- }
- /**创建泛型List */
- static createGenericList<T>(t: any) {
- let List = $generic(System.Collections.Generic.List$1, t);
- let list = new List<T>();
- return list
- }
- /**创建泛型字典 */
- static createGenericDictionary<T, K>(t: any, k: any) {
- let Dictionary = $generic(System.Collections.Generic.Dictionary$2, t, k);
- let dictionary = new Dictionary<T, K>();
- return dictionary;
- }
- //#region IO
- /**加载Json文件 */
- static loadJson(path: string) {
- if (System.IO.File.Exists(path) == false) {
- return null;
- }
- let f = System.IO.File.ReadAllText(path)
- return JSON.parse(f) as {}
- }
- /**保存Json文件 */
- static writerJson(path: string, msg: {} | null) {
- let config = JSON.stringify(msg)
- System.IO.File.WriteAllText(path, config)
- }
- /**是否清理了依赖 */
- static isClearOnPublish(item: CS.FairyEditor.FPackageItem, map: Map<string, CS.FairyEditor.FPackage>, quertIndex) {
- let xml = CS.FairyEditor.XMLExtension.Load(item.file)
- let rootElements = xml.Elements()
- for (let index = 0; index < rootElements.Count; index++) {
- let child = rootElements.get_Item(index)
- if (child.name != 'displayList') continue
- let childElements = child.Elements();
- for (let index = 0; index < childElements.Count; index++) {
- const element = childElements.get_Item(index);
- if (element.name == 'loader' || element.name == 'loader3D') {
- if (element.GetAttribute('clearOnPublish')) continue
- let url = element.GetAttribute('url')
- if (!url) continue
- //没清理依赖
- let packageItem = App.project.GetItemByURL(url)
- if (!packageItem) {
- App.consoleView.Log(`${FileUtils.getUBBUrl(item.GetURL(), item.name)} 引用了失效资源 ${url}`)
- }
- else if (!map.has(packageItem.owner.name)) {
- if (packageItem.type == FairyEditor.FPackageItemType.COMPONENT) {
- quertIndex = this.isClearOnPublish(packageItem, map, quertIndex)
- }
- map.set(packageItem.owner.name, packageItem.owner)
- quertIndex += 1;
- }
- } else {
- let src = element.GetAttribute("src")
- if (!src) {
- //系统资源 不需要依赖
- continue
- }
- let pkg = element.GetAttribute("pkg")
- if (!pkg) {
- //自己依赖包的资源 所以不需要pkg
- pkg = item.owner.id;
- }
- let url = `ui://${pkg}${src}`
- if (src == null) App.consoleView.Log(element.name)
- let packageItem = App.project.GetItemByURL(url)
- if (!packageItem) {
- App.consoleView.Log(`${FileUtils.getUBBUrl(item.GetURL(), item.name)} 引用了失效资源 ${url}`)
- }
- else if (!map.has(packageItem.owner.name)) {
- if (packageItem.type == FairyEditor.FPackageItemType.COMPONENT) {
- quertIndex = this.isClearOnPublish(packageItem, map, quertIndex)
- }
- map.set(packageItem.owner.name, packageItem.owner)
- quertIndex += 1;
- }
- }
- }
- }
- return quertIndex
- }
- /**获取清理依赖后的资源 */
- static getClearOnPublishPackageItem(item: CS.FairyEditor.FPackageItem, map: Map<string, CS.FairyEditor.FPackageItem>, quertIndex) {
- let xml = CS.FairyEditor.XMLExtension.Load(item.file)
- let rootElements = xml.Elements()
- for (let index = 0; index < rootElements.Count; index++) {
- let child = rootElements.get_Item(index)
- if (child.name != 'displayList') continue
- let childElements = child.Elements();
- for (let index = 0; index < childElements.Count; index++) {
- const element = childElements.get_Item(index);
- if (element.name == 'loader' || element.name == 'loader3D') {
- if (element.GetAttribute('clearOnPublish')) continue
- let url = element.GetAttribute('url')
- if (!url) continue
- //没清理依赖
- let packageItem = App.project.GetItemByURL(url)
- if (!packageItem) {
- App.consoleView.Log(`${FileUtils.getUBBUrl(item.GetURL(), item.name)} 引用了失效资源 ${url}`)
- }
- else if (!map.has(packageItem.name)) {
- if (packageItem.type == FairyEditor.FPackageItemType.COMPONENT) {
- quertIndex = this.getClearOnPublishPackageItem(packageItem, map, quertIndex)
- }
- map.set(packageItem.owner.name, packageItem)
- quertIndex += 1;
- }
- } else {
- let src = element.GetAttribute("src")
- if (!src) {
- //系统资源 不需要依赖
- continue
- }
- let pkg = element.GetAttribute("pkg")
- if (!pkg) {
- //自己依赖包的资源 所以不需要pkg
- pkg = item.owner.id;
- }
- let url = `ui://${pkg}${src}`
- if (src == null) App.consoleView.Log(element.name)
- let packageItem = App.project.GetItemByURL(url)
- if (!packageItem) {
- App.consoleView.Log(`${FileUtils.getUBBUrl(item.GetURL(), item.name)} 引用了失效资源 ${url}`)
- }
- else if (!map.has(packageItem.name)) {
- if (packageItem.type == FairyEditor.FPackageItemType.COMPONENT) {
- quertIndex = this.getClearOnPublishPackageItem(packageItem, map, quertIndex)
- }
- map.set(packageItem.name, packageItem)
- quertIndex += 1;
- }
- }
- }
- }
- return quertIndex
- }
- /**生成模板值 */
- static getTemplateVars(keys: Array<string>, nums: Array<string>) {
- let dic = FileUtils.createGenericDictionary<string, string>(System.String, System.String);
- keys.forEach((key, index) => {
- dic.set_Item(key, nums[index])
- }, this)
- return dic;
- }
- /**所有packageItem累加数量*/
- static getAllPackageItemsCount(): number {
- let count = 0;
- let pkgs = App.project.allPackages;
- pkgs.ForEach(element => {
- count += element.items.Count
- })
- return count
- }
- /**睡眠 delay: 毫秒*/
- static sleep(delay: number) {
- return new Promise((resolve) => {
- setTimeout(() => { resolve(null) }, delay);
- });
- }
- //#endregion
- }
|