Skip to content

Commit

Permalink
Reflection so it compiles when grav accel not exist.
Browse files Browse the repository at this point in the history
  • Loading branch information
Dunbaratu committed Jan 29, 2019
1 parent c86bf63 commit a23eac7
Showing 1 changed file with 21 additions and 1 deletion.
22 changes: 21 additions & 1 deletion src/kOS/Module/kOSProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using kOS.Safe.Execution;
using UnityEngine;
using kOS.Safe.Encapsulation;
Expand Down Expand Up @@ -510,7 +511,26 @@ public void InitObjects()
// the KSP API. It's in kOS.Safe.
private void CalcConstsFromKSP()
{
ConstantValue.G0 = PhysicsGlobals.GravitationalAcceleration;
// GravitationalAcceleration did not exist in PhysicsGlobals prior to KSP 1.6.x.
// This code has to use reflection to avoid calling it on older backports:
Type physGlobType = typeof(PhysicsGlobals);
if (physGlobType != null)
{
// KSP often changes its mind whether a member is a Field or Property, so let's write this
// to future-proof against them changing which it is by trying both ways:
FieldInfo asField = (physGlobType.GetField("GravitationalAcceleration", BindingFlags.Public | BindingFlags.Static));
if (asField != null)
ConstantValue.G0 = (double) asField.GetValue(null);
else
{
PropertyInfo asProperty = (physGlobType.GetProperty("GravitationalAcceleration", BindingFlags.Public | BindingFlags.Static));
if (asProperty != null)
ConstantValue.G0 = (double)asProperty.GetValue(null, null);
}
}
// Fallback: Note if none of the above work, G0 still does have a reasonable value because we
// hardcode it to a literal in ConstantValue before doing any of the above work.


// Cannot find anything in KSP's API exposing their value of G, so this indirect means
// of calculating it from an arbitrary body is used:
Expand Down

0 comments on commit a23eac7

Please sign in to comment.