Decompiled source of Farmland v1.0.3

Farmland.dll

Decompiled a year ago
using System;
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.3.0")]
[assembly: TargetFramework(".NETFramework,Version=v4.8", FrameworkDisplayName = ".NET Framework 4.8")]
[assembly: AssemblyVersion("1.0.3.0")]
namespace Farmland;

[BepInPlugin("dev.crystal.farmland", "Farmland", "1.0.3.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
		}

		[HarmonyPatch("UpdatePlacementGhost")]
		[HarmonyTranspiler]
		private static IEnumerable<CodeInstruction> UpdatePlacementGhost_Transpiler(IEnumerable<CodeInstruction> instructions)
		{
			TranspilerState state = TranspilerState.Searching;
			foreach (CodeInstruction instruction in instructions)
			{
				switch (state)
				{
				case TranspilerState.Searching:
					if (instruction.opcode == OpCodes.Callvirt)
					{
						state = TranspilerState.Updating;
					}
					yield return instruction;
					break;
				case TranspilerState.Updating:
					if (instruction.opcode == OpCodes.Ldc_R4 && (float)instruction.operand == 0.25f)
					{
						yield return new CodeInstruction(OpCodes.Ldc_R4, (object)VegetationThreshold.Value);
						state = TranspilerState.Finishing;
					}
					else
					{
						yield return instruction;
						state = TranspilerState.Searching;
					}
					break;
				case TranspilerState.Finishing:
					yield return instruction;
					break;
				}
			}
		}
	}

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

		[HarmonyPatch("PaintCleared")]
		[HarmonyTranspiler]
		private static IEnumerable<CodeInstruction> PaintCleared_Transpiler(IEnumerable<CodeInstruction> instructions, ILGenerator generator)
		{
			Label label1 = generator.DefineLabel();
			TranspilerState state = TranspilerState.Searching;
			foreach (CodeInstruction instruction in instructions)
			{
				switch (state)
				{
				case TranspilerState.Searching:
					if (instruction.opcode == OpCodes.Ldfld && (FieldInfo)instruction.operand == typeof(Color).GetField("a"))
					{
						state = TranspilerState.Updating;
					}
					yield return instruction;
					break;
				case TranspilerState.Updating:
					if (instruction.opcode == OpCodes.Stloc_S)
					{
						yield return instruction;
						yield return new CodeInstruction(OpCodes.Ldloc_S, instruction.operand);
						yield return new CodeInstruction(OpCodes.Ldc_R4, (object)0.25f);
						yield return new CodeInstruction(OpCodes.Bge_Un_S, (object)label1);
						yield return new CodeInstruction(OpCodes.Ldarg_3, (object)null);
						yield return new CodeInstruction(OpCodes.Ldc_I4_1, (object)null);
						yield return new CodeInstruction(OpCodes.Bne_Un_S, (object)label1);
						yield return new CodeInstruction(OpCodes.Ldc_R4, (object)0.25f);
						yield return new CodeInstruction(OpCodes.Stloc_S, instruction.operand);
						state = TranspilerState.Labeling;
					}
					else
					{
						yield return instruction;
						state = TranspilerState.Searching;
					}
					break;
				case TranspilerState.Labeling:
					instruction.labels.Add(label1);
					yield return instruction;
					state = TranspilerState.Finishing;
					break;
				case TranspilerState.Finishing:
					yield return instruction;
					break;
				}
			}
		}
	}

	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));
	}
}