Decompiled source of ZenTerrain v0.2.3

plugins\ZenTerrain.dll

Decompiled a month ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Reflection.Emit;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using System.Security.Permissions;
using BepInEx;
using BepInEx.Configuration;
using HarmonyLib;
using Jotunn.Utils;
using Microsoft.CodeAnalysis;
using UnityEngine;
using Zen.Config;
using Zen.Lib;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: AssemblyTitle("ZenTerrain")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("ZenTerrain")]
[assembly: AssemblyCopyright("Copyright \ufffd  2021")]
[assembly: AssemblyTrademark("")]
[assembly: ComVisible(false)]
[assembly: Guid("e3243d22-4307-4008-ba36-9f326008cde5")]
[assembly: AssemblyFileVersion("0.0.1.0")]
[assembly: TargetFramework(".NETFramework,Version=v4.8", FrameworkDisplayName = ".NET Framework 4.8")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("0.0.1.0")]
[module: RefSafetyRules(11)]
namespace Microsoft.CodeAnalysis
{
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.Embedded]
	internal sealed class EmbeddedAttribute : Attribute
	{
	}
}
namespace System.Runtime.CompilerServices
{
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.Embedded]
	[AttributeUsage(AttributeTargets.Class | AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Event | AttributeTargets.Parameter | AttributeTargets.ReturnValue | AttributeTargets.GenericParameter, AllowMultiple = false, Inherited = false)]
	internal sealed class NullableAttribute : Attribute
	{
		public readonly byte[] NullableFlags;

		public NullableAttribute(byte P_0)
		{
			NullableFlags = new byte[1] { P_0 };
		}

		public NullableAttribute(byte[] P_0)
		{
			NullableFlags = P_0;
		}
	}
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.Embedded]
	[AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Method | AttributeTargets.Interface | AttributeTargets.Delegate, AllowMultiple = false, Inherited = false)]
	internal sealed class NullableContextAttribute : Attribute
	{
		public readonly byte Flag;

		public NullableContextAttribute(byte P_0)
		{
			Flag = P_0;
		}
	}
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.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 ZenTerrain
{
	public static class Configs
	{
		public static readonly ConfigEntry<float> RaiseLimit;

		public static readonly ConfigEntry<float> DigLimit;

		public static readonly ConfigEntry<float> BurriedSearchRadius;

		public static readonly ConfigEntry<KeyCode> AdminOverrideKey;

		static Configs()
		{
			AdminOverrideKey = Config.Define<KeyCode>(true, "Modification", "Admin Override Key", (KeyCode)304, "Admin can override the limits by being in God Mode and holding down this key when terraforming.");
			RaiseLimit = Config.Define<float>(true, "Modification", "Terrain Raise Limit", 0.5f, Config.AcceptRange<float>(0f, 8f), "Max height that terrain can be raised from default. (Vanilla: 8)");
			DigLimit = Config.Define<float>(true, "Modification", "Terrain Dig Limit", 1f, Config.AcceptRange<float>(0f, 8f), "Max depth that terrain can be lowered from default. (Vanilla: 8)");
			BurriedSearchRadius = Config.Define<float>(true, "Modification", "Burried Search Radius", 2f, Config.AcceptRange<float>(0f, 8f), "Radius to search for burried objects that if found will allow bypass of digging restrictions.");
		}
	}
	[BepInPlugin("ZenDragon.ZenTerrain", "ZenTerrain", "0.2.3")]
	[BepInDependency(/*Could not decode attribute arguments.*/)]
	[BepInDependency(/*Could not decode attribute arguments.*/)]
	[NetworkCompatibility(/*Could not decode attribute arguments.*/)]
	internal class Plugin : ZenMod<Plugin>
	{
		public const string PluginName = "ZenTerrain";

		public const string PluginVersion = "0.2.3";

		public const string PluginGUID = "ZenDragon.ZenTerrain";

		protected override void Setup()
		{
		}

		protected override void TitleScene(bool isFirstBoot)
		{
		}

		protected override void WorldStart()
		{
		}

		protected override void Shutdown()
		{
		}
	}
	[HarmonyPatch]
	public static class TerrainLimit
	{
		public const float VanillaLimit = 8f;

		private static readonly Collider[] HitCache = (Collider[])(object)new Collider[1024];

		private static readonly LayerMask SearchLayers = LayerMask.op_Implicit(LayerMask.GetMask(new string[3] { "Default", "Default_small", "static_solid" }));

		private static float ApplyLimit(float curHeight, float delta)
		{
			//IL_0015: 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_0060: Unknown result type (might be due to invalid IL or missing references)
			float num = curHeight + delta;
			if ((((Character)Player.m_localPlayer).InGodMode() && ZInput.GetKey(Configs.AdminOverrideKey.Value, true)) || ((Character)Player.m_localPlayer).InInterior())
			{
				return Mathf.Clamp(num, -8f, 8f);
			}
			int num2 = Physics.OverlapSphereNonAlloc(((Component)Player.m_localPlayer).transform.position, Configs.BurriedSearchRadius.Value, HitCache, LayerMask.op_Implicit(SearchLayers));
			for (int i = 0; i < num2; i++)
			{
				if (IsMinable(((Component)HitCache[i]).gameObject))
				{
					return Mathf.Clamp(num, -8f, Configs.RaiseLimit.Value);
				}
			}
			if (curHeight < 0f - Configs.DigLimit.Value)
			{
				if (delta > 0f)
				{
					return Mathf.Clamp(num, -8f, Configs.RaiseLimit.Value);
				}
				return curHeight;
			}
			if (curHeight > Configs.RaiseLimit.Value)
			{
				if (delta < 0f)
				{
					return Mathf.Clamp(num, 0f - Configs.DigLimit.Value, 8f);
				}
				return curHeight;
			}
			return Mathf.Clamp(num, 0f - Configs.DigLimit.Value, Configs.RaiseLimit.Value);
			static bool HasOre(DropTable? dropTable)
			{
				if (dropTable != null)
				{
					return dropTable.GetDropList().Any((GameObject drop) => ((Object)drop).name.Contains("Ore") || ((Object)drop).name.Contains("Scrap"));
				}
				return false;
			}
			static bool IsMinable(GameObject obj)
			{
				if (!HasOre(obj.GetComponentInParent<MineRock5>()?.m_dropItems) && !HasOre(obj.GetComponentInParent<MineRock>()?.m_dropItems) && !Object.op_Implicit((Object)(object)obj.GetComponentInParent<Beacon>()))
				{
					return Object.op_Implicit((Object)(object)obj.GetComponentInChildren<Beacon>());
				}
				return true;
			}
		}

		[HarmonyTranspiler]
		[HarmonyPatch(typeof(TerrainComp), "RaiseTerrain")]
		public static IEnumerable<CodeInstruction> TerrainComp_RaiseTerrain_Transpiler(IEnumerable<CodeInstruction> codes)
		{
			//IL_0002: Unknown result type (might be due to invalid IL or missing references)
			//IL_0008: Expected O, but got Unknown
			//IL_001d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0023: Expected O, but got Unknown
			//IL_0031: Unknown result type (might be due to invalid IL or missing references)
			//IL_0037: Expected O, but got Unknown
			//IL_0045: Unknown result type (might be due to invalid IL or missing references)
			//IL_004b: Expected O, but got Unknown
			//IL_0081: Unknown result type (might be due to invalid IL or missing references)
			//IL_0087: Expected O, but got Unknown
			//IL_009e: Unknown result type (might be due to invalid IL or missing references)
			//IL_00a4: Expected O, but got Unknown
			//IL_00e9: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ef: Expected O, but got Unknown
			CodeMatcher val = new CodeMatcher(codes, (ILGenerator)null);
			val.MatchStartForward((CodeMatch[])(object)new CodeMatch[3]
			{
				new CodeMatch((OpCode?)OpCodes.Ldloc_S, (object)null, (string)null),
				new CodeMatch((OpCode?)OpCodes.Add, (object)null, (string)null),
				new CodeMatch((OpCode?)OpCodes.Stind_R4, (object)null, (string)null)
			}).Set(OpCodes.Ldc_R4, (object)0f).MatchStartForward((CodeMatch[])(object)new CodeMatch[3]
			{
				new CodeMatch((OpCode?)OpCodes.Ldc_R4, (object)(-8f), (string)null),
				new CodeMatch((OpCode?)OpCodes.Ldc_R4, (object)8f, (string)null),
				new CodeMatch(CodeInstruction.Call(typeof(Mathf), "Clamp", new Type[3]
				{
					typeof(float),
					typeof(float),
					typeof(float)
				}, (Type[])null), (string)null)
			})
				.SetAndAdvance(OpCodes.Ldloc_S, (object)(byte)14)
				.SetAndAdvance(OpCodes.Nop, (object)null)
				.SetInstruction(CodeInstruction.Call(typeof(TerrainLimit), "ApplyLimit", (Type[])null, (Type[])null));
			return val.InstructionEnumeration();
		}
	}
}