using Godot; using System; using System.Collections.Generic; using System.IO; using System.Linq; using BITKit; using BITKit.IO; using Godot.Collections; namespace BITFactory; [GlobalClass] public partial class IDIS_TemplateResource : TemplateResource { private static readonly List Templates=new(); private static InitializationState state = InitializationState.None; private static string assetPath => PathHelper.GetPath("标识模板资源包.zip"); private static string entryPath => "Templates.json"; public override FormResource[] GetTemplates() { EnsureCreated(); return Templates.ToArray(); } public override string[] GetTemplateNames() { EnsureCreated(); return Templates.Select(x => x.Name).ToArray(); } public override FormResource GetTemplate(string name) { EnsureCreated(); return Templates.Single(x => x.Name == name); } public override bool IsSupportCreateTemplate => true; public override void CreateTemplate() { Templates.Add(new IDIS_TemplateFormResource() { Name = "新的标识模板:"+Guid.NewGuid(), Fields = new IFormField[] { new IDIS_TemplateForm() } }); } private void EnsureCreated() { if (state != InitializationState.None) return; if (File.Exists(assetPath)) { foreach (var x in BITAssets.Read[]>(assetPath, entryPath)) { Templates.Add(new IDIS_TemplateFormResource() { Name = x.Key, Fields = x.Value.Select(_=>_ as IFormField).ToArray() }); } } state = InitializationState.Initialized; } public override void OnStop() { ManualSave(); } public override void ManualSave() { // var values = new System.Collections.Generic.Dictionary>( // Templates.Select(x=>new KeyValuePair>( // x.Key,x.Value.Fields.ToList() // )) // ); var values = Templates.Select(x => new KeyValuePair( x.Name, x.Fields )); BITAssets.Build(assetPath,new BITAsset(entryPath,JsonHelper.Get(values))); } }