From a23eac7665cfe67f2c5eeb9a9686bec9410dd216 Mon Sep 17 00:00:00 2001 From: Dunbaratu Date: Tue, 29 Jan 2019 15:44:07 -0600 Subject: [PATCH] Reflection so it compiles when grav accel not exist. --- src/kOS/Module/kOSProcessor.cs | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/src/kOS/Module/kOSProcessor.cs b/src/kOS/Module/kOSProcessor.cs index 814db1dfe..8c4498a84 100644 --- a/src/kOS/Module/kOSProcessor.cs +++ b/src/kOS/Module/kOSProcessor.cs @@ -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; @@ -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: