From 2722f73976b71e25c056cf5c1ac6df20063f7224 Mon Sep 17 00:00:00 2001 From: minenice55 Date: Sun, 12 Jun 2022 11:10:41 -0400 Subject: [PATCH] very messy "already loaded" checks --- Assets/Scripts/GameManager.cs | 85 ++++++++++++----------- Assets/Scripts/Games/CoinToss/CoinToss.cs | 2 +- Assets/Scripts/Minigames.cs | 43 +++++++++--- 3 files changed, 79 insertions(+), 51 deletions(-) diff --git a/Assets/Scripts/GameManager.cs b/Assets/Scripts/GameManager.cs index 5d005b6d1..f4dcdc4c1 100644 --- a/Assets/Scripts/GameManager.cs +++ b/Assets/Scripts/GameManager.cs @@ -152,6 +152,49 @@ namespace HeavenStudio } } + public void SeekAheadAndPreload(float start, float seekTime = 8f) + { + //seek ahead to preload games that have assetbundles + //check game switches first + var gameSwitchs = Beatmap.entities.FindAll(c => c.datamodel.Split(1) == "switchGame"); + if (currentPreSwitch < gameSwitchs.Count && currentPreSwitch >= 0) + { + if (start + seekTime >= gameSwitchs[currentPreSwitch].beat) + { + string gameName = gameSwitchs[currentPreSwitch].datamodel.Split(2); + var inf = GetGameInfo(gameName); + if (inf.usesAssetBundle && !inf.AssetsLoaded) + { + Debug.Log("ASYNC loading assetbundle for game " + gameName); + StartCoroutine(inf.LoadCommonAssetBundleAsync()); + StartCoroutine(inf.LoadLocalizedAssetBundleAsync()); + } + currentPreSwitch++; + } + } + //then check game entities + List entities = Beatmap.entities.Select(c => c.beat).ToList(); + if (currentPreEvent < Beatmap.entities.Count && currentPreEvent >= 0) + { + if (start + seekTime >= entities[currentPreEvent]) + { + var entitiesAtSameBeat = Beatmap.entities.FindAll(c => c.beat == Beatmap.entities[currentPreEvent].beat && !EventCaller.FXOnlyGames().Contains(EventCaller.instance.GetMinigame(c.datamodel.Split('/')[0]))); + for (int i = 0; i < entitiesAtSameBeat.Count; i++) + { + string gameName = entitiesAtSameBeat[i].datamodel.Split('/')[0]; + var inf = GetGameInfo(gameName); + if (inf.usesAssetBundle && !inf.AssetsLoaded) + { + Debug.Log("ASYNC loading assetbundle for game " + gameName); + StartCoroutine(inf.LoadCommonAssetBundleAsync()); + StartCoroutine(inf.LoadLocalizedAssetBundleAsync()); + } + } + currentPreEvent++; + } + } + } + // LateUpdate works a bit better(?) but causes some bugs (like issues with bop animations). private void Update() { @@ -177,45 +220,7 @@ namespace HeavenStudio float seekTime = 8f; //seek ahead to preload games that have assetbundles - //check game switches first - var gameSwitchs = Beatmap.entities.FindAll(c => c.datamodel.Split(1) == "switchGame"); - if (currentPreSwitch < gameSwitchs.Count && currentPreSwitch >= 0) - { - if (Conductor.instance.songPositionInBeats + seekTime >= gameSwitchs[currentPreSwitch].beat) - { - string gameName = gameSwitchs[currentPreSwitch].datamodel.Split(2); - Debug.Log("checking if assetbundle for game " + gameName); - var inf = GetGameInfo(gameName); - if (inf.usesAssetBundle) - { - Debug.Log("ASYNC loading assetbundle for game " + gameName); - StartCoroutine(inf.LoadCommonAssetBundleAsync()); - StartCoroutine(inf.LoadLocalizedAssetBundleAsync()); - } - currentPreSwitch++; - } - } - //then check game entities - if (currentPreEvent < Beatmap.entities.Count && currentPreEvent >= 0) - { - if (Conductor.instance.songPositionInBeats + seekTime >= entities[currentPreEvent]) - { - var entitiesAtSameBeat = Beatmap.entities.FindAll(c => c.beat == Beatmap.entities[currentPreEvent].beat && !EventCaller.FXOnlyGames().Contains(EventCaller.instance.GetMinigame(c.datamodel.Split('/')[0]))); - for (int i = 0; i < entitiesAtSameBeat.Count; i++) - { - string gameName = entitiesAtSameBeat[i].datamodel.Split('/')[0]; - Debug.Log("checking if assetbundle for game " + gameName); - var inf = GetGameInfo(gameName); - if (inf.usesAssetBundle) - { - Debug.Log("ASYNC loading assetbundle for game " + gameName); - StartCoroutine(inf.LoadCommonAssetBundleAsync()); - StartCoroutine(inf.LoadLocalizedAssetBundleAsync()); - } - } - currentPreEvent++; - } - } + SeekAheadAndPreload(Conductor.instance.songPositionInBeats, seekTime); if (currentEvent < Beatmap.entities.Count && currentEvent >= 0) { @@ -390,6 +395,8 @@ namespace HeavenStudio } // Debug.Log("currentTempoEvent is now " + currentTempoEvent); } + + SeekAheadAndPreload(beat); } #endregion diff --git a/Assets/Scripts/Games/CoinToss/CoinToss.cs b/Assets/Scripts/Games/CoinToss/CoinToss.cs index 8cbb662ff..535a310f2 100644 --- a/Assets/Scripts/Games/CoinToss/CoinToss.cs +++ b/Assets/Scripts/Games/CoinToss/CoinToss.cs @@ -41,7 +41,7 @@ namespace HeavenStudio.Games.Loaders }, new List() {"ntr", "aim"}, "ntrcoin", "en", - new List() {"en"} + new List() {} ); } } diff --git a/Assets/Scripts/Minigames.cs b/Assets/Scripts/Minigames.cs index b1e380289..0ae3b5e6b 100644 --- a/Assets/Scripts/Minigames.cs +++ b/Assets/Scripts/Minigames.cs @@ -34,10 +34,16 @@ namespace HeavenStudio public List supportedLocales; public bool usesAssetBundle => (wantAssetBundle != ""); + public bool hasLocales => (supportedLocales.Count > 0); + public bool AssetsLoaded => (((hasLocales && localeLoaded && currentLoadedLocale == defaultLocale) || (!hasLocales)) && commonLoaded); - private AssetBundle bundleCommon; + private AssetBundle bundleCommon = null; + private bool commonLoaded = false; + private bool commonPreloaded = false; private string currentLoadedLocale = ""; - private AssetBundle bundleLocalized; + private AssetBundle bundleLocalized = null; + private bool localeLoaded = false; + private bool localePreloaded = false; public Minigame(string name, string displayName, string color, bool threeD, bool fxOnly, List actions, List tags = null, string assetBundle = "", string defaultLocale = "en", List supportedLocales = null) { @@ -56,59 +62,74 @@ namespace HeavenStudio public AssetBundle GetLocalizedAssetBundle() { + if (!hasLocales) return null; if (!usesAssetBundle) return null; if (bundleLocalized == null || currentLoadedLocale != defaultLocale) //TEMPORARY: use the game's default locale until we add localization support { + if (localeLoaded) return bundleLocalized; // TODO: try/catch for missing assetbundles - bundleLocalized = AssetBundle.LoadFromFile(Path.Combine(Application.streamingAssetsPath, wantAssetBundle + "/locale." + defaultLocale)); currentLoadedLocale = defaultLocale; + bundleLocalized = AssetBundle.LoadFromFile(Path.Combine(Application.streamingAssetsPath, wantAssetBundle + "/locale." + defaultLocale)); + localeLoaded = true; } return bundleLocalized; } public AssetBundle GetCommonAssetBundle() { + if (commonLoaded) return bundleCommon; if (!usesAssetBundle) return null; if (bundleCommon == null) { // TODO: try/catch for missing assetbundles bundleCommon = AssetBundle.LoadFromFile(Path.Combine(Application.streamingAssetsPath, wantAssetBundle + "/common")); + commonLoaded = true; } return bundleCommon; } public IEnumerator LoadCommonAssetBundleAsync() { + if (commonPreloaded || commonLoaded) yield break; + commonPreloaded = true; if (!usesAssetBundle) yield break; + if (bundleCommon != null) yield break; - if (bundleCommon != null) yield break; AssetBundleCreateRequest asyncBundleRequest = AssetBundle.LoadFromFileAsync(Path.Combine(Application.streamingAssetsPath, wantAssetBundle + "/common")); - yield return asyncBundleRequest; if (bundleCommon != null) yield break; + yield return asyncBundleRequest; AssetBundle localAssetBundle = asyncBundleRequest.assetBundle; - yield return localAssetBundle; if (bundleCommon != null) yield break; + yield return localAssetBundle; + + if (localAssetBundle == null) yield break; bundleCommon = localAssetBundle; + commonLoaded = true; } public IEnumerator LoadLocalizedAssetBundleAsync() { + if (localePreloaded) yield break; + localePreloaded = true; + if (!hasLocales) yield break; if (!usesAssetBundle) yield break; - if (currentLoadedLocale == defaultLocale) yield break; + if (localeLoaded && bundleLocalized != null && currentLoadedLocale == defaultLocale) yield break; - if (bundleLocalized != null) yield break; AssetBundleCreateRequest asyncBundleRequest = AssetBundle.LoadFromFileAsync(Path.Combine(Application.streamingAssetsPath, wantAssetBundle + "/locale." + defaultLocale)); + if (localeLoaded && bundleLocalized != null && currentLoadedLocale == defaultLocale) yield break; yield return asyncBundleRequest; - if (bundleLocalized != null) yield break; AssetBundle localAssetBundle = asyncBundleRequest.assetBundle; + if (localeLoaded && bundleLocalized != null && currentLoadedLocale == defaultLocale) yield break; yield return localAssetBundle; - if (bundleLocalized != null) yield break; - currentLoadedLocale = defaultLocale; + if (localAssetBundle == null) yield break; + bundleLocalized = localAssetBundle; + currentLoadedLocale = defaultLocale; + localeLoaded = true; } }