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

Added simple RemoteTech 2 integration. #136

Open
wants to merge 2 commits into
base: dev
Choose a base branch
from
Open
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
105 changes: 105 additions & 0 deletions SCANsat/Hooks/RT2Hook.cs
Original file line number Diff line number Diff line change
@@ -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 <http://www.gnu.org/licenses/>.
//
///////////////////////////////////////////////////////////////////////////////


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<Guid, bool> HasFlightComputer { get; internal set; }
public Action<Guid, Action<FlightCtrlState>> AddSanctionedPilot { get; internal set; }
public Action<Guid, Action<FlightCtrlState>> RemoveSanctionedPilot { get; internal set; }
public Func<Guid, bool> HasAnyConnection { get; internal set; }
public Func<Guid, bool> HasConnectionToKSC { get; internal set; }
public Func<Guid, double> GetShortestSignalDelay { get; internal set; }
public Func<Guid, double> GetSignalDelayToKSC { get; internal set; }
public Func<Guid, Guid, double> GetSignalDelayToSatellite { get; internal set; }
}

public interface IRemoteTechAPIv1
{
Func<Guid, bool> HasFlightComputer { get; }
Action<Guid, Action<FlightCtrlState>> AddSanctionedPilot { get; }
Action<Guid, Action<FlightCtrlState>> RemoveSanctionedPilot { get; }
Func<Guid, bool> HasAnyConnection { get; }
Func<Guid, bool> HasConnectionToKSC { get; }
Func<Guid, double> GetShortestSignalDelay { get; }
Func<Guid, double> GetSignalDelayToKSC { get; }
Func<Guid, Guid, double> GetSignalDelayToSatellite { get; }
}
}
9 changes: 9 additions & 0 deletions SCANsat/SCAN_UI/SCANsettingsUI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
*/
#endregion

using SCANsat.Hooks;
using SCANsat.SCAN_Toolbar;
using SCANsat.SCAN_Data;
using SCANsat.SCAN_UI.UI_Framework;
Expand Down Expand Up @@ -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)
{
Expand Down
22 changes: 16 additions & 6 deletions SCANsat/SCANcontroller.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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)]
Expand Down Expand Up @@ -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;
Expand All @@ -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;
}
}
}
}

Expand Down
1 change: 1 addition & 0 deletions SCANsat/SCANsat.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
<NoStdLib>true</NoStdLib>
</PropertyGroup>
<ItemGroup>
<Compile Include="Hooks\RT2Hook.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="SCANconfigLoader.cs" />
<Compile Include="SCANresourceScanner.cs" />
Expand Down