Decompiled source of ShipTurrets v1.0.6

ShipTurrets.dll

Decompiled 4 months ago
using System;
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 BepInEx.Logging;
using GameNetcodeStuff;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using ShipTurrets.Patches;
using TerminalApi;
using TerminalApi.Classes;
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("ShipTurrets")]
[assembly: AssemblyConfiguration("Debug")]
[assembly: AssemblyDescription("Plugin that spawns two turrets on the front and rear of the player ship that targets and neutralises enemies instead of players.")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("ShipTurrets")]
[assembly: AssemblyTitle("ShipTurrets")]
[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 ShipTurrets
{
	[BepInPlugin("ShipTurrets", "ShipTurrets", "1.0.0")]
	public class Plugin : BaseUnityPlugin
	{
		public static ManualLogSource logger;

		public static ConfigEntry<bool> isFrontTurretSpawned;

		public static ConfigEntry<bool> isRearTurretSpawned;

		public static ConfigEntry<bool> isTurretFF;

		public static ConfigEntry<bool> isTerminalHostBuy;

		private readonly Harmony harmony = new Harmony("ShipTurrets");

		private void Awake()
		{
			isFrontTurretSpawned = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "Ship front turret spawn", true, "Is the front turret spawned onto the player ship");
			isRearTurretSpawned = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "Ship rear turret spawn", true, "Is the rear turret spawned onto the player ship");
			isTurretFF = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "Ship turret friendly fire", false, "Is the turret friendly fire on (it shoots players & enemies)");
			isTerminalHostBuy = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "Terminal Host Turret Buying", false, "Does the host have to buy front and rear turrets before they are enabled. (Disable front and rear turrets otherwise they will already be bought)");
			((BaseUnityPlugin)this).Logger.LogInfo((object)"Plugin ShipTurrets is loaded successfully");
			logger = ((BaseUnityPlugin)this).Logger;
			harmony.PatchAll(typeof(Plugin));
			harmony.PatchAll(typeof(RoundManagerPatch));
			harmony.PatchAll(typeof(TurretPatch));
			harmony.PatchAll(typeof(TerminalPatch));
		}
	}
	public static class PluginInfo
	{
		public const string PLUGIN_GUID = "ShipTurrets";

		public const string PLUGIN_NAME = "ShipTurrets";

		public const string PLUGIN_VERSION = "1.0.0";
	}
}
namespace ShipTurrets.Patches
{
	[HarmonyPatch(typeof(RoundManager))]
	internal class RoundManagerPatch
	{
		internal static GameObject mapPropsContainer;

		[HarmonyPatch("SpawnMapObjects")]
		[HarmonyPostfix]
		private static void spawnTurret(ref RoundManager __instance)
		{
			//IL_0021: Unknown result type (might be due to invalid IL or missing references)
			//IL_0026: Unknown result type (might be due to invalid IL or missing references)
			//IL_00a5: Unknown result type (might be due to invalid IL or missing references)
			//IL_00a6: Unknown result type (might be due to invalid IL or missing references)
			//IL_00c3: Unknown result type (might be due to invalid IL or missing references)
			//IL_00e0: Unknown result type (might be due to invalid IL or missing references)
			//IL_0112: Unknown result type (might be due to invalid IL or missing references)
			//IL_0113: Unknown result type (might be due to invalid IL or missing references)
			//IL_0130: Unknown result type (might be due to invalid IL or missing references)
			//IL_014d: Unknown result type (might be due to invalid IL or missing references)
			mapPropsContainer = GameObject.FindGameObjectWithTag("MapPropsContainer");
			Vector3 position = ((Component)__instance.playersManager.localPlayerController).transform.position;
			Vector3 val = default(Vector3);
			((Vector3)(ref val))..ctor(8.894f, 7.2597f, -14.0808f);
			Vector3 val2 = default(Vector3);
			((Vector3)(ref val2))..ctor(-5.0311f, 5.4649f, -14.1322f);
			SpawnableMapObject[] spawnableMapObjects = __instance.currentLevel.spawnableMapObjects;
			foreach (SpawnableMapObject val3 in spawnableMapObjects)
			{
				if (!((Object)(object)val3.prefabToSpawn.GetComponentInChildren<Turret>() == (Object)null))
				{
					if (Plugin.isFrontTurretSpawned.Value)
					{
						GameObject val4 = Object.Instantiate<GameObject>(val3.prefabToSpawn, val, Quaternion.identity, mapPropsContainer.transform);
						val4.transform.position = val;
						val4.transform.forward = new Vector3(1f, 0f, 0f);
						val4.GetComponent<NetworkObject>().Spawn(true);
					}
					if (Plugin.isRearTurretSpawned.Value)
					{
						GameObject val5 = Object.Instantiate<GameObject>(val3.prefabToSpawn, val2, Quaternion.identity, mapPropsContainer.transform);
						val5.transform.position = val2;
						val5.transform.forward = new Vector3(-1f, 0f, 0f);
						val5.GetComponent<NetworkObject>().Spawn(true);
					}
				}
			}
		}
	}
	[HarmonyPatch(typeof(Terminal))]
	internal class TerminalPatch : NetworkBehaviour
	{
		internal static GameObject mapPropsContainer;

		internal static int boughtTurret;

		internal static int tempGroupCredits;

		[ServerRpc(RequireOwnership = false)]
		[HarmonyPatch("Start")]
		[HarmonyPostfix]
		private static void addCommands(ref Terminal __instance)
		{
			//IL_0024: Unknown result type (might be due to invalid IL or missing references)
			//IL_0029: Unknown result type (might be due to invalid IL or missing references)
			//IL_004f: Unknown result type (might be due to invalid IL or missing references)
			//IL_005b: Unknown result type (might be due to invalid IL or missing references)
			//IL_006e: Expected O, but got Unknown
			//IL_0074: Unknown result type (might be due to invalid IL or missing references)
			//IL_0079: Unknown result type (might be due to invalid IL or missing references)
			//IL_009f: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ab: Unknown result type (might be due to invalid IL or missing references)
			//IL_00be: Expected O, but got Unknown
			tempGroupCredits = __instance.groupCredits;
			if (!Plugin.isTerminalHostBuy.Value)
			{
				return;
			}
			TerminalApi.AddCommand("Front Turret", new CommandInfo
			{
				DisplayTextSupplier = delegate
				{
					if (tempGroupCredits > 1000)
					{
						boughtTurret++;
						Plugin.logger.LogWarning((object)"Front Turret Enabled.");
						Plugin.isFrontTurretSpawned.Value = true;
						return "Front Turret Enabled";
					}
					Plugin.logger.LogWarning((object)"Can't afford Front Turret.");
					return "You do not have enough credits to purchase the Front Turret.";
				},
				Category = "store",
				Description = "Buy the front turret for your ship. (Costs 1000)"
			}, (string)null, true);
			TerminalApi.AddCommand("Rear Turret", new CommandInfo
			{
				DisplayTextSupplier = delegate
				{
					if (tempGroupCredits > 1000)
					{
						boughtTurret++;
						Plugin.logger.LogWarning((object)"Rear Turret Enabled.");
						Plugin.isRearTurretSpawned.Value = true;
						return "Rear Turret Enabled";
					}
					Plugin.logger.LogWarning((object)"Can't afford rear Turret.");
					return "You do not have enough credits to purchase the rear Turret.";
				},
				Category = "store",
				Description = "Buy the rear turret for your ship. (Costs 1000"
			}, (string)null, true);
		}

		[HarmonyPatch("Update")]
		[HarmonyPostfix]
		private static void PatchUpdate(ref Terminal __instance)
		{
			tempGroupCredits = __instance.groupCredits;
			if (boughtTurret > 0)
			{
				Terminal obj = __instance;
				obj.groupCredits -= 1000;
				boughtTurret--;
			}
		}
	}
	[HarmonyPatch(typeof(Turret))]
	internal class TurretPatch
	{
		public static float viewRadius = 20f;

		public static float viewAngle = 180f;

		public static float missChance = 0.97f;

		private static Vector3 turretLocFront = new Vector3(8.9f, 8.19f, -14.09f);

		private static Vector3 turretLocRear = new Vector3(-5.04f, 6.4f, -14.12f);

		[HarmonyPatch("Update")]
		[HarmonyPrefix]
		public static void PatchUpdateBefore(ref Turret __instance)
		{
			//IL_002b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0030: 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_0032: Unknown result type (might be due to invalid IL or missing references)
			//IL_0034: Unknown result type (might be due to invalid IL or missing references)
			//IL_0036: Unknown result type (might be due to invalid IL or missing references)
			//IL_0037: Unknown result type (might be due to invalid IL or missing references)
			//IL_005f: Unknown result type (might be due to invalid IL or missing references)
			//IL_003c: Unknown result type (might be due to invalid IL or missing references)
			//IL_003e: Invalid comparison between Unknown and I4
			//IL_008e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0094: Invalid comparison between Unknown and I4
			//IL_0098: Unknown result type (might be due to invalid IL or missing references)
			//IL_009e: Invalid comparison between Unknown and I4
			//IL_0084: Unknown result type (might be due to invalid IL or missing references)
			//IL_00c8: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ca: Invalid comparison between Unknown and I4
			//IL_00cc: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ce: Invalid comparison between Unknown and I4
			//IL_00df: Unknown result type (might be due to invalid IL or missing references)
			//IL_016d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0177: Unknown result type (might be due to invalid IL or missing references)
			//IL_0181: Unknown result type (might be due to invalid IL or missing references)
			//IL_0187: Unknown result type (might be due to invalid IL or missing references)
			//IL_014a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0150: Unknown result type (might be due to invalid IL or missing references)
			if (!isShipTurret(__instance))
			{
				return;
			}
			TurretMode val = (TurretMode)AccessTools.Field(typeof(Turret), "turretModeLastFrame").GetValue(__instance);
			TurretMode val2 = val;
			TurretMode val3 = val2;
			List<EnemyAICollisionDetect> enemies;
			if ((int)val3 != 0)
			{
				if ((int)val3 == 1)
				{
					enemies = GetEnemies(GetTargets(__instance));
					if (enemies.Any())
					{
						__instance.turretMode = (TurretMode)2;
					}
				}
			}
			else
			{
				enemies = GetEnemies(GetTargets(__instance));
				if (enemies.Any())
				{
					__instance.turretMode = (TurretMode)1;
				}
			}
			if ((int)__instance.turretMode != 2 && (int)__instance.turretMode != 3)
			{
				return;
			}
			enemies = GetEnemies(GetTargets(__instance));
			if (!TargetEnemies(__instance, enemies) && ((int)val == 2 || (int)val == 3))
			{
				__instance.turretMode = (TurretMode)0;
			}
			if (Plugin.isTurretFF.Value && (Object)(object)__instance.CheckForPlayersInLineOfSight(3f, false) == (Object)(object)GameNetworkManager.Instance.localPlayerController)
			{
				if (GameNetworkManager.Instance.localPlayerController.health - 34 > 0)
				{
					GameNetworkManager.Instance.localPlayerController.DamagePlayer(34, true, true, (CauseOfDeath)7, 0, false, default(Vector3));
				}
				else
				{
					GameNetworkManager.Instance.localPlayerController.KillPlayer(__instance.aimPoint.forward * 40f, true, (CauseOfDeath)7, 0, default(Vector3));
				}
			}
		}

		[HarmonyPatch("TurnTowardsTargetIfHasLOS")]
		[HarmonyPrefix]
		public static void PatchTurnTowardsTargetIfHasLOS(ref Turret __instance)
		{
			//IL_00e3: Unknown result type (might be due to invalid IL or missing references)
			//IL_00fa: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ff: Unknown result type (might be due to invalid IL or missing references)
			//IL_0109: Unknown result type (might be due to invalid IL or missing references)
			//IL_010e: Unknown result type (might be due to invalid IL or missing references)
			bool flag = (bool)AccessTools.Field(typeof(Turret), "hasLineOfSight").GetValue(__instance);
			float num = (float)AccessTools.Field(typeof(Turret), "lostLOSTimer").GetValue(__instance);
			List<EnemyAICollisionDetect> enemies = GetEnemies(GetTargets(__instance));
			if (enemies.Any())
			{
				using List<EnemyAICollisionDetect>.Enumerator enumerator = enemies.GetEnumerator();
				if (enumerator.MoveNext())
				{
					EnemyAICollisionDetect current = enumerator.Current;
					flag = true;
					num = 0f;
					AccessTools.Field(typeof(Turret), "hasLineOfSight").SetValue(__instance, true);
					AccessTools.Field(typeof(Turret), "lostLOSTimer").SetValue(__instance, 0f);
					__instance.tempTransform.position = ((Component)current.mainScript).transform.position;
					Transform tempTransform = __instance.tempTransform;
					tempTransform.position -= Vector3.up * 0.15f;
					__instance.turnTowardsObjectCompass.LookAt(__instance.tempTransform);
				}
			}
			if (flag)
			{
				flag = false;
				num = 0f;
				AccessTools.Field(typeof(Turret), "hasLineOfSight").SetValue(__instance, false);
				AccessTools.Field(typeof(Turret), "lostLOSTimer").SetValue(__instance, 0f);
			}
		}

		public static List<EnemyAICollisionDetect> GetTargets(Turret turret)
		{
			//IL_000d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0035: Unknown result type (might be due to invalid IL or missing references)
			//IL_0040: Expected O, but got Unknown
			List<EnemyAICollisionDetect> list = new List<EnemyAICollisionDetect>();
			Collider[] array = Physics.OverlapSphere(turret.aimPoint.position, viewRadius);
			for (int i = 0; i < array.Length; i++)
			{
				Transform transform = ((Component)array[i]).transform;
				EnemyAICollisionDetect component = ((Component)transform).GetComponent<EnemyAICollisionDetect>();
				if ((Object)component != (Object)null)
				{
					EnemyAICollisionDetect component2 = ((Component)transform).GetComponent<EnemyAICollisionDetect>();
					list.Add(component2);
				}
			}
			return list;
		}

		public static List<EnemyAICollisionDetect> GetEnemies(List<EnemyAICollisionDetect> targets)
		{
			List<EnemyAICollisionDetect> retList = new List<EnemyAICollisionDetect>();
			targets.ForEach(delegate(EnemyAICollisionDetect target)
			{
				if (!target.mainScript.isEnemyDead && target.mainScript.enemyType.enemyName != "Manticoil" && target.mainScript.enemyType.enemyName != "Red Locust Bees" && target.mainScript.enemyType.enemyName != "Docile Roaming Locust")
				{
					retList.Add(target);
				}
			});
			return retList;
		}

		public static bool TargetEnemies(Turret turret, List<EnemyAICollisionDetect> visibleEnemies)
		{
			//IL_003b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0041: Unknown result type (might be due to invalid IL or missing references)
			bool result = false;
			if (visibleEnemies.Count > 0)
			{
				if (Random.Range(0f, 1f) > missChance)
				{
					visibleEnemies[0].mainScript.HitEnemyOnLocalClient(3, default(Vector3), (PlayerControllerB)null, false, -1);
					Plugin.logger.LogInfo((object)(visibleEnemies[0].mainScript.enemyType.enemyName + " was hit."));
					result = true;
				}
				else
				{
					result = false;
				}
			}
			return result;
		}

		private static bool isShipTurret(Turret turret)
		{
			//IL_0007: 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_0028: Unknown result type (might be due to invalid IL or missing references)
			//IL_002d: Unknown result type (might be due to invalid IL or missing references)
			if ((double)Vector3.Distance(((Component)turret).transform.position, turretLocFront) < 1.0 || (double)Vector3.Distance(((Component)turret).transform.position, turretLocRear) < 1.0)
			{
				return true;
			}
			return false;
		}
	}
}