Decompiled source of LegoDeathEffect v1.0.0

LegoDeathEffect.dll

Decompiled 2 years ago
using System;
using System.Collections;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using BepInEx;
using HarmonyLib;
using UnityEngine;
using UnityEngine.Networking;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.Default | DebuggableAttribute.DebuggingModes.DisableOptimizations | DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints | DebuggableAttribute.DebuggingModes.EnableEditAndContinue)]
[assembly: AssemblyTitle("LegoDeathEffect")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("LegoDeathEffect")]
[assembly: AssemblyCopyright("Copyright ©  2024")]
[assembly: AssemblyTrademark("")]
[assembly: ComVisible(false)]
[assembly: Guid("860099af-2888-4aa4-9066-c976e248341b")]
[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.legodeatheffect", "LegoDeathEffect", "1.0.0")]
public class LegoDeathEffect : BaseUnityPlugin
{
	private void Awake()
	{
		//IL_0017: Unknown result type (might be due to invalid IL or missing references)
		//IL_001d: Expected O, but got Unknown
		((BaseUnityPlugin)this).Logger.LogInfo((object)"LegoDeathEffect mod initialized.");
		Harmony val = new Harmony("com.morsecodeguy.legodeatheffect");
		val.PatchAll();
	}
}
[HarmonyPatch(typeof(PlayerHealth))]
public class PlayerHealthPatch
{
	[HarmonyPostfix]
	[HarmonyPatch("Die")]
	public static void PostfixDie(PlayerHealth __instance)
	{
		Debug.Log((object)"Player has died. Triggering LegoDeathEffect.");
		Debug.Log((object)"Detaching critical joints and playing death sound.");
		DetachCriticalJoints(__instance);
		((MonoBehaviour)__instance).StartCoroutine(PlayLegoBreakingSound(__instance));
	}

	private static void DetachCriticalJoints(PlayerHealth playerHealth)
	{
		Debug.Log((object)"Starting critical joint detachment...");
		string[] criticalJoints = 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"
		};
		WeaponDamageablePart[] componentsInChildren = ((Component)playerHealth).GetComponentsInChildren<WeaponDamageablePart>();
		WeaponDamageablePart[] array = componentsInChildren;
		foreach (WeaponDamageablePart val in array)
		{
			Transform parent = ((Component)val).transform.parent;
			if ((Object)(object)parent != (Object)null && IsCriticalJoint(((Object)parent).name, criticalJoints))
			{
				Debug.Log((object)("Detaching critical joint: " + ((Object)parent).name));
				DetachLimb(parent);
			}
		}
		Debug.Log((object)"Critical joint detachment completed.");
	}

	private static bool IsCriticalJoint(string limbName, string[] criticalJoints)
	{
		foreach (string value in criticalJoints)
		{
			if (limbName.Contains(value))
			{
				return true;
			}
		}
		return false;
	}

	private static void DetachLimb(Transform limbParent)
	{
		limbParent.SetParent((Transform)null);
		Rigidbody val = ((Component)limbParent).GetComponent<Rigidbody>();
		if ((Object)(object)val == (Object)null)
		{
			Debug.Log((object)("Adding Rigidbody to " + ((Object)limbParent).name));
			val = ((Component)limbParent).gameObject.AddComponent<Rigidbody>();
		}
		val.isKinematic = false;
		val.useGravity = true;
		ConfigurableJoint[] components = ((Component)limbParent).GetComponents<ConfigurableJoint>();
		Debug.Log((object)$"Found {components.Length} joints on {((Object)limbParent).name}. Destroying joints.");
		ConfigurableJoint[] array = components;
		foreach (ConfigurableJoint val2 in array)
		{
			Object.Destroy((Object)(object)val2);
		}
		IgnorePlayerCollisions(limbParent);
	}

	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 PlayLegoBreakingSound(PlayerHealth playerHealth)
	{
		string soundPath = Path.Combine(Path.GetDirectoryName(typeof(LegoDeathEffect).Assembly.Location), "lego-breaking.mp3");
		Debug.Log((object)("Looking for sound file at: " + soundPath));
		if (!File.Exists(soundPath))
		{
			Debug.LogError((object)"lego-breaking.mp3 sound file not found in the mod folder.");
			yield break;
		}
		Debug.Log((object)"lego-breaking.mp3 file found. Loading audio clip...");
		UnityWebRequest www = UnityWebRequestMultimedia.GetAudioClip("file://" + soundPath, (AudioType)13);
		try
		{
			yield return www.SendWebRequest();
			if ((int)www.result == 2 || (int)www.result == 3)
			{
				Debug.LogError((object)("Error loading lego-breaking.mp3: " + www.error));
				yield break;
			}
			Debug.Log((object)"Audio clip loaded successfully.");
			AudioClip clip = DownloadHandlerAudioClip.GetContent(www);
			AudioSource audioSource = ((Component)playerHealth).gameObject.AddComponent<AudioSource>();
			audioSource.clip = clip;
			audioSource.spatialBlend = 1f;
			Debug.Log((object)"Playing lego-breaking.mp3 at player position.");
			audioSource.Play();
			Object.Destroy((Object)(object)audioSource, clip.length);
			Debug.Log((object)"AudioSource destroyed after playback.");
		}
		finally
		{
			((IDisposable)www)?.Dispose();
		}
	}
}