using System; using System.Collections.Generic; using System.Diagnostics; using System.Reflection; using System.Runtime.CompilerServices; using System.Runtime.Versioning; using BepInEx; using HarmonyLib; using UnityEngine; [assembly: CompilationRelaxations(8)] [assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)] [assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)] [assembly: TargetFramework(".NETFramework,Version=v4.6", FrameworkDisplayName = ".NET Framework 4.6")] [assembly: AssemblyCompany("DSPPreserveVegetation")] [assembly: AssemblyConfiguration("Release")] [assembly: AssemblyFileVersion("1.1.1.0")] [assembly: AssemblyInformationalVersion("1.1.1")] [assembly: AssemblyProduct("DSPPreserveVegetation")] [assembly: AssemblyTitle("DSPPreserveVegetation")] [assembly: AssemblyVersion("1.1.1.0")] namespace DSPPreserveVegetation; internal static class BuildingVegetationPatch { private sealed class BuildCollisionContext { internal readonly PlanetFactory Factory; private readonly ColliderData[] buildingColliders; private readonly Vector3 center; private readonly float searchRadius; internal BuildCollisionContext(PlanetFactory factory, ColliderData[] colliders) { //IL_0021: 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_0026: Unknown result type (might be due to invalid IL or missing references) //IL_002e: Unknown result type (might be due to invalid IL or missing references) Factory = factory; buildingColliders = colliders; center = ((colliders.Length != 0) ? GetCenter(colliders) : Vector3.zero); searchRadius = GetSearchRadius(colliders, center); } internal void RemoveOverlappingVegetation(bool emitEffects, int[] candidateIds) { //IL_0020: Unknown result type (might be due to invalid IL or missing references) //IL_009d: Unknown result type (might be due to invalid IL or missing references) //IL_00a3: Unknown result type (might be due to invalid IL or missing references) //IL_00b6: Unknown result type (might be due to invalid IL or missing references) if (buildingColliders.Length == 0) { return; } int vegetablesInAreaNonAlloc = Factory.planet.physics.nearColliderLogic.GetVegetablesInAreaNonAlloc(center, searchRadius, candidateIds); for (int i = 0; i < vegetablesInAreaNonAlloc; i++) { int num = candidateIds[i]; if (!OverlapsVegetation(num)) { continue; } ref VegeData reference = ref Factory.vegePool[num]; if (emitEffects && GameMain.gameTick > 0 && Factory.gameData.localLoadedPlanetFactory == Factory) { VegeProto val = ((ProtoSet)(object)LDB.veges).Select((int)reference.protoId); if (val != null) { VFEffectEmitter.Emit(val.MiningEffect, reference.pos, reference.rot); VFAudio.Create(val.MiningAudio, (Transform)null, reference.pos, true, 1, -1, -1L); } } Factory.RemoveVegeWithComponents(num); } } private bool OverlapsVegetation(int vegetationId) { //IL_0092: 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_0098: Unknown result type (might be due to invalid IL or missing references) //IL_00a2: Unknown result type (might be due to invalid IL or missing references) //IL_00af: Unknown result type (might be due to invalid IL or missing references) //IL_00b5: 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_00c0: Unknown result type (might be due to invalid IL or missing references) //IL_00c5: Unknown result type (might be due to invalid IL or missing references) //IL_00d4: Unknown result type (might be due to invalid IL or missing references) //IL_00d9: Unknown result type (might be due to invalid IL or missing references) if (vegetationId <= 0 || vegetationId >= Factory.vegePool.Length || Factory.vegePool[vegetationId].id != vegetationId || Factory.vegePool[vegetationId].protoId >= 9999) { return false; } ref VegeData reference = ref Factory.vegePool[vegetationId]; ColliderData[] array = ((ProtoSet)(object)LDB.veges).Select((int)reference.protoId)?.prefabDesc?.colliders; if (array == null) { return false; } for (int i = 0; i < array.Length; i++) { ColliderData val = array[i]; if (val.idType == 0 || (int)((ColliderData)(ref val)).usage != 0) { continue; } ColliderData second = ((ColliderData)(ref val)).BindToObject(vegetationId, 0, (EObjectType)1, reference.pos, reference.rot, reference.scl); for (int j = 0; j < buildingColliders.Length; j++) { if (ColliderIntersection.Overlaps(buildingColliders[j], second)) { return true; } } } return false; } private static Vector3 GetCenter(ColliderData[] colliders) { //IL_0000: Unknown result type (might be due to invalid IL or missing references) //IL_0005: Unknown result type (might be due to invalid IL or missing references) //IL_000a: 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_0017: Unknown result type (might be due to invalid IL or missing references) //IL_001c: Unknown result type (might be due to invalid IL or missing references) //IL_0027: Unknown result type (might be due to invalid IL or missing references) //IL_002c: Unknown result type (might be due to invalid IL or missing references) Vector3 val = Vector3.zero; for (int i = 0; i < colliders.Length; i++) { val += colliders[i].pos; } return val / (float)colliders.Length; } private static float GetSearchRadius(ColliderData[] colliders, Vector3 origin) { //IL_000c: Unknown result type (might be due to invalid IL or missing references) //IL_0011: 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_001a: Invalid comparison between Unknown and I4 //IL_0031: 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_0039: Unknown result type (might be due to invalid IL or missing references) //IL_003a: Unknown result type (might be due to invalid IL or missing references) //IL_003b: Unknown result type (might be due to invalid IL or missing references) float num = 0.1f; for (int i = 0; i < colliders.Length; i++) { ColliderData val = colliders[i]; float num2 = (((int)((ColliderData)(ref val)).shape == 1) ? val.radius : (((Vector3)(ref val.ext)).magnitude + val.radius)); num = Mathf.Max(num, Vector3.Distance(origin, val.pos) + num2); } return num + 6f; } } [ThreadStatic] private static BuildCollisionContext pendingBuild; [ThreadStatic] private static int[] candidateVegetationIds; internal static void Apply(Harmony harmony) { MethodInfo methodInfo = RequireMethod(typeof(PlanetFactory), "BuildFinally", new Type[4] { typeof(Player), typeof(int), typeof(bool), typeof(bool) }); MethodInfo methodInfo2 = RequireMethod(typeof(PlanetFactory), "FlattenTerrain", new Type[10] { typeof(Vector3), typeof(Quaternion), typeof(Bounds), typeof(float), typeof(float), typeof(bool), typeof(bool), typeof(bool), typeof(bool), typeof(Bounds) }); harmony.Patch((MethodBase)methodInfo, PatchMethod("BeforeBuildFinally"), (HarmonyMethod)null, (HarmonyMethod)null, PatchMethod("AfterBuildFinally"), (HarmonyMethod)null); harmony.Patch((MethodBase)methodInfo2, PatchMethod("BeforeFlattenTerrain"), PatchMethod("AfterFlattenTerrain"), (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null); } private static HarmonyMethod PatchMethod(string name) { //IL_0012: Unknown result type (might be due to invalid IL or missing references) //IL_0018: Expected O, but got Unknown return new HarmonyMethod(AccessTools.Method(typeof(BuildingVegetationPatch), name, (Type[])null, (Type[])null)); } private static MethodInfo RequireMethod(Type type, string name, Type[] parameters) { MethodInfo methodInfo = AccessTools.Method(type, name, parameters, (Type[])null); if (methodInfo == null) { throw new MissingMethodException(type.FullName, name); } return methodInfo; } private static void BeforeBuildFinally(PlanetFactory __instance, int prebuildId, bool flattenTerrain, out BuildCollisionContext __state) { __state = pendingBuild; pendingBuild = (flattenTerrain ? CreateBuildContext(__instance, prebuildId) : null); } private static Exception AfterBuildFinally(Exception __exception, BuildCollisionContext __state) { pendingBuild = __state; return __exception; } private static void BeforeFlattenTerrain(PlanetFactory __instance, ref Bounds removeVegeBound, out BuildCollisionContext __state) { //IL_0032: Unknown result type (might be due to invalid IL or missing references) //IL_0046: Unknown result type (might be due to invalid IL or missing references) //IL_004b: Unknown result type (might be due to invalid IL or missing references) //IL_0050: Unknown result type (might be due to invalid IL or missing references) __state = ((pendingBuild != null && pendingBuild.Factory == __instance) ? pendingBuild : null); if (__state != null) { removeVegeBound = new Bounds(new Vector3(1000000f, 1000000f, 1000000f), new Vector3(0.002f, 0.002f, 0.002f)); } } private static void AfterFlattenTerrain(bool emitEffects, BuildCollisionContext __state) { __state?.RemoveOverlappingVegetation(emitEffects, GetCandidateBuffer()); } private static int[] GetCandidateBuffer() { if (candidateVegetationIds == null) { candidateVegetationIds = new int[4096]; } return candidateVegetationIds; } private static BuildCollisionContext CreateBuildContext(PlanetFactory factory, int prebuildId) { //IL_0018: Unknown result type (might be due to invalid IL or missing references) //IL_001d: Unknown result type (might be due to invalid IL or missing references) //IL_001e: 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) //IL_004f: Unknown result type (might be due to invalid IL or missing references) //IL_009b: Unknown result type (might be due to invalid IL or missing references) //IL_00a0: Unknown result type (might be due to invalid IL or missing references) //IL_00a2: Unknown result type (might be due to invalid IL or missing references) //IL_00ad: Unknown result type (might be due to invalid IL or missing references) //IL_00d5: Unknown result type (might be due to invalid IL or missing references) //IL_00d7: 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_00c2: Unknown result type (might be due to invalid IL or missing references) //IL_00c3: Unknown result type (might be due to invalid IL or missing references) //IL_00c8: Unknown result type (might be due to invalid IL or missing references) //IL_00c9: 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) if (prebuildId <= 0 || prebuildId >= factory.prebuildPool.Length) { return null; } PrebuildData val = factory.prebuildPool[prebuildId]; if (val.id != prebuildId) { return null; } PrefabDesc val2 = LDB.models.modelArray[val.modelIndex]?.prefabDesc ?? ((ProtoSet)(object)LDB.items).Select((int)val.protoId)?.prefabDesc; if (val2?.colliders == null) { return new BuildCollisionContext(factory, (ColliderData[])(object)new ColliderData[0]); } List list = new List(val2.colliders.Length); for (int i = 0; i < val2.colliders.Length; i++) { ColliderData val3 = val2.colliders[i]; if (val3.idType != 0 && (int)((ColliderData)(ref val3)).usage == 0) { list.Add(val2.isInserter ? BindInserterCollider(val3, val) : ((ColliderData)(ref val3)).BindToObject(0, 0, (EObjectType)0, val.pos, val.rot)); } } return new BuildCollisionContext(factory, list.ToArray()); } private static ColliderData BindInserterCollider(ColliderData source, PrebuildData prebuild) { //IL_0000: Unknown result type (might be due to invalid IL or missing references) //IL_0001: Unknown result type (might be due to invalid IL or missing references) //IL_0006: Unknown result type (might be due to invalid IL or missing references) //IL_0007: Unknown result type (might be due to invalid IL or missing references) //IL_0011: Unknown result type (might be due to invalid IL or missing references) //IL_0016: 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_0018: Unknown result type (might be due to invalid IL or missing references) //IL_001d: Unknown result type (might be due to invalid IL or missing references) //IL_0022: Unknown result type (might be due to invalid IL or missing references) //IL_0027: 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_002d: 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_0037: Unknown result type (might be due to invalid IL or missing references) //IL_003c: Unknown result type (might be due to invalid IL or missing references) //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_0045: Unknown result type (might be due to invalid IL or missing references) //IL_0046: Unknown result type (might be due to invalid IL or missing references) //IL_004b: 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_0051: Unknown result type (might be due to invalid IL or missing references) //IL_0056: 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_005c: Unknown result type (might be due to invalid IL or missing references) //IL_005f: 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) //IL_006a: 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) //IL_0075: Unknown result type (might be due to invalid IL or missing references) //IL_0076: Unknown result type (might be due to invalid IL or missing references) //IL_007b: Unknown result type (might be due to invalid IL or missing references) //IL_007c: Unknown result type (might be due to invalid IL or missing references) //IL_008c: Unknown result type (might be due to invalid IL or missing references) //IL_008d: Unknown result type (might be due to invalid IL or missing references) //IL_0098: Unknown result type (might be due to invalid IL or missing references) //IL_009d: Unknown result type (might be due to invalid IL or missing references) //IL_00a7: 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_00a9: Unknown result type (might be due to invalid IL or missing references) Vector3 val = Vector3.Lerp(prebuild.pos, prebuild.pos2, 0.5f); Vector3 val2 = prebuild.rot * Vector3.up + prebuild.rot2 * Vector3.up; Vector3 normalized = ((Vector3)(ref val2)).normalized; Quaternion val3 = Quaternion.LookRotation(prebuild.pos2 - prebuild.pos, normalized); source.ext = new Vector3(source.ext.x, source.ext.y, Vector3.Distance(prebuild.pos2, prebuild.pos) * 0.5f + source.ext.z); return ((ColliderData)(ref source)).BindToObject(0, 0, (EObjectType)0, val, val3); } } internal static class ColliderIntersection { private const float RequiredPenetration = 0.001f; internal static bool Overlaps(ColliderData first, ColliderData second) { //IL_0000: Unknown result type (might be due to invalid IL or missing references) //IL_0008: 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_0019: Unknown result type (might be due to invalid IL or missing references) //IL_001a: Unknown result type (might be due to invalid IL or missing references) //IL_001c: Unknown result type (might be due to invalid IL or missing references) //IL_002e: Expected I4, but got Unknown //IL_0030: Unknown result type (might be due to invalid IL or missing references) //IL_0031: Unknown result type (might be due to invalid IL or missing references) //IL_0038: Unknown result type (might be due to invalid IL or missing references) //IL_0039: Unknown result type (might be due to invalid IL or missing references) //IL_0040: Unknown result type (might be due to invalid IL or missing references) //IL_0041: Unknown result type (might be due to invalid IL or missing references) if (first.idType == 0 || second.idType == 0) { return false; } EColliderShape shape = ((ColliderData)(ref first)).shape; return (shape - 1) switch { 0 => SphereOverlaps(first, second), 1 => CapsuleOverlaps(first, second), 2 => BoxOverlaps(first, second), _ => false, }; } private static bool SphereOverlaps(ColliderData sphere, ColliderData other) { //IL_0002: Unknown result type (might be due to invalid IL or missing references) //IL_0007: Unknown result type (might be due to invalid IL or missing references) //IL_0008: Unknown result type (might be due to invalid IL or missing references) //IL_000a: Unknown result type (might be due to invalid IL or missing references) //IL_001c: Expected I4, but got Unknown //IL_001e: Unknown result type (might be due to invalid IL or missing references) //IL_001f: 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_0025: Unknown result type (might be due to invalid IL or missing references) //IL_002a: Unknown result type (might be due to invalid IL or missing references) //IL_002f: Unknown result type (might be due to invalid IL or missing references) //IL_0037: Unknown result type (might be due to invalid IL or missing references) //IL_003d: Unknown result type (might be due to invalid IL or missing references) //IL_004a: Unknown result type (might be due to invalid IL or missing references) //IL_0054: Unknown result type (might be due to invalid IL or missing references) //IL_0055: Unknown result type (might be due to invalid IL or missing references) //IL_005a: 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_0061: Unknown result type (might be due to invalid IL or missing references) //IL_0067: Unknown result type (might be due to invalid IL or missing references) //IL_0074: 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) //IL_007a: Unknown result type (might be due to invalid IL or missing references) //IL_0080: Unknown result type (might be due to invalid IL or missing references) EColliderShape shape = ((ColliderData)(ref other)).shape; switch (shape - 1) { case 0: { Vector3 val = sphere.pos - other.pos; return IsWithinRadius(((Vector3)(ref val)).sqrMagnitude, sphere.radius + other.radius); } case 1: { GetCapsuleSegment(other, out var start, out var end); return IsWithinRadius(PointSegmentDistanceSquared(sphere.pos, start, end), sphere.radius + other.radius); } case 2: return IsWithinRadius(PointBoxDistanceSquared(sphere.pos, other), sphere.radius); default: return false; } } private static bool CapsuleOverlaps(ColliderData capsule, ColliderData other) { //IL_0000: Unknown result type (might be due to invalid IL or missing references) //IL_000c: Unknown result type (might be due to invalid IL or missing references) //IL_0011: 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_0016: Unknown result type (might be due to invalid IL or missing references) //IL_0028: Expected I4, but got Unknown //IL_002a: Unknown result type (might be due to invalid IL or missing references) //IL_002b: Unknown result type (might be due to invalid IL or missing references) //IL_0030: Unknown result type (might be due to invalid IL or missing references) //IL_0031: Unknown result type (might be due to invalid IL or missing references) //IL_0037: Unknown result type (might be due to invalid IL or missing references) //IL_003d: Unknown result type (might be due to invalid IL or missing references) //IL_004a: Unknown result type (might be due to invalid IL or missing references) //IL_0054: Unknown result type (might be due to invalid IL or missing references) //IL_0055: Unknown result type (might be due to invalid IL or missing references) //IL_0056: 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_005d: Unknown result type (might be due to invalid IL or missing references) //IL_0063: Unknown result type (might be due to invalid IL or missing references) //IL_0070: Unknown result type (might be due to invalid IL or missing references) //IL_0071: Unknown result type (might be due to invalid IL or missing references) //IL_0072: Unknown result type (might be due to invalid IL or missing references) //IL_0078: Unknown result type (might be due to invalid IL or missing references) GetCapsuleSegment(capsule, out var start, out var end); EColliderShape shape = ((ColliderData)(ref other)).shape; switch (shape - 1) { case 0: return IsWithinRadius(PointSegmentDistanceSquared(other.pos, start, end), capsule.radius + other.radius); case 1: { GetCapsuleSegment(other, out var start2, out var end2); return IsWithinRadius(SegmentSegmentDistanceSquared(start, end, start2, end2), capsule.radius + other.radius); } case 2: return IsWithinRadius(SegmentBoxDistanceSquared(start, end, other), capsule.radius); default: return false; } } private static bool BoxOverlaps(ColliderData box, ColliderData other) { //IL_0002: Unknown result type (might be due to invalid IL or missing references) //IL_0007: Unknown result type (might be due to invalid IL or missing references) //IL_0008: Unknown result type (might be due to invalid IL or missing references) //IL_000a: Unknown result type (might be due to invalid IL or missing references) //IL_001c: Expected I4, but got Unknown //IL_001e: Unknown result type (might be due to invalid IL or missing references) //IL_001f: 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_002a: Unknown result type (might be due to invalid IL or missing references) //IL_0036: Unknown result type (might be due to invalid IL or missing references) //IL_0040: Unknown result type (might be due to invalid IL or missing references) //IL_0041: Unknown result type (might be due to invalid IL or missing references) //IL_0042: Unknown result type (might be due to invalid IL or missing references) //IL_0048: Unknown result type (might be due to invalid IL or missing references) //IL_0054: Unknown result type (might be due to invalid IL or missing references) //IL_0055: Unknown result type (might be due to invalid IL or missing references) EColliderShape shape = ((ColliderData)(ref other)).shape; switch (shape - 1) { case 0: return IsWithinRadius(PointBoxDistanceSquared(other.pos, box), other.radius); case 1: { GetCapsuleSegment(other, out var start, out var end); return IsWithinRadius(SegmentBoxDistanceSquared(start, end, box), other.radius); } case 2: return BoxesOverlap(box, other); default: return false; } } private static bool IsWithinRadius(float distanceSquared, float combinedRadius) { float num = combinedRadius - 0.001f; if (num > 0f) { return distanceSquared < num * num; } return false; } private static void GetCapsuleSegment(ColliderData capsule, out Vector3 start, out Vector3 end) { //IL_0000: Unknown result type (might be due to invalid IL or missing references) //IL_0001: Unknown result type (might be due to invalid IL or missing references) //IL_0006: Unknown result type (might be due to invalid IL or missing references) //IL_0007: Unknown result type (might be due to invalid IL or missing references) //IL_000c: Unknown result type (might be due to invalid IL or missing references) //IL_0011: 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_0019: Unknown result type (might be due to invalid IL or missing references) //IL_001a: Unknown result type (might be due to invalid IL or missing references) //IL_001f: Unknown result type (might be due to invalid IL or missing references) //IL_0025: Unknown result type (might be due to invalid IL or missing references) //IL_0026: Unknown result type (might be due to invalid IL or missing references) //IL_002b: Unknown result type (might be due to invalid IL or missing references) //IL_002c: Unknown result type (might be due to invalid IL or missing references) //IL_0031: Unknown result type (might be due to invalid IL or missing references) Vector3 val = capsule.q * capsule.ext; start = capsule.pos - val; end = capsule.pos + val; } private static float PointSegmentDistanceSquared(Vector3 point, Vector3 start, Vector3 end) { //IL_0000: Unknown result type (might be due to invalid IL or missing references) //IL_0001: Unknown result type (might be due to invalid IL or missing references) //IL_0002: Unknown result type (might be due to invalid IL or missing references) //IL_0007: 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_0029: Unknown result type (might be due to invalid IL or missing references) //IL_002a: Unknown result type (might be due to invalid IL or missing references) //IL_002f: Unknown result type (might be due to invalid IL or missing references) //IL_003d: Unknown result type (might be due to invalid IL or missing references) //IL_003e: Unknown result type (might be due to invalid IL or missing references) //IL_003f: Unknown result type (might be due to invalid IL or missing references) //IL_0041: Unknown result type (might be due to invalid IL or missing references) //IL_0046: Unknown result type (might be due to invalid IL or missing references) //IL_004b: Unknown result type (might be due to invalid IL or missing references) //IL_0050: Unknown result type (might be due to invalid IL or missing references) //IL_0018: 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_001a: Unknown result type (might be due to invalid IL or missing references) //IL_001f: Unknown result type (might be due to invalid IL or missing references) Vector3 val = end - start; float sqrMagnitude = ((Vector3)(ref val)).sqrMagnitude; Vector3 val2; if (sqrMagnitude <= 1E-12f) { val2 = point - start; return ((Vector3)(ref val2)).sqrMagnitude; } float num = Mathf.Clamp01(Vector3.Dot(point - start, val) / sqrMagnitude); val2 = point - (start + val * num); return ((Vector3)(ref val2)).sqrMagnitude; } private static float PointBoxDistanceSquared(Vector3 point, ColliderData box) { //IL_0000: Unknown result type (might be due to invalid IL or missing references) //IL_0001: Unknown result type (might be due to invalid IL or missing references) //IL_0006: Unknown result type (might be due to invalid IL or missing references) //IL_000b: Unknown result type (might be due to invalid IL or missing references) //IL_000c: Unknown result type (might be due to invalid IL or missing references) //IL_000d: 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_0017: Unknown result type (might be due to invalid IL or missing references) //IL_001c: Unknown result type (might be due to invalid IL or missing references) //IL_001d: Unknown result type (might be due to invalid IL or missing references) //IL_0022: Unknown result type (might be due to invalid IL or missing references) return PointAabbDistanceSquared(Inverse(box.q) * (point - box.pos), Absolute(box.ext)); } private static float SegmentBoxDistanceSquared(Vector3 start, Vector3 end, ColliderData box) { //IL_0000: Unknown result type (might be due to invalid IL or missing references) //IL_0001: Unknown result type (might be due to invalid IL or missing references) //IL_0006: Unknown result type (might be due to invalid IL or missing references) //IL_000b: Unknown result type (might be due to invalid IL or missing references) //IL_000c: Unknown result type (might be due to invalid IL or missing references) //IL_000d: Unknown result type (might be due to invalid IL or missing references) //IL_000e: 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_0018: Unknown result type (might be due to invalid IL or missing references) //IL_001d: Unknown result type (might be due to invalid IL or missing references) //IL_001e: Unknown result type (might be due to invalid IL or missing references) //IL_001f: 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_0025: Unknown result type (might be due to invalid IL or missing references) //IL_002a: Unknown result type (might be due to invalid IL or missing references) //IL_002f: Unknown result type (might be due to invalid IL or missing references) //IL_0030: Unknown result type (might be due to invalid IL or missing references) //IL_0031: 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_0033: Unknown result type (might be due to invalid IL or missing references) //IL_0038: Unknown result type (might be due to invalid IL or missing references) Quaternion val = Inverse(box.q); Vector3 start2 = val * (start - box.pos); Vector3 end2 = val * (end - box.pos); return SegmentAabbDistanceSquared(start2, end2, Absolute(box.ext)); } private static float PointAabbDistanceSquared(Vector3 point, Vector3 extents) { //IL_0000: Unknown result type (might be due to invalid IL or missing references) //IL_000b: Unknown result type (might be due to invalid IL or missing references) //IL_001c: Unknown result type (might be due to invalid IL or missing references) //IL_0027: Unknown result type (might be due to invalid IL or missing references) //IL_0039: 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) float num = Mathf.Max(Mathf.Abs(point.x) - extents.x, 0f); float num2 = Mathf.Max(Mathf.Abs(point.y) - extents.y, 0f); float num3 = Mathf.Max(Mathf.Abs(point.z) - extents.z, 0f); return num * num + num2 * num2 + num3 * num3; } private static float SegmentAabbDistanceSquared(Vector3 start, Vector3 end, Vector3 extents) { //IL_0000: Unknown result type (might be due to invalid IL or missing references) //IL_0001: Unknown result type (might be due to invalid IL or missing references) //IL_0002: Unknown result type (might be due to invalid IL or missing references) //IL_0007: Unknown result type (might be due to invalid IL or missing references) //IL_0029: Unknown result type (might be due to invalid IL or missing references) //IL_002f: Unknown result type (might be due to invalid IL or missing references) //IL_0035: Unknown result type (might be due to invalid IL or missing references) //IL_0043: Unknown result type (might be due to invalid IL or missing references) //IL_0049: Unknown result type (might be due to invalid IL or missing references) //IL_004f: Unknown result type (might be due to invalid IL or missing references) //IL_005d: Unknown result type (might be due to invalid IL or missing references) //IL_0063: 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) //IL_008e: Unknown result type (might be due to invalid IL or missing references) //IL_008f: Unknown result type (might be due to invalid IL or missing references) //IL_0094: Unknown result type (might be due to invalid IL or missing references) //IL_00e8: Unknown result type (might be due to invalid IL or missing references) //IL_00ee: 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_0105: Unknown result type (might be due to invalid IL or missing references) //IL_010b: 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_0122: Unknown result type (might be due to invalid IL or missing references) //IL_0128: Unknown result type (might be due to invalid IL or missing references) //IL_012e: Unknown result type (might be due to invalid IL or missing references) //IL_0160: Unknown result type (might be due to invalid IL or missing references) //IL_0161: Unknown result type (might be due to invalid IL or missing references) //IL_0164: Unknown result type (might be due to invalid IL or missing references) Vector3 val = end - start; float[] array = new float[8]; int count = 0; array[count++] = 0f; array[count++] = 1f; AddBreakpoints(start.x, val.x, extents.x, array, ref count); AddBreakpoints(start.y, val.y, extents.y, array, ref count); AddBreakpoints(start.z, val.z, extents.z, array, ref count); Array.Sort(array, 0, count); float num = float.MaxValue; for (int i = 0; i < count; i++) { num = Mathf.Min(num, EvaluateSegmentPoint(start, val, array[i], extents)); if (i != count - 1 && !(array[i + 1] - array[i] <= 1E-07f)) { float num2 = array[i]; float num3 = array[i + 1]; float midpoint = (num2 + num3) * 0.5f; float quadratic = 0f; float linear = 0f; AccumulateDistanceTerm(start.x, val.x, extents.x, midpoint, ref quadratic, ref linear); AccumulateDistanceTerm(start.y, val.y, extents.y, midpoint, ref quadratic, ref linear); AccumulateDistanceTerm(start.z, val.z, extents.z, midpoint, ref quadratic, ref linear); if (quadratic > 1E-12f) { float amount = Mathf.Clamp((0f - linear) / (2f * quadratic), num2, num3); num = Mathf.Min(num, EvaluateSegmentPoint(start, val, amount, extents)); } } } return num; } private static void AddBreakpoints(float start, float direction, float extent, float[] breakpoints, ref int count) { if (!(Mathf.Abs(direction) <= 1E-12f)) { AddBreakpoint((0f - extent - start) / direction, breakpoints, ref count); AddBreakpoint((extent - start) / direction, breakpoints, ref count); } } private static void AddBreakpoint(float value, float[] breakpoints, ref int count) { if (value > 0f && value < 1f) { breakpoints[count++] = value; } } private static void AccumulateDistanceTerm(float start, float direction, float extent, float midpoint, ref float quadratic, ref float linear) { float num = start + direction * midpoint; float num2; if (num < 0f - extent) { num2 = 0f - extent; } else { if (!(num > extent)) { return; } num2 = extent; } quadratic += direction * direction; linear += 2f * direction * (start - num2); } private static float EvaluateSegmentPoint(Vector3 start, Vector3 direction, float amount, Vector3 extents) { //IL_0000: Unknown result type (might be due to invalid IL or missing references) //IL_0001: Unknown result type (might be due to invalid IL or missing references) //IL_0003: Unknown result type (might be due to invalid IL or missing references) //IL_0008: Unknown result type (might be due to invalid IL or missing references) //IL_000d: Unknown result type (might be due to invalid IL or missing references) return PointAabbDistanceSquared(start + direction * amount, extents); } private static float SegmentSegmentDistanceSquared(Vector3 firstStart, Vector3 firstEnd, Vector3 secondStart, Vector3 secondEnd) { //IL_0000: Unknown result type (might be due to invalid IL or missing references) //IL_0001: Unknown result type (might be due to invalid IL or missing references) //IL_0002: Unknown result type (might be due to invalid IL or missing references) //IL_0007: Unknown result type (might be due to invalid IL or missing references) //IL_0008: 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) //IL_000a: Unknown result type (might be due to invalid IL or missing references) //IL_000f: Unknown result type (might be due to invalid IL or missing references) //IL_0010: Unknown result type (might be due to invalid IL or missing references) //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_0017: Unknown result type (might be due to invalid IL or missing references) //IL_0018: 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_0020: 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_0029: Unknown result type (might be due to invalid IL or missing references) //IL_002a: 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_0033: Unknown result type (might be due to invalid IL or missing references) //IL_003b: Unknown result type (might be due to invalid IL or missing references) //IL_003c: 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) //IL_0061: Unknown result type (might be due to invalid IL or missing references) //IL_0062: Unknown result type (might be due to invalid IL or missing references) //IL_0072: Unknown result type (might be due to invalid IL or missing references) //IL_0073: Unknown result type (might be due to invalid IL or missing references) //IL_0074: Unknown result type (might be due to invalid IL or missing references) //IL_018c: Unknown result type (might be due to invalid IL or missing references) //IL_018d: 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_0195: Unknown result type (might be due to invalid IL or missing references) //IL_019a: Unknown result type (might be due to invalid IL or missing references) //IL_019d: Unknown result type (might be due to invalid IL or missing references) //IL_01a2: Unknown result type (might be due to invalid IL or missing references) //IL_01a7: Unknown result type (might be due to invalid IL or missing references) Vector3 val = firstEnd - firstStart; Vector3 val2 = secondEnd - secondStart; Vector3 val3 = firstStart - secondStart; float num = Vector3.Dot(val, val); float num2 = Vector3.Dot(val, val2); float num3 = Vector3.Dot(val2, val2); float num4 = Vector3.Dot(val, val3); float num5 = Vector3.Dot(val2, val3); float num6 = num * num3 - num2 * num2; float num7 = num6; float num8 = num6; if (num <= 1E-12f) { return PointSegmentDistanceSquared(firstStart, secondStart, secondEnd); } if (num3 <= 1E-12f) { return PointSegmentDistanceSquared(secondStart, firstStart, firstEnd); } float num9; float num10; if (num6 < 1E-12f) { num9 = 0f; num7 = 1f; num10 = num5; num8 = num3; } else { num9 = num2 * num5 - num3 * num4; num10 = num * num5 - num2 * num4; if (num9 < 0f) { num9 = 0f; num10 = num5; num8 = num3; } else if (num9 > num7) { num9 = num7; num10 = num5 + num2; num8 = num3; } } if (num10 < 0f) { num10 = 0f; if (0f - num4 < 0f) { num9 = 0f; } else if (0f - num4 > num) { num9 = num7; } else { num9 = 0f - num4; num7 = num; } } else if (num10 > num8) { num10 = num8; if (num2 - num4 < 0f) { num9 = 0f; } else if (num2 - num4 > num) { num9 = num7; } else { num9 = num2 - num4; num7 = num; } } float num11 = ((Mathf.Abs(num9) < 1E-12f) ? 0f : (num9 / num7)); float num12 = ((Mathf.Abs(num10) < 1E-12f) ? 0f : (num10 / num8)); Vector3 val4 = val3 + val * num11 - val2 * num12; return ((Vector3)(ref val4)).sqrMagnitude; } private static bool BoxesOverlap(ColliderData first, ColliderData second) { //IL_0000: Unknown result type (might be due to invalid IL or missing references) //IL_0001: Unknown result type (might be due to invalid IL or missing references) //IL_000c: Unknown result type (might be due to invalid IL or missing references) //IL_000d: Unknown result type (might be due to invalid IL or missing references) //IL_0030: Unknown result type (might be due to invalid IL or missing references) //IL_0035: Unknown result type (might be due to invalid IL or missing references) //IL_0043: Unknown result type (might be due to invalid IL or missing references) //IL_0048: Unknown result type (might be due to invalid IL or missing references) //IL_0098: 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_00a3: Unknown result type (might be due to invalid IL or missing references) //IL_00a5: Unknown result type (might be due to invalid IL or missing references) //IL_00a6: Unknown result type (might be due to invalid IL or missing references) //IL_00ab: 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_00b2: Unknown result type (might be due to invalid IL or missing references) //IL_00b3: Unknown result type (might be due to invalid IL or missing references) //IL_00b8: Unknown result type (might be due to invalid IL or missing references) //IL_00b9: Unknown result type (might be due to invalid IL or missing references) //IL_00be: Unknown result type (might be due to invalid IL or missing references) //IL_00c3: 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) //IL_0073: Unknown result type (might be due to invalid IL or missing references) //IL_0078: Unknown result type (might be due to invalid IL or missing references) //IL_007d: Unknown result type (might be due to invalid IL or missing references) //IL_00cd: Unknown result type (might be due to invalid IL or missing references) //IL_00d2: Unknown result type (might be due to invalid IL or missing references) //IL_00e6: Unknown result type (might be due to invalid IL or missing references) //IL_00ef: 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_00f7: Unknown result type (might be due to invalid IL or missing references) //IL_00f9: Unknown result type (might be due to invalid IL or missing references) //IL_0103: Unknown result type (might be due to invalid IL or missing references) //IL_0105: Unknown result type (might be due to invalid IL or missing references) //IL_010e: Unknown result type (might be due to invalid IL or missing references) //IL_0110: Unknown result type (might be due to invalid IL or missing references) Vector3[] boxAxes = GetBoxAxes(first.q); Vector3[] boxAxes2 = GetBoxAxes(second.q); Vector3[] array = (Vector3[])(object)new Vector3[15]; int num = 0; for (int i = 0; i < 3; i++) { array[num++] = boxAxes[i]; array[num++] = boxAxes2[i]; } for (int j = 0; j < 3; j++) { for (int k = 0; k < 3; k++) { array[num++] = Vector3.Cross(boxAxes[j], boxAxes2[k]); } } Vector3 extents = Absolute(first.ext); Vector3 extents2 = Absolute(second.ext); Vector3 val = second.pos - first.pos; for (int l = 0; l < num; l++) { Vector3 val2 = array[l]; float sqrMagnitude = ((Vector3)(ref val2)).sqrMagnitude; if (!(sqrMagnitude <= 1E-10f)) { val2 /= Mathf.Sqrt(sqrMagnitude); float num2 = ProjectBox(boxAxes, extents, val2); float num3 = ProjectBox(boxAxes2, extents2, val2); if (Mathf.Abs(Vector3.Dot(val, val2)) >= num2 + num3 - 0.001f) { return false; } } } return true; } private static Vector3[] GetBoxAxes(Quaternion rotation) { //IL_0008: 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) //IL_000e: 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_0016: Unknown result type (might be due to invalid IL or missing references) //IL_001b: Unknown result type (might be due to invalid IL or missing references) //IL_0022: Unknown result type (might be due to invalid IL or missing references) //IL_0023: 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_002d: Unknown result type (might be due to invalid IL or missing references) //IL_0030: Unknown result type (might be due to invalid IL or missing references) //IL_0035: Unknown result type (might be due to invalid IL or missing references) //IL_003c: Unknown result type (might be due to invalid IL or missing references) //IL_003d: Unknown result type (might be due to invalid IL or missing references) //IL_0042: 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_004a: Unknown result type (might be due to invalid IL or missing references) //IL_004f: Unknown result type (might be due to invalid IL or missing references) Vector3[] array = new Vector3[3]; Vector3 val = rotation * Vector3.right; array[0] = ((Vector3)(ref val)).normalized; val = rotation * Vector3.up; array[1] = ((Vector3)(ref val)).normalized; val = rotation * Vector3.forward; array[2] = ((Vector3)(ref val)).normalized; return (Vector3[])(object)array; } private static float ProjectBox(Vector3[] axes, Vector3 extents, Vector3 testAxis) { //IL_0000: Unknown result type (might be due to invalid IL or missing references) //IL_0008: Unknown result type (might be due to invalid IL or missing references) //IL_000d: 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_0026: 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) //IL_003b: Unknown result type (might be due to invalid IL or missing references) //IL_0040: Unknown result type (might be due to invalid IL or missing references) return extents.x * Mathf.Abs(Vector3.Dot(axes[0], testAxis)) + extents.y * Mathf.Abs(Vector3.Dot(axes[1], testAxis)) + extents.z * Mathf.Abs(Vector3.Dot(axes[2], testAxis)); } private static Vector3 Absolute(Vector3 value) { //IL_0000: Unknown result type (might be due to invalid IL or missing references) //IL_000b: Unknown result type (might be due to invalid IL or missing references) //IL_0016: 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) return new Vector3(Mathf.Abs(value.x), Mathf.Abs(value.y), Mathf.Abs(value.z)); } private static Quaternion Inverse(Quaternion rotation) { //IL_0000: Unknown result type (might be due to invalid IL or missing references) //IL_0006: Unknown result type (might be due to invalid IL or missing references) //IL_000d: 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_001b: 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_0029: Unknown result type (might be due to invalid IL or missing references) //IL_002f: 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_0057: 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) //IL_0069: Unknown result type (might be due to invalid IL or missing references) //IL_0071: Unknown result type (might be due to invalid IL or missing references) //IL_0040: Unknown result type (might be due to invalid IL or missing references) float num = rotation.x * rotation.x + rotation.y * rotation.y + rotation.z * rotation.z + rotation.w * rotation.w; if (num <= 1E-12f) { return Quaternion.identity; } float num2 = 1f / num; return new Quaternion((0f - rotation.x) * num2, (0f - rotation.y) * num2, (0f - rotation.z) * num2, rotation.w * num2); } } [BepInPlugin("Zincon.DSPPreserveVegetation", "Vegetation Protection", "1.1.1")] public sealed class Plugin : BaseUnityPlugin { private static class PlayerCollisionPatch { internal static bool SkipVegetationCollisionDamage() { return false; } } public const string PluginGuid = "Zincon.DSPPreserveVegetation"; public const string PluginName = "Vegetation Protection"; public const string PluginVersion = "1.1.1"; private Harmony harmony; private void Awake() { //IL_0023: Unknown result type (might be due to invalid IL or missing references) //IL_002d: Expected O, but got Unknown //IL_0035: Unknown result type (might be due to invalid IL or missing references) //IL_0043: Expected O, but got Unknown try { MethodInfo methodInfo = FindCollisionHandler(); MethodInfo methodInfo2 = AccessTools.Method(typeof(PlayerCollisionPatch), "SkipVegetationCollisionDamage", (Type[])null, (Type[])null); harmony = new Harmony("Zincon.DSPPreserveVegetation"); harmony.Patch((MethodBase)methodInfo, new HarmonyMethod(methodInfo2), (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null); BuildingVegetationPatch.Apply(harmony); ((BaseUnityPlugin)this).Logger.LogInfo((object)"Vegetation Protection 1.1.1 loaded. Icarus collision damage is disabled and building vegetation clearing uses physical colliders."); } catch (Exception arg) { Harmony obj = harmony; if (obj != null) { obj.UnpatchSelf(); } harmony = null; ((BaseUnityPlugin)this).Logger.LogError((object)$"Failed to apply the vegetation preservation patches: {arg}"); } } private void OnDestroy() { Harmony obj = harmony; if (obj != null) { obj.UnpatchSelf(); } } private static MethodInfo FindCollisionHandler() { Type type = AccessTools.TypeByName("UnityEngine.Collision"); if (type == null) { throw new TypeLoadException("UnityEngine.Collision was not found."); } MethodInfo methodInfo = AccessTools.Method(typeof(PlayerController), "HandleCollision", new Type[2] { type, typeof(bool) }, (Type[])null); if (methodInfo == null || methodInfo.ReturnType != typeof(void) || !methodInfo.IsPrivate) { throw new MissingMethodException("PlayerController.HandleCollision(UnityEngine.Collision, bool) has changed."); } return methodInfo; } }