"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.onDestroy = exports.onPublish = exports.CalFGUIData = void 0; const csharp_1 = require("csharp"); const GenCode_CSharp_1 = require("./GenCode_CSharp"); const App = csharp_1.FairyEditor.App; App.pluginManager.LoadUIPackage(App.pluginManager.basePath + "/" + eval("__dirname") + '/Common'); class CalFGUIJsonDataItem { constructor(url) { this.url = url; this.isGenerate = false; this.comment = null; } } //let ListString = $generic(System.Collections.Generic.List$1, System.String); //let DictionaryStringCalFGUIJsonDataItem = $generic(System.Collections.Generic.Dictionary$2, System.String, CalFGUIJsonDataItem); class CalFGUIJsonData { constructor(dict) { this.keys = new Array(); this.values = new Array(); for (let [k, v] of dict) { this.keys.push(k); this.values.push(v); } } Get() { let dict = new Map(); if (this.keys) { for (let index = 0; index < this.keys.length; index++) { const key = this.keys[index]; const value = this.values[index]; dict.set(key, value); } } return dict; } } class CalFGUIData extends csharp_1.System.Object { constructor(path) { super(); this.path = path; if (!csharp_1.System.IO.File.Exists(path)) { csharp_1.System.IO.File.Create(path); } this.dic = new Map(); } SetData(name, obj) { this.dic.set(name, obj); } GetData(name) { if (this.dic.has(name)) { return this.dic.get(name); } return null; } Serialize() { let json = JSON.stringify(new CalFGUIJsonData(this.dic)); json = json.replace(/,\{/g, ",\r\n{"); csharp_1.System.IO.File.WriteAllText(this.path, json); } static Derserialize(calFGUIData) { let json = csharp_1.System.IO.File.ReadAllText(calFGUIData.path); if (!json) { calFGUIData.dic = new Map(); return; } let jsonData = JSON.parse(json); let calFGUIJsonData = Object.create(CalFGUIJsonData.prototype); let ret = Object.assign(calFGUIJsonData, jsonData); if (ret) { calFGUIData.dic = ret.Get(); console.log("加载成功"); } } CheckMissURL() { let list = new Array(); for (const [k, v] of this.dic) { let pi = App.project.GetItemByURL(k); if (!pi || pi.isDisposed) { list.push(k); } } for (const key of list) { console.log(key); this.dic.delete(key); } } } exports.CalFGUIData = CalFGUIData; class DemoInspector extends csharp_1.FairyEditor.View.PluginInspector { constructor() { super(); let path = App.project.assetsPath + "/data.txt"; this.data = new CalFGUIData(path); this.OnDeserialize(); this.CheckMissURL(); this.panel = csharp_1.FairyGUI.UIPackage.CreateObject("Common", "UIPlaugin").asCom; this.button = this.panel.GetChild("btn").asButton; this.text = this.panel.GetChild("inp").asTextInput; this.button.onClick.Set(() => { let url = App.activeDoc.packageItem.GetURL(); let itemData = this.data.GetData(url); if (!itemData) { itemData = new CalFGUIJsonDataItem(url); } itemData.isGenerate = this.button.selected; this.data.SetData(url, itemData); this.updateUI(); this.OnSerialize(); }); this.text.onFocusOut.Set(() => { let url = App.activeDoc.packageItem.GetURL(); let itemData = this.data.GetData(url); if (!itemData) { itemData = new CalFGUIJsonDataItem(url); } itemData.comment = this.text.text; this.data.SetData(url, itemData); this.updateUI(); this.OnSerialize(); }); this.updateAction = () => { return this.updateUI(); }; this.disposeAction = () => { }; } OnSerialize() { this.data.Serialize(); } OnDeserialize() { CalFGUIData.Derserialize(this.data); } CheckMissURL() { this.data.CheckMissURL(); } updateUI() { let url = App.activeDoc.packageItem.GetURL(); let itemData = this.data.GetData(url); if (!itemData) { itemData = new CalFGUIJsonDataItem(url); } this.button.selected = itemData.isGenerate; this.text.text = itemData.comment; return true; //if everything is ok, return false to hide the inspector } } //Register a inspector App.inspectorView.AddInspector(() => new DemoInspector(), "CustomInspectorJS", "CustomInspector"); //Condition to show it App.docFactory.ConnectInspector("CustomInspectorJS", "mixed", true, false); let hasCalData = false; let data; function onPublish(handler) { if (!handler.genCode) return; handler.genCode = false; //prevent default output console.log('Handling gen code in plugin'); if (!hasCalData) { hasCalData = true; let path = App.project.assetsPath + "/data.txt"; try { data = new CalFGUIData(path); CalFGUIData.Derserialize(data); } catch (error) { App.consoleView.LogError(error.toString()); } } try { GenCode_CSharp_1.genCode(handler, data); //do it myself } catch (error) { App.consoleView.LogError(error.toString()); } } exports.onPublish = onPublish; function onDestroy() { //do cleanup here } exports.onDestroy = onDestroy;