Please disclose if any significant portion of your mod was created using AI tools by adding the 'AI Generated' category. Failing to do so may result in the mod being removed from Thunderstore.
Decompiled source of SlopeSafePaving v1.0.4
BepInEx/plugins/SlopeSafePaving/SlopeSafePaving.dll
Decompiled 16 hours agousing System; using System.Collections.Generic; using System.Reflection; using System.Reflection.Emit; using System.Runtime.CompilerServices; using BepInEx; using BepInEx.Configuration; using HarmonyLib; using UnityEngine; [assembly: CompilationRelaxations(8)] [assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)] [assembly: AssemblyVersion("0.0.0.0")] namespace SlopeSafePaving; [BepInPlugin("cz.misaw.slopesafepaving", "Slope Safe Paving", "1.0.4")] public sealed class Plugin : BaseUnityPlugin { internal static ConfigEntry<bool> Enabled; internal static ConfigEntry<float> MaxSlope; internal static ConfigEntry<int> EdgeProtection; internal static ConfigEntry<bool> UseWideEdgeProtection; private Harmony harmony; private void Awake() { //IL_004f: Unknown result type (might be due to invalid IL or missing references) //IL_0059: Expected O, but got Unknown //IL_00a1: Unknown result type (might be due to invalid IL or missing references) //IL_00ab: Expected O, but got Unknown //IL_00b6: Unknown result type (might be due to invalid IL or missing references) //IL_00c0: Expected O, but got Unknown Enabled = ((BaseUnityPlugin)this).Config.Bind<bool>("Paving", "Enabled", true, "Skip steep terrain when painting paved roads."); MaxSlope = ((BaseUnityPlugin)this).Config.Bind<float>("Paving", "MaxSlopeDegrees", 45f, new ConfigDescription("Maximum terrain slope to pave, in degrees. Lower values protect gentler slopes too.", (AcceptableValueBase)(object)new AcceptableValueRange<float>(0f, 89f), new object[0])); UseWideEdgeProtection = ((BaseUnityPlugin)this).Config.Bind<bool>("Paving", "UseWideEdgeProtection", false, "False: check only terrain triangles touching the paint point, allowing paving closer to cliff edges. True: use the wider protective margin from version 1.0.1."); EdgeProtection = ((BaseUnityPlugin)this).Config.Bind<int>("Paving", "EdgeProtectionCells", 1, new ConfigDescription("Extra grid cells checked only when UseWideEdgeProtection is true. Ignored by the default local triangle filter.", (AcceptableValueBase)(object)new AcceptableValueRange<int>(1, 2), new object[0])); harmony = new Harmony("cz.misaw.slopesafepaving"); try { harmony.PatchAll(typeof(Plugin).Assembly); ((BaseUnityPlugin)this).Logger.LogInfo((object)("Slope Safe Paving 1.0.4 loaded. Maximum paving slope: " + MaxSlope.Value + "; edge mode: " + (UseWideEdgeProtection.Value ? ("wide, cells=" + EdgeProtection.Value) : "local terrain triangles"))); } catch (Exception ex) { harmony.UnpatchSelf(); ((BaseUnityPlugin)this).Logger.LogError((object)("Slope Safe Paving could not patch this game version; filter is inactive. " + ex)); } } private void OnDestroy() { if (harmony != null) { harmony.UnpatchSelf(); } } } [HarmonyPatch(typeof(TerrainComp), "PaintCleared")] internal static class PavingPatch { internal static bool AllowPaint(Heightmap map, int x, int z, Settings settings) { //IL_0019: Unknown result type (might be due to invalid IL or missing references) //IL_001f: Invalid comparison between Unknown and I4 //IL_0080: Unknown result type (might be due to invalid IL or missing references) //IL_0085: Unknown result type (might be due to invalid IL or missing references) //IL_0097: Unknown result type (might be due to invalid IL or missing references) //IL_00a8: 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_00ce: Unknown result type (might be due to invalid IL or missing references) //IL_00e1: Unknown result type (might be due to invalid IL or missing references) //IL_00f4: Unknown result type (might be due to invalid IL or missing references) //IL_0109: 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) if (Plugin.Enabled == null || !Plugin.Enabled.Value) { return true; } if (settings == null || (int)settings.m_paintType != 2) { return true; } if ((Object)(object)map == (Object)null || Plugin.MaxSlope == null || Plugin.UseWideEdgeProtection == null || Plugin.EdgeProtection == null) { return false; } float scale = map.m_scale; if (scale <= 0f || float.IsNaN(scale) || float.IsInfinity(scale)) { return false; } float value = Plugin.MaxSlope.Value; bool value2 = Plugin.UseWideEdgeProtection.Value; Vector3 position = ((Component)map).transform.position; if (!value2) { LocalHeights h = new LocalHeights { Center = Sample(map, position, x, z, scale), West = Sample(map, position, x - 1, z, scale), East = Sample(map, position, x + 1, z, scale), South = Sample(map, position, x, z - 1, scale), North = Sample(map, position, x, z + 1, scale), NorthWest = Sample(map, position, x - 1, z + 1, scale), SouthEast = Sample(map, position, x + 1, z - 1, scale) }; return SlopeRule.AllowsLocalSurface(h, scale, value); } int num = Math.Max(1, Math.Min(2, Plugin.EdgeProtection.Value)) + 1; float[,] array = new float[2 * num + 1, 2 * num + 1]; for (int i = -num; i <= num; i++) { for (int j = -num; j <= num; j++) { array[j + num, i + num] = Sample(map, position, x + j, z + i, scale); } } return SlopeRule.AllowsFootprint(array, scale, value); } private static float Sample(Heightmap current, Vector3 origin, int x, int z, float spacing) { //IL_002b: Unknown result type (might be due to invalid IL or missing references) //IL_004d: Unknown result type (might be due to invalid IL or missing references) //IL_0052: Unknown result type (might be due to invalid IL or missing references) //IL_0057: Unknown result type (might be due to invalid IL or missing references) //IL_0058: Unknown result type (might be due to invalid IL or missing references) //IL_0069: Unknown result type (might be due to invalid IL or missing references) if (x >= 0 && z >= 0 && x <= current.m_width && z <= current.m_width) { return current.GetHeight(x, z) + origin.y; } Vector3 val = origin + new Vector3((float)(x - current.m_width / 2) * spacing, 0f, (float)(z - current.m_width / 2) * spacing); Heightmap val2 = Heightmap.FindHeightmap(val); float result = default(float); if (!((Object)(object)val2 != (Object)null) || !val2.GetWorldHeight(val, ref result)) { return float.NaN; } return result; } internal static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstruction> instructions, MethodBase __originalMethod) { //IL_009c: Unknown result type (might be due to invalid IL or missing references) //IL_00a6: Expected O, but got Unknown //IL_0362: Unknown result type (might be due to invalid IL or missing references) //IL_036c: Expected O, but got Unknown //IL_0375: Unknown result type (might be due to invalid IL or missing references) //IL_037f: Expected O, but got Unknown //IL_0393: Unknown result type (might be due to invalid IL or missing references) //IL_039d: Expected O, but got Unknown //IL_0325: Unknown result type (might be due to invalid IL or missing references) //IL_032f: Expected O, but got Unknown ParameterInfo[] parameters = __originalMethod.GetParameters(); if (__originalMethod.IsStatic || __originalMethod.DeclaringType != typeof(TerrainComp) || parameters.Length != 3 || parameters[0].ParameterType != typeof(Vector3) || parameters[1].ParameterType != typeof(Vector3) || parameters[2].ParameterType != typeof(Settings)) { throw new InvalidOperationException("Unsupported terrain paint method signature"); } List<CodeInstruction> list = new List<CodeInstruction>(); foreach (CodeInstruction instruction in instructions) { list.Add(new CodeInstruction(instruction)); } MethodInfo methodInfo = AccessTools.Method(typeof(Heightmap), "GetHeight", new Type[2] { typeof(int), typeof(int) }, (Type[])null); MethodInfo methodInfo2 = AccessTools.Method(typeof(PavingPatch), "AllowPaint", (Type[])null, (Type[])null); FieldInfo fieldInfo = AccessTools.Field(typeof(Settings), "m_paintHeightCheck"); int num = 0; for (int i = 0; i < list.Count; i++) { if (!CodeInstructionExtensions.LoadsField(list[i], fieldInfo, false)) { continue; } int j; for (j = i + 1; j < list.Count && j < i + 18 && !CodeInstructionExtensions.Calls(list[j], methodInfo); j++) { } if (j >= list.Count || j >= i + 18) { continue; } int num2 = i + 2; int num3 = j + 2; if ((list[i + 1].opcode != OpCodes.Brfalse && list[i + 1].opcode != OpCodes.Brfalse_S) || num3 >= list.Count || (list[num3].opcode != OpCodes.Bgt && list[num3].opcode != OpCodes.Bgt_S)) { continue; } int num4 = num3 + 1; if (num4 >= list.Count || !(list[i + 1].operand is Label) || !list[num4].labels.Contains((Label)list[i + 1].operand) || list[num2].opcode != OpCodes.Ldarg_0 || !CodeInstructionExtensions.LoadsField(list[num2 + 1], AccessTools.Field(typeof(TerrainComp), "m_hmap"), false)) { continue; } List<CodeInstruction> list2 = new List<CodeInstruction>(); for (int k = num2; k < j; k++) { if (list[k].blocks.Count != 0 || list[k].opcode.FlowControl == FlowControl.Branch || list[k].opcode.FlowControl == FlowControl.Cond_Branch) { throw new InvalidOperationException("Unsupported control flow inside terrain height sample"); } list2.Add(new CodeInstruction(list[k].opcode, list[k].operand)); } if (list[num4].blocks.Count != 0) { throw new InvalidOperationException("Unsupported exception boundary at terrain paint guard"); } list2.Add(new CodeInstruction(OpCodes.Ldarg_3, (object)null)); list2.Add(new CodeInstruction(OpCodes.Call, (object)methodInfo2)); list2.Add(new CodeInstruction(OpCodes.Brfalse, list[num3].operand)); list2[0].labels.AddRange(list[num4].labels); list[num4].labels.Clear(); list.InsertRange(num4, list2); num++; i = num4 + list2.Count; } if (num != 1) { throw new InvalidOperationException("Expected one terrain paint height guard, found " + num); } return list; } } internal struct LocalHeights { internal float Center; internal float West; internal float East; internal float South; internal float North; internal float NorthWest; internal float SouthEast; } internal static class SlopeRule { internal static bool AllowsLocalSurface(float[,] heights, float spacing, float degrees) { if (heights == null || heights.GetLength(0) != 3 || heights.GetLength(1) != 3) { return false; } return AllowsLocalSurface(new LocalHeights { Center = heights[1, 1], West = heights[0, 1], East = heights[2, 1], South = heights[1, 0], North = heights[1, 2], NorthWest = heights[0, 2], SouthEast = heights[2, 0] }, spacing, degrees); } internal static bool AllowsLocalSurface(LocalHeights h, float spacing, float degrees) { if (!TryLimit(spacing, degrees, out var limitSquared)) { return false; } if (AllowsTriangle(h.Center, h.West, h.South, spacing, limitSquared) && AllowsTriangle(h.South, h.SouthEast, h.Center, spacing, limitSquared) && AllowsTriangle(h.East, h.Center, h.SouthEast, spacing, limitSquared) && AllowsTriangle(h.West, h.Center, h.NorthWest, spacing, limitSquared) && AllowsTriangle(h.North, h.NorthWest, h.Center, spacing, limitSquared)) { return AllowsTriangle(h.Center, h.East, h.North, spacing, limitSquared); } return false; } private static bool TryLimit(float spacing, float degrees, out double limitSquared) { limitSquared = 0.0; if (spacing <= 0f || float.IsNaN(spacing) || float.IsInfinity(spacing) || degrees < 0f || degrees >= 90f || float.IsNaN(degrees)) { return false; } double num = Math.Tan((double)degrees * Math.PI / 180.0); limitSquared = num * num + 1E-06; return true; } private static bool AllowsTriangle(float corner, float alongX, float alongZ, float spacing, double limitSquared) { double num = ((double)alongX - (double)corner) / (double)spacing; double num2 = ((double)alongZ - (double)corner) / (double)spacing; return num * num + num2 * num2 <= limitSquared; } internal static bool AllowsFootprint(float[,] heights, float spacing, float degrees) { if (heights == null || heights.GetLength(0) < 3 || heights.GetLength(1) < 3 || !TryLimit(spacing, degrees, out var limitSquared)) { return false; } for (int i = 1; i < heights.GetLength(1) - 1; i++) { for (int j = 1; j < heights.GetLength(0) - 1; j++) { if (!AllowsGradient(heights[j, i], heights[j - 1, i], heights[j + 1, i], heights[j, i - 1], heights[j, i + 1], spacing, limitSquared)) { return false; } } } return true; } internal static bool Allows(float center, float left, float right, float back, float front, float spacing, float degrees) { if (TryLimit(spacing, degrees, out var limitSquared)) { return AllowsGradient(center, left, right, back, front, spacing, limitSquared); } return false; } private static bool AllowsGradient(float center, float left, float right, float back, float front, float spacing, double limitSquared) { double num = Math.Max(Math.Abs(center - left), Math.Abs(right - center)) / spacing; double num2 = Math.Max(Math.Abs(center - back), Math.Abs(front - center)) / spacing; return num * num + num2 * num2 <= limitSquared; } }