Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Only check for ullage on ModuleEnginesRF #1924

Merged
merged 1 commit into from
Dec 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
}
}
}