Skip to content

Commit

Permalink
Merge pull request #1924 from Nazfib/ullage-reflection-fix
Browse files Browse the repository at this point in the history
  • Loading branch information
lamont-granquist authored Dec 11, 2024
2 parents 5bc25a9 + b54372b commit 184a534
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 184a534

Please sign in to comment.