Decompiled source of Farmland v1.0.4

Farmland.dll

Decompiled 16 hours 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;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: AssemblyTitle("Farmland")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("Crystal")]
[assembly: AssemblyProduct("Farmland")]
[assembly: AssemblyCopyright("Copyright © 2023 Crystal Ferrai")]
[assembly: AssemblyTrademark("")]
[assembly: ComVisible(false)]
[assembly: Guid("af35011c-e111-4c38-9a7c-d50c6d83d322")]
[assembly: AssemblyFileVersion("1.0.4.0")]
[assembly: TargetFramework(".NETFramework,Version=v4.8", FrameworkDisplayName = ".NET Framework 4.8")]
[assembly: AssemblyVersion("1.0.4.0")]
namespace Farmland;

[BepInPlugin("dev.crystal.farmland", "Farmland", "1.0.4.0")]
[BepInProcess("valheim.exe")]
[BepInProcess("valheim_server.exe")]
public class FarmlandPlugin : BaseUnityPlugin
{
	[HarmonyPatch(typeof(Player))]
	private static class Player_Patches
	{
		private enum TranspilerState
		{
			Searching,
			Updating,
			Finishing
		}

		[CompilerGenerated]
		private sealed class <UpdatePlacementGhost_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 IEnumerator<CodeInstruction> <>7__wrap2;

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

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

			[DebuggerHidden]
			public <UpdatePlacementGhost_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) <= 3u)
				{
					try
					{
					}
					finally
					{
						<>m__Finally1();
					}
				}
				<>7__wrap2 = null;
				<>1__state = -2;
			}

			private bool MoveNext()
			{
				//IL_00f2: Unknown result type (might be due to invalid IL or missing references)
				//IL_00fc: Expected O, but got Unknown
				try
				{
					switch (<>1__state)
					{
					default:
						return false;
					case 0:
						<>1__state = -1;
						<state>5__2 = TranspilerState.Searching;
						<>7__wrap2 = instructions.GetEnumerator();
						<>1__state = -3;
						break;
					case 1:
						<>1__state = -3;
						break;
					case 2:
						<>1__state = -3;
						<state>5__2 = TranspilerState.Finishing;
						break;
					case 3:
						<>1__state = -3;
						<state>5__2 = TranspilerState.Searching;
						break;
					case 4:
						<>1__state = -3;
						break;
					}
					while (<>7__wrap2.MoveNext())
					{
						CodeInstruction current = <>7__wrap2.Current;
						switch (<state>5__2)
						{
						case TranspilerState.Searching:
							if (current.opcode == OpCodes.Callvirt)
							{
								<state>5__2 = TranspilerState.Updating;
							}
							<>2__current = current;
							<>1__state = 1;
							return true;
						case TranspilerState.Updating:
							if (current.opcode == OpCodes.Ldc_R4 && (float)current.operand == 0.25f)
							{
								<>2__current = new CodeInstruction(OpCodes.Ldc_R4, (object)VegetationThreshold.Value);
								<>1__state = 2;
								return true;
							}
							<>2__current = current;
							<>1__state = 3;
							return true;
						case TranspilerState.Finishing:
							<>2__current = current;
							<>1__state = 4;
							return true;
						}
					}
					<>m__Finally1();
					<>7__wrap2 = 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__wrap2 != null)
				{
					<>7__wrap2.Dispose();
				}
			}

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

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

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

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

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

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

			private CodeInstruction <>2__current;

			private int <>l__initialThreadId;

			private ILGenerator generator;

			public ILGenerator <>3__generator;

			private IEnumerable<CodeInstruction> instructions;

			public IEnumerable<CodeInstruction> <>3__instructions;

			private Label <label1>5__2;

			private TranspilerState <state>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 <PaintCleared_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) <= 12u)
				{
					try
					{
					}
					finally
					{
						<>m__Finally1();
					}
				}
				<>7__wrap3 = null;
				<instruction>5__5 = null;
				<>1__state = -2;
			}

			private bool MoveNext()
			{
				//IL_0179: Unknown result type (might be due to invalid IL or missing references)
				//IL_0183: Expected O, but got Unknown
				//IL_01a9: Unknown result type (might be due to invalid IL or missing references)
				//IL_01b3: Expected O, but got Unknown
				//IL_01da: Unknown result type (might be due to invalid IL or missing references)
				//IL_01e4: Expected O, but got Unknown
				//IL_0201: Unknown result type (might be due to invalid IL or missing references)
				//IL_020b: Expected O, but got Unknown
				//IL_0228: Unknown result type (might be due to invalid IL or missing references)
				//IL_0232: Expected O, but got Unknown
				//IL_0259: Unknown result type (might be due to invalid IL or missing references)
				//IL_0263: Expected O, but got Unknown
				//IL_0289: Unknown result type (might be due to invalid IL or missing references)
				//IL_0293: Expected O, but got Unknown
				//IL_02bb: Unknown result type (might be due to invalid IL or missing references)
				//IL_02c5: Expected O, but got Unknown
				try
				{
					switch (<>1__state)
					{
					default:
						return false;
					case 0:
						<>1__state = -1;
						<label1>5__2 = generator.DefineLabel();
						<state>5__3 = TranspilerState.Searching;
						<>7__wrap3 = instructions.GetEnumerator();
						<>1__state = -3;
						goto IL_037a;
					case 1:
						<>1__state = -3;
						goto IL_0373;
					case 2:
						<>1__state = -3;
						<>2__current = new CodeInstruction(OpCodes.Ldloc_S, <instruction>5__5.operand);
						<>1__state = 3;
						return true;
					case 3:
						<>1__state = -3;
						<>2__current = new CodeInstruction(OpCodes.Ldc_R4, (object)0.25f);
						<>1__state = 4;
						return true;
					case 4:
						<>1__state = -3;
						<>2__current = new CodeInstruction(OpCodes.Bge_Un_S, (object)<label1>5__2);
						<>1__state = 5;
						return true;
					case 5:
						<>1__state = -3;
						<>2__current = new CodeInstruction(OpCodes.Ldarg_3, (object)null);
						<>1__state = 6;
						return true;
					case 6:
						<>1__state = -3;
						<>2__current = new CodeInstruction(OpCodes.Ldc_I4_1, (object)null);
						<>1__state = 7;
						return true;
					case 7:
						<>1__state = -3;
						<>2__current = new CodeInstruction(OpCodes.Bne_Un_S, (object)<label1>5__2);
						<>1__state = 8;
						return true;
					case 8:
						<>1__state = -3;
						<>2__current = new CodeInstruction(OpCodes.Ldc_R4, (object)0.25f);
						<>1__state = 9;
						return true;
					case 9:
						<>1__state = -3;
						<>2__current = new CodeInstruction(OpCodes.Stloc_S, <instruction>5__5.operand);
						<>1__state = 10;
						return true;
					case 10:
						<>1__state = -3;
						<state>5__3 = TranspilerState.Labeling;
						goto IL_0373;
					case 11:
						<>1__state = -3;
						<state>5__3 = TranspilerState.Searching;
						goto IL_0373;
					case 12:
						<>1__state = -3;
						<state>5__3 = TranspilerState.Finishing;
						goto IL_0373;
					case 13:
						{
							<>1__state = -3;
							goto IL_0373;
						}
						IL_037a:
						if (<>7__wrap3.MoveNext())
						{
							<instruction>5__5 = <>7__wrap3.Current;
							switch (<state>5__3)
							{
							case TranspilerState.Searching:
								if (<instruction>5__5.opcode == OpCodes.Ldfld && (FieldInfo)<instruction>5__5.operand == typeof(Color).GetField("a"))
								{
									<state>5__3 = TranspilerState.Updating;
								}
								<>2__current = <instruction>5__5;
								<>1__state = 1;
								return true;
							case TranspilerState.Updating:
								if (<instruction>5__5.opcode == OpCodes.Stloc_S)
								{
									<>2__current = <instruction>5__5;
									<>1__state = 2;
									return true;
								}
								<>2__current = <instruction>5__5;
								<>1__state = 11;
								return true;
							case TranspilerState.Labeling:
								<instruction>5__5.labels.Add(<label1>5__2);
								<>2__current = <instruction>5__5;
								<>1__state = 12;
								return true;
							case TranspilerState.Finishing:
								<>2__current = <instruction>5__5;
								<>1__state = 13;
								return true;
							}
							goto IL_0373;
						}
						<>m__Finally1();
						<>7__wrap3 = null;
						return false;
						IL_0373:
						<instruction>5__5 = null;
						goto IL_037a;
					}
				}
				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()
			{
				<PaintCleared_Transpiler>d__1 <PaintCleared_Transpiler>d__;
				if (<>1__state == -2 && <>l__initialThreadId == Environment.CurrentManagedThreadId)
				{
					<>1__state = 0;
					<PaintCleared_Transpiler>d__ = this;
				}
				else
				{
					<PaintCleared_Transpiler>d__ = new <PaintCleared_Transpiler>d__1(0);
				}
				<PaintCleared_Transpiler>d__.instructions = <>3__instructions;
				<PaintCleared_Transpiler>d__.generator = <>3__generator;
				return <PaintCleared_Transpiler>d__;
			}

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

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

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

	public static ConfigEntry<float> VegetationThreshold;

	private static Harmony sPlayerHarmony;

	private static Harmony sTerrainCompHarmony;

	private void Awake()
	{
		//IL_0044: Unknown result type (might be due to invalid IL or missing references)
		//IL_004e: Expected O, but got Unknown
		//IL_0067: Unknown result type (might be due to invalid IL or missing references)
		//IL_0071: Expected O, but got Unknown
		VegetationThreshold = ((BaseUnityPlugin)this).Config.Bind<float>("Land", "VegetationThreshold", 0f, "The amount of vegetation land must support to allow cultivation. Lower values provide more farmable land. Range 0.0 to 1.0. Game default 0.25. Mod default 0.0.");
		VegetationThreshold.SettingChanged += VegetationThreshold_SettingChanged;
		ClampConfig();
		sPlayerHarmony = new Harmony("dev.crystal.farmland_Player");
		sPlayerHarmony.PatchAll(typeof(Player_Patches));
		sTerrainCompHarmony = new Harmony("dev.crystal.farmland_TerrainComp");
		sTerrainCompHarmony.PatchAll(typeof(TerrainComp_Patches));
	}

	private void OnDestroy()
	{
		sPlayerHarmony.UnpatchSelf();
		sTerrainCompHarmony.UnpatchSelf();
	}

	private static void ClampConfig()
	{
		if (VegetationThreshold.Value < 0f)
		{
			VegetationThreshold.Value = 0f;
		}
		if (VegetationThreshold.Value > 1f)
		{
			VegetationThreshold.Value = 1f;
		}
	}

	private void VegetationThreshold_SettingChanged(object sender, EventArgs e)
	{
		ClampConfig();
		sPlayerHarmony.UnpatchSelf();
		sPlayerHarmony.PatchAll(typeof(Player_Patches));
	}
}