Decompiled source of TwoHandedFree v1.2.0

TwoHandedFree.dll

Decompiled a day ago
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using System.Security;
using System.Security.Permissions;
using BepInEx;
using BepInEx.Configuration;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using Unity.Collections;
using Unity.Netcode;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.Default | DebuggableAttribute.DebuggingModes.DisableOptimizations | DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints | DebuggableAttribute.DebuggingModes.EnableEditAndContinue)]
[assembly: TargetFramework(".NETStandard,Version=v2.1", FrameworkDisplayName = ".NET Standard 2.1")]
[assembly: AssemblyCompany("TwoHandedFree")]
[assembly: AssemblyConfiguration("Debug")]
[assembly: AssemblyDescription("Frees up your hands by making two-handed items one-handed.")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("TwoHandedFree")]
[assembly: AssemblyTitle("TwoHandedFree")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.0.0.0")]
[module: UnverifiableCode]
[module: RefSafetyRules(11)]
namespace Microsoft.CodeAnalysis
{
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.Embedded]
	internal sealed class EmbeddedAttribute : Attribute
	{
	}
}
namespace System.Runtime.CompilerServices
{
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.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 TwoHandedFree
{
	[BepInPlugin("YourName.TwoHandedFree", "TwoHandedFree", "1.3.0")]
	public class TwoHandedFreePlugin : BaseUnityPlugin
	{
		[CompilerGenerated]
		private sealed class <HandshakeCoroutine>d__12 : IEnumerator<object>, IEnumerator, IDisposable
		{
			private int <>1__state;

			private object <>2__current;

			public TwoHandedFreePlugin <>4__this;

			private int <attempts>5__1;

			object IEnumerator<object>.Current
			{
				[DebuggerHidden]
				get
				{
					return <>2__current;
				}
			}

			object IEnumerator.Current
			{
				[DebuggerHidden]
				get
				{
					return <>2__current;
				}
			}

			[DebuggerHidden]
			public <HandshakeCoroutine>d__12(int <>1__state)
			{
				this.<>1__state = <>1__state;
			}

			[DebuggerHidden]
			void IDisposable.Dispose()
			{
				<>1__state = -2;
			}

			private bool MoveNext()
			{
				//IL_008b: Unknown result type (might be due to invalid IL or missing references)
				//IL_0095: Expected O, but got Unknown
				switch (<>1__state)
				{
				default:
					return false;
				case 0:
					<>1__state = -1;
					<attempts>5__1 = 0;
					break;
				case 1:
					<>1__state = -1;
					break;
				}
				if (!IsModActive && <attempts>5__1 < 10)
				{
					if ((Object)(object)NetworkManager.Singleton != (Object)null && NetworkManager.Singleton.IsConnectedClient)
					{
						Debug.Log((object)string.Format("[{0}] Sending Handshake Attempt #{1}...", "TwoHandedFree", <attempts>5__1 + 1));
						NetworkHandshake.SendCheckToServer();
					}
					<attempts>5__1++;
					<>2__current = (object)new WaitForSeconds(1.5f);
					<>1__state = 1;
					return true;
				}
				if (!IsModActive)
				{
					Debug.LogWarning((object)"[TwoHandedFree] Failed to sync with Host after 10 attempts. Assuming Vanilla host.");
				}
				return false;
			}

			bool IEnumerator.MoveNext()
			{
				//ILSpy generated this explicit interface implementation from .override directive in MoveNext
				return this.MoveNext();
			}

			[DebuggerHidden]
			void IEnumerator.Reset()
			{
				throw new NotSupportedException();
			}
		}

		private const string modGUID = "YourName.TwoHandedFree";

		private const string modName = "TwoHandedFree";

		private const string modVersion = "1.3.0";

		private readonly Harmony harmony = new Harmony("YourName.TwoHandedFree");

		public static TwoHandedFreePlugin Instance;

		public static ConfigEntry<string> ExcludedItems;

		public static Dictionary<Item, bool> OriginalTwoHandedStates = new Dictionary<Item, bool>();

		public static bool IsModActive = false;

		private void Awake()
		{
			if ((Object)(object)Instance == (Object)null)
			{
				Instance = this;
			}
			ExcludedItems = ((BaseUnityPlugin)this).Config.Bind<string>("General", "ExcludedItems", "", "Items to keep two-handed (comma separated).");
			harmony.PatchAll();
			((BaseUnityPlugin)this).Logger.LogInfo((object)"TwoHandedFree loaded! Waiting for lobby connection...");
		}

		public void EnableModFeatures()
		{
			if (!IsModActive)
			{
				IsModActive = true;
				Debug.Log((object)"[TwoHandedFree] Handshake Success! Enabling Two-Handed features.");
				ItemLogic.ApplyChanges();
			}
		}

		public void DisableModFeatures()
		{
			if (IsModActive)
			{
				IsModActive = false;
				Debug.Log((object)"[TwoHandedFree] Disabling features (Disconnect/Vanilla).");
				ItemLogic.RevertChanges();
			}
		}

		public void StartHandshakeLoop()
		{
			((MonoBehaviour)this).StartCoroutine(HandshakeCoroutine());
		}

		[IteratorStateMachine(typeof(<HandshakeCoroutine>d__12))]
		private IEnumerator HandshakeCoroutine()
		{
			//yield-return decompiler failed: Unexpected instruction in Iterator.Dispose()
			return new <HandshakeCoroutine>d__12(0)
			{
				<>4__this = this
			};
		}
	}
	public static class ItemLogic
	{
		public static void ApplyChanges()
		{
			if ((Object)(object)StartOfRound.Instance == (Object)null)
			{
				return;
			}
			string[] source = (from x in TwoHandedFreePlugin.ExcludedItems.Value.Split(',')
				select x.Trim().ToLower()).ToArray();
			foreach (Item items in StartOfRound.Instance.allItemsList.itemsList)
			{
				if (!TwoHandedFreePlugin.OriginalTwoHandedStates.ContainsKey(items))
				{
					TwoHandedFreePlugin.OriginalTwoHandedStates.Add(items, items.twoHanded);
				}
				if (TwoHandedFreePlugin.OriginalTwoHandedStates[items] && !source.Contains(items.itemName.ToLower()))
				{
					items.twoHanded = false;
				}
			}
		}

		public static void RevertChanges()
		{
			foreach (KeyValuePair<Item, bool> originalTwoHandedState in TwoHandedFreePlugin.OriginalTwoHandedStates)
			{
				originalTwoHandedState.Key.twoHanded = originalTwoHandedState.Value;
			}
			TwoHandedFreePlugin.OriginalTwoHandedStates.Clear();
		}
	}
	[HarmonyPatch(typeof(GameNetworkManager))]
	internal class NetworkHandshake
	{
		[CompilerGenerated]
		private static class <>O
		{
			public static HandleNamedMessageDelegate <0>__OnReceiveCheck;

			public static HandleNamedMessageDelegate <1>__OnReceiveConfirm;

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

		[HarmonyPatch("Start")]
		[HarmonyPostfix]
		private static void RegisterMessages()
		{
			//IL_0027: Unknown result type (might be due to invalid IL or missing references)
			//IL_002c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0032: Expected O, but got Unknown
			//IL_0053: 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_005e: Expected O, but got Unknown
			NetworkManager component = ((Component)GameNetworkManager.Instance).GetComponent<NetworkManager>();
			CustomMessagingManager customMessagingManager = component.CustomMessagingManager;
			object obj = <>O.<0>__OnReceiveCheck;
			if (obj == null)
			{
				HandleNamedMessageDelegate val = OnReceiveCheck;
				<>O.<0>__OnReceiveCheck = val;
				obj = (object)val;
			}
			customMessagingManager.RegisterNamedMessageHandler("TwoHandedFree_Check", (HandleNamedMessageDelegate)obj);
			CustomMessagingManager customMessagingManager2 = component.CustomMessagingManager;
			object obj2 = <>O.<1>__OnReceiveConfirm;
			if (obj2 == null)
			{
				HandleNamedMessageDelegate val2 = OnReceiveConfirm;
				<>O.<1>__OnReceiveConfirm = val2;
				obj2 = (object)val2;
			}
			customMessagingManager2.RegisterNamedMessageHandler("TwoHandedFree_Confirm", (HandleNamedMessageDelegate)obj2);
			component.OnClientConnectedCallback += OnClientConnected;
		}

		[HarmonyPatch("StartDisconnect")]
		[HarmonyPostfix]
		private static void OnDisconnect()
		{
			TwoHandedFreePlugin.Instance.DisableModFeatures();
		}

		private static void OnClientConnected(ulong clientId)
		{
			//IL_005f: Unknown result type (might be due to invalid IL or missing references)
			if (NetworkManager.Singleton.IsHost)
			{
				TwoHandedFreePlugin.Instance.EnableModFeatures();
				Debug.Log((object)$"[{((BaseUnityPlugin)TwoHandedFreePlugin.Instance).Info.Metadata.Name}] Client {clientId} connected. Sending Welcome Packet.");
				FastBufferWriter val = default(FastBufferWriter);
				((FastBufferWriter)(ref val))..ctor(0, (Allocator)2, -1);
				NetworkManager.Singleton.CustomMessagingManager.SendNamedMessage("TwoHandedFree_Confirm", clientId, val, (NetworkDelivery)3);
			}
			else if (clientId == NetworkManager.Singleton.LocalClientId)
			{
				TwoHandedFreePlugin.Instance.StartHandshakeLoop();
			}
		}

		public static void SendCheckToServer()
		{
			//IL_002f: Unknown result type (might be due to invalid IL or missing references)
			if (NetworkManager.Singleton.IsConnectedClient)
			{
				FastBufferWriter val = default(FastBufferWriter);
				((FastBufferWriter)(ref val))..ctor(0, (Allocator)2, -1);
				NetworkManager.Singleton.CustomMessagingManager.SendNamedMessage("TwoHandedFree_Check", 0uL, val, (NetworkDelivery)3);
			}
		}

		private static void OnReceiveCheck(ulong senderId, FastBufferReader reader)
		{
			//IL_002e: Unknown result type (might be due to invalid IL or missing references)
			if (NetworkManager.Singleton.IsHost)
			{
				FastBufferWriter val = default(FastBufferWriter);
				((FastBufferWriter)(ref val))..ctor(0, (Allocator)2, -1);
				NetworkManager.Singleton.CustomMessagingManager.SendNamedMessage("TwoHandedFree_Confirm", senderId, val, (NetworkDelivery)3);
			}
		}

		private static void OnReceiveConfirm(ulong senderId, FastBufferReader reader)
		{
			TwoHandedFreePlugin.Instance.EnableModFeatures();
		}
	}
}