Decompiled source of SBGIntroSkip v0.2.0

SBGIntroSkip.dll

Decompiled 2 hours ago
using System;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using UnityEngine;
using UnityEngine.Playables;
using UnityEngine.Rendering;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: TargetFramework(".NETFramework,Version=v4.7.2", FrameworkDisplayName = ".NET Framework 4.7.2")]
[assembly: AssemblyVersion("0.0.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 SBGIntroSkip
{
	[BepInPlugin("com.ometavr.sbgintroskip", "SBG Intro Skip", "0.2.0")]
	public sealed class IntroSkipPlugin : BaseUnityPlugin
	{
		public const string PluginGuid = "com.ometavr.sbgintroskip";

		public const string PluginName = "SBG Intro Skip";

		public const string PluginVersion = "0.2.0";

		internal static ManualLogSource Log;

		internal static ConfigEntry<bool> SkipIntro;

		private void Awake()
		{
			//IL_004c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0056: Expected O, but got Unknown
			Log = ((BaseUnityPlugin)this).Logger;
			SkipIntro = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "SkipIntro", true, "Skip the animated intro that plays before the main menu on startup.");
			if (!SkipIntro.Value)
			{
				Log.LogInfo((object)"SkipIntro is off.");
			}
			else if (!IntroPatches.TryPatch(new Harmony("com.ometavr.sbgintroskip")))
			{
				Log.LogError((object)"MainMenu intro hooks not found; this game version is not supported. Intro skip disabled.");
			}
			else
			{
				Log.LogInfo((object)"Loaded.");
			}
		}
	}
	internal static class IntroPatches
	{
		private static readonly FieldRef<MainMenu, PlayableDirector> StartupPlayableDirector = AccessTools.FieldRefAccess<MainMenu, PlayableDirector>("startupPlayableDirector");

		private static readonly FieldRef<MainMenu, GameObject> IntroOverlay = AccessTools.FieldRefAccess<MainMenu, GameObject>("introOverlay");

		private static readonly FieldRef<MainMenu, GameObject> SkipProgress = AccessTools.FieldRefAccess<MainMenu, GameObject>("skipProgress");

		private const float MaxSettingsWaitSeconds = 15f;

		private static float settingsWaitStarted = -1f;

		private static bool skipped;

		internal static bool TryPatch(Harmony harmony)
		{
			//IL_00a4: Unknown result type (might be due to invalid IL or missing references)
			//IL_00b1: Expected O, but got Unknown
			//IL_00c4: Unknown result type (might be due to invalid IL or missing references)
			//IL_00d2: Expected O, but got Unknown
			//IL_00e5: Unknown result type (might be due to invalid IL or missing references)
			//IL_00f3: Expected O, but got Unknown
			MethodInfo[] source = AccessTools.GetDeclaredMethods(typeof(MainMenu)).ToArray();
			MethodInfo methodInfo = source.FirstOrDefault((MethodInfo m) => m.Name.StartsWith("<Start>g__TimeOutIntro|"));
			MethodInfo methodInfo2 = source.FirstOrDefault((MethodInfo m) => m.Name.StartsWith("<Start>g__ShouldSkipIntroInstantly|") && m.ReturnType == typeof(bool));
			MethodInfo methodInfo3 = AccessTools.PropertyGetter(typeof(SplashScreen), "isFinished");
			if (methodInfo == null || methodInfo2 == null || methodInfo3 == null)
			{
				return false;
			}
			harmony.Patch((MethodBase)methodInfo3, (HarmonyMethod)null, new HarmonyMethod(typeof(IntroPatches), "SplashFinishedPostfix", (Type[])null), (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
			harmony.Patch((MethodBase)methodInfo, new HarmonyMethod(typeof(IntroPatches), "TimeOutIntroPrefix", (Type[])null), (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
			harmony.Patch((MethodBase)methodInfo2, new HarmonyMethod(typeof(IntroPatches), "ShouldSkipIntroInstantlyPrefix", (Type[])null), (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
			return true;
		}

		private static void SplashFinishedPostfix(ref bool __result)
		{
			if (__result && !skipped && !GameSettings.IsInitialized)
			{
				if (settingsWaitStarted < 0f)
				{
					settingsWaitStarted = Time.realtimeSinceStartup;
				}
				if (Time.realtimeSinceStartup - settingsWaitStarted < 15f)
				{
					__result = false;
				}
			}
		}

		private static bool TimeOutIntroPrefix(MainMenu __instance)
		{
			if (skipped || !GameSettings.IsInitialized || (Object)(object)__instance == (Object)null)
			{
				return true;
			}
			skipped = true;
			__instance.OnIntroMenuShown();
			PlayableDirector obj = StartupPlayableDirector.Invoke(__instance);
			if (obj != null)
			{
				((Component)obj).gameObject.SetActive(false);
			}
			GameObject obj2 = IntroOverlay.Invoke(__instance);
			if (obj2 != null)
			{
				obj2.SetActive(false);
			}
			GameObject obj3 = SkipProgress.Invoke(__instance);
			if (obj3 != null)
			{
				obj3.SetActive(false);
			}
			IntroSkipPlugin.Log.LogInfo((object)"Skipped main menu intro.");
			return false;
		}

		private static bool ShouldSkipIntroInstantlyPrefix(ref bool __result)
		{
			if (!GameSettings.IsInitialized)
			{
				return true;
			}
			if (!skipped)
			{
				skipped = true;
				IntroSkipPlugin.Log.LogInfo((object)"Skipped main menu intro (fallback).");
			}
			__result = true;
			return false;
		}
	}
}