继续推动Config更新(本机测试发生错误)

This commit is contained in:
2025-12-19 01:01:25 +08:00
parent ab60b35be2
commit 63b762453a
6 changed files with 118 additions and 103 deletions

View File

@@ -1,10 +1,9 @@
using Convention;
using System; using System;
using System.Collections; using System.Collections;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO; using System.IO;
using Convention;
using Unity.Cinemachine; using Unity.Cinemachine;
using Unity.VisualScripting;
using UnityEngine; using UnityEngine;
using UnityEngine.InputSystem; using UnityEngine.InputSystem;
using UnityEngine.SceneManagement; using UnityEngine.SceneManagement;
@@ -52,6 +51,98 @@ namespace Demo.Game
[Header("Environment")] [Header("Environment")]
[Resources] public Transform GlobalLight; [Resources] public Transform GlobalLight;
private IEnumerator LoadWithRecompile(RootObject rootGameObject,
ToolFile rootObject,
Dictionary<string, string> projectHash,
ToolFile projectHashFile,
ToolFile cacheDir)
{
static IEnumerator Foo(IEnumerator ir)
{
Stack<IEnumerator> loadingTask = new();
loadingTask.Push(ir);
while (loadingTask.Count > 0)
{
if (loadingTask.Peek().MoveNext())
{
if (loadingTask.Peek().Current is IEnumerator next)
loadingTask.Push(next);
else if (loadingTask.Peek().Current is ScriptableObject)
yield return null;
}
else
{
loadingTask.Pop();
}
}
yield break;
}
yield return ConventionUtility.AvoidFakeStop(rootGameObject.ParseFromScriptFile2Expr(rootObject));
int applyDownCount = 0;
void NDFS(ScriptableObject current)
{
foreach (var child in current.Childs)
{
NDFS(child);
}
if (current.IsScriptApply == false)
{
applyDownCount++;
IEnumerator NDFSFoo()
{
yield return current.ApplyScript();
current.ResetEnterGameStatus();
applyDownCount--;
}
ConventionUtility.StartCoroutine(NDFSFoo());
}
}
NDFS(rootGameObject);
yield return new WaitUntil(() => applyDownCount == 0);
projectHash.Clear();
foreach (var path in rootGameObject.LoadedScriptSet)
{
projectHash.Add(path, new ToolFile(path).CalculateHash());
}
// 哈希缓存
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);
}
private IEnumerator LoadFromCompiled(RootObject rootGameObject, ToolFile cacheDir)
{
// 加载
var projectBinaryFile = cacheDir | "project.dat";
using var stream = new FileInfo(projectBinaryFile).OpenRead();
using var reader = new BinaryReader(stream);
rootGameObject.Config.Deserialize(reader);
int applyDownCount = 0;
void NDFS(ScriptableObject current)
{
foreach (var child in current.Childs)
{
NDFS(child);
}
if (current.IsScriptApply == false)
{
applyDownCount++;
IEnumerator NDFSFoo()
{
yield return current.ApplyScript();
current.ResetEnterGameStatus();
applyDownCount--;
}
ConventionUtility.StartCoroutine(NDFSFoo());
}
}
NDFS(rootGameObject);
yield return new WaitUntil(() => applyDownCount == 0);
}
public IEnumerator GameInit() public IEnumerator GameInit()
{ {
float gameInitStartTime = Time.realtimeSinceStartup; float gameInitStartTime = Time.realtimeSinceStartup;
@@ -211,7 +302,8 @@ namespace Demo.Game
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) var scriptFile = new ToolFile(file);
if (scriptFile.Exists() == false || scriptFile.CalculateHash() != md5)
{ {
isRecompile = true; isRecompile = true;
break; break;
@@ -228,91 +320,13 @@ 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)
{ {
static IEnumerator Foo(IEnumerator ir) yield return LoadWithRecompile(rootGameObject, rootObject, projectHash, projectHashFile, cacheDir);
{
Stack<IEnumerator> loadingTask = new();
loadingTask.Push(ir);
while (loadingTask.Count > 0)
{
if (loadingTask.Peek().MoveNext())
{
if (loadingTask.Peek().Current is IEnumerator next)
loadingTask.Push(next);
else if (loadingTask.Peek().Current is ScriptableObject)
yield return null;
} }
else else
{ {
loadingTask.Pop(); yield return LoadFromCompiled(rootGameObject, cacheDir);
}
}
yield break;
}
yield return ConventionUtility.AvoidFakeStop(rootGameObject.ParseFromScriptFile2Expr(rootObject));//Foo(rootGameObject.ParseFromScriptFile2Expr(rootObject));
int applyDownCount = 0;
void NDFS(ScriptableObject current)
{
foreach (var child in current.Childs)
{
NDFS(child);
}
if (current.IsScriptApply == false)
{
applyDownCount++;
IEnumerator NDFSFoo()
{
yield return current.ApplyScript();
current.ResetEnterGameStatus();
applyDownCount--;
}
ConventionUtility.StartCoroutine(NDFSFoo());
}
}
NDFS(rootGameObject);
yield return new WaitUntil(() => applyDownCount == 0);
projectHash.Clear();
foreach (var path in rootGameObject.LoadedScriptSet)
{
projectHash.Add(path, new ToolFile(path).CalculateHash());
}
// 哈希缓存
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
{
// 加载
var projectBinaryFile = cacheDir | "project.dat";
using var stream = new FileInfo(projectBinaryFile).OpenRead();
using var reader = new BinaryReader(stream);
rootGameObject.Config.Deserialize(reader);
int applyDownCount = 0;
void NDFS(ScriptableObject current)
{
foreach (var child in current.Childs)
{
NDFS(child);
}
if (current.IsScriptApply == false)
{
applyDownCount++;
IEnumerator NDFSFoo()
{
yield return current.ApplyScript();
current.ResetEnterGameStatus();
applyDownCount--;
}
ConventionUtility.StartCoroutine(NDFSFoo());
}
}
NDFS(rootGameObject);
yield return new WaitUntil(() => applyDownCount == 0);
} }
float loadRootObjectEndTime = Time.realtimeSinceStartup; float loadRootObjectEndTime = Time.realtimeSinceStartup;
float loadRootObjectElapsed = (loadRootObjectEndTime - loadRootObjectStartTime) * 1000f; float loadRootObjectElapsed = (loadRootObjectEndTime - loadRootObjectStartTime) * 1000f;

View File

@@ -18,7 +18,7 @@ namespace Demo.Game
[Content] public GameController RootGameController; [Content] public GameController RootGameController;
[Setting]public string SourcePath; [Setting] public string SourcePath;
[Header("GlobalConfig")] [Header("GlobalConfig")]
public readonly HashSet<string> LoadedScriptSet = new(); public readonly HashSet<string> LoadedScriptSet = new();
public readonly Dictionary<int, ScriptableObject> LoadedScriptIndex = new(); public readonly Dictionary<int, ScriptableObject> LoadedScriptIndex = new();
@@ -55,7 +55,7 @@ namespace Demo.Game
/// <returns></returns> /// <returns></returns>
public ScriptableObject FindWithIndex(int index) public ScriptableObject FindWithIndex(int index)
{ {
if(index<0) if (index < 0)
return null; return null;
return LoadedScriptIndex[index]; return LoadedScriptIndex[index];
} }

View File

@@ -42,10 +42,11 @@ namespace Demo.Game
[Content] public bool IsSetObjectDisable = false; [Content] public bool IsSetObjectDisable = false;
[Content] public int UpdatePerFrame = 1; [Content] public int UpdatePerFrame = 1;
[Setting] public string ScriptName = "";
private int[] ChildTypes = null; private int[] ChildTypes = null;
private string[] ChildNames = null;
private ScriptLoadableConfig[] childs = null; private ScriptLoadableConfig[] childs = null;
[Setting] public ScriptableObject target; [Setting] public ScriptableObject target;
public virtual void Deserialize(BinaryReader reader) public virtual void Deserialize(BinaryReader reader)
@@ -56,14 +57,13 @@ namespace Demo.Game
EnterGameLocalScaling = BinarySerializeUtility.ReadVec3(reader); EnterGameLocalScaling = BinarySerializeUtility.ReadVec3(reader);
IsSetObjectDisable = BinarySerializeUtility.ReadBool(reader); IsSetObjectDisable = BinarySerializeUtility.ReadBool(reader);
UpdatePerFrame = BinarySerializeUtility.ReadInt(reader); UpdatePerFrame = BinarySerializeUtility.ReadInt(reader);
ScriptName = BinarySerializeUtility.ReadString(reader);
ChildTypes = BinarySerializeUtility.DeserializeIntArray(reader); ChildTypes = BinarySerializeUtility.DeserializeIntArray(reader);
ChildNames = BinarySerializeUtility.DeserializeStringArray(reader);
int childCount = ChildTypes.Length; int childCount = ChildTypes.Length;
childs = new ScriptLoadableConfig[childCount]; childs = new ScriptLoadableConfig[childCount];
for (int i = 0; i < childCount; i++) for (int i = 0; i < childCount; i++)
{ {
var scriptObject = DefaultInstantiate.GetScriptableObjectInstantiate()[DefaultInstantiate.ScriptableObjectID2Typename[ChildTypes[i]]].Invoke(); var scriptObject = target.NewSubScript(DefaultInstantiate.ScriptableObjectID2Typename[ChildTypes[i]], ChildNames[i]);
scriptObject.EnableScript(target);
childs[i] = scriptObject.Config; childs[i] = scriptObject.Config;
childs[i].Deserialize(reader); childs[i].Deserialize(reader);
} }
@@ -76,10 +76,11 @@ namespace Demo.Game
BinarySerializeUtility.WriteVec3(writer, EnterGameLocalScaling); BinarySerializeUtility.WriteVec3(writer, EnterGameLocalScaling);
BinarySerializeUtility.WriteBool(writer, IsSetObjectDisable); BinarySerializeUtility.WriteBool(writer, IsSetObjectDisable);
BinarySerializeUtility.WriteInt(writer, UpdatePerFrame); BinarySerializeUtility.WriteInt(writer, UpdatePerFrame);
BinarySerializeUtility.WriteString(writer, ScriptName);
childs = (from child in target.Childs select child.Config).ToArray(); childs = (from child in target.Childs select child.Config).ToArray();
ChildTypes = (from child in childs select DefaultInstantiate.ScriptableObjectTypename2ID[child.target.GetType().Name]).ToArray(); ChildTypes = (from child in childs select DefaultInstantiate.ScriptableObjectTypename2ID[child.target.GetType().Name]).ToArray();
ChildNames = (from child in childs select child.target.ScriptName).ToArray();
BinarySerializeUtility.SerializeArray(writer, ChildTypes); BinarySerializeUtility.SerializeArray(writer, ChildTypes);
BinarySerializeUtility.SerializeArray(writer, ChildNames);
foreach (var child in childs) foreach (var child in childs)
{ {
child.Serialize(writer); child.Serialize(writer);
@@ -227,11 +228,7 @@ namespace Demo.Game
public static float OneBarTime = 60; public static float OneBarTime = 60;
private bool isEnableScript = false; private bool isEnableScript = false;
public string ScriptName [Setting] public string ScriptName;
{
get => Config.ScriptName;
set => Config.ScriptName = value;
}
private string s_ScriptType = null; private string s_ScriptType = null;
public string m_ScriptType public string m_ScriptType
{ {
@@ -338,7 +335,7 @@ namespace Demo.Game
return result; return result;
} }
public void EnableScript(ScriptableObject parent,int uid = -1) public void EnableScript(ScriptableObject parent)
{ {
if (isEnableScript) if (isEnableScript)
{ {
@@ -360,12 +357,13 @@ namespace Demo.Game
this.name = ScriptName; this.name = ScriptName;
isEnableScript = true; if (Config.UID < 0)
// 只有RootObject的parent会是空的
if (uid < 0)
Config.UID = GetRoot().PushLoadedScriptObject(this); Config.UID = GetRoot().PushLoadedScriptObject(this);
else else
GetRoot().PushLoadedScriptObject(this, uid); GetRoot().PushLoadedScriptObject(this, Config.UID);
isEnableScript = true;
// 只有RootObject的parent会是空的
if (parent != null) if (parent != null)
{ {
MyHierarchyItem = parent.MyHierarchyItem.GetHierarchyItem().CreateSubPropertyItem(1)[0]; MyHierarchyItem = parent.MyHierarchyItem.GetHierarchyItem().CreateSubPropertyItem(1)[0];

View File

@@ -113,7 +113,10 @@ namespace Demo.Game
get get
{ {
if (m_MyMeshRenderer == null) if (m_MyMeshRenderer == null)
{
m_MyMeshRenderer = this.GetOrAddComponent<MeshRenderer>(); m_MyMeshRenderer = this.GetOrAddComponent<MeshRenderer>();
m_MyMeshRenderer.material = StaticCacheDefaultMaterial;
}
return m_MyMeshRenderer; return m_MyMeshRenderer;
} }
} }