Files
Convention-Unity-Demo/Assets/Scripts/Framework/DDT.cs

120 lines
4.3 KiB
C#
Raw Normal View History

2025-09-25 19:04:05 +08:00
using Convention;
2025-12-15 17:20:55 +08:00
using Demo.Game.Attr;
using System;
2025-12-12 15:19:10 +08:00
using System.Collections;
using System.IO;
2025-12-04 15:58:38 +08:00
using Unity.Collections;
2025-12-12 15:19:10 +08:00
using UnityEngine;
2025-12-04 15:58:38 +08:00
using UnityEngine.Rendering;
2025-09-25 19:04:05 +08:00
namespace Demo.Game
{
2025-12-15 17:20:55 +08:00
namespace ConfigType
{
public class DDTConfig : ScriptLoadableConfig
{
2025-12-18 10:06:49 +08:00
[Content] public NativeArray<float> Datas = new(128, Allocator.Persistent, NativeArrayOptions.UninitializedMemory);
[Content] public int Count = 0;
2025-12-15 17:20:55 +08:00
public override void Deserialize(BinaryReader reader)
{
BinarySerializeUtility.DeserializeNativeArray(reader, ref Datas);
2025-12-18 10:10:49 +08:00
Count = Datas.Length;
2025-12-15 17:20:55 +08:00
base.Deserialize(reader);
}
public override void Serialize(BinaryWriter writer)
{
2025-12-18 10:10:49 +08:00
var newArray = new NativeArray<float>(Count, Allocator.Persistent, NativeArrayOptions.UninitializedMemory);
NativeArray<float>.Copy(Datas, newArray, Count);
Datas = newArray;
2025-12-15 17:20:55 +08:00
BinarySerializeUtility.SerializeNativeArray(writer, Datas);
base.Serialize(writer);
}
}
}
2025-12-12 15:19:10 +08:00
[Scriptable]
2025-12-04 15:58:38 +08:00
public class DDT : ScriptableObject
2025-09-25 19:04:05 +08:00
{
2025-12-18 10:06:49 +08:00
protected override ConfigType.ScriptLoadableConfig MakeConfig()
{
return new ConfigType.DDTConfig();
}
2025-11-29 00:44:05 +08:00
protected override bool IsSelfEnableUpdate => false;
2025-09-25 19:04:05 +08:00
public static DDT Make()
{
return new GameObject().AddComponent<DDT>();
}
2025-11-12 10:09:26 +08:00
[Convention.RScript.Variable.Attr.Method]
public void Add(float value)
{
2025-12-18 10:06:49 +08:00
int Count = GetConfig<ConfigType.DDTConfig>().Count;
if (Count >= GetConfig<ConfigType.DDTConfig>().Datas.Length)
GetConfig<ConfigType.DDTConfig>().Datas.ResizeArray(Mathf.FloorToInt(Count * 1.5f));
GetConfig<ConfigType.DDTConfig>().Datas[Count] = value;
GetConfig<ConfigType.DDTConfig>().Count++;
2025-11-12 10:09:26 +08:00
}
[Convention.RScript.Variable.Attr.Method]
public void Add(int barSplitTimes, int barCount, int tickCount)
{
2025-12-04 15:58:38 +08:00
Add((barCount + tickCount / (float)barSplitTimes) * OneBarTime);
2025-11-12 10:09:26 +08:00
}
2025-11-25 17:04:19 +08:00
[Convention.RScript.Variable.Attr.Method]
public float At(int index)
{
2025-12-18 10:06:49 +08:00
int Count = GetConfig<ConfigType.DDTConfig>().Count;
if (index < 0)
2025-12-04 15:58:38 +08:00
index = Count + index;
if (index < 0 || index >= Count)
throw new IndexOutOfRangeException($"{index} is out of [0, {Count})");
2025-12-18 10:06:49 +08:00
return GetConfig<ConfigType.DDTConfig>().Datas[index];
2025-11-25 17:04:19 +08:00
}
[Convention.RScript.Variable.Attr.Method]
public int GetCount()
{
2025-12-18 10:06:49 +08:00
return GetConfig<ConfigType.DDTConfig>().Count;
}
2025-12-05 16:26:42 +08:00
private void OnDestroy()
{
2025-12-18 10:06:49 +08:00
if (GetConfig<ConfigType.DDTConfig>().Datas.IsCreated)
GetConfig<ConfigType.DDTConfig>().Datas.Dispose();
2025-12-05 16:26:42 +08:00
}
#region Serialize
protected override bool IsImptSerialize => true;
protected override IEnumerator CreateAndLoadingImptCacheFile(ToolFile scriptFile, ToolFile cacheFile)
{
yield return this.ParseScript2Expr(scriptFile.LoadAsText());
using var stream = File.OpenWrite(cacheFile.GetFullPath());
using var writer = new BinaryWriter(stream);
2025-12-18 10:06:49 +08:00
writer.Write(GetConfig<ConfigType.DDTConfig>().Count);
for (int i = 0, e = GetConfig<ConfigType.DDTConfig>().Count; i < e; i++)
{
2025-12-18 10:06:49 +08:00
writer.Write(GetConfig<ConfigType.DDTConfig>().Datas[i]);
}
}
protected override IEnumerator LoadFromImptCacheFile(ToolFile cacheFile)
{
2025-12-09 15:12:25 +08:00
using var stream = File.OpenRead(cacheFile.GetFullPath());
using var reader = new BinaryReader(stream);
2025-12-18 10:06:49 +08:00
GetConfig<ConfigType.DDTConfig>().Count = reader.ReadInt32();
GetConfig<ConfigType.DDTConfig>().Datas.ResizeArray(Mathf.Max(128, GetConfig<ConfigType.DDTConfig>().Count));
for (int i = 0, e = GetConfig<ConfigType.DDTConfig>().Count; i < e; i++)
{
2025-12-18 10:06:49 +08:00
GetConfig<ConfigType.DDTConfig>().Datas[i] = reader.ReadSingle();
}
yield break;
}
#endregion
2025-09-25 19:04:05 +08:00
}
}