From 500250614910496947a76e7ff9596988536c7354 Mon Sep 17 00:00:00 2001 From: Paul Hebble Date: Sun, 23 Jun 2024 18:47:35 -0500 Subject: [PATCH] Ignore NREs from FlightGlobals.ActiveVessel (fixes #33) Happens during compilation of internal spaces according to @JonnyOThan --- GameData/Astrogator/Astrogator-Changelog.cfg | 5 +++++ Source/AstrogatorMenu.cs | 4 ++-- Source/KerbalTools.cs | 19 +++++++++++++++++++ 3 files changed, 26 insertions(+), 2 deletions(-) diff --git a/GameData/Astrogator/Astrogator-Changelog.cfg b/GameData/Astrogator/Astrogator-Changelog.cfg index 8fbafb3..2f725ad 100644 --- a/GameData/Astrogator/Astrogator-Changelog.cfg +++ b/GameData/Astrogator/Astrogator-Changelog.cfg @@ -10,6 +10,11 @@ KERBALCHANGELOG version = 1.0.1 versionName = Vanish in black smoke versionKSP = 1.12 + CHANGE + { + change = Ignore NREs from FlightGlobals.ActiveVessel + type = Fix + } } VERSION { diff --git a/Source/AstrogatorMenu.cs b/Source/AstrogatorMenu.cs index b68bf5f..1bca6b5 100644 --- a/Source/AstrogatorMenu.cs +++ b/Source/AstrogatorMenu.cs @@ -20,8 +20,8 @@ public class AstrogatorMenu : InternalModule { : base() { model = new AstrogationModel( - (ITargetable)FlightGlobals.ActiveVessel - ?? (ITargetable)FlightGlobals.getMainBody()); + DefaultIfThrows(() => FlightGlobals.ActiveVessel) + ?? FlightGlobals.getMainBody()); loader = new AstrogationLoadBehaviorette(model, null); timeToWait = new List(); cursorTransfer = 0; diff --git a/Source/KerbalTools.cs b/Source/KerbalTools.cs index 90114b5..c3edb66 100644 --- a/Source/KerbalTools.cs +++ b/Source/KerbalTools.cs @@ -316,5 +316,24 @@ public static string FilePath(string filename, bool GameDataRelative = true) } } + /// Discard annoying exceptions + /// The function to evaluate + /// + /// The return value of func + /// or the default of its return type (typically null) + /// if it throws an exception + /// + public static T DefaultIfThrows(Func func) + { + try + { + return func(); + } + catch + { + return default; + } + } + } }