Skip to content
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
5 changes: 4 additions & 1 deletion Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,11 @@
<KspData Condition="'$(OS)' != 'Windows_NT' And !Exists('$(KspDir)/KSP.app')">$(KspDir)/KSP_Data</KspData>
</PropertyGroup>

<!-- Derive KosDll from KspDir. -->
<!-- Derive KosDll / KosSafeDll from KspDir. -->
<PropertyGroup Condition="'$(KosDll)' == ''">
<KosDll>$(KspDir)/GameData/kOS/Plugins/kOS.dll</KosDll>
</PropertyGroup>
<PropertyGroup Condition="'$(KosSafeDll)' == ''">
<KosSafeDll>$(KspDir)/GameData/kOS/Plugins/kOS.Safe.dll</KosSafeDll>
</PropertyGroup>
</Project>
43 changes: 43 additions & 0 deletions MechJebKos/Addon.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* Copyright Lamont Granquist, Sebastien Gaggini and the MechJeb contributors
* SPDX-License-Identifier: LicenseRef-PD-hp OR Unlicense OR CC0-1.0 OR 0BSD OR MIT-0 OR MIT OR LGPL-2.1+
*/

using JetBrains.Annotations;
using kOS;
using kOS.AddOns;
using kOS.Safe.Encapsulation;
using kOS.Safe.Encapsulation.Suffixes;
using kOS.Safe.Utilities;

namespace MuMech.MechJebKos
{
// The kOS addon entry point, reached from scripts as ADDONS:MECHJEB.
//
// kOS instantiates one Addon per processor (per SharedObjects), so this is inherently
// local to a single vessel: everything resolves through shared.Vessel and we never reach
// for FlightGlobals.activeVessel or a global singleton. Cross-vessel coordination is left
// to kOS itself.
[kOSAddon("MechJeb")]
[KOSNomenclature("MechJebAddon")]
[UsedImplicitly]
public class Addon : kOS.Suffixed.Addon
{
public Addon(SharedObjects shared) : base(shared) => RegisterInitializer(InitializeSuffixes);

// must never be cached, it can be updated dyanmically
private MechJebCore? _core => shared.Vessel.GetMasterMechJeb();

public override BooleanValue Available() => !(_core is null);

private void InitializeSuffixes()
{
var nodeExecutor = new NodeExecutorBinding(() => _core);

AddSuffix("RUNNING", new NoArgsSuffix<BooleanValue>(() => _core?.running ?? false,
"True if MechJeb is present and running on this vessel."));
AddSuffix(new[] { "NODE", "NODEEXECUTOR" }, new NoArgsSuffix<NodeExecutorBinding>(() => nodeExecutor,
"The maneuver node executor."));
}
}
}
52 changes: 52 additions & 0 deletions MechJebKos/ComputerModuleBinding.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* Copyright Lamont Granquist, Sebastien Gaggini and the MechJeb contributors
* SPDX-License-Identifier: LicenseRef-PD-hp OR Unlicense OR CC0-1.0 OR 0BSD OR MIT-0 OR MIT OR LGPL-2.1+
*/

using System;
using kOS.Safe.Encapsulation;
using kOS.Safe.Encapsulation.Suffixes;
using kOS.Safe.Exceptions;
using kOS.Safe.Utilities;

namespace MuMech.MechJebKos
{
[KOSNomenclature("MechJebComputerModule")]
public abstract class ComputerModuleBinding<T> : Structure where T : ComputerModule
{
private readonly Func<MechJebCore?> _core;

protected ComputerModuleBinding(Func<MechJebCore?> core)
{
_core = core;
RegisterInitializer(InitSuffixes);
}

// the Module deliberately re-evaluates on every call since the core can update dynamically
protected T Module
{
get
{
MechJebCore core = _core() ?? throw new KOSException("MechJeb is not available on this vessel.");
return core.GetComputerModule<T>();
}
}

private void InitSuffixes()
{
AddSuffix("ENABLED", new SetSuffix<BooleanValue>(() => Module.Enabled, value => SetEnabled(value),
"Whether the module is enabled."));
InitializeSuffixes();
}

protected abstract void InitializeSuffixes();

protected virtual void SetEnabled(bool enabled)
{
if (enabled)
Module.Enable();
else
Module.Disable();
}
}
}
11 changes: 11 additions & 0 deletions MechJebKos/MechJebKos.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
<AppendTargetFrameworkToOutputPath>false</AppendTargetFrameworkToOutputPath>
<DefineConstants>$(DefineConstants);UNITY_2017_1</DefineConstants>
<Prefer32Bit>false</Prefer32Bit>
<Nullable>enable</Nullable>
</PropertyGroup>

<PropertyGroup Condition=" '$(Configuration)' == 'Debug' ">
Expand All @@ -24,11 +25,21 @@
<SpecificVersion>False</SpecificVersion>
<Private>False</Private>
</Reference>
<Reference Include="kOS.Safe">
<HintPath>$(KosSafeDll)</HintPath>
<SpecificVersion>False</SpecificVersion>
<Private>False</Private>
</Reference>
<Reference Include="Assembly-CSharp">
<HintPath>$(KspData)/Managed/Assembly-CSharp.dll</HintPath>
<SpecificVersion>False</SpecificVersion>
<Private>False</Private>
</Reference>
<Reference Include="Assembly-CSharp-firstpass">
<HintPath>$(KspData)/Managed/Assembly-CSharp-firstpass.dll</HintPath>
<SpecificVersion>False</SpecificVersion>
<Private>False</Private>
</Reference>
<Reference Include="UnityEngine">
<HintPath>$(KspData)/Managed/UnityEngine.dll</HintPath>
<SpecificVersion>False</SpecificVersion>
Expand Down
11 changes: 0 additions & 11 deletions MechJebKos/MechJebKosAddon.cs

This file was deleted.

37 changes: 37 additions & 0 deletions MechJebKos/NodeExecutorBinding.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* Copyright Lamont Granquist, Sebastien Gaggini and the MechJeb contributors
* SPDX-License-Identifier: LicenseRef-PD-hp OR Unlicense OR CC0-1.0 OR 0BSD OR MIT-0 OR MIT OR LGPL-2.1+
*/

using System;
using kOS.Safe.Encapsulation;
using kOS.Safe.Encapsulation.Suffixes;
using kOS.Safe.Utilities;

namespace MuMech.MechJebKos
{
// ADDONS:MECHJEB:NODE - drives MechJebModuleNodeExecutor.
[KOSNomenclature("MechJebNodeExecutor")]
public class NodeExecutorBinding : ComputerModuleBinding<MechJebModuleNodeExecutor>
{
public NodeExecutorBinding(Func<MechJebCore?> core) : base(core) { }

protected override void InitializeSuffixes()
{
AddSuffix("STATE", new Suffix<StringValue>(() => Module.State.ToString(),
"Executor state: WARPALIGN, LEAD, BURN, or IDLE."));
AddSuffix(new[] { "AUTOWARP", "WARP" },
new SetSuffix<BooleanValue>(() => Module.Autowarp, value => Module.Autowarp = value,
"Automatically time-warp to the node."));
}

// The node executor engages via ExecuteOneNode / Abort rather than the plain Enabled toggle.
protected override void SetEnabled(bool enabled)
{
if (enabled)
Module.ExecuteOneNode(this);
else
Module.Abort();
}
}
}
9 changes: 9 additions & 0 deletions MechJebKos/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/*
* Copyright Lamont Granquist, Sebastien Gaggini and the MechJeb contributors
* SPDX-License-Identifier: LicenseRef-PD-hp OR Unlicense OR CC0-1.0 OR 0BSD OR MIT-0 OR MIT OR LGPL-2.1+
*/

// KSP load-order dependencies. The (major, minor) pair is a *minimum* version requirement;
// KSP refuses to load this assembly unless a matching-or-newer KSPAssembly is present.
[assembly: KSPAssemblyDependency("kOS", 1, 6)]
[assembly: KSPAssemblyDependency("MechJeb2", 2, 16)]
Loading