Decompiled source of ImFRIENDLY DAMMIT v1.1.8

ImFRIENDLY.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.InteropServices;
using System.Runtime.Versioning;
using System.Security;
using System.Security.Permissions;
using BepInEx;
using BepInEx.Logging;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: AssemblyTitle("ImFRIENDLYDAMMIT")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("Azumatt")]
[assembly: AssemblyProduct("ImFRIENDLYDAMMIT")]
[assembly: AssemblyCopyright("Copyright ©  2021")]
[assembly: AssemblyTrademark("")]
[assembly: ComVisible(false)]
[assembly: Guid("4358610B-F3F4-4843-B7AF-98B7BC60DCDE")]
[assembly: AssemblyFileVersion("1.1.8")]
[assembly: TargetFramework(".NETFramework,Version=v4.6.2", FrameworkDisplayName = ".NET Framework 4.6.2")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.1.8.0")]
[module: UnverifiableCode]
namespace Microsoft.CodeAnalysis
{
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.Embedded]
	internal sealed class EmbeddedAttribute : Attribute
	{
	}
}
namespace System.Runtime.CompilerServices
{
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.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]
	[Microsoft.CodeAnalysis.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;
		}
	}
}
namespace ImFRIENDLY
{
	[BepInPlugin("Azumatt.ImFRIENDLYDAMMIT", "ImFRIENDLYDAMMIT", "1.1.8")]
	public class ImFRIENDLYDAMMITPlugin : BaseUnityPlugin
	{
		internal const string ModName = "ImFRIENDLYDAMMIT";

		internal const string ModVersion = "1.1.8";

		internal const string Author = "Azumatt";

		private const string ModGUID = "Azumatt.ImFRIENDLYDAMMIT";

		private readonly Harmony _harmony = new Harmony("Azumatt.ImFRIENDLYDAMMIT");

		public static readonly ManualLogSource ImFRIENDLYDAMMITLogger = Logger.CreateLogSource("ImFRIENDLYDAMMIT");

		public void Awake()
		{
			Assembly executingAssembly = Assembly.GetExecutingAssembly();
			_harmony.PatchAll(executingAssembly);
		}
	}
	[HarmonyPatch(typeof(Turret), "UpdateTarget")]
	public static class TurretUpdateTargetPatch
	{
		private static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstruction> instructions)
		{
			MethodInfo targetMethod = typeof(TurretUpdateTargetPatch).GetMethod("ImFRIENDLYDAMMIT");
			return instructions.Select((CodeInstruction inst) => (CodeInstruction)((inst.opcode == OpCodes.Call && inst.operand is MethodInfo methodInfo && methodInfo.Name == "FindClosestCreature") ? ((object)new CodeInstruction(OpCodes.Call, (object)targetMethod)) : ((object)inst)));
		}

		public static Character ImFRIENDLYDAMMIT(Transform me, Vector3 eyePoint, float hearRange, float viewRange, float viewAngle, bool alerted, bool mistVision, bool passiveAggresive, bool includePlayers = true, bool includeTamed = true, bool includeEnemies = true, List<Character> onlyTargets = null)
		{
			//IL_01cf: Unknown result type (might be due to invalid IL or missing references)
			//IL_016e: Unknown result type (might be due to invalid IL or missing references)
			//IL_018a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0190: Unknown result type (might be due to invalid IL or missing references)
			List<Character> allCharacters = Character.GetAllCharacters();
			Character val = null;
			float num = 99999f;
			int num2;
			if (!includeEnemies && ZoneSystem.instance.GetGlobalKey((GlobalKeys)24))
			{
				WearNTear component = ((Component)me).GetComponent<WearNTear>();
				if ((Object)(object)component != (Object)null)
				{
					num2 = (((double)component.GetHealthPercentage() == 1.0) ? 1 : 0);
					goto IL_004a;
				}
			}
			num2 = 0;
			goto IL_004a;
			IL_004a:
			if (num2 != 0)
			{
				return null;
			}
			if (!(includePlayers || includeTamed) || includeEnemies)
			{
				foreach (Character item in allCharacters)
				{
					if ((includePlayers || !(item is Player)) && (includeEnemies || !(item is Player)) && (includeTamed || !item.IsTamed()))
					{
						ImFRIENDLYDAMMITPlugin.ImFRIENDLYDAMMITLogger.LogDebug((object)("Checking " + item.m_name + " from " + Utils.GetPrefabName(((Object)((Component)me).gameObject).name)));
						if (AttackTarget(item) && includeEnemies)
						{
							if (onlyTargets != null && onlyTargets.Count > 0)
							{
								bool flag = false;
								foreach (Character onlyTarget in onlyTargets)
								{
									if (item.m_name == onlyTarget.m_name)
									{
										flag = true;
										break;
									}
								}
								if (!flag)
								{
									continue;
								}
							}
							if (!item.IsDead())
							{
								BaseAI baseAI = item.GetBaseAI();
								if ((!((Object)(object)baseAI != (Object)null) || !baseAI.IsSleeping()) && BaseAI.CanSenseTarget(me, eyePoint, hearRange, viewRange, viewAngle, alerted, mistVision, item, passiveAggresive, false))
								{
									float num3 = Vector3.Distance(((Component)item).transform.position, me.position);
									if ((double)num3 < (double)num || (Object)(object)val == (Object)null)
									{
										val = item;
										num = num3;
									}
								}
							}
						}
					}
				}
				return val;
			}
			return BaseAI.FindClosestCreature(me, eyePoint, hearRange, viewRange, viewAngle, alerted, mistVision, passiveAggresive, includePlayers, includeTamed, includeEnemies, onlyTargets);
		}

		internal static bool AttackTarget(Character target)
		{
			//IL_0042: Unknown result type (might be due to invalid IL or missing references)
			if (!target.m_nview.IsValid())
			{
				return true;
			}
			if (target.IsDead())
			{
				return false;
			}
			if (target.IsTamed())
			{
				return false;
			}
			if (((Component)target).GetComponents<Growup>().Any())
			{
				return false;
			}
			if (((Component)target).GetComponents<AnimalAI>().Any())
			{
				return false;
			}
			if ((int)target.GetFaction() == 0)
			{
				return false;
			}
			if (target.IsPVPEnabled())
			{
				return true;
			}
			if (!target.IsPlayer())
			{
				return true;
			}
			if (target.IsPlayer())
			{
				return false;
			}
			if ((Object)(object)target == (Object)(object)Player.m_localPlayer)
			{
				return false;
			}
			return true;
		}
	}
	[HarmonyPatch(typeof(Turret), "ShootProjectile")]
	internal static class TurretShootProjectilePatch
	{
		private static bool Prefix(Turret __instance, ref Character ___m_target)
		{
			ImFRIENDLYDAMMITPlugin.ImFRIENDLYDAMMITLogger.LogDebug((object)("Shooting projectile from " + Utils.GetPrefabName(((Object)((Component)__instance).gameObject).name) + " (" + __instance.m_name + ")"));
			if (__instance.m_name == "$piece_charredballista")
			{
				return true;
			}
			return TurretUpdateTargetPatch.AttackTarget(___m_target);
		}
	}
}