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

Issue 1295 external tool #1552

Open
wants to merge 4 commits into
base: develop
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
15 changes: 15 additions & 0 deletions src/kOS.CommandLine/Binding/BindingConfig.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using kOS.Safe.Binding;
using kOS.Safe.Utilities;
using kOS.Safe;

namespace kOS.CommandLine.Binding
{
[Binding("ksp")]
public class BindingConfig : kOS.Safe.Binding.SafeBinding
{
public override void AddTo(SharedObjects shared)
{
shared.BindingMgr.AddGetter("CONFIG", () => SafeHouse.Config);
}
}
}
158 changes: 158 additions & 0 deletions src/kOS.CommandLine/Binding/BindingManager.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
using kOS.Safe;
using kOS.Safe.Binding;
using kOS.Safe.Utilities;
using System;
using System.Collections.Generic;
using System.Linq;

namespace kOS.CommandLine.Binding
{
[AssemblyWalk(AttributeType = typeof(BindingAttribute), StaticRegisterMethod = "RegisterMethod")]
public class BindingManager : IDisposable, IBindingManager
{
private readonly SharedObjects shared;
private readonly List<kOS.Safe.Binding.SafeBinding> bindings = new List<kOS.Safe.Binding.SafeBinding>();
private readonly Dictionary<string, BoundVariable> variables;
private static readonly Dictionary<BindingAttribute, Type> rawAttributes = new Dictionary<BindingAttribute, Type>();
//private FlightControlManager flightControl;

public BindingManager(SharedObjects shared)
{
variables = new Dictionary<string, BoundVariable>(StringComparer.OrdinalIgnoreCase);
this.shared = shared;
this.shared.BindingMgr = this;
}

public void Load()
{
var contexts = new string[1];
contexts[0] = "ksp";

bindings.Clear();
variables.Clear();
//flightControl = null;

foreach (BindingAttribute attr in rawAttributes.Keys)
{
var t = rawAttributes[attr];
if (attr.Contexts.Any() && !attr.Contexts.Intersect(contexts).Any()) continue;
var b = (kOS.Safe.Binding.SafeBinding)Activator.CreateInstance(t);
b.AddTo(shared);
bindings.Add(b);

//var manager = b as FlightControlManager;
//if (manager != null)
//{
// flightControl = manager;
//}
}
}

public static void RegisterMethod(BindingAttribute attr, Type type)
{
if (!rawAttributes.ContainsKey(attr))
{
rawAttributes.Add(attr, type);
}
}

public void AddBoundVariable(string name, BindingGetDlg getDelegate, BindingSetDlg setDelegate)
{
BoundVariable variable;
if (variables.ContainsKey(name))
{
variable = variables[name];
}
else
{
variable = new BoundVariable
{
Name = name,
};
variables.Add(name, variable);
shared.Cpu.AddVariable(variable, name, false);
}

if (getDelegate != null)
variable.Get = getDelegate;

if (setDelegate != null)
variable.Set = setDelegate;
}

public void AddGetter(string name, BindingGetDlg dlg)
{
AddBoundVariable(name, dlg, null);
}

public void AddGetter(IEnumerable<string> names, BindingGetDlg dlg)
{
foreach (var name in names)
{
AddBoundVariable(name, dlg, null);
}
}

public void AddSetter(string name, BindingSetDlg dlg)
{
AddBoundVariable(name, null, dlg);
}

public void AddSetter(IEnumerable<string> names, BindingSetDlg dlg)
{
foreach (var name in names)
{
AddBoundVariable(name, null, dlg);
}
}

public void PreUpdate()
{
foreach (var variable in variables)
{
variable.Value.ClearCache();
}
// update the bindings
foreach (var b in bindings)
{
b.Update();
}
}

public void PostUpdate()
{
}

public void ToggleFlyByWire(string paramName, bool enabled)
{
//if (flightControl != null)
//{
// flightControl.ToggleFlyByWire(paramName, enabled);
//}
}

public void UnBindAll()
{
//if (flightControl != null)
//{
// flightControl.UnBind();
//}
}

public void Dispose()
{
//if (flightControl != null)
//{
// flightControl.Dispose();
//}
}

public void SelectAutopilotMode(string autopilotMode)
{
//if (flightControl != null)
//{
// flightControl.SelectAutopilotMode(autopilotMode);
//}
}
}
}
48 changes: 48 additions & 0 deletions src/kOS.CommandLine/ConsoleLogger.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.IO;
using System.Text;
using kOS.Safe;

namespace kOS.CommandLine
{
public class ConsoleLogger : ILogger
{
public void Log(string text)
{
Console.WriteLine(text);
//throw new NotImplementedException();
}

public void Log(Exception e)
{
Console.WriteLine(e.Message);
//throw new NotImplementedException();
}

public void SuperVerbose(string s)
{
Console.WriteLine(s);
//throw new NotImplementedException();
}

public void LogWarning(string s)
{
Console.WriteLine(s);
//throw new NotImplementedException();
}

public void LogException(Exception exception)
{
Console.WriteLine(exception.Message);
//throw new NotImplementedException();
}

public void LogError(string s)
{
Console.WriteLine(s);
//throw new NotImplementedException();
}
}
}
Loading