Decompiled source of ValheimIntroSkipper v1.0.0

plugins/ValheimIntroSkipper.dll

Decompiled a week ago
using System;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Logging;
using HarmonyLib;
using Microsoft.CodeAnalysis;

[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("LuminairPrime")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyDescription("Skips the publisher logos and the intro cinematic so Valheim reaches the main menu without them.")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0+d204bae4a14e77cd68a7c472fc317d77487ed68a")]
[assembly: AssemblyProduct("ValheimIntroSkipper")]
[assembly: AssemblyTitle("ValheimIntroSkipper")]
[assembly: AssemblyVersion("1.0.0.0")]
[module: RefSafetyRules(11)]
namespace Microsoft.CodeAnalysis
{
	[CompilerGenerated]
	[Embedded]
	internal sealed class EmbeddedAttribute : Attribute
	{
	}
}
namespace System.Runtime.CompilerServices
{
	[CompilerGenerated]
	[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]
	[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]
	[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 ValheimIntroSkipper
{
	[BepInPlugin("LuminairPrime.valheim.introskipper", "Valheim Intro Skipper", "1.0.0")]
	public sealed class ValheimIntroSkipperPlugin : BaseUnityPlugin
	{
		public const string PluginGuid = "LuminairPrime.valheim.introskipper";

		public const string PluginName = "Valheim Intro Skipper";

		public const string PluginVersion = "1.0.0";

		private const string TestedGameVersion = "1.0.12";

		private Harmony? _harmony;

		internal static ManualLogSource Log { get; private set; }

		private void Awake()
		{
			//IL_001b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0025: Expected O, but got Unknown
			Log = ((BaseUnityPlugin)this).Logger;
			IntroCinematicPatch.Resolve();
			LoadingLogosPatch.Resolve();
			_harmony = new Harmony("LuminairPrime.valheim.introskipper");
			try
			{
				_harmony.PatchAll(typeof(ValheimIntroSkipperPlugin).Assembly);
			}
			catch (Exception ex)
			{
				Log.LogWarning((object)("Could not apply one or more startup patches, so part of the intro may still play. A game update probably renamed something. (" + ex.GetType().Name + ": " + ex.Message + ")"));
			}
			string text = ReadGameVersion();
			Log.LogInfo((object)("Valheim Intro Skipper v1.0.0 loaded on Valheim " + (text ?? "an unreadable version") + ". Intro cinematic: " + Outcome(IntroCinematicPatch.Armed) + ", loading logos: " + Outcome(LoadingLogosPatch.Armed) + "."));
			if (text != null && text != "1.0.12")
			{
				Log.LogInfo((object)"Valheim Intro Skipper was built against Valheim 1.0.12. If the intro or the logos come back, re-check CinematicsManager.m_introOnStartup and SceneLoader._showLogos.");
			}
		}

		private static string Outcome(bool armed)
		{
			if (!armed)
			{
				return "left alone";
			}
			return "skipped";
		}

		private static string? ReadGameVersion()
		{
			//IL_0000: Unknown result type (might be due to invalid IL or missing references)
			//IL_0005: Unknown result type (might be due to invalid IL or missing references)
			try
			{
				return ((object)Version.CurrentVersion/*cast due to .constrained prefix*/).ToString();
			}
			catch (Exception ex)
			{
				Log.LogWarning((object)("Could not read the game version: " + ex.Message));
				return null;
			}
		}

		private void OnDestroy()
		{
			Harmony? harmony = _harmony;
			if (harmony != null)
			{
				harmony.UnpatchSelf();
			}
			_harmony = null;
		}
	}
	[HarmonyPatch(typeof(CinematicsManager), "Awake")]
	internal static class IntroCinematicPatch
	{
		private static FieldRef<CinematicsManager, bool>? _introOnStartup;

		internal static bool Armed => _introOnStartup != null;

		internal static void Resolve()
		{
			try
			{
				_introOnStartup = AccessTools.FieldRefAccess<CinematicsManager, bool>("m_introOnStartup");
			}
			catch (Exception ex)
			{
				PatchLog.LogNotFound("(" + ex.GetType().Name + ")", "CinematicsManager.m_introOnStartup", "the intro cinematic will play");
				return;
			}
			if (_introOnStartup == null)
			{
				PatchLog.LogNotFound("", "CinematicsManager.m_introOnStartup", "the intro cinematic will play");
			}
		}

		[HarmonyPostfix]
		private static void Postfix(CinematicsManager __instance)
		{
			if (_introOnStartup == null)
			{
				return;
			}
			try
			{
				_introOnStartup.Invoke(__instance) = false;
			}
			catch (Exception ex)
			{
				ValheimIntroSkipperPlugin.Log.LogWarning((object)("Could not disable the intro cinematic: " + ex.Message));
			}
		}
	}
	[HarmonyPatch(typeof(SceneLoader), "Awake")]
	internal static class LoadingLogosPatch
	{
		private static FieldRef<SceneLoader, bool>? _showLogos;

		internal static bool Armed => _showLogos != null;

		internal static void Resolve()
		{
			try
			{
				_showLogos = AccessTools.FieldRefAccess<SceneLoader, bool>("_showLogos");
			}
			catch (Exception ex)
			{
				PatchLog.LogNotFound("(" + ex.GetType().Name + ")", "SceneLoader._showLogos", "the publisher logos will play");
				return;
			}
			if (_showLogos == null)
			{
				PatchLog.LogNotFound("", "SceneLoader._showLogos", "the publisher logos will play");
			}
		}

		[HarmonyPostfix]
		private static void Postfix(SceneLoader __instance)
		{
			if (_showLogos == null)
			{
				return;
			}
			try
			{
				_showLogos.Invoke(__instance) = false;
			}
			catch (Exception ex)
			{
				ValheimIntroSkipperPlugin.Log.LogWarning((object)("Could not disable the publisher logos: " + ex.Message));
			}
		}
	}
	internal static class PatchLog
	{
		internal static void LogNotFound(string exception, string member, string consequence)
		{
			ValheimIntroSkipperPlugin.Log.LogWarning((object)(member + " was not found, so " + consequence + " as usual. " + ("A game update probably renamed it. " + exception).TrimEnd()));
		}
	}
}