Skip to content

Commit

Permalink
Merge pull request #2527 from pleroy/LongFlightPlan
Browse files Browse the repository at this point in the history
Avoid exceptions on very long flight plans
  • Loading branch information
pleroy authored Apr 14, 2020
2 parents 380dfa1 + bc5d7e1 commit 9067d8c
Showing 1 changed file with 33 additions and 9 deletions.
42 changes: 33 additions & 9 deletions ksp_plugin_adapter/flight_planner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,7 @@ private void RenderUpcomingEvents() {
UnityEngine.GUILayout.Label("Upcoming manœuvre #" +
(first_future_manœuvre + 1) + ":");
UnityEngine.GUILayout.Label(
"Ignition " + FormatTimeSpan(TimeSpan.FromSeconds(
"Ignition " + FormatTimeSpan(TimeSpanFromSeconds(
current_time - manœuvre.burn.initial_time)),
style : Style.RightAligned(UnityEngine.GUI.skin.label));
}
Expand All @@ -338,7 +338,7 @@ private void RenderUpcomingEvents() {
UnityEngine.GUILayout.Label("Ongoing manœuvre #" +
(first_future_manœuvre + 1) + ":");
UnityEngine.GUILayout.Label(
"Cutoff " + FormatTimeSpan(TimeSpan.FromSeconds(
"Cutoff " + FormatTimeSpan(TimeSpanFromSeconds(
current_time - manœuvre.final_time)),
style : Style.RightAligned(UnityEngine.GUI.skin.label));
}
Expand Down Expand Up @@ -366,6 +366,22 @@ private void RenderUpcomingEvents() {
}
}

// A conversion function that swallows the overflow/NaN exceptions and
// saturates. Better than killing the UI.
internal static TimeSpan TimeSpanFromSeconds(double seconds) {
try {
return TimeSpan.FromSeconds(seconds);
} catch (OverflowException) {
if (seconds >= 0.0) {
return TimeSpan.MaxValue;
} else {
return TimeSpan.MinValue;
}
} catch (ArgumentException) {
return TimeSpan.Zero;
}
}

internal static string FormatPositiveTimeSpan(TimeSpan span) {
return (GameSettings.KERBIN_TIME
? (span.Days * 4 + span.Hours / 6).ToString("0000;0000") +
Expand Down Expand Up @@ -406,15 +422,23 @@ internal static bool TryParseTimeSpan(string str, out TimeSpan value) {
out double s)) {
return false;
}
value = TimeSpan.FromDays((double)d / (GameSettings.KERBIN_TIME ? 4 : 1)) +
TimeSpan.FromHours(h) +
TimeSpan.FromMinutes(min) +
TimeSpan.FromSeconds(s);
return true;
// Fail to parse if the user gives us overflowing input.
try {
value =
TimeSpan.FromDays((double)d / (GameSettings.KERBIN_TIME ? 4 : 1)) +
TimeSpan.FromHours(h) +
TimeSpan.FromMinutes(min) +
TimeSpan.FromSeconds(s);
return true;
} catch (OverflowException) {
return false;
} catch (ArgumentException) {
return false;
}
}

internal string FormatPlanLength(double value) {
return FormatPositiveTimeSpan(TimeSpan.FromSeconds(
return FormatPositiveTimeSpan(TimeSpanFromSeconds(
value -
plugin.FlightPlanGetInitialTime(vessel_.id.ToString())));
}
Expand Down Expand Up @@ -460,7 +484,7 @@ private string GetStatusMessage() {
string status_message = "computation failed"; // Preceded by "The".
string time_out_message =
timed_out ? " after " +
FormatPositiveTimeSpan(TimeSpan.FromSeconds(
FormatPositiveTimeSpan(TimeSpanFromSeconds(
actual_final_time -
plugin.FlightPlanGetInitialTime(vessel_guid)))
: "";
Expand Down

0 comments on commit 9067d8c

Please sign in to comment.