Esempio n. 1
0
 public static ReadExecution GenerateCommandListHandler(MemoryContents memory)
 => (exec, info) =>
 {
     foreach (var commandInfo in info.Children)
     {
         if (commandInfo.Name != "Command")
         {
             continue;
         }
         var commandString = commandInfo.Value;
         if (commandString.Contains("\n"))
         {
             var cmdArr = commandString.Split(Utils.robustNewlineDelim, StringSplitOptions.None);
             foreach (var cmdStr in cmdArr)
             {
                 memory.CommandsRun.Add(Folder
                                        .deFilter(string.IsNullOrEmpty(cmdStr) ? " " : cmdStr)
                                        .HacknetFilter());
             }
         }
         else
         {
             memory.CommandsRun.Add(Folder.deFilter(commandString).HacknetFilter());
         }
     }
 };
Esempio n. 2
0
        public static MemoryContents LoadMemoryContents(ElementInfo info)
        {
            if (info.Children.TryGetElement("Memory", out var internalInfo))
            {
                info = internalInfo;
            }
            var memory = new MemoryContents();

            if (info.Children.TryGetElement("Commands", out var commands))
            {
                foreach (var command in commands.Children.Select(x => x.Content).Where(StringExtensions.HasContent))
                {
                    if (command.Contains("\n"))
                    {
                        foreach (var trueCommand in command.Split(Utils.robustNewlineDelim, StringSplitOptions.RemoveEmptyEntries))
                        {
                            memory.CommandsRun.Add(trueCommand.HasContent() ? trueCommand.Filter() : " ");
                        }
                    }
                    else
                    {
                        memory.CommandsRun.Add(command.Filter());
                    }
                }
            }

            if (info.Children.TryGetElement("Data", out var data))
            {
                foreach (var block in data.Children)
                {
                    memory.DataBlocks.Add(block.Content.Filter());
                }
            }

            if (info.Children.TryGetElement("FileFragments", out var files))
            {
                foreach (var file in files.Children)
                {
                    memory.FileFragments.Add(new KeyValuePair <string, string>(
                                                 file.Attributes.GetString("name", "UNKNOWN"),
                                                 file.Content
                                                 ));
                }
            }

            if (info.Children.TryGetElement("Images", out var images))
            {
                foreach (var image in images.Children)
                {
                    memory.Images.Add(image.Content);
                }
            }

            return(memory);
        }
Esempio n. 3
0
        internal static XElement GetMemoryContentsSaveElement(MemoryContents contents)
        {
            var result = new XElement("Memory");

            if (contents.DataBlocks != null && contents.DataBlocks.Count > 0)
            {
                var dataTag = new XElement("Data");
                foreach (var dataBlock in contents.DataBlocks)
                {
                    var blockTag = new XElement("Block");
                    blockTag.SetValue(dataBlock);
                    dataTag.Add(blockTag);
                }
                result.Add(dataTag);
            }
            if (contents.CommandsRun != null && contents.CommandsRun.Count > 0)
            {
                var dataTag = new XElement("Commands");
                foreach (var command in contents.CommandsRun)
                {
                    var blockTag = new XElement("Command");
                    blockTag.SetValue(command);
                    dataTag.Add(blockTag);
                }
                result.Add(dataTag);
            }
            if (contents.FileFragments != null && contents.FileFragments.Count > 0)
            {
                var dataTag = new XElement("FileFragments");
                foreach (var fileFrag in contents.FileFragments)
                {
                    var blockTag = new XElement("File");
                    blockTag.SetAttributeValue("name", fileFrag.Key);
                    blockTag.SetValue(fileFrag.Value);
                    dataTag.Add(blockTag);
                }
                result.Add(dataTag);
            }
            if (contents.Images != null && contents.Images.Count > 0)
            {
                var dataTag = new XElement("Images");
                foreach (var image in contents.Images)
                {
                    var blockTag = new XElement("Image");
                    blockTag.SetValue(image);
                    dataTag.Add(blockTag);
                }
                result.Add(dataTag);
            }
            return(result);
        }
Esempio n. 4
0
        public static string TestMemoryOnLoadedComputer(ScreenManager screenMan, out int errorsAdded)
        {
            int      num      = 0;
            string   str1     = "";
            Computer computer = (Computer)ComputerLoader.loadComputer("Content/Tests/DLCTests/TestComp1.xml", false, false);

            if (computer.Memory == null)
            {
                ++num;
                str1 += "\r\nLoaded test comp1 Does not correctly load in memory.";
            }
            if (computer.Memory.DataBlocks.Count != 3)
            {
                ++num;
                str1 = str1 + "\r\nLoaded test comp reads in " + (object)computer.Memory.DataBlocks.Count + " data blocks in memory, instead of the expected 3";
            }
            if (computer.Memory.CommandsRun.Count != 1)
            {
                ++num;
                str1 = str1 + "\r\nLoaded test comp reads in " + (object)computer.Memory.CommandsRun.Count + " commands run in memory, instead of the expected 1";
            }
            else if (computer.Memory.CommandsRun[0] != "connect 123.123.123.123")
            {
                ++num;
                str1 = str1 + "\r\nLoaded test comp reads in " + computer.Memory.CommandsRun[0] + " as command, instead of the expected value";
            }
            string str2 = MemoryContents.GetMemoryFromEncodedFileString(computer.Memory.GetEncodedFileString()).TestEqualsWithErrorReport(computer.Memory);

            if (str2.Length > 0)
            {
                ++num;
                str1 = str1 + "\r\nErrors in Memory file serialization cycle!\r\n" + str2 + "\n\n";
            }
            errorsAdded = num;
            return(str1);
        }
        public static MemoryContents LoadMemoryContents(ElementInfo root)
        {
            var result = new MemoryContents();

            /*
             * I've chosen not to spin up an entire ParsedTreeExecutor here,
             * as it is only four elements and if anyone truly needs this parser extended,
             * we can always add that later.
             */

            var commandsInfo = root.Children.FirstOrDefault(e => e.Name == "Commands");

            if(commandsInfo != null)
            {
                foreach (var commandStr in commandsInfo.Children.Select(commandInfo => commandInfo.Value))
                {
                    if(commandStr.Contains("\n"))
                    {
                        var commands = commandStr.Split(Hacknet.Utils.robustNewlineDelim, StringSplitOptions.None);
                        foreach(var rawCommand in commands)
                        {
                            var command = rawCommand;
                            if(string.IsNullOrEmpty(command))
                                command = " ";
                            result.CommandsRun.Add(Folder.deFilter(command).HacknetFilter());
                        }
                    } else
                        result.CommandsRun.Add(Folder.deFilter(commandStr).HacknetFilter());
                }
            }

            var dataInfo = root.Children.FirstOrDefault(e => e.Name == "Data");

            if(dataInfo != null)
            {
                foreach(var blockInfo in dataInfo.Children)
                {
                    result.DataBlocks.Add(Folder.deFilter(blockInfo.Value).HacknetFilter());
                }
            }

            var fragmentsInfo = root.Children.FirstOrDefault(e => e.Name == "FileFragments");

            if(fragmentsInfo != null)
            {
                foreach(var fileInfo in fragmentsInfo.Children)
                {
                    result.FileFragments.Add(new KeyValuePair<string, string>(
                        Folder.deFilter(fileInfo.Attributes.GetValueOrDefault("name", "UNKNOWN")),
                        Folder.deFilter(fileInfo.Value)
                    ));
                }
            }

            var imagesInfo = root.Children.FirstOrDefault(e => e.Name == "Images");

            if(imagesInfo != null)
            {
                foreach(var imageInfo in imagesInfo.Children)
                {
                    result.Images.Add(Folder.deFilter(imageInfo.Value));
                }
            }

            return result;
        }