Esempio n. 1
0
        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");
        }
Esempio n. 2
0
        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");
        }
Esempio n. 3
0
        // 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);
        }
Esempio n. 4
0
        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;
        }
Esempio n. 5
0
        public static TimeSpan CreateFromDump(SafeSharedObjects shared, Dump d)
        {
            var newObj = new TimeSpan();

            newObj.LoadDump(d);
            return(newObj);
        }
Esempio n. 6
0
        // 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);
        }
Esempio n. 7
0
        // 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);
        }
Esempio n. 8
0
        // 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);
        }
Esempio n. 10
0
        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;
        }
Esempio n. 11
0
        // 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);
        }
Esempio n. 12
0
        // 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);
        }
Esempio n. 13
0
        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");
            }
        }
Esempio n. 14
0
        /// <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);
        }
Esempio n. 15
0
        // 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);
        }
Esempio n. 16
0
        public override void Execute(SafeSharedObjects shared)
        {
            string textToPrint = PopValueAssert(shared).ToString();

            AssertArgBottomAndConsume(shared);
            shared.Screen.Print(textToPrint);
        }
Esempio n. 17
0
        public override void Execute(SafeSharedObjects shared)
        {
            string autopilotMode = PopValueAssert(shared).ToString();

            AssertArgBottomAndConsume(shared);
            ((CPU)shared.Cpu).SelectAutopilotMode(autopilotMode);
        }
Esempio n. 18
0
        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");
                }
            }
        }
Esempio n. 19
0
        // 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);
            }
        }
Esempio n. 21
0
        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);
        }
Esempio n. 22
0
            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;
            }
Esempio n. 23
0
        public override void Execute(SafeSharedObjects shared)
        {
            string name = PopValueAssert(shared).ToString();

            AssertArgBottomAndConsume(shared);

            ReturnValue = new BuiltinDelegate(shared.Cpu, name);
        }
Esempio n. 24
0
        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);
        }
Esempio n. 25
0
        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);
        }
Esempio n. 26
0
 public override void ThreadInitialize(SafeSharedObjects shared)
 {
     if (compileMode != CompileMode.FILE)
     {
         programContext = shared.Cpu.SwitchToProgramContext(); // only switch the context if executing
     }
     codeParts = new List <CodePart>();
 }
Esempio n. 27
0
        public override void Execute(SafeSharedObjects shared)
        {
            int    seed = GetInt(PopValueAssert(shared));
            string key  = PopValueAssert(shared).ToString();

            AssertArgBottomAndConsume(shared);
            Utilities.KOSMath.StartRandomFromSeed(key, seed);
        }
Esempio n. 28
0
        // 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);
        }
Esempio n. 29
0
        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);
        }
Esempio n. 30
0
        public override void Execute(SafeSharedObjects shared)
        {
            double argument = GetDouble(PopValueAssert(shared));

            AssertArgBottomAndConsume(shared);
            double result = Math.Sqrt(argument);

            ReturnValue = result;
        }
Esempio n. 31
0
File: CPU.cs Progetto: KSP-KOS/KOS
 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);
 }
Esempio n. 32
0
 /// <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);
Esempio n. 33
0
 public override void Begin(SafeSharedObjects shared)
 {
 }
Esempio n. 34
0
 /// <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;
 }
Esempio n. 35
0
 /// <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);
 }
Esempio n. 36
0
 /// <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;
 }
Esempio n. 37
0
 public abstract void Execute(SafeSharedObjects shared);
Esempio n. 38
0
        /// <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());
        }
Esempio n. 39
0
 /// <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;
 }
Esempio n. 40
0
 public SafeSerializationMgr(SafeSharedObjects sharedObjects)
 {
     this.safeSharedObjects = sharedObjects;
 }
Esempio n. 41
0
 public override void Begin(SafeSharedObjects shared)
 {
     this.shared = shared;
 }
Esempio n. 42
0
 public TerminalInput(SafeSharedObjects shared)
 {
     this.shared = shared;
     InitializeSuffixes();
 }
Esempio n. 43
0
 public FunctionManager(SafeSharedObjects shared)
 {
     this.shared = shared;
     Load();
 }