very messy "already loaded" checks

This commit is contained in:
minenice55 2022-06-12 11:10:41 -04:00
parent a993ac02fc
commit 2722f73976
3 changed files with 79 additions and 51 deletions

View file

@ -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<float> 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). // LateUpdate works a bit better(?) but causes some bugs (like issues with bop animations).
private void Update() private void Update()
{ {
@ -177,45 +220,7 @@ namespace HeavenStudio
float seekTime = 8f; float seekTime = 8f;
//seek ahead to preload games that have assetbundles //seek ahead to preload games that have assetbundles
//check game switches first SeekAheadAndPreload(Conductor.instance.songPositionInBeats, seekTime);
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++;
}
}
if (currentEvent < Beatmap.entities.Count && currentEvent >= 0) if (currentEvent < Beatmap.entities.Count && currentEvent >= 0)
{ {
@ -390,6 +395,8 @@ namespace HeavenStudio
} }
// Debug.Log("currentTempoEvent is now " + currentTempoEvent); // Debug.Log("currentTempoEvent is now " + currentTempoEvent);
} }
SeekAheadAndPreload(beat);
} }
#endregion #endregion

View file

@ -41,7 +41,7 @@ namespace HeavenStudio.Games.Loaders
}, },
new List<string>() {"ntr", "aim"}, new List<string>() {"ntr", "aim"},
"ntrcoin", "en", "ntrcoin", "en",
new List<string>() {"en"} new List<string>() {}
); );
} }
} }

View file

@ -34,10 +34,16 @@ namespace HeavenStudio
public List<string> supportedLocales; public List<string> supportedLocales;
public bool usesAssetBundle => (wantAssetBundle != ""); 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 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<GameAction> actions, List<string> tags = null, string assetBundle = "", string defaultLocale = "en", List<string> supportedLocales = null) public Minigame(string name, string displayName, string color, bool threeD, bool fxOnly, List<GameAction> actions, List<string> tags = null, string assetBundle = "", string defaultLocale = "en", List<string> supportedLocales = null)
{ {
@ -56,59 +62,74 @@ namespace HeavenStudio
public AssetBundle GetLocalizedAssetBundle() public AssetBundle GetLocalizedAssetBundle()
{ {
if (!hasLocales) return null;
if (!usesAssetBundle) return null; if (!usesAssetBundle) return null;
if (bundleLocalized == null || currentLoadedLocale != defaultLocale) //TEMPORARY: use the game's default locale until we add localization support 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 // TODO: try/catch for missing assetbundles
bundleLocalized = AssetBundle.LoadFromFile(Path.Combine(Application.streamingAssetsPath, wantAssetBundle + "/locale." + defaultLocale));
currentLoadedLocale = defaultLocale; currentLoadedLocale = defaultLocale;
bundleLocalized = AssetBundle.LoadFromFile(Path.Combine(Application.streamingAssetsPath, wantAssetBundle + "/locale." + defaultLocale));
localeLoaded = true;
} }
return bundleLocalized; return bundleLocalized;
} }
public AssetBundle GetCommonAssetBundle() public AssetBundle GetCommonAssetBundle()
{ {
if (commonLoaded) return bundleCommon;
if (!usesAssetBundle) return null; if (!usesAssetBundle) return null;
if (bundleCommon == null) if (bundleCommon == null)
{ {
// TODO: try/catch for missing assetbundles // TODO: try/catch for missing assetbundles
bundleCommon = AssetBundle.LoadFromFile(Path.Combine(Application.streamingAssetsPath, wantAssetBundle + "/common")); bundleCommon = AssetBundle.LoadFromFile(Path.Combine(Application.streamingAssetsPath, wantAssetBundle + "/common"));
commonLoaded = true;
} }
return bundleCommon; return bundleCommon;
} }
public IEnumerator LoadCommonAssetBundleAsync() public IEnumerator LoadCommonAssetBundleAsync()
{ {
if (commonPreloaded || commonLoaded) yield break;
commonPreloaded = true;
if (!usesAssetBundle) yield break; if (!usesAssetBundle) yield break;
if (bundleCommon != null) yield break;
if (bundleCommon != null) yield break;
AssetBundleCreateRequest asyncBundleRequest = AssetBundle.LoadFromFileAsync(Path.Combine(Application.streamingAssetsPath, wantAssetBundle + "/common")); AssetBundleCreateRequest asyncBundleRequest = AssetBundle.LoadFromFileAsync(Path.Combine(Application.streamingAssetsPath, wantAssetBundle + "/common"));
yield return asyncBundleRequest;
if (bundleCommon != null) yield break; if (bundleCommon != null) yield break;
yield return asyncBundleRequest;
AssetBundle localAssetBundle = asyncBundleRequest.assetBundle; AssetBundle localAssetBundle = asyncBundleRequest.assetBundle;
yield return localAssetBundle;
if (bundleCommon != null) yield break; if (bundleCommon != null) yield break;
yield return localAssetBundle;
if (localAssetBundle == null) yield break;
bundleCommon = localAssetBundle; bundleCommon = localAssetBundle;
commonLoaded = true;
} }
public IEnumerator LoadLocalizedAssetBundleAsync() public IEnumerator LoadLocalizedAssetBundleAsync()
{ {
if (localePreloaded) yield break;
localePreloaded = true;
if (!hasLocales) yield break;
if (!usesAssetBundle) 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)); AssetBundleCreateRequest asyncBundleRequest = AssetBundle.LoadFromFileAsync(Path.Combine(Application.streamingAssetsPath, wantAssetBundle + "/locale." + defaultLocale));
if (localeLoaded && bundleLocalized != null && currentLoadedLocale == defaultLocale) yield break;
yield return asyncBundleRequest; yield return asyncBundleRequest;
if (bundleLocalized != null) yield break;
AssetBundle localAssetBundle = asyncBundleRequest.assetBundle; AssetBundle localAssetBundle = asyncBundleRequest.assetBundle;
if (localeLoaded && bundleLocalized != null && currentLoadedLocale == defaultLocale) yield break;
yield return localAssetBundle; yield return localAssetBundle;
if (bundleLocalized != null) yield break;
currentLoadedLocale = defaultLocale; if (localAssetBundle == null) yield break;
bundleLocalized = localAssetBundle; bundleLocalized = localAssetBundle;
currentLoadedLocale = defaultLocale;
localeLoaded = true;
} }
} }