public override void Execute(SafeSharedObjects shared) { object arg3 = PopValueAssert(shared, true); object arg2 = PopValueAssert(shared, true); object arg1 = PopValueAssert(shared, true); AssertArgBottomAndConsume(shared); string fromName, toName; if (arg2.ToString() == "from") { fromName = arg3.ToString() + ":/" + arg1.ToString(); toName = ""; } else { fromName = arg1.ToString(); toName = arg3.ToString() + ":/" + fromName; } shared.Logger.LogWarningAndScreen( string.Format("WARNING: COPY {0} {1} {2} is deprecated as of kOS v1.0.0. Use COPYPATH(\"{3}\", \"{4}\") instead.", arg1.ToString(), arg2.ToString(), arg3.ToString(), fromName, toName)); // Redirect into a call to the copypath function, so as to keep all // the copy file logic there in one unified location. This is slightly slow, // but we don't care because this is just to support deprecation: shared.Cpu.PushArgumentStack(new kOS.Safe.Execution.KOSArgMarkerType()); shared.Cpu.PushArgumentStack(fromName); shared.Cpu.PushArgumentStack(toName); shared.Cpu.CallBuiltinFunction("copypath"); }
public override void Execute(SafeSharedObjects shared) { object volumeId = PopValueAssert(shared, true); object fileName = PopValueAssert(shared, true); AssertArgBottomAndConsume(shared); string pathName; if (volumeId != null) { pathName = volumeId.ToString() + ":/" + fileName.ToString(); } else { pathName = fileName.ToString(); } shared.Logger.LogWarningAndScreen( string.Format("WARNING: DELETE {0}{1} is deprecated as of kOS v1.0.0. Use DELETEPATH(\"{2}\") instead.", fileName.ToString(), (volumeId == null ? "" : (" FROM " + volumeId.ToString())), pathName)); // Redirect into a call to the deletepath function, so as to keep all // the file logic there in one unified location. This is slightly slow, // but we don't care because this is just to support deprecation: shared.Cpu.PushArgumentStack(new kOS.Safe.Execution.KOSArgMarkerType()); shared.Cpu.PushArgumentStack(pathName); shared.Cpu.CallBuiltinFunction("deletepath"); }
// Required for all IDumpers for them to work, but can't enforced by the interface because it's static: public static QueueValue <T> CreateFromDump(SafeSharedObjects shared, Dump d) { var newObj = new QueueValue <T>(); newObj.LoadDump(d); return(newObj); }
public override void Execute(SafeSharedObjects shared) { int remaining = CountRemainingArgs(shared); VolumeDirectory directory; if (remaining == 0) { directory = shared.VolumeMgr.CurrentVolume.Root; } else { object pathObject = PopValueAssert(shared, true); GlobalPath path = shared.VolumeMgr.GlobalPathFromObject(pathObject); Volume volume = shared.VolumeMgr.GetVolumeFromPath(path); directory = volume.Open(path) as VolumeDirectory; if (directory == null) { throw new KOSException("Invalid directory: " + pathObject); } } AssertArgBottomAndConsume(shared); shared.VolumeMgr.CurrentDirectory = directory; }
public static TimeSpan CreateFromDump(SafeSharedObjects shared, Dump d) { var newObj = new TimeSpan(); newObj.LoadDump(d); return(newObj); }
// Required for all IDumpers for them to work, but can't enforced by the interface because it's static: public static FileContent CreateFromDump(SafeSharedObjects shared, Dump d) { var newObj = new FileContent(); newObj.LoadDump(d); return(newObj); }
// Required for all IDumpers for them to work, but can't enforced by the interface because it's static: public static PIDLoop CreateFromDump(SafeSharedObjects shared, Dump d) { var newObj = new PIDLoop(); newObj.LoadDump(d); return(newObj); }
// Required for all IDumpers for them to work, but can't enforced by the interface because it's static: public static Message CreateFromDump(SafeSharedObjects shared, Dump d) { var newObj = new Message(); newObj.LoadDump(d); return(newObj); }
// Required for all IDumpers for them to work, but can't enforced by the interface because it's static: public static GenericMessageQueue <M, TP> CreateFromDump(SafeSharedObjects shared, Dump d) { var newObj = new GenericMessageQueue <M, TP>(); newObj.LoadDump(d); return(newObj); }
public override void Execute(SafeSharedObjects shared) { int decimals; int argCount = CountRemainingArgs(shared); switch (argCount) { case 1: decimals = 0; break; case 2: decimals = GetInt(PopValueAssert(shared)); break; default: throw new KOSArgumentMismatchException(new [] { 1, 2 }, argCount); } double argument = GetDouble(PopValueAssert(shared)); AssertArgBottomAndConsume(shared); double result = Math.Round(argument, decimals); ReturnValue = result; }
// Required for all IDumpers for them to work, but can't enforced by the interface because it's static: public static RgbaColor CreateFromDump(SafeSharedObjects shared, Dump d) { var newObj = new RgbaColor(); newObj.LoadDump(d); return(newObj); }
// Required for all IDumpers for them to work, but can't enforced by the interface because it's static: public static ScalarDoubleValue CreateFromDump(SafeSharedObjects shared, Dump d) { var newObj = new ScalarDoubleValue(); newObj.LoadDump(d); return(newObj); }
public override void Execute(SafeSharedObjects shared) { Structure argument1 = PopStructureAssertEncapsulated(shared); Structure argument2 = PopStructureAssertEncapsulated(shared); AssertArgBottomAndConsume(shared); Type scalarCompare = typeof(ScalarValue); Type stringCompare = typeof(StringValue); if (scalarCompare.IsInstanceOfType(argument1) && scalarCompare.IsInstanceOfType(argument2)) { double d1 = ((ScalarValue)argument1).GetDoubleValue(); double d2 = ((ScalarValue)argument2).GetDoubleValue(); ReturnValue = Math.Max(d1, d2); } else if (stringCompare.IsInstanceOfType(argument1) && stringCompare.IsInstanceOfType(argument2)) { string arg1 = argument1.ToString(); string arg2 = argument2.ToString(); int compareNum = string.Compare(arg1, arg2, StringComparison.OrdinalIgnoreCase); ReturnValue = (compareNum > 0) ? arg1 : arg2; } else { throw new KOSException("Argument Mismatch: the function MAX only accepts matching arguments of type Scalar or String"); } }
/// <summary> /// Constructor. /// </summary> /// <param name="options">Options for the compiler.</param> public CompileAction(CompileOptions options, bool usedExternally = false) { Opcode.InitMachineCodeData(); CompiledObject.InitTypeData(); SafeSerializationMgr.CheckIDumperStatics(); _usedExternally = usedExternally; _options = options; _shared = new SafeSharedObjects(); _shared.FunctionManager = new FunctionManager(_shared); _shared.GameEventDispatchManager = new NoopGameEventDispatchManager(); _shared.Processor = new NoopProcessor(); _shared.ScriptHandler = new KSScript(); _shared.Screen = new Screen(); _shared.UpdateHandler = new UpdateHandler(); _shared.VolumeMgr = new VolumeManager(); _shared.FunctionManager.Load(); _compilerOptions = new CompilerOptions() { LoadProgramsInSameAddressSpace = false, IsCalledFromRun = false, FuncManager = _shared.FunctionManager }; _logger = new CompilerLogger(); _scriptLoader = new KerboscriptLoader(_shared.VolumeMgr as VolumeManager, _logger, _options); _scriptDeleter = new KerboscriptDeleter(_options); }
// Required for all IDumpers for them to work, but can't enforced by the interface because it's static: public static BooleanValue CreateFromDump(SafeSharedObjects shared, Dump d) { var newObj = new BooleanValue(); newObj.LoadDump(d); return(newObj); }
public override void Execute(SafeSharedObjects shared) { string textToPrint = PopValueAssert(shared).ToString(); AssertArgBottomAndConsume(shared); shared.Screen.Print(textToPrint); }
public override void Execute(SafeSharedObjects shared) { string autopilotMode = PopValueAssert(shared).ToString(); AssertArgBottomAndConsume(shared); ((CPU)shared.Cpu).SelectAutopilotMode(autopilotMode); }
public override void Execute(SafeSharedObjects shared) { string pathString = PopValueAssert(shared, true).ToString(); string toAppend = PopValueAssert(shared).ToString(); AssertArgBottomAndConsume(shared); if (shared.VolumeMgr != null) { GlobalPath path = shared.VolumeMgr.GlobalPathFromObject(pathString); Volume volume = shared.VolumeMgr.GetVolumeFromPath(path); VolumeItem volumeItem = volume.Open(path) as VolumeFile; VolumeFile volumeFile = null; if (volumeItem == null) { volumeFile = volume.CreateFile(path); } else if (volumeItem is VolumeDirectory) { throw new KOSFileException("Can't append to file: path points to a directory"); } else { volumeFile = volumeItem as VolumeFile; } if (!volumeFile.WriteLn(toAppend)) { throw new KOSFileException("Can't append to file: not enough space or access forbidden"); } } }
// Required for all IDumpers for them to work, but can't enforced by the interface because it's static: public static Lexicon CreateFromDump(SafeSharedObjects shared, Dump d) { var newObj = new Lexicon(); newObj.LoadDump(d); return(newObj); }
/// <summary> /// A utility function that a function's Execute() may use if it wishes to, to get a count of /// how many args passed to it that it has not yet consumed still remain on the stack. /// </summary> /// <param name="shared"></param> /// <returns>Number of args as yet unpopped. returns zero if there are no args, or -1 if there's a bug and the argstart marker is missing.</returns> protected int CountRemainingArgs(SafeSharedObjects shared) { int depth = 0; bool found = false; bool stillInStack = true; while (stillInStack && !found) { object peekItem = shared.Cpu.PeekRaw(depth, out stillInStack); if (stillInStack && peekItem != null && peekItem.GetType() == OpcodeCall.ArgMarkerType) { found = true; } else { ++depth; } } if (found) { return(depth); } else { return(-1); } }
public override void Execute(SafeSharedObjects shared) { // Default values for parameters int from = RangeValue.DEFAULT_START; int to = RangeValue.DEFAULT_STOP; int step = RangeValue.DEFAULT_STEP; int argCount = CountRemainingArgs(shared); // assign parameter values from the stack, pop them in reverse order switch (argCount) { case 1: to = GetInt(PopStructureAssertEncapsulated(shared)); break; case 2: to = GetInt(PopStructureAssertEncapsulated(shared)); from = GetInt(PopStructureAssertEncapsulated(shared)); break; case 3: step = GetInt(PopStructureAssertEncapsulated(shared)); to = GetInt(PopStructureAssertEncapsulated(shared)); from = GetInt(PopStructureAssertEncapsulated(shared)); break; default: throw new KOSArgumentMismatchException(new int[] { 1, 2, 3 }, argCount, "Thrown from function RANGE()"); } AssertArgBottomAndConsume(shared); ReturnValue = new RangeValue(from, to, step); }
public override void Execute(SafeSharedObjects shared) { int remaining = CountRemainingArgs(shared); Volume volume; if (remaining == 0) { volume = shared.VolumeMgr.CurrentVolume; } else { object volumeId = PopValueAssert(shared, true); volume = shared.VolumeMgr.GetVolume(volumeId); if (volume == null) { throw new KOSPersistenceException("Could not find volume: " + volumeId); } } AssertArgBottomAndConsume(shared); ReturnValue = volume; }
public override void Execute(SafeSharedObjects shared) { string name = PopValueAssert(shared).ToString(); AssertArgBottomAndConsume(shared); ReturnValue = new BuiltinDelegate(shared.Cpu, name); }
public override void Execute(SafeSharedObjects shared) { bool enabled = Convert.ToBoolean(PopValueAssert(shared)); string paramName = PopValueAssert(shared).ToString(); AssertArgBottomAndConsume(shared); ((CPU)shared.Cpu).ToggleFlyByWire(paramName, enabled); }
public override void Execute(SafeSharedObjects shared) { // How many milliseconds of extra sleep to cause? int ms = GetInt(PopValueAssert(shared)); AssertArgBottomAndConsume(shared); System.Threading.Thread.Sleep(ms); }
public override void ThreadInitialize(SafeSharedObjects shared) { if (compileMode != CompileMode.FILE) { programContext = shared.Cpu.SwitchToProgramContext(); // only switch the context if executing } codeParts = new List <CodePart>(); }
public override void Execute(SafeSharedObjects shared) { int seed = GetInt(PopValueAssert(shared)); string key = PopValueAssert(shared).ToString(); AssertArgBottomAndConsume(shared); Utilities.KOSMath.StartRandomFromSeed(key, seed); }
// Required for all IDumpers for them to work, but can't enforced by the interface because it's static: public static GeoCoordinates CreateFromDump(SafeSharedObjects shared, Dump d) { var newObj = new GeoCoordinates(); newObj.Shared = (SharedObjects)shared; newObj.LoadDump(d); return(newObj); }
public override void Execute(SafeSharedObjects shared) { AssertArgBottomAndConsume(shared); int currentOpcode = shared.Cpu.GetCallTrace()[0]; Opcode opcode = shared.Cpu.GetOpcodeAt(currentOpcode); ReturnValue = new PathValue(opcode.SourcePath, shared); }
public override void Execute(SafeSharedObjects shared) { double argument = GetDouble(PopValueAssert(shared)); AssertArgBottomAndConsume(shared); double result = Math.Sqrt(argument); ReturnValue = result; }
public CPU(SafeSharedObjects shared) { this.shared = shared; this.shared.Cpu = this; stack = new Stack(); globalVariables = new VariableScope(0, -1); contexts = new List<ProgramContext>(); mainYields = new List<YieldFinishedDetector>(); triggerYields = new List<YieldFinishedDetector>(); if (this.shared.UpdateHandler != null) this.shared.UpdateHandler.AddFixedObserver(this); }
/// <summary> /// When the CPU starts the yield, it will call Begin to tell you the shared /// objects handle in case you need some information from it, and to let you /// get anything you like started up, like a timer for example.<br/> /// <br/> /// You can be guaranteed that the CPU will call Begin() before it ever calls IsFinished(), /// so it's safe for IsFinished() to use information that was obtained from Begin(). /// </summary> /// <param name="shared"></param> public abstract void Begin(SafeSharedObjects shared);
public override void Begin(SafeSharedObjects shared) { }
/// <summary> /// A utility function that a function's Execute() may use if it wishes to, to get a count of /// how many args passed to it that it has not yet consumed still remain on the stack. /// </summary> /// <param name="shared"></param> /// <returns>Number of args as yet unpopped. returns zero if there are no args, or -1 if there's a bug and the argstart marker is missing.</returns> protected int CountRemainingArgs(SafeSharedObjects shared) { int depth = 0; bool found = false; bool stillInStack = true; while (stillInStack && !found) { object peekItem = shared.Cpu.PeekRaw(depth, out stillInStack); if (stillInStack && peekItem != null && peekItem.GetType() == OpcodeCall.ArgMarkerType) found = true; else ++depth; } if (found) return depth; else return -1; }
/// <summary> /// Identical to PopValueAssert, but with the additional step of coercing the result /// into a Structure to be sure, so it won't return primitives. /// </summary> /// <returns>value after coercion into a kOS Structure</returns> protected Structure PopStructureAssertEncapsulated(SafeSharedObjects shared, bool barewordOkay = false) { object returnValue = PopValueAssert(shared, barewordOkay); return Structure.FromPrimitiveWithAssert(returnValue); }
/// <summary> /// A utility function that a function's Execute() should use in place of cpu.PopValue(), /// because it will assert that the value being popped is NOT an ARG_MARKER_STRING, and if it /// is, it will throw the appropriate error. /// </summary> /// <returns></returns> protected object PopValueAssert(SafeSharedObjects shared, bool barewordOkay = false) { object returnValue = shared.Cpu.PopValue(barewordOkay); if (returnValue != null && returnValue.GetType() == OpcodeCall.ArgMarkerType) throw new KOSArgumentMismatchException("Too few arguments were passed to " + GetFuncName()); return returnValue; }
public abstract void Execute(SafeSharedObjects shared);
/// <summary> /// A utility function that a function's Execute() must use after it has popped all the /// arguments it was expecting from the stack. It will assert that all the arguments /// have been consumed exactly, and the next item on the stack is the arg bottom mark. /// It will consume the arg bottom mark as well. /// <br/> /// If the assert fails, an exception is thrown. /// </summary> /// <param name="shared"></param> protected void AssertArgBottomAndConsume(SafeSharedObjects shared) { object shouldBeBottom = shared.Cpu.PopStack(); if (shouldBeBottom != null && shouldBeBottom.GetType() == OpcodeCall.ArgMarkerType) return; // Assert passed. throw new KOSArgumentMismatchException("Too many arguments were passed to " + GetFuncName()); }
/// <summary> /// Need to track the shared object in order to query current game time. The timer /// starts "counting" from where the clock was when the CPU calls this (which it does /// as soon as you do a YieldProgram() call.) /// </summary> /// <param name="shared"></param> public override void Begin(SafeSharedObjects shared) { this.shared = shared; endTime += shared.UpdateHandler.CurrentFixedTime; }
public SafeSerializationMgr(SafeSharedObjects sharedObjects) { this.safeSharedObjects = sharedObjects; }
public override void Begin(SafeSharedObjects shared) { this.shared = shared; }
public TerminalInput(SafeSharedObjects shared) { this.shared = shared; InitializeSuffixes(); }
public FunctionManager(SafeSharedObjects shared) { this.shared = shared; Load(); }