Skip to content

Commit a20ad38

Browse files
Add various protections against the construction functionality introduced by KSP 1.11
1 parent ab52796 commit a20ad38

File tree

6 files changed

+35
-24
lines changed

6 files changed

+35
-24
lines changed

src/RemoteTech/FlightComputer/FlightComputer.cs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -164,11 +164,14 @@ public FlightComputer(ISignalProcessor s)
164164

165165
// Add RT listeners from KSP Autopilot
166166
StockAutopilotCommand.UIreference = GameObject.FindObjectOfType<VesselAutopilotUI>();
167-
for (var index = 0; index < StockAutopilotCommand.UIreference.modeButtons.Length; index++)
167+
if (StockAutopilotCommand.UIreference != null)
168168
{
169-
var buttonIndex = index; // prevent compiler optimisation from assigning static final index value
170-
StockAutopilotCommand.UIreference.modeButtons[index].onClick.AddListener(delegate { StockAutopilotCommand.AutopilotButtonClick(buttonIndex, this); });
171-
// bad idea to use RemoveAllListeners() since no easy way to re-add the original stock listener to onClick
169+
for (var index = 0; index < StockAutopilotCommand.UIreference.modeButtons.Length; index++)
170+
{
171+
var buttonIndex = index; // prevent compiler optimisation from assigning static final index value
172+
StockAutopilotCommand.UIreference.modeButtons[index].onClick.AddListener(delegate { StockAutopilotCommand.AutopilotButtonClick(buttonIndex, this); });
173+
// bad idea to use RemoveAllListeners() since no easy way to re-add the original stock listener to onClick
174+
}
172175
}
173176
}
174177

src/RemoteTech/Modules/ModuleRTAntenna.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -784,7 +784,7 @@ private void OnPartUndock(Part p)
784784

785785
private void OnVesselModified(Vessel v)
786786
{
787-
if (RTCore.Instance != null && mRegisteredId != vessel.id)
787+
if (RTCore.Instance != null && vessel != null && mRegisteredId != vessel.id)
788788
{
789789
RTCore.Instance.Antennas.Unregister(mRegisteredId, this);
790790
mRegisteredId = vessel.id;

src/RemoteTech/Modules/ModuleSPU.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,18 +23,18 @@ public class ModuleSPU : PartModule, ISignalProcessor
2323

2424
public string VesselName
2525
{
26-
get { return vessel.vesselName; }
27-
set { vessel.vesselName = value; }
26+
get { return vessel != null ? vessel.vesselName : "vessel-null"; }
27+
set { if(vessel != null) vessel.vesselName = value; }
2828
}
29-
public bool VesselLoaded => vessel.loaded;
29+
public bool VesselLoaded => vessel != null && vessel.loaded;
3030
public Guid VesselId { get; private set; }
3131

32-
public Vector3 Position => vessel.GetWorldPos3D();
33-
public CelestialBody Body => vessel.mainBody;
32+
public Vector3 Position { get { return vessel != null ? vessel.GetWorldPos3D() : new Vector3d(); } }
33+
public CelestialBody Body { get { return vessel != null ? vessel.mainBody : null; } }
3434
public bool Visible => MapViewFiltering.CheckAgainstFilter(vessel);
35-
public bool Powered => IsRTPowered;
35+
public bool Powered => vessel != null && IsRTPowered;
3636

37-
public bool IsCommandStation => IsRTPowered && IsRTCommandStation && vessel.GetVesselCrew().Count >= RTCommandMinCrew;
37+
public bool IsCommandStation => IsRTPowered && IsRTCommandStation && vessel != null && vessel.GetVesselCrew().Count >= RTCommandMinCrew;
3838
public FlightComputer.FlightComputer FlightComputer { get; private set; }
3939
public Vessel Vessel => vessel;
4040
public bool IsMaster => Satellite != null && ReferenceEquals(Satellite.SignalProcessor, this);
@@ -279,7 +279,7 @@ public void OnDestroy()
279279

280280
public void OnVesselModified(Vessel v)
281281
{
282-
if (RTCore.Instance == null || VesselId == vessel.id)
282+
if (RTCore.Instance == null || vessel == null || VesselId == vessel.id)
283283
return;
284284

285285
RTCore.Instance.Satellites.Unregister(VesselId, this);

src/RemoteTech/Modules/ModuleSPUPassive.cs

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
using System;
2-
using System.Linq;
3-
using System.Text;
42
using UnityEngine;
53

64
namespace RemoteTech.Modules
@@ -14,15 +12,15 @@ public class ModuleSPUPassive : PartModule, ISignalProcessor
1412
public string Name => $"ModuleSPUPassive({VesselName})";
1513
public string VesselName
1614
{
17-
get { return vessel.vesselName; }
18-
set { vessel.vesselName = value; }
15+
get { return vessel != null ? vessel.vesselName : "vessel-null"; }
16+
set { if(vessel != null) vessel.vesselName = value; }
1917
}
20-
public bool VesselLoaded => vessel.loaded;
18+
public bool VesselLoaded => vessel != null && vessel.loaded;
2119
public Guid VesselId { get; private set; }
22-
public Vector3 Position => vessel.GetWorldPos3D();
23-
public CelestialBody Body => vessel.mainBody;
20+
public Vector3 Position { get { return vessel != null ? vessel.GetWorldPos3D() : new Vector3d(); } }
21+
public CelestialBody Body { get { return vessel != null ? vessel.mainBody : null; } }
2422
public bool Visible => MapViewFiltering.CheckAgainstFilter(vessel);
25-
public bool Powered => Vessel.IsControllable;
23+
public bool Powered => vessel != null && vessel.IsControllable;
2624
public bool IsCommandStation => false;
2725
public FlightComputer.FlightComputer FlightComputer => null;
2826
public Vessel Vessel => vessel;
@@ -74,7 +72,7 @@ public void OnPartUndock(Part p)
7472

7573
public void OnVesselModified(Vessel v)
7674
{
77-
if (RTCore.Instance != null && VesselId != vessel.id)
75+
if (RTCore.Instance != null && vessel != null && VesselId != vessel.id)
7876
{
7977
RTCore.Instance.Satellites.Unregister(VesselId, this);
8078
VesselId = vessel.id;

src/RemoteTech/UI/FilterOverlay.cs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,11 @@ private Rect Position
7272
{
7373
// New side bar location checking... if someone finds a better method for this please fix
7474
if (mImg == null)
75-
mImg = GameObject.Find("Side Bar").GetChild("bg (stretch)").GetComponent<UnityEngine.UI.Image>();
75+
{
76+
var obj = GameObject.Find("Side Bar");
77+
if (obj != null)
78+
mImg = obj.GetChild("bg (stretch)").GetComponent<UnityEngine.UI.Image>();
79+
}
7680

7781
posX = mImg.rectTransform.rect.width * GameSettings.UI_SCALE;
7882
}
@@ -95,7 +99,11 @@ private Rect PositionSatellite
9599

96100
// Same new side bar checking... if someone finds a better method for this please fix
97101
if (mImg == null)
98-
mImg = GameObject.Find("Side Bar").GetChild("bg (stretch)").GetComponent<UnityEngine.UI.Image>();
102+
{
103+
var obj = GameObject.Find("Side Bar");
104+
if(obj != null)
105+
mImg = obj.GetChild("bg (stretch)").GetComponent<UnityEngine.UI.Image>();
106+
}
99107
posX = mImg.rectTransform.rect.width * GameSettings.UI_SCALE;
100108
}
101109

src/RemoteTech/VesselExtension.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ public static class VesselExtension
1111
/// <returns>true if the vessel has a local control, false otherwise.</returns>
1212
public static bool HasLocalControl(this Vessel vessel)
1313
{
14+
if (vessel == null) return false;
15+
1416
// vessel must be a control source and it must be crewed or not implementing a module processor
1517
var hasLocalControl = vessel.parts.Any(p => (p.isControlSource > Vessel.ControlLevel.NONE) &&
1618
(p.protoModuleCrew.Any() || !p.FindModulesImplementing<ISignalProcessor>().Any() ||

0 commit comments

Comments
 (0)