Decompiled source of ExplodingCart v1.0.0

ExplodingCart.dll

Decompiled 4 months ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
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.Default | DebuggableAttribute.DebuggingModes.DisableOptimizations | DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints | DebuggableAttribute.DebuggingModes.EnableEditAndContinue)]
[assembly: TargetFramework(".NETStandard,Version=v2.1", FrameworkDisplayName = ".NET Standard 2.1")]
[assembly: IgnoresAccessChecksTo("")]
[assembly: AssemblyCompany("Omniscye")]
[assembly: AssemblyConfiguration("Debug")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("ExplodingCart")]
[assembly: AssemblyTitle("ExplodingCart")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.0.0.0")]
[module: UnverifiableCode]
[module: RefSafetyRules(11)]
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;
		}
	}
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.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;
		}
	}
}
[HarmonyPatch]
public class CartExplosionPatch
{
	private const float EXPLOSION_THRESHOLD = 10000f;

	private const float CAMERA_SHAKE_STRENGTH = 5f;

	private const float CAMERA_SHAKE_TIME = 0.5f;

	private const float TUMBLE_FORCE_MULTIPLIER = 20f;

	private const float SELF_TORQUE_MULTIPLIER = 3f;

	private const float INSTANT_KILL_RADIUS = 4f;

	private const float DAMAGE_RADIUS = 8f;

	private static HashSet<int> explodedCarts;

	private static AssetBundle explosionAssetBundle;

	private static AudioClip omniBoomClip;

	private static bool assetBundleLoaded;

	static CartExplosionPatch()
	{
		explodedCarts = new HashSet<int>();
		assetBundleLoaded = false;
		LoadExplosionAssets();
	}

	private static void LoadExplosionAssets()
	{
		try
		{
			string directoryName = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
			string text = Path.Combine(directoryName, "boomcart");
			if (File.Exists(text))
			{
				explosionAssetBundle = AssetBundle.LoadFromFile(text);
				if ((Object)(object)explosionAssetBundle != (Object)null)
				{
					omniBoomClip = explosionAssetBundle.LoadAsset<AudioClip>("OmniBoom");
					if ((Object)(object)omniBoomClip != (Object)null)
					{
						assetBundleLoaded = true;
						Debug.Log((object)"[ExplodingCart] Successfully loaded OmniBoom sound from asset bundle!");
					}
					else
					{
						Debug.LogError((object)"[ExplodingCart] Failed to load OmniBoom audio clip from asset bundle!");
					}
				}
				else
				{
					Debug.LogError((object)"[ExplodingCart] Failed to load boomcart asset bundle!");
				}
			}
			else
			{
				Debug.LogError((object)("[ExplodingCart] Asset bundle not found at: " + text));
			}
		}
		catch (Exception ex)
		{
			Debug.LogError((object)("[ExplodingCart] Error loading explosion assets: " + ex.Message));
		}
	}

	[HarmonyPatch(typeof(PhysGrabCart), "ObjectsInCart")]
	[HarmonyPostfix]
	private static void CheckForExplosion(PhysGrabCart __instance)
	{
		if (SemiFunc.IsMasterClientOrSingleplayer())
		{
			int instanceID = ((Object)__instance).GetInstanceID();
			if (!explodedCarts.Contains(instanceID) && (float)__instance.haulCurrent >= 10000f)
			{
				explodedCarts.Add(instanceID);
				ExplodeCart(__instance);
			}
		}
	}

	private static void ExplodeCart(PhysGrabCart cart)
	{
		//IL_002f: Unknown result type (might be due to invalid IL or missing references)
		PlayExplosionEffects(cart);
		ExplodeCameraShake(cart);
		TumbleNearbyPlayers(cart);
		TumbleCart(cart);
		LaunchCartItems(cart);
		EnemyDirector.instance.SetInvestigate(((Component)cart).transform.position, 20f, false);
	}

	private static void PlayExplosionEffects(PhysGrabCart cart)
	{
		//IL_0007: Unknown result type (might be due to invalid IL or missing references)
		//IL_000c: Unknown result type (might be due to invalid IL or missing references)
		//IL_0016: Unknown result type (might be due to invalid IL or missing references)
		//IL_001b: Unknown result type (might be due to invalid IL or missing references)
		//IL_0020: Unknown result type (might be due to invalid IL or missing references)
		//IL_0021: Unknown result type (might be due to invalid IL or missing references)
		//IL_0058: Unknown result type (might be due to invalid IL or missing references)
		Vector3 position = ((Component)cart).transform.position + Vector3.up * 0.5f;
		PlayExplosionSound(position);
		Component[] componentsInChildren = ((Component)cart).GetComponentsInChildren<Component>();
		Component[] array = componentsInChildren;
		foreach (Component val in array)
		{
			ParticleSystem val2 = (ParticleSystem)(object)((val is ParticleSystem) ? val : null);
			if (val2 != null)
			{
				((Component)val2).transform.position = position;
				val2.Play();
			}
		}
		Object.Destroy((Object)(object)((Component)cart).gameObject, 2f);
	}

	private static void PlayExplosionSound(Vector3 position)
	{
		//IL_0145: Unknown result type (might be due to invalid IL or missing references)
		//IL_0023: Unknown result type (might be due to invalid IL or missing references)
		//IL_0029: Expected O, but got Unknown
		//IL_002f: Unknown result type (might be due to invalid IL or missing references)
		//IL_00b9: Unknown result type (might be due to invalid IL or missing references)
		if (assetBundleLoaded && (Object)(object)omniBoomClip != (Object)null)
		{
			GameObject val = new GameObject("CartExplosionSound");
			val.transform.position = position;
			AudioSource val2 = val.AddComponent<AudioSource>();
			val2.clip = omniBoomClip;
			val2.volume = 1f;
			val2.pitch = 1f;
			val2.spatialBlend = 0.5f;
			val2.rolloffMode = (AudioRolloffMode)1;
			val2.minDistance = 1f;
			val2.maxDistance = 100f;
			val2.priority = 0;
			val2.bypassEffects = true;
			val2.bypassListenerEffects = true;
			val2.bypassReverbZones = true;
			val2.Play();
			AudioSource.PlayClipAtPoint(omniBoomClip, position, 1f);
			try
			{
				Camera main = Camera.main;
				if ((Object)(object)main != (Object)null)
				{
					AudioSource val3 = ((Component)main).GetComponent<AudioSource>();
					if ((Object)(object)val3 == (Object)null)
					{
						val3 = ((Component)main).gameObject.AddComponent<AudioSource>();
					}
					val3.PlayOneShot(omniBoomClip, 1f);
				}
			}
			catch
			{
			}
			Object.Destroy((Object)(object)val, omniBoomClip.length + 0.5f);
			Debug.Log((object)$"[ExplodingCart] Played OmniBoom explosion sound! Clip length: {omniBoomClip.length}s, Volume: 1.0, Position: {position}");
		}
		else
		{
			Debug.LogWarning((object)"[ExplodingCart] OmniBoom sound not loaded, falling back to existing sounds");
		}
	}

	private static void ExplodeCameraShake(PhysGrabCart cart)
	{
		//IL_0007: Unknown result type (might be due to invalid IL or missing references)
		//IL_000c: Unknown result type (might be due to invalid IL or missing references)
		//IL_0026: Unknown result type (might be due to invalid IL or missing references)
		//IL_004b: Unknown result type (might be due to invalid IL or missing references)
		Vector3 position = ((Component)cart).transform.position;
		GameDirector.instance.CameraShake.ShakeDistance(5f, 5f, 15f, position, 0.5f);
		GameDirector.instance.CameraImpact.ShakeDistance(5f, 2f, 8f, position, 0.5f);
	}

	private static void TumbleNearbyPlayers(PhysGrabCart cart)
	{
		//IL_0007: Unknown result type (might be due to invalid IL or missing references)
		//IL_000c: Unknown result type (might be due to invalid IL or missing references)
		//IL_0130: Unknown result type (might be due to invalid IL or missing references)
		//IL_0108: Unknown result type (might be due to invalid IL or missing references)
		//IL_010d: Unknown result type (might be due to invalid IL or missing references)
		//IL_0126: Unknown result type (might be due to invalid IL or missing references)
		//IL_0163: Unknown result type (might be due to invalid IL or missing references)
		//IL_009e: Unknown result type (might be due to invalid IL or missing references)
		Vector3 position = ((Component)cart).transform.position;
		float num = 8f;
		if ((Object)(object)cart.physGrabObject != (Object)null && cart.physGrabObject.playerGrabbing != null)
		{
			foreach (PhysGrabber item in new List<PhysGrabber>(cart.physGrabObject.playerGrabbing))
			{
				if ((Object)(object)item != (Object)null && (Object)(object)item.playerAvatar != (Object)null)
				{
					PlayerAvatar component = ((Component)item.playerAvatar).GetComponent<PlayerAvatar>();
					if ((Object)(object)component != (Object)null)
					{
						TumblePlayerFromExplosion(component, position);
					}
				}
			}
		}
		if ((Object)(object)PlayerController.instance?.playerAvatar != (Object)null)
		{
			PlayerAvatar component2 = PlayerController.instance.playerAvatar.GetComponent<PlayerAvatar>();
			if ((Object)(object)component2 != (Object)null)
			{
				float num2 = Vector3.Distance(((Component)component2).transform.position, position);
				if (num2 <= num)
				{
					TumblePlayerFromExplosion(component2, position);
				}
			}
		}
		Collider[] array = Physics.OverlapSphere(position, num);
		Collider[] array2 = array;
		foreach (Collider val in array2)
		{
			PlayerAvatar componentInParent = ((Component)val).GetComponentInParent<PlayerAvatar>();
			if ((Object)(object)componentInParent != (Object)null)
			{
				TumblePlayerFromExplosion(componentInParent, position);
			}
		}
	}

	private static void TumblePlayerFromExplosion(PlayerAvatar player, Vector3 explosionCenter)
	{
		//IL_0007: Unknown result type (might be due to invalid IL or missing references)
		//IL_000c: Unknown result type (might be due to invalid IL or missing references)
		//IL_000d: Unknown result type (might be due to invalid IL or missing references)
		//IL_0012: Unknown result type (might be due to invalid IL or missing references)
		//IL_0015: Unknown result type (might be due to invalid IL or missing references)
		//IL_001a: Unknown result type (might be due to invalid IL or missing references)
		//IL_002f: Unknown result type (might be due to invalid IL or missing references)
		//IL_0035: Unknown result type (might be due to invalid IL or missing references)
		//IL_0046: Unknown result type (might be due to invalid IL or missing references)
		//IL_0050: Unknown result type (might be due to invalid IL or missing references)
		Vector3 val = ((Component)player).transform.position - explosionCenter;
		Vector3 normalized = ((Vector3)(ref val)).normalized;
		player.tumble.TumbleRequest(true, false);
		player.tumble.TumbleForce(normalized * 20f);
		player.tumble.TumbleTorque(Random.insideUnitSphere * 60f);
		player.tumble.TumbleOverrideTime(4f);
		player.tumble.ImpactHurtSet(4f, 50);
	}

	private static void TumbleCart(PhysGrabCart cart)
	{
		//IL_0012: Unknown result type (might be due to invalid IL or missing references)
		//IL_0017: Unknown result type (might be due to invalid IL or missing references)
		//IL_001a: Unknown result type (might be due to invalid IL or missing references)
		//IL_0024: Unknown result type (might be due to invalid IL or missing references)
		//IL_0029: Unknown result type (might be due to invalid IL or missing references)
		//IL_002a: Unknown result type (might be due to invalid IL or missing references)
		//IL_0034: Unknown result type (might be due to invalid IL or missing references)
		//IL_003e: Unknown result type (might be due to invalid IL or missing references)
		//IL_0043: Unknown result type (might be due to invalid IL or missing references)
		//IL_004a: Unknown result type (might be due to invalid IL or missing references)
		//IL_004b: Unknown result type (might be due to invalid IL or missing references)
		//IL_004c: Unknown result type (might be due to invalid IL or missing references)
		//IL_0056: Unknown result type (might be due to invalid IL or missing references)
		//IL_0068: Unknown result type (might be due to invalid IL or missing references)
		//IL_0072: Unknown result type (might be due to invalid IL or missing references)
		if ((Object)(object)cart.rb != (Object)null)
		{
			Vector3 insideUnitSphere = Random.insideUnitSphere;
			Vector3 val = ((Vector3)(ref insideUnitSphere)).normalized * 3f;
			Vector3 val2 = Vector3.up * 3f * 0.5f;
			cart.rb.AddForce((val + val2) * 3f, (ForceMode)1);
			cart.rb.AddTorque(Random.insideUnitSphere * 3f, (ForceMode)1);
		}
	}

	private static void LaunchCartItems(PhysGrabCart cart)
	{
		//IL_0019: Unknown result type (might be due to invalid IL or missing references)
		//IL_001e: Unknown result type (might be due to invalid IL or missing references)
		//IL_0058: Unknown result type (might be due to invalid IL or missing references)
		//IL_005d: Unknown result type (might be due to invalid IL or missing references)
		//IL_0061: Unknown result type (might be due to invalid IL or missing references)
		//IL_0066: Unknown result type (might be due to invalid IL or missing references)
		//IL_006a: Unknown result type (might be due to invalid IL or missing references)
		//IL_0092: Unknown result type (might be due to invalid IL or missing references)
		//IL_0096: Unknown result type (might be due to invalid IL or missing references)
		//IL_00a8: Unknown result type (might be due to invalid IL or missing references)
		//IL_00b2: Unknown result type (might be due to invalid IL or missing references)
		if (cart.itemsInCart == null)
		{
			return;
		}
		Vector3 position = ((Component)cart).transform.position;
		foreach (PhysGrabObject item in new List<PhysGrabObject>(cart.itemsInCart))
		{
			if ((Object)(object)item?.rb != (Object)null)
			{
				Vector3 insideUnitSphere = Random.insideUnitSphere;
				Vector3 normalized = ((Vector3)(ref insideUnitSphere)).normalized;
				normalized.y = Mathf.Abs(normalized.y);
				float num = Random.Range(8f, 15f);
				item.rb.AddForce(normalized * num, (ForceMode)1);
				item.rb.AddTorque(Random.insideUnitSphere * 5f, (ForceMode)1);
			}
		}
	}

	[HarmonyPatch(typeof(LevelGenerator), "Generate")]
	[HarmonyPostfix]
	private static void ResetExplosionTracking()
	{
		explodedCarts.Clear();
	}

	[HarmonyPatch(typeof(PhysGrabCart), "Update")]
	[HarmonyPostfix]
	private static void PeriodicCleanup(PhysGrabCart __instance)
	{
		if (!(Time.time % 30f < Time.deltaTime))
		{
			return;
		}
		HashSet<int> hashSet = new HashSet<int>();
		foreach (int explodedCart in explodedCarts)
		{
			hashSet.Add(explodedCart);
		}
		explodedCarts = hashSet;
	}
}
namespace ExplodingCart
{
	[BepInPlugin("Omniscye.ExplodingCart", "ExplodingCart", "1.0")]
	public class ExplodingCart : BaseUnityPlugin
	{
		internal static ExplodingCart Instance { get; private set; }

		internal static ManualLogSource Logger => Instance._logger;

		private ManualLogSource _logger => ((BaseUnityPlugin)this).Logger;

		internal Harmony? Harmony { get; set; }

		private void Awake()
		{
			Instance = this;
			((Component)this).gameObject.transform.parent = null;
			((Object)((Component)this).gameObject).hideFlags = (HideFlags)61;
			Patch();
			Logger.LogInfo((object)$"{((BaseUnityPlugin)this).Info.Metadata.GUID} v{((BaseUnityPlugin)this).Info.Metadata.Version} has loaded!");
		}

		internal void Patch()
		{
			//IL_001a: Unknown result type (might be due to invalid IL or missing references)
			//IL_001f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0021: Expected O, but got Unknown
			//IL_0026: Expected O, but got Unknown
			if (Harmony == null)
			{
				Harmony val = new Harmony(((BaseUnityPlugin)this).Info.Metadata.GUID);
				Harmony val2 = val;
				Harmony = val;
			}
			Harmony.PatchAll();
		}

		internal void Unpatch()
		{
			Harmony? harmony = Harmony;
			if (harmony != null)
			{
				harmony.UnpatchSelf();
			}
		}

		private void Update()
		{
		}
	}
}