Decompiled source of Crumble v1.0.0

Mods/Crumble.dll

Decompiled 9 hours ago
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using HarmonyLib;
using Il2CppInterop.Runtime.Injection;
using Il2CppInterop.Runtime.InteropTypes;
using Il2CppInterop.Runtime.InteropTypes.Arrays;
using Il2CppRUMBLE.MoveSystem;
using Il2CppSystem;
using Il2CppTMPro;
using Main;
using MelonLoader;
using MelonLoader.Preferences;
using Microsoft.CodeAnalysis;
using RockUI;
using RumbleModdingAPI.RMAPI;
using UnityEngine;
using UnityEngine.Rendering;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: MelonInfo(typeof(global::Main.Main), "Crumble", "1.0.0", "Nano", null)]
[assembly: MelonColor(127, 52, 235, 131)]
[assembly: MelonGame("Buckethead Entertainment", "RUMBLE")]
[assembly: MelonAdditionalDependencies(new string[] { "RockUI" })]
[assembly: TargetFramework(".NETCoreApp,Version=v6.0", FrameworkDisplayName = ".NET 6.0")]
[assembly: AssemblyVersion("0.0.0.0")]
[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.Module, AllowMultiple = false, Inherited = false)]
	internal sealed class RefSafetyRulesAttribute : Attribute
	{
		public readonly int Version;

		public RefSafetyRulesAttribute(int P_0)
		{
			Version = P_0;
		}
	}
}
namespace Main
{
	public static class ChunkCache
	{
		[CompilerGenerated]
		private sealed class <PreWarm>d__4 : IEnumerator<object>, IEnumerator, IDisposable
		{
			private int <>1__state;

			private object <>2__current;

			private HashSet<int> <seen>5__2;

			private IEnumerator<Structure> <>7__wrap2;

			object IEnumerator<object>.Current
			{
				[DebuggerHidden]
				get
				{
					return <>2__current;
				}
			}

			object IEnumerator.Current
			{
				[DebuggerHidden]
				get
				{
					return <>2__current;
				}
			}

			[DebuggerHidden]
			public <PreWarm>d__4(int <>1__state)
			{
				this.<>1__state = <>1__state;
			}

			[DebuggerHidden]
			void IDisposable.Dispose()
			{
				int num = <>1__state;
				if (num == -3 || num == 1)
				{
					try
					{
					}
					finally
					{
						<>m__Finally1();
					}
				}
				<seen>5__2 = null;
				<>7__wrap2 = null;
				<>1__state = -2;
			}

			private bool MoveNext()
			{
				//IL_0096: Unknown result type (might be due to invalid IL or missing references)
				try
				{
					switch (<>1__state)
					{
					default:
						return false;
					case 0:
					{
						<>1__state = -1;
						<seen>5__2 = new HashSet<int>();
						Il2CppArrayBase<Structure> val = Object.FindObjectsOfType<Structure>();
						<>7__wrap2 = val.GetEnumerator();
						<>1__state = -3;
						break;
					}
					case 1:
						<>1__state = -3;
						break;
					}
					while (<>7__wrap2.MoveNext())
					{
						MeshFilter componentInChildren = ((Component)<>7__wrap2.Current).GetComponentInChildren<MeshFilter>();
						if (!((Object)(object)componentInChildren == (Object)null) && !((Object)(object)componentInChildren.sharedMesh == (Object)null))
						{
							int instanceID = ((Object)componentInChildren.sharedMesh).GetInstanceID();
							if (<seen>5__2.Add(instanceID))
							{
								Build(componentInChildren.sharedMesh, componentInChildren.sharedMesh.bounds);
								<>2__current = null;
								<>1__state = 1;
								return true;
							}
						}
					}
					<>m__Finally1();
					<>7__wrap2 = null;
					Preferences.Log($"Pre-warm complete: {<seen>5__2.Count} structure meshes cached.");
					return false;
				}
				catch
				{
					//try-fault
					((IDisposable)this).Dispose();
					throw;
				}
			}

			bool IEnumerator.MoveNext()
			{
				//ILSpy generated this explicit interface implementation from .override directive in MoveNext
				return this.MoveNext();
			}

			private void <>m__Finally1()
			{
				<>1__state = -1;
				if (<>7__wrap2 != null)
				{
					<>7__wrap2.Dispose();
				}
			}

			[DebuggerHidden]
			void IEnumerator.Reset()
			{
				throw new NotSupportedException();
			}
		}

		private static readonly Dictionary<string, ChunkData[]> Cache = new Dictionary<string, ChunkData[]>();

		private static string MakeKey(Mesh mesh, int count, bool minecraft)
		{
			return $"{((Object)mesh).GetInstanceID()}|{count}|{minecraft}";
		}

		public static bool TryGet(Mesh mesh, out ChunkData[] chunks)
		{
			int value = Preferences.ChunksPerBreak.Value;
			bool value2 = Preferences.ChunkStyleMinecraft.Value;
			return Cache.TryGetValue(MakeKey(mesh, value, value2), out chunks);
		}

		public static ChunkData[] Build(Mesh mesh, Bounds bounds)
		{
			//IL_003f: Unknown result type (might be due to invalid IL or missing references)
			//IL_015f: Unknown result type (might be due to invalid IL or missing references)
			int value = Preferences.ChunksPerBreak.Value;
			bool value2 = Preferences.ChunkStyleMinecraft.Value;
			string key = MakeKey(mesh, value, value2);
			if (Cache.TryGetValue(key, out var value3))
			{
				return value3;
			}
			float value4 = Preferences.SliceRandomness.Value;
			ChunkData[] chunks;
			if (value2)
			{
				chunks = ShardClusterBuilder.Build(bounds, value, value4);
				Preferences.Log($"Built {chunks.Length} shards (minecraft) for mesh {((Object)mesh).GetInstanceID()}.");
			}
			else
			{
				Mesh val = (mesh.isReadable ? mesh : null);
				bool flag = false;
				if ((Object)(object)val == (Object)null && MeshReadback.TryMakeReadable(mesh, out var readable))
				{
					val = readable;
					flag = true;
				}
				if ((Object)(object)val != (Object)null && MeshSlicer.TrySlice(val, value, value4, out chunks))
				{
					Preferences.Log($"Sliced mesh {((Object)mesh).GetInstanceID()} into {chunks.Length} ({(flag ? "GPU readback" : "readable")}).");
				}
				else
				{
					chunks = ShardClusterBuilder.Build(bounds, value, value4);
					Preferences.Log($"Mesh {((Object)mesh).GetInstanceID()} not sliceable; built {chunks.Length} shards.");
				}
			}
			Cache[key] = chunks;
			return chunks;
		}

		[IteratorStateMachine(typeof(<PreWarm>d__4))]
		public static IEnumerator PreWarm()
		{
			//yield-return decompiler failed: Unexpected instruction in Iterator.Dispose()
			return new <PreWarm>d__4(0);
		}

		public static void Clear()
		{
			foreach (ChunkData[] value in Cache.Values)
			{
				for (int i = 0; i < value.Length; i++)
				{
					if ((Object)(object)value[i].Mesh != (Object)null)
					{
						Object.Destroy((Object)(object)value[i].Mesh);
					}
				}
			}
			Cache.Clear();
		}
	}
	public struct ChunkData
	{
		public Mesh Mesh;

		public Vector3 LocalOffset;

		public ChunkData(Mesh mesh, Vector3 localOffset)
		{
			//IL_0008: Unknown result type (might be due to invalid IL or missing references)
			//IL_0009: Unknown result type (might be due to invalid IL or missing references)
			Mesh = mesh;
			LocalOffset = localOffset;
		}
	}
	internal static class CrumbleUI
	{
		private const float PanelW = 10.5f;

		private const float PanelH = 15.5f;

		private const float TitleY = 6f;

		private const float ModeLabelY = 3.7f;

		private const float ModeStateY = 4.5f;

		private const float ModeLeverY = 3f;

		private const float ChunksLabelY = 0.5f;

		private const float ChunksValueY = 1.3f;

		private const float SliderY = -1.2f;

		private const float StyleLabelY = -3.5f;

		private const float StyleStateY = -2.7f;

		private const float StyleLeverY = -4.3f;

		private const float LeftColX = -3.5f;

		private const float RightColX = 3.5f;

		private const float CentreX = 0f;

		private const float Distance = 3f;

		private const float HeightDrop = 0.3f;

		private static GameObject _panel;

		private static TextMeshPro _modeTmp;

		private static TextMeshPro _styleTmp;

		private static TextMeshPro _chunksTmp;

		private const string ModeStateTag = "__MODE_STATE__";

		private const string StyleStateTag = "__STYLE_STATE__";

		private const string ChunksValueTag = "__CHUNKS_VALUE__";

		public static bool IsOpen => (Object)(object)_panel != (Object)null;

		public static void Toggle()
		{
			if (IsOpen)
			{
				Close();
			}
			else
			{
				Open();
			}
		}

		public static void Close()
		{
			if ((Object)(object)_panel != (Object)null)
			{
				Object.Destroy((Object)(object)_panel);
				_panel = null;
			}
			_modeTmp = null;
			_styleTmp = null;
			_chunksTmp = null;
		}

		public static void Open()
		{
			if (IsOpen)
			{
				return;
			}
			try
			{
				BuildPanel();
			}
			catch (Exception value)
			{
				MelonLogger.Error($"[Crumble] CrumbleUI.Open failed: {value}");
			}
		}

		private static void BuildPanel()
		{
			//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_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_0062: Unknown result type (might be due to invalid IL or missing references)
			//IL_0069: Expected O, but got Unknown
			//IL_007f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0084: Unknown result type (might be due to invalid IL or missing references)
			//IL_008c: Unknown result type (might be due to invalid IL or missing references)
			//IL_009d: Unknown result type (might be due to invalid IL or missing references)
			//IL_00a2: Unknown result type (might be due to invalid IL or missing references)
			//IL_00a7: Unknown result type (might be due to invalid IL or missing references)
			//IL_00af: Unknown result type (might be due to invalid IL or missing references)
			//IL_00b6: Expected O, but got Unknown
			//IL_00cc: Unknown result type (might be due to invalid IL or missing references)
			//IL_00d1: Unknown result type (might be due to invalid IL or missing references)
			//IL_00d9: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ea: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ef: Unknown result type (might be due to invalid IL or missing references)
			//IL_00f4: Unknown result type (might be due to invalid IL or missing references)
			//IL_00fc: Unknown result type (might be due to invalid IL or missing references)
			//IL_0103: Expected O, but got Unknown
			//IL_011f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0124: Unknown result type (might be due to invalid IL or missing references)
			//IL_012c: Unknown result type (might be due to invalid IL or missing references)
			//IL_013d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0142: Unknown result type (might be due to invalid IL or missing references)
			//IL_0147: Unknown result type (might be due to invalid IL or missing references)
			//IL_014f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0156: Expected O, but got Unknown
			//IL_0159: Unknown result type (might be due to invalid IL or missing references)
			//IL_016a: Unknown result type (might be due to invalid IL or missing references)
			//IL_016f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0196: Unknown result type (might be due to invalid IL or missing references)
			//IL_019e: Unknown result type (might be due to invalid IL or missing references)
			//IL_01a5: Expected O, but got Unknown
			//IL_01bb: Unknown result type (might be due to invalid IL or missing references)
			//IL_01c0: Unknown result type (might be due to invalid IL or missing references)
			//IL_01c8: Unknown result type (might be due to invalid IL or missing references)
			//IL_01d9: Unknown result type (might be due to invalid IL or missing references)
			//IL_01de: Unknown result type (might be due to invalid IL or missing references)
			//IL_01e3: Unknown result type (might be due to invalid IL or missing references)
			//IL_01eb: Unknown result type (might be due to invalid IL or missing references)
			//IL_01f2: Expected O, but got Unknown
			//IL_020f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0214: Unknown result type (might be due to invalid IL or missing references)
			//IL_021c: Unknown result type (might be due to invalid IL or missing references)
			//IL_022d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0232: Unknown result type (might be due to invalid IL or missing references)
			//IL_0237: Unknown result type (might be due to invalid IL or missing references)
			//IL_023f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0246: Expected O, but got Unknown
			//IL_025a: Unknown result type (might be due to invalid IL or missing references)
			//IL_026b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0270: Unknown result type (might be due to invalid IL or missing references)
			//IL_0297: Unknown result type (might be due to invalid IL or missing references)
			//IL_029f: Unknown result type (might be due to invalid IL or missing references)
			//IL_02a6: Expected O, but got Unknown
			//IL_02bc: Unknown result type (might be due to invalid IL or missing references)
			//IL_02c1: Unknown result type (might be due to invalid IL or missing references)
			//IL_02c9: Unknown result type (might be due to invalid IL or missing references)
			//IL_02da: Unknown result type (might be due to invalid IL or missing references)
			//IL_02df: Unknown result type (might be due to invalid IL or missing references)
			//IL_02e4: Unknown result type (might be due to invalid IL or missing references)
			//IL_02ec: Unknown result type (might be due to invalid IL or missing references)
			//IL_02f3: Expected O, but got Unknown
			//IL_030f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0314: Unknown result type (might be due to invalid IL or missing references)
			//IL_031c: Unknown result type (might be due to invalid IL or missing references)
			//IL_032d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0332: Unknown result type (might be due to invalid IL or missing references)
			//IL_0337: Unknown result type (might be due to invalid IL or missing references)
			//IL_033f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0346: Expected O, but got Unknown
			//IL_0349: Unknown result type (might be due to invalid IL or missing references)
			//IL_035a: Unknown result type (might be due to invalid IL or missing references)
			//IL_035f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0386: Unknown result type (might be due to invalid IL or missing references)
			//IL_0394: Expected O, but got Unknown
			bool value = Preferences.PersistUntilSceneChange.Value;
			bool value2 = Preferences.ChunkStyleMinecraft.Value;
			int value3 = Preferences.ChunksPerBreak.Value;
			string text = (value ? "Chaos" : "Default");
			string text2 = (value2 ? "Minecraft" : "Real slices");
			string text3 = value3.ToString();
			RockPanel val = new RockPanel
			{
				size = new Vector2(10.5f, 15.5f)
			};
			RockText val2 = new RockText();
			val2.text = "Crumble";
			val2.fontSize = 2;
			val2.fontColor = Color.white;
			((UIElement)val2).anchor = (AnchorType)8;
			((UIElement)val2).anchorOffset = new Vector2(0f, 6f);
			((UIElement)val).AddChildUI((UIElement)(object)val2);
			RockText val3 = new RockText();
			val3.text = "Mode:";
			val3.fontSize = 1;
			val3.fontColor = Color.white;
			((UIElement)val3).anchor = (AnchorType)8;
			((UIElement)val3).anchorOffset = new Vector2(-3.5f, 3.7f);
			((UIElement)val).AddChildUI((UIElement)(object)val3);
			RockText val4 = new RockText();
			val4.text = "__MODE_STATE__" + text;
			val4.fontSize = 1;
			val4.fontColor = Color.cyan;
			((UIElement)val4).anchor = (AnchorType)8;
			((UIElement)val4).anchorOffset = new Vector2(3.5f, 4.5f);
			((UIElement)val).AddChildUI((UIElement)(object)val4);
			RockLever val5 = new RockLever();
			((UIElement)val5).anchor = (AnchorType)8;
			((UIElement)val5).anchorOffset = new Vector2(3.5f, 3f);
			val5.leverToggledAction = OnModeLever;
			((UIElement)val).AddChildUI((UIElement)(object)val5);
			RockText val6 = new RockText();
			val6.text = "Chunks:";
			val6.fontSize = 1;
			val6.fontColor = Color.white;
			((UIElement)val6).anchor = (AnchorType)8;
			((UIElement)val6).anchorOffset = new Vector2(-3.5f, 0.5f);
			((UIElement)val).AddChildUI((UIElement)(object)val6);
			RockText val7 = new RockText();
			val7.text = "__CHUNKS_VALUE__" + text3;
			val7.fontSize = 1;
			val7.fontColor = Color.yellow;
			((UIElement)val7).anchor = (AnchorType)8;
			((UIElement)val7).anchorOffset = new Vector2(3.5f, 1.3f);
			((UIElement)val).AddChildUI((UIElement)(object)val7);
			RockSlider val8 = new RockSlider();
			val8.useSteps = true;
			val8.stepCount = 10;
			((UIElement)val8).anchor = (AnchorType)8;
			((UIElement)val8).anchorOffset = new Vector2(0f, -1.2f);
			val8.stepReachedAction = OnChunksSlider;
			((UIElement)val).AddChildUI((UIElement)(object)val8);
			RockText val9 = new RockText();
			val9.text = "Style:";
			val9.fontSize = 1;
			val9.fontColor = Color.white;
			((UIElement)val9).anchor = (AnchorType)8;
			((UIElement)val9).anchorOffset = new Vector2(-3.5f, -3.5f);
			((UIElement)val).AddChildUI((UIElement)(object)val9);
			RockText val10 = new RockText();
			val10.text = "__STYLE_STATE__" + text2;
			val10.fontSize = 1;
			val10.fontColor = Color.cyan;
			((UIElement)val10).anchor = (AnchorType)8;
			((UIElement)val10).anchorOffset = new Vector2(3.5f, -2.7f);
			((UIElement)val).AddChildUI((UIElement)(object)val10);
			RockLever val11 = new RockLever();
			((UIElement)val11).anchor = (AnchorType)8;
			((UIElement)val11).anchorOffset = new Vector2(3.5f, -4.3f);
			val11.leverToggledAction = OnStyleLever;
			((UIElement)val).AddChildUI((UIElement)(object)val11);
			_panel = RockUI.CreateFinalisedUI((UIElement)val, false);
			MelonLogger.Msg("[Crumble] Settings panel built.");
			ResolveTMPRefs();
			SetText(_modeTmp, text);
			SetText(_styleTmp, text2);
			SetText(_chunksTmp, text3);
			PositionPanel();
		}

		private static void ResolveTMPRefs()
		{
			if ((Object)(object)_panel == (Object)null)
			{
				return;
			}
			Il2CppArrayBase<TextMeshPro> componentsInChildren = _panel.GetComponentsInChildren<TextMeshPro>(true);
			if (componentsInChildren == null)
			{
				return;
			}
			foreach (TextMeshPro item in componentsInChildren)
			{
				if (!((Object)(object)item == (Object)null))
				{
					string text = ((TMP_Text)item).text ?? string.Empty;
					if (text.StartsWith("__MODE_STATE__"))
					{
						_modeTmp = item;
					}
					else if (text.StartsWith("__STYLE_STATE__"))
					{
						_styleTmp = item;
					}
					else if (text.StartsWith("__CHUNKS_VALUE__"))
					{
						_chunksTmp = item;
					}
				}
			}
			if ((Object)(object)_modeTmp == (Object)null)
			{
				MelonLogger.Warning("[Crumble] Could not find mode-state TMP.");
			}
			if ((Object)(object)_styleTmp == (Object)null)
			{
				MelonLogger.Warning("[Crumble] Could not find style-state TMP.");
			}
			if ((Object)(object)_chunksTmp == (Object)null)
			{
				MelonLogger.Warning("[Crumble] Could not find chunks-value TMP.");
			}
		}

		private static void PositionPanel()
		{
			//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_003f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0038: Unknown result type (might be due to invalid IL or missing references)
			//IL_0044: 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_0050: 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_005b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0060: Unknown result type (might be due to invalid IL or missing references)
			//IL_007b: Unknown result type (might be due to invalid IL or missing references)
			//IL_008b: Unknown result type (might be due to invalid IL or missing references)
			//IL_008c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0091: Unknown result type (might be due to invalid IL or missing references)
			Camera main = Camera.main;
			if (!((Object)(object)main == (Object)null))
			{
				Vector3 val = ((Component)main).transform.forward;
				val.y = 0f;
				val = ((((Vector3)(ref val)).sqrMagnitude < 0.001f) ? Vector3.forward : ((Vector3)(ref val)).normalized);
				Vector3 position = ((Component)main).transform.position + val * 3f;
				position.y -= 0.3f;
				_panel.transform.position = position;
				_panel.transform.rotation = Quaternion.LookRotation(val, Vector3.up);
			}
		}

		private static void OnModeLever(int v)
		{
			bool flag = v == 1;
			Preferences.PersistUntilSceneChange.Value = flag;
			SetText(_modeTmp, flag ? "Chaos" : "Default");
			MelonPreferences.Save();
		}

		private static void OnChunksSlider(int step)
		{
			int value = step + 1;
			Preferences.ChunksPerBreak.Value = value;
			SetText(_chunksTmp, value.ToString());
			MelonPreferences.Save();
		}

		private static void OnStyleLever(int v)
		{
			bool flag = v == 1;
			Preferences.ChunkStyleMinecraft.Value = flag;
			SetText(_styleTmp, flag ? "Minecraft" : "Real slices");
			MelonPreferences.Save();
		}

		private static void SetText(TextMeshPro tmp, string s)
		{
			if ((Object)(object)tmp == (Object)null)
			{
				return;
			}
			try
			{
				((TMP_Text)tmp).text = s;
			}
			catch (Exception ex)
			{
				MelonLogger.Error("[Crumble] SetText failed: " + ex.Message);
			}
		}
	}
	[RegisterTypeInIl2Cpp]
	public class DebrisChunk : MonoBehaviour
	{
		private static readonly LinkedList<DebrisChunk> Live = new LinkedList<DebrisChunk>();

		private LinkedListNode<DebrisChunk> _node;

		private float _age;

		private float _lifetime;

		private bool _dying;

		private float _initialScaleMax = 1f;

		private const float FadeDuration = 0.6f;

		private float _fadeAge;

		private bool _useAlphaFade;

		private Material _fadeMat;

		private string _fadeProp;

		private Color _startColor;

		public DebrisChunk(IntPtr ptr)
			: base(ptr)
		{
		}

		public void Init(float lifetime)
		{
			_lifetime = lifetime;
			_node = Live.AddLast(this);
			if (!Preferences.PersistUntilSceneChange.Value)
			{
				EnforceCap();
			}
		}

		private static void EnforceCap()
		{
			int value = Preferences.MaxConcurrentDebris.Value;
			while (Live.First != null && (Object)(object)Live.First.Value == (Object)null)
			{
				Live.RemoveFirst();
			}
			while (Live.Count > value)
			{
				DebrisChunk debrisChunk = Live.First?.Value;
				if ((Object)(object)debrisChunk == (Object)null)
				{
					Live.RemoveFirst();
				}
				else
				{
					debrisChunk.Kill();
				}
			}
		}

		private void Update()
		{
			//IL_00c0: Unknown result type (might be due to invalid IL or missing references)
			//IL_00c5: Unknown result type (might be due to invalid IL or missing references)
			//IL_00d5: Unknown result type (might be due to invalid IL or missing references)
			//IL_00e0: Unknown result type (might be due to invalid IL or missing references)
			//IL_00e5: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ea: Unknown result type (might be due to invalid IL or missing references)
			//IL_00eb: Unknown result type (might be due to invalid IL or missing references)
			//IL_00f1: Unknown result type (might be due to invalid IL or missing references)
			//IL_00f7: Unknown result type (might be due to invalid IL or missing references)
			//IL_0076: Unknown result type (might be due to invalid IL or missing references)
			//IL_007b: Unknown result type (might be due to invalid IL or missing references)
			//IL_00a5: Unknown result type (might be due to invalid IL or missing references)
			//IL_011b: Unknown result type (might be due to invalid IL or missing references)
			if (Preferences.PersistUntilSceneChange.Value && !_dying)
			{
				return;
			}
			_age += Time.deltaTime;
			if (!_dying && _age >= _lifetime)
			{
				BeginFade();
			}
			if (!_dying)
			{
				return;
			}
			_fadeAge += Time.deltaTime;
			if (_useAlphaFade)
			{
				float num = _fadeAge / 0.6f;
				Color startColor = _startColor;
				startColor.a = Mathf.Lerp(_startColor.a, 0f, num);
				_fadeMat.SetColor(_fadeProp, startColor);
				if (num >= 1f)
				{
					Kill();
				}
			}
			else
			{
				Vector3 val = ((Component)this).transform.localScale - Vector3.one * (Time.deltaTime / 0.5f) * _initialScaleMax;
				if (Mathf.Max(val.x, Mathf.Max(val.y, val.z)) <= 0f)
				{
					Kill();
				}
				else
				{
					((Component)this).transform.localScale = val;
				}
			}
		}

		private void BeginFade()
		{
			//IL_0119: Unknown result type (might be due to invalid IL or missing references)
			//IL_011e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0120: 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_012c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0101: 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)
			_dying = true;
			_fadeAge = 0f;
			MeshRenderer component = ((Component)this).GetComponent<MeshRenderer>();
			if ((Object)(object)component != (Object)null)
			{
				Material material = ((Renderer)component).material;
				string text = null;
				if (material.HasProperty("_BaseColor"))
				{
					text = "_BaseColor";
				}
				else if (material.HasProperty("_Color"))
				{
					text = "_Color";
				}
				if (text != null)
				{
					try
					{
						material.SetFloat("_Surface", 1f);
						material.SetFloat("_Mode", 3f);
						material.SetInt("_SrcBlend", 5);
						material.SetInt("_DstBlend", 10);
						material.SetInt("_ZWrite", 0);
						material.EnableKeyword("_SURFACE_TYPE_TRANSPARENT");
						material.EnableKeyword("_ALPHABLEND_ON");
						material.DisableKeyword("_ALPHATEST_ON");
						material.renderQueue = 3000;
					}
					catch (Exception ex)
					{
						MelonLogger.Warning("[Crumble] Alpha-fade shader setup failed, falling back to shrink: " + ex.Message);
						goto IL_010c;
					}
					_useAlphaFade = true;
					_fadeMat = material;
					_fadeProp = text;
					_startColor = material.GetColor(text);
					return;
				}
			}
			goto IL_010c;
			IL_010c:
			_useAlphaFade = false;
			Vector3 localScale = ((Component)this).transform.localScale;
			_initialScaleMax = Mathf.Max(localScale.x, Mathf.Max(localScale.y, localScale.z));
		}

		public void Kill()
		{
			if (_node != null)
			{
				Live.Remove(_node);
				_node = null;
			}
			if ((Object)(object)this != (Object)null && (Object)(object)((Component)this).gameObject != (Object)null)
			{
				Object.Destroy((Object)(object)((Component)this).gameObject);
			}
		}

		private void OnDestroy()
		{
			if (_node != null)
			{
				Live.Remove(_node);
				_node = null;
			}
		}
	}
	public static class DebrisLayers
	{
		public static int DebrisLayer = -1;

		private static readonly string[] CollideNames = new string[4] { "Floor", "CombatFloor", "Environment", "PedestalFloor" };

		public static void DumpLayers()
		{
			for (int i = 0; i < 32; i++)
			{
				string value = LayerMask.LayerToName(i);
				if (!string.IsNullOrEmpty(value))
				{
					Preferences.Log($"Layer {i} = '{value}'");
				}
			}
		}

		public static void Setup()
		{
			DebrisLayer = FindUnusedLayer();
			if (DebrisLayer < 0)
			{
				DebrisLayer = 26;
			}
			for (int i = 0; i < 32; i++)
			{
				Physics.IgnoreLayerCollision(DebrisLayer, i, true);
			}
			string[] collideNames = CollideNames;
			for (int j = 0; j < collideNames.Length; j++)
			{
				int num = LayerMask.NameToLayer(collideNames[j]);
				if (num >= 0)
				{
					Physics.IgnoreLayerCollision(DebrisLayer, num, false);
				}
			}
			Physics.IgnoreLayerCollision(DebrisLayer, DebrisLayer, false);
			Preferences.Log($"Debris layer = {DebrisLayer}");
		}

		private static int FindUnusedLayer()
		{
			for (int num = 31; num >= 8; num--)
			{
				if (string.IsNullOrEmpty(LayerMask.LayerToName(num)))
				{
					return num;
				}
			}
			return -1;
		}
	}
	public static class DebrisSpawner
	{
		public static void Spawn(Transform structure)
		{
			//IL_0051: 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_006d: Unknown result type (might be due to invalid IL or missing references)
			try
			{
				MeshRenderer componentInChildren = ((Component)structure).GetComponentInChildren<MeshRenderer>();
				MeshFilter componentInChildren2 = ((Component)structure).GetComponentInChildren<MeshFilter>();
				if ((Object)(object)componentInChildren == (Object)null || (Object)(object)componentInChildren2 == (Object)null || (Object)(object)componentInChildren2.sharedMesh == (Object)null)
				{
					Preferences.Log("No renderer/mesh on structure; skipping debris.");
					return;
				}
				Transform transform = ((Component)componentInChildren2).transform;
				Material sharedMaterial = ((Renderer)componentInChildren).sharedMaterial;
				Bounds bounds = componentInChildren2.sharedMesh.bounds;
				ChunkData[] chunks;
				ChunkData[] array = (ChunkCache.TryGet(componentInChildren2.sharedMesh, out chunks) ? chunks : ChunkCache.Build(componentInChildren2.sharedMesh, bounds));
				ChunkData[] array2 = array;
				for (int i = 0; i < array2.Length; i++)
				{
					SpawnOne(array2[i], transform, sharedMaterial);
				}
				Preferences.Log($"Spawned {array.Length} debris chunks.");
			}
			catch (Exception value)
			{
				MelonLogger.Error($"[Crumble] Spawn failed (gameplay unaffected): {value}");
			}
		}

		private static void SpawnOne(ChunkData c, Transform pivot, Material mat)
		{
			//IL_000e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0013: 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_0026: Unknown result type (might be due to invalid IL or missing references)
			//IL_002b: 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_003c: 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_006a: 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_007b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0085: 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_00ad: Unknown result type (might be due to invalid IL or missing references)
			//IL_00c5: Unknown result type (might be due to invalid IL or missing references)
			//IL_00e3: Unknown result type (might be due to invalid IL or missing references)
			//IL_00e8: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ef: Unknown result type (might be due to invalid IL or missing references)
			//IL_00f4: Unknown result type (might be due to invalid IL or missing references)
			//IL_00f7: Unknown result type (might be due to invalid IL or missing references)
			//IL_0107: Unknown result type (might be due to invalid IL or missing references)
			//IL_010c: Unknown result type (might be due to invalid IL or missing references)
			//IL_010f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0120: Unknown result type (might be due to invalid IL or missing references)
			//IL_0125: Unknown result type (might be due to invalid IL or missing references)
			//IL_0128: Unknown result type (might be due to invalid IL or missing references)
			//IL_0133: Unknown result type (might be due to invalid IL or missing references)
			//IL_0139: Unknown result type (might be due to invalid IL or missing references)
			//IL_0140: Unknown result type (might be due to invalid IL or missing references)
			if (DebrisLayers.DebrisLayer >= 0)
			{
				GameObject val = new GameObject("CrumbleChunk")
				{
					layer = DebrisLayers.DebrisLayer
				};
				val.transform.position = pivot.TransformPoint(c.LocalOffset);
				val.transform.rotation = pivot.rotation * Quaternion.Euler((float)Random.Range(0, 360), (float)Random.Range(0, 360), (float)Random.Range(0, 360));
				val.transform.localScale = pivot.lossyScale;
				val.AddComponent<MeshFilter>().sharedMesh = c.Mesh;
				MeshRenderer val2 = val.AddComponent<MeshRenderer>();
				if ((Object)(object)mat != (Object)null)
				{
					((Renderer)val2).sharedMaterial = mat;
				}
				MeshCollider obj = val.AddComponent<MeshCollider>();
				obj.sharedMesh = c.Mesh;
				obj.convex = true;
				Rigidbody obj2 = val.AddComponent<Rigidbody>();
				obj2.useGravity = true;
				obj2.angularDrag = Preferences.AngularDrag.Value;
				Vector3 lossyScale = pivot.lossyScale;
				Bounds bounds = c.Mesh.bounds;
				float x = ((Bounds)(ref bounds)).size.x;
				bounds = c.Mesh.bounds;
				float num = x * ((Bounds)(ref bounds)).size.y;
				bounds = c.Mesh.bounds;
				float num2 = num * ((Bounds)(ref bounds)).size.z * Mathf.Abs(lossyScale.x * lossyScale.y * lossyScale.z);
				obj2.mass = Mathf.Max(0.05f, num2 * Preferences.MassScale.Value);
				val.AddComponent<DebrisChunk>().Init(Preferences.ChunkLifetime.Value);
			}
		}
	}
	public class Main : MelonMod
	{
		private bool _layersReady;

		private bool _comboWasDown;

		public override void OnInitializeMelon()
		{
			try
			{
				Preferences.Init();
				try
				{
					ClassInjector.RegisterTypeInIl2Cpp<DebrisChunk>();
				}
				catch (Exception ex)
				{
					MelonLogger.Warning("[Crumble] DebrisChunk register: " + ex.Message);
				}
				((MelonBase)this).HarmonyInstance.PatchAll(Assembly.GetExecutingAssembly());
				MelonLogger.Msg("[Crumble] Initialized and patched Structure.Kill.");
				Actions.onMapInitialized += OnMapInitialized;
			}
			catch (Exception value)
			{
				MelonLogger.Error($"[Crumble] Init failed: {value}");
			}
		}

		private void OnMapInitialized(string mapName)
		{
			try
			{
				CrumbleUI.Close();
				DebrisLayers.DumpLayers();
				if (!_layersReady)
				{
					DebrisLayers.Setup();
					_layersReady = true;
				}
				ChunkCache.Clear();
				MelonCoroutines.Start(ChunkCache.PreWarm());
			}
			catch (Exception value)
			{
				MelonLogger.Error($"[Crumble] map-init failed: {value}");
			}
		}

		public override void OnUpdate()
		{
			try
			{
				float trigger = LeftController.GetTrigger();
				float primary = LeftController.GetPrimary();
				bool flag = trigger > 0.5f && primary > 0.5f;
				if (flag && !_comboWasDown)
				{
					string sceneName = Scene.GetSceneName();
					MelonLogger.Msg("[Crumble] Menu combo pressed in scene '" + sceneName + "'.");
					if (sceneName == "Gym" || sceneName == "Park")
					{
						CrumbleUI.Toggle();
					}
					else
					{
						MelonLogger.Msg("[Crumble] Settings menu only opens in Gym or Park (current: '" + sceneName + "').");
					}
				}
				_comboWasDown = flag;
			}
			catch (Exception value)
			{
				MelonLogger.Error($"[Crumble] OnUpdate error: {value}");
			}
		}
	}
	public static class MeshReadback
	{
		public static bool TryMakeReadable(Mesh src, out Mesh readable)
		{
			//IL_00ce: Unknown result type (might be due to invalid IL or missing references)
			//IL_00d3: Unknown result type (might be due to invalid IL or missing references)
			//IL_0115: Unknown result type (might be due to invalid IL or missing references)
			//IL_01b1: Unknown result type (might be due to invalid IL or missing references)
			//IL_01b8: Expected O, but got Unknown
			readable = null;
			GraphicsBuffer val = null;
			GraphicsBuffer val2 = null;
			try
			{
				if ((Object)(object)src == (Object)null)
				{
					return false;
				}
				if (!src.HasVertexAttribute((VertexAttribute)0))
				{
					return false;
				}
				if (src.vertexBufferCount < 1)
				{
					return false;
				}
				int vertexBufferStride = src.GetVertexBufferStride(0);
				int vertexAttributeOffset = src.GetVertexAttributeOffset((VertexAttribute)0);
				if (vertexBufferStride <= 0)
				{
					return false;
				}
				val = src.GetVertexBuffer(0);
				if (val == null)
				{
					return false;
				}
				int count = val.count;
				if (count == 0)
				{
					return false;
				}
				byte[] array = ReadBytes(val, count * vertexBufferStride);
				if (array == null)
				{
					return false;
				}
				Vector3[] array2 = (Vector3[])(object)new Vector3[count];
				for (int i = 0; i < count; i++)
				{
					int num = i * vertexBufferStride + vertexAttributeOffset;
					array2[i] = new Vector3(BitConverter.ToSingle(array, num), BitConverter.ToSingle(array, num + 4), BitConverter.ToSingle(array, num + 8));
				}
				val2 = src.GetIndexBuffer();
				if (val2 == null)
				{
					return false;
				}
				int count2 = val2.count;
				if (count2 < 3)
				{
					return false;
				}
				int[] array3 = new int[count2];
				if ((int)src.indexFormat == 0)
				{
					byte[] array4 = ReadBytes(val2, count2 * 2);
					if (array4 == null)
					{
						return false;
					}
					for (int j = 0; j < count2; j++)
					{
						int num2 = BitConverter.ToUInt16(array4, j * 2);
						if (num2 >= count)
						{
							return false;
						}
						array3[j] = num2;
					}
				}
				else
				{
					byte[] array5 = ReadBytes(val2, count2 * 4);
					if (array5 == null)
					{
						return false;
					}
					for (int k = 0; k < count2; k++)
					{
						uint num3 = BitConverter.ToUInt32(array5, k * 4);
						if (num3 >= (uint)count)
						{
							return false;
						}
						array3[k] = (int)num3;
					}
				}
				Mesh val3 = new Mesh();
				if (count > 65535)
				{
					val3.indexFormat = (IndexFormat)1;
				}
				val3.vertices = Il2CppStructArray<Vector3>.op_Implicit(array2);
				val3.triangles = Il2CppStructArray<int>.op_Implicit(array3);
				val3.RecalculateNormals();
				val3.RecalculateBounds();
				readable = val3;
				return true;
			}
			catch
			{
				readable = null;
				return false;
			}
			finally
			{
				if (val != null)
				{
					val.Dispose();
				}
				if (val2 != null)
				{
					val2.Dispose();
				}
			}
		}

		private static byte[] ReadBytes(GraphicsBuffer buf, int length)
		{
			if (length <= 0)
			{
				return null;
			}
			Il2CppStructArray<byte> val = new Il2CppStructArray<byte>((long)length);
			buf.GetData(((Il2CppObjectBase)val).Cast<Array>());
			byte[] array = new byte[length];
			for (int i = 0; i < length; i++)
			{
				array[i] = ((Il2CppArrayBase<byte>)(object)val)[i];
			}
			return array;
		}
	}
	public static class MeshSlicer
	{
		private struct Edge
		{
			public Vector3 a;

			public Vector3 b;
		}

		private struct Vec3Key
		{
			public float x;

			public float y;

			public float z;

			public Vec3Key(Vector3 v)
			{
				//IL_0001: 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_003b: Unknown result type (might be due to invalid IL or missing references)
				x = Mathf.Round(v.x * 1000f) / 1000f;
				y = Mathf.Round(v.y * 1000f) / 1000f;
				z = Mathf.Round(v.z * 1000f) / 1000f;
			}

			public override bool Equals(object obj)
			{
				if (!(obj is Vec3Key vec3Key))
				{
					return false;
				}
				if (x == vec3Key.x && y == vec3Key.y)
				{
					return z == vec3Key.z;
				}
				return false;
			}

			public override int GetHashCode()
			{
				return x.GetHashCode() ^ (y.GetHashCode() << 2) ^ (z.GetHashCode() >> 2);
			}
		}

		private class SimpleMesh
		{
			public List<Vector3> vertices;

			public List<int> triangles;

			public Dictionary<Vec3Key, int> vertMap;

			public SimpleMesh()
			{
				vertices = new List<Vector3>();
				triangles = new List<int>();
				vertMap = new Dictionary<Vec3Key, int>();
			}

			public SimpleMesh(Mesh mesh)
			{
				//IL_0040: Unknown result type (might be due to invalid IL or missing references)
				vertices = new List<Vector3>((IEnumerable<Vector3>)mesh.vertices);
				triangles = new List<int>((IEnumerable<int>)mesh.triangles);
				vertMap = new Dictionary<Vec3Key, int>();
				for (int i = 0; i < vertices.Count; i++)
				{
					Vec3Key key = new Vec3Key(vertices[i]);
					if (!vertMap.ContainsKey(key))
					{
						vertMap[key] = i;
					}
				}
			}

			public int AddVertex(Vector3 v)
			{
				//IL_0002: Unknown result type (might be due to invalid IL or missing references)
				//IL_002c: Unknown result type (might be due to invalid IL or missing references)
				Vec3Key key = new Vec3Key(v);
				if (vertMap.TryGetValue(key, out var value))
				{
					return value;
				}
				value = vertices.Count;
				vertices.Add(v);
				vertMap[key] = value;
				return value;
			}

			public void AddTriangle(Vector3 v0, Vector3 v1, Vector3 v2)
			{
				//IL_0001: Unknown result type (might be due to invalid IL or missing references)
				//IL_0009: Unknown result type (might be due to invalid IL or missing references)
				//IL_0011: Unknown result type (might be due to invalid IL or missing references)
				int num = AddVertex(v0);
				int num2 = AddVertex(v1);
				int num3 = AddVertex(v2);
				if (num != num2 && num2 != num3 && num3 != num)
				{
					triangles.Add(num);
					triangles.Add(num2);
					triangles.Add(num3);
				}
			}
		}

		private static Bounds GetBounds(SimpleMesh m)
		{
			//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_0031: Unknown result type (might be due to invalid IL or missing references)
			//IL_0036: 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_0017: Unknown result type (might be due to invalid IL or missing references)
			//IL_003b: 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_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_004e: 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_005b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0060: Unknown result type (might be due to invalid IL or missing references)
			//IL_0073: 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_0075: Unknown result type (might be due to invalid IL or missing references)
			//IL_007f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0084: Unknown result type (might be due to invalid IL or missing references)
			//IL_0085: Unknown result type (might be due to invalid IL or missing references)
			//IL_0086: Unknown result type (might be due to invalid IL or missing references)
			//IL_008b: Unknown result type (might be due to invalid IL or missing references)
			if (m.vertices.Count == 0)
			{
				return new Bounds(Vector3.zero, Vector3.zero);
			}
			Vector3 val = m.vertices[0];
			Vector3 val2 = m.vertices[0];
			for (int i = 1; i < m.vertices.Count; i++)
			{
				val = Vector3.Min(val, m.vertices[i]);
				val2 = Vector3.Max(val2, m.vertices[i]);
			}
			return new Bounds((val + val2) * 0.5f, val2 - val);
		}

		private static float GetVolume(SimpleMesh m)
		{
			//IL_0001: Unknown result type (might be due to invalid IL or missing references)
			//IL_0006: Unknown result type (might be due to invalid IL or missing references)
			//IL_0009: 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_0022: Unknown result type (might be due to invalid IL or missing references)
			Bounds bounds = GetBounds(m);
			return ((Bounds)(ref bounds)).size.x * ((Bounds)(ref bounds)).size.y * ((Bounds)(ref bounds)).size.z;
		}

		private static Vector3 Intersect(Vector3 a, Vector3 b, float da, float db)
		{
			//IL_001e: 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_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_0027: Unknown result type (might be due to invalid IL or missing references)
			//IL_002c: Unknown result type (might be due to invalid IL or missing references)
			float num = da - db;
			float num2 = ((num == 0f) ? 0.5f : (da / num));
			num2 = Mathf.Clamp01(num2);
			return a + (b - a) * num2;
		}

		private static void FillCap(SimpleMesh mesh, List<Edge> edges)
		{
			//IL_0009: Unknown result type (might be due to invalid IL or missing references)
			//IL_000e: 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_0022: Unknown result type (might be due to invalid IL or missing references)
			//IL_0027: Unknown result type (might be due to invalid IL or missing references)
			//IL_002c: Unknown result type (might be due to invalid IL or missing references)
			//IL_002d: 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_0034: 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_0053: 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_0062: Unknown result type (might be due to invalid IL or missing references)
			//IL_0076: Unknown result type (might be due to invalid IL or missing references)
			//IL_007c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0081: Unknown result type (might be due to invalid IL or missing references)
			if (edges.Count == 0)
			{
				return;
			}
			Vector3 val = Vector3.zero;
			foreach (Edge edge in edges)
			{
				val += edge.a;
				val += edge.b;
			}
			val /= (float)(edges.Count * 2);
			foreach (Edge edge2 in edges)
			{
				mesh.AddTriangle(edge2.a, edge2.b, val);
			}
		}

		private static bool Split(SimpleMesh input, Vector3 planePoint, Vector3 planeNormal, out SimpleMesh posMesh, out SimpleMesh negMesh)
		{
			//IL_003a: Unknown result type (might be due to invalid IL or missing references)
			//IL_003f: 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_0051: Unknown result type (might be due to invalid IL or missing references)
			//IL_005e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0063: 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_0067: 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_006d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0075: Unknown result type (might be due to invalid IL or missing references)
			//IL_0077: Unknown result type (might be due to invalid IL or missing references)
			//IL_0078: Unknown result type (might be due to invalid IL or missing references)
			//IL_007d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0085: Unknown result type (might be due to invalid IL or missing references)
			//IL_0087: Unknown result type (might be due to invalid IL or missing references)
			//IL_0088: Unknown result type (might be due to invalid IL or missing references)
			//IL_008d: Unknown result type (might be due to invalid IL or missing references)
			//IL_00cb: Unknown result type (might be due to invalid IL or missing references)
			//IL_00cd: Unknown result type (might be due to invalid IL or missing references)
			//IL_00cf: Unknown result type (might be due to invalid IL or missing references)
			//IL_014e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0150: Unknown result type (might be due to invalid IL or missing references)
			//IL_0152: Unknown result type (might be due to invalid IL or missing references)
			//IL_0154: Unknown result type (might be due to invalid IL or missing references)
			//IL_0156: Unknown result type (might be due to invalid IL or missing references)
			//IL_0158: 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_0108: Unknown result type (might be due to invalid IL or missing references)
			//IL_010a: Unknown result type (might be due to invalid IL or missing references)
			//IL_010c: Unknown result type (might be due to invalid IL or missing references)
			//IL_010e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0110: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ea: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ec: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ee: Unknown result type (might be due to invalid IL or missing references)
			//IL_016a: Unknown result type (might be due to invalid IL or missing references)
			//IL_016c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0172: Unknown result type (might be due to invalid IL or missing references)
			//IL_0177: Unknown result type (might be due to invalid IL or missing references)
			//IL_0179: Unknown result type (might be due to invalid IL or missing references)
			//IL_017b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0181: Unknown result type (might be due to invalid IL or missing references)
			//IL_0186: 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_0132: Unknown result type (might be due to invalid IL or missing references)
			//IL_0134: Unknown result type (might be due to invalid IL or missing references)
			//IL_0136: Unknown result type (might be due to invalid IL or missing references)
			//IL_0138: Unknown result type (might be due to invalid IL or missing references)
			//IL_013a: Unknown result type (might be due to invalid IL or missing references)
			//IL_01fe: Unknown result type (might be due to invalid IL or missing references)
			//IL_0200: Unknown result type (might be due to invalid IL or missing references)
			//IL_0202: Unknown result type (might be due to invalid IL or missing references)
			//IL_020b: Unknown result type (might be due to invalid IL or missing references)
			//IL_020d: Unknown result type (might be due to invalid IL or missing references)
			//IL_020f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0218: Unknown result type (might be due to invalid IL or missing references)
			//IL_021a: Unknown result type (might be due to invalid IL or missing references)
			//IL_021c: Unknown result type (might be due to invalid IL or missing references)
			//IL_022e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0230: Unknown result type (might be due to invalid IL or missing references)
			//IL_0237: Unknown result type (might be due to invalid IL or missing references)
			//IL_0239: Unknown result type (might be due to invalid IL or missing references)
			//IL_0250: Unknown result type (might be due to invalid IL or missing references)
			//IL_0252: Unknown result type (might be due to invalid IL or missing references)
			//IL_0259: Unknown result type (might be due to invalid IL or missing references)
			//IL_025b: Unknown result type (might be due to invalid IL or missing references)
			//IL_018e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0190: Unknown result type (might be due to invalid IL or missing references)
			//IL_0192: Unknown result type (might be due to invalid IL or missing references)
			//IL_019c: Unknown result type (might be due to invalid IL or missing references)
			//IL_019e: Unknown result type (might be due to invalid IL or missing references)
			//IL_01a0: Unknown result type (might be due to invalid IL or missing references)
			//IL_01aa: Unknown result type (might be due to invalid IL or missing references)
			//IL_01ac: Unknown result type (might be due to invalid IL or missing references)
			//IL_01ae: Unknown result type (might be due to invalid IL or missing references)
			//IL_01c0: Unknown result type (might be due to invalid IL or missing references)
			//IL_01c2: Unknown result type (might be due to invalid IL or missing references)
			//IL_01c9: Unknown result type (might be due to invalid IL or missing references)
			//IL_01cb: Unknown result type (might be due to invalid IL or missing references)
			//IL_01e2: Unknown result type (might be due to invalid IL or missing references)
			//IL_01e4: Unknown result type (might be due to invalid IL or missing references)
			//IL_01eb: Unknown result type (might be due to invalid IL or missing references)
			//IL_01ed: Unknown result type (might be due to invalid IL or missing references)
			posMesh = new SimpleMesh();
			negMesh = new SimpleMesh();
			List<Edge> list = new List<Edge>();
			List<Edge> list2 = new List<Edge>();
			List<Vector3> vertices = input.vertices;
			List<int> triangles = input.triangles;
			for (int i = 0; i < triangles.Count; i += 3)
			{
				Vector3 val = vertices[triangles[i]];
				Vector3 val2 = vertices[triangles[i + 1]];
				Vector3 val3 = vertices[triangles[i + 2]];
				float num = Vector3.Dot(val - planePoint, planeNormal);
				float num2 = Vector3.Dot(val2 - planePoint, planeNormal);
				float num3 = Vector3.Dot(val3 - planePoint, planeNormal);
				bool flag = num >= 0f;
				bool flag2 = num2 >= 0f;
				bool flag3 = num3 >= 0f;
				if (flag && flag2 && flag3)
				{
					posMesh.AddTriangle(val, val2, val3);
					continue;
				}
				if (!flag && !flag2 && !flag3)
				{
					negMesh.AddTriangle(val, val2, val3);
					continue;
				}
				Vector3 val4;
				Vector3 val5;
				Vector3 val6;
				float da;
				float db;
				float db2;
				bool flag4;
				if (flag != flag2 && flag != flag3)
				{
					val4 = val;
					val5 = val2;
					val6 = val3;
					da = num;
					db = num2;
					db2 = num3;
					flag4 = flag;
				}
				else if (flag2 != flag && flag2 != flag3)
				{
					val4 = val2;
					val5 = val3;
					val6 = val;
					da = num2;
					db = num3;
					db2 = num;
					flag4 = flag2;
				}
				else
				{
					val4 = val3;
					val5 = val;
					val6 = val2;
					da = num3;
					db = num;
					db2 = num2;
					flag4 = flag3;
				}
				Vector3 val7 = Intersect(val4, val5, da, db);
				Vector3 val8 = Intersect(val4, val6, da, db2);
				if (flag4)
				{
					posMesh.AddTriangle(val4, val7, val8);
					negMesh.AddTriangle(val5, val6, val7);
					negMesh.AddTriangle(val6, val8, val7);
					list.Add(new Edge
					{
						a = val8,
						b = val7
					});
					list2.Add(new Edge
					{
						a = val7,
						b = val8
					});
				}
				else
				{
					negMesh.AddTriangle(val4, val7, val8);
					posMesh.AddTriangle(val5, val6, val7);
					posMesh.AddTriangle(val6, val8, val7);
					list2.Add(new Edge
					{
						a = val8,
						b = val7
					});
					list.Add(new Edge
					{
						a = val7,
						b = val8
					});
				}
			}
			if (posMesh.triangles.Count == 0 || negMesh.triangles.Count == 0)
			{
				return false;
			}
			FillCap(posMesh, list);
			FillCap(negMesh, list2);
			return true;
		}

		private static ChunkData CreateChunkData(SimpleMesh m)
		{
			//IL_0000: Unknown result type (might be due to invalid IL or missing references)
			//IL_0005: 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_001c: Unknown result type (might be due to invalid IL or missing references)
			//IL_001d: 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_0023: 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_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_007f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0084: Unknown result type (might be due to invalid IL or missing references)
			//IL_0085: Unknown result type (might be due to invalid IL or missing references)
			//IL_008a: Unknown result type (might be due to invalid IL or missing references)
			//IL_00a4: Unknown result type (might be due to invalid IL or missing references)
			//IL_00a9: Unknown result type (might be due to invalid IL or missing references)
			//IL_00b5: Unknown result type (might be due to invalid IL or missing references)
			//IL_00cb: Unknown result type (might be due to invalid IL or missing references)
			//IL_00d1: Unknown result type (might be due to invalid IL or missing references)
			//IL_00d7: Unknown result type (might be due to invalid IL or missing references)
			//IL_00dd: Expected O, but got Unknown
			Vector3 val = Vector3.zero;
			foreach (Vector3 vertex in m.vertices)
			{
				val += vertex;
			}
			if (m.vertices.Count > 0)
			{
				val /= (float)m.vertices.Count;
			}
			Vector3[] array = (Vector3[])(object)new Vector3[m.vertices.Count];
			for (int i = 0; i < m.vertices.Count; i++)
			{
				array[i] = m.vertices[i] - val;
			}
			Mesh val2 = new Mesh
			{
				vertices = Il2CppStructArray<Vector3>.op_Implicit(array),
				triangles = Il2CppStructArray<int>.op_Implicit(m.triangles.ToArray())
			};
			val2.RecalculateNormals();
			val2.RecalculateBounds();
			return new ChunkData(val2, val);
		}

		public static bool TrySlice(Mesh mesh, int count, float randomness, out ChunkData[] chunks)
		{
			//IL_0038: Unknown result type (might be due to invalid IL or missing references)
			//IL_003d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0044: Unknown result type (might be due to invalid IL or missing references)
			//IL_0049: 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)
			//IL_0055: Unknown result type (might be due to invalid IL or missing references)
			//IL_005c: 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_0068: Unknown result type (might be due to invalid IL or missing references)
			//IL_006d: 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_0079: Unknown result type (might be due to invalid IL or missing references)
			//IL_00da: Unknown result type (might be due to invalid IL or missing references)
			//IL_00df: 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_00e6: 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_00ee: 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_00f7: Unknown result type (might be due to invalid IL or missing references)
			//IL_010f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0114: Unknown result type (might be due to invalid IL or missing references)
			//IL_011a: 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_0137: Unknown result type (might be due to invalid IL or missing references)
			//IL_0143: Unknown result type (might be due to invalid IL or missing references)
			//IL_0154: Unknown result type (might be due to invalid IL or missing references)
			//IL_0160: Unknown result type (might be due to invalid IL or missing references)
			//IL_0174: Unknown result type (might be due to invalid IL or missing references)
			//IL_0176: Unknown result type (might be due to invalid IL or missing references)
			//IL_0107: Unknown result type (might be due to invalid IL or missing references)
			//IL_010c: Unknown result type (might be due to invalid IL or missing references)
			chunks = null;
			try
			{
				if ((Object)(object)mesh == (Object)null || count < 2)
				{
					return false;
				}
				List<SimpleMesh> list = new List<SimpleMesh>();
				list.Add(new SimpleMesh(mesh));
				int num = 0;
				int num2 = count * 5;
				Vector3[] array = (Vector3[])(object)new Vector3[6]
				{
					Vector3.up,
					Vector3.down,
					Vector3.left,
					Vector3.right,
					Vector3.forward,
					Vector3.back
				};
				Vector3 planePoint = default(Vector3);
				while (list.Count < count && num < num2)
				{
					int num3 = -1;
					float num4 = -1f;
					for (int i = 0; i < list.Count; i++)
					{
						float volume = GetVolume(list[i]);
						if (volume > num4)
						{
							num4 = volume;
							num3 = i;
						}
					}
					if (num3 == -1)
					{
						break;
					}
					SimpleMesh simpleMesh = list[num3];
					Vector3 val = array[Random.Range(0, array.Length)];
					Vector3 onUnitSphere = Random.onUnitSphere;
					Vector3 val2 = Vector3.Lerp(val, onUnitSphere, randomness);
					Vector3 planeNormal = ((Vector3)(ref val2)).normalized;
					if (((Vector3)(ref planeNormal)).sqrMagnitude < 0.001f)
					{
						planeNormal = Vector3.up;
					}
					Bounds bounds = GetBounds(simpleMesh);
					((Vector3)(ref planePoint))..ctor(Random.Range(((Bounds)(ref bounds)).min.x, ((Bounds)(ref bounds)).max.x), Random.Range(((Bounds)(ref bounds)).min.y, ((Bounds)(ref bounds)).max.y), Random.Range(((Bounds)(ref bounds)).min.z, ((Bounds)(ref bounds)).max.z));
					if (Split(simpleMesh, planePoint, planeNormal, out var posMesh, out var negMesh))
					{
						list.RemoveAt(num3);
						list.Add(posMesh);
						list.Add(negMesh);
					}
					else
					{
						num++;
					}
				}
				if (list.Count < 2)
				{
					return false;
				}
				chunks = new ChunkData[list.Count];
				for (int j = 0; j < list.Count; j++)
				{
					chunks[j] = CreateChunkData(list[j]);
				}
				return true;
			}
			catch
			{
				chunks = null;
				return false;
			}
		}
	}
	public static class Preferences
	{
		private const string Dir = "UserData/Crumble/";

		private const string File = "config.cfg";

		public static MelonPreferences_Category Category;

		public static MelonPreferences_Entry<bool> Enabled;

		public static MelonPreferences_Entry<int> ChunksPerBreak;

		public static MelonPreferences_Entry<bool> ChunkStyleMinecraft;

		public static MelonPreferences_Entry<float> ChunkLifetime;

		public static MelonPreferences_Entry<float> MassScale;

		public static MelonPreferences_Entry<float> AngularDrag;

		public static MelonPreferences_Entry<int> MaxConcurrentDebris;

		public static MelonPreferences_Entry<bool> PersistUntilSceneChange;

		public static MelonPreferences_Entry<float> SliceRandomness;

		public static MelonPreferences_Entry<bool> DebugLogging;

		public static void Init()
		{
			//IL_0086: Unknown result type (might be due to invalid IL or missing references)
			//IL_0091: Expected O, but got Unknown
			//IL_00e3: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ee: Expected O, but got Unknown
			//IL_011d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0128: Expected O, but got Unknown
			//IL_0157: Unknown result type (might be due to invalid IL or missing references)
			//IL_0162: Expected O, but got Unknown
			//IL_018a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0195: Expected O, but got Unknown
			//IL_01e7: Unknown result type (might be due to invalid IL or missing references)
			//IL_01f2: Expected O, but got Unknown
			if (!Directory.Exists("UserData/Crumble/"))
			{
				Directory.CreateDirectory("UserData/Crumble/");
			}
			Category = MelonPreferences.CreateCategory("Crumble", "Settings");
			Category.SetFilePath(Path.Combine("UserData/Crumble/", "config.cfg"));
			Enabled = Category.CreateEntry<bool>("Enabled", true, "Enabled", "Toggle the whole mod on/off", false, false, (ValueValidator)null, (string)null);
			ChunksPerBreak = Category.CreateEntry<int>("ChunksPerBreak", 8, "Chunks Per Break", "Exact number of chunks spawned per destruction", false, false, (ValueValidator)new ValueRange<int>(1, 10), (string)null);
			ChunkStyleMinecraft = Category.CreateEntry<bool>("ChunkStyleMinecraft", false, "Chunk Style: Minecraft", "false = attempt real mesh slices, true = force box shards", false, false, (ValueValidator)null, (string)null);
			ChunkLifetime = Category.CreateEntry<float>("ChunkLifetime", 6f, "Chunk Lifetime (s)", "Seconds before a chunk despawns", false, false, (ValueValidator)new ValueRange<float>(0.5f, 60f), (string)null);
			MassScale = Category.CreateEntry<float>("MassScale", 1f, "Mass Scale", "Multiplier on chunk mass (scaled by size)", false, false, (ValueValidator)new ValueRange<float>(0.01f, 100f), (string)null);
			AngularDrag = Category.CreateEntry<float>("AngularDrag", 0.5f, "Angular Drag", "Higher = chunks stop spinning sooner", false, false, (ValueValidator)new ValueRange<float>(0f, 20f), (string)null);
			MaxConcurrentDebris = Category.CreateEntry<int>("MaxConcurrentDebris", 120, "Max Concurrent Debris", "Hard cap on live chunks (VR safety). Ignored when PersistUntilSceneChange is on", false, false, (ValueValidator)new ValueRange<int>(8, 1000), (string)null);
			PersistUntilSceneChange = Category.CreateEntry<bool>("PersistUntilSceneChange", true, "Persist Until Scene Change", "If true, chunks never despawn on a timer and the concurrent cap is ignored; they clear only on map/scene change (VR perf risk if you spam destructions)", false, false, (ValueValidator)null, (string)null);
			SliceRandomness = Category.CreateEntry<float>("SliceRandomness", 0.5f, "Slice Randomness", "0 = uniform cuts, 1 = chaotic cuts", false, false, (ValueValidator)new ValueRange<float>(0f, 1f), (string)null);
			DebugLogging = Category.CreateEntry<bool>("DebugLogging", false, "Debug Logging", "Verbose Crumble logs", false, false, (ValueValidator)null, (string)null);
		}

		public static void Log(string msg)
		{
			if (DebugLogging != null && DebugLogging.Value)
			{
				MelonLogger.Msg("[Crumble] " + msg);
			}
		}
	}
	public static class ShardClusterBuilder
	{
		public static ChunkData[] Build(Bounds bounds, int count, float randomness)
		{
			//IL_0010: 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_0033: Unknown result type (might be due to invalid IL or missing references)
			//IL_005c: 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_006c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0071: Unknown result type (might be due to invalid IL or missing references)
			//IL_0089: Unknown result type (might be due to invalid IL or missing references)
			//IL_0090: Unknown result type (might be due to invalid IL or missing references)
			//IL_0331: Unknown result type (might be due to invalid IL or missing references)
			//IL_0336: Unknown result type (might be due to invalid IL or missing references)
			//IL_033a: Unknown result type (might be due to invalid IL or missing references)
			//IL_034e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0353: Unknown result type (might be due to invalid IL or missing references)
			//IL_0357: Unknown result type (might be due to invalid IL or missing references)
			//IL_016a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0171: Unknown result type (might be due to invalid IL or missing references)
			//IL_009c: Unknown result type (might be due to invalid IL or missing references)
			//IL_00a3: Unknown result type (might be due to invalid IL or missing references)
			//IL_024d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0257: Unknown result type (might be due to invalid IL or missing references)
			//IL_026e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0278: Unknown result type (might be due to invalid IL or missing references)
			//IL_0288: Unknown result type (might be due to invalid IL or missing references)
			//IL_0292: Unknown result type (might be due to invalid IL or missing references)
			//IL_02a9: Unknown result type (might be due to invalid IL or missing references)
			//IL_02b0: Unknown result type (might be due to invalid IL or missing references)
			//IL_02bc: Unknown result type (might be due to invalid IL or missing references)
			//IL_02c8: Unknown result type (might be due to invalid IL or missing references)
			//IL_02cd: Unknown result type (might be due to invalid IL or missing references)
			//IL_02d2: Unknown result type (might be due to invalid IL or missing references)
			//IL_02d6: Unknown result type (might be due to invalid IL or missing references)
			//IL_02e2: Unknown result type (might be due to invalid IL or missing references)
			//IL_02ee: Unknown result type (might be due to invalid IL or missing references)
			//IL_02f5: Unknown result type (might be due to invalid IL or missing references)
			//IL_02fa: Unknown result type (might be due to invalid IL or missing references)
			//IL_02ff: Unknown result type (might be due to invalid IL or missing references)
			//IL_017d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0184: Unknown result type (might be due to invalid IL or missing references)
			//IL_00b1: Unknown result type (might be due to invalid IL or missing references)
			//IL_00bb: Unknown result type (might be due to invalid IL or missing references)
			//IL_00d2: Unknown result type (might be due to invalid IL or missing references)
			//IL_00dc: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ec: Unknown result type (might be due to invalid IL or missing references)
			//IL_00f6: 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_0116: Unknown result type (might be due to invalid IL or missing references)
			//IL_0122: Unknown result type (might be due to invalid IL or missing references)
			//IL_012c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0131: Unknown result type (might be due to invalid IL or missing references)
			//IL_0136: Unknown result type (might be due to invalid IL or missing references)
			//IL_013c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0148: Unknown result type (might be due to invalid IL or missing references)
			//IL_0152: Unknown result type (might be due to invalid IL or missing references)
			//IL_0159: 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_0163: Unknown result type (might be due to invalid IL or missing references)
			//IL_0302: Unknown result type (might be due to invalid IL or missing references)
			//IL_030a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0192: Unknown result type (might be due to invalid IL or missing references)
			//IL_019c: Unknown result type (might be due to invalid IL or missing references)
			//IL_01b3: Unknown result type (might be due to invalid IL or missing references)
			//IL_01bd: Unknown result type (might be due to invalid IL or missing references)
			//IL_01cd: Unknown result type (might be due to invalid IL or missing references)
			//IL_01d7: Unknown result type (might be due to invalid IL or missing references)
			//IL_01ee: Unknown result type (might be due to invalid IL or missing references)
			//IL_01f5: Unknown result type (might be due to invalid IL or missing references)
			//IL_0203: Unknown result type (might be due to invalid IL or missing references)
			//IL_020d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0212: Unknown result type (might be due to invalid IL or missing references)
			//IL_0217: Unknown result type (might be due to invalid IL or missing references)
			//IL_021b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0229: Unknown result type (might be due to invalid IL or missing references)
			//IL_0233: Unknown result type (might be due to invalid IL or missing references)
			//IL_023a: Unknown result type (might be due to invalid IL or missing references)
			//IL_023f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0244: Unknown result type (might be due to invalid IL or missing references)
			count = Mathf.Max(1, count);
			List<Bounds> list = new List<Bounds>(count) { bounds };
			while (list.Count < count)
			{
				int index = 0;
				float num = Volume(list[0]);
				for (int i = 1; i < list.Count; i++)
				{
					float num2 = Volume(list[i]);
					if (num2 > num)
					{
						num = num2;
						index = i;
					}
				}
				Bounds val = list[index];
				list.RemoveAt(index);
				Vector3 size = ((Bounds)(ref val)).size;
				float num3 = 1f + Random.Range(0f - randomness, randomness) * 0.4f;
				Bounds item;
				Bounds item2;
				if (size.x >= size.y && size.x >= size.z)
				{
					float num4 = ((Bounds)(ref val)).min.x + size.x * 0.5f * num3;
					num4 = Mathf.Clamp(num4, ((Bounds)(ref val)).min.x + size.x * 0.1f, ((Bounds)(ref val)).max.x - size.x * 0.1f);
					item = BoundsFromMinMax(((Bounds)(ref val)).min, new Vector3(num4, ((Bounds)(ref val)).max.y, ((Bounds)(ref val)).max.z));
					item2 = BoundsFromMinMax(new Vector3(num4, ((Bounds)(ref val)).min.y, ((Bounds)(ref val)).min.z), ((Bounds)(ref val)).max);
				}
				else if (size.y >= size.x && size.y >= size.z)
				{
					float num5 = ((Bounds)(ref val)).min.y + size.y * 0.5f * num3;
					num5 = Mathf.Clamp(num5, ((Bounds)(ref val)).min.y + size.y * 0.1f, ((Bounds)(ref val)).max.y - size.y * 0.1f);
					item = BoundsFromMinMax(((Bounds)(ref val)).min, new Vector3(((Bounds)(ref val)).max.x, num5, ((Bounds)(ref val)).max.z));
					item2 = BoundsFromMinMax(new Vector3(((Bounds)(ref val)).min.x, num5, ((Bounds)(ref val)).min.z), ((Bounds)(ref val)).max);
				}
				else
				{
					float num6 = ((Bounds)(ref val)).min.z + size.z * 0.5f * num3;
					num6 = Mathf.Clamp(num6, ((Bounds)(ref val)).min.z + size.z * 0.1f, ((Bounds)(ref val)).max.z - size.z * 0.1f);
					item = BoundsFromMinMax(((Bounds)(ref val)).min, new Vector3(((Bounds)(ref val)).max.x, ((Bounds)(ref val)).max.y, num6));
					item2 = BoundsFromMinMax(new Vector3(((Bounds)(ref val)).min.x, ((Bounds)(ref val)).min.y, num6), ((Bounds)(ref val)).max);
				}
				list.Add(item);
				list.Add(item2);
			}
			ChunkData[] array = new ChunkData[list.Count];
			for (int j = 0; j < list.Count; j++)
			{
				Bounds val2 = list[j];
				Mesh mesh = Box(((Bounds)(ref val2)).extents);
				int num7 = j;
				val2 = list[j];
				array[num7] = new ChunkData(mesh, ((Bounds)(ref val2)).center);
			}
			return array;
		}

		private static float Volume(Bounds b)
		{
			//IL_0002: Unknown result type (might be due to invalid IL or missing references)
			//IL_0007: Unknown result type (might be due to invalid IL or missing references)
			//IL_0008: Unknown result type (might be due to invalid IL or missing references)
			//IL_000e: 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)
			Vector3 size = ((Bounds)(ref b)).size;
			return size.x * size.y * size.z;
		}

		private static Bounds BoundsFromMinMax(Vector3 min, Vector3 max)
		{
			//IL_0002: Unknown result type (might be due to invalid IL or missing references)
			//IL_000a: Unknown result type (might be due to invalid IL or missing references)
			//IL_000b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0011: Unknown result type (might be due to invalid IL or missing references)
			Bounds result = default(Bounds);
			((Bounds)(ref result)).SetMinMax(min, max);
			return result;
		}

		private static Mesh Box(Vector3 half)
		{
			//IL_0008: Unknown result type (might be due to invalid IL or missing references)
			//IL_000f: 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_001d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0022: 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_002f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0036: Unknown result type (might be due to invalid IL or missing references)
			//IL_003d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0042: Unknown result type (might be due to invalid IL or missing references)
			//IL_0049: Unknown result type (might be due to invalid IL or missing references)
			//IL_004f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0055: Unknown result type (might be due to invalid IL or missing references)
			//IL_005c: 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_0068: Unknown result type (might be due to invalid IL or missing references)
			//IL_006f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0075: Unknown result type (might be due to invalid IL or missing references)
			//IL_007c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0081: Unknown result type (might be due to invalid IL or missing references)
			//IL_0088: Unknown result type (might be due to invalid IL or missing references)
			//IL_008f: 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_009c: Unknown result type (might be due to invalid IL or missing references)
			//IL_00a1: 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_00ae: Unknown result type (might be due to invalid IL or missing references)
			//IL_00b5: Unknown result type (might be due to invalid IL or missing references)
			//IL_00bb: Unknown result type (might be due to invalid IL or missing references)
			//IL_00c0: Unknown result type (might be due to invalid IL or missing references)
			//IL_00c7: Unknown result type (might be due to invalid IL or missing references)
			//IL_00cd: Unknown result type (might be due to invalid IL or missing references)
			//IL_00d3: Unknown result type (might be due to invalid IL or missing references)
			//IL_00d9: Unknown result type (might be due to invalid IL or missing references)
			//IL_00de: Unknown result type (might be due to invalid IL or missing references)
			//IL_00e5: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ec: 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_00f8: 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_0116: Unknown result type (might be due to invalid IL or missing references)
			//IL_011b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0127: Unknown result type (might be due to invalid IL or missing references)
			//IL_0133: Unknown result type (might be due to invalid IL or missing references)
			//IL_0139: Unknown result type (might be due to invalid IL or missing references)
			//IL_0140: Expected O, but got Unknown
			Vector3[] array = (Vector3[])(object)new Vector3[8]
			{
				new Vector3(0f - half.x, 0f - half.y, 0f - half.z),
				new Vector3(half.x, 0f - half.y, 0f - half.z),
				new Vector3(half.x, half.y, 0f - half.z),
				new Vector3(0f - half.x, half.y, 0f - half.z),
				new Vector3(0f - half.x, 0f - half.y, half.z),
				new Vector3(half.x, 0f - half.y, half.z),
				new Vector3(half.x, half.y, half.z),
				new Vector3(0f - half.x, half.y, half.z)
			};
			int[] array2 = new int[36]
			{
				0, 2, 1, 0, 3, 2, 1, 6, 5, 1,
				2, 6, 5, 7, 4, 5, 6, 7, 4, 3,
				0, 4, 7, 3, 3, 6, 2, 3, 7, 6,
				4, 1, 5, 4, 0, 1
			};
			Mesh val = new Mesh
			{
				vertices = Il2CppStructArray<Vector3>.op_Implicit(array),
				triangles = Il2CppStructArray<int>.op_Implicit(array2)
			};
			val.RecalculateNormals();
			val.RecalculateBounds();
			return val;
		}
	}
	[HarmonyPatch(typeof(Structure), "Kill", new Type[]
	{
		typeof(Vector3),
		typeof(bool),
		typeof(bool),
		typeof(bool)
	})]
	public static class StructureKillPatch
	{
		public static bool Prefix(Structure __instance, Vector3 __0)
		{
			//IL_0032: 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)
			try
			{
				if (!Preferences.Enabled.Value)
				{
					return true;
				}
				Transform transform = ((Component)__instance).transform;
				Preferences.Log($"Structure.Kill fired at {transform.position} (killPoint {__0})");
				DebrisSpawner.Spawn(transform);
			}
			catch (Exception value)
			{
				MelonLogger.Error($"[Crumble] Kill prefix threw (gameplay unaffected): {value}");
			}
			return true;
		}
	}
}