using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using BepInEx;
using HarmonyLib;
using MoveClasses;
using UnityEngine;
[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.Default | DebuggableAttribute.DebuggingModes.DisableOptimizations | DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints | DebuggableAttribute.DebuggingModes.EnableEditAndContinue)]
[assembly: AssemblyTitle("Disemberment")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("Disemberment")]
[assembly: AssemblyCopyright("Copyright © 2024")]
[assembly: AssemblyTrademark("")]
[assembly: ComVisible(false)]
[assembly: Guid("61440d51-602e-416a-b15f-dcc42c72d09e")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: TargetFramework(".NETFramework,Version=v4.8", FrameworkDisplayName = ".NET Framework 4.8")]
[assembly: AssemblyVersion("1.0.0.0")]
[BepInPlugin("com.morsecodeguy.dismemberment", "Dismemberment", "1.0.1")]
public class DismembermentMod : BaseUnityPlugin
{
public const float BaseDamage = 25f;
public const float DamageThreshold = 100f;
public const float JointDamageThreshold = 150f;
private void Awake()
{
//IL_0006: Unknown result type (might be due to invalid IL or missing references)
//IL_000c: Expected O, but got Unknown
Harmony val = new Harmony("com.morsecodeguy.dismemberment");
val.PatchAll();
}
}
[HarmonyPatch]
public class DismembermentPatch
{
private static Dictionary<WeaponDamageablePart, float> partDamage = new Dictionary<WeaponDamageablePart, float>();
[HarmonyPostfix]
[HarmonyPatch(typeof(WeaponDamageablePart), "Destory")]
public static void PostfixTakeDamage(WeaponDamageablePart __instance, DamageOrigin? damageOrigin)
{
float num = CalculateImpactFactor(damageOrigin);
if (IsCriticalJoint(__instance))
{
float num2 = 25f * num;
if (!partDamage.ContainsKey(__instance))
{
partDamage[__instance] = 0f;
}
partDamage[__instance] += num2;
if (!(partDamage[__instance] >= 150f))
{
return;
}
if (((Object)__instance).name == "NECK")
{
PlayerHealth componentInParent = ((Component)__instance).GetComponentInParent<PlayerHealth>();
if ((Object)(object)componentInParent != (Object)null)
{
componentInParent.alive = false;
((MonoBehaviour)__instance).StartCoroutine(DelayedDeath(componentInParent));
}
}
DetachLimbParent(__instance);
}
else
{
float num3 = 25f * num;
if (!partDamage.ContainsKey(__instance))
{
partDamage[__instance] = 0f;
}
partDamage[__instance] += num3;
if (partDamage[__instance] >= 100f)
{
DetachLimbParent(__instance);
}
}
}
private static float CalculateImpactFactor(DamageOrigin? damageOrigin)
{
if (damageOrigin.HasValue)
{
float num = 5f;
return Mathf.Clamp(num / 10f, 0.1f, 10f);
}
return 1f;
}
private static bool IsCriticalJoint(WeaponDamageablePart part)
{
string[] array = new string[13]
{
"KNEE_LEFT", "KNEE_RIGHT", "HIP_JOINT_LEFT", "HIP_JOINT_RIGHT", "SCAPULA_LEFT", "SCAPULA_RIGHT", "NECK", "ELBOW_LEFT", "ELBOW_RIGHT", "WRIST_LEFT",
"WRIST_RIGHT", "ANKLE_LEFT", "ANKLE_RIGHT"
};
string[] array2 = array;
foreach (string value in array2)
{
if (((Object)((Component)part).transform.parent).name.Contains(value))
{
return true;
}
}
return false;
}
private static void DetachLimbParent(WeaponDamageablePart limb)
{
Transform parent = ((Component)limb).transform.parent;
if ((Object)(object)parent != (Object)null && (Object)(object)parent.parent != (Object)null)
{
parent.SetParent((Transform)null);
Rigidbody val = ((Component)parent).GetComponent<Rigidbody>();
if ((Object)(object)val == (Object)null)
{
val = ((Component)parent).gameObject.AddComponent<Rigidbody>();
}
val.isKinematic = false;
val.useGravity = true;
ConfigurableJoint[] components = ((Component)parent).GetComponents<ConfigurableJoint>();
ConfigurableJoint[] array = components;
foreach (ConfigurableJoint val2 in array)
{
Object.Destroy((Object)(object)val2);
}
IgnorePlayerCollisions(parent);
}
}
private static void IgnorePlayerCollisions(Transform limbParent)
{
Collider component = ((Component)limbParent).GetComponent<Collider>();
if (!((Object)(object)component != (Object)null))
{
return;
}
PlayerHealth componentInParent = ((Component)limbParent).GetComponentInParent<PlayerHealth>();
if ((Object)(object)componentInParent != (Object)null)
{
Collider[] componentsInChildren = ((Component)componentInParent).GetComponentsInChildren<Collider>();
Collider[] array = componentsInChildren;
foreach (Collider val in array)
{
Physics.IgnoreCollision(component, val);
}
}
}
private static IEnumerator DelayedDeath(PlayerHealth playerHealth)
{
yield return (object)new WaitForSeconds(2f);
playerHealth.Die((DeathReason)4);
}
}