Skip to content

Commit

Permalink
Bypass incautious accessors (really fixes #33)
Browse files Browse the repository at this point in the history
  • Loading branch information
HebaruSan committed Jun 24, 2024
1 parent b2babe7 commit 724ae7b
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 22 deletions.
5 changes: 5 additions & 0 deletions GameData/Astrogator/Astrogator-Changelog.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ KERBALCHANGELOG
version = 1.0.2
versionName = As if it never happened
versionKSP = 1.12
CHANGE
{
change = Better fixes for FlightGlobals NREs
type = Fix
}
}

VERSION
Expand Down
4 changes: 1 addition & 3 deletions Source/AstrogatorMenu.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,7 @@ public class AstrogatorMenu : InternalModule {
AstrogatorMenu()
: base()
{
model = new AstrogationModel(
DefaultIfThrows<ITargetable>(() => FlightGlobals.ActiveVessel)
?? FlightGlobals.getMainBody());
model = new AstrogationModel(GetBestOrigin());
loader = new AstrogationLoadBehaviorette(model, null);
timeToWait = new List<DateTimeParts>();
cursorTransfer = 0;
Expand Down
27 changes: 27 additions & 0 deletions Source/EnumerableExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using System;
using System.Linq;
using System.Collections.Generic;

namespace Astrogator {

/// <summary>
/// Going to extra effort to be lazy
/// </summary>
public static class EnumerableExtensions
{

/// <summary>
/// Return the first element in a sequence matching a predicate,
/// or the the first element at all if none match.
/// </summary>
/// <typeparam name="T">The type of elements in the sequence</typeparam>
/// <param name="source">The sequence to scan, may be checked twice</param>
/// <param name="predicate">The predicate to check per element</param>
/// <returns></returns>
public static T FirstOrDefaultOrFirst<T>(this IEnumerable<T> source, Func<T, bool> predicate)
where T : class
=> source.FirstOrDefault(predicate) ?? source.FirstOrDefault();

}

}
27 changes: 8 additions & 19 deletions Source/KerbalTools.cs
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,14 @@ public static CelestialBody ParentBody(ITargetable target)
}
}

/// <summary>
/// Get the active vessel, or the home world, or just the first world in the list, or null.
/// Works around FlightGlobals' broken singleton implementation.
/// </summary>
public static ITargetable GetBestOrigin()
=> (ITargetable)FlightGlobals.fetch?.activeVessel
?? FlightGlobals.fetch?.bodies?.FirstOrDefaultOrFirst(b => b?.isHomeWorld ?? false);

/// <summary>
/// Find the orbit that contains the given orbit.
/// Wrapper around Orbit.referenceBody that returns
Expand Down Expand Up @@ -316,24 +324,5 @@ public static string FilePath(string filename, bool GameDataRelative = true)
}
}

/// <summary>Discard annoying exceptions</summary>
/// <param name="func">The function to evaluate</param>
/// <returns>
/// The return value of func
/// or the default of its return type (typically null)
/// if it throws an exception
/// </returns>
public static T DefaultIfThrows<T>(Func<T> func)
{
try
{
return func();
}
catch
{
return default;
}
}

}
}

0 comments on commit 724ae7b

Please sign in to comment.