From b54372b006122a3aad5bc90d5380d01bb46427b5 Mon Sep 17 00:00:00 2001 From: Nazfib Date: Mon, 25 Nov 2024 22:39:08 +0100 Subject: [PATCH] Only check for ullage on ModuleEnginesRF When trying to check for ullage on a non-ModuleEnginesRF engine, VesselState sets the reflection fields to null; this causes the autostaging code to spam NREs and refuse to work. This change makes sure that an engine is a (subclass of) ModuleEnginesRF before trying to access the ullage field. --- MechJeb2/PartExtensions.cs | 2 ++ MechJeb2/VesselState.cs | 15 ++++++++++++++ MechJebLibBindings/ReflectionUtils.cs | 29 +++++++++++++++++++++++++++ 3 files changed, 46 insertions(+) diff --git a/MechJeb2/PartExtensions.cs b/MechJeb2/PartExtensions.cs index e5fad171f..e6431510d 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 0d6f94db0..0f70b0c38 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 75cfdc316..acd7e49f0 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; + } } }