Decompiled source of NoStun v0.1.1

plugins/NoStun.dll

Decompiled 3 hours ago
using System;
using System.Diagnostics;
using System.IO;
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;

[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("NoStun")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("2.0.4.0")]
[assembly: AssemblyInformationalVersion("2.0.4")]
[assembly: AssemblyProduct("NoStun")]
[assembly: AssemblyTitle("NoStun")]
[assembly: AssemblyVersion("2.0.4.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;
		}
	}
}
[BepInPlugin("vstguy.nostun", "No Stun", "2.0.4")]
public class NoStun : BaseUnityPlugin
{
	private const int RequiredSteamBuildId = 26407089;

	private const int MaxSteamCheckAttempts = 300;

	internal static ConfigEntry<bool> Enabled;

	internal static ManualLogSource ModLog;

	private static bool versionCheckFinished;

	private static bool targetBuildAllowed;

	private static int steamCheckAttempts;

	private static bool steamApiAssemblyLoadAttempted;

	private static bool noTumbleMemberFound;

	private static PropertyInfo debugNoTumbleProperty;

	private static FieldInfo debugNoTumbleField;

	private static bool setErrorLogged;

	private Harmony harmony;

	private void Awake()
	{
		//IL_003c: Unknown result type (might be due to invalid IL or missing references)
		//IL_0046: Expected O, but got Unknown
		ModLog = ((BaseUnityPlugin)this).Logger;
		Enabled = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "Enabled", true, "Prevents player tumbling/stun. This plugin only activates on R.E.P.O. Steam Build ID 26407089 (v0.5.1).");
		harmony = new Harmony(((BaseUnityPlugin)this).Info.Metadata.GUID);
		harmony.PatchAll();
		((BaseUnityPlugin)this).Logger.LogInfo((object)"No Stun 2.0.4 loaded. Waiting to verify the live Steam build before activating.");
		((BaseUnityPlugin)this).Logger.LogInfo((object)("Required R.E.P.O. Steam Build ID: " + 26407089 + " (v0.5.1)."));
	}

	internal static void OnPlayerControllerUpdate(PlayerController controller)
	{
		if ((Object)(object)controller == (Object)null)
		{
			return;
		}
		if (!versionCheckFinished)
		{
			CheckTargetBuild();
		}
		else
		{
			if (!targetBuildAllowed || Enabled == null || !Enabled.Value)
			{
				return;
			}
			if (!noTumbleMemberFound)
			{
				FindDebugNoTumbleMember();
			}
			if (debugNoTumbleProperty == null && debugNoTumbleField == null)
			{
				return;
			}
			try
			{
				if (debugNoTumbleProperty != null && debugNoTumbleProperty.CanWrite)
				{
					debugNoTumbleProperty.SetValue(controller, true, null);
				}
				else if (debugNoTumbleField != null)
				{
					debugNoTumbleField.SetValue(controller, true);
				}
			}
			catch (Exception ex)
			{
				if (!setErrorLogged)
				{
					setErrorLogged = true;
					ManualLogSource modLog = ModLog;
					if (modLog != null)
					{
						modLog.LogError((object)("Could not set DebugNoTumble: " + ex.Message));
					}
				}
			}
		}
	}

	private static void CheckTargetBuild()
	{
		steamCheckAttempts++;
		if (!TryGetSteamBuildId(out var buildId, out var detail))
		{
			if (steamCheckAttempts >= 300)
			{
				versionCheckFinished = true;
				targetBuildAllowed = false;
				ManualLogSource modLog = ModLog;
				if (modLog != null)
				{
					modLog.LogError((object)("No Stun BLOCKED: could not read the running game's Steam Build ID after " + 300 + " attempts. " + detail));
				}
			}
			return;
		}
		if (buildId == 0)
		{
			if (steamCheckAttempts >= 300)
			{
				versionCheckFinished = true;
				targetBuildAllowed = false;
				ManualLogSource modLog2 = ModLog;
				if (modLog2 != null)
				{
					modLog2.LogError((object)"No Stun BLOCKED: Steam returned Build ID 0. Launch the Steam-installed game to use the v0.5.1-only build.");
				}
			}
			return;
		}
		versionCheckFinished = true;
		targetBuildAllowed = buildId == 26407089;
		if (targetBuildAllowed)
		{
			ManualLogSource modLog3 = ModLog;
			if (modLog3 != null)
			{
				modLog3.LogInfo((object)("Verified R.E.P.O. v0.5.1. Steam Build ID: " + buildId + ". No Stun is active."));
			}
			FindDebugNoTumbleMember();
			if (debugNoTumbleProperty == null && debugNoTumbleField == null)
			{
				ManualLogSource modLog4 = ModLog;
				if (modLog4 != null)
				{
					modLog4.LogError((object)"The v0.5.1 controller does not expose DebugNoTumble. No Stun cannot apply its prevention patch.");
				}
			}
		}
		else
		{
			ManualLogSource modLog5 = ModLog;
			if (modLog5 != null)
			{
				modLog5.LogError((object)("No Stun BLOCKED: detected Steam Build ID " + buildId + "; required " + 26407089 + " (R.E.P.O. v0.5.1). The mod will remain disabled."));
			}
		}
	}

	private static bool TryGetSteamBuildId(out int buildId, out string detail)
	{
		buildId = 0;
		detail = "Facepunch Steamworks SteamApps.BuildId was not available yet.";
		try
		{
			Type type = FindSteamAppsType();
			if (type == null)
			{
				return false;
			}
			PropertyInfo property = type.GetProperty("BuildId", BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic);
			if (property == null)
			{
				detail = "Steamworks.SteamApps.BuildId property was not found.";
				return false;
			}
			object value = property.GetValue(null, null);
			buildId = Convert.ToInt32(value);
			detail = null;
			return true;
		}
		catch (Exception ex)
		{
			detail = "Steam Build ID query is not ready: " + ex.GetBaseException().Message;
			return false;
		}
	}

	private static Type FindSteamAppsType()
	{
		Type type = Type.GetType("Steamworks.SteamApps, Facepunch.Steamworks.Win64", throwOnError: false);
		if (type != null)
		{
			return type;
		}
		Assembly[] assemblies = AppDomain.CurrentDomain.GetAssemblies();
		for (int i = 0; i < assemblies.Length; i++)
		{
			type = assemblies[i].GetType("Steamworks.SteamApps", throwOnError: false);
			if (type != null)
			{
				return type;
			}
		}
		if (!steamApiAssemblyLoadAttempted)
		{
			steamApiAssemblyLoadAttempted = true;
			try
			{
				string text = Path.Combine(Path.GetDirectoryName(typeof(PlayerController).Assembly.Location), "Facepunch.Steamworks.Win64.dll");
				if (File.Exists(text))
				{
					type = Assembly.LoadFrom(text).GetType("Steamworks.SteamApps", throwOnError: false);
					if (type != null)
					{
						return type;
					}
				}
			}
			catch (Exception ex)
			{
				ManualLogSource modLog = ModLog;
				if (modLog != null)
				{
					modLog.LogWarning((object)("Could not load Facepunch Steamworks for build verification: " + ex.GetBaseException().Message));
				}
			}
		}
		return null;
	}

	private static void FindDebugNoTumbleMember()
	{
		if (noTumbleMemberFound)
		{
			return;
		}
		noTumbleMemberFound = true;
		Type type = typeof(PlayerController);
		while (type != null)
		{
			PropertyInfo property = type.GetProperty("DebugNoTumble", BindingFlags.DeclaredOnly | BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
			FieldInfo field = type.GetField("DebugNoTumble", BindingFlags.DeclaredOnly | BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
			if (property != null && property.CanWrite)
			{
				debugNoTumbleProperty = property;
				ManualLogSource modLog = ModLog;
				if (modLog != null)
				{
					modLog.LogInfo((object)("Found writable DebugNoTumble property on " + type.FullName + "."));
				}
				return;
			}
			if (field != null)
			{
				debugNoTumbleField = field;
				ManualLogSource modLog2 = ModLog;
				if (modLog2 != null)
				{
					modLog2.LogInfo((object)("Found DebugNoTumble field on " + type.FullName + "."));
				}
				return;
			}
			type = type.BaseType;
		}
		ManualLogSource modLog3 = ModLog;
		if (modLog3 != null)
		{
			modLog3.LogError((object)"DebugNoTumble was not found on PlayerController or its base classes.");
		}
	}

	private void OnDestroy()
	{
		Harmony obj = harmony;
		if (obj != null)
		{
			obj.UnpatchSelf();
		}
	}
}
[HarmonyPatch(typeof(PlayerController), "Update")]
internal static class PlayerControllerUpdate_NoStunPatch
{
	private static void Prefix(PlayerController __instance)
	{
		NoStun.OnPlayerControllerUpdate(__instance);
	}
}