Skip to content

Commit

Permalink
Extract KSCSwitcher integration into a separate class
Browse files Browse the repository at this point in the history
  • Loading branch information
siimav committed Jul 5, 2024
1 parent 3e16156 commit 49b3c5f
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 66 deletions.
65 changes: 65 additions & 0 deletions Source/RP0/ModIntegrations/KSCSwitcherInterop.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
using System;
using System.Linq;
using System.Reflection;

namespace RP0
{
public static class KSCSwitcherInterop
{
private static bool? _isKSCSwitcherInstalled = null;
private static FieldInfo _fiKSCSwInstance;
private static FieldInfo _fiKSCSwSites;
private static FieldInfo _fiKSCSwLastSite;
private static FieldInfo _fiKSCSwDefaultSite;

public const string LegacyDefaultKscId = "Stock";
public const string DefaultKscId = "us_cape_canaveral";

public static bool IsKSCSwitcherInstalled
{
get
{
if (!_isKSCSwitcherInstalled.HasValue)
{
Assembly a = AssemblyLoader.loadedAssemblies.FirstOrDefault(la => string.Equals(la.name, "KSCSwitcher", StringComparison.OrdinalIgnoreCase))?.assembly;
_isKSCSwitcherInstalled = a != null;
if (_isKSCSwitcherInstalled.Value)
{
Type t = a.GetType("regexKSP.KSCLoader");
_fiKSCSwInstance = t?.GetField("instance", BindingFlags.Public | BindingFlags.Static);
_fiKSCSwSites = t?.GetField("Sites", BindingFlags.Public | BindingFlags.Instance | BindingFlags.FlattenHierarchy);

t = a.GetType("regexKSP.KSCSiteManager");
_fiKSCSwLastSite = t?.GetField("lastSite", BindingFlags.Public | BindingFlags.Instance | BindingFlags.FlattenHierarchy);
_fiKSCSwDefaultSite = t?.GetField("defaultSite", BindingFlags.Public | BindingFlags.Instance | BindingFlags.FlattenHierarchy);

if (_fiKSCSwInstance == null || _fiKSCSwSites == null || _fiKSCSwLastSite == null || _fiKSCSwDefaultSite == null)
{
RP0Debug.LogError("Failed to bind to KSCSwitcher");
_isKSCSwitcherInstalled = false;
}
}
}
return _isKSCSwitcherInstalled.Value;
}
}

public static string GetActiveRSSKSC()
{
if (!IsKSCSwitcherInstalled) return null;

// get the LastKSC.KSCLoader.instance object
// check the Sites object (KSCSiteManager) for the lastSite, if "" then get defaultSite

object loaderInstance = _fiKSCSwInstance.GetValue(null);
if (loaderInstance == null)
return null;
object sites = _fiKSCSwSites.GetValue(loaderInstance);
string lastSite = _fiKSCSwLastSite.GetValue(sites) as string;

if (lastSite == string.Empty)
lastSite = _fiKSCSwDefaultSite.GetValue(sites) as string;
return lastSite;
}
}
}
1 change: 1 addition & 0 deletions Source/RP0/RP0.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
<Compile Include="ConfigurableStart\Utilities.cs" />
<Compile Include="Harmony\FlightInputHandler.cs" />
<Compile Include="Harmony\ModuleRCS.cs" />
<Compile Include="ModIntegrations\KSCSwitcherInterop.cs" />
<Compile Include="ModIntegrations\TFInterop.cs" />
<Compile Include="Singletons\LeaderNotifications.cs" />
<Compile Include="SpaceCenter\Projects\HireStaffProject.cs" />
Expand Down
73 changes: 7 additions & 66 deletions Source/RP0/SpaceCenter/SpaceCenterManagement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -525,7 +525,7 @@ public override void OnLoad(ConfigNode node)
bool foundStockKSC = false;
foreach (var ksc in KSCs)
{
if (ksc.KSCName.Length > 0 && string.Equals(ksc.KSCName, _legacyDefaultKscId, StringComparison.OrdinalIgnoreCase))
if (ksc.KSCName.Length > 0 && string.Equals(ksc.KSCName, KSCSwitcherInterop.LegacyDefaultKscId, StringComparison.OrdinalIgnoreCase))
{
foundStockKSC = true;
break;
Expand Down Expand Up @@ -646,11 +646,11 @@ private bool FixVesselSatPayload(VesselProject vp)

private void TryMigrateStockKSC()
{
LCSpaceCenter stockKsc = KSCs.Find(k => string.Equals(k.KSCName, _legacyDefaultKscId, StringComparison.OrdinalIgnoreCase));
LCSpaceCenter stockKsc = KSCs.Find(k => string.Equals(k.KSCName, KSCSwitcherInterop.LegacyDefaultKscId, StringComparison.OrdinalIgnoreCase));
if (KSCs.Count == 1)
{
// Rename the stock KSC to the new default (Cape)
stockKsc.KSCName = _defaultKscId;
stockKsc.KSCName = KSCSwitcherInterop.DefaultKscId;
SetActiveKSC(stockKsc.KSCName);
return;
}
Expand All @@ -666,7 +666,7 @@ private void TryMigrateStockKSC()
int numOtherUsedKSCs = KSCs.Count(k => !k.IsEmpty && k != stockKsc);
if (numOtherUsedKSCs == 0)
{
string kscName = GetActiveRSSKSC() ?? _defaultKscId;
string kscName = KSCSwitcherInterop.GetActiveRSSKSC() ?? KSCSwitcherInterop.DefaultKscId;
LCSpaceCenter newDefault = KSCs.Find(k => string.Equals(k.KSCName, kscName, StringComparison.OrdinalIgnoreCase));
if (newDefault != null)
{
Expand All @@ -679,7 +679,7 @@ private void TryMigrateStockKSC()
}

// Can't really do anything if there's multiple KSCs in use.
if (!IsKSCSwitcherInstalled)
if (!KSCSwitcherInterop.IsKSCSwitcherInstalled)
{
// Need to switch back to the legacy "Stock" KSC if KSCSwitcher isn't installed
SetActiveKSC(stockKsc.KSCName);
Expand Down Expand Up @@ -866,77 +866,18 @@ public VesselRepairProject FindRepairForVessel(Vessel v)

#endregion

#region KSCSwitcher section

private static bool? _isKSCSwitcherInstalled = null;
private static FieldInfo _fiKSCSwInstance;
private static FieldInfo _fiKSCSwSites;
private static FieldInfo _fiKSCSwLastSite;
private static FieldInfo _fiKSCSwDefaultSite;
private const string _legacyDefaultKscId = "Stock";
private const string _defaultKscId = "us_cape_canaveral";

private static bool IsKSCSwitcherInstalled
{
get
{
if (!_isKSCSwitcherInstalled.HasValue)
{
Assembly a = AssemblyLoader.loadedAssemblies.FirstOrDefault(la => string.Equals(la.name, "KSCSwitcher", StringComparison.OrdinalIgnoreCase))?.assembly;
_isKSCSwitcherInstalled = a != null;
if (_isKSCSwitcherInstalled.Value)
{
Type t = a.GetType("regexKSP.KSCLoader");
_fiKSCSwInstance = t?.GetField("instance", BindingFlags.Public | BindingFlags.Static);
_fiKSCSwSites = t?.GetField("Sites", BindingFlags.Public | BindingFlags.Instance | BindingFlags.FlattenHierarchy);

t = a.GetType("regexKSP.KSCSiteManager");
_fiKSCSwLastSite = t?.GetField("lastSite", BindingFlags.Public | BindingFlags.Instance | BindingFlags.FlattenHierarchy);
_fiKSCSwDefaultSite = t?.GetField("defaultSite", BindingFlags.Public | BindingFlags.Instance | BindingFlags.FlattenHierarchy);

if (_fiKSCSwInstance == null || _fiKSCSwSites == null || _fiKSCSwLastSite == null || _fiKSCSwDefaultSite == null)
{
RP0Debug.LogError("Failed to bind to KSCSwitcher");
_isKSCSwitcherInstalled = false;
}
}
}
return _isKSCSwitcherInstalled.Value;
}
}

private string GetActiveRSSKSC()
{
if (!IsKSCSwitcherInstalled) return null;

// get the LastKSC.KSCLoader.instance object
// check the Sites object (KSCSiteManager) for the lastSite, if "" then get defaultSite

object loaderInstance = _fiKSCSwInstance.GetValue(null);
if (loaderInstance == null)
return null;
object sites = _fiKSCSwSites.GetValue(loaderInstance);
string lastSite = _fiKSCSwLastSite.GetValue(sites) as string;

if (lastSite == string.Empty)
lastSite = _fiKSCSwDefaultSite.GetValue(sites) as string;
return lastSite;
}

#endregion

#region KSC

private void SetActiveKSCToRSS()
{
string site = GetActiveRSSKSC();
string site = KSCSwitcherInterop.GetActiveRSSKSC();
SetActiveKSC(site);
}

public void SetActiveKSC(string site)
{
if (site == null || site.Length == 0)
site = _defaultKscId;
site = KSCSwitcherInterop.DefaultKscId;
if (ActiveSC == null || site != ActiveSC.KSCName)
{
RP0Debug.Log($"Setting active site to {site}");
Expand Down

0 comments on commit 49b3c5f

Please sign in to comment.