From 0f831b8862f9de9458725d8a36e3d3b4e47d4ad9 Mon Sep 17 00:00:00 2001 From: ninemine <1371605831@qq.com> Date: Thu, 18 Dec 2025 00:04:41 +0800 Subject: [PATCH] =?UTF-8?q?=E5=BC=80=E5=A7=8B=E6=9B=B4=E6=96=B0GameControl?= =?UTF-8?q?ler=E5=85=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Framework/GameContent/GameController.cs | 137 +++++++++++------- 1 file changed, 82 insertions(+), 55 deletions(-) diff --git a/Assets/Scripts/Framework/GameContent/GameController.cs b/Assets/Scripts/Framework/GameContent/GameController.cs index 8fc16f0..4f8f324 100644 --- a/Assets/Scripts/Framework/GameContent/GameController.cs +++ b/Assets/Scripts/Framework/GameContent/GameController.cs @@ -200,65 +200,92 @@ namespace Demo.Game var rootFileName = (string)MainConfig.FindItem("root"); var rootObject = new ToolFile(Path.Combine(content.RootSourceDir, rootFileName)); rootObject.MustExistsPath(); - var rootGameObject = new GameObject(rootObject.GetName(true)).AddComponent(); - MainObject = rootGameObject; - rootGameObject.transform.SetParent(transform); - rootGameObject.ScriptName = rootObject.GetName(true); - rootGameObject.audioSystem = MainAudio; - rootGameObject.LoadedScriptSet.Add(rootObject); - rootGameObject.EnableScript(content.RootSourceDir, this); - rootGameObject.SetContent(nameof(SongOffset), SongOffset); - rootGameObject.SetContent(nameof(IsAutoPlay), IsAutoPlay ? 1 : 0); - rootGameObject.SetContent("SongLength", MainAudio.CurrentClip.length); - static IEnumerator Foo(IEnumerator ir) - { - Stack 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 Foo(rootGameObject.ParseFromScriptFile2Expr(rootObject));//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(); - applyDownCount--; - } - ConventionUtility.StartCoroutine(NDFSFoo()); - } - } - NDFS(rootGameObject); - yield return new WaitUntil(() => applyDownCount == 0); + // 缓存MD5与重编译检查 var rootObjectDir = rootObject.GetParentDir(); - ToolFile projectHashFile = rootObjectDir | ".cache" | "projectHash.json"; + ToolFile cacheDir = rootObjectDir | ".cache"; + var projectHashFile = cacheDir | "projectHash.json"; Dictionary projectHash = new(); - foreach (var path in rootGameObject.LoadedScriptSet) + bool isRecompile = true; + if(!!projectHashFile) { - projectHash.Add(path, new ToolFile(path).CalculateHash()); + projectHash = projectHashFile.LoadAsJson>(); + foreach (var (file,md5) in projectHash) + { + if(new ToolFile(file).CalculateHash()!=md5) + { + isRecompile = true; + break; + } + } + } + var rootGameObject = new GameObject(rootObject.GetName(true)).AddComponent(); + MainObject = rootGameObject; + rootGameObject.transform.SetParent(transform); + rootGameObject.ScriptName = rootObject.GetName(true); + rootGameObject.audioSystem = MainAudio; + rootGameObject.LoadedScriptSet.Add(rootObject); + rootGameObject.EnableScript(content.RootSourceDir, this); + rootGameObject.SetContent(nameof(SongOffset), SongOffset); + rootGameObject.SetContent(nameof(IsAutoPlay), IsAutoPlay ? 1 : 0); + rootGameObject.SetContent("SongLength", MainAudio.CurrentClip.length); + if (isRecompile||true) + { + static IEnumerator Foo(IEnumerator ir) + { + Stack 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 Foo(rootGameObject.ParseFromScriptFile2Expr(rootObject));//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(); + applyDownCount--; + } + ConventionUtility.StartCoroutine(NDFSFoo()); + } + } + NDFS(rootGameObject); + yield return new WaitUntil(() => applyDownCount == 0); + foreach (var path in rootGameObject.LoadedScriptSet) + { + projectHash.Add(path, new ToolFile(path).CalculateHash()); + } + projectHashFile.SaveAsJson(projectHash); + } + else + { + var projectBinaryFile = cacheDir | "project.dat"; + using var stream = new FileInfo(projectBinaryFile).OpenRead(); + using var reader = new BinaryReader(stream); + + + } - projectHashFile.SaveAsJson(projectHash); float loadRootObjectEndTime = Time.realtimeSinceStartup; float loadRootObjectElapsed = (loadRootObjectEndTime - loadRootObjectStartTime) * 1000f; Debug.Log($"[GameInit] Load Root Object 耗时: {loadRootObjectElapsed:F2} ms", this);