Decompiled source of RemoveIntroCinematic v1.1.0

RemoveIntroCinematic.dll

Decompiled 6 hours ago
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Bootstrap;
using BepInEx.Configuration;
using BepInEx.Logging;
using Microsoft.CodeAnalysis;
using On;
using On.RoR2;
using RiskOfOptions;
using RiskOfOptions.OptionConfigs;
using RiskOfOptions.Options;
using RoR2;
using UnityEngine;
using UnityEngine.AddressableAssets;
using UnityEngine.AddressableAssets.ResourceLocators;
using UnityEngine.ResourceManagement.AsyncOperations;
using UnityEngine.ResourceManagement.ResourceLocations;
using UnityEngine.SceneManagement;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: TargetFramework(".NETStandard,Version=v2.1", FrameworkDisplayName = ".NET Standard 2.1")]
[assembly: AssemblyCompany("RemoveIntroCinematic")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.1.0.0")]
[assembly: AssemblyInformationalVersion("1.1.0+d4d565d9eef7909f8cabeaa369f33def7b14b878")]
[assembly: AssemblyProduct("RemoveIntroCinematic")]
[assembly: AssemblyTitle("RemoveIntroCinematic")]
[assembly: AssemblyVersion("1.1.0.0")]
[module: RefSafetyRules(11)]
namespace Microsoft.CodeAnalysis
{
	[CompilerGenerated]
	[Embedded]
	internal sealed class EmbeddedAttribute : Attribute
	{
	}
}
namespace System.Runtime.CompilerServices
{
	[CompilerGenerated]
	[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 RemoveIntroCinematic
{
	[BepInDependency(/*Could not decode attribute arguments.*/)]
	[BepInPlugin("com.elevated.RemoveIntroCinematic", "RemoveIntroCinematic", "1.1.0")]
	public class RemoveIntroCinematicPlugin : BaseUnityPlugin
	{
		[CompilerGenerated]
		private static class <>O
		{
			public static hook_LoadSceneAdditivelyAsync <0>__LoadHelper_LoadSceneAdditivelyAsync;

			public static hook_StartLoadSceneAdditiveAsync <1>__LoadHelper_StartLoadSceneAdditiveAsync;

			public static hook_Finish <2>__SplashScreenController_Finish;

			public static hook_StartIntroMusic <3>__MusicController_StartIntroMusic;

			public static hook_SceneManager_OnLoadNextScene <4>__MusicController_SceneManager_OnLoadNextScene;

			public static hook_SceneCatalog_OnSceneDefChanged <5>__MusicController_SceneCatalog_OnSceneDefChanged;

			public static Action <6>__GoToTitle;
		}

		public const string PluginGUID = "com.elevated.RemoveIntroCinematic";

		public const string PluginName = "RemoveIntroCinematic";

		public const string PluginVersion = "1.1.0";

		private const string SplashSceneGuid = "fe13e00695117284fb0d1b25422a0529";

		private const string IntroSceneGuid = "330d792ae1727574e969e68ce8e966d2";

		private const string SplashSceneName = "splash";

		private const string IntroSceneName = "intro";

		private const string LoadingSceneName = "loadingbasic";

		internal static ManualLogSource Log;

		private static ConfigEntry<bool> skipSplash;

		private static ConfigEntry<bool> skipIntro;

		private static ConfigEntry<bool> skipIntroMusic;

		private static readonly Dictionary<string, string> sceneNameByKey = new Dictionary<string, string>();

		private static FieldInfo haveLoadedSplashField;

		private static bool titleRequested;

		private static bool introPreloadStarted;

		private static bool musicStartPending;

		private static bool ShouldSkipIntro
		{
			get
			{
				if (!skipIntro.Value)
				{
					return IntroCutsceneController.shouldSkip;
				}
				return true;
			}
		}

		public void Awake()
		{
			//IL_0105: Unknown result type (might be due to invalid IL or missing references)
			//IL_010a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0110: Expected O, but got Unknown
			//IL_0125: Unknown result type (might be due to invalid IL or missing references)
			//IL_012a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0130: Expected O, but got Unknown
			//IL_0145: Unknown result type (might be due to invalid IL or missing references)
			//IL_014a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0150: Expected O, but got Unknown
			//IL_0165: Unknown result type (might be due to invalid IL or missing references)
			//IL_016a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0170: Expected O, but got Unknown
			//IL_0185: Unknown result type (might be due to invalid IL or missing references)
			//IL_018a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0190: Expected O, but got Unknown
			//IL_01a5: Unknown result type (might be due to invalid IL or missing references)
			//IL_01aa: Unknown result type (might be due to invalid IL or missing references)
			//IL_01b0: Expected O, but got Unknown
			Log = ((BaseUnityPlugin)this).Logger;
			skipSplash = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "SkipSplashScreen", true, "Skip the developer logo splash screens shown at startup.");
			skipIntro = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "SkipIntroCinematic", true, "Skip the intro cutscene and go straight to the main menu once loading is done.");
			skipIntroMusic = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "SkipIntroMusic", true, "Don't play the intro music during startup. Music starts normally once the main menu is reached.");
			if (Chainloader.PluginInfos.ContainsKey("com.rune580.riskofoptions"))
			{
				RiskOfOptionsCompat.Init(Path.GetDirectoryName(((BaseUnityPlugin)this).Info.Location), skipSplash, skipIntro, skipIntroMusic);
			}
			haveLoadedSplashField = typeof(FadeToBlackManager).GetField("haveLoadedSplash", BindingFlags.Static | BindingFlags.NonPublic);
			if (haveLoadedSplashField == null)
			{
				Log.LogWarning((object)"FadeToBlackManager.haveLoadedSplash not found; screen fades may not work if the splash is skipped.");
			}
			musicStartPending = skipIntroMusic.Value;
			object obj = <>O.<0>__LoadHelper_LoadSceneAdditivelyAsync;
			if (obj == null)
			{
				hook_LoadSceneAdditivelyAsync val = LoadHelper_LoadSceneAdditivelyAsync;
				<>O.<0>__LoadHelper_LoadSceneAdditivelyAsync = val;
				obj = (object)val;
			}
			LoadHelper.LoadSceneAdditivelyAsync += (hook_LoadSceneAdditivelyAsync)obj;
			object obj2 = <>O.<1>__LoadHelper_StartLoadSceneAdditiveAsync;
			if (obj2 == null)
			{
				hook_StartLoadSceneAdditiveAsync val2 = LoadHelper_StartLoadSceneAdditiveAsync;
				<>O.<1>__LoadHelper_StartLoadSceneAdditiveAsync = val2;
				obj2 = (object)val2;
			}
			LoadHelper.StartLoadSceneAdditiveAsync += (hook_StartLoadSceneAdditiveAsync)obj2;
			object obj3 = <>O.<2>__SplashScreenController_Finish;
			if (obj3 == null)
			{
				hook_Finish val3 = SplashScreenController_Finish;
				<>O.<2>__SplashScreenController_Finish = val3;
				obj3 = (object)val3;
			}
			SplashScreenController.Finish += (hook_Finish)obj3;
			object obj4 = <>O.<3>__MusicController_StartIntroMusic;
			if (obj4 == null)
			{
				hook_StartIntroMusic val4 = MusicController_StartIntroMusic;
				<>O.<3>__MusicController_StartIntroMusic = val4;
				obj4 = (object)val4;
			}
			MusicController.StartIntroMusic += (hook_StartIntroMusic)obj4;
			object obj5 = <>O.<4>__MusicController_SceneManager_OnLoadNextScene;
			if (obj5 == null)
			{
				hook_SceneManager_OnLoadNextScene val5 = MusicController_SceneManager_OnLoadNextScene;
				<>O.<4>__MusicController_SceneManager_OnLoadNextScene = val5;
				obj5 = (object)val5;
			}
			MusicController.SceneManager_OnLoadNextScene += (hook_SceneManager_OnLoadNextScene)obj5;
			object obj6 = <>O.<5>__MusicController_SceneCatalog_OnSceneDefChanged;
			if (obj6 == null)
			{
				hook_SceneCatalog_OnSceneDefChanged val6 = MusicController_SceneCatalog_OnSceneDefChanged;
				<>O.<5>__MusicController_SceneCatalog_OnSceneDefChanged = val6;
				obj6 = (object)val6;
			}
			MusicController.SceneCatalog_OnSceneDefChanged += (hook_SceneCatalog_OnSceneDefChanged)obj6;
		}

		public void OnDestroy()
		{
			//IL_0010: Unknown result type (might be due to invalid IL or missing references)
			//IL_0015: Unknown result type (might be due to invalid IL or missing references)
			//IL_001b: Expected O, but got Unknown
			//IL_0030: Unknown result type (might be due to invalid IL or missing references)
			//IL_0035: Unknown result type (might be due to invalid IL or missing references)
			//IL_003b: Expected O, but got Unknown
			//IL_0050: Unknown result type (might be due to invalid IL or missing references)
			//IL_0055: Unknown result type (might be due to invalid IL or missing references)
			//IL_005b: Expected O, but got Unknown
			//IL_0070: Unknown result type (might be due to invalid IL or missing references)
			//IL_0075: Unknown result type (might be due to invalid IL or missing references)
			//IL_007b: Expected O, but got Unknown
			//IL_0090: Unknown result type (might be due to invalid IL or missing references)
			//IL_0095: Unknown result type (might be due to invalid IL or missing references)
			//IL_009b: Expected O, but got Unknown
			//IL_00b0: Unknown result type (might be due to invalid IL or missing references)
			//IL_00b5: Unknown result type (might be due to invalid IL or missing references)
			//IL_00bb: Expected O, but got Unknown
			object obj = <>O.<0>__LoadHelper_LoadSceneAdditivelyAsync;
			if (obj == null)
			{
				hook_LoadSceneAdditivelyAsync val = LoadHelper_LoadSceneAdditivelyAsync;
				<>O.<0>__LoadHelper_LoadSceneAdditivelyAsync = val;
				obj = (object)val;
			}
			LoadHelper.LoadSceneAdditivelyAsync -= (hook_LoadSceneAdditivelyAsync)obj;
			object obj2 = <>O.<1>__LoadHelper_StartLoadSceneAdditiveAsync;
			if (obj2 == null)
			{
				hook_StartLoadSceneAdditiveAsync val2 = LoadHelper_StartLoadSceneAdditiveAsync;
				<>O.<1>__LoadHelper_StartLoadSceneAdditiveAsync = val2;
				obj2 = (object)val2;
			}
			LoadHelper.StartLoadSceneAdditiveAsync -= (hook_StartLoadSceneAdditiveAsync)obj2;
			object obj3 = <>O.<2>__SplashScreenController_Finish;
			if (obj3 == null)
			{
				hook_Finish val3 = SplashScreenController_Finish;
				<>O.<2>__SplashScreenController_Finish = val3;
				obj3 = (object)val3;
			}
			SplashScreenController.Finish -= (hook_Finish)obj3;
			object obj4 = <>O.<3>__MusicController_StartIntroMusic;
			if (obj4 == null)
			{
				hook_StartIntroMusic val4 = MusicController_StartIntroMusic;
				<>O.<3>__MusicController_StartIntroMusic = val4;
				obj4 = (object)val4;
			}
			MusicController.StartIntroMusic -= (hook_StartIntroMusic)obj4;
			object obj5 = <>O.<4>__MusicController_SceneManager_OnLoadNextScene;
			if (obj5 == null)
			{
				hook_SceneManager_OnLoadNextScene val5 = MusicController_SceneManager_OnLoadNextScene;
				<>O.<4>__MusicController_SceneManager_OnLoadNextScene = val5;
				obj5 = (object)val5;
			}
			MusicController.SceneManager_OnLoadNextScene -= (hook_SceneManager_OnLoadNextScene)obj5;
			object obj6 = <>O.<5>__MusicController_SceneCatalog_OnSceneDefChanged;
			if (obj6 == null)
			{
				hook_SceneCatalog_OnSceneDefChanged val6 = MusicController_SceneCatalog_OnSceneDefChanged;
				<>O.<5>__MusicController_SceneCatalog_OnSceneDefChanged = val6;
				obj6 = (object)val6;
			}
			MusicController.SceneCatalog_OnSceneDefChanged -= (hook_SceneCatalog_OnSceneDefChanged)obj6;
		}

		private static IEnumerator LoadHelper_LoadSceneAdditivelyAsync(orig_LoadSceneAdditivelyAsync orig, string sceneKey)
		{
			if (skipSplash.Value && RoR2Application.isLoading && IsScene(sceneKey, "fe13e00695117284fb0d1b25422a0529", "splash"))
			{
				return SkipSplash();
			}
			return orig.Invoke(sceneKey);
		}

		private static AsyncOperationHandle LoadHelper_StartLoadSceneAdditiveAsync(orig_StartLoadSceneAdditiveAsync orig, string sceneKey)
		{
			//IL_0041: Unknown result type (might be due to invalid IL or missing references)
			//IL_0031: Unknown result type (might be due to invalid IL or missing references)
			//IL_0037: Unknown result type (might be due to invalid IL or missing references)
			if (IsScene(sceneKey, "330d792ae1727574e969e68ce8e966d2", "intro"))
			{
				if (ShouldSkipIntro && RoR2Application.isLoading)
				{
					Log.LogInfo((object)"Skipping intro cinematic preload.");
					return default(AsyncOperationHandle);
				}
				introPreloadStarted = true;
			}
			return orig.Invoke(sceneKey);
		}

		private static void SplashScreenController_Finish(orig_Finish orig, SplashScreenController self)
		{
			if (ShouldSkipIntro)
			{
				RequestTitle();
			}
			else
			{
				orig.Invoke(self);
			}
		}

		private static IEnumerator SkipSplash()
		{
			Log.LogInfo((object)"Skipping splash screen.");
			if (!musicStartPending)
			{
				StartIntroMusicNow();
			}
			if (ShouldSkipIntro)
			{
				RequestTitle();
			}
			else
			{
				((MonoBehaviour)RoR2Application.instance).StartCoroutine(PlayIntroAfterPreload());
			}
			yield break;
		}

		private static IEnumerator PlayIntroAfterPreload()
		{
			while (!introPreloadStarted && !RoR2Application.loadFinished)
			{
				yield return null;
			}
			MarkSplashSeen();
			yield return LoadHelper.LoadSceneAdditivelyAsync("330d792ae1727574e969e68ce8e966d2");
		}

		private static void RequestTitle()
		{
			if (!titleRequested)
			{
				titleRequested = true;
				if (RoR2Application.loadFinished)
				{
					GoToTitle();
					return;
				}
				Log.LogInfo((object)"Intro skipped; going to the main menu when loading finishes.");
				RoR2Application.onLoad = (Action)Delegate.Combine(RoR2Application.onLoad, new Action(GoToTitle));
			}
		}

		private static void GoToTitle()
		{
			try
			{
				MarkSplashSeen();
				Console.instance.SubmitCmd((NetworkUser)null, "set_scene title", false);
			}
			catch (Exception arg)
			{
				Log.LogError((object)$"Failed to open the main menu: {arg}");
			}
		}

		private static void MarkSplashSeen()
		{
			haveLoadedSplashField?.SetValue(null, true);
		}

		private static bool IsScene(string key, string guid, string sceneName)
		{
			if (key == guid)
			{
				return true;
			}
			return string.Equals(ResolveSceneName(key), sceneName, StringComparison.OrdinalIgnoreCase);
		}

		private static string ResolveSceneName(string key)
		{
			if (string.IsNullOrEmpty(key))
			{
				return null;
			}
			if (sceneNameByKey.TryGetValue(key, out var value))
			{
				return value;
			}
			string text = null;
			try
			{
				IList<IResourceLocation> list = default(IList<IResourceLocation>);
				foreach (IResourceLocator resourceLocator in Addressables.ResourceLocators)
				{
					if (resourceLocator.Locate((object)key, (Type)null, ref list) && list.Count > 0)
					{
						text = Path.GetFileNameWithoutExtension(list[0].InternalId);
						break;
					}
				}
			}
			catch (Exception ex)
			{
				Log.LogDebug((object)("Could not resolve addressable key " + key + ": " + ex.Message));
			}
			sceneNameByKey[key] = text;
			return text;
		}

		private static void StartIntroMusicNow()
		{
			MusicController instance = MusicController.Instance;
			if (Object.op_Implicit((Object)(object)instance))
			{
				instance.StartIntroMusic();
			}
			else
			{
				musicStartPending = true;
			}
		}

		private static void MusicController_StartIntroMusic(orig_StartIntroMusic orig, MusicController self)
		{
			if (musicStartPending)
			{
				Log.LogInfo((object)"Skipping intro music.");
			}
			else
			{
				orig.Invoke(self);
			}
		}

		private static void MusicController_SceneManager_OnLoadNextScene(orig_SceneManager_OnLoadNextScene orig, MusicController self, Scene scene, LoadSceneMode mode)
		{
			//IL_001d: Unknown result type (might be due to invalid IL or missing references)
			//IL_001e: Unknown result type (might be due to invalid IL or missing references)
			if (!musicStartPending || !(((Scene)(ref scene)).name == "intro"))
			{
				orig.Invoke(self, scene, mode);
			}
		}

		private static void MusicController_SceneCatalog_OnSceneDefChanged(orig_SceneCatalog_OnSceneDefChanged orig, MusicController self, SceneDef sceneDef)
		{
			if (musicStartPending && Object.op_Implicit((Object)(object)sceneDef) && !IsBootScene(sceneDef.baseSceneName))
			{
				musicStartPending = false;
				Log.LogInfo((object)("Starting music system in scene '" + sceneDef.baseSceneName + "'."));
				try
				{
					self.StartIntroMusic();
				}
				catch (Exception arg)
				{
					Log.LogError((object)$"Failed to start the music system: {arg}");
				}
			}
			orig.Invoke(self, sceneDef);
		}

		private static bool IsBootScene(string sceneName)
		{
			if (!(sceneName == "splash") && !(sceneName == "intro"))
			{
				return sceneName == "loadingbasic";
			}
			return true;
		}
	}
	internal static class RiskOfOptionsCompat
	{
		internal const string PluginGUID = "com.rune580.riskofoptions";

		[MethodImpl(MethodImplOptions.NoInlining | MethodImplOptions.NoOptimization)]
		internal static void Init(string pluginDirectory, params ConfigEntry<bool>[] entries)
		{
			//IL_0046: Unknown result type (might be due to invalid IL or missing references)
			//IL_004b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0057: Expected O, but got Unknown
			//IL_0052: Unknown result type (might be due to invalid IL or missing references)
			//IL_0066: Expected O, but got Unknown
			ModSettingsManager.SetModDescription("Skips the developer logos, the intro cinematic and the intro music on startup. Changes take effect the next time the game starts.", "com.elevated.RemoveIntroCinematic", "Remove Intro Cinematic");
			Sprite val = LoadIcon(Path.Combine(pluginDirectory, "icon.png"));
			if (Object.op_Implicit((Object)(object)val))
			{
				ModSettingsManager.SetModIcon(val, "com.elevated.RemoveIntroCinematic", "Remove Intro Cinematic");
			}
			for (int i = 0; i < entries.Length; i++)
			{
				ModSettingsManager.AddOption((BaseOption)new CheckBoxOption(entries[i], new CheckBoxConfig
				{
					restartRequired = true
				}), "com.elevated.RemoveIntroCinematic", "Remove Intro Cinematic");
			}
		}

		private static Sprite LoadIcon(string path)
		{
			//IL_000c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0012: Expected O, but got Unknown
			//IL_003b: Unknown result type (might be due to invalid IL or missing references)
			//IL_004a: Unknown result type (might be due to invalid IL or missing references)
			if (!File.Exists(path))
			{
				return null;
			}
			Texture2D val = new Texture2D(2, 2);
			if (!ImageConversion.LoadImage(val, File.ReadAllBytes(path)))
			{
				return null;
			}
			return Sprite.Create(val, new Rect(0f, 0f, (float)((Texture)val).width, (float)((Texture)val).height), new Vector2(0.5f, 0.5f));
		}
	}
}