Skip to content

Commit

Permalink
Fix potential weird exceptions on shutdown
Browse files Browse the repository at this point in the history
  • Loading branch information
nicoco007 committed Mar 23, 2024
1 parent 09c8ef8 commit 3f929b6
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
17 changes: 17 additions & 0 deletions BeatSaberMarkupLanguage/BSMLParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
using BeatSaberMarkupLanguage.TypeHandlers;
using BeatSaberMarkupLanguage.Util;
using UnityEngine;
using UnityEngine.AddressableAssets;
using UnityEngine.ResourceManagement.AsyncOperations;
using UnityEngine.ResourceManagement.ResourceProviders;
using Zenject;

namespace BeatSaberMarkupLanguage
Expand Down Expand Up @@ -118,6 +121,11 @@ public BSMLParserParams Parse(string content, GameObject parent, object host = n

public BSMLParserParams Parse(XmlNode parentNode, GameObject parent, object host = null)
{
if (!IsMainMenuSceneLoaded())
{
return null;
}

BSMLParserParams parserParams = new(host);

FieldAccessOption fieldAccessOptions = FieldAccessOption.Auto;
Expand Down Expand Up @@ -450,6 +458,15 @@ private Dictionary<string, string> GetParameters(XmlNode node, Dictionary<string
return parameters;
}

private bool IsMainMenuSceneLoaded()
{
// Unity tends to unload all asset bundles before calling OnDestroy on everything when shutting down.
// Since scenes are now loaded through Addressables, we need to make sure asset bundles haven't been
// unloaded before parsing. Without this check, weird instantiation errors can pop up.
AsyncOperationHandle instance = Addressables.Instance.m_SceneInstances.FirstOrDefault(si => ((SceneInstance)si.Result).Scene.name == "MainMenu");
return instance.IsValid() && ((SceneProvider.SceneOp)instance.m_InternalOp).m_DepOp.Result.Select(op => op.Result).OfType<AssetBundleResource>().All(ab => ab.m_AssetBundle != null);
}

public struct ComponentTypeWithData
{
public TypeHandler typeHandler;
Expand Down
14 changes: 14 additions & 0 deletions BeatSaberMarkupLanguage/BeatSaberMarkupLanguage.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,10 @@
<Private>False</Private>
<Publicize>True</Publicize>
</Reference>
<Reference Include="UnityEngine.AssetBundleModule">
<HintPath>$(BeatSaberDir)\Beat Saber_Data\Managed\UnityEngine.AssetBundleModule.dll</HintPath>
<Private>False</Private>
</Reference>
<Reference Include="UnityEngine.CoreModule">
<HintPath>$(BeatSaberDir)\Beat Saber_Data\Managed\UnityEngine.CoreModule.dll</HintPath>
<Private>False</Private>
Expand Down Expand Up @@ -108,6 +112,16 @@
<HintPath>$(BeatSaberDir)\Beat Saber_Data\Managed\UnityEngine.UnityWebRequestModule.dll</HintPath>
<Private>False</Private>
</Reference>
<Reference Include="Unity.Addressables">
<HintPath>$(BeatSaberDir)\Beat Saber_Data\Managed\Unity.Addressables.dll</HintPath>
<Private>False</Private>
<Publicize>True</Publicize>
</Reference>
<Reference Include="Unity.ResourceManager">
<HintPath>$(BeatSaberDir)\Beat Saber_Data\Managed\Unity.ResourceManager.dll</HintPath>
<Private>False</Private>
<Publicize>True</Publicize>
</Reference>
<Reference Include="VRUI">
<HintPath>$(BeatSaberDir)\Beat Saber_Data\Managed\VRUI.dll</HintPath>
<Private>False</Private>
Expand Down

0 comments on commit 3f929b6

Please sign in to comment.