Decompiled source of Extra Target Locks v1.0.1

ExtraTargetLocks.dll

Decompiled 2 months ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Reflection.Emit;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using System.Security;
using System.Security.Permissions;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using CG.Ship.TargetLocking;
using Gameplay.TacticalTargeting;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using Photon.Pun;
using Photon.Realtime;
using UnityEngine;
using VoidManager;
using VoidManager.CustomGUI;
using VoidManager.MPModChecks;
using VoidManager.ModMessages;
using VoidManager.Utilities;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.Default | DebuggableAttribute.DebuggingModes.DisableOptimizations | DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints | DebuggableAttribute.DebuggingModes.EnableEditAndContinue)]
[assembly: TargetFramework(".NETFramework,Version=v4.7.2", FrameworkDisplayName = ".NET Framework 4.7.2")]
[assembly: AssemblyCompany("ExtraTargetLocks")]
[assembly: AssemblyConfiguration("Debug")]
[assembly: AssemblyDescription("Increases max target locks. All must have.")]
[assembly: AssemblyFileVersion("1.0.1.0")]
[assembly: AssemblyInformationalVersion("1.0.1+e6f0b3efd0d93d49656ff0c449e9ac606445508c")]
[assembly: AssemblyProduct("ExtraTargetLocks")]
[assembly: AssemblyTitle("Increases max target locks. All must have.")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.0.1.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 ExtraTargetLocks
{
	[BepInPlugin("Dragon.ExtraTargetLocks", "Extra Target Locks", "1.0.1")]
	[BepInProcess("Void Crew.exe")]
	[BepInDependency(/*Could not decode attribute arguments.*/)]
	public class BepinPlugin : BaseUnityPlugin
	{
		internal class Bindings
		{
			internal static ConfigEntry<int> MaxTargetLocks;

			internal const int AbsoluteMaxTargetLocks = 26;

			internal static int CachedMaxTargetLocks;
		}

		internal static ManualLogSource Log;

		private void Awake()
		{
			//IL_0030: Unknown result type (might be due to invalid IL or missing references)
			//IL_003a: Expected O, but got Unknown
			Log = ((BaseUnityPlugin)this).Logger;
			Bindings.MaxTargetLocks = ((BaseUnityPlugin)this).Config.Bind<int>("General", "CurrentMaxTargetLocks", 12, new ConfigDescription("The currently active number of max target locks. The host's value is automatically sent to clients and can be changed during runtime.", (AcceptableValueBase)(object)new AcceptableValueRange<int>(4, 26), Array.Empty<object>()));
			Bindings.CachedMaxTargetLocks = Bindings.MaxTargetLocks.Value;
			Harmony.CreateAndPatchAll(Assembly.GetExecutingAssembly(), (string)null);
			((BaseUnityPlugin)this).Logger.LogInfo((object)"Dragon.ExtraTargetLocks is loaded!");
		}
	}
	internal class GUI : ModSettingsMenu
	{
		private string MTLString = string.Empty;

		public override string Name()
		{
			return "Extra Target Locks: " + BepinPlugin.Bindings.CachedMaxTargetLocks;
		}

		public override void Draw()
		{
			if (Game.InGame && !PhotonNetwork.IsMasterClient)
			{
				GUILayout.Label("Must be host to configure. Current setting: " + BepinPlugin.Bindings.CachedMaxTargetLocks, Array.Empty<GUILayoutOption>());
				return;
			}
			GUILayout.Label("Extra Target Locks", Array.Empty<GUILayoutOption>());
			MTLString = GUILayout.TextField(MTLString, Array.Empty<GUILayoutOption>());
			if (int.TryParse(MTLString, out var result) && result >= 4 && result <= 26)
			{
				if (GUILayout.Button("Apply Setting - Current value: " + BepinPlugin.Bindings.CachedMaxTargetLocks, Array.Empty<GUILayoutOption>()))
				{
					BepinPlugin.Bindings.CachedMaxTargetLocks = result;
					BepinPlugin.Bindings.MaxTargetLocks.Value = result;
					if (Patches.CurrentUsedTacticalLock != null)
					{
						Patches.CurrentUsedTacticalLock.LockLimit = result;
					}
					UpdateCurrentMTLMessage.Instance.SendToOthers();
				}
			}
			else
			{
				GUILayout.Label("Cannot Change Setting - Must be a number between 4 and 26.", Array.Empty<GUILayoutOption>());
			}
		}

		public override void OnOpen()
		{
			MTLString = BepinPlugin.Bindings.MaxTargetLocks.Value.ToString();
		}
	}
	public class MyPluginInfo
	{
		public const string PLUGIN_GUID = "Dragon.ExtraTargetLocks";

		public const string PLUGIN_NAME = "ExtraTargetLocks";

		public const string USERS_PLUGIN_NAME = "Extra Target Locks";

		public const string PLUGIN_VERSION = "1.0.1";

		public const string PLUGIN_DESCRIPTION = "Increases max target locks. All must have.";

		public const string PLUGIN_ORIGINAL_AUTHOR = "Dragon";

		public const string PLUGIN_AUTHORS = "Dragon";

		public const string PLUGIN_THUNDERSTORE_ID = "VoidCrewModdingTeam/Extra_Target_Locks";
	}
	internal class Patches
	{
		[HarmonyPatch(typeof(TacticalLock), "AbilityStarted")]
		internal class TacticalLockPatch
		{
			[HarmonyPostfix]
			private static void Patch(TacticalLock __instance)
			{
				CurrentUsedTacticalLock = __instance;
				__instance.LockLimit = BepinPlugin.Bindings.CachedMaxTargetLocks;
			}
		}

		[HarmonyPatch(/*Could not decode attribute arguments.*/)]
		internal class TLCConstructorPatch
		{
			[HarmonyTranspiler]
			private static IEnumerable<CodeInstruction> Patch(IEnumerable<CodeInstruction> instructions)
			{
				return Patchout7(instructions, "Patch");
			}
		}

		[HarmonyPatch(typeof(TargetLockComponent), "Awake")]
		internal class TLCAwakePatch
		{
			[HarmonyTranspiler]
			private static IEnumerable<CodeInstruction> Patch(IEnumerable<CodeInstruction> instructions)
			{
				return Patchout7(instructions, "Patch");
			}
		}

		[HarmonyPatch(typeof(TargetLockComponent), "OnPhotonSerializeView")]
		internal class TLCOnPhotonSerializeViewPatch
		{
			[HarmonyTranspiler]
			private static IEnumerable<CodeInstruction> Patch(IEnumerable<CodeInstruction> instructions)
			{
				bool flag = false;
				foreach (CodeInstruction instruction in instructions)
				{
					if (instruction.opcode == OpCodes.Ldc_I4_7)
					{
						flag = true;
						instruction.opcode = OpCodes.Ldsfld;
						instruction.operand = AccessTools.Field(typeof(BepinPlugin.Bindings), "CachedMaxTargetLocks");
						break;
					}
				}
				if (!flag)
				{
					BepinPlugin.Log.LogError((object)"TargetLockComponent OnPhotonSerializeView Transpiler failed to find and patch target sequence.");
				}
				return instructions;
			}
		}

		[HarmonyPatch(typeof(TargetLockComponent), "TryGetTargetsInView")]
		internal class TLCTryGetTargetsInViewPatch
		{
			[HarmonyTranspiler]
			private static IEnumerable<CodeInstruction> Patch(IEnumerable<CodeInstruction> instructions)
			{
				return Patchout7(instructions, "Patch");
			}
		}

		[HarmonyPatch(typeof(TargetLockComponent), "GetFirstValidMissileTarget")]
		internal class TLCGetFirstValidMissileTargetPatch
		{
			[HarmonyTranspiler]
			private static IEnumerable<CodeInstruction> Patch(IEnumerable<CodeInstruction> instructions)
			{
				return Patchout7(Patchout7(instructions, "Patch"), "Patch");
			}
		}

		[HarmonyPatch(typeof(TargetLockComponent), "IncrementFocusIndex")]
		internal class TLCIncrementFocusIndexPatch
		{
			[HarmonyTranspiler]
			private static IEnumerable<CodeInstruction> Patch(IEnumerable<CodeInstruction> instructions)
			{
				return Patchout7(Patchout7(instructions, "Patch"), "Patch");
			}
		}

		[HarmonyPatch(typeof(TargetLockComponent), "TryClearAll")]
		internal class TLCTryClearAllPatch
		{
			[HarmonyTranspiler]
			private static IEnumerable<CodeInstruction> Patch(IEnumerable<CodeInstruction> instructions)
			{
				return Patchout7(instructions, "Patch");
			}
		}

		internal static TacticalLock CurrentUsedTacticalLock;

		private static IEnumerable<CodeInstruction> Patchout7(IEnumerable<CodeInstruction> instructions, [CallerMemberName] string callerName = "")
		{
			CodeInstruction[] array = instructions.ToArray();
			bool flag = false;
			for (int num = array.Length - 1; num >= 0; num--)
			{
				if (array[num].opcode == OpCodes.Ldc_I4_7)
				{
					array[num].opcode = OpCodes.Ldc_I4;
					array[num].operand = 26;
					flag = true;
					break;
				}
			}
			if (!flag)
			{
				BepinPlugin.Log.LogError((object)("Patchout7() failed to find and patch target sequence for patch: " + callerName));
			}
			return array;
		}
	}
	[HarmonyPatch(typeof(GameSessionManager), "HostGameSession")]
	internal class ResetOnHostPatch
	{
		[HarmonyPrefix]
		private static void Patch()
		{
			BepinPlugin.Bindings.CachedMaxTargetLocks = BepinPlugin.Bindings.MaxTargetLocks.Value;
		}
	}
	internal class UpdateCurrentMTLMessage : ModMessage
	{
		internal static UpdateCurrentMTLMessage Instance;

		public UpdateCurrentMTLMessage()
		{
			Instance = this;
		}

		internal void SendToPlayer(Player player)
		{
			ModMessage.Send("Dragon.ExtraTargetLocks", ((ModMessage)Instance).GetIdentifier(), player, new object[1] { BepinPlugin.Bindings.CachedMaxTargetLocks }, true);
		}

		internal void SendToOthers()
		{
			ModMessage.Send("Dragon.ExtraTargetLocks", ((ModMessage)Instance).GetIdentifier(), (ReceiverGroup)0, new object[1] { BepinPlugin.Bindings.CachedMaxTargetLocks }, true);
		}

		public override void Handle(object[] arguments, Player sender)
		{
			if (sender.IsMasterClient)
			{
				int lockLimit = (BepinPlugin.Bindings.CachedMaxTargetLocks = (int)arguments[0]);
				BepinPlugin.Log.LogInfo((object)("Updating Cached Max Target Locks to " + lockLimit));
				if (Patches.CurrentUsedTacticalLock != null)
				{
					Patches.CurrentUsedTacticalLock.LockLimit = lockLimit;
				}
			}
		}
	}
	public class VoidManagerPlugin : VoidPlugin
	{
		public override MultiplayerType MPType => (MultiplayerType)30;

		public override string Author => "Dragon";

		public override string Description => "Increases max target locks. All must have.";

		public override string ThunderstoreID => "VoidCrewModdingTeam/Extra_Target_Locks";

		public VoidManagerPlugin()
		{
			Events.Instance.HostVerifiedClient += HostVerifiedClient;
		}

		private static void HostVerifiedClient(object source, PlayerEventArgs Player)
		{
			UpdateCurrentMTLMessage.Instance.SendToPlayer(Player.player);
		}
	}
}