add something

This commit is contained in:
ww-rm
2025-03-17 21:11:09 +08:00
parent e5e9357649
commit 5009ef479f
2 changed files with 104 additions and 8 deletions

View File

@@ -801,9 +801,105 @@ namespace SpineViewer.Spine.Implementations.SkeletonConverter
} }
} }
protected override void WriteBinary(JsonObject root, string binPath) private SkeletonWriter writer;
private readonly Dictionary<string, int> bone2idx = [];
private readonly Dictionary<string, int> slot2idx = [];
private readonly Dictionary<string, int> ik2idx = [];
private readonly Dictionary<string, int> transform2idx = [];
private readonly Dictionary<string, int> path2idx = [];
private readonly Dictionary<string, int> event2idx = [];
protected override void WriteBinary(JsonObject root, string binPath, bool nonessential = false)
{ {
throw new NotImplementedException(); this.nonessential = nonessential;
using var outputBody = new MemoryStream(); // 先把主体写入内存缓冲区
writer = new(outputBody);
this.root = root;
WriteBones();
WriteSlots();
WriteIK();
WriteTransform();
WritePath();
WriteSkins();
WriteEvents();
WriteAnimations();
using var output = File.Create(binPath); // 将数据写入文件
writer = new(output);
WriteSkeleton();
WriteStrings();
output.Write(outputBody.GetBuffer());
writer = null;
this.root = null;
}
private void WriteSkeleton()
{
JsonObject skeleton = root["skeleton"].AsObject();
writer.WriteString(skeleton["hash"].GetValue<string>());
writer.WriteString(skeleton["spine"].GetValue<string>());
if (skeleton.TryGetPropertyValue("x", out var x)) writer.WriteFloat(x.GetValue<float>()); else writer.WriteFloat(0);
if (skeleton.TryGetPropertyValue("y", out var y)) writer.WriteFloat(y.GetValue<float>()); else writer.WriteFloat(0);
if (skeleton.TryGetPropertyValue("width", out var width)) writer.WriteFloat(width.GetValue<float>()); else writer.WriteFloat(0);
if (skeleton.TryGetPropertyValue("height", out var height)) writer.WriteFloat(height.GetValue<float>()); else writer.WriteFloat(0);
writer.WriteBoolean(nonessential);
if (nonessential)
{
if (skeleton.TryGetPropertyValue("fps", out var fps)) writer.WriteFloat(fps.GetValue<float>()); else writer.WriteFloat(30);
if (skeleton.TryGetPropertyValue("images", out var images)) writer.WriteString(images.GetValue<string>()); else writer.WriteString(null);
if (skeleton.TryGetPropertyValue("audio", out var audio)) writer.WriteString(audio.GetValue<string>()); else writer.WriteString(null);
}
}
private void WriteStrings()
{
writer.WriteVarInt(writer.StringTable.Count);
foreach (var s in writer.StringTable)
writer.WriteString(s);
}
private void WriteBones()
{
}
private void WriteSlots()
{
}
private void WriteIK()
{
}
private void WriteTransform()
{
}
private void WritePath()
{
}
private void WriteSkins()
{
}
private void WriteEvents()
{
}
private void WriteAnimations()
{
} }
//public void WriteFloatArray(float[] array) //public void WriteFloatArray(float[] array)

View File

@@ -87,7 +87,7 @@ namespace SpineViewer.Spine
/// <summary> /// <summary>
/// 将 Json 对象写入二进制骨骼文件 /// 将 Json 对象写入二进制骨骼文件
/// </summary> /// </summary>
protected abstract void WriteBinary(JsonObject root, string binPath); protected abstract void WriteBinary(JsonObject root, string binPath, bool nonessential = false);
/// <summary> /// <summary>
/// 读取 Json 对象 /// 读取 Json 对象
@@ -234,7 +234,7 @@ namespace SpineViewer.Spine
{ {
protected byte[] buffer = new byte[32]; protected byte[] buffer = new byte[32];
protected byte[] bytesBigEndian = new byte[8]; protected byte[] bytesBigEndian = new byte[8];
public readonly List<string> Strings = new(32); public readonly List<string> StringTable = new(32);
protected Stream output; protected Stream output;
public SkeletonWriter(Stream output) { this.output = output; } public SkeletonWriter(Stream output) { this.output = output; }
@@ -323,18 +323,18 @@ namespace SpineViewer.Spine
System.Text.Encoding.UTF8.GetBytes(val, 0, val.Length, buffer, 0); System.Text.Encoding.UTF8.GetBytes(val, 0, val.Length, buffer, 0);
WriteFully(buffer, 0, byteCount); WriteFully(buffer, 0, byteCount);
} }
public void WriteStringRef(List<string> strings, string val) public void WriteStringRef(string val)
{ {
if (val is null) if (val is null)
{ {
WriteVarInt(0); WriteVarInt(0);
return; return;
} }
int index = strings.IndexOf(val); int index = StringTable.IndexOf(val);
if (index < 0) if (index < 0)
{ {
strings.Add(val); StringTable.Add(val);
index = strings.Count - 1; index = StringTable.Count - 1;
} }
WriteVarInt(index + 1); WriteVarInt(index + 1);
} }