Пример #1
0
        public override bool SaveFile(ProgramFile file)
        {
            base.SaveFile(file);

            Directory.CreateDirectory(_archiveFolder);

            try
            {
                using (StreamWriter outfile = new StreamWriter(_archiveFolder + file.Filename + ".txt", false))
                {
                    string fileBody = file.Content;

                    if (Application.platform == RuntimePlatform.WindowsPlayer)
                    {
                        // Only evil windows gets evil windows line breaks
                        fileBody = fileBody.Replace("\n", "\r\n");
                    }

                    outfile.Write(fileBody);
                }
            }
            catch (Exception e)
            {
                Debug.LogException(e);
                return(false);
            }

            return(true);
        }
Пример #2
0
        private string GetSourceLine(string filePath, int line)
        {
            string returnVal = "(Can't show source line)";

            string[] pathParts = filePath.Split('/');
            string   fileName  = pathParts.Last();
            Volume   vol;
            bool     getFile = true; // should it try to retrive the file?

            if (line < 0)
            {
                // Special exception - if line number is negative then this isn't from any
                // line of user's code but from the system itself (like the triggers the compiler builds
                // to recalculate LOCK THROTTLE and LOCK STEERING each time there's an Update).
                return("<<System Built-In Flight Control Updater>>");
            }
            if (pathParts.Length > 1)
            {
                string volName = pathParts.First();
                if (Regex.IsMatch(volName, @"^\d+$"))
                {
                    // If the volume is a number, then get the volume by integer id.
                    int volNum;
                    int.TryParse(volName, out volNum);
                    vol = Shared.VolumeMgr.GetVolume(volNum);
                }
                else
                {
                    // If the volume is not a number, then get the volume by name string.
                    vol = Shared.VolumeMgr.GetVolume(volName);
                }
            }
            else
            {
                vol = Shared.VolumeMgr.CurrentVolume;
            }

            if (fileName == "interpreter history")
            {
                // Get the line from the interpreter command history instead of from a file on volume:
                getFile   = false;
                returnVal = Shared.Interpreter.GetCommandHistoryAbsolute(line);
            }

            if (getFile)
            {
                ProgramFile file = vol.GetByName(fileName);
                if (file != null)
                {
                    string[] splitLines = file.Content.Split('\n');
                    if (splitLines.Length >= line)
                    {
                        returnVal = splitLines[line - 1];
                    }
                }
            }

            return(returnVal);
        }
Пример #3
0
 public override bool SaveFile(ProgramFile file)
 {
     if (IsRoomFor(file))
     {
         return(base.SaveFile(file));
     }
     else
     {
         return(false);
     }
 }
Пример #4
0
        public override bool IsRoomFor(ProgramFile newFile)
        {
            int         usedSpace    = GetUsedSpace();
            ProgramFile existingFile = GetByName(newFile.Filename);

            if (existingFile != null)
            {
                usedSpace -= existingFile.GetSize();
            }

            return((Capacity - usedSpace) >= newFile.GetSize());
        }
Пример #5
0
        public virtual bool RenameFile(string name, string newName)
        {
            ProgramFile file = GetByName(name);

            if (file != null)
            {
                DeleteByName(name);
                file.Filename = newName;
                Add(file);
                return(true);
            }
            else
            {
                return(false);
            }
        }
Пример #6
0
        public virtual void AppendToFile(string name, string textToAppend)
        {
            ProgramFile file = GetByName(name);

            if (file == null)
            {
                file = new ProgramFile(name);
            }

            if (file.Content.Length > 0 && !file.Content.EndsWith("\n"))
            {
                textToAppend = "\n" + textToAppend;
            }

            file.Content += textToAppend;
            SaveFile(file);
        }
Пример #7
0
        public override void Execute(SharedObjects shared)
        {
            object volumeId = shared.Cpu.PopValue();
            string direction = shared.Cpu.PopValue().ToString();
            string fileName = shared.Cpu.PopValue().ToString();

            if (shared.VolumeMgr != null)
            {
                Volume origin;
                Volume destination;

                if (direction == "from")
                {
                    origin = shared.VolumeMgr.GetVolume(volumeId);
                    destination = shared.VolumeMgr.CurrentVolume;
                }
                else
                {
                    origin = shared.VolumeMgr.CurrentVolume;
                    destination = shared.VolumeMgr.GetVolume(volumeId);
                }

                if (origin != null && destination != null)
                {
                    if (origin != destination)
                    {
                        ProgramFile file = origin.GetByName(fileName);
                        if (file != null)
                        {
                            if (!destination.SaveFile(new ProgramFile(file)))
                            {
                                throw new Exception("File copy failed");
                            }
                        }
                        else
                        {
                            throw new Exception(string.Format("File '{0}' not found", fileName));
                        }
                    }
                }
                else
                {
                    throw new Exception("Volume not found");
                } 
            }
        }
Пример #8
0
        public override ProgramFile GetByName(string name)
        {
            try
            {
                using (StreamReader infile = new StreamReader(_archiveFolder + name + ".txt", true))
                {
                    string fileBody = infile.ReadToEnd().Replace("\r\n", "\n");

                    ProgramFile retFile = new ProgramFile(name);
                    retFile.Content = fileBody;
                    base.DeleteByName(name);
                    base.Add(retFile);

                    return(retFile);
                }
            }
            catch (Exception e)
            {
                Debug.LogException(e);
                return(null);
            }
        }
Пример #9
0
        public void OnSave(ConfigNode node)
        {
            ConfigNode contextNode = new ConfigNode("context");

            // Save variables
            if (_vars.Count > 0)
            {
                ConfigNode varNode = new ConfigNode("variables");

                foreach (var kvp in _vars)
                {
                    if (!(kvp.Value is BoundVariable))
                    {
                        varNode.AddValue(kvp.Key.TrimStart('$'), ProgramFile.EncodeLine(kvp.Value.Value.ToString()));
                    }
                }

                contextNode.AddNode(varNode);
            }

            node.AddNode(contextNode);
        }
Пример #10
0
        public void OnLoad(ConfigNode node)
        {
            StringBuilder scriptBuilder = new StringBuilder();

            foreach (ConfigNode contextNode in node.GetNodes("context"))
            {
                foreach (ConfigNode varNode in contextNode.GetNodes("variables"))
                {
                    foreach (ConfigNode.Value value in varNode.values)
                    {
                        string varValue = ProgramFile.DecodeLine(value.value);
                        scriptBuilder.AppendLine(string.Format("set {0} to {1}.", value.name, varValue));
                    }
                }
            }

            if (_shared.ScriptHandler != null && scriptBuilder.Length > 0)
            {
                ProgramBuilder programBuilder = new ProgramBuilder();
                programBuilder.AddRange(_shared.ScriptHandler.Compile(scriptBuilder.ToString()));
                List <Opcode> program = programBuilder.BuildProgram(false);
                RunProgram(program, true);
            }
        }
Пример #11
0
        public override void Execute(SharedObjects shared)
        {
            object volumeId = shared.Cpu.PopValue();
            string fileName = shared.Cpu.PopValue().ToString();

            if (shared.VolumeMgr != null)
            {
                if (shared.VolumeMgr.CurrentVolume != null)
                {
                    ProgramFile file = shared.VolumeMgr.CurrentVolume.GetByName(fileName);
                    if (file != null)
                    {
                        if (shared.ScriptHandler != null)
                        {
                            Stopwatch compileWatch = null;
                            bool showStatistics = Config.GetInstance().ShowStatistics;
                            if (showStatistics) compileWatch = Stopwatch.StartNew();

                            List<CodePart> parts = shared.ScriptHandler.Compile(file.Content);
                            ProgramBuilder builder = new ProgramBuilder();
                            builder.AddRange(parts);
                            List<Opcode> program = builder.BuildProgram(false);

                            if (showStatistics)
                            {
                                compileWatch.Stop();
                                shared.Cpu.TotalCompileTime += compileWatch.ElapsedMilliseconds;
                            }

                            if (volumeId != null)
                            {
                                Volume targetVolume = shared.VolumeMgr.GetVolume(volumeId);
                                if (targetVolume != null)
                                {
                                    if (shared.ProcessorMgr != null)
                                    {
                                        shared.ProcessorMgr.RunProgramOn(program, targetVolume);
                                    }
                                }
                                else
                                {
                                    throw new Exception("Volume not found");
                                }
                            }
                            else
                            {
                                shared.Cpu.RunProgram(program);
                            }
                        }
                    }
                    else
                    {
                        throw new Exception(string.Format("File '{0}' not found", fileName));
                    }
                }
                else
                {
                    throw new Exception("Volume not found");
                }
            }
        }
Пример #12
0
 public virtual bool SaveFile(ProgramFile file)
 {
     DeleteByName(file.Filename);
     Add(file);
     return(true);
 }
Пример #13
0
 public virtual void Add(ProgramFile file)
 {
     _files.Add(file.Filename.ToLower(), file);
 }
Пример #14
0
 public override bool IsRoomFor(ProgramFile newFile)
 {
     return(true);
 }
Пример #15
0
 public virtual bool IsRoomFor(ProgramFile newFile)
 {
     return(true);
 }
Пример #16
0
        private string GetSourceLine(string filePath, int line)
        {
            string returnVal = "(Can't show source line)";

            if (line < 0 && string.IsNullOrEmpty(filePath))
            {
                // Special exception - if line number is negative then this isn't from any
                // line of user's code but from the system itself (like the triggers the compiler builds
                // to recalculate LOCK THROTTLE and LOCK STEERING each time there's an Update).
                return("<<System Built-In Flight Control Updater>>");
            }

            if (string.IsNullOrEmpty(filePath))
            {
                return("<<Probably internal error within kOS C# code>>");
            }
            string[] pathParts = filePath.Split('/');
            string   fileName  = pathParts.Last();
            Volume   vol;

            if (pathParts.Length > 1)
            {
                string volName = pathParts.First();
                if (Regex.IsMatch(volName, @"^\d+$"))
                {
                    // If the volume is a number, then get the volume by integer id.
                    int volNum;
                    int.TryParse(volName, out volNum);
                    vol = Shared.VolumeMgr.GetVolume(volNum);
                }
                else
                {
                    // If the volume is not a number, then get the volume by name string.
                    vol = Shared.VolumeMgr.GetVolume(volName);
                }
            }
            else
            {
                vol = Shared.VolumeMgr.CurrentVolume;
            }

            if (fileName == "interpreter history")
            {
                return(Shared.Interpreter.GetCommandHistoryAbsolute(line));
            }

            ProgramFile file = vol.GetByName(fileName);

            if (file != null)
            {
                if (file.Category == FileCategory.KSM)
                {
                    return("<<machine language file: can't show source line>>");
                }

                string[] splitLines = file.StringContent.Split('\n');
                if (splitLines.Length >= line)
                {
                    returnVal = splitLines[line - 1];
                }
            }
            return(returnVal);
        }
Пример #17
0
 public ProgramFile(ProgramFile copy)
 {
     Filename = copy.Filename;
     Content  = copy.Content;
 }