diff --git a/MechJeb2/PartExtensions.cs b/MechJeb2/PartExtensions.cs index e5fad171..e6431510 100644 --- a/MechJeb2/PartExtensions.cs +++ b/MechJeb2/PartExtensions.cs @@ -49,6 +49,8 @@ public static bool UnstableUllage(this Part p) try { + if (!VesselState.RFModuleEnginesRFType.IsInstanceOfType(eng)) + return false; if (VesselState.RFignitedField.GetValue(eng) is bool ignited && ignited) return false; if (VesselState.RFignitionsField.GetValue(eng) is int ignitions && ignitions == 0) diff --git a/MechJeb2/VesselState.cs b/MechJeb2/VesselState.cs index 0d6f94db..0f70b0c3 100644 --- a/MechJeb2/VesselState.cs +++ b/MechJeb2/VesselState.cs @@ -16,6 +16,9 @@ public class VesselState public static bool isLoadedRealFuels; + // RealFuels.ModuleEngineRF class + public static Type RFModuleEnginesRFType; + // RealFuels.ModuleEngineRF ullageSet field to call via reflection public static FieldInfo RFullageSetField; @@ -365,6 +368,13 @@ public void InitReflection() if (isLoadedRealFuels) { Debug.Log("MechJeb: RealFuels Assembly is loaded"); + RFModuleEnginesRFType = ReflectionUtils.GetClassByReflection("RealFuels", "RealFuels.ModuleEnginesRF"); + if (RFModuleEnginesRFType == null) + { + Debug.LogWarning("MechJeb BUG: RealFuels loaded, but RealFuels ModuleEnginesRF was not found, disabling RF"); + isLoadedRealFuels = false; + } + RFullageSetField = ReflectionUtils.GetFieldByReflection("RealFuels", "RealFuels.ModuleEnginesRF", "ullageSet"); if (RFullageSetField == null) { @@ -1429,6 +1439,11 @@ public void CheckUllageStatus(ModuleEngines e) return; } + if (!RFModuleEnginesRFType.IsInstanceOfType(e)) + { + return; + } + bool? ullage; try { diff --git a/MechJebLibBindings/ReflectionUtils.cs b/MechJebLibBindings/ReflectionUtils.cs index 75cfdc31..acd7e49f 100644 --- a/MechJebLibBindings/ReflectionUtils.cs +++ b/MechJebLibBindings/ReflectionUtils.cs @@ -114,5 +114,34 @@ public static bool IsAssemblyLoaded(string assemblyName) return type.GetMethod(methodName, flags, null, args, null); } + + public static Type? GetClassByReflection(string assemblyString, string className) + { + string assemblyName = ""; + + foreach (AssemblyLoader.LoadedAssembly loaded in AssemblyLoader.loadedAssemblies) + { + if (loaded.assembly.GetName().Name == assemblyString) + { + assemblyName = loaded.assembly.FullName; + } + } + + if (assemblyName == "") + { + Debug.Log("[MechJeb] ReflectionUtils: could not find assembly " + assemblyString); + return null; + } + + var type = Type.GetType(className + ", " + assemblyName); + + if (type == null) + { + Debug.Log("[MechJeb] ReflectionUtils: could not find type " + className + ", " + assemblyName); + return null; + } + + return type; + } } }