继续推动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.Collections;
using System.Collections.Generic;
using System.IO;
using Convention;
using Unity.Cinemachine;
using Unity.VisualScripting;
using UnityEngine;
using UnityEngine.InputSystem;
using UnityEngine.SceneManagement;
@@ -52,6 +51,98 @@ namespace Demo.Game
[Header("Environment")]
[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()
{
float gameInitStartTime = Time.realtimeSinceStartup;
@@ -211,7 +302,8 @@ namespace Demo.Game
projectHash = projectHashFile.LoadAsJson<Dictionary<string, string>>();
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;
break;
@@ -228,91 +320,13 @@ namespace Demo.Game
rootGameObject.SetContent(nameof(SongOffset), SongOffset);
rootGameObject.SetContent(nameof(IsAutoPlay), IsAutoPlay ? 1 : 0);
rootGameObject.SetContent("SongLength", MainAudio.CurrentClip.length);
if (isRecompile || true)
if (isRecompile)
{
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));//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);
yield return LoadWithRecompile(rootGameObject, rootObject, projectHash, projectHashFile, cacheDir);
}
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);
yield return LoadFromCompiled(rootGameObject, cacheDir);
}
float loadRootObjectEndTime = Time.realtimeSinceStartup;
float loadRootObjectElapsed = (loadRootObjectEndTime - loadRootObjectStartTime) * 1000f;