diff --git a/SCANsat/Hooks/RT2Hook.cs b/SCANsat/Hooks/RT2Hook.cs new file mode 100644 index 000000000..77c12831a --- /dev/null +++ b/SCANsat/Hooks/RT2Hook.cs @@ -0,0 +1,105 @@ +/////////////////////////////////////////////////////////////////////////////// +// +// RemoteTech Hook code by Cilph from KOS Mod +// Much thanks to him for his help on this part ;) +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program. If not, see . +// +/////////////////////////////////////////////////////////////////////////////// + + +using System; +using System.Reflection; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using UnityEngine; + +namespace SCANsat.Hooks +{ + public static class RT2Hook + { + public const String RemoteTechAssembly = "RemoteTech"; + public const String RemoteTechApi = "RemoteTech.API.API"; + + private static bool mHookFail; + private static IRemoteTechAPIv1 mInstance; + + public static IRemoteTechAPIv1 Instance + { + get + { + if (mHookFail) return null; + mInstance = mInstance ?? InitializeAPI(); + if (mInstance == null) mHookFail = true; + return mInstance; + } + } + + private static IRemoteTechAPIv1 InitializeAPI() + { + var loadedAssembly = AssemblyLoader.loadedAssemblies.FirstOrDefault(a => a.assembly.GetName().Name.Equals(RemoteTechAssembly)); + if (loadedAssembly == null) return null; + + var type = loadedAssembly.assembly.GetTypes().FirstOrDefault(t => t.FullName.Equals(RemoteTechApi)); + if (type == null) return null; + + var methods = type.GetMethods(); + var api = new RTInterfaceImplementation(); + + try + { + foreach (var property in api.GetType().GetProperties()) + { + var method = methods.FirstOrDefault(m => { Debug.Log(m.Name); return m.Name.Equals(property.Name); }); + if (method == null) throw new ArgumentNullException(property.Name); + var del = Delegate.CreateDelegate(property.PropertyType, type, method.Name); + property.SetValue(api, del, null); + } + } + catch (Exception e) + { + Debug.Log("[SCANlogger] Error creating RemoteTech interface: " + e); + return null; + } + + Debug.Log("[SCANlogger] RemoteTech interface successfully created."); + return api; + } + } + + internal class RTInterfaceImplementation : IRemoteTechAPIv1 + { + public Func HasFlightComputer { get; internal set; } + public Action> AddSanctionedPilot { get; internal set; } + public Action> RemoveSanctionedPilot { get; internal set; } + public Func HasAnyConnection { get; internal set; } + public Func HasConnectionToKSC { get; internal set; } + public Func GetShortestSignalDelay { get; internal set; } + public Func GetSignalDelayToKSC { get; internal set; } + public Func GetSignalDelayToSatellite { get; internal set; } + } + + public interface IRemoteTechAPIv1 + { + Func HasFlightComputer { get; } + Action> AddSanctionedPilot { get; } + Action> RemoveSanctionedPilot { get; } + Func HasAnyConnection { get; } + Func HasConnectionToKSC { get; } + Func GetShortestSignalDelay { get; } + Func GetSignalDelayToKSC { get; } + Func GetSignalDelayToSatellite { get; } + } +} \ No newline at end of file diff --git a/SCANsat/SCAN_UI/SCANsettingsUI.cs b/SCANsat/SCAN_UI/SCANsettingsUI.cs index d08d45044..7de7bf654 100644 --- a/SCANsat/SCAN_UI/SCANsettingsUI.cs +++ b/SCANsat/SCAN_UI/SCANsettingsUI.cs @@ -13,6 +13,7 @@ */ #endregion +using SCANsat.Hooks; using SCANsat.SCAN_Toolbar; using SCANsat.SCAN_Data; using SCANsat.SCAN_UI.UI_Framework; @@ -339,6 +340,14 @@ private void gui_settings_window_resets_tooltips(int id) if (SCANmainMenuLoader.MechJebLoaded) SCANcontroller.controller.mechJebTargetSelection = GUILayout.Toggle(SCANcontroller.controller.mechJebTargetSelection, "MechJeb Target Selection", SCANskins.SCAN_settingsToggle); stopE(); + + if (RT2Hook.Instance != null) + { + growE(); + SCANcontroller.controller.rt2Integration = GUILayout.Toggle(SCANcontroller.controller.rt2Integration, "RemoteTech 2 integration (Experimental)", SCANskins.SCAN_settingsToggle); + stopE(); + } + fillS(8); if (popup) { diff --git a/SCANsat/SCANcontroller.cs b/SCANsat/SCANcontroller.cs index 1fff5ac90..1103f8bfa 100644 --- a/SCANsat/SCANcontroller.cs +++ b/SCANsat/SCANcontroller.cs @@ -15,6 +15,7 @@ using System; using System.Linq; using System.Collections.Generic; +using SCANsat.Hooks; using UnityEngine; using SCANsat.SCAN_UI; using SCANsat.SCAN_UI.UI_Framework; @@ -111,6 +112,8 @@ public static SCANcontroller controller public float biomeTransparency = 40; [KSPField(isPersistant = true)] public bool mechJebTargetSelection = false; + [KSPField(isPersistant = true)] + public bool rt2Integration = false; [KSPField(isPersistant = true)] public bool easyModeScanning = true; [KSPField(isPersistant = true)] @@ -1299,6 +1302,9 @@ private void scanFromAllVessels() i++; if (i >= body_data.Count) i = 0; } + + var remoteTechAPI = RT2Hook.Instance; + foreach (Vessel v in FlightGlobals.Vessels) { if (!knownVessels.ContainsKey(v.id)) continue; @@ -1312,12 +1318,16 @@ private void scanFromAllVessels() { if (v.mainBody == FlightGlobals.currentMainBody || scan_background) { - if (isVesselKnown(v)) - { - doScanPass(knownVessels[v.id], scan_UT, scan_UT, vessel.lastUT, vessel.latitude, vessel.longitude); - ++currentActiveVessel; - currentActiveSensor += knownVessels[v.id].sensors.Count; - } + if (isVesselKnown(v)) + { + // Check for connection to KSC if RemoteTech mod is installed and integration enabled. + if (!rt2Integration || remoteTechAPI == null || remoteTechAPI.HasConnectionToKSC(v.id)) + { + doScanPass(knownVessels[v.id], scan_UT, scan_UT, vessel.lastUT, vessel.latitude, vessel.longitude); + ++currentActiveVessel; + currentActiveSensor += knownVessels[v.id].sensors.Count; + } + } } } diff --git a/SCANsat/SCANsat.csproj b/SCANsat/SCANsat.csproj index e58707acb..464a12dcb 100644 --- a/SCANsat/SCANsat.csproj +++ b/SCANsat/SCANsat.csproj @@ -45,6 +45,7 @@ true +