Skip to content

Commit

Permalink
Only check for ullage on ModuleEnginesRF
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Nazfib committed Dec 10, 2024
1 parent 5bc25a9 commit b54372b
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 0 deletions.
2 changes: 2 additions & 0 deletions MechJeb2/PartExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
15 changes: 15 additions & 0 deletions MechJeb2/VesselState.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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)
{
Expand Down Expand Up @@ -1429,6 +1439,11 @@ public void CheckUllageStatus(ModuleEngines e)
return;
}

if (!RFModuleEnginesRFType.IsInstanceOfType(e))
{
return;
}

bool? ullage;
try
{
Expand Down
29 changes: 29 additions & 0 deletions MechJebLibBindings/ReflectionUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
}

0 comments on commit b54372b

Please sign in to comment.