Decompiled source of GradualTerrain v1.0.0

plugins/GradualTerrain.dll

Decompiled 18 hours ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
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 BepInEx.Logging;
using GradualTerrain.modules;
using HarmonyLib;
using Jotunn.Utils;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.Default | DebuggableAttribute.DebuggingModes.DisableOptimizations | DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints | DebuggableAttribute.DebuggingModes.EnableEditAndContinue)]
[assembly: AssemblyTitle("GradualTerrain")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("GradualTerrain")]
[assembly: AssemblyCopyright("Copyright ©  2021")]
[assembly: AssemblyTrademark("")]
[assembly: ComVisible(false)]
[assembly: Guid("e3243d22-4307-4008-ba36-9f326008cde5")]
[assembly: AssemblyFileVersion("1.0.0")]
[assembly: TargetFramework(".NETFramework,Version=v4.8", FrameworkDisplayName = ".NET Framework 4.8")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.0.0.0")]
namespace GradualTerrain
{
	internal class Logger
	{
		public static LogLevel Level = (LogLevel)16;

		public static void enableDebugLogging(object sender, EventArgs e)
		{
			//IL_001d: 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)
			if (ValConfig.EnableDebugMode.Value)
			{
				Level = (LogLevel)32;
			}
			else
			{
				Level = (LogLevel)16;
			}
		}

		public static void setDebugLogging(bool state)
		{
			//IL_0014: Unknown result type (might be due to invalid IL or missing references)
			//IL_0009: Unknown result type (might be due to invalid IL or missing references)
			if (state)
			{
				Level = (LogLevel)32;
			}
			else
			{
				Level = (LogLevel)16;
			}
		}

		public static void LogDebug(string message)
		{
			//IL_0001: Unknown result type (might be due to invalid IL or missing references)
			//IL_0008: Invalid comparison between Unknown and I4
			if ((int)Level >= 32)
			{
				GradualTerrain.Log.LogInfo((object)message);
			}
		}

		public static void LogInfo(string message)
		{
			//IL_0001: Unknown result type (might be due to invalid IL or missing references)
			//IL_0008: Invalid comparison between Unknown and I4
			if ((int)Level >= 16)
			{
				GradualTerrain.Log.LogInfo((object)message);
			}
		}

		public static void LogWarning(string message)
		{
			//IL_0001: Unknown result type (might be due to invalid IL or missing references)
			//IL_0007: Invalid comparison between Unknown and I4
			if ((int)Level >= 4)
			{
				GradualTerrain.Log.LogWarning((object)message);
			}
		}

		public static void LogError(string message)
		{
			//IL_0001: Unknown result type (might be due to invalid IL or missing references)
			//IL_0007: Invalid comparison between Unknown and I4
			if ((int)Level >= 2)
			{
				GradualTerrain.Log.LogError((object)message);
			}
		}
	}
	internal class ValConfig
	{
		public static ConfigFile cfg;

		public static ConfigEntry<bool> EnableDebugMode;

		public static ConfigEntry<float> MaxTerrainHeightAdjustment;

		public static ConfigEntry<float> MinTerrainHeightAdjustment;

		public static ConfigEntry<int> AdjustmentRange;

		public static ConfigEntry<float> MaxAdjustmentMineSlope;

		public static ConfigEntry<float> MaxAdjustmentHillSlope;

		public static ConfigEntry<float> RingIncrements;

		public static ConfigEntry<float> OffsetFromCenter;

		public static ConfigEntry<float> CircularGranularity;

		public static ConfigEntry<float> OperationRadius;

		public static ConfigEntry<int> ChangesPerInterval;

		public static ConfigEntry<float> TerrainSmoothingModifier;

		public static ConfigEntry<float> TerrainSmoothingPower;

		public static ConfigEntry<bool> PaintTerrainDuringChange;

		public static ConfigEntry<bool> SmoothTerrainOnChange;

		public static ConfigEntry<bool> AdjustmentSquare;

		public static ConfigEntry<float> AdjustmentPower;

		public static ConfigEntry<bool> EnableBiomeSpecificHeightAdjustments;

		public static Dictionary<Biome, ConfigEntry<float>> BiomeBasedMinTerrainAdjust = new Dictionary<Biome, ConfigEntry<float>>();

		public static Dictionary<Biome, ConfigEntry<float>> BiomeBasedMaxTerrainAdjust = new Dictionary<Biome, ConfigEntry<float>>();

		public ValConfig(ConfigFile cf)
		{
			cfg = cf;
			cfg.SaveOnConfigSet = true;
			CreateConfigValues(cf);
			Logger.setDebugLogging(EnableDebugMode.Value);
			SetupMainFileWatcher();
		}

		private void CreateConfigValues(ConfigFile Config)
		{
			//IL_001b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0020: Unknown result type (might be due to invalid IL or missing references)
			//IL_002d: Expected O, but got Unknown
			//IL_002d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0037: Expected O, but got Unknown
			//IL_010c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0111: Unknown result type (might be due to invalid IL or missing references)
			//IL_0113: Unknown result type (might be due to invalid IL or missing references)
			//IL_0116: Unknown result type (might be due to invalid IL or missing references)
			//IL_011c: Invalid comparison between Unknown and I4
			//IL_012d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0139: Unknown result type (might be due to invalid IL or missing references)
			//IL_0154: Unknown result type (might be due to invalid IL or missing references)
			//IL_0175: Unknown result type (might be due to invalid IL or missing references)
			//IL_0190: Unknown result type (might be due to invalid IL or missing references)
			//IL_01ff: Unknown result type (might be due to invalid IL or missing references)
			//IL_020c: Unknown result type (might be due to invalid IL or missing references)
			EnableDebugMode = Config.Bind<bool>("Client config", "EnableDebugMode", false, new ConfigDescription("Enables Debug logging.", (AcceptableValueBase)null, new object[1] { (object)new ConfigurationManagerAttributes
			{
				IsAdvanced = true
			} }));
			EnableDebugMode.SettingChanged += Logger.enableDebugLogging;
			MaxTerrainHeightAdjustment = BindServerConfig("Terrain Height Adjustment", "MaxTerrainHeightAdjustment", 12f, "The height terrain can be raised compared to its original position (vanilla default 8).", advanced: false, 0f, 2000f);
			MinTerrainHeightAdjustment = BindServerConfig("Terrain Height Adjustment", "MinTerrainHeightAdjustment", -12f, "The depth terrain can be lowered to compared to its original position (vanilla default 8).", advanced: false, -2000f, 0f);
			EnableBiomeSpecificHeightAdjustments = BindServerConfig("Gradual Digging", "EnableBiomeSpecificHeightAdjustments", value: true, "Uses biome-specific configurations for height map adjustments. This allows for things such as, leveling mountains but not turning plains into an ocean.", null, advanced: true);
			EnableBiomeSpecificHeightAdjustments.SettingChanged += delegate
			{
				BiomeConfiguration.ClearCache();
			};
			foreach (Biome value in Enum.GetValues(typeof(Biome)))
			{
				if ((int)value != 0 && (int)value != 895)
				{
					Tuple<float, float> biomeDefaultHeightAdjustments = GetBiomeDefaultHeightAdjustments(value);
					ConfigEntry<float> val2 = BindServerConfig($"Terrain Height Adjustment Per Biome - {(object)value}", "MinTerrainHeightAdjustment", biomeDefaultHeightAdjustments.Item2, $"Minimum terrain height adjustment for {(object)value}.", advanced: true, -2000f, 0f);
					ConfigEntry<float> val3 = BindServerConfig($"Terrain Height Adjustment Per Biome - {(object)value}", "MaxTerrainHeightAdjustment", biomeDefaultHeightAdjustments.Item1, $"Maximum terrain height adjustment for {(object)value}.", advanced: true, 0f, 2000f);
					val2.SettingChanged += delegate
					{
						BiomeConfiguration.ClearCache();
					};
					val3.SettingChanged += delegate
					{
						BiomeConfiguration.ClearCache();
					};
					BiomeBasedMinTerrainAdjust.Add(value, val2);
					BiomeBasedMaxTerrainAdjust.Add(value, val3);
				}
			}
			AdjustmentRange = BindServerConfig("Gradual Digging", "AdjustmentRange", 60, "The range that gradual terrain modifications will be applied", advanced: false, 0, 200);
			MaxAdjustmentMineSlope = BindServerConfig("Gradual Digging", "MaxAdjustmentMineSlope", 0.5f, "The force that smoothing is applied outward from the target operation.");
			MaxAdjustmentHillSlope = BindServerConfig("Gradual Digging", "MaxAdjustmentHillSlope", 0.75f, "The force that smoothing is applied outward from the target operation.");
		}

		private static Tuple<float, float> GetBiomeDefaultHeightAdjustments(Biome targetBiome)
		{
			//IL_0011: 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_0013: Unknown result type (might be due to invalid IL or missing references)
			//IL_0014: Unknown result type (might be due to invalid IL or missing references)
			//IL_0015: Unknown result type (might be due to invalid IL or missing references)
			//IL_0018: Invalid comparison between Unknown and I4
			//IL_003d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0040: Invalid comparison between Unknown and I4
			//IL_001a: Unknown result type (might be due to invalid IL or missing references)
			//IL_001c: Invalid comparison between Unknown and I4
			//IL_0050: Unknown result type (might be due to invalid IL or missing references)
			//IL_0056: Invalid comparison between Unknown and I4
			//IL_0042: Unknown result type (might be due to invalid IL or missing references)
			//IL_0045: Invalid comparison between Unknown and I4
			//IL_002d: Unknown result type (might be due to invalid IL or missing references)
			//IL_002f: Invalid comparison between Unknown and I4
			//IL_001e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0020: Invalid comparison between Unknown and I4
			//IL_005a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0060: Invalid comparison between Unknown and I4
			//IL_0049: Unknown result type (might be due to invalid IL or missing references)
			//IL_004c: Invalid comparison between Unknown and I4
			//IL_0033: Unknown result type (might be due to invalid IL or missing references)
			//IL_0036: Invalid comparison between Unknown and I4
			//IL_0024: Unknown result type (might be due to invalid IL or missing references)
			//IL_0026: Invalid comparison between Unknown and I4
			Tuple<float, float> tuple = new Tuple<float, float>(0f, 0f);
			if ((int)targetBiome <= 16)
			{
				if ((int)targetBiome <= 4)
				{
					if ((int)targetBiome == 1)
					{
						goto IL_0076;
					}
					if ((int)targetBiome == 4)
					{
						goto IL_0064;
					}
				}
				else if ((int)targetBiome == 8 || (int)targetBiome == 16)
				{
					return new Tuple<float, float>(10f, -10f);
				}
			}
			else if ((int)targetBiome <= 64)
			{
				if ((int)targetBiome == 32)
				{
					goto IL_0076;
				}
				if ((int)targetBiome == 64)
				{
					goto IL_0064;
				}
			}
			else
			{
				if ((int)targetBiome == 256)
				{
					return new Tuple<float, float>(300f, -300f);
				}
				if ((int)targetBiome == 512)
				{
					return new Tuple<float, float>(10f, -20f);
				}
			}
			return new Tuple<float, float>(8f, -8f);
			IL_0064:
			return new Tuple<float, float>(6f, -50f);
			IL_0076:
			return new Tuple<float, float>(8f, -8f);
		}

		internal static void SetupMainFileWatcher()
		{
			FileSystemWatcher fileSystemWatcher = new FileSystemWatcher();
			fileSystemWatcher.NotifyFilter = NotifyFilters.LastWrite;
			fileSystemWatcher.Path = Path.GetDirectoryName(cfg.ConfigFilePath);
			fileSystemWatcher.Filter = "MidnightsFX.GradualTerrain.cfg";
			fileSystemWatcher.Changed += OnConfigFileChanged;
			fileSystemWatcher.SynchronizingObject = ThreadingHelper.SynchronizingObject;
			fileSystemWatcher.EnableRaisingEvents = true;
		}

		private static void OnConfigFileChanged(object sender, FileSystemEventArgs e)
		{
			if (ZNet.instance.IsServer())
			{
				Logger.LogInfo("Configuration file has been changed, reloading settings.");
				cfg.Reload();
			}
		}

		public static ConfigEntry<float[]> BindServerConfig(string catagory, string key, float[] value, string description, bool advanced = false, float valmin = 0f, float valmax = 150f)
		{
			//IL_001b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0020: Unknown result type (might be due to invalid IL or missing references)
			//IL_0028: Unknown result type (might be due to invalid IL or missing references)
			//IL_0036: Expected O, but got Unknown
			//IL_0036: Unknown result type (might be due to invalid IL or missing references)
			//IL_0040: Expected O, but got Unknown
			return cfg.Bind<float[]>(catagory, key, value, new ConfigDescription(description, (AcceptableValueBase)(object)new AcceptableValueRange<float>(valmin, valmax), new object[1] { (object)new ConfigurationManagerAttributes
			{
				IsAdminOnly = true,
				IsAdvanced = advanced
			} }));
		}

		public static ConfigEntry<bool> BindServerConfig(string catagory, string key, bool value, string description, AcceptableValueBase acceptableValues = null, bool advanced = false)
		{
			//IL_0014: Unknown result type (might be due to invalid IL or missing references)
			//IL_0019: Unknown result type (might be due to invalid IL or missing references)
			//IL_0021: Unknown result type (might be due to invalid IL or missing references)
			//IL_002f: Expected O, but got Unknown
			//IL_002f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0039: Expected O, but got Unknown
			return cfg.Bind<bool>(catagory, key, value, new ConfigDescription(description, acceptableValues, new object[1] { (object)new ConfigurationManagerAttributes
			{
				IsAdminOnly = true,
				IsAdvanced = advanced
			} }));
		}

		public static ConfigEntry<int> BindServerConfig(string catagory, string key, int value, string description, bool advanced = false, int valmin = 0, int valmax = 150)
		{
			//IL_001b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0020: Unknown result type (might be due to invalid IL or missing references)
			//IL_0028: Unknown result type (might be due to invalid IL or missing references)
			//IL_0036: Expected O, but got Unknown
			//IL_0036: Unknown result type (might be due to invalid IL or missing references)
			//IL_0040: Expected O, but got Unknown
			return cfg.Bind<int>(catagory, key, value, new ConfigDescription(description, (AcceptableValueBase)(object)new AcceptableValueRange<int>(valmin, valmax), new object[1] { (object)new ConfigurationManagerAttributes
			{
				IsAdminOnly = true,
				IsAdvanced = advanced
			} }));
		}

		public static ConfigEntry<float> BindServerConfig(string catagory, string key, float value, string description, bool advanced = false, float valmin = 0f, float valmax = 150f)
		{
			//IL_001b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0020: Unknown result type (might be due to invalid IL or missing references)
			//IL_0028: Unknown result type (might be due to invalid IL or missing references)
			//IL_0036: Expected O, but got Unknown
			//IL_0036: Unknown result type (might be due to invalid IL or missing references)
			//IL_0040: Expected O, but got Unknown
			return cfg.Bind<float>(catagory, key, value, new ConfigDescription(description, (AcceptableValueBase)(object)new AcceptableValueRange<float>(valmin, valmax), new object[1] { (object)new ConfigurationManagerAttributes
			{
				IsAdminOnly = true,
				IsAdvanced = advanced
			} }));
		}

		public static ConfigEntry<string> BindServerConfig(string catagory, string key, string value, string description, AcceptableValueList<string> acceptableValues = null, bool advanced = false)
		{
			//IL_0014: Unknown result type (might be due to invalid IL or missing references)
			//IL_0019: Unknown result type (might be due to invalid IL or missing references)
			//IL_0021: Unknown result type (might be due to invalid IL or missing references)
			//IL_002f: Expected O, but got Unknown
			//IL_002f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0039: Expected O, but got Unknown
			return cfg.Bind<string>(catagory, key, value, new ConfigDescription(description, (AcceptableValueBase)(object)acceptableValues, new object[1] { (object)new ConfigurationManagerAttributes
			{
				IsAdminOnly = true,
				IsAdvanced = advanced
			} }));
		}
	}
	[BepInPlugin("MidnightsFX.GradualTerrain", "GradualTerrain", "1.0.0")]
	[BepInDependency(/*Could not decode attribute arguments.*/)]
	[NetworkCompatibility(/*Could not decode attribute arguments.*/)]
	internal class GradualTerrain : BaseUnityPlugin
	{
		public const string PluginGUID = "MidnightsFX.GradualTerrain";

		public const string PluginName = "GradualTerrain";

		public const string PluginVersion = "1.0.0";

		internal static ManualLogSource Log;

		internal ValConfig cfg;

		public void Awake()
		{
			//IL_0038: Unknown result type (might be due to invalid IL or missing references)
			//IL_003e: Expected O, but got Unknown
			Log = ((BaseUnityPlugin)this).Logger;
			cfg = new ValConfig(((BaseUnityPlugin)this).Config);
			Log.LogInfo((object)"Breaking things down smoothly.");
			Assembly executingAssembly = Assembly.GetExecutingAssembly();
			Harmony val = new Harmony("MidnightsFX.GradualTerrain");
			val.PatchAll(executingAssembly);
		}
	}
}
namespace GradualTerrain.modules
{
	internal static class BiomeConfiguration
	{
		internal class HeightLimits
		{
			public float Min;

			public float Max;
		}

		private static ConditionalWeakTable<Heightmap, HeightLimits> cache = new ConditionalWeakTable<Heightmap, HeightLimits>();

		internal static float CurrentMin = -8f;

		internal static float CurrentMax = 8f;

		private static HeightLimits envelopeLimits;

		internal static HeightLimits GetHeightLimits(Heightmap hmap)
		{
			if ((Object)(object)hmap == (Object)null || !ValConfig.EnableBiomeSpecificHeightAdjustments.Value)
			{
				return new HeightLimits
				{
					Min = ValConfig.MinTerrainHeightAdjustment.Value,
					Max = ValConfig.MaxTerrainHeightAdjustment.Value
				};
			}
			if (cache.TryGetValue(hmap, out var value))
			{
				return value;
			}
			HeightLimits heightLimits = ComputeLimits(hmap);
			cache.Add(hmap, heightLimits);
			return heightLimits;
		}

		internal static void SetCurrentLimits(Heightmap hmap)
		{
			HeightLimits heightLimits = GetHeightLimits(hmap);
			CurrentMin = heightLimits.Min;
			CurrentMax = heightLimits.Max;
		}

		internal static void SetEnvelopeLimits()
		{
			HeightLimits heightLimits = GetEnvelopeLimits();
			CurrentMin = heightLimits.Min;
			CurrentMax = heightLimits.Max;
		}

		private static HeightLimits GetEnvelopeLimits()
		{
			if (!ValConfig.EnableBiomeSpecificHeightAdjustments.Value)
			{
				return new HeightLimits
				{
					Min = ValConfig.MinTerrainHeightAdjustment.Value,
					Max = ValConfig.MaxTerrainHeightAdjustment.Value
				};
			}
			if (envelopeLimits != null)
			{
				return envelopeLimits;
			}
			float num = ValConfig.MinTerrainHeightAdjustment.Value;
			float num2 = ValConfig.MaxTerrainHeightAdjustment.Value;
			foreach (ConfigEntry<float> value in ValConfig.BiomeBasedMinTerrainAdjust.Values)
			{
				num = Mathf.Min(num, value.Value);
			}
			foreach (ConfigEntry<float> value2 in ValConfig.BiomeBasedMaxTerrainAdjust.Values)
			{
				num2 = Mathf.Max(num2, value2.Value);
			}
			envelopeLimits = new HeightLimits
			{
				Min = num,
				Max = num2
			};
			return envelopeLimits;
		}

		internal static void ClearCache()
		{
			cache = new ConditionalWeakTable<Heightmap, HeightLimits>();
			envelopeLimits = null;
		}

		private static HeightLimits ComputeLimits(Heightmap hmap)
		{
			//IL_0023: Unknown result type (might be due to invalid IL or missing references)
			//IL_0032: Unknown result type (might be due to invalid IL or missing references)
			//IL_0034: Invalid comparison between I4 and Unknown
			//IL_0065: Unknown result type (might be due to invalid IL or missing references)
			//IL_0075: Unknown result type (might be due to invalid IL or missing references)
			Biome[] cornerBiomes = hmap.m_cornerBiomes;
			float num = 0f;
			float num2 = 0f;
			int num3 = 0;
			for (int i = 0; i < cornerBiomes.Length; i++)
			{
				Biome val = cornerBiomes[i];
				bool flag = false;
				for (int j = 0; j < i; j++)
				{
					if ((int)cornerBiomes[j] == (int)val)
					{
						flag = true;
						break;
					}
				}
				if (!flag && ValConfig.BiomeBasedMinTerrainAdjust.TryGetValue(val, out var value) && ValConfig.BiomeBasedMaxTerrainAdjust.TryGetValue(val, out var value2))
				{
					num += value.Value;
					num2 += value2.Value;
					num3++;
				}
			}
			if (num3 == 0)
			{
				return new HeightLimits
				{
					Min = ValConfig.MinTerrainHeightAdjustment.Value,
					Max = ValConfig.MaxTerrainHeightAdjustment.Value
				};
			}
			return new HeightLimits
			{
				Min = num / (float)num3,
				Max = num2 / (float)num3
			};
		}
	}
	internal static class GradualDigging
	{
		[HarmonyPatch(typeof(TerrainOp))]
		internal static class CheckAndModifySurroundingHeightMap
		{
			[HarmonyPostfix]
			[HarmonyPatch("Awake")]
			private static void Postfix(TerrainOp __instance)
			{
				//IL_0049: Unknown result type (might be due to invalid IL or missing references)
				//IL_004e: Unknown result type (might be due to invalid IL or missing references)
				//IL_005b: Unknown result type (might be due to invalid IL or missing references)
				//IL_00b0: Unknown result type (might be due to invalid IL or missing references)
				//IL_015d: Unknown result type (might be due to invalid IL or missing references)
				//IL_0191: Unknown result type (might be due to invalid IL or missing references)
				//IL_01d2: Unknown result type (might be due to invalid IL or missing references)
				//IL_0104: Unknown result type (might be due to invalid IL or missing references)
				//IL_0205: Unknown result type (might be due to invalid IL or missing references)
				//IL_02a6: Unknown result type (might be due to invalid IL or missing references)
				if (TerrainOp.m_forceDisableTerrainOps || !__instance.m_settings.m_raise)
				{
					return;
				}
				int value = ValConfig.AdjustmentRange.Value;
				if (value <= 0)
				{
					return;
				}
				Vector3 position = ((Component)__instance).transform.position;
				float raiseRadius = __instance.m_settings.m_raiseRadius;
				TerrainComp val = TerrainComp.FindTerrainCompiler(position);
				if ((Object)(object)val == (Object)null || !val.m_initialized)
				{
					return;
				}
				Heightmap hmap = val.m_hmap;
				if ((Object)(object)hmap == (Object)null || hmap.m_buildData == null)
				{
					return;
				}
				int num = default(int);
				int num2 = default(int);
				hmap.WorldToVertex(position, ref num, ref num2);
				int num3 = hmap.m_width + 1;
				int num4 = num2 * num3 + num;
				if (num4 < 0 || num4 >= hmap.m_buildData.m_baseHeights.Count)
				{
					Logger.LogWarning($"Got invalid index for centerpoint: pos: {position} idx: {num4} x: {num} y: {num2} rowlen: {num3}");
					return;
				}
				float num5 = hmap.m_buildData.m_baseHeights[num4] + ((Component)hmap).transform.position.y + val.m_levelDelta[num4];
				HashSet<TerrainComp> hashSet = new HashSet<TerrainComp>();
				BiomeConfiguration.HeightLimits heightLimits = BiomeConfiguration.GetHeightLimits(hmap);
				Logger.LogDebug($"Starting gradual terrain change from {position} at height {num5} with limits ->  max:{heightLimits.Max} min:{heightLimits.Min}");
				List<Heightmap> list = new List<Heightmap>();
				Heightmap.FindHeightmap(position, (float)value, list);
				foreach (Heightmap item in list)
				{
					if (!item.IsDistantLod)
					{
						SmoothHeightmap(item, position, num5, value, heightLimits, hashSet);
					}
				}
				float num6 = (val.m_lastOpRadius = Mathf.Max(raiseRadius, (float)value));
				foreach (TerrainComp item2 in hashSet)
				{
					item2.m_lastOpRadius = num6;
					item2.Save();
					item2.m_hmap.Poke(false);
				}
				if (Object.op_Implicit((Object)(object)ClutterSystem.instance))
				{
					ClutterSystem.instance.ResetGrass(position, num6);
				}
			}

			internal static void SmoothHeightmap(Heightmap hmap, Vector3 center, float centerTotalHeight, int range, BiomeConfiguration.HeightLimits limits, HashSet<TerrainComp> modifiedComps)
			{
				//IL_003f: Unknown result type (might be due to invalid IL or missing references)
				//IL_0044: 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_00bb: Unknown result type (might be due to invalid IL or missing references)
				//IL_00d8: Unknown result type (might be due to invalid IL or missing references)
				//IL_00ea: Unknown result type (might be due to invalid IL or missing references)
				//IL_00fc: Unknown result type (might be due to invalid IL or missing references)
				//IL_0107: Unknown result type (might be due to invalid IL or missing references)
				//IL_0171: Unknown result type (might be due to invalid IL or missing references)
				//IL_01c4: Unknown result type (might be due to invalid IL or missing references)
				//IL_01fb: Unknown result type (might be due to invalid IL or missing references)
				if (hmap.m_buildData == null)
				{
					return;
				}
				List<float> baseHeights = hmap.m_buildData.m_baseHeights;
				float scale = hmap.m_scale;
				int width = hmap.m_width;
				int num = width + 1;
				int num2 = width / 2;
				Vector3 position = ((Component)hmap).transform.position;
				int num3 = default(int);
				int num4 = default(int);
				hmap.WorldToVertex(center, ref num3, ref num4);
				int num5 = Mathf.CeilToInt((float)range / scale);
				int num6 = Mathf.Max(0, num3 - num5);
				int num7 = Mathf.Min(width, num3 + num5);
				int num8 = Mathf.Max(0, num4 - num5);
				int num9 = Mathf.Min(width, num4 + num5);
				float value = ValConfig.MaxAdjustmentMineSlope.Value;
				float value2 = ValConfig.MaxAdjustmentHillSlope.Value;
				float min = limits.Min;
				float max = limits.Max;
				TerrainComp val = TerrainComp.FindTerrainCompiler(position);
				for (int i = num8; i <= num9; i++)
				{
					for (int j = num6; j <= num7; j++)
					{
						float num10 = position.x + (float)(j - num2) * scale;
						float num11 = position.z + (float)(i - num2) * scale;
						float num12 = center.x - num10;
						float num13 = center.z - num11;
						float num14 = Mathf.Sqrt(num12 * num12 + num13 * num13);
						if (num14 > (float)range)
						{
							continue;
						}
						int num15 = i * num + j;
						float num16 = baseHeights[num15];
						float num17 = (((Object)(object)val != (Object)null && val.m_initialized) ? val.m_levelDelta[num15] : 0f);
						float num18 = num16 + position.y + num17;
						float num19 = num14 * value;
						float num20 = num14 * value2;
						float num21;
						if (num18 > centerTotalHeight + num19)
						{
							num21 = centerTotalHeight + num19;
						}
						else
						{
							if (!(num18 < centerTotalHeight - num20))
							{
								continue;
							}
							num21 = centerTotalHeight - num20;
						}
						float num22 = Mathf.Clamp(num21 - position.y - num16, min, max);
						if ((Object)(object)val == (Object)null)
						{
							if ((Object)(object)ZoneSystem.instance == (Object)null || !ZoneSystem.instance.IsZoneLoaded(position))
							{
								return;
							}
							val = hmap.GetAndCreateTerrainCompiler();
							if ((Object)(object)val == (Object)null || !val.m_initialized)
							{
								return;
							}
						}
						val.m_levelDelta[num15] = num22;
						val.m_modifiedHeight[num15] = true;
						modifiedComps.Add(val);
					}
				}
			}
		}
	}
	internal class HeightmapPatches
	{
		[HarmonyPatch(typeof(TerrainComp))]
		internal static class TerrainCompositionPatches
		{
			[HarmonyTranspiler]
			[HarmonyPatch("ApplyToHeightmap")]
			private static IEnumerable<CodeInstruction> ApplyToHeightmap(IEnumerable<CodeInstruction> instructions)
			{
				//IL_0003: Unknown result type (might be due to invalid IL or missing references)
				//IL_0009: Expected O, but got Unknown
				//IL_0027: Unknown result type (might be due to invalid IL or missing references)
				//IL_002d: Expected O, but got Unknown
				//IL_0089: Unknown result type (might be due to invalid IL or missing references)
				//IL_008f: Expected O, but got Unknown
				CodeMatcher val = new CodeMatcher(instructions, (ILGenerator)null);
				val.MatchStartForward((CodeMatch[])(object)new CodeMatch[1]
				{
					new CodeMatch((OpCode?)OpCodes.Ldc_R4, (object)8f, (string)null)
				}).RemoveInstructions(1).InsertAndAdvance((CodeInstruction[])(object)new CodeInstruction[1] { Transpilers.EmitDelegate<Func<float>>((Func<float>)MinTerrainAdjustNoNeg) })
					.ThrowIfNotMatch("Unable to patch min terrain adjustment.", Array.Empty<CodeMatch>())
					.Advance(1)
					.MatchStartForward((CodeMatch[])(object)new CodeMatch[1]
					{
						new CodeMatch((OpCode?)OpCodes.Ldc_R4, (object)8f, (string)null)
					})
					.RemoveInstructions(1)
					.InsertAndAdvance((CodeInstruction[])(object)new CodeInstruction[1] { Transpilers.EmitDelegate<Func<float>>((Func<float>)MaxTerrainAdjust) })
					.ThrowIfNotMatch("Unable to patch max terrain adjustment.", Array.Empty<CodeMatch>());
				return val.Instructions();
			}

			[HarmonyTranspiler]
			[HarmonyPatch("LevelTerrain")]
			private static IEnumerable<CodeInstruction> LevelTerrain(IEnumerable<CodeInstruction> instructions)
			{
				//IL_0003: Unknown result type (might be due to invalid IL or missing references)
				//IL_0009: Expected O, but got Unknown
				//IL_0027: Unknown result type (might be due to invalid IL or missing references)
				//IL_002d: Expected O, but got Unknown
				//IL_0044: Unknown result type (might be due to invalid IL or missing references)
				//IL_004a: Expected O, but got Unknown
				CodeMatcher val = new CodeMatcher(instructions, (ILGenerator)null);
				val.MatchStartForward((CodeMatch[])(object)new CodeMatch[2]
				{
					new CodeMatch((OpCode?)OpCodes.Ldc_R4, (object)(-8f), (string)null),
					new CodeMatch((OpCode?)OpCodes.Ldc_R4, (object)8f, (string)null)
				}).RemoveInstructions(2).InsertAndAdvance((CodeInstruction[])(object)new CodeInstruction[2]
				{
					Transpilers.EmitDelegate<Func<float>>((Func<float>)MinTerrainAdjust),
					Transpilers.EmitDelegate<Func<float>>((Func<float>)MaxTerrainAdjust)
				})
					.ThrowIfNotMatch("Unable to patch terrain level height adjustments.", Array.Empty<CodeMatch>());
				return val.Instructions();
			}

			[HarmonyTranspiler]
			[HarmonyPatch("RaiseTerrain")]
			private static IEnumerable<CodeInstruction> RaiseTerrain(IEnumerable<CodeInstruction> instructions)
			{
				//IL_0003: Unknown result type (might be due to invalid IL or missing references)
				//IL_0009: Expected O, but got Unknown
				//IL_0027: Unknown result type (might be due to invalid IL or missing references)
				//IL_002d: Expected O, but got Unknown
				//IL_0044: Unknown result type (might be due to invalid IL or missing references)
				//IL_004a: Expected O, but got Unknown
				CodeMatcher val = new CodeMatcher(instructions, (ILGenerator)null);
				val.MatchStartForward((CodeMatch[])(object)new CodeMatch[2]
				{
					new CodeMatch((OpCode?)OpCodes.Ldc_R4, (object)(-8f), (string)null),
					new CodeMatch((OpCode?)OpCodes.Ldc_R4, (object)8f, (string)null)
				}).RemoveInstructions(2).InsertAndAdvance((CodeInstruction[])(object)new CodeInstruction[2]
				{
					Transpilers.EmitDelegate<Func<float>>((Func<float>)MinTerrainAdjust),
					Transpilers.EmitDelegate<Func<float>>((Func<float>)MaxTerrainAdjust)
				})
					.ThrowIfNotMatch("Unable to patch terrain level height adjustments.", Array.Empty<CodeMatch>());
				return val.Instructions();
			}

			[HarmonyPrefix]
			[HarmonyPatch("LevelTerrain")]
			private static void LevelTerrainPrefix(TerrainComp __instance)
			{
				BiomeConfiguration.SetCurrentLimits(__instance.m_hmap);
			}

			[HarmonyPrefix]
			[HarmonyPatch("RaiseTerrain")]
			private static void RaiseTerrainPrefix(TerrainComp __instance)
			{
				BiomeConfiguration.SetCurrentLimits(__instance.m_hmap);
			}

			[HarmonyPrefix]
			[HarmonyPatch("ApplyToHeightmap")]
			private static void ApplyToHeightmapPrefix()
			{
				BiomeConfiguration.SetEnvelopeLimits();
			}

			internal static float MinTerrainAdjustNoNeg()
			{
				return Mathf.Abs(BiomeConfiguration.CurrentMin);
			}

			internal static float MinTerrainAdjust()
			{
				return BiomeConfiguration.CurrentMin;
			}

			internal static float MaxTerrainAdjust()
			{
				return BiomeConfiguration.CurrentMax;
			}
		}
	}
}