Decompiled source of DigDeeper v1.1.6

DigDeeper.dll

Decompiled 2 months ago
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.Reflection.Emit;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Configuration;
using HarmonyLib;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: AssemblyTitle("DigDeeper")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("Crystal")]
[assembly: AssemblyProduct("DigDeeper")]
[assembly: AssemblyCopyright("Copyright © 2023 Crystal Ferrai")]
[assembly: AssemblyTrademark("")]
[assembly: ComVisible(false)]
[assembly: Guid("257c1abb-2713-468a-8321-84c583805faa")]
[assembly: AssemblyFileVersion("1.1.6.0")]
[assembly: TargetFramework(".NETFramework,Version=v4.8", FrameworkDisplayName = ".NET Framework 4.8")]
[assembly: AssemblyVersion("1.1.6.0")]
namespace DigDeeper;

[BepInPlugin("dev.crystal.digdeeper", "Dig Deeper", "1.1.6.0")]
[BepInProcess("valheim.exe")]
[BepInProcess("valheim_server.exe")]
public class DigDeeperPlugin : BaseUnityPlugin
{
	[HarmonyPatch(typeof(Heightmap))]
	private static class Heightmap_Patches
	{
		private enum TranspilerState
		{
			Searching,
			Replacing
		}

		[CompilerGenerated]
		private sealed class <LevelTerrain_Transpiler>d__1 : IEnumerable<CodeInstruction>, IEnumerable, IEnumerator<CodeInstruction>, IDisposable, IEnumerator
		{
			private int <>1__state;

			private CodeInstruction <>2__current;

			private int <>l__initialThreadId;

			private IEnumerable<CodeInstruction> instructions;

			public IEnumerable<CodeInstruction> <>3__instructions;

			private TranspilerState <state>5__2;

			private CodeInstruction <valueInstruction>5__3;

			private IEnumerator<CodeInstruction> <>7__wrap3;

			private CodeInstruction <instruction>5__5;

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

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

			[DebuggerHidden]
			public <LevelTerrain_Transpiler>d__1(int <>1__state)
			{
				this.<>1__state = <>1__state;
				<>l__initialThreadId = Environment.CurrentManagedThreadId;
			}

			[DebuggerHidden]
			void IDisposable.Dispose()
			{
				int num = <>1__state;
				if (num == -3 || (uint)(num - 1) <= 2u)
				{
					try
					{
					}
					finally
					{
						<>m__Finally1();
					}
				}
				<valueInstruction>5__3 = null;
				<>7__wrap3 = null;
				<instruction>5__5 = null;
				<>1__state = -2;
			}

			private bool MoveNext()
			{
				try
				{
					switch (<>1__state)
					{
					default:
						return false;
					case 0:
						<>1__state = -1;
						<state>5__2 = TranspilerState.Searching;
						<valueInstruction>5__3 = null;
						<>7__wrap3 = instructions.GetEnumerator();
						<>1__state = -3;
						goto IL_019f;
					case 1:
						<>1__state = -3;
						goto IL_0198;
					case 2:
						<>1__state = -3;
						<>2__current = <instruction>5__5;
						<>1__state = 3;
						return true;
					case 3:
						{
							<>1__state = -3;
							<valueInstruction>5__3 = null;
							<state>5__2 = TranspilerState.Searching;
							goto IL_0198;
						}
						IL_019f:
						if (<>7__wrap3.MoveNext())
						{
							<instruction>5__5 = <>7__wrap3.Current;
							switch (<state>5__2)
							{
							case TranspilerState.Searching:
								if (<instruction>5__5.opcode == OpCodes.Ldc_R4 && (float)<instruction>5__5.operand == 8f)
								{
									<valueInstruction>5__3 = <instruction>5__5;
									<state>5__2 = TranspilerState.Replacing;
									break;
								}
								<>2__current = <instruction>5__5;
								<>1__state = 1;
								return true;
							case TranspilerState.Replacing:
								if (<instruction>5__5.opcode == OpCodes.Sub)
								{
									<valueInstruction>5__3.operand = MaximumDepth.Value;
								}
								else if (<instruction>5__5.opcode == OpCodes.Add)
								{
									<valueInstruction>5__3.operand = MaximumHeight.Value;
								}
								<>2__current = <valueInstruction>5__3;
								<>1__state = 2;
								return true;
							}
							goto IL_0198;
						}
						<>m__Finally1();
						<>7__wrap3 = null;
						return false;
						IL_0198:
						<instruction>5__5 = null;
						goto IL_019f;
					}
				}
				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__wrap3 != null)
				{
					<>7__wrap3.Dispose();
				}
			}

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

			[DebuggerHidden]
			IEnumerator<CodeInstruction> IEnumerable<CodeInstruction>.GetEnumerator()
			{
				<LevelTerrain_Transpiler>d__1 <LevelTerrain_Transpiler>d__;
				if (<>1__state == -2 && <>l__initialThreadId == Environment.CurrentManagedThreadId)
				{
					<>1__state = 0;
					<LevelTerrain_Transpiler>d__ = this;
				}
				else
				{
					<LevelTerrain_Transpiler>d__ = new <LevelTerrain_Transpiler>d__1(0);
				}
				<LevelTerrain_Transpiler>d__.instructions = <>3__instructions;
				return <LevelTerrain_Transpiler>d__;
			}

			[DebuggerHidden]
			IEnumerator IEnumerable.GetEnumerator()
			{
				return ((IEnumerable<CodeInstruction>)this).GetEnumerator();
			}
		}

		[IteratorStateMachine(typeof(<LevelTerrain_Transpiler>d__1))]
		[HarmonyPatch("LevelTerrain")]
		[HarmonyTranspiler]
		private static IEnumerable<CodeInstruction> LevelTerrain_Transpiler(IEnumerable<CodeInstruction> instructions)
		{
			//yield-return decompiler failed: Unexpected instruction in Iterator.Dispose()
			return new <LevelTerrain_Transpiler>d__1(-2)
			{
				<>3__instructions = instructions
			};
		}
	}

	[HarmonyPatch(typeof(TerrainComp))]
	private static class TerrainComp_Patches
	{
		private enum TranspilerState
		{
			Searching,
			Replacing
		}

		[CompilerGenerated]
		private sealed class <ApplyToHeightmap_Transpiler>d__1 : IEnumerable<CodeInstruction>, IEnumerable, IEnumerator<CodeInstruction>, IDisposable, IEnumerator
		{
			private int <>1__state;

			private CodeInstruction <>2__current;

			private int <>l__initialThreadId;

			private IEnumerable<CodeInstruction> instructions;

			public IEnumerable<CodeInstruction> <>3__instructions;

			private TranspilerState <state>5__2;

			private CodeInstruction <valueInstruction>5__3;

			private IEnumerator<CodeInstruction> <>7__wrap3;

			private CodeInstruction <instruction>5__5;

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

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

			[DebuggerHidden]
			public <ApplyToHeightmap_Transpiler>d__1(int <>1__state)
			{
				this.<>1__state = <>1__state;
				<>l__initialThreadId = Environment.CurrentManagedThreadId;
			}

			[DebuggerHidden]
			void IDisposable.Dispose()
			{
				int num = <>1__state;
				if (num == -3 || (uint)(num - 1) <= 2u)
				{
					try
					{
					}
					finally
					{
						<>m__Finally1();
					}
				}
				<valueInstruction>5__3 = null;
				<>7__wrap3 = null;
				<instruction>5__5 = null;
				<>1__state = -2;
			}

			private bool MoveNext()
			{
				try
				{
					switch (<>1__state)
					{
					default:
						return false;
					case 0:
						<>1__state = -1;
						<state>5__2 = TranspilerState.Searching;
						<valueInstruction>5__3 = null;
						<>7__wrap3 = instructions.GetEnumerator();
						<>1__state = -3;
						goto IL_019f;
					case 1:
						<>1__state = -3;
						goto IL_0198;
					case 2:
						<>1__state = -3;
						<>2__current = <instruction>5__5;
						<>1__state = 3;
						return true;
					case 3:
						{
							<>1__state = -3;
							<valueInstruction>5__3 = null;
							<state>5__2 = TranspilerState.Searching;
							goto IL_0198;
						}
						IL_019f:
						if (<>7__wrap3.MoveNext())
						{
							<instruction>5__5 = <>7__wrap3.Current;
							switch (<state>5__2)
							{
							case TranspilerState.Searching:
								if (<instruction>5__5.opcode == OpCodes.Ldc_R4 && (float)<instruction>5__5.operand == 8f)
								{
									<valueInstruction>5__3 = <instruction>5__5;
									<state>5__2 = TranspilerState.Replacing;
									break;
								}
								<>2__current = <instruction>5__5;
								<>1__state = 1;
								return true;
							case TranspilerState.Replacing:
								if (<instruction>5__5.opcode == OpCodes.Sub)
								{
									<valueInstruction>5__3.operand = MaximumDepth.Value;
								}
								else if (<instruction>5__5.opcode == OpCodes.Add)
								{
									<valueInstruction>5__3.operand = MaximumHeight.Value;
								}
								<>2__current = <valueInstruction>5__3;
								<>1__state = 2;
								return true;
							}
							goto IL_0198;
						}
						<>m__Finally1();
						<>7__wrap3 = null;
						return false;
						IL_0198:
						<instruction>5__5 = null;
						goto IL_019f;
					}
				}
				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__wrap3 != null)
				{
					<>7__wrap3.Dispose();
				}
			}

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

			[DebuggerHidden]
			IEnumerator<CodeInstruction> IEnumerable<CodeInstruction>.GetEnumerator()
			{
				<ApplyToHeightmap_Transpiler>d__1 <ApplyToHeightmap_Transpiler>d__;
				if (<>1__state == -2 && <>l__initialThreadId == Environment.CurrentManagedThreadId)
				{
					<>1__state = 0;
					<ApplyToHeightmap_Transpiler>d__ = this;
				}
				else
				{
					<ApplyToHeightmap_Transpiler>d__ = new <ApplyToHeightmap_Transpiler>d__1(0);
				}
				<ApplyToHeightmap_Transpiler>d__.instructions = <>3__instructions;
				return <ApplyToHeightmap_Transpiler>d__;
			}

			[DebuggerHidden]
			IEnumerator IEnumerable.GetEnumerator()
			{
				return ((IEnumerable<CodeInstruction>)this).GetEnumerator();
			}
		}

		[CompilerGenerated]
		private sealed class <LevelTerrain_Transpiler>d__2 : IEnumerable<CodeInstruction>, IEnumerable, IEnumerator<CodeInstruction>, IDisposable, IEnumerator
		{
			private int <>1__state;

			private CodeInstruction <>2__current;

			private int <>l__initialThreadId;

			private IEnumerable<CodeInstruction> instructions;

			public IEnumerable<CodeInstruction> <>3__instructions;

			private IEnumerator<CodeInstruction> <>7__wrap1;

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

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

			[DebuggerHidden]
			public <LevelTerrain_Transpiler>d__2(int <>1__state)
			{
				this.<>1__state = <>1__state;
				<>l__initialThreadId = Environment.CurrentManagedThreadId;
			}

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

			private bool MoveNext()
			{
				try
				{
					switch (<>1__state)
					{
					default:
						return false;
					case 0:
						<>1__state = -1;
						<>7__wrap1 = instructions.GetEnumerator();
						<>1__state = -3;
						break;
					case 1:
						<>1__state = -3;
						break;
					}
					if (<>7__wrap1.MoveNext())
					{
						CodeInstruction current = <>7__wrap1.Current;
						if (current.opcode == OpCodes.Ldc_R4)
						{
							if ((float)current.operand == 8f)
							{
								current.operand = MaximumHeight.Value;
							}
							else if ((float)current.operand == -8f)
							{
								current.operand = 0f - MaximumDepth.Value;
							}
						}
						<>2__current = current;
						<>1__state = 1;
						return true;
					}
					<>m__Finally1();
					<>7__wrap1 = null;
					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__wrap1 != null)
				{
					<>7__wrap1.Dispose();
				}
			}

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

			[DebuggerHidden]
			IEnumerator<CodeInstruction> IEnumerable<CodeInstruction>.GetEnumerator()
			{
				<LevelTerrain_Transpiler>d__2 <LevelTerrain_Transpiler>d__;
				if (<>1__state == -2 && <>l__initialThreadId == Environment.CurrentManagedThreadId)
				{
					<>1__state = 0;
					<LevelTerrain_Transpiler>d__ = this;
				}
				else
				{
					<LevelTerrain_Transpiler>d__ = new <LevelTerrain_Transpiler>d__2(0);
				}
				<LevelTerrain_Transpiler>d__.instructions = <>3__instructions;
				return <LevelTerrain_Transpiler>d__;
			}

			[DebuggerHidden]
			IEnumerator IEnumerable.GetEnumerator()
			{
				return ((IEnumerable<CodeInstruction>)this).GetEnumerator();
			}
		}

		[CompilerGenerated]
		private sealed class <RaiseTerrain_Transpiler>d__3 : IEnumerable<CodeInstruction>, IEnumerable, IEnumerator<CodeInstruction>, IDisposable, IEnumerator
		{
			private int <>1__state;

			private CodeInstruction <>2__current;

			private int <>l__initialThreadId;

			private IEnumerable<CodeInstruction> instructions;

			public IEnumerable<CodeInstruction> <>3__instructions;

			private IEnumerator<CodeInstruction> <>7__wrap1;

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

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

			[DebuggerHidden]
			public <RaiseTerrain_Transpiler>d__3(int <>1__state)
			{
				this.<>1__state = <>1__state;
				<>l__initialThreadId = Environment.CurrentManagedThreadId;
			}

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

			private bool MoveNext()
			{
				try
				{
					switch (<>1__state)
					{
					default:
						return false;
					case 0:
						<>1__state = -1;
						<>7__wrap1 = instructions.GetEnumerator();
						<>1__state = -3;
						break;
					case 1:
						<>1__state = -3;
						break;
					}
					if (<>7__wrap1.MoveNext())
					{
						CodeInstruction current = <>7__wrap1.Current;
						if (current.opcode == OpCodes.Ldc_R4)
						{
							if ((float)current.operand == 8f)
							{
								current.operand = MaximumHeight.Value;
							}
							else if ((float)current.operand == -8f)
							{
								current.operand = 0f - MaximumDepth.Value;
							}
						}
						<>2__current = current;
						<>1__state = 1;
						return true;
					}
					<>m__Finally1();
					<>7__wrap1 = null;
					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__wrap1 != null)
				{
					<>7__wrap1.Dispose();
				}
			}

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

			[DebuggerHidden]
			IEnumerator<CodeInstruction> IEnumerable<CodeInstruction>.GetEnumerator()
			{
				<RaiseTerrain_Transpiler>d__3 <RaiseTerrain_Transpiler>d__;
				if (<>1__state == -2 && <>l__initialThreadId == Environment.CurrentManagedThreadId)
				{
					<>1__state = 0;
					<RaiseTerrain_Transpiler>d__ = this;
				}
				else
				{
					<RaiseTerrain_Transpiler>d__ = new <RaiseTerrain_Transpiler>d__3(0);
				}
				<RaiseTerrain_Transpiler>d__.instructions = <>3__instructions;
				return <RaiseTerrain_Transpiler>d__;
			}

			[DebuggerHidden]
			IEnumerator IEnumerable.GetEnumerator()
			{
				return ((IEnumerable<CodeInstruction>)this).GetEnumerator();
			}
		}

		[IteratorStateMachine(typeof(<ApplyToHeightmap_Transpiler>d__1))]
		[HarmonyPatch("ApplyToHeightmap")]
		[HarmonyTranspiler]
		private static IEnumerable<CodeInstruction> ApplyToHeightmap_Transpiler(IEnumerable<CodeInstruction> instructions)
		{
			//yield-return decompiler failed: Unexpected instruction in Iterator.Dispose()
			return new <ApplyToHeightmap_Transpiler>d__1(-2)
			{
				<>3__instructions = instructions
			};
		}

		[IteratorStateMachine(typeof(<LevelTerrain_Transpiler>d__2))]
		[HarmonyPatch("LevelTerrain")]
		[HarmonyTranspiler]
		private static IEnumerable<CodeInstruction> LevelTerrain_Transpiler(IEnumerable<CodeInstruction> instructions)
		{
			//yield-return decompiler failed: Unexpected instruction in Iterator.Dispose()
			return new <LevelTerrain_Transpiler>d__2(-2)
			{
				<>3__instructions = instructions
			};
		}

		[IteratorStateMachine(typeof(<RaiseTerrain_Transpiler>d__3))]
		[HarmonyPatch("RaiseTerrain")]
		[HarmonyTranspiler]
		private static IEnumerable<CodeInstruction> RaiseTerrain_Transpiler(IEnumerable<CodeInstruction> instructions)
		{
			//yield-return decompiler failed: Unexpected instruction in Iterator.Dispose()
			return new <RaiseTerrain_Transpiler>d__3(-2)
			{
				<>3__instructions = instructions
			};
		}
	}

	public const string ModId = "dev.crystal.digdeeper";

	public static ConfigEntry<float> MaximumDepth;

	public static ConfigEntry<float> MaximumHeight;

	private static Harmony sHeightmapHarmony;

	private static Harmony sTerrainCompHarmony;

	private void Awake()
	{
		//IL_007e: Unknown result type (might be due to invalid IL or missing references)
		//IL_0088: Expected O, but got Unknown
		//IL_00a1: Unknown result type (might be due to invalid IL or missing references)
		//IL_00ab: Expected O, but got Unknown
		MaximumDepth = ((BaseUnityPlugin)this).Config.Bind<float>("Digging", "MaximumDepth", 20f, "The maximum depth you can dig below the terrain surface. Range 0-128. Game default is 8.");
		MaximumDepth.SettingChanged += Config_SettingChanged;
		MaximumHeight = ((BaseUnityPlugin)this).Config.Bind<float>("Digging", "MaximumHeight", 8f, "The maximum height you can raise the terrain. Range 0-128. Game default is 8.");
		MaximumHeight.SettingChanged += Config_SettingChanged;
		ClampConfig();
		sHeightmapHarmony = new Harmony("dev.crystal.digdeeper_Heightmap");
		sHeightmapHarmony.PatchAll(typeof(Heightmap_Patches));
		sTerrainCompHarmony = new Harmony("dev.crystal.digdeeper_TerrainComp");
		sTerrainCompHarmony.PatchAll(typeof(TerrainComp_Patches));
	}

	private void OnDestroy()
	{
		sHeightmapHarmony.UnpatchSelf();
	}

	private static void ClampConfig()
	{
		if (MaximumDepth.Value < 0f)
		{
			MaximumDepth.Value = 0f;
		}
		if (MaximumDepth.Value > 128f)
		{
			MaximumDepth.Value = 128f;
		}
		if (MaximumHeight.Value < 0f)
		{
			MaximumHeight.Value = 0f;
		}
		if (MaximumHeight.Value > 128f)
		{
			MaximumHeight.Value = 128f;
		}
	}

	private static void Config_SettingChanged(object sender, EventArgs e)
	{
		ClampConfig();
		sHeightmapHarmony.UnpatchSelf();
		sHeightmapHarmony.PatchAll(typeof(Heightmap_Patches));
		sTerrainCompHarmony.UnpatchSelf();
		sTerrainCompHarmony.PatchAll(typeof(TerrainComp_Patches));
	}
}