Decompiled source of ImFRIENDLY DAMMIT v1.1.3

ImFRIENDLY.dll

Decompiled 2 weeks 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.3")]
[assembly: TargetFramework(".NETFramework,Version=v4.6.2", FrameworkDisplayName = ".NET Framework 4.6.2")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.1.3.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.3")]
	public class ImFRIENDLYDAMMITPlugin : BaseUnityPlugin
	{
		internal const string ModName = "ImFRIENDLYDAMMIT";

		internal const string ModVersion = "1.1.3";

		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, List<Character> onlyTargets = null)
		{
			//IL_00c8: Unknown result type (might be due to invalid IL or missing references)
			//IL_00e2: Unknown result type (might be due to invalid IL or missing references)
			//IL_00e8: Unknown result type (might be due to invalid IL or missing references)
			List<Character> allCharacters = Character.GetAllCharacters();
			Character val = null;
			float num = 99999f;
			foreach (Character item in allCharacters)
			{
				if ((!includePlayers && item is Player) || (!includeTamed && item.IsTamed()) || !AttackTarget(item))
				{
					continue;
				}
				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())
				{
					continue;
				}
				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 num2 = Vector3.Distance(((Component)item).transform.position, me.position);
					if ((double)num2 < (double)num || (Object)(object)val == (Object)null)
					{
						val = item;
						num = num2;
					}
				}
			}
			return val;
		}

		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)
		{
			return TurretUpdateTargetPatch.AttackTarget(___m_target);
		}
	}
}