Decompiled source of CultivateAnywhere v1.0.0

CultivateAnywhere.dll

Decompiled 4 days ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[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 BakaCultivateAnywhere
{
	[BepInPlugin("baka.CultivateAnywhere", "CultivateAnywhere", "1.0.0")]
	public class CultivateAnywherePlugin : BaseUnityPlugin
	{
		public const string GUID = "baka.CultivateAnywhere";

		public const string NAME = "CultivateAnywhere";

		public const string VERSION = "1.0.0";

		internal static ManualLogSource Log;

		internal static ConfigEntry<bool> Enabled;

		internal static ConfigEntry<bool> AllowInAshlands;

		private Harmony _harmony;

		private void Awake()
		{
			//IL_0051: Unknown result type (might be due to invalid IL or missing references)
			//IL_005b: Expected O, but got Unknown
			Log = ((BaseUnityPlugin)this).Logger;
			Enabled = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "Enabled", true, "Let the cultivator (and any other piece that checks the vegetation mask) work on rock and cliff ground.");
			AllowInAshlands = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "AllowInAshlands", false, "Ashlands uses the same mask to mark lava. Leave this off to keep the vanilla Ashlands rule.");
			_harmony = new Harmony("baka.CultivateAnywhere");
			_harmony.PatchAll(typeof(CultivateAnywherePlugin).Assembly);
			Log.LogInfo((object)"CultivateAnywhere 1.0.0 loaded.");
		}

		private void OnDestroy()
		{
			if (_harmony != null)
			{
				_harmony.UnpatchSelf();
			}
		}
	}
	internal static class State
	{
		public static Piece GhostPiece;

		public static bool GhostOriginalFlag;

		public static readonly Dictionary<int, bool> Touched = new Dictionary<int, bool>();

		public static readonly HashSet<string> LoggedPrefabs = new HashSet<string>();

		public static bool WarnedOnce;

		public static void Clear()
		{
			GhostPiece = null;
			GhostOriginalFlag = false;
		}

		public static void Capture(Piece p)
		{
			int instanceID = ((Object)p).GetInstanceID();
			if (!Touched.TryGetValue(instanceID, out var value))
			{
				value = p.m_vegetationGroundOnly;
				Touched[instanceID] = value;
			}
			GhostPiece = p;
			GhostOriginalFlag = value;
			if (value)
			{
				string name = ((Object)((Component)p).gameObject).name;
				if (LoggedPrefabs.Add(name))
				{
					CultivateAnywherePlugin.Log.LogInfo((object)("Piece '" + name + "' uses the vegetation-mask check; CultivateAnywhere will bypass it while allowed."));
				}
			}
		}
	}
	internal static class ServerSanction
	{
		public const string HELLO_RPC = "CultivateAnywhere_ServerHello";

		public const int PROTO = 1;

		public static bool ServerHasMod;

		private static bool _loggedHello;

		public static bool Allowed()
		{
			if ((Object)(object)ZNet.instance != (Object)null)
			{
				if (!ZNet.instance.IsServer())
				{
					return ServerHasMod;
				}
				return true;
			}
			return false;
		}

		public static void RPC_ServerHello(ZRpc rpc, int proto)
		{
			ServerHasMod = true;
			if (!_loggedHello)
			{
				_loggedHello = true;
				CultivateAnywherePlugin.Log.LogInfo((object)("Server sanctions CultivateAnywhere (proto " + proto + "); bypass ACTIVE on this server."));
			}
		}

		public static void Reset()
		{
			ServerHasMod = false;
			_loggedHello = false;
		}
	}
	[HarmonyPatch(typeof(Player), "SetupPlacementGhost")]
	internal static class Patch_SetupPlacementGhost
	{
		private static void Postfix(Player __instance, GameObject ___m_placementGhost)
		{
			try
			{
				State.Touched.Clear();
				State.WarnedOnce = false;
				if ((Object)(object)___m_placementGhost == (Object)null)
				{
					State.Clear();
					return;
				}
				Piece component = ___m_placementGhost.GetComponent<Piece>();
				if ((Object)(object)component == (Object)null)
				{
					State.Clear();
				}
				else
				{
					State.Capture(component);
				}
			}
			catch (Exception e)
			{
				WarnOnce(e);
			}
		}

		internal static void WarnOnce(Exception e)
		{
			if (!State.WarnedOnce)
			{
				State.WarnedOnce = true;
				CultivateAnywherePlugin.Log.LogWarning((object)("CultivateAnywhere skipped a placement check: " + e));
			}
		}
	}
	[HarmonyPatch(typeof(Player), "UpdatePlacementGhost", new Type[] { typeof(bool) })]
	internal static class Patch_UpdatePlacementGhost
	{
		private static void Prefix(Player __instance, GameObject ___m_placementGhost)
		{
			try
			{
				if ((Object)(object)___m_placementGhost == (Object)null)
				{
					return;
				}
				if ((Object)(object)State.GhostPiece == (Object)null || (Object)(object)((Component)State.GhostPiece).gameObject != (Object)(object)___m_placementGhost)
				{
					Piece component = ___m_placementGhost.GetComponent<Piece>();
					if ((Object)(object)component == (Object)null)
					{
						State.Clear();
						return;
					}
					State.Capture(component);
				}
				bool flag = CultivateAnywherePlugin.Enabled.Value && ServerSanction.Allowed() && (CultivateAnywherePlugin.AllowInAshlands.Value || !TouchesAshlands(__instance, ___m_placementGhost));
				State.GhostPiece.m_vegetationGroundOnly = State.GhostOriginalFlag && !flag;
			}
			catch (Exception e)
			{
				Patch_SetupPlacementGhost.WarnOnce(e);
			}
		}

		private static bool TouchesAshlands(Player player, GameObject ghost)
		{
			//IL_0001: Unknown result type (might be due to invalid IL or missing references)
			//IL_0008: Invalid comparison between Unknown and I4
			//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_001e: Invalid comparison between Unknown and I4
			if ((int)player.GetCurrentBiome() == 32)
			{
				return true;
			}
			return (int)Heightmap.FindBiome(ghost.transform.position) == 32;
		}
	}
	[HarmonyPatch(typeof(ZNet), "OnNewConnection")]
	internal static class Patch_ZNet_OnNewConnection
	{
		private static void Postfix(ZNet __instance, ZNetPeer peer)
		{
			try
			{
				if (peer != null && peer.m_rpc != null)
				{
					peer.m_rpc.Register<int>("CultivateAnywhere_ServerHello", (Action<ZRpc, int>)ServerSanction.RPC_ServerHello);
					if (__instance.IsServer())
					{
						peer.m_rpc.Invoke("CultivateAnywhere_ServerHello", new object[1] { 1 });
					}
				}
			}
			catch (Exception ex)
			{
				CultivateAnywherePlugin.Log.LogWarning((object)("CultivateAnywhere handshake setup failed: " + ex.Message));
			}
		}
	}
	[HarmonyPatch(typeof(ZNet), "Shutdown", new Type[] { typeof(bool) })]
	internal static class Patch_ZNet_Shutdown
	{
		private static void Postfix()
		{
			ServerSanction.Reset();
		}
	}
}