97 lines
2.4 KiB
C#
97 lines
2.4 KiB
C#
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<FormResource> 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 FormResource CreateTemplate()
|
|
{
|
|
var newResource =
|
|
new IDIS_TemplateFormResource()
|
|
{
|
|
Name = "新的标识模板:"+Guid.NewGuid(),
|
|
Fields = new IFormField[]
|
|
{
|
|
new IDIS_TemplateForm()
|
|
{
|
|
Name = "新的标识字段:",
|
|
Type = "string",
|
|
DefaultValue = "new",
|
|
Category = "默认分类"
|
|
}
|
|
}
|
|
};
|
|
Templates.Add(newResource);
|
|
return newResource;
|
|
}
|
|
|
|
public override void DeleteTemplate(string name)
|
|
{
|
|
var index = Templates.FindIndex(x => x.Name == name);
|
|
if (index == -1) return;
|
|
Templates.RemoveAt(index);
|
|
}
|
|
|
|
private void EnsureCreated()
|
|
{
|
|
if (state != InitializationState.None) return;
|
|
if (File.Exists(assetPath))
|
|
{
|
|
foreach (var x in BITAssets.Read<KeyValuePair<string,IDIS_TemplateForm[]>[]>(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<string, List<IFormField>>(
|
|
// Templates.Select(x=>new KeyValuePair<string, List<IFormField>>(
|
|
// x.Key,x.Value.Fields.ToList()
|
|
// ))
|
|
// );
|
|
var values = Templates.Select(x => new KeyValuePair<string, object>(
|
|
x.Name, x.Fields
|
|
));
|
|
BITAssets.Build(assetPath,new BITAsset(entryPath,JsonHelper.Get(values)));
|
|
}
|
|
}
|