Decompiled source of ValheimSkipIntro v1.0.0

plugins/ValheimSkipIntro/ValheimSkipIntro.dll

Decompiled 18 hours ago
using System;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using BepInEx;
using BepInEx.Configuration;
using HarmonyLib;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: AssemblyTitle("Valheim Skip Intro")]
[assembly: AssemblyFileVersion("0.4.0.0")]
[assembly: AssemblyVersion("0.4.0.0")]
namespace Lesly.ValheimSkipIntro;

[BepInPlugin("lesly.valheim.skipintro", "Valheim Skip Intro", "0.4.0")]
[BepInProcess("valheim.exe")]
public sealed class SkipIntroPlugin : BaseUnityPlugin
{
	[HarmonyPatch(typeof(FejdStartup), "Start")]
	private static class SkipFirstStartupMenuIntro
	{
		private static void Postfix(FejdStartup __instance)
		{
			SkipIntroPlugin instance = Instance;
			if ((Object)(object)instance != (Object)null)
			{
				instance.DisableFirstStartupMenuIntro(__instance);
			}
		}
	}

	[HarmonyPatch(typeof(SceneLoader), "StartLoading")]
	private static class SkipDeveloperLogos
	{
		private static void Prefix(SceneLoader __instance)
		{
			SkipIntroPlugin instance = Instance;
			if ((Object)(object)instance != (Object)null)
			{
				instance.DisableSceneLoaderLogos(__instance);
			}
		}

		private static void Postfix(SceneLoader __instance)
		{
			SkipIntroPlugin instance = Instance;
			if ((Object)(object)instance != (Object)null)
			{
				instance.DisableSceneLoaderLogos(__instance);
			}
		}
	}

	[HarmonyPatch(typeof(SceneLoader), "Update")]
	private static class KeepDeveloperLogosHidden
	{
		private static void Prefix(SceneLoader __instance)
		{
			SkipIntroPlugin instance = Instance;
			if ((Object)(object)instance != (Object)null)
			{
				instance.DisableSceneLoaderLogos(__instance);
			}
		}
	}

	[HarmonyPatch(typeof(CinematicsManager), "Awake")]
	private static class DisableCinematicIntroOnAwake
	{
		private static void Postfix()
		{
			SkipIntroPlugin instance = Instance;
			if ((Object)(object)instance != (Object)null)
			{
				instance.DisableStartupIntroIfReady();
			}
		}
	}

	[HarmonyPatch(typeof(FejdStartup), "PlayIntroCinematic")]
	private static class SkipStartupIntro
	{
		private static void Prefix(FejdStartup __instance)
		{
			SkipIntroPlugin instance = Instance;
			if ((Object)(object)instance != (Object)null)
			{
				instance.DisableStartupIntroIfReady();
				instance.DisableFirstStartupMenuIntro(__instance);
			}
		}
	}

	public const string PluginId = "lesly.valheim.skipintro";

	public const string PluginVersion = "0.4.0";

	internal static SkipIntroPlugin Instance;

	private ConfigEntry<bool> _enabled;

	private ConfigEntry<bool> _hideConsole;

	private Harmony _harmony;

	private bool _consoleHidden;

	private const int HideWindowCommand = 0;

	[DllImport("kernel32.dll")]
	private static extern IntPtr GetConsoleWindow();

	[DllImport("user32.dll")]
	private static extern bool ShowWindow(IntPtr windowHandle, int command);

	private void Awake()
	{
		//IL_005b: Unknown result type (might be due to invalid IL or missing references)
		//IL_0065: Expected O, but got Unknown
		if (Application.isBatchMode)
		{
			return;
		}
		Instance = this;
		_enabled = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "Enabled", true, "Skip Valheim's startup intro cinematic and first-start developer/menu intro.");
		_hideConsole = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "HideBepInExConsole", true, "Hide the BepInEx console window after this plugin loads (Windows only).");
		try
		{
			ValidateHooks();
			_harmony = new Harmony("lesly.valheim.skipintro");
			_harmony.PatchAll(typeof(SkipIntroPlugin).Assembly);
			DisableStartupIntroIfReady();
			HideBepInExConsoleIfRequested();
			((BaseUnityPlugin)this).Logger.LogInfo((object)"Valheim Skip Intro 0.4.0 loaded for Steam build 25185596.");
		}
		catch (Exception ex)
		{
			if (_harmony != null)
			{
				_harmony.UnpatchSelf();
			}
			((BaseUnityPlugin)this).Logger.LogError((object)("Valheim Skip Intro could not install its startup hook and remains inactive. " + ex));
		}
	}

	private void Update()
	{
		DisableStartupIntroIfReady();
		HideBepInExConsoleIfRequested();
	}

	private void HideBepInExConsoleIfRequested()
	{
		//IL_001e: Unknown result type (might be due to invalid IL or missing references)
		//IL_0024: Invalid comparison between Unknown and I4
		if (!_consoleHidden && _hideConsole != null && _hideConsole.Value && (int)Application.platform == 2)
		{
			IntPtr consoleWindow = GetConsoleWindow();
			if (!(consoleWindow == IntPtr.Zero))
			{
				ShowWindow(consoleWindow, 0);
				_consoleHidden = true;
			}
		}
	}

	private void DisableStartupIntroIfReady()
	{
		if (_enabled.Value)
		{
			CinematicsManager s_instance = CinematicsManager.s_instance;
			if ((Object)(object)s_instance != (Object)null)
			{
				s_instance.m_introOnStartup = false;
			}
		}
	}

	private void DisableFirstStartupMenuIntro(FejdStartup startup)
	{
		if (_enabled.Value && !((Object)(object)startup == (Object)null) && (Object)(object)startup.m_menuAnimator != (Object)null)
		{
			startup.m_menuAnimator.SetBool("FirstStartup", false);
		}
	}

	private static void ValidateHooks()
	{
		if (AccessTools.DeclaredMethod(typeof(FejdStartup), "PlayIntroCinematic", Type.EmptyTypes, (Type[])null) == null)
		{
			throw new MissingMethodException(typeof(FejdStartup).FullName, "PlayIntroCinematic");
		}
		if (AccessTools.Field(typeof(CinematicsManager), "m_introOnStartup") == null)
		{
			throw new MissingFieldException(typeof(CinematicsManager).FullName, "m_introOnStartup");
		}
		if (AccessTools.Field(typeof(FejdStartup), "m_menuAnimator") == null)
		{
			throw new MissingFieldException(typeof(FejdStartup).FullName, "m_menuAnimator");
		}
		if (AccessTools.DeclaredMethod(typeof(SceneLoader), "StartLoading", (Type[])null, (Type[])null) == null)
		{
			throw new MissingMethodException(typeof(SceneLoader).FullName, "StartLoading");
		}
		if (AccessTools.DeclaredMethod(typeof(SceneLoader), "Update", (Type[])null, (Type[])null) == null)
		{
			throw new MissingMethodException(typeof(SceneLoader).FullName, "Update");
		}
		if (AccessTools.Field(typeof(SceneLoader), "_showLogos") == null)
		{
			throw new MissingFieldException(typeof(SceneLoader).FullName, "_showLogos");
		}
		if (AccessTools.Field(typeof(SceneLoader), "coffeeStainLogo") == null)
		{
			throw new MissingFieldException(typeof(SceneLoader).FullName, "coffeeStainLogo");
		}
		if (AccessTools.Field(typeof(SceneLoader), "ironGateLogo") == null)
		{
			throw new MissingFieldException(typeof(SceneLoader).FullName, "ironGateLogo");
		}
		if (AccessTools.Field(typeof(SceneLoader), "gameLogo") == null)
		{
			throw new MissingFieldException(typeof(SceneLoader).FullName, "gameLogo");
		}
	}

	private void DisableSceneLoaderLogos(SceneLoader loader)
	{
		if (_enabled.Value && !((Object)(object)loader == (Object)null))
		{
			FieldInfo fieldInfo = AccessTools.Field(typeof(SceneLoader), "_showLogos");
			if (fieldInfo != null)
			{
				fieldInfo.SetValue(loader, false);
			}
			HideSceneLoaderObject(loader, "coffeeStainLogo");
			HideSceneLoaderObject(loader, "ironGateLogo");
			HideSceneLoaderObject(loader, "gameLogo");
		}
	}

	private static void HideSceneLoaderObject(SceneLoader loader, string fieldName)
	{
		FieldInfo fieldInfo = AccessTools.Field(typeof(SceneLoader), fieldName);
		GameObject val = (GameObject)((fieldInfo == null) ? null : /*isinst with value type is only supported in some contexts*/);
		if ((Object)(object)val != (Object)null)
		{
			val.SetActive(false);
		}
	}

	private void OnDestroy()
	{
		if (_harmony != null)
		{
			_harmony.UnpatchSelf();
		}
		if ((Object)(object)Instance == (Object)(object)this)
		{
			Instance = null;
		}
	}
}