Decompiled source of Turret Self Hit Fix v1.0.1

plugins\TurretSelfHitFix\TurretSelfHitFix.dll

Decompiled 2 days ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
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: TargetFramework(".NETFramework,Version=v4.7.2", FrameworkDisplayName = ".NET Framework 4.7.2")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("0.0.0.0")]
[module: UnverifiableCode]
[module: RefSafetyRules(11)]
namespace Microsoft.CodeAnalysis
{
	[CompilerGenerated]
	[Embedded]
	internal sealed class EmbeddedAttribute : Attribute
	{
	}
}
namespace System.Runtime.CompilerServices
{
	[CompilerGenerated]
	[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 Valheim1Mod
{
	[BepInPlugin("MrGay.TurretSelfHitFix", "Turret Self Hit Fix", "1.0.1")]
	public class Valheim1ModPlugin : BaseUnityPlugin
	{
		private static readonly Harmony Harmony = new Harmony("MrGay.TurretSelfHitFix");

		internal static ManualLogSource Log;

		private void Awake()
		{
			Log = ((BaseUnityPlugin)this).Logger;
			Harmony.PatchAll();
			((BaseUnityPlugin)this).Logger.LogInfo((object)"Turret Self Hit Fix loaded. Turret missile collision fix active.");
		}
	}
	[HarmonyPatch(typeof(Turret), "ShootProjectile")]
	public static class Turret_ShootProjectile_CollisionFix
	{
		private static readonly Dictionary<Projectile, HashSet<Collider>> IgnoredCollisions = new Dictionary<Projectile, HashSet<Collider>>();

		[HarmonyPostfix]
		public static void Postfix(Turret __instance, GameObject ___m_lastProjectile)
		{
			if ((Object)(object)__instance == (Object)null || (Object)(object)___m_lastProjectile == (Object)null)
			{
				return;
			}
			Projectile component = ___m_lastProjectile.GetComponent<Projectile>();
			if ((Object)(object)component == (Object)null)
			{
				return;
			}
			Collider[] componentsInChildren = ((Component)__instance).GetComponentsInChildren<Collider>(true);
			Collider[] componentsInChildren2 = ___m_lastProjectile.GetComponentsInChildren<Collider>(true);
			HashSet<Collider> value = new HashSet<Collider>(componentsInChildren);
			IgnoredCollisions[component] = value;
			Collider[] array = componentsInChildren2;
			foreach (Collider val in array)
			{
				Collider[] array2 = componentsInChildren;
				foreach (Collider val2 in array2)
				{
					Physics.IgnoreCollision(val, val2, true);
				}
			}
		}

		public static HashSet<Collider> GetIgnoredCollisions(Projectile projectile)
		{
			if ((Object)(object)projectile == (Object)null)
			{
				return null;
			}
			IgnoredCollisions.TryGetValue(projectile, out var value);
			return value;
		}
	}
	[HarmonyPatch(typeof(Projectile), "OnHit")]
	public static class Projectile_OnHit_TurretCollisionFix
	{
		[HarmonyPrefix]
		public static bool Prefix(Projectile __instance, Collider collider)
		{
			if (!((Object)(object)collider == (Object)null))
			{
				return !IsIgnoredTurretCollider(__instance, collider);
			}
			return true;
		}

		private static bool IsIgnoredTurretCollider(Projectile projectile, Collider collider)
		{
			return Turret_ShootProjectile_CollisionFix.GetIgnoredCollisions(projectile)?.Contains(collider) ?? false;
		}
	}
}