Skip to content

Commit

Permalink
Add various protections against the construction functionality introd…
Browse files Browse the repository at this point in the history
…uced by KSP 1.11
  • Loading branch information
KSP-TaxiService committed Jan 2, 2021
1 parent ab52796 commit a20ad38
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 24 deletions.
11 changes: 7 additions & 4 deletions src/RemoteTech/FlightComputer/FlightComputer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -164,11 +164,14 @@ public FlightComputer(ISignalProcessor s)

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

Expand Down
2 changes: 1 addition & 1 deletion src/RemoteTech/Modules/ModuleRTAntenna.cs
Original file line number Diff line number Diff line change
Expand Up @@ -784,7 +784,7 @@ private void OnPartUndock(Part p)

private void OnVesselModified(Vessel v)
{
if (RTCore.Instance != null && mRegisteredId != vessel.id)
if (RTCore.Instance != null && vessel != null && mRegisteredId != vessel.id)
{
RTCore.Instance.Antennas.Unregister(mRegisteredId, this);
mRegisteredId = vessel.id;
Expand Down
16 changes: 8 additions & 8 deletions src/RemoteTech/Modules/ModuleSPU.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,18 @@ public class ModuleSPU : PartModule, ISignalProcessor

public string VesselName
{
get { return vessel.vesselName; }
set { vessel.vesselName = value; }
get { return vessel != null ? vessel.vesselName : "vessel-null"; }
set { if(vessel != null) vessel.vesselName = value; }
}
public bool VesselLoaded => vessel.loaded;
public bool VesselLoaded => vessel != null && vessel.loaded;
public Guid VesselId { get; private set; }

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

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

public void OnVesselModified(Vessel v)
{
if (RTCore.Instance == null || VesselId == vessel.id)
if (RTCore.Instance == null || vessel == null || VesselId == vessel.id)
return;

RTCore.Instance.Satellites.Unregister(VesselId, this);
Expand Down
16 changes: 7 additions & 9 deletions src/RemoteTech/Modules/ModuleSPUPassive.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using System;
using System.Linq;
using System.Text;
using UnityEngine;

namespace RemoteTech.Modules
Expand All @@ -14,15 +12,15 @@ public class ModuleSPUPassive : PartModule, ISignalProcessor
public string Name => $"ModuleSPUPassive({VesselName})";
public string VesselName
{
get { return vessel.vesselName; }
set { vessel.vesselName = value; }
get { return vessel != null ? vessel.vesselName : "vessel-null"; }
set { if(vessel != null) vessel.vesselName = value; }
}
public bool VesselLoaded => vessel.loaded;
public bool VesselLoaded => vessel != null && vessel.loaded;
public Guid VesselId { get; private set; }
public Vector3 Position => vessel.GetWorldPos3D();
public CelestialBody Body => vessel.mainBody;
public Vector3 Position { get { return vessel != null ? vessel.GetWorldPos3D() : new Vector3d(); } }
public CelestialBody Body { get { return vessel != null ? vessel.mainBody : null; } }
public bool Visible => MapViewFiltering.CheckAgainstFilter(vessel);
public bool Powered => Vessel.IsControllable;
public bool Powered => vessel != null && vessel.IsControllable;
public bool IsCommandStation => false;
public FlightComputer.FlightComputer FlightComputer => null;
public Vessel Vessel => vessel;
Expand Down Expand Up @@ -74,7 +72,7 @@ public void OnPartUndock(Part p)

public void OnVesselModified(Vessel v)
{
if (RTCore.Instance != null && VesselId != vessel.id)
if (RTCore.Instance != null && vessel != null && VesselId != vessel.id)
{
RTCore.Instance.Satellites.Unregister(VesselId, this);
VesselId = vessel.id;
Expand Down
12 changes: 10 additions & 2 deletions src/RemoteTech/UI/FilterOverlay.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,11 @@ private Rect Position
{
// New side bar location checking... if someone finds a better method for this please fix
if (mImg == null)
mImg = GameObject.Find("Side Bar").GetChild("bg (stretch)").GetComponent<UnityEngine.UI.Image>();
{
var obj = GameObject.Find("Side Bar");
if (obj != null)
mImg = obj.GetChild("bg (stretch)").GetComponent<UnityEngine.UI.Image>();
}

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

// Same new side bar checking... if someone finds a better method for this please fix
if (mImg == null)
mImg = GameObject.Find("Side Bar").GetChild("bg (stretch)").GetComponent<UnityEngine.UI.Image>();
{
var obj = GameObject.Find("Side Bar");
if(obj != null)
mImg = obj.GetChild("bg (stretch)").GetComponent<UnityEngine.UI.Image>();
}
posX = mImg.rectTransform.rect.width * GameSettings.UI_SCALE;
}

Expand Down
2 changes: 2 additions & 0 deletions src/RemoteTech/VesselExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ public static class VesselExtension
/// <returns>true if the vessel has a local control, false otherwise.</returns>
public static bool HasLocalControl(this Vessel vessel)
{
if (vessel == null) return false;

// vessel must be a control source and it must be crewed or not implementing a module processor
var hasLocalControl = vessel.parts.Any(p => (p.isControlSource > Vessel.ControlLevel.NONE) &&
(p.protoModuleCrew.Any() || !p.FindModulesImplementing<ISignalProcessor>().Any() ||
Expand Down

0 comments on commit a20ad38

Please sign in to comment.