Skip to content
Open
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
46 changes: 43 additions & 3 deletions SCANmechjeb/SCANmechjeb.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,45 @@ class SCANmechjeb : MonoBehaviour
private SCANdata data;
private Vector2d coords = new Vector2d();
private bool shutdown, mjOnboard, mjTechTreeLocked;

#region Helpers

/// <summary>
/// Reflectively fetches MechJebCore's target controller, tolerating the
/// 'Target'/'target' field rename across MechJeb versions. Returns null
/// (and logs) if the field can't be resolved under any known name.
/// </summary>
private static MechJebModuleTargetController GetMJCoreTarget(MechJebCore mjc)
{
var t = mjc.GetType();
var field = t.GetField("Target") ?? t.GetField("target");
if (field == null)
{
KSPBuildTools.Log.Message(
"MechJebCore 'target' field could not be found under any known name; MechJeb support broken.");
}

return field?.GetValue(mjc) as MechJebModuleTargetController;
}

/// <summary>
/// Reflectively reads the guidance module's hidden flag, tolerating the
/// 'Hidden'/'hidden' field rename across MechJeb versions. Returns null
/// (and logs) if the field can't be resolved under any known name.
/// </summary>
private static bool? IsMJGuidanceModuleHidden(DisplayModule gm)
{
var t = gm.GetType();
var field = t.GetField("Hidden") ?? t.GetField("hidden");
if (field == null)
{
KSPBuildTools.Log.Message(
"MechJebGuidanceModule 'hidden' field could not be found under any known name; MechJeb support broken.");
}
return field?.GetValue(gm) as bool?;
}

#endregion

private void Start()
{
Expand Down Expand Up @@ -227,8 +266,8 @@ private void RefereshAfterVesselChange()
return;
}

target = mjCore.target;

target = GetMJCoreTarget(mjCore);
if (target == null)
{
SCANcontroller.controller.MechJebLoaded = false;
Expand Down Expand Up @@ -256,7 +295,8 @@ private void RefereshAfterVesselChange()

guidanceModule.UnlockCheck();

if (guidanceModule.hidden)
var hidden = IsMJGuidanceModuleHidden(guidanceModule);
if (hidden ?? true) // Defaults to a hidden state if the field cannot be found
{
SCANcontroller.controller.MechJebLoaded = false;
way = null;
Expand Down