diff --git a/src/kOS.Safe.Test/Execution/BaseIntegrationTest.cs b/src/kOS.Safe.Test/Execution/BaseIntegrationTest.cs index 723b49015d..54c75b8e9b 100644 --- a/src/kOS.Safe.Test/Execution/BaseIntegrationTest.cs +++ b/src/kOS.Safe.Test/Execution/BaseIntegrationTest.cs @@ -67,7 +67,7 @@ public void Setup() shared.UpdateHandler = new UpdateHandler(); shared.VolumeMgr = new VolumeManager(); - shared.FunctionManager.Load(); + shared.FunctionManager.Load(new string[] { "ksp" }); Archive archive = new Archive(baseDir); shared.VolumeMgr.Add(archive); diff --git a/src/kOS.Safe.Test/Opcode/FakeCpu.cs b/src/kOS.Safe.Test/Opcode/FakeCpu.cs index 3be5ffd251..ba4551c39d 100644 --- a/src/kOS.Safe.Test/Opcode/FakeCpu.cs +++ b/src/kOS.Safe.Test/Opcode/FakeCpu.cs @@ -282,7 +282,7 @@ public Compilation.Opcode GetOpcodeAt(int instructionPtr) throw new NotImplementedException(); } - public void Boot() + public void Boot(string[] contexts) { throw new NotImplementedException(); } diff --git a/src/kOS.Safe/Binding/IBindingManager.cs b/src/kOS.Safe/Binding/IBindingManager.cs index de4e48fd3f..b033ac8a35 100644 --- a/src/kOS.Safe/Binding/IBindingManager.cs +++ b/src/kOS.Safe/Binding/IBindingManager.cs @@ -4,7 +4,7 @@ namespace kOS.Safe.Binding { public interface IBindingManager { - void Load(); + void Load(string[] contexts); void AddBoundVariable(string name, BindingGetDlg getDelegate, BindingSetDlg setDelegate); void AddGetter(string name, BindingGetDlg dlg); void AddGetter(IEnumerable names, BindingGetDlg dlg); diff --git a/src/kOS.Safe/Encapsulation/Lexicon.cs b/src/kOS.Safe/Encapsulation/Lexicon.cs index e621281051..1e561531f7 100644 --- a/src/kOS.Safe/Encapsulation/Lexicon.cs +++ b/src/kOS.Safe/Encapsulation/Lexicon.cs @@ -14,7 +14,7 @@ namespace kOS.Safe.Encapsulation [kOS.Safe.Utilities.KOSNomenclature("Lex", CSharpToKOS = false) ] public class Lexicon : SerializableStructure, IDictionary, IIndexable { - [Function("lex", "lexicon")] + [Function("lex", "lexicon", Contexts=new string[] { "ksp", "archive" })] public class FunctionLexicon : SafeFunctionBase { public override void Execute(SafeSharedObjects shared) diff --git a/src/kOS.Safe/Encapsulation/ListValue.cs b/src/kOS.Safe/Encapsulation/ListValue.cs index 5650380282..fd7a1b1498 100644 --- a/src/kOS.Safe/Encapsulation/ListValue.cs +++ b/src/kOS.Safe/Encapsulation/ListValue.cs @@ -150,7 +150,7 @@ public void Insert(int index, T item) [kOS.Safe.Utilities.KOSNomenclature("List", KOSToCSharp = false)] // one-way because the generic templated ListValue is the canonical one. public class ListValue : ListValue { - [Function("list")] + [Function("list", Contexts=new string[] { "ksp", "archive" })] public class FunctionList : SafeFunctionBase { public override void Execute(SafeSharedObjects shared) diff --git a/src/kOS.Safe/Encapsulation/PIDLoop.cs b/src/kOS.Safe/Encapsulation/PIDLoop.cs index c2c36af9c9..f0fa30d0c0 100644 --- a/src/kOS.Safe/Encapsulation/PIDLoop.cs +++ b/src/kOS.Safe/Encapsulation/PIDLoop.cs @@ -9,7 +9,7 @@ namespace kOS.Safe.Encapsulation [kOS.Safe.Utilities.KOSNomenclature("PIDLoop")] public class PIDLoop : SerializableStructure { - [Function("pidloop")] + [Function("pidloop", Contexts=new string[] { "ksp", "archive" })] public class PIDLoopConstructor : SafeFunctionBase { public override void Execute(SafeSharedObjects shared) diff --git a/src/kOS.Safe/Encapsulation/QueueValue.cs b/src/kOS.Safe/Encapsulation/QueueValue.cs index 9c91ac7e3e..26dedb0028 100644 --- a/src/kOS.Safe/Encapsulation/QueueValue.cs +++ b/src/kOS.Safe/Encapsulation/QueueValue.cs @@ -68,7 +68,7 @@ public static QueueValue CreateQueue(IEnumerable list) [kOS.Safe.Utilities.KOSNomenclature("Queue", KOSToCSharp = false)] // one-way because the generic templated QueueValue is the canonical one. public class QueueValue : QueueValue { - [Function("queue")] + [Function("queue", Contexts=new string[] { "ksp", "archive" })] public class FunctionQueue : SafeFunctionBase { public override void Execute(SafeSharedObjects shared) diff --git a/src/kOS.Safe/Encapsulation/StackValue.cs b/src/kOS.Safe/Encapsulation/StackValue.cs index 1eed16dae6..5a75a5aa08 100644 --- a/src/kOS.Safe/Encapsulation/StackValue.cs +++ b/src/kOS.Safe/Encapsulation/StackValue.cs @@ -74,7 +74,7 @@ public static StackValue CreateStack(IEnumerable list) [kOS.Safe.Utilities.KOSNomenclature("Stack", KOSToCSharp = false)] // one-way because the generic templated StackValue is the canonical one. public class StackValue : StackValue { - [Function("stack")] + [Function("stack", Contexts=new string[] { "ksp", "archive" })] public class FunctionStack : SafeFunctionBase { public override void Execute(SafeSharedObjects shared) diff --git a/src/kOS.Safe/Encapsulation/UniqueSetValue.cs b/src/kOS.Safe/Encapsulation/UniqueSetValue.cs index 4e0e1c0da9..7d2b3749e9 100644 --- a/src/kOS.Safe/Encapsulation/UniqueSetValue.cs +++ b/src/kOS.Safe/Encapsulation/UniqueSetValue.cs @@ -73,7 +73,7 @@ private void SetInitializeSuffixes() [kOS.Safe.Utilities.KOSNomenclature("UniqueSet", KOSToCSharp = false)] // one-way because the generic templated UniqueSetValue is the canonical one. public class UniqueSetValue : UniqueSetValue { - [Function("uniqueset")] + [Function("uniqueset", Contexts=new string[] { "ksp", "archive" })] public class FunctionSet : SafeFunctionBase { public override void Execute(SafeSharedObjects shared) diff --git a/src/kOS.Safe/Execution/CPU.cs b/src/kOS.Safe/Execution/CPU.cs index 08534b28bd..b519df0228 100644 --- a/src/kOS.Safe/Execution/CPU.cs +++ b/src/kOS.Safe/Execution/CPU.cs @@ -91,8 +91,10 @@ public CPU(SafeSharedObjects shared) popContextNotifyees = new List(); } - public void Boot() + public void Boot(string[] ctxs = null) { + if (ctxs == null) + ctxs = new string[] { "ksp" }; // break all running programs currentContext = null; contexts.Clear(); @@ -107,9 +109,9 @@ public void Boot() // clear interpreter if (shared.Interpreter != null) shared.Interpreter.Reset(); // load functions - if (shared.FunctionManager != null) shared.FunctionManager.Load(); + if (shared.FunctionManager != null) shared.FunctionManager.Load(ctxs); // load bindings - if (shared.BindingMgr != null) shared.BindingMgr.Load(); + if (shared.BindingMgr != null) shared.BindingMgr.Load(ctxs); // Booting message if (shared.Screen != null) @@ -400,6 +402,8 @@ public IUserDelegate MakeUserDelegate(int entryPoint, bool withClosure) // only two contexts exist now, one for the interpreter and one for the programs public IProgramContext GetInterpreterContext() { + if (contexts.Count == 0) + return null; return contexts[0]; } @@ -507,7 +511,8 @@ public void BreakExecution(bool manual) { PopFirstContext(); shared.Screen.Print("Program aborted."); - shared.SoundMaker.StopAllVoices(); // stop voices if execution was manually broken, but not if the program ends normally + if (shared.SoundMaker != null) + shared.SoundMaker.StopAllVoices(); // stop voices if execution was manually broken, but not if the program ends normally PrintStatistics(); stack.Clear(); } diff --git a/src/kOS.Safe/Execution/ICpu.cs b/src/kOS.Safe/Execution/ICpu.cs index 51c03c75cb..c2d6c838fd 100644 --- a/src/kOS.Safe/Execution/ICpu.cs +++ b/src/kOS.Safe/Execution/ICpu.cs @@ -58,7 +58,7 @@ public interface ICpu : IFixedUpdateObserver void RemovePopContextNotifyee(IPopContextNotifyee notifyee); Opcode GetCurrentOpcode(); Opcode GetOpcodeAt(int instructionPtr); - void Boot(); + void Boot(string[] contexts = null); int InstructionsThisUpdate { get; } void StartCompileStopwatch(); void StopCompileStopwatch(); diff --git a/src/kOS.Safe/Function/FunctionAttribute.cs b/src/kOS.Safe/Function/FunctionAttribute.cs index 6fb891d874..fe95580905 100644 --- a/src/kOS.Safe/Function/FunctionAttribute.cs +++ b/src/kOS.Safe/Function/FunctionAttribute.cs @@ -1,14 +1,16 @@ -using System; +using System; namespace kOS.Safe.Function { public class FunctionAttribute : Attribute { public string[] Names { get; set; } + public string[] Contexts { get; set; } public FunctionAttribute(params string[] names) { Names = names; + Contexts = new string[] { "ksp" }; } } } diff --git a/src/kOS.Safe/Function/FunctionManager.cs b/src/kOS.Safe/Function/FunctionManager.cs index c19106db5d..15fffb4c83 100644 --- a/src/kOS.Safe/Function/FunctionManager.cs +++ b/src/kOS.Safe/Function/FunctionManager.cs @@ -1,4 +1,4 @@ -using kOS.Safe.Utilities; +using kOS.Safe.Utilities; using System; using System.Collections.Generic; using System.Linq; @@ -16,16 +16,17 @@ public class FunctionManager : IFunctionManager public FunctionManager(SafeSharedObjects shared) { this.shared = shared; - Load(); + Load(new string[] { "ksp" }); } - public void Load() + public void Load(string[] contexts) { functions = new Dictionary(StringComparer.OrdinalIgnoreCase); foreach (FunctionAttribute attr in rawAttributes.Keys) { var type = rawAttributes[attr]; if (attr == null || type == null) continue; + if (attr.Contexts.Any() && !attr.Contexts.Intersect(contexts).Any()) continue; object functionObject = Activator.CreateInstance(type); foreach (string functionName in attr.Names) { diff --git a/src/kOS.Safe/Function/IFunctionManager.cs b/src/kOS.Safe/Function/IFunctionManager.cs index 1a243698f7..2d5f533639 100644 --- a/src/kOS.Safe/Function/IFunctionManager.cs +++ b/src/kOS.Safe/Function/IFunctionManager.cs @@ -2,7 +2,7 @@ namespace kOS.Safe.Function { public interface IFunctionManager { - void Load(); + void Load(string[] contexts); void CallFunction(string functionName); bool Exists(string functionName); } diff --git a/src/kOS.Safe/Function/Math.cs b/src/kOS.Safe/Function/Math.cs index 554a8c062a..781321c1c9 100644 --- a/src/kOS.Safe/Function/Math.cs +++ b/src/kOS.Safe/Function/Math.cs @@ -4,7 +4,7 @@ namespace kOS.Safe.Function { - [Function("abs")] + [Function("abs", Contexts = new string[] { "ksp", "archive" })] public class FunctionAbs : SafeFunctionBase { public override void Execute(SafeSharedObjects shared) @@ -16,7 +16,7 @@ public override void Execute(SafeSharedObjects shared) } } - [Function("mod")] + [Function("mod", Contexts = new string[] { "ksp", "archive" })] public class FunctionMod : SafeFunctionBase { public override void Execute(SafeSharedObjects shared) @@ -29,7 +29,7 @@ public override void Execute(SafeSharedObjects shared) } } - [Function("floor")] + [Function("floor", Contexts = new string[] { "ksp", "archive" })] public class FunctionFloor : SafeFunctionBase { public override void Execute(SafeSharedObjects shared) @@ -44,7 +44,7 @@ public override void Execute(SafeSharedObjects shared) } } - [Function("ceiling")] + [Function("ceiling", Contexts = new string[] { "ksp", "archive" })] public class FunctionCeiling : SafeFunctionBase { public override void Execute(SafeSharedObjects shared) @@ -59,7 +59,7 @@ public override void Execute(SafeSharedObjects shared) } } - [Function("round")] + [Function("round", Contexts = new string[] { "ksp", "archive" })] public class FunctionRound : SafeFunctionBase { public override void Execute(SafeSharedObjects shared) @@ -86,7 +86,7 @@ public override void Execute(SafeSharedObjects shared) } } - [Function("sqrt")] + [Function("sqrt", Contexts = new string[] { "ksp", "archive" })] public class FunctionSqrt : SafeFunctionBase { public override void Execute(SafeSharedObjects shared) @@ -99,7 +99,7 @@ public override void Execute(SafeSharedObjects shared) } - [Function("ln")] + [Function("ln", Contexts = new string[] { "ksp", "archive" })] public class FunctionLn : SafeFunctionBase { public override void Execute(SafeSharedObjects shared) @@ -111,7 +111,7 @@ public override void Execute(SafeSharedObjects shared) } } - [Function("log10")] + [Function("log10", Contexts = new string[] { "ksp", "archive" })] public class FunctionLog10 : SafeFunctionBase { public override void Execute(SafeSharedObjects shared) @@ -123,7 +123,7 @@ public override void Execute(SafeSharedObjects shared) } } - [Function("min")] + [Function("min", Contexts = new string[] { "ksp", "archive" })] public class FunctionMin : SafeFunctionBase { public override void Execute(SafeSharedObjects shared) @@ -153,7 +153,7 @@ public override void Execute(SafeSharedObjects shared) } } - [Function("max")] + [Function("max", Contexts = new string[] { "ksp", "archive" })] public class FunctionMax : SafeFunctionBase { public override void Execute(SafeSharedObjects shared) @@ -183,7 +183,7 @@ public override void Execute(SafeSharedObjects shared) } } - [Function("random")] + [Function("random", Contexts = new string[] { "ksp", "archive" })] public class FunctionRandom : SafeFunctionBase { private readonly Random random = new Random(); @@ -201,7 +201,7 @@ public override void Execute(SafeSharedObjects shared) } } - [Function("randomseed")] + [Function("randomseed", Contexts = new string[] { "ksp", "archive" })] public class FunctionRandomSeed : SafeFunctionBase { private readonly Random random = new Random(); @@ -215,7 +215,7 @@ public override void Execute(SafeSharedObjects shared) } } - [Function("char")] + [Function("char", Contexts = new string[] { "ksp", "archive" })] public class FunctionChar : SafeFunctionBase { public override void Execute(SafeSharedObjects shared) @@ -227,7 +227,7 @@ public override void Execute(SafeSharedObjects shared) } } - [Function("unchar")] + [Function("unchar", Contexts = new string[] { "ksp", "archive" })] public class FunctionUnchar : SafeFunctionBase { public override void Execute(SafeSharedObjects shared) diff --git a/src/kOS.Safe/Function/Misc.cs b/src/kOS.Safe/Function/Misc.cs index ccaae4cbe1..c4a8dacf64 100644 --- a/src/kOS.Safe/Function/Misc.cs +++ b/src/kOS.Safe/Function/Misc.cs @@ -10,7 +10,7 @@ namespace kOS.Safe.Function { - [Function("print")] + [Function("print", Contexts = new string[] { "ksp", "archive" })] public class FunctionPrint : SafeFunctionBase { public override void Execute(SafeSharedObjects shared) @@ -21,7 +21,7 @@ public override void Execute(SafeSharedObjects shared) } } - [Function("printat")] + [Function("printat", Contexts = new string[] { "ksp", "archive" })] public class FunctionPrintAt : SafeFunctionBase { public override void Execute(SafeSharedObjects shared) @@ -57,7 +57,7 @@ public override void Execute(SafeSharedObjects shared) } } - [Function("run")] + [Function("run", Contexts = new string[] { "ksp", "archive" })] public class FunctionRun : SafeFunctionBase { public override void Execute(SafeSharedObjects shared) @@ -128,7 +128,7 @@ public override void Execute(SafeSharedObjects shared) } } - [FunctionAttribute("load")] + [FunctionAttribute("load", Contexts = new string[] { "ksp", "archive" })] public class FunctionLoad : SafeFunctionBase { public override void Execute(SafeSharedObjects shared) @@ -251,7 +251,7 @@ public override void Execute(SafeSharedObjects shared) } } - [Function("logfile")] + [Function("logfile", Contexts = new string[] { "ksp", "archive" })] public class FunctionLogFile : SafeFunctionBase { public override void Execute(SafeSharedObjects shared) @@ -289,7 +289,7 @@ public override void Execute(SafeSharedObjects shared) } } - [Function("reboot")] + [Function("reboot", Contexts = new string[] { "ksp", "archive" })] public class FunctionReboot : SafeFunctionBase { public override void Execute(SafeSharedObjects shared) @@ -315,7 +315,7 @@ public override void Execute(SafeSharedObjects shared) } } - [Function("debugdump")] + [Function("debugdump", Contexts = new string[] { "ksp", "archive" })] public class DebugDump : SafeFunctionBase { public override void Execute(SafeSharedObjects shared) @@ -372,7 +372,7 @@ public override void Execute(SafeSharedObjects shared) } } - [Function("makebuiltindelegate")] + [Function("makebuiltindelegate", Contexts = new string[] { "ksp", "archive" })] public class MakeBuiltinDelegate : SafeFunctionBase { public override void Execute(SafeSharedObjects shared) @@ -384,7 +384,7 @@ public override void Execute(SafeSharedObjects shared) } } - [Function("droppriority")] + [Function("droppriority", Contexts = new string[] { "ksp", "archive" })] public class AllowInterrupt : SafeFunctionBase { public override void Execute(SafeSharedObjects shared) diff --git a/src/kOS.Safe/Function/Persistence.cs b/src/kOS.Safe/Function/Persistence.cs index 59a248e969..e05aab2250 100644 --- a/src/kOS.Safe/Function/Persistence.cs +++ b/src/kOS.Safe/Function/Persistence.cs @@ -12,7 +12,7 @@ namespace kOS.Safe.Function * remove these function below as well any metions of delete/rename file/rename volume/copy from kRISC.tpg in the future. */ - [Function("copy_deprecated")] + [Function("copy_deprecated", Contexts = new string[] { "ksp", "archive" })] public class FunctionCopyDeprecated : SafeFunctionBase { public override void Execute(SafeSharedObjects shared) @@ -49,7 +49,7 @@ public override void Execute(SafeSharedObjects shared) } } - [Function("rename_file_deprecated")] + [Function("rename_file_deprecated", Contexts = new string[] { "ksp", "archive" })] public class FunctionRenameFileDeprecated : SafeFunctionBase { public override void Execute(SafeSharedObjects shared) @@ -76,7 +76,7 @@ public override void Execute(SafeSharedObjects shared) } } - [Function("rename_volume_deprecated")] + [Function("rename_volume_deprecated", Contexts = new string[] { "ksp", "archive" })] public class FunctionRenameVolumeDeprecated : SafeFunctionBase { public override void Execute(SafeSharedObjects shared) @@ -99,7 +99,7 @@ public override void Execute(SafeSharedObjects shared) } } - [Function("delete_deprecated")] + [Function("delete_deprecated", Contexts = new string[] { "ksp", "archive" })] public class FunctionDeleteDeprecated : SafeFunctionBase { public override void Execute(SafeSharedObjects shared) @@ -127,7 +127,7 @@ public override void Execute(SafeSharedObjects shared) } } - [Function("scriptpath")] + [Function("scriptpath", Contexts = new string[] { "ksp", "archive" })] public class FunctionScriptPath : SafeFunctionBase { public override void Execute(SafeSharedObjects shared) @@ -164,7 +164,7 @@ public override void Execute(SafeSharedObjects shared) } } - [Function("cd", "chdir")] + [Function("cd", "chdir", Contexts = new string[] { "ksp", "archive" })] public class FunctionCd : SafeFunctionBase { public override void Execute(SafeSharedObjects shared) @@ -198,7 +198,7 @@ public override void Execute(SafeSharedObjects shared) } } - [Function("copypath")] + [Function("copypath", Contexts = new string[] { "ksp", "archive" })] public class FunctionCopyPath : SafeFunctionBase { public override void Execute(SafeSharedObjects shared) @@ -214,7 +214,7 @@ public override void Execute(SafeSharedObjects shared) } } - [Function("movepath")] + [Function("movepath", Contexts = new string[] { "ksp", "archive" })] public class FunctionMove : SafeFunctionBase { public override void Execute(SafeSharedObjects shared) @@ -230,7 +230,7 @@ public override void Execute(SafeSharedObjects shared) } } - [Function("deletepath")] + [Function("deletepath", Contexts = new string[] { "ksp", "archive" })] public class FunctionDeletePath : SafeFunctionBase { public override void Execute(SafeSharedObjects shared) @@ -245,7 +245,7 @@ public override void Execute(SafeSharedObjects shared) } } - [Function("writejson")] + [Function("writejson", Contexts = new string[] { "ksp", "archive" })] public class FunctionWriteJson : SafeFunctionBase { public override void Execute(SafeSharedObjects shared) @@ -270,7 +270,7 @@ public override void Execute(SafeSharedObjects shared) } } - [Function("readjson")] + [Function("readjson", Contexts = new string[] { "ksp", "archive" })] public class FunctionReadJson : SafeFunctionBase { public override void Execute(SafeSharedObjects shared) @@ -293,7 +293,7 @@ public override void Execute(SafeSharedObjects shared) } } - [Function("exists")] + [Function("exists", Contexts = new string[] { "ksp", "archive" })] public class FunctionExists : SafeFunctionBase { public override void Execute(SafeSharedObjects shared) @@ -308,7 +308,7 @@ public override void Execute(SafeSharedObjects shared) } } - [Function("open")] + [Function("open", Contexts = new string[] { "ksp", "archive" })] public class FunctionOpen : SafeFunctionBase { public override void Execute(SafeSharedObjects shared) @@ -328,7 +328,7 @@ public override void Execute(SafeSharedObjects shared) } } - [Function("create")] + [Function("create", Contexts = new string[] { "ksp", "archive" })] public class FunctionCreate : SafeFunctionBase { public override void Execute(SafeSharedObjects shared) @@ -345,7 +345,7 @@ public override void Execute(SafeSharedObjects shared) } } - [Function("createdir")] + [Function("createdir", Contexts = new string[] { "ksp", "archive" })] public class FunctionCreateDirectory : SafeFunctionBase { public override void Execute(SafeSharedObjects shared) diff --git a/src/kOS.Safe/Function/Suffixed.cs b/src/kOS.Safe/Function/Suffixed.cs index 3367b373d6..52c3805c9f 100644 --- a/src/kOS.Safe/Function/Suffixed.cs +++ b/src/kOS.Safe/Function/Suffixed.cs @@ -3,7 +3,7 @@ namespace kOS.Safe.Function { - [Function("range")] + [Function("range", Contexts = new string[] { "ksp", "archive" })] public class FunctionRange : SafeFunctionBase { public override void Execute(SafeSharedObjects shared) @@ -38,7 +38,7 @@ public override void Execute(SafeSharedObjects shared) } } - [Function("constant")] + [Function("constant", Contexts = new string[] { "ksp", "archive" })] public class FunctionConstant : SafeFunctionBase { public override void Execute(SafeSharedObjects shared) diff --git a/src/kOS.Safe/Function/Trigonometry.cs b/src/kOS.Safe/Function/Trigonometry.cs index f8e53cd512..4c189ccc5b 100644 --- a/src/kOS.Safe/Function/Trigonometry.cs +++ b/src/kOS.Safe/Function/Trigonometry.cs @@ -4,7 +4,7 @@ namespace kOS.Safe.Function { - [Function("sin")] + [Function("sin", Contexts = new string[] { "ksp", "archive" })] public class FunctionSin : SafeFunctionBase { public override void Execute(SafeSharedObjects shared) @@ -17,7 +17,7 @@ public override void Execute(SafeSharedObjects shared) } } - [Function("cos")] + [Function("cos", Contexts = new string[] { "ksp", "archive" })] public class FunctionCos : SafeFunctionBase { public override void Execute(SafeSharedObjects shared) @@ -30,7 +30,7 @@ public override void Execute(SafeSharedObjects shared) } } - [Function("tan")] + [Function("tan", Contexts = new string[] { "ksp", "archive" })] public class FunctionTan : SafeFunctionBase { public override void Execute(SafeSharedObjects shared) @@ -43,7 +43,7 @@ public override void Execute(SafeSharedObjects shared) } } - [Function("arcsin")] + [Function("arcsin", Contexts = new string[] { "ksp", "archive" })] public class FunctionArcSin : SafeFunctionBase { public override void Execute(SafeSharedObjects shared) @@ -55,7 +55,7 @@ public override void Execute(SafeSharedObjects shared) } } - [Function("arccos")] + [Function("arccos", Contexts = new string[] { "ksp", "archive" })] public class FunctionArcCos : SafeFunctionBase { public override void Execute(SafeSharedObjects shared) @@ -67,7 +67,7 @@ public override void Execute(SafeSharedObjects shared) } } - [Function("arctan")] + [Function("arctan", Contexts = new string[] { "ksp", "archive" })] public class FunctionArcTan : SafeFunctionBase { public override void Execute(SafeSharedObjects shared) @@ -79,7 +79,7 @@ public override void Execute(SafeSharedObjects shared) } } - [Function("arctan2")] + [Function("arctan2", Contexts = new string[] { "ksp", "archive" })] public class FunctionArcTan2 : SafeFunctionBase { public override void Execute(SafeSharedObjects shared) @@ -92,7 +92,7 @@ public override void Execute(SafeSharedObjects shared) } } - [Function("anglediff")] + [Function("anglediff", Contexts = new string[] { "ksp", "archive" })] public class FunctionAngleDiff : SafeFunctionBase { public override void Execute(SafeSharedObjects shared) diff --git a/src/kOS.Safe/Persistence/PathValue.cs b/src/kOS.Safe/Persistence/PathValue.cs index 6c10d9c0ce..5a2db28c31 100644 --- a/src/kOS.Safe/Persistence/PathValue.cs +++ b/src/kOS.Safe/Persistence/PathValue.cs @@ -18,7 +18,7 @@ namespace kOS.Safe [kOS.Safe.Utilities.KOSNomenclature("Path")] public class PathValue : SerializableStructure { - [Function("path")] + [Function("path", Contexts=new string[] { "ksp", "archive" })] public class FunctionPath : SafeFunctionBase { public override void Execute(SafeSharedObjects shared) diff --git a/src/kOS.Safe/Persistence/Volume.cs b/src/kOS.Safe/Persistence/Volume.cs index d4127657ff..388d83eb59 100644 --- a/src/kOS.Safe/Persistence/Volume.cs +++ b/src/kOS.Safe/Persistence/Volume.cs @@ -10,7 +10,7 @@ namespace kOS.Safe.Persistence [kOS.Safe.Utilities.KOSNomenclature("Volume")] public abstract class Volume : Structure { - [Function("volume")] + [Function("volume", Contexts=new string[] { "ksp", "archive" })] public class FunctionVolume : SafeFunctionBase { public override void Execute(SafeSharedObjects shared) diff --git a/src/kOS/AddOns/ArchiveMainframe/ArchiveBindings.cs b/src/kOS/AddOns/ArchiveMainframe/ArchiveBindings.cs new file mode 100644 index 0000000000..655a4ede65 --- /dev/null +++ b/src/kOS/AddOns/ArchiveMainframe/ArchiveBindings.cs @@ -0,0 +1,29 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using kOS.Safe.Binding; +using kOS.Suffixed; +using kOS.Suffixed.Part; +using kOS.Utilities; +using kOS.Module; +using kOS.Communication; +using kOS.Safe.Encapsulation; +using kOS.Safe.Encapsulation.Suffixes; + +namespace kOS.AddOns.ArchiveMainframe +{ + [Binding("archive")] + public class ArchiveMissionSettings : kOS.Binding.Binding + { + public override void AddTo(SharedObjects shared) + { + var mainframeShared = shared as SharedMainframeObjects; + if (mainframeShared != null) { + shared.BindingMgr.AddGetter("SHIP", () => mainframeShared.ArchiveShip); + shared.BindingMgr.AddGetter("CORE", () => mainframeShared.ArchiveCore); + shared.BindingMgr.AddGetter("TIME", () => new TimeStamp(Planetarium.GetUniversalTime())); + } + } + } +} diff --git a/src/kOS/AddOns/ArchiveMainframe/ArchiveConnection.cs b/src/kOS/AddOns/ArchiveMainframe/ArchiveConnection.cs new file mode 100644 index 0000000000..b410eb32eb --- /dev/null +++ b/src/kOS/AddOns/ArchiveMainframe/ArchiveConnection.cs @@ -0,0 +1,27 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using kOS.Safe.Encapsulation; +using kOS.Safe.Encapsulation.Suffixes; +using kOS.Communication; + +namespace kOS.AddOns.ArchiveMainframe +{ + [kOS.Safe.Utilities.KOSNomenclature("ArchiveConnection")] + class ArchiveConnection : Structure + { + + private readonly SharedMainframeObjects shared; + public ArchiveConnection(SharedMainframeObjects shared) + { + this.shared = shared; + AddSuffix("ISCONNECTED", new NoArgsSuffix(() => new BooleanValue(true))); + AddSuffix("DELAY", new NoArgsSuffix(() => ScalarValue.Create(0))); + AddSuffix("DESTINATION", new NoArgsSuffix(() => shared.ArchiveShip)); + if (Mainframe.instance != null) + { + AddSuffix("MESSAGES", new NoArgsSuffix(() => new MessageQueueStructure(Mainframe.instance.messageQueue, shared))); + } + } + } +} diff --git a/src/kOS/AddOns/ArchiveMainframe/ArchiveCore.cs b/src/kOS/AddOns/ArchiveMainframe/ArchiveCore.cs new file mode 100644 index 0000000000..de9e86aa12 --- /dev/null +++ b/src/kOS/AddOns/ArchiveMainframe/ArchiveCore.cs @@ -0,0 +1,30 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using kOS.Safe.Encapsulation; +using kOS.Safe.Encapsulation.Suffixes; +using kOS.Communication; +using kOS.Safe.Persistence; + +namespace kOS.AddOns.ArchiveMainframe +{ + [kOS.Safe.Utilities.KOSNomenclature("ArchiveCore")] + class ArchiveCore : Structure + { + + private readonly SharedMainframeObjects shared; + public ArchiveCore(SharedMainframeObjects shared) + { + this.shared = shared; + AddSuffix("VESSEL", new NoArgsSuffix(() => shared.ArchiveShip)); + AddSuffix("TAG", new NoArgsSuffix(() => new StringValue("Archive"))); + AddSuffix("VOLUME", new NoArgsSuffix(() => shared.VolumeMgr.CurrentVolume)); + AddSuffix("CURRENTVOLUME", new NoArgsSuffix(() => shared.VolumeMgr.CurrentVolume)); + AddSuffix("HOMECONNECTION", new NoArgsSuffix(() => shared.ArchiveConnection)); + if (Mainframe.instance != null) + { + AddSuffix("MESSAGES", new NoArgsSuffix(() => new MessageQueueStructure(Mainframe.instance.messageQueue, shared))); + } + } + } +} diff --git a/src/kOS/AddOns/ArchiveMainframe/ArchiveShip.cs b/src/kOS/AddOns/ArchiveMainframe/ArchiveShip.cs new file mode 100644 index 0000000000..85c8647eb9 --- /dev/null +++ b/src/kOS/AddOns/ArchiveMainframe/ArchiveShip.cs @@ -0,0 +1,26 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using kOS.Safe.Encapsulation; +using kOS.Safe.Encapsulation.Suffixes; +using kOS.Communication; + +namespace kOS.AddOns.ArchiveMainframe +{ + [kOS.Safe.Utilities.KOSNomenclature("ArchiveShip")] + class ArchiveShip : Structure + { + + private readonly SharedMainframeObjects shared; + public ArchiveShip(SharedMainframeObjects shared) + { + this.shared = shared; + AddSuffix("NAME", new NoArgsSuffix(() => new StringValue("Archive"))); + AddSuffix("CONNECTION", new NoArgsSuffix(() => shared.ArchiveConnection)); + if (Mainframe.instance != null) + { + AddSuffix("MESSAGES", new NoArgsSuffix(() => new MessageQueueStructure(Mainframe.instance.messageQueue, shared))); + } + } + } +} diff --git a/src/kOS/AddOns/ArchiveMainframe/Mainframe.cs b/src/kOS/AddOns/ArchiveMainframe/Mainframe.cs new file mode 100644 index 0000000000..f3e7b0d244 --- /dev/null +++ b/src/kOS/AddOns/ArchiveMainframe/Mainframe.cs @@ -0,0 +1,70 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using kOS.Safe; +using kOS.Binding; +using kOS.Safe.Compilation.KS; +using kOS.Persistence; +using kOS.Safe.Persistence; +using kOS.Communication; +using kOS.Safe.Function; +using kOS.Safe.Execution; +using kOS.Safe.Utilities; +using kOS.Callback; +using UnityEngine; + +namespace kOS.AddOns.ArchiveMainframe +{ + [KSPAddon(KSPAddon.Startup.FlightEditorAndKSC, false)] + class Mainframe : MonoBehaviour + { + public static Mainframe instance; + public static Dump queueDump; + public void Start() + { + Mainframe.instance = this; + shared = new SharedMainframeObjects(); + + shared.UpdateHandler = new UpdateHandler(); + shared.BindingMgr = new BindingManager(shared); + shared.Interpreter = new Screen.ConnectivityInterpreter(shared); + shared.Screen = shared.Interpreter; + shared.ScriptHandler = new KSScript(); + shared.Logger = new KSPLogger(shared); + shared.VolumeMgr = new ConnectivityVolumeManager(shared); + shared.ProcessorMgr = new ProcessorManager(); + shared.FunctionManager = new FunctionManager(shared); + shared.Cpu = new CPU(shared); + shared.AddonManager = new AddOns.AddonManager(shared); + shared.GameEventDispatchManager = new GameEventDispatchManager(shared); + + shared.Window = gameObject.AddComponent(); + shared.Window.VirtualCPU = true; + shared.Window.AttachTo(shared); + + processor = new MainframeProcessor(shared); + shared.Processor = processor; + + // initialize archive + shared.VolumeMgr.Add(new Archive(SafeHouse.ArchiveFolder)); + + messageQueue = new MessageQueue(); + if (queueDump != null) + messageQueue.LoadDump(queueDump); + } + + private SharedObjects shared; + public MainframeProcessor processor { get; private set; } + public MessageQueue messageQueue { get; private set; } + + public void Update() + { + processor.Update(); + } + + public void OnDestroy() + { + queueDump = messageQueue.Dump(); + } + } +} diff --git a/src/kOS/AddOns/ArchiveMainframe/MainframeProcessor.cs b/src/kOS/AddOns/ArchiveMainframe/MainframeProcessor.cs new file mode 100644 index 0000000000..88783eec59 --- /dev/null +++ b/src/kOS/AddOns/ArchiveMainframe/MainframeProcessor.cs @@ -0,0 +1,84 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using kOS.Safe; +using kOS.Safe.Module; +using kOS.Safe.Persistence; +using kOS.Safe.Utilities; + +namespace kOS.AddOns.ArchiveMainframe +{ + class MainframeProcessor : IProcessor + { + public MainframeProcessor(SharedObjects shared) + { + this.shared = shared; + } + private bool firstUpdate = true; + private SharedObjects shared; + public VolumePath BootFilePath { get { return VolumePath.FromString("boot/archive"); } } + public bool CheckCanBoot() { return true; } + public string Tag { get { return ""; } } + public int KOSCoreId { get { return -1; } } + public void SetMode(ProcessorModes newProcessorMode) + { + switch (newProcessorMode) + { + case ProcessorModes.READY: + shared.VolumeMgr.SwitchTo(shared.VolumeMgr.GetVolume(0)); + if (shared.Interpreter != null) shared.Interpreter.SetInputLock(false); + firstUpdate = true; + if (shared.Window != null) shared.Window.IsPowered = true; + foreach (var w in shared.ManagedWindows) w.IsPowered = true; + break; + case ProcessorModes.OFF: + if (shared.Cpu != null) shared.Cpu.BreakExecution(true); + if (shared.Interpreter != null) shared.Interpreter.SetInputLock(true); + if (shared.Window != null) shared.Window.IsPowered = false; + if (shared.SoundMaker != null) shared.SoundMaker.StopAllVoices(); + foreach (var w in shared.ManagedWindows) w.IsPowered = false; + break; + } + } + + public void OpenWindow() + { + shared.Window.Open(); + } + + public void CloseWindow() + { + shared.Window.Close(); + } + + public void ToggleWindow() + { + shared.Window.Toggle(); + } + + public bool WindowIsOpen() + { + return shared.Window.IsOpen; + } + + public bool TelnetIsAttached() + { + return shared.Window.NumTelnets() > 0; + } + + public void Update() + { + if (firstUpdate) + { + firstUpdate = false; + shared.Cpu.Boot(new string[] { "archive" }); + } + + // Mainframe runs in realtime, not physics time + if (shared.UpdateHandler != null) shared.UpdateHandler.UpdateObservers(TimeWarp.deltaTime); + if (shared.UpdateHandler != null) shared.UpdateHandler.UpdateFixedObservers(TimeWarp.fixedDeltaTime); + } + } +} diff --git a/src/kOS/AddOns/ArchiveMainframe/SharedMainframeObjects.cs b/src/kOS/AddOns/ArchiveMainframe/SharedMainframeObjects.cs new file mode 100644 index 0000000000..e52b1dda21 --- /dev/null +++ b/src/kOS/AddOns/ArchiveMainframe/SharedMainframeObjects.cs @@ -0,0 +1,21 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using kOS; + +namespace kOS.AddOns.ArchiveMainframe +{ + class SharedMainframeObjects : SharedObjects + { + public SharedMainframeObjects() + { + ArchiveShip = new ArchiveShip(this); + ArchiveCore = new ArchiveCore(this); + ArchiveConnection = new ArchiveConnection(this); + } + + public ArchiveShip ArchiveShip; + public ArchiveCore ArchiveCore; + public ArchiveConnection ArchiveConnection; + } +} diff --git a/src/kOS/AddOns/RemoteTech/RemoteTechConnectivityManager.cs b/src/kOS/AddOns/RemoteTech/RemoteTechConnectivityManager.cs index 842d767b9f..659cf17266 100644 --- a/src/kOS/AddOns/RemoteTech/RemoteTechConnectivityManager.cs +++ b/src/kOS/AddOns/RemoteTech/RemoteTechConnectivityManager.cs @@ -39,6 +39,10 @@ public bool NeedAutopilotResubscribe public double GetDelay(Vessel vessel1, Vessel vessel2) { + if (vessel1 == null) + return GetDelayToHome(vessel2); + if (vessel2 == null) + return GetDelayToHome(vessel1); if (!(RemoteTechHook.IsAvailable())) return -1; // default to no connection if RT itself isn't available. double delay = RemoteTechHook.Instance.GetSignalDelayToSatellite(vessel1.id, vessel2.id); @@ -47,6 +51,9 @@ public double GetDelay(Vessel vessel1, Vessel vessel2) public double GetDelayToControl(Vessel vessel) { + // Archive Mainframe <-> Home = 0ms + if (vessel == null) + return 0; if (!RemoteTechHook.IsAvailable()) return -1; // default to no connection if RT itself isn't available. if (RemoteTechHook.Instance.HasLocalControl(vessel.id)) return 0d; @@ -56,6 +63,9 @@ public double GetDelayToControl(Vessel vessel) public double GetDelayToHome(Vessel vessel) { + // Archive Mainframe <-> Home = 0ms + if (vessel == null) + return 0; if (!RemoteTechHook.IsAvailable()) return -1; // default to no connection if RT itself isn't available. double delay = RemoteTechHook.Instance.GetSignalDelayToKSC(vessel.id); @@ -70,6 +80,8 @@ public bool HasConnection(Vessel vessel1, Vessel vessel2) public bool HasConnectionToHome(Vessel vessel) { + if (vessel == null) + return true; if (!RemoteTechHook.IsAvailable()) return false; // default to no connection if RT itself isn't available. return RemoteTechHook.Instance.HasConnectionToKSC(vessel.id); @@ -77,6 +89,8 @@ public bool HasConnectionToHome(Vessel vessel) public bool HasConnectionToControl(Vessel vessel) { + if (vessel == null) + return true; if (!RemoteTechHook.IsAvailable()) return vessel.CurrentControlLevel >= Vessel.ControlLevel.PARTIAL_MANNED; // default to checking for local control if RT itself isn't available. return RemoteTechHook.Instance.HasAnyConnection(vessel.id) || RemoteTechHook.Instance.HasLocalControl(vessel.id); diff --git a/src/kOS/Binding/BindingConfig.cs b/src/kOS/Binding/BindingConfig.cs index f5b80048e0..b6c48a32d1 100644 --- a/src/kOS/Binding/BindingConfig.cs +++ b/src/kOS/Binding/BindingConfig.cs @@ -4,7 +4,7 @@ namespace kOS.Binding { - [Binding("ksp")] + [Binding("ksp", "archive")] public class BindingConfig : Binding { public override void AddTo(SharedObjects shared) diff --git a/src/kOS/Binding/BindingManager.cs b/src/kOS/Binding/BindingManager.cs index 20aee02e5f..d66671b876 100644 --- a/src/kOS/Binding/BindingManager.cs +++ b/src/kOS/Binding/BindingManager.cs @@ -30,14 +30,10 @@ public BindingManager(SharedObjects shared) { variables = new Dictionary(StringComparer.OrdinalIgnoreCase); this.shared = shared; - this.shared.BindingMgr = this; } - public void Load() + public void Load(string[] contexts) { - var contexts = new string[1]; - contexts[0] = "ksp"; - bindings.Clear(); variables.Clear(); flightControl = null; diff --git a/src/kOS/Binding/ColorBinding.cs b/src/kOS/Binding/ColorBinding.cs index 86cb98bb1c..1b7a585c71 100644 --- a/src/kOS/Binding/ColorBinding.cs +++ b/src/kOS/Binding/ColorBinding.cs @@ -3,7 +3,7 @@ namespace kOS.Binding { - [Binding("ksp")] + [Binding("ksp", "archive")] public class ColorBinding : Binding { public override void AddTo(SharedObjects shared) diff --git a/src/kOS/Binding/DoNothingBinding.cs b/src/kOS/Binding/DoNothingBinding.cs index b89c6ce40c..301110cf17 100644 --- a/src/kOS/Binding/DoNothingBinding.cs +++ b/src/kOS/Binding/DoNothingBinding.cs @@ -4,7 +4,7 @@ namespace kOS.Binding { - [Binding("ksp")] + [Binding("ksp", "archive")] public class DoNothingBinding : Binding { private NoDelegate doNothingInstance = null; diff --git a/src/kOS/Binding/TerminalSettings.cs b/src/kOS/Binding/TerminalSettings.cs index 14f0010c8c..37848439c8 100644 --- a/src/kOS/Binding/TerminalSettings.cs +++ b/src/kOS/Binding/TerminalSettings.cs @@ -3,7 +3,7 @@ namespace kOS.Binding { - [Binding("ksp")] + [Binding("ksp", "archive")] public class TerminalSettings : Binding { private TerminalStruct terminalStructInstance = null; diff --git a/src/kOS/Communication/CommNetConnectivityManager.cs b/src/kOS/Communication/CommNetConnectivityManager.cs index 7f266e3a28..81afddf30c 100644 --- a/src/kOS/Communication/CommNetConnectivityManager.cs +++ b/src/kOS/Communication/CommNetConnectivityManager.cs @@ -117,6 +117,10 @@ public bool HasConnection(Vessel vessel1, Vessel vessel2) { if (!IsEnabled) return true; + if (vessel1 == null) + return HasConnectionToControl(vessel2); + if (vessel2 == null) + return HasConnectionToControl(vessel1); if (!IsCommnetParticipant(vessel1) || !IsCommnetParticipant(vessel2)) return false; diff --git a/src/kOS/Communication/HomeConnection.cs b/src/kOS/Communication/HomeConnection.cs index 474c675ddd..40368422e0 100644 --- a/src/kOS/Communication/HomeConnection.cs +++ b/src/kOS/Communication/HomeConnection.cs @@ -1,5 +1,7 @@ -using kOS.Safe.Communication; +using kOS.Safe.Communication; using kOS.Safe.Encapsulation; +using kOS.Suffixed; +using kOS.AddOns.ArchiveMainframe; namespace kOS.Communication { @@ -36,7 +38,14 @@ protected override Structure Destination() protected override BooleanValue SendMessage(Structure content) { - throw new Safe.Exceptions.KOSException("kOS does not support sending messages to HOME (KSC in Stock)"); + if (Mainframe.instance == null) + throw new Safe.Exceptions.KOSException("KSC Mainframe not responding."); + double sentAt = Planetarium.GetUniversalTime(); + VesselTarget sender = null; + if (!(shared is SharedMainframeObjects)) + sender = VesselTarget.CreateOrGetExisting(shared); + Mainframe.instance.messageQueue.Push(Message.Create(content, sentAt, sentAt, sender, "archive")); + return true; } } } \ No newline at end of file diff --git a/src/kOS/Communication/Message.cs b/src/kOS/Communication/Message.cs index 6da8007b18..46cf47f786 100644 --- a/src/kOS/Communication/Message.cs +++ b/src/kOS/Communication/Message.cs @@ -42,13 +42,19 @@ private Message() public Message(Dump content, double sentAt, double receivedAt, VesselTarget sender) : base(content, sentAt, receivedAt) { - Vessel = sender.Guid.ToString(); + if (sender == null) + Vessel = "Archive"; + else + Vessel = sender.Guid.ToString(); } public Message(PrimitiveStructure content, double sentAt, double receivedAt, VesselTarget sender) : base(content, sentAt, receivedAt) { - Vessel = sender.Guid.ToString(); + if (sender == null) + Vessel = "Archive"; + else + Vessel = sender.Guid.ToString(); } // Required for all IDumpers for them to work, but can't enforced by the interface because it's static: diff --git a/src/kOS/Function/Math.cs b/src/kOS/Function/Math.cs index 8850e087e7..4a5b844bfa 100644 --- a/src/kOS/Function/Math.cs +++ b/src/kOS/Function/Math.cs @@ -1,4 +1,4 @@ -using System; +using System; using kOS.Safe.Compilation; using kOS.Safe.Function; using kOS.Suffixed; @@ -7,7 +7,7 @@ namespace kOS.Function { - [Function("vcrs", "vectorcrossproduct")] + [Function("vcrs", "vectorcrossproduct", Contexts=new string[] { "ksp", "archive" })] public class FunctionVectorCross : FunctionBase { public override void Execute(SharedObjects shared) @@ -26,7 +26,7 @@ public override void Execute(SharedObjects shared) } } - [Function("vdot", "vectordotproduct")] + [Function("vdot", "vectordotproduct", Contexts=new string[] { "ksp", "archive" })] public class FunctionVectorDot : FunctionBase { public override void Execute(SharedObjects shared) @@ -45,7 +45,7 @@ public override void Execute(SharedObjects shared) } } - [Function("vxcl", "vectorexclude")] + [Function("vxcl", "vectorexclude", Contexts=new string[] { "ksp", "archive" })] public class FunctionVectorExclude : FunctionBase { public override void Execute(SharedObjects shared) @@ -64,7 +64,7 @@ public override void Execute(SharedObjects shared) } } - [Function("vang", "vectorangle")] + [Function("vang", "vectorangle", Contexts=new string[] { "ksp", "archive" })] public class FunctionVectorAngle : FunctionBase { public override void Execute(SharedObjects shared) diff --git a/src/kOS/Function/Misc.cs b/src/kOS/Function/Misc.cs index eb32c6619f..cc72ae3d80 100644 --- a/src/kOS/Function/Misc.cs +++ b/src/kOS/Function/Misc.cs @@ -19,7 +19,7 @@ namespace kOS.Function { - [Function("clearscreen")] + [Function("clearscreen", Contexts=new string[] { "ksp", "archive" })] public class FunctionClearScreen : FunctionBase { public override void Execute(SharedObjects shared) @@ -29,7 +29,7 @@ public override void Execute(SharedObjects shared) } } - [Function("hudtext")] + [Function("hudtext", Contexts=new string[] { "ksp", "archive" })] public class FunctionHudText : FunctionBase { public override void Execute(SharedObjects shared) @@ -115,7 +115,7 @@ public override void Execute(SharedObjects shared) } } - [Function("warpto")] + [Function("warpto", Contexts=new string[] { "ksp", "archive" })] public class WarpTo : FunctionBase { public override void Execute(SharedObjects shared) diff --git a/src/kOS/Function/Persistence.cs b/src/kOS/Function/Persistence.cs index 4b21299740..98c1b55891 100644 --- a/src/kOS/Function/Persistence.cs +++ b/src/kOS/Function/Persistence.cs @@ -13,7 +13,7 @@ namespace kOS.Function { - [Function("edit")] + [Function("edit", Contexts=new string[] { "ksp", "archive" })] public class FunctionEdit : FunctionBase { public override void Execute(SharedObjects shared) diff --git a/src/kOS/Function/PrintList.cs b/src/kOS/Function/PrintList.cs index 0667db142c..396d5cf10a 100644 --- a/src/kOS/Function/PrintList.cs +++ b/src/kOS/Function/PrintList.cs @@ -16,7 +16,7 @@ namespace kOS.Function { - [Function("printlist")] + [Function("printlist", Contexts=new string[] { "ksp", "archive" })] public class FunctionPrintList : FunctionBase { public override void Execute(SharedObjects shared) diff --git a/src/kOS/Function/Suffixed.cs b/src/kOS/Function/Suffixed.cs index 3ffb2ab308..08b2134f6b 100644 --- a/src/kOS/Function/Suffixed.cs +++ b/src/kOS/Function/Suffixed.cs @@ -40,7 +40,7 @@ public override void Execute(SharedObjects shared) } } - [Function("v")] + [Function("v", Contexts=new string[] { "ksp", "archive" })] public class FunctionVector : FunctionBase { public override void Execute(SharedObjects shared) @@ -55,7 +55,7 @@ public override void Execute(SharedObjects shared) } } - [Function("r")] + [Function("r", Contexts=new string[] { "ksp", "archive" })] public class FunctionRotation : FunctionBase { public override void Execute(SharedObjects shared) @@ -70,7 +70,7 @@ public override void Execute(SharedObjects shared) } } - [Function("q")] + [Function("q", Contexts=new string[] { "ksp", "archive" })] public class FunctionQuaternion : FunctionBase { public override void Execute(SharedObjects shared) @@ -86,7 +86,7 @@ public override void Execute(SharedObjects shared) } } - [Function("createOrbit")] + [Function("createOrbit", Contexts=new string[] { "ksp", "archive" })] public class FunctionCreateOrbit : FunctionBase { public override void Execute(SharedObjects shared) @@ -131,7 +131,7 @@ public override void Execute(SharedObjects shared) } } - [Function("rotatefromto")] + [Function("rotatefromto", Contexts=new string[] { "ksp", "archive" })] public class FunctionRotateFromTo : FunctionBase { public override void Execute(SharedObjects shared) @@ -145,7 +145,7 @@ public override void Execute(SharedObjects shared) } } - [Function("lookdirup")] + [Function("lookdirup", Contexts=new string[] { "ksp", "archive" })] public class FunctionLookDirUp : FunctionBase { public override void Execute(SharedObjects shared) @@ -159,7 +159,7 @@ public override void Execute(SharedObjects shared) } } - [Function("angleaxis")] + [Function("angleaxis", Contexts=new string[] { "ksp", "archive" })] public class FunctionAngleAxis : FunctionBase { public override void Execute(SharedObjects shared) @@ -173,7 +173,7 @@ public override void Execute(SharedObjects shared) } } - [Function("latlng")] + [Function("latlng", Contexts=new string[] { "ksp", "archive" })] public class FunctionLatLng : FunctionBase { public override void Execute(SharedObjects shared) @@ -187,7 +187,7 @@ public override void Execute(SharedObjects shared) } } - [Function("vessel")] + [Function("vessel", Contexts=new string[] { "ksp", "archive" })] public class FunctionVessel : FunctionBase { public override void Execute(SharedObjects shared) @@ -199,7 +199,7 @@ public override void Execute(SharedObjects shared) } } - [Function("body")] + [Function("body", Contexts=new string[] { "ksp", "archive" })] public class FunctionBody : FunctionBase { public override void Execute(SharedObjects shared) @@ -211,7 +211,7 @@ public override void Execute(SharedObjects shared) } } - [Function("bodyexists")] + [Function("bodyexists", Contexts=new string[] { "ksp", "archive" })] public class FunctionBodyExists : FunctionBase { public override void Execute(SharedObjects shared) @@ -222,7 +222,7 @@ public override void Execute(SharedObjects shared) } } - [Function("bodyatmosphere")] + [Function("bodyatmosphere", Contexts=new string[] { "ksp", "archive" })] public class FunctionBodyAtmosphere : FunctionBase { public override void Execute(SharedObjects shared) @@ -237,7 +237,7 @@ public override void Execute(SharedObjects shared) } } - [Function("bounds")] + [Function("bounds", Contexts=new string[] { "ksp", "archive" })] public class FunctionBounds : FunctionBase { public override void Execute(SharedObjects shared) @@ -274,7 +274,7 @@ public override void Execute(SharedObjects shared) } } - [Function("slidenote")] + [Function("slidenote", Contexts=new string[] { "ksp", "archive" })] public class FunctionSlideNote : FunctionBase { public override void Execute(SharedObjects shared) @@ -304,7 +304,7 @@ public override void Execute(SharedObjects shared) } } - [Function("note")] + [Function("note", Contexts=new string[] { "ksp", "archive" })] public class FunctionNote : FunctionBase { public override void Execute(SharedObjects shared) @@ -333,7 +333,7 @@ public override void Execute(SharedObjects shared) } } - [Function("GetVoice")] + [Function("GetVoice", Contexts=new string[] { "ksp", "archive" })] public class FunctionGetVoice : FunctionBase { public override void Execute(SharedObjects shared) @@ -354,7 +354,7 @@ public override void Execute(SharedObjects shared) } - [Function("StopAllVoices")] + [Function("StopAllVoices", Contexts=new string[] { "ksp", "archive" })] public class FunctionStopAllVoices : FunctionBase { public override void Execute(SharedObjects shared) @@ -362,7 +362,7 @@ public override void Execute(SharedObjects shared) shared.SoundMaker.StopAllVoices(); } } - [Function("timestamp", "time")] + [Function("timestamp", "time", Contexts=new string[] { "ksp", "archive" })] public class FunctionTimeStamp : FunctionBase { // Note: "TIME" is both a bound variable AND a built-in function now. @@ -426,7 +426,7 @@ public override void Execute(SharedObjects shared) } } - [Function("timespan")] + [Function("timespan", Contexts=new string[] { "ksp", "archive" })] public class FunctionTimeSpan : FunctionBase { public override void Execute(SharedObjects shared) @@ -477,7 +477,7 @@ public override void Execute(SharedObjects shared) } } - [Function("hsv")] + [Function("hsv", Contexts=new string[] { "ksp", "archive" })] public class FunctionHsv : FunctionBase { public override void Execute(SharedObjects shared) @@ -490,7 +490,7 @@ public override void Execute(SharedObjects shared) } } - [Function("hsva")] + [Function("hsva", Contexts=new string[] { "ksp", "archive" })] public class FunctionHsva : FunctionBase { public override void Execute(SharedObjects shared) @@ -504,7 +504,7 @@ public override void Execute(SharedObjects shared) } } - [Function("rgb")] + [Function("rgb", Contexts=new string[] { "ksp", "archive" })] public class FunctionRgb : FunctionBase { public override void Execute(SharedObjects shared) @@ -517,7 +517,7 @@ public override void Execute(SharedObjects shared) } } - [Function("rgba")] + [Function("rgba", Contexts=new string[] { "ksp", "archive" })] public class FunctionRgba : FunctionBase { public override void Execute(SharedObjects shared) @@ -639,7 +639,7 @@ public override void Execute(SharedObjects shared) } } - [Function("clearguis")] + [Function("clearguis", Contexts=new string[] { "ksp", "archive" })] public class FunctionClearAllGuis : FunctionBase { public override void Execute(SharedObjects shared) @@ -649,7 +649,7 @@ public override void Execute(SharedObjects shared) } } - [Function("gui")] + [Function("gui", Contexts=new string[] { "ksp", "archive" })] public class FunctionWidgets : FunctionBase { public override void Execute(SharedObjects shared) @@ -714,7 +714,7 @@ public override void Execute(SharedObjects shared) } } - [Function("career")] + [Function("career", Contexts=new string[] { "ksp", "archive" })] public class FunctionCareer : FunctionBase { public override void Execute(SharedObjects shared) @@ -724,7 +724,7 @@ public override void Execute(SharedObjects shared) } } - [Function("allwaypoints")] + [Function("allwaypoints", Contexts=new string[] { "ksp", "archive" })] public class FunctionAllWaypoints : FunctionBase { public override void Execute(SharedObjects shared) @@ -758,7 +758,7 @@ public override void Execute(SharedObjects shared) } } - [Function("waypoint")] + [Function("waypoint", Contexts=new string[] { "ksp", "archive" })] public class FunctionWaypoint : FunctionBase { public override void Execute(SharedObjects shared) diff --git a/src/kOS/Persistence/ConnectivityVolumeManager.cs b/src/kOS/Persistence/ConnectivityVolumeManager.cs index 82723c4146..038d7bd6a3 100644 --- a/src/kOS/Persistence/ConnectivityVolumeManager.cs +++ b/src/kOS/Persistence/ConnectivityVolumeManager.cs @@ -1,4 +1,4 @@ -using kOS.Safe.Exceptions; +using kOS.Safe.Exceptions; using kOS.Safe.Persistence; namespace kOS.Persistence @@ -39,6 +39,11 @@ public override bool CheckRange(Volume volume) var archive = volume as Archive; if (archive != null) { + if (shared.Vessel == null) + { + // We're a floating cpu and therefore part of the KSC mainframe. + return true; + } if (shared.Vessel.situation == Vessel.Situations.PRELAUNCH || Communication.ConnectivityManager.HasConnectionToHome(shared.Vessel)) return true; } diff --git a/src/kOS/Screen/Interpreter.cs b/src/kOS/Screen/Interpreter.cs index bdd93e0315..0f7088768d 100644 --- a/src/kOS/Screen/Interpreter.cs +++ b/src/kOS/Screen/Interpreter.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.Collections.Generic; using System.Text; using kOS.Execution; @@ -162,6 +162,8 @@ public bool IsWaitingForCommand() // If running from a boot script, there will be no interpreter instructions, // only a single OpcodeEOF. So we check to see if the interpreter is locked, // which is a sign that a sub-program is running. + if (context == null || context.InstructionPointer >= context.Program.Count || context.InstructionPointer < 0) + return !locked; return !locked && context.Program[context.InstructionPointer] is OpcodeEOF; } diff --git a/src/kOS/Screen/KOSToolbarWindow.cs b/src/kOS/Screen/KOSToolbarWindow.cs index af9d725972..22981e1861 100644 --- a/src/kOS/Screen/KOSToolbarWindow.cs +++ b/src/kOS/Screen/KOSToolbarWindow.cs @@ -4,6 +4,7 @@ using kOS.Safe.Utilities; using kOS.UserIO; using kOS.Utilities; +using kOS.AddOns.ArchiveMainframe; using KSP.UI.Screens; using System; using System.Globalization; @@ -712,6 +713,10 @@ private void DrawActiveCPUsOnPanel() } DrawPartRow(thisPart); } + if (Mainframe.instance != null) + { + DrawMainframeRow(Mainframe.instance); + } if (!atLeastOne) GUILayout.Label("No Loaded CPUs Found.\n" + "-------------------------\n" + @@ -777,6 +782,28 @@ private void DrawPartRow(Part part) CheckHoverOnPreviousGUIElement(part); } + private void DrawMainframeRow(Mainframe mainframe) + { + CountBeginHorizontal(); + + GUILayout.Box(new GUIContent("Archive\nMainframe", ""), partNameStyle); + + var processorModule = mainframe.processor; + GUIStyle powerBoxStyle = boxOnStyle; + string powerLabelText = "power\non"; + string powerLabelTooltip = "Mainframe is turned on and running.\n"; + + GUILayout.Box(new GUIContent(powerLabelText, powerLabelTooltip), powerBoxStyle); + if (GUILayout.Button((processorModule.WindowIsOpen() ? + new GUIContent((processorModule.TelnetIsAttached() ? terminalOpenTelnetIconTexture : terminalOpenIconTexture), + "Click to close terminal window.") : + new GUIContent((processorModule.TelnetIsAttached() ? terminalClosedTelnetIconTexture : terminalClosedIconTexture), + "Click to open terminal window.")), + panelSkin.button)) + processorModule.ToggleWindow(); + + CountEndHorizontal(); + } private void DrawPart(Part part) { diff --git a/src/kOS/Screen/TermWindow.cs b/src/kOS/Screen/TermWindow.cs index 41a6d9a26f..48881c7607 100644 --- a/src/kOS/Screen/TermWindow.cs +++ b/src/kOS/Screen/TermWindow.cs @@ -136,6 +136,7 @@ public TermWindow() } public bool ShowCursor { get; set; } + public bool VirtualCPU { get; set; } public void Awake() { @@ -352,10 +353,13 @@ void OnGUI() void Update() { - if (shared == null || shared.Vessel == null || shared.Vessel.parts.Count == 0) + if (!VirtualCPU) { - // Holding onto a vessel instance that no longer exists? - Close(); + if (shared == null || shared.Vessel == null || shared.Vessel.parts.Count == 0) + { + // Holding onto a vessel instance that no longer exists? + Close(); + } } GetNewestBuffer(); TelnetOutputUpdate(); @@ -1124,8 +1128,10 @@ internal void AttachTo(SharedObjects sharedObj) internal string CalcualteTitle() { - KOSNameTag partTag = shared.KSPPart.Modules.OfType().FirstOrDefault(); - return String.Format("{0} CPU: {1} ({2})", + if (shared.KSPPart == null) + return "CPU"; + KOSNameTag partTag = shared.KSPPart.Modules.OfType().FirstOrDefault(); + return String.Format("{0} CPU: {1} ({2})", shared.Vessel.vesselName, shared.KSPPart.partInfo.title.Split(' ')[0], // just the first word of the name, i.e "CX-4181" ((partTag==null) ? "" : partTag.nameTag) diff --git a/src/kOS/kOS.csproj b/src/kOS/kOS.csproj index 59bea5c557..33ffccdfcd 100644 --- a/src/kOS/kOS.csproj +++ b/src/kOS/kOS.csproj @@ -94,6 +94,13 @@ + + + + + + +