Decompiled source of BreakablePeas v0.4.2

plugins/BreakablePeas.dll

Decompiled a week ago
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Core.Logging.Interpolation;
using BepInEx.Logging;
using BepInEx.Unity.Mono;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using Obstacles;
using Obstacles.ObstacleGameModifiers;
using Player;
using Unity.Netcode;
using UnityEngine;
using UnityEngine.Events;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: TargetFramework(".NETStandard,Version=v2.1", FrameworkDisplayName = ".NET Standard 2.1")]
[assembly: AssemblyCompany("BreakablePeas")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("0.4.2.0")]
[assembly: AssemblyInformationalVersion("0.4.2+25bd57d2248d6863e98bdfd75bfd7599707267f5")]
[assembly: AssemblyProduct("BreakablePeas")]
[assembly: AssemblyTitle("BreakablePeas")]
[assembly: AssemblyVersion("0.4.2.0")]
[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 BreakablePeas
{
	[BepInPlugin("peapack.breakablepeas", "BreakablePeas", "0.4.2")]
	public class Plugin : BaseUnityPlugin
	{
		internal static ManualLogSource Log;

		internal static ConfigEntry<float> FallbackBreakSpeed;

		internal static ConfigEntry<int> DebrisCount;

		internal static float BreakSpeed;

		internal static float BreakDelay;

		private const string PeaId = "bed8133d-50d9-43f9-a313-d5331e2727a6";

		private void Awake()
		{
			Log = ((BaseUnityPlugin)this).Logger;
			FallbackBreakSpeed = ((BaseUnityPlugin)this).Config.Bind<float>("Breaking", "FallbackBreakSpeed", 6f, "Impact speed (m/s) used only if the chocolate bar's own break speed can't be found.");
			DebrisCount = ((BaseUnityPlugin)this).Config.Bind<int>("Breaking", "DebrisCount", 6, "Number of little fragments spawned when a pea breaks (0 = none).");
			Harmony.CreateAndPatchAll(typeof(Plugin), (string)null);
			((MonoBehaviour)this).StartCoroutine(ScanLoop());
			Log.LogInfo((object)"BreakablePeas loaded.");
		}

		[HarmonyPatch(typeof(Obstacle), "Respawn")]
		[HarmonyPrefix]
		private static bool SkipRespawnIfBroken(Obstacle __instance)
		{
			PeaBreakable component = ((Component)__instance).GetComponent<PeaBreakable>();
			if (!((Object)(object)component == (Object)null))
			{
				return !component.Broken;
			}
			return true;
		}

		private static void ReadChocolateSettings()
		{
			//IL_00c6: Unknown result type (might be due to invalid IL or missing references)
			//IL_00cc: Expected O, but got Unknown
			//IL_0075: Unknown result type (might be due to invalid IL or missing references)
			//IL_007b: Expected O, but got Unknown
			BreakSpeed = FallbackBreakSpeed.Value;
			ZoneWithMinSpeed val = (from d in Resources.FindObjectsOfTypeAll<ObstacleDescription>()
				where ((Object)d).name.ToLowerInvariant().Contains("chocolate") && (Object)(object)d.prefab != (Object)null
				select d).SelectMany((ObstacleDescription d) => ((Component)d.prefab).GetComponentsInChildren<ZoneWithMinSpeed>(true)).FirstOrDefault();
			bool flag = default(bool);
			if ((Object)(object)val == (Object)null)
			{
				ManualLogSource log = Log;
				BepInExWarningLogInterpolatedStringHandler val2 = new BepInExWarningLogInterpolatedStringHandler(58, 1, ref flag);
				if (flag)
				{
					((BepInExLogInterpolatedStringHandler)val2).AppendLiteral("Chocolate break zone not found, using FallbackBreakSpeed ");
					((BepInExLogInterpolatedStringHandler)val2).AppendFormatted<float>(BreakSpeed);
					((BepInExLogInterpolatedStringHandler)val2).AppendLiteral(".");
				}
				log.LogWarning(val2);
				return;
			}
			BreakSpeed = val.minSpeed;
			BreakDelay = val.delay;
			ManualLogSource log2 = Log;
			BepInExInfoLogInterpolatedStringHandler val3 = new BepInExInfoLogInterpolatedStringHandler(58, 3, ref flag);
			if (flag)
			{
				((BepInExLogInterpolatedStringHandler)val3).AppendLiteral("Using chocolate break settings from '");
				((BepInExLogInterpolatedStringHandler)val3).AppendFormatted<string>(((Object)val).name);
				((BepInExLogInterpolatedStringHandler)val3).AppendLiteral("': minSpeed ");
				((BepInExLogInterpolatedStringHandler)val3).AppendFormatted<float>(BreakSpeed);
				((BepInExLogInterpolatedStringHandler)val3).AppendLiteral(", delay ");
				((BepInExLogInterpolatedStringHandler)val3).AppendFormatted<float>(BreakDelay);
				((BepInExLogInterpolatedStringHandler)val3).AppendLiteral(".");
			}
			log2.LogInfo(val3);
		}

		private static HashSet<ObstacleDescription> PeaDescriptions()
		{
			HashSet<ObstacleDescription> hashSet = new HashSet<ObstacleDescription>(from d in Resources.FindObjectsOfTypeAll<ObstacleDescription>()
				where d.id != null && ((object)d.id).ToString() == "bed8133d-50d9-43f9-a313-d5331e2727a6"
				select d);
			hashSet.UnionWith(from m in Resources.FindObjectsOfTypeAll<ObstacleGameModifierSpawnObstacleOnPlayerDash>()
				select m.obstacleToSpawnOnDash into d
				where (Object)(object)d != (Object)null
				select d);
			return hashSet;
		}

		private IEnumerator ScanLoop()
		{
			WaitForSeconds wait = new WaitForSeconds(1f);
			bool settingsRead = false;
			while (true)
			{
				HashSet<ObstacleDescription> hashSet = PeaDescriptions();
				if (hashSet.Count > 0 && !settingsRead)
				{
					ReadChocolateSettings();
					Log.LogInfo((object)("Pea obstacles: " + string.Join(", ", hashSet.Select((ObstacleDescription d) => ((Object)d).name))));
					settingsRead = true;
				}
				Obstacle[] array = Object.FindObjectsOfType<Obstacle>();
				foreach (Obstacle val in array)
				{
					if (!val.Destroyed && hashSet.Contains(val.description) && !((Object)(object)((Component)val).GetComponent<PeaBreakable>() != (Object)null))
					{
						((Component)val).gameObject.AddComponent<PeaBreakable>();
					}
				}
				yield return wait;
			}
		}
	}
	public class PeaBreakable : MonoBehaviour
	{
		public bool Broken;

		private void Start()
		{
			//IL_0014: Unknown result type (might be due to invalid IL or missing references)
			//IL_001e: Expected O, but got Unknown
			//IL_002b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0035: Expected O, but got Unknown
			Obstacle component = ((Component)this).GetComponent<Obstacle>();
			component.onBeforeDestroy.AddListener((UnityAction)delegate
			{
				Broken = true;
			});
			component.onBeforeDestroy.AddListener(new UnityAction(SpawnDebris));
			Collider[] componentsInChildren = ((Component)this).GetComponentsInChildren<Collider>(true);
			for (int num = 0; num < componentsInChildren.Length; num++)
			{
				((Component)componentsInChildren[num]).gameObject.AddComponent<PeaHitDetector>().obstacle = component;
			}
		}

		private void SpawnDebris()
		{
			//IL_0055: Unknown result type (might be due to invalid IL or missing references)
			//IL_0039: 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_0048: Unknown result type (might be due to invalid IL or missing references)
			//IL_004d: Unknown result type (might be due to invalid IL or missing references)
			//IL_005a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0065: Unknown result type (might be due to invalid IL or missing references)
			//IL_0074: Unknown result type (might be due to invalid IL or missing references)
			//IL_0083: Unknown result type (might be due to invalid IL or missing references)
			//IL_00e4: Unknown result type (might be due to invalid IL or missing references)
			//IL_00e9: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ed: Unknown result type (might be due to invalid IL or missing references)
			//IL_00f2: Unknown result type (might be due to invalid IL or missing references)
			//IL_00fd: Unknown result type (might be due to invalid IL or missing references)
			//IL_0102: Unknown result type (might be due to invalid IL or missing references)
			//IL_0106: Unknown result type (might be due to invalid IL or missing references)
			//IL_010b: Unknown result type (might be due to invalid IL or missing references)
			//IL_011c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0123: Unknown result type (might be due to invalid IL or missing references)
			//IL_0137: Unknown result type (might be due to invalid IL or missing references)
			//IL_015e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0165: Unknown result type (might be due to invalid IL or missing references)
			int value = Plugin.DebrisCount.Value;
			if (value <= 0)
			{
				return;
			}
			Renderer componentInChildren = ((Component)this).GetComponentInChildren<Renderer>();
			Material val = (((Object)(object)componentInChildren != (Object)null) ? componentInChildren.sharedMaterial : null);
			Bounds val2 = (Bounds)(((Object)(object)componentInChildren != (Object)null) ? componentInChildren.bounds : new Bounds(((Component)this).transform.position, Vector3.one * 0.2f));
			float num = Mathf.Max(new float[3]
			{
				((Bounds)(ref val2)).size.x,
				((Bounds)(ref val2)).size.y,
				((Bounds)(ref val2)).size.z
			}) * 0.3f;
			for (int i = 0; i < value; i++)
			{
				GameObject val3 = GameObject.CreatePrimitive((PrimitiveType)0);
				((Object)val3).name = "PeaFragment";
				Vector3 val4 = new Vector3(Random.Range(-1f, 1f), Random.Range(0f, 1f), Random.Range(-1f, 1f));
				Vector3 normalized = ((Vector3)(ref val4)).normalized;
				val3.transform.position = ((Bounds)(ref val2)).center + normalized * num;
				val3.transform.localScale = Vector3.one * num * Random.Range(0.6f, 1.2f);
				if ((Object)(object)val != (Object)null)
				{
					val3.GetComponent<Renderer>().sharedMaterial = val;
				}
				val3.AddComponent<Rigidbody>().velocity = normalized * 3f;
				Object.Destroy((Object)(object)val3, 3f);
			}
		}
	}
	public class PeaHitDetector : MonoBehaviour
	{
		public Obstacle obstacle;

		private bool _hit;

		private void OnCollisionEnter(Collision collision)
		{
			//IL_0025: 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)
			if ((Object)(object)collision.body == (Object)null)
			{
				return;
			}
			PlayerNetworking componentInParent = collision.body.GetComponentInParent<PlayerNetworking>();
			if ((Object)(object)componentInParent != (Object)null)
			{
				Vector3 relativeVelocity = collision.relativeVelocity;
				if (((Vector3)(ref relativeVelocity)).sqrMagnitude > Plugin.BreakSpeed * Plugin.BreakSpeed)
				{
					Break(componentInParent);
				}
			}
		}

		private void OnTriggerEnter(Collider other)
		{
			//IL_0029: Unknown result type (might be due to invalid IL or missing references)
			//IL_002e: Unknown result type (might be due to invalid IL or missing references)
			PlayerNetworking componentInParent = ((Component)other).GetComponentInParent<PlayerNetworking>();
			if ((Object)(object)componentInParent != (Object)null && (Object)(object)componentInParent.carController != (Object)null)
			{
				Vector3 velocity = componentInParent.carController.sphereRigidbody.velocity;
				if (((Vector3)(ref velocity)).sqrMagnitude > Plugin.BreakSpeed * Plugin.BreakSpeed)
				{
					Break(componentInParent);
				}
			}
		}

		private void Break(PlayerNetworking player)
		{
			if (!_hit && ((NetworkBehaviour)player).IsOwner && !((Object)(object)obstacle == (Object)null) && !obstacle.Destroyed)
			{
				_hit = true;
				if (Plugin.BreakDelay > 0.01f)
				{
					((MonoBehaviour)obstacle).StartCoroutine(DelayedDestroy());
				}
				else
				{
					obstacle.Destroy();
				}
			}
		}

		private IEnumerator DelayedDestroy()
		{
			yield return (object)new WaitForSeconds(Plugin.BreakDelay);
			if ((Object)(object)obstacle != (Object)null)
			{
				obstacle.Destroy();
			}
		}
	}
	public static class MyPluginInfo
	{
		public const string PLUGIN_GUID = "peapack.breakablepeas";

		public const string PLUGIN_NAME = "BreakablePeas";

		public const string PLUGIN_VERSION = "0.4.2";
	}
}