Skip to content

Commit f90accd

Browse files
committed
Preemptive fix for issue #282. Can't reproduce the nullrefs on my end, but added back a few null checks to handle things more gracefully.
1 parent 794335c commit f90accd

File tree

1 file changed

+13
-13
lines changed

1 file changed

+13
-13
lines changed

KSPCommunityFixes/Performance/CollisionEnhancerFastUpdate.cs

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
1-
using System;
2-
using HarmonyLib;
3-
using KSP.Localization;
4-
using System.Collections.Generic;
1+
using KSP.Localization;
2+
using System;
53
using UnityEngine;
64

75
namespace KSPCommunityFixes.Performance
@@ -12,32 +10,36 @@ internal class CollisionEnhancerFastUpdate : BasePatch
1210

1311
protected override void ApplyPatches()
1412
{
15-
AddPatch(PatchType.Prefix, typeof(CollisionEnhancer), nameof(CollisionEnhancer.FixedUpdate));
13+
AddPatch(PatchType.Override, typeof(CollisionEnhancer), nameof(CollisionEnhancer.FixedUpdate));
1614
}
1715

18-
private static bool CollisionEnhancer_FixedUpdate_Prefix(CollisionEnhancer __instance)
16+
private static void CollisionEnhancer_FixedUpdate_Override(CollisionEnhancer __instance)
1917
{
2018
Part part = __instance.part;
19+
20+
if (part.IsNullOrDestroyed() || part.partTransform.IsNullOrDestroyed())
21+
return;
22+
2123
Vector3 position = part.partTransform.position;
2224

2325
if (part.packed)
2426
{
2527
__instance.lastPos = position;
2628
__instance.wasPacked = true;
27-
return false;
29+
return;
2830
}
2931

3032
if (__instance.framesToSkip > 0)
3133
{
3234
__instance.lastPos = position;
3335
__instance.framesToSkip--;
34-
return false;
36+
return;
3537
}
3638

37-
if (part.vessel.heightFromTerrain > 1000f)
39+
if (part.vessel.IsNullOrDestroyed() || part.vessel.heightFromTerrain > 1000f)
3840
{
3941
__instance.lastPos = position;
40-
return false;
42+
return;
4143
}
4244

4345
if (!__instance.wasPacked)
@@ -54,7 +56,7 @@ private static bool CollisionEnhancer_FixedUpdate_Prefix(CollisionEnhancer __ins
5456
&& Physics.Linecast(__instance.lastPos, position, out RaycastHit hit, 32768, QueryTriggerInteraction.Ignore)) // linecast against the "LocalScenery" layer
5557
{
5658
Vector3 rbVelocity = __instance.rb.velocity;
57-
Debug.Log("[F: " + Time.frameCount + "]: [" + __instance.name + "] Collision Enhancer Punch Through - vel: " + rbVelocity.magnitude, __instance.gameObject);
59+
Debug.Log($"[F: {Time.frameCount}]: [{__instance.name}] Collision Enhancer Punch Through - vel: {rbVelocity.magnitude}");
5860

5961
if (mode == CollisionEnhancerBehaviour.EXPLODE
6062
&& !CheatOptions.NoCrashDamage
@@ -89,8 +91,6 @@ private static bool CollisionEnhancer_FixedUpdate_Prefix(CollisionEnhancer __ins
8991
}
9092

9193
__instance.lastPos = position;
92-
93-
return false;
9494
}
9595
}
9696
}

0 commit comments

Comments
 (0)