From 64cbe48863bb093cd976aa3ab3476ddfd3003af6 Mon Sep 17 00:00:00 2001 From: minenice55 Date: Mon, 8 Apr 2024 19:52:10 -0400 Subject: [PATCH] (hopefullyt) fix asset loading issue --- Assets/Scripts/Conductor.cs | 2 +- Assets/Scripts/EventCaller.cs | 22 ++++++++++------------ Assets/Scripts/GameManager.cs | 30 ++++++++++++++++++++++++------ Assets/Scripts/Minigames.cs | 8 +++++++- 4 files changed, 42 insertions(+), 20 deletions(-) diff --git a/Assets/Scripts/Conductor.cs b/Assets/Scripts/Conductor.cs index f2c6445dc..4083842e4 100644 --- a/Assets/Scripts/Conductor.cs +++ b/Assets/Scripts/Conductor.cs @@ -171,7 +171,7 @@ namespace HeavenStudio songPosBeat = GetBeatFromSongPos(time); - gameManager.SetCurrentEventToClosest(beat); + gameManager.SetCurrentEventToClosest(beat, true); } public void PlaySetup(double beat) diff --git a/Assets/Scripts/EventCaller.cs b/Assets/Scripts/EventCaller.cs index 29e67d5b4..42cecc80e 100644 --- a/Assets/Scripts/EventCaller.cs +++ b/Assets/Scripts/EventCaller.cs @@ -118,24 +118,22 @@ namespace HeavenStudio public static List GetAllInGameManagerList(string gameName, string[] include) { - List temp1 = instance.gameManager.Beatmap.Entities.FindAll(c => c.datamodel.Split('/')[0] == gameName); - List temp2 = new List(); - foreach (string s in include) + Predicate match = c => { - temp2.AddRange(temp1.FindAll(c => c.datamodel.Split('/')[1].Equals(s))); - } - return temp2; + string[] details = c.datamodel.Split('/'); + return details[0] == gameName && include.Contains(details[1]); + }; + return instance.gameManager.Beatmap.Entities.FindAll(match); } public static List GetAllInGameManagerListExclude(string gameName, string[] exclude) { - List temp1 = instance.gameManager.Beatmap.Entities.FindAll(c => c.datamodel.Split('/')[0] == gameName); - List temp2 = new List(); - foreach (string s in exclude) + Predicate match = c => { - temp2.AddRange(temp1.FindAll(c => !c.datamodel.Split('/')[1].Equals(s))); - } - return temp2; + string[] details = c.datamodel.Split('/'); + return details[0] == gameName && !exclude.Contains(details[1]); + }; + return instance.gameManager.Beatmap.Entities.FindAll(match); } public static List FXOnlyGames() diff --git a/Assets/Scripts/GameManager.cs b/Assets/Scripts/GameManager.cs index a5a7b8bf3..f381ffbdc 100644 --- a/Assets/Scripts/GameManager.cs +++ b/Assets/Scripts/GameManager.cs @@ -22,7 +22,6 @@ namespace HeavenStudio [Header("Lists")] [NonSerialized] public RiqBeatmap Beatmap = new(); - private Dictionary cachedGamePrefabs = new(); [NonSerialized] public ObjectPool SoundObjects; [Header("Components")] @@ -1124,7 +1123,7 @@ namespace HeavenStudio } } - while (beat + 0.25 > Math.Max(conductor.songPositionInBeatsAsDouble, 0)) + while (conductor.GetUnSwungBeat(beat + 0.25) > Math.Max(conductor.unswungSongPositionInBeatsAsDouble, 0)) { if (!conductor.isPlaying) { @@ -1165,13 +1164,18 @@ namespace HeavenStudio public void DestroyGame() { - cachedGamePrefabs.Clear(); SoundByte.UnloadAudioClips(); SetGame("noGame"); } + string currentGameRequest = null; private IEnumerator WaitAndSetGame(string game, bool useMinigameColor = true) { + if (game == currentGameRequest) + { + yield break; + } + currentGameRequest = game; var inf = GetGameInfo(game); if (inf != null && inf.usesAssetBundle) { @@ -1179,13 +1183,15 @@ namespace HeavenStudio { // Debug.Log($"ASYNC loading assetbundles for game {game}"); inf.LoadAssetsAsync().Forget(); + yield return new WaitUntil(() => inf.AssetsLoaded); } - yield return new WaitUntil(() => inf.AssetsLoaded); SetGame(game, useMinigameColor); + currentGameRequest = null; } else { SetGame(game, useMinigameColor); + currentGameRequest = null; } } @@ -1200,10 +1206,14 @@ namespace HeavenStudio public GameObject GetGame(string name) { - var gameInfo = GetGameInfo(name); + if (name is null or "" or "noGame") + { + return Resources.Load($"Games/noGame"); + } + + Minigames.Minigame gameInfo = GetGameInfo(name); if (gameInfo != null) { - GameObject prefab; if (gameInfo.inferred) { return Resources.Load($"Games/noGame"); @@ -1226,6 +1236,8 @@ namespace HeavenStudio return Resources.Load($"Games/noGame"); } } + + GameObject prefab; if (gameInfo.usesAssetBundle) { //game is packed in an assetbundle, load from that instead @@ -1234,6 +1246,11 @@ namespace HeavenStudio try { Debug.LogWarning($"Game prefab wasn't cached, loading from assetbundle for game {name}"); + if (gameInfo.LoadingPrefab) + { + Debug.LogWarning($"Game {name} is already loading, using noGame"); + return Resources.Load($"Games/noGame"); + } return gameInfo.LoadGamePrefab(); } catch (Exception e) @@ -1251,6 +1268,7 @@ namespace HeavenStudio } } } + // games with no assetbundle (usually indev games) prefab = Resources.Load($"Games/{name}"); if (prefab != null) { diff --git a/Assets/Scripts/Minigames.cs b/Assets/Scripts/Minigames.cs index a6d803cbe..55c359190 100644 --- a/Assets/Scripts/Minigames.cs +++ b/Assets/Scripts/Minigames.cs @@ -395,8 +395,10 @@ namespace HeavenStudio public bool usesAssetBundle => wantAssetBundle is not null or ""; public bool hasLocales => supportedLocales.Count > 0; - public bool AssetsLoaded => ((hasLocales && localeLoaded && currentLoadedLocale == defaultLocale) || (!hasLocales)) && commonLoaded && loadComplete; + public bool AssetsLoaded => ((hasLocales && localeLoaded && currentLoadedLocale == defaultLocale) || (!hasLocales)) && commonLoaded && (!loadingPrefab) && loadComplete; public bool AlreadyLoading => alreadyLoading; + public bool LoadingPrefab => loadingPrefab; + public bool SequencesPreloaded => soundSequences != null; public string LoadableName => inferred ? "noGame" : name; public GameObject LoadedPrefab => loadedPrefab; @@ -410,6 +412,7 @@ namespace HeavenStudio private bool localePreloaded = false; private GameObject loadedPrefab = null; + bool loadingPrefab = false; bool loadComplete = false; private SoundSequence.SequenceKeyValue[] soundSequences = null; @@ -510,6 +513,7 @@ namespace HeavenStudio if (alreadyLoading || AssetsLoaded || !usesAssetBundle) return; loadComplete = false; alreadyLoading = true; + loadingPrefab = true; await UniTask.WhenAll(LoadCommonAssetBundleAsync(), LoadLocalizedAssetBundleAsync()); await UniTask.WhenAll(LoadGamePrefabAsync(), LoadCommonAudioClips(), LoadLocalizedAudioClips()); SoundByte.PreloadGameAudioClips(this); @@ -585,6 +589,7 @@ namespace HeavenStudio soundSequences = minigame.SoundSequences; } loadedPrefab = prefab; + loadingPrefab = false; } public GameObject LoadGamePrefab() @@ -636,6 +641,7 @@ namespace HeavenStudio } SoundByte.UnloadAudioClips(name); loadComplete = false; + loadingPrefab = false; } }