Decompiled source of R2API SceneAsset v1.1.2

plugins/R2API.SceneAsset/R2API.SceneAsset.dll

Decompiled 11 months ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using System.Security;
using System.Security.Permissions;
using BepInEx;
using BepInEx.Logging;
using Microsoft.CodeAnalysis;
using R2API.AutoVersionGen;
using R2API.MiscHelpers;
using RoR2;
using RoR2.Networking;
using UnityEngine;
using UnityEngine.AddressableAssets;
using UnityEngine.ResourceManagement.AsyncOperations;
using UnityEngine.ResourceManagement.ResourceProviders;
using UnityEngine.SceneManagement;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: TargetFramework(".NETStandard,Version=v2.0", FrameworkDisplayName = ".NET Standard 2.0")]
[assembly: AssemblyCompany("R2API.SceneAsset")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.1.2.0")]
[assembly: AssemblyInformationalVersion("1.1.2+29a853601a15da95e1fda76dd9b3d09c22b2ec84")]
[assembly: AssemblyProduct("R2API.SceneAsset")]
[assembly: AssemblyTitle("R2API.SceneAsset")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.1.2.0")]
[module: UnverifiableCode]
[module: RefSafetyRules(11)]
namespace Microsoft.CodeAnalysis
{
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.Embedded]
	internal sealed class EmbeddedAttribute : Attribute
	{
	}
}
namespace System.Runtime.CompilerServices
{
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.Embedded]
	[AttributeUsage(AttributeTargets.Class | AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Event | AttributeTargets.Parameter | AttributeTargets.ReturnValue | AttributeTargets.GenericParameter, AllowMultiple = false, Inherited = false)]
	internal sealed class NullableAttribute : Attribute
	{
		public readonly byte[] NullableFlags;

		public NullableAttribute(byte P_0)
		{
			NullableFlags = new byte[1] { P_0 };
		}

		public NullableAttribute(byte[] P_0)
		{
			NullableFlags = P_0;
		}
	}
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.Embedded]
	[AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Method | AttributeTargets.Interface | AttributeTargets.Delegate, AllowMultiple = false, Inherited = false)]
	internal sealed class NullableContextAttribute : Attribute
	{
		public readonly byte Flag;

		public NullableContextAttribute(byte P_0)
		{
			Flag = P_0;
		}
	}
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.Embedded]
	[AttributeUsage(AttributeTargets.Module, AllowMultiple = false, Inherited = false)]
	internal sealed class RefSafetyRulesAttribute : Attribute
	{
		public readonly int Version;

		public RefSafetyRulesAttribute(int P_0)
		{
			Version = P_0;
		}
	}
}
namespace System.Diagnostics.CodeAnalysis
{
	[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Parameter, Inherited = false)]
	[ExcludeFromCodeCoverage]
	[DebuggerNonUserCode]
	internal sealed class AllowNullAttribute : Attribute
	{
	}
	[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Parameter, Inherited = false)]
	[ExcludeFromCodeCoverage]
	[DebuggerNonUserCode]
	internal sealed class DisallowNullAttribute : Attribute
	{
	}
	[AttributeUsage(AttributeTargets.Method, Inherited = false)]
	[ExcludeFromCodeCoverage]
	[DebuggerNonUserCode]
	internal sealed class DoesNotReturnAttribute : Attribute
	{
	}
	[AttributeUsage(AttributeTargets.Parameter, Inherited = false)]
	[ExcludeFromCodeCoverage]
	[DebuggerNonUserCode]
	internal sealed class DoesNotReturnIfAttribute : Attribute
	{
		public bool ParameterValue { get; }

		public DoesNotReturnIfAttribute(bool parameterValue)
		{
			ParameterValue = parameterValue;
		}
	}
	[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Parameter | AttributeTargets.ReturnValue, Inherited = false)]
	[ExcludeFromCodeCoverage]
	[DebuggerNonUserCode]
	internal sealed class MaybeNullAttribute : Attribute
	{
	}
	[AttributeUsage(AttributeTargets.Parameter, Inherited = false)]
	[ExcludeFromCodeCoverage]
	[DebuggerNonUserCode]
	internal sealed class MaybeNullWhenAttribute : Attribute
	{
		public bool ReturnValue { get; }

		public MaybeNullWhenAttribute(bool returnValue)
		{
			ReturnValue = returnValue;
		}
	}
	[AttributeUsage(AttributeTargets.Method | AttributeTargets.Property, Inherited = false, AllowMultiple = true)]
	[ExcludeFromCodeCoverage]
	[DebuggerNonUserCode]
	internal sealed class MemberNotNullAttribute : Attribute
	{
		public string[] Members { get; }

		public MemberNotNullAttribute(string member)
		{
			Members = new string[1] { member };
		}

		public MemberNotNullAttribute(params string[] members)
		{
			Members = members;
		}
	}
	[AttributeUsage(AttributeTargets.Method | AttributeTargets.Property, Inherited = false, AllowMultiple = true)]
	[ExcludeFromCodeCoverage]
	[DebuggerNonUserCode]
	internal sealed class MemberNotNullWhenAttribute : Attribute
	{
		public bool ReturnValue { get; }

		public string[] Members { get; }

		public MemberNotNullWhenAttribute(bool returnValue, string member)
		{
			ReturnValue = returnValue;
			Members = new string[1] { member };
		}

		public MemberNotNullWhenAttribute(bool returnValue, params string[] members)
		{
			ReturnValue = returnValue;
			Members = members;
		}
	}
	[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Parameter | AttributeTargets.ReturnValue, Inherited = false)]
	[ExcludeFromCodeCoverage]
	[DebuggerNonUserCode]
	internal sealed class NotNullAttribute : Attribute
	{
	}
	[AttributeUsage(AttributeTargets.Property | AttributeTargets.Parameter | AttributeTargets.ReturnValue, AllowMultiple = true, Inherited = false)]
	[ExcludeFromCodeCoverage]
	[DebuggerNonUserCode]
	internal sealed class NotNullIfNotNullAttribute : Attribute
	{
		public string ParameterName { get; }

		public NotNullIfNotNullAttribute(string parameterName)
		{
			ParameterName = parameterName;
		}
	}
	[AttributeUsage(AttributeTargets.Parameter, Inherited = false)]
	[ExcludeFromCodeCoverage]
	[DebuggerNonUserCode]
	internal sealed class NotNullWhenAttribute : Attribute
	{
		public bool ReturnValue { get; }

		public NotNullWhenAttribute(bool returnValue)
		{
			ReturnValue = returnValue;
		}
	}
}
namespace R2API
{
	[AutoVersion]
	public static class SceneAssetAPI
	{
		public const string PluginGUID = "com.bepis.r2api.sceneasset";

		public const string PluginName = "R2API.SceneAsset";

		private static readonly Dictionary<string, Action<GameObject[]>> SceneNameToAssetRequests = new Dictionary<string, Action<GameObject[]>>();

		private static bool _hooksEnabled = false;

		private static bool _requestsDone = false;

		public const string PluginVersion = "1.1.2";

		[Obsolete("All submodules are automatically loaded and this property is now unused")]
		public static bool Loaded => true;

		internal static void SetHooks()
		{
			if (!_hooksEnabled)
			{
				SceneManager.sceneLoaded += SceneManager_sceneLoaded;
				_hooksEnabled = true;
			}
		}

		private static void SceneManager_sceneLoaded(Scene arg0, LoadSceneMode arg1)
		{
			if (!_requestsDone && ((ResourceAvailability)(ref SceneCatalog.availability)).available && ((Scene)(ref arg0)).name != "loadingbasic" && ((Scene)(ref arg0)).name != "splash" && ((Scene)(ref arg0)).name != "intro")
			{
				PrepareRequests();
				_requestsDone = true;
			}
		}

		internal static void UnsetHooks()
		{
			_hooksEnabled = false;
		}

		private static void PrepareRequests()
		{
			//IL_008b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0090: Unknown result type (might be due to invalid IL or missing references)
			string text = default(string);
			Action<GameObject[]> action = default(Action<GameObject[]>);
			foreach (KeyValuePair<string, Action<GameObject[]>> sceneNameToAssetRequest in SceneNameToAssetRequests)
			{
				KeyValuePairExtensions.Deconstruct<string, Action<GameObject[]>>(sceneNameToAssetRequest, ref text, ref action);
				string sceneName = text;
				Action<GameObject[]> actionList = action;
				try
				{
					SceneDef val = SceneCatalog.FindSceneDef(sceneName);
					if (Object.op_Implicit((Object)(object)val))
					{
						AssetReferenceScene sceneAddress = val.sceneAddress;
						string text2 = ((sceneAddress != null) ? ((AssetReference)sceneAddress).AssetGUID : null);
						if (!string.IsNullOrEmpty(text2))
						{
							if (NetworkManagerSystem.IsAddressablesKeyValid(text2, typeof(SceneInstance)))
							{
								AsyncOperationHandle<SceneInstance> val2 = Addressables.LoadSceneAsync((object)text2, (LoadSceneMode)1, true, 100);
								val2.Completed += delegate(AsyncOperationHandle<SceneInstance> handle)
								{
									//IL_000e: Unknown result type (might be due to invalid IL or missing references)
									ExecuteAddressableRequest(sceneName, actionList, handle.Result);
								};
								SceneAssetPlugin.Logger.LogInfo((object)("Loaded the scene " + sceneName + " through Addressables.LoadSceneAsync"));
							}
							else
							{
								SceneAssetPlugin.Logger.LogError((object)$"Addressable key Scene address is invalid for sceneName {sceneName} | {sceneAddress}");
							}
						}
						else
						{
							SceneManager.LoadSceneAsync(sceneName, (LoadSceneMode)1).completed += delegate
							{
								ExecuteRequest(sceneName, actionList);
							};
							SceneAssetPlugin.Logger.LogInfo((object)("Loaded the scene " + sceneName + " through SceneManager.LoadScene"));
						}
						continue;
					}
					SceneAssetPlugin.Logger.LogError((object)(sceneName + " doesnt exist, available scene names:"));
					foreach (KeyValuePair<string, SceneIndex> item in SceneCatalog.nameToIndex)
					{
						SceneAssetPlugin.Logger.LogError((object)(item.Key ?? ""));
					}
				}
				catch (Exception arg)
				{
					SceneAssetPlugin.Logger.LogError((object)$"Exception in ExecuteRequests : {arg}");
				}
			}
		}

		private static void ExecuteAddressableRequest(string sceneName, Action<GameObject[]> actionList, SceneInstance sceneInstance)
		{
			//IL_0002: Unknown result type (might be due to invalid IL or missing references)
			//IL_0007: Unknown result type (might be due to invalid IL or missing references)
			//IL_0009: Unknown result type (might be due to invalid IL or missing references)
			//IL_000f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0011: Unknown result type (might be due to invalid IL or missing references)
			Scene scene = ((SceneInstance)(ref sceneInstance)).Scene;
			ExecuteRequestInternal(actionList, scene);
			Addressables.UnloadSceneAsync(sceneInstance, true);
		}

		private static void ExecuteRequest(string sceneName, Action<GameObject[]> actionList)
		{
			//IL_0001: Unknown result type (might be due to invalid IL or missing references)
			//IL_0006: Unknown result type (might be due to invalid IL or missing references)
			//IL_0008: Unknown result type (might be due to invalid IL or missing references)
			Scene sceneByName = SceneManager.GetSceneByName(sceneName);
			ExecuteRequestInternal(actionList, sceneByName);
			SceneManager.UnloadSceneAsync(sceneName);
		}

		private static void ExecuteRequestInternal(Action<GameObject[]> actionList, Scene scene)
		{
			GameObject[] rootGameObjects = ((Scene)(ref scene)).GetRootGameObjects();
			Delegate[] invocationList = actionList.GetInvocationList();
			for (int i = 0; i < invocationList.Length; i++)
			{
				Action<GameObject[]> action = (Action<GameObject[]>)invocationList[i];
				try
				{
					action(rootGameObjects);
				}
				catch (Exception ex)
				{
					SceneAssetPlugin.Logger.LogError((object)ex);
				}
			}
		}

		public static void AddAssetRequest(string? sceneName, Action<GameObject[]>? onSceneObjectsLoaded)
		{
			SetHooks();
			if (SceneNameToAssetRequests.TryGetValue(sceneName, out Action<GameObject[]> _))
			{
				Dictionary<string, Action<GameObject[]>> sceneNameToAssetRequests = SceneNameToAssetRequests;
				sceneNameToAssetRequests[sceneName] = (Action<GameObject[]>)Delegate.Combine(sceneNameToAssetRequests[sceneName], onSceneObjectsLoaded);
			}
			else
			{
				SceneNameToAssetRequests[sceneName] = onSceneObjectsLoaded;
			}
		}
	}
	[BepInPlugin("com.bepis.r2api.sceneasset", "R2API.SceneAsset", "1.1.2")]
	public sealed class SceneAssetPlugin : BaseUnityPlugin
	{
		internal static ManualLogSource Logger { get; set; }

		private void Awake()
		{
			Logger = ((BaseUnityPlugin)this).Logger;
		}

		private void OnDestroy()
		{
			SceneAssetAPI.UnsetHooks();
		}
	}
}
namespace R2API.AutoVersionGen
{
	[AttributeUsage(AttributeTargets.Class, Inherited = false, AllowMultiple = false)]
	internal class AutoVersionAttribute : Attribute
	{
	}
}