Decompiled source of CultivatedGroundFix v1.0.0

plugins/CultivatedGroundFix.dll

Decompiled a day ago
using System;
using System.Collections;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Configuration;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: TargetFramework(".NETFramework,Version=v4.7.2", FrameworkDisplayName = ".NET Framework 4.7.2")]
[assembly: AssemblyVersion("0.0.0.0")]
[module: RefSafetyRules(11)]
namespace Microsoft.CodeAnalysis
{
	[CompilerGenerated]
	[Embedded]
	internal sealed class EmbeddedAttribute : Attribute
	{
	}
}
namespace System.Runtime.CompilerServices
{
	[CompilerGenerated]
	[Embedded]
	[AttributeUsage(AttributeTargets.Module, AllowMultiple = false, Inherited = false)]
	internal sealed class RefSafetyRulesAttribute : Attribute
	{
		public readonly int Version;

		public RefSafetyRulesAttribute(int P_0)
		{
			Version = P_0;
		}
	}
}
namespace CultivatedGroundFix
{
	[BepInPlugin("valheimsquad.cultivatedgroundfix", "Cultivated Ground Fix", "1.0.0")]
	[BepInDependency(/*Could not decode attribute arguments.*/)]
	public class Plugin : BaseUnityPlugin
	{
		public const string Guid = "valheimsquad.cultivatedgroundfix";

		private const float Cell = 0.5f;

		private const int ChunkSize = 32;

		private const int Reach = 3;

		private const byte CultivatedCode = 2;

		private static Plugin s_self;

		private static ConfigEntry<bool> s_enabled;

		private static Func<float, float, bool> s_isColumn;

		private static Func<int, int, int, Vector3Int> s_chunkOf;

		private static IDictionary s_chunks;

		private static FieldInfo s_paintField;

		private static PropertyInfo s_paintIndexer;

		private static bool s_ready;

		private static int s_fixed;

		private static int s_reported;

		private static float s_nextReport;

		private void Awake()
		{
			//IL_007c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0099: Unknown result type (might be due to invalid IL or missing references)
			//IL_009e: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ad: Expected O, but got Unknown
			s_self = this;
			s_enabled = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "Enabled", true, "Count ground as cultivated when Tunnelheim hides a cultivated mark under a stale dirt mark.");
			try
			{
				if (BindTunnelheim())
				{
					MethodInfo methodInfo = AccessTools.Method(AccessTools.TypeByName("Heightmap"), "IsCultivated", new Type[1] { typeof(Vector3) }, (Type[])null);
					if (methodInfo == null)
					{
						((BaseUnityPlugin)this).Logger.LogError((object)"Heightmap.IsCultivated(Vector3) not found; fix disabled.");
						return;
					}
					new Harmony("valheimsquad.cultivatedgroundfix").Patch((MethodBase)methodInfo, (HarmonyMethod)null, new HarmonyMethod(typeof(Plugin).GetMethod("Decide", BindingFlags.Static | BindingFlags.NonPublic))
					{
						priority = 0
					}, (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
					s_ready = true;
					((BaseUnityPlugin)this).Logger.LogInfo((object)"Cultivated ground fix active.");
				}
			}
			catch (Exception ex)
			{
				((BaseUnityPlugin)this).Logger.LogError((object)("Setup failed, fix disabled: " + ex));
			}
		}

		private bool BindTunnelheim()
		{
			Type type = AccessTools.TypeByName("ValheimTunneling.VoxelWorld");
			if (type == null)
			{
				((BaseUnityPlugin)this).Logger.LogInfo((object)"Tunnelheim not loaded; nothing to fix.");
				return false;
			}
			s_isColumn = Bind<Func<float, float, bool>>(type, "IsVoxelColumnAt");
			s_chunkOf = Bind<Func<int, int, int, Vector3Int>>(type, "ChunkOf");
			s_chunks = AccessTools.Field(type, "s_chunks")?.GetValue(null) as IDictionary;
			IDictionary dictionary = s_chunks;
			s_paintField = ((dictionary != null) ? dictionary.GetType().GetGenericArguments()[1] : null)?.GetField("Paint");
			s_paintIndexer = s_paintField?.FieldType.GetProperty("Item");
			int num;
			if (s_isColumn != null && s_chunkOf != null && s_chunks != null && s_paintField != null)
			{
				num = ((s_paintIndexer != null) ? 1 : 0);
				if (num != 0)
				{
					goto IL_00f9;
				}
			}
			else
			{
				num = 0;
			}
			((BaseUnityPlugin)this).Logger.LogError((object)"A Tunnelheim member was not found (its internals changed?); fix disabled.");
			goto IL_00f9;
			IL_00f9:
			return (byte)num != 0;
		}

		private static T Bind<T>(Type owner, string method) where T : class
		{
			MethodInfo methodInfo = AccessTools.Method(owner, method, (Type[])null, (Type[])null);
			if (!(methodInfo == null))
			{
				return Delegate.CreateDelegate(typeof(T), methodInfo) as T;
			}
			return null;
		}

		private static void Decide(Vector3 worldPos, ref bool __result)
		{
			//IL_001e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0024: 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)
			if (__result || !s_ready || !s_enabled.Value)
			{
				return;
			}
			try
			{
				if (s_isColumn(worldPos.x, worldPos.z) && HasCultivatedMark(worldPos))
				{
					__result = true;
					s_fixed++;
				}
			}
			catch (Exception ex)
			{
				s_ready = false;
				((BaseUnityPlugin)s_self).Logger.LogError((object)("Fix disabled after an error: " + ex));
			}
		}

		private static bool HasCultivatedMark(Vector3 point)
		{
			//IL_0000: 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_0024: Unknown result type (might be due to invalid IL or missing references)
			//IL_0047: 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_0053: Unknown result type (might be due to invalid IL or missing references)
			//IL_006b: Unknown result type (might be due to invalid IL or missing references)
			int num = Mathf.RoundToInt(point.x / 0.5f);
			int num2 = Mathf.RoundToInt(point.z / 0.5f);
			int num3 = Mathf.RoundToInt(point.y / 0.5f);
			for (int num4 = num3 + 3; num4 >= num3 - 3; num4--)
			{
				Vector3Int val = s_chunkOf(num, num4, num2);
				if (s_chunks.Contains(val))
				{
					object value = s_paintField.GetValue(s_chunks[val]);
					if (value != null)
					{
						int num5 = num - ((Vector3Int)(ref val)).x * 32 + 32 * (num4 - ((Vector3Int)(ref val)).y * 32 + 32 * (num2 - ((Vector3Int)(ref val)).z * 32));
						if ((byte)s_paintIndexer.GetValue(value, new object[1] { num5 }) == 2)
						{
							return true;
						}
					}
				}
			}
			return false;
		}

		private void Update()
		{
			if (!(Time.unscaledTime < s_nextReport))
			{
				s_nextReport = Time.unscaledTime + 60f;
				if (s_fixed != s_reported)
				{
					s_reported = s_fixed;
					((BaseUnityPlugin)this).Logger.LogInfo((object)("Accepted " + s_fixed + " cultivated checks that Tunnelheim rejected."));
				}
			}
		}
	}
}