Decompiled source of UnbreakableSurfaces v1.0.2

UnbreakableSurfaces.dll

Decompiled a month ago
using System;
using System.Diagnostics;
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 Unity.Collections;
using Unity.Netcode;
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("UnbreakableSurfaces")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.2.0")]
[assembly: AssemblyInformationalVersion("1.0.2")]
[assembly: AssemblyProduct("UnbreakableSurfaces")]
[assembly: AssemblyTitle("UnbreakableSurfaces")]
[assembly: AssemblyVersion("1.0.2.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 UnbreakableSurfaces
{
	internal static class HostModGate
	{
		[CompilerGenerated]
		private static class <>O
		{
			public static HandleNamedMessageDelegate <0>__OnMessage;

			public static Action<ulong> <1>__OnClientConnected;
		}

		public const string MessageName = "MrGlim.UnbreakableSurfaces";

		private const byte OpHostHello = 0;

		private const byte OpClientSyncRequest = 1;

		private static bool _registered;

		private static bool _clientConnectedHooked;

		private static bool _requestedSync;

		private static bool _clientHostEnabled;

		private static bool _clientBridges = true;

		private static bool _clientSurfaces = true;

		public static bool HostHasMod
		{
			get
			{
				EnsureRegistered();
				NetworkManager singleton = NetworkManager.Singleton;
				if ((Object)(object)singleton == (Object)null)
				{
					return true;
				}
				if (singleton.IsServer || singleton.IsHost)
				{
					if (Plugin.Enabled != null)
					{
						return Plugin.Enabled.Value;
					}
					return false;
				}
				return _clientHostEnabled;
			}
		}

		public static bool FeaturesActive => HostHasMod;

		public static bool BridgesActive
		{
			get
			{
				if (!FeaturesActive)
				{
					return false;
				}
				NetworkManager singleton = NetworkManager.Singleton;
				if ((Object)(object)singleton == (Object)null || singleton.IsServer || singleton.IsHost)
				{
					if (Plugin.Bridges != null)
					{
						return Plugin.Bridges.Value;
					}
					return false;
				}
				return _clientBridges;
			}
		}

		public static bool SurfacesActive
		{
			get
			{
				if (!FeaturesActive)
				{
					return false;
				}
				NetworkManager singleton = NetworkManager.Singleton;
				if ((Object)(object)singleton == (Object)null || singleton.IsServer || singleton.IsHost)
				{
					if (Plugin.BreakableSurfaces != null)
					{
						return Plugin.BreakableSurfaces.Value;
					}
					return false;
				}
				return _clientSurfaces;
			}
		}

		public static void Reset()
		{
			_registered = false;
			_clientConnectedHooked = false;
			_requestedSync = false;
			_clientHostEnabled = false;
			_clientBridges = true;
			_clientSurfaces = true;
		}

		public static void EnsureRegistered()
		{
			//IL_003e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0043: Unknown result type (might be due to invalid IL or missing references)
			//IL_0049: Expected O, but got Unknown
			NetworkManager singleton = NetworkManager.Singleton;
			if ((Object)(object)singleton == (Object)null)
			{
				_registered = false;
				_clientConnectedHooked = false;
				return;
			}
			if (!_registered)
			{
				CustomMessagingManager customMessagingManager = singleton.CustomMessagingManager;
				object obj = <>O.<0>__OnMessage;
				if (obj == null)
				{
					HandleNamedMessageDelegate val = OnMessage;
					<>O.<0>__OnMessage = val;
					obj = (object)val;
				}
				customMessagingManager.RegisterNamedMessageHandler("MrGlim.UnbreakableSurfaces", (HandleNamedMessageDelegate)obj);
				_registered = true;
				Plugin.Log.LogInfo((object)"MrGlim.UnbreakableSurfaces net handler registered.");
			}
			if (singleton.IsServer && !_clientConnectedHooked)
			{
				singleton.OnClientConnectedCallback += OnClientConnected;
				_clientConnectedHooked = true;
			}
			if (!singleton.IsServer && singleton.IsConnectedClient && !_requestedSync)
			{
				_requestedSync = true;
				RequestSync();
			}
		}

		private static void OnClientConnected(ulong clientId)
		{
			NetworkManager singleton = NetworkManager.Singleton;
			if (!((Object)(object)singleton == (Object)null) && singleton.IsServer && clientId != singleton.LocalClientId)
			{
				SendHello(clientId);
			}
		}

		private static void RequestSync()
		{
			//IL_002b: 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_0044: Unknown result type (might be due to invalid IL or missing references)
			NetworkManager singleton = NetworkManager.Singleton;
			if (!((Object)(object)singleton == (Object)null) && !singleton.IsServer)
			{
				FastBufferWriter val = default(FastBufferWriter);
				((FastBufferWriter)(ref val))..ctor(16, (Allocator)2, -1);
				byte b = 1;
				((FastBufferWriter)(ref val)).WriteValueSafe<byte>(ref b, default(ForPrimitives));
				singleton.CustomMessagingManager.SendNamedMessage("MrGlim.UnbreakableSurfaces", 0uL, val, (NetworkDelivery)2);
				((FastBufferWriter)(ref val)).Dispose();
			}
		}

		private static void SendHello(ulong clientId)
		{
			//IL_006b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0071: Unknown result type (might be due to invalid IL or missing references)
			//IL_007e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0084: Unknown result type (might be due to invalid IL or missing references)
			//IL_0091: Unknown result type (might be due to invalid IL or missing references)
			//IL_0097: Unknown result type (might be due to invalid IL or missing references)
			//IL_00a4: Unknown result type (might be due to invalid IL or missing references)
			//IL_00aa: Unknown result type (might be due to invalid IL or missing references)
			//IL_00bd: Unknown result type (might be due to invalid IL or missing references)
			NetworkManager singleton = NetworkManager.Singleton;
			if (!((Object)(object)singleton == (Object)null) && singleton.IsServer)
			{
				bool flag = Plugin.Enabled != null && Plugin.Enabled.Value;
				bool flag2 = Plugin.Bridges != null && Plugin.Bridges.Value;
				bool flag3 = Plugin.BreakableSurfaces != null && Plugin.BreakableSurfaces.Value;
				FastBufferWriter val = default(FastBufferWriter);
				((FastBufferWriter)(ref val))..ctor(32, (Allocator)2, -1);
				byte b = 0;
				((FastBufferWriter)(ref val)).WriteValueSafe<byte>(ref b, default(ForPrimitives));
				((FastBufferWriter)(ref val)).WriteValueSafe<bool>(ref flag, default(ForPrimitives));
				((FastBufferWriter)(ref val)).WriteValueSafe<bool>(ref flag2, default(ForPrimitives));
				((FastBufferWriter)(ref val)).WriteValueSafe<bool>(ref flag3, default(ForPrimitives));
				singleton.CustomMessagingManager.SendNamedMessage("MrGlim.UnbreakableSurfaces", clientId, val, (NetworkDelivery)2);
				((FastBufferWriter)(ref val)).Dispose();
			}
		}

		private static void OnMessage(ulong sender, FastBufferReader reader)
		{
			//IL_0006: Unknown result type (might be due to invalid IL or missing references)
			//IL_000c: Unknown result type (might be due to invalid IL or missing references)
			//IL_003f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0045: Unknown result type (might be due to invalid IL or missing references)
			//IL_0052: Unknown result type (might be due to invalid IL or missing references)
			//IL_0058: Unknown result type (might be due to invalid IL or missing references)
			//IL_0065: Unknown result type (might be due to invalid IL or missing references)
			//IL_006b: Unknown result type (might be due to invalid IL or missing references)
			byte b = default(byte);
			((FastBufferReader)(ref reader)).ReadValueSafe<byte>(ref b, default(ForPrimitives));
			NetworkManager singleton = NetworkManager.Singleton;
			switch (b)
			{
			case 1:
				if ((Object)(object)singleton != (Object)null && singleton.IsServer)
				{
					SendHello(sender);
				}
				break;
			case 0:
			{
				bool flag = default(bool);
				((FastBufferReader)(ref reader)).ReadValueSafe<bool>(ref flag, default(ForPrimitives));
				bool flag2 = default(bool);
				((FastBufferReader)(ref reader)).ReadValueSafe<bool>(ref flag2, default(ForPrimitives));
				bool flag3 = default(bool);
				((FastBufferReader)(ref reader)).ReadValueSafe<bool>(ref flag3, default(ForPrimitives));
				if ((Object)(object)singleton != (Object)null && !singleton.IsServer)
				{
					_clientHostEnabled = flag;
					_clientBridges = flag2;
					_clientSurfaces = flag3;
					Plugin.Log.LogInfo((object)$"Host hello: enabled={flag} bridges={flag2} surfaces={flag3}");
				}
				break;
			}
			}
		}
	}
	[HarmonyPatch(typeof(GameNetworkManager), "Disconnect")]
	internal static class HostModGateDisconnectPatch
	{
		public static void Prefix()
		{
			HostModGate.Reset();
			Plugin.Log.LogInfo((object)"UnbreakableSurfaces gate reset on disconnect.");
		}
	}
	[HarmonyPatch(typeof(BridgeTrigger))]
	internal static class BridgePatches
	{
		private static readonly FieldInfo? HasFallenField;

		private static FieldRef<BridgeTrigger, bool>? _hasFallenRef;

		private static bool _loggedFieldMiss;

		static BridgePatches()
		{
			HasFallenField = AccessTools.Field(typeof(BridgeTrigger), "hasBridgeFallen");
			if (HasFallenField == null)
			{
				return;
			}
			try
			{
				_hasFallenRef = AccessTools.FieldRefAccess<BridgeTrigger, bool>(HasFallenField);
			}
			catch (Exception ex)
			{
				ManualLogSource log = Plugin.Log;
				if (log != null)
				{
					log.LogWarning((object)("FieldRef for hasBridgeFallen failed, will use SetValue: " + ex.Message));
				}
			}
		}

		[HarmonyPatch("BridgeFallServerRpc")]
		[HarmonyPrefix]
		private static bool BlockFallServer()
		{
			return !HostModGate.BridgesActive;
		}

		[HarmonyPatch("BridgeFallClientRpc")]
		[HarmonyPrefix]
		private static bool BlockFallClient()
		{
			return !HostModGate.BridgesActive;
		}

		[HarmonyPatch("LateUpdate")]
		[HarmonyPostfix]
		private static void KeepStanding(BridgeTrigger __instance)
		{
			if (!HostModGate.BridgesActive || (Object)(object)__instance == (Object)null)
			{
				return;
			}
			if (__instance.bridgeDurability < 1f)
			{
				__instance.bridgeDurability = 1f;
			}
			try
			{
				if (_hasFallenRef != null)
				{
					_hasFallenRef.Invoke(__instance) = false;
				}
				else if (HasFallenField != null)
				{
					HasFallenField.SetValue(__instance, false);
				}
				else if (!_loggedFieldMiss)
				{
					_loggedFieldMiss = true;
					Plugin.Log.LogWarning((object)"BridgeTrigger.hasBridgeFallen field not found; fall RPCs are still blocked.");
				}
			}
			catch (Exception arg)
			{
				if (!_loggedFieldMiss)
				{
					_loggedFieldMiss = true;
					Plugin.Log.LogError((object)$"Failed to clear hasBridgeFallen: {arg}");
				}
			}
		}
	}
	[HarmonyPatch(typeof(BridgeTriggerType2))]
	internal static class SurfacePatches
	{
		private static readonly FieldInfo? TimesTriggeredField = AccessTools.Field(typeof(BridgeTriggerType2), "timesTriggered");

		private static readonly FieldInfo? BridgeFellField = AccessTools.Field(typeof(BridgeTriggerType2), "bridgeFell");

		[HarmonyPatch("AddToBridgeInstabilityServerRpc")]
		[HarmonyPrefix]
		private static bool BlockInstability()
		{
			return !HostModGate.SurfacesActive;
		}

		[HarmonyPatch("OnTriggerEnter")]
		[HarmonyPostfix]
		private static void ResetInstability(BridgeTriggerType2 __instance)
		{
			if (!HostModGate.SurfacesActive || (Object)(object)__instance == (Object)null)
			{
				return;
			}
			try
			{
				if (TimesTriggeredField != null)
				{
					TimesTriggeredField.SetValue(__instance, 0);
				}
				else
				{
					__instance.timesTriggered = 0;
				}
				if (BridgeFellField != null)
				{
					BridgeFellField.SetValue(__instance, false);
				}
				else
				{
					__instance.bridgeFell = false;
				}
			}
			catch (Exception ex)
			{
				Plugin.Log.LogError((object)("Failed to reset BridgeTriggerType2: " + ex.Message));
			}
		}
	}
	[HarmonyPatch(typeof(StartOfRound), "Start")]
	internal static class HostModGateStartPatch
	{
		private static void Postfix()
		{
			HostModGate.EnsureRegistered();
		}
	}
	[BepInPlugin("com.benhough.lethal.UnbreakableSurfaces", "UnbreakableSurfaces", "1.0.2")]
	public class Plugin : BaseUnityPlugin
	{
		public const string ModGuid = "com.benhough.lethal.UnbreakableSurfaces";

		public const string ModName = "UnbreakableSurfaces";

		public const string ModVersion = "1.0.2";

		internal static ManualLogSource Log { get; private set; }

		internal static ConfigEntry<bool> Enabled { get; private set; }

		internal static ConfigEntry<bool> Bridges { get; private set; }

		internal static ConfigEntry<bool> BreakableSurfaces { get; private set; }

		private void Awake()
		{
			//IL_0070: Unknown result type (might be due to invalid IL or missing references)
			Log = ((BaseUnityPlugin)this).Logger;
			Enabled = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "Enabled", true, "Master toggle.");
			Bridges = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "Bridges", true, "Vow-style bridges never collapse.");
			BreakableSurfaces = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "BreakableSurfaces", true, "Unstable platforms / type-2 bridges never fall.");
			new Harmony("com.benhough.lethal.UnbreakableSurfaces").PatchAll(typeof(Plugin).Assembly);
			Log.LogInfo((object)"UnbreakableSurfaces v1.0.2 loaded.");
		}
	}
	internal static class PluginInfo
	{
		public const string PLUGIN_GUID = "com.benhough.lethal.UnbreakableSurfaces";

		public const string PLUGIN_NAME = "UnbreakableSurfaces";

		public const string PLUGIN_VERSION = "1.0.2";
	}
}