推进修改

This commit is contained in:
2025-12-18 10:06:49 +08:00
parent d6336faed0
commit 04ad260824
3 changed files with 48 additions and 56 deletions

View File

@@ -14,7 +14,8 @@ namespace Demo.Game
{ {
public class DDTConfig : ScriptLoadableConfig public class DDTConfig : ScriptLoadableConfig
{ {
public NativeArray<float> Datas = new(128, Allocator.Persistent, NativeArrayOptions.UninitializedMemory); [Content] public NativeArray<float> Datas = new(128, Allocator.Persistent, NativeArrayOptions.UninitializedMemory);
[Content] public int Count = 0;
public override void Deserialize(BinaryReader reader) public override void Deserialize(BinaryReader reader)
{ {
@@ -33,22 +34,25 @@ namespace Demo.Game
[Scriptable] [Scriptable]
public class DDT : ScriptableObject public class DDT : ScriptableObject
{ {
protected override ConfigType.ScriptLoadableConfig MakeConfig()
{
return new ConfigType.DDTConfig();
}
protected override bool IsSelfEnableUpdate => false; protected override bool IsSelfEnableUpdate => false;
public static DDT Make() public static DDT Make()
{ {
return new GameObject().AddComponent<DDT>(); return new GameObject().AddComponent<DDT>();
} }
public NativeArray<float> Datas = new(128, Allocator.Persistent, NativeArrayOptions.UninitializedMemory);
public int Count { get; private set; } = 0;
[Convention.RScript.Variable.Attr.Method] [Convention.RScript.Variable.Attr.Method]
public void Add(float value) public void Add(float value)
{ {
if (Count >= Datas.Length) int Count = GetConfig<ConfigType.DDTConfig>().Count;
Datas.ResizeArray(Mathf.FloorToInt(Count * 1.5f)); if (Count >= GetConfig<ConfigType.DDTConfig>().Datas.Length)
Datas[Count] = value; GetConfig<ConfigType.DDTConfig>().Datas.ResizeArray(Mathf.FloorToInt(Count * 1.5f));
Count++; GetConfig<ConfigType.DDTConfig>().Datas[Count] = value;
GetConfig<ConfigType.DDTConfig>().Count++;
} }
[Convention.RScript.Variable.Attr.Method] [Convention.RScript.Variable.Attr.Method]
@@ -60,27 +64,24 @@ namespace Demo.Game
[Convention.RScript.Variable.Attr.Method] [Convention.RScript.Variable.Attr.Method]
public float At(int index) public float At(int index)
{ {
int Count = GetConfig<ConfigType.DDTConfig>().Count;
if (index < 0) if (index < 0)
index = Count + index; index = Count + index;
if (index < 0 || index >= Count) if (index < 0 || index >= Count)
throw new IndexOutOfRangeException($"{index} is out of [0, {Count})"); throw new IndexOutOfRangeException($"{index} is out of [0, {Count})");
return Datas[index]; return GetConfig<ConfigType.DDTConfig>().Datas[index];
} }
/// <summary>
/// <20><>ȡ<EFBFBD><C8A1><EFBFBD>ݳ<EFBFBD><DDB3><EFBFBD>
/// </summary>
/// <returns></returns>
[Convention.RScript.Variable.Attr.Method] [Convention.RScript.Variable.Attr.Method]
public int GetCount() public int GetCount()
{ {
return Count; return GetConfig<ConfigType.DDTConfig>().Count;
} }
private void OnDestroy() private void OnDestroy()
{ {
if (Datas.IsCreated) if (GetConfig<ConfigType.DDTConfig>().Datas.IsCreated)
Datas.Dispose(); GetConfig<ConfigType.DDTConfig>().Datas.Dispose();
} }
#region Serialize #region Serialize
@@ -91,36 +92,25 @@ namespace Demo.Game
yield return this.ParseScript2Expr(scriptFile.LoadAsText()); yield return this.ParseScript2Expr(scriptFile.LoadAsText());
using var stream = File.OpenWrite(cacheFile.GetFullPath()); using var stream = File.OpenWrite(cacheFile.GetFullPath());
using var writer = new BinaryWriter(stream); using var writer = new BinaryWriter(stream);
writer.Write(Count); writer.Write(GetConfig<ConfigType.DDTConfig>().Count);
for (int i = 0; i < Count; i++) for (int i = 0, e = GetConfig<ConfigType.DDTConfig>().Count; i < e; i++)
{ {
writer.Write(Datas[i]); writer.Write(GetConfig<ConfigType.DDTConfig>().Datas[i]);
} }
} }
protected override IEnumerator LoadFromImptCacheFile(ToolFile cacheFile) protected override IEnumerator LoadFromImptCacheFile(ToolFile cacheFile)
{ {
using var stream = File.OpenRead(cacheFile.GetFullPath()); using var stream = File.OpenRead(cacheFile.GetFullPath());
using var reader = new BinaryReader(stream); using var reader = new BinaryReader(stream);
Count = reader.ReadInt32(); GetConfig<ConfigType.DDTConfig>().Count = reader.ReadInt32();
Datas.ResizeArray(Mathf.Max(128, Count)); GetConfig<ConfigType.DDTConfig>().Datas.ResizeArray(Mathf.Max(128, GetConfig<ConfigType.DDTConfig>().Count));
for (int i = 0; i < Count; i++) for (int i = 0, e = GetConfig<ConfigType.DDTConfig>().Count; i < e; i++)
{ {
Datas[i] = reader.ReadSingle(); GetConfig<ConfigType.DDTConfig>().Datas[i] = reader.ReadSingle();
} }
yield break; yield break;
} }
#endregion #endregion
#if UNITY_EDITOR
[Setting, SerializeField] private List<float> d_Datas = new();
protected override IEnumerator DoSomethingDuringApplyScript()
{
yield return base.DoSomethingDuringApplyScript();
for (int i = 0; i < Count; i++)
d_Datas.Add(Datas[i]);
}
#endif
} }
} }

View File

@@ -206,12 +206,12 @@ namespace Demo.Game
var projectHashFile = cacheDir | "projectHash.json"; var projectHashFile = cacheDir | "projectHash.json";
Dictionary<string, string> projectHash = new(); Dictionary<string, string> projectHash = new();
bool isRecompile = true; bool isRecompile = true;
if(!!projectHashFile) if (!!projectHashFile)
{ {
projectHash = projectHashFile.LoadAsJson<Dictionary<string, string>>(); projectHash = projectHashFile.LoadAsJson<Dictionary<string, string>>();
foreach (var (file,md5) in projectHash) foreach (var (file, md5) in projectHash)
{ {
if(new ToolFile(file).CalculateHash()!=md5) if (new ToolFile(file).CalculateHash() != md5)
{ {
isRecompile = true; isRecompile = true;
break; break;
@@ -228,7 +228,7 @@ namespace Demo.Game
rootGameObject.SetContent(nameof(SongOffset), SongOffset); rootGameObject.SetContent(nameof(SongOffset), SongOffset);
rootGameObject.SetContent(nameof(IsAutoPlay), IsAutoPlay ? 1 : 0); rootGameObject.SetContent(nameof(IsAutoPlay), IsAutoPlay ? 1 : 0);
rootGameObject.SetContent("SongLength", MainAudio.CurrentClip.length); rootGameObject.SetContent("SongLength", MainAudio.CurrentClip.length);
if (isRecompile||true) if (isRecompile || true)
{ {
static IEnumerator Foo(IEnumerator ir) static IEnumerator Foo(IEnumerator ir)
{ {
@@ -271,20 +271,26 @@ namespace Demo.Game
} }
NDFS(rootGameObject); NDFS(rootGameObject);
yield return new WaitUntil(() => applyDownCount == 0); yield return new WaitUntil(() => applyDownCount == 0);
projectHash.Clear();
foreach (var path in rootGameObject.LoadedScriptSet) foreach (var path in rootGameObject.LoadedScriptSet)
{ {
projectHash.Add(path, new ToolFile(path).CalculateHash()); projectHash.Add(path, new ToolFile(path).CalculateHash());
} }
// 哈希缓存
projectHashFile.SaveAsJson(projectHash); projectHashFile.SaveAsJson(projectHash);
// 编译结果
var projectBinaryFile = cacheDir | "project.dat";
using var stream = new FileInfo(projectBinaryFile).OpenWrite();
using var writer = new BinaryWriter(stream);
rootGameObject.Config.Serialize(writer);
} }
else else
{ {
// 加载
var projectBinaryFile = cacheDir | "project.dat"; var projectBinaryFile = cacheDir | "project.dat";
using var stream = new FileInfo(projectBinaryFile).OpenRead(); using var stream = new FileInfo(projectBinaryFile).OpenRead();
using var reader = new BinaryReader(stream); using var reader = new BinaryReader(stream);
rootGameObject.Config.Deserialize(reader);
} }
float loadRootObjectEndTime = Time.realtimeSinceStartup; float loadRootObjectEndTime = Time.realtimeSinceStartup;
float loadRootObjectElapsed = (loadRootObjectEndTime - loadRootObjectStartTime) * 1000f; float loadRootObjectElapsed = (loadRootObjectEndTime - loadRootObjectStartTime) * 1000f;

View File

@@ -44,7 +44,7 @@ namespace Demo.Game
[Setting] public string ScriptName = ""; [Setting] public string ScriptName = "";
private string[] ChildTypes = null; private string[] ChildTypes = null;
public ScriptLoadableConfig[] childs = null; private ScriptLoadableConfig[] childs = null;
[Setting] public ScriptableObject target; [Setting] public ScriptableObject target;
@@ -77,6 +77,7 @@ namespace Demo.Game
BinarySerializeUtility.WriteBool(writer, IsSetObjectDisable); BinarySerializeUtility.WriteBool(writer, IsSetObjectDisable);
BinarySerializeUtility.WriteInt(writer, UpdatePerFrame); BinarySerializeUtility.WriteInt(writer, UpdatePerFrame);
BinarySerializeUtility.WriteString(writer, ScriptName); BinarySerializeUtility.WriteString(writer, ScriptName);
childs = (from child in target.Childs select child.Config).ToArray();
ChildTypes = (from child in childs select child.GetType().Name).ToArray(); ChildTypes = (from child in childs select child.GetType().Name).ToArray();
BinarySerializeUtility.SerializeArray(writer, ChildTypes); BinarySerializeUtility.SerializeArray(writer, ChildTypes);
foreach (var child in childs) foreach (var child in childs)
@@ -122,10 +123,6 @@ namespace Demo.Game
{ {
return (ConfigT)Config; return (ConfigT)Config;
} }
public virtual void UpdateConfig()
{
Config.childs = (from child in this.Childs select child.Config).ToArray();
}
/// <summary> /// <summary>
/// 设置坐标 /// 设置坐标
@@ -654,7 +651,6 @@ namespace Demo.Game
if (this.ScriptableObjectContents.IsCreated) if (this.ScriptableObjectContents.IsCreated)
this.ScriptableObjectContents.Dispose(); this.ScriptableObjectContents.Dispose();
} }
UpdateConfig();
IsScriptApply = true; IsScriptApply = true;
} }