Пример #1
0
 public static FileContent ToHarddiskFile(this ConfigNode configNode, Harddisk harddisk, HarddiskDirectory directory)
 {
     try
     {
         string content = null;
         if (configNode.TryGetValue("ascii", ref content)) // ASCII files just get decoded from the ConfigNode safe representation
         {
             return(new FileContent(PersistenceUtilities.DecodeLine(content)));
         }
         if (configNode.TryGetValue("binary", ref content)) // binary files get decoded from Base64 and gzip
         {
             return(new FileContent(PersistenceUtilities.DecodeBase64ToBinary(content)));
         }
         if (configNode.TryGetValue("line", ref content)) // fall back to legacy logic
         {
             return(Decode(content));
         }
     }
     catch (Exception ex)
     {
         SafeHouse.Logger.LogError(string.Format("Exception caught loading file information: {0}\n\nStack Trace:\n{1}", ex.Message, ex.StackTrace));
     }
     SafeHouse.Logger.LogError(string.Format("Error loading file information from ConfigNode at path {0} on hard disk {1}", directory.Path, harddisk.Name));
     return(new FileContent(""));  // if there was an error, just return a blank file.
 }
Пример #2
0
 private static FileContent Decode(string input)
 {
     try
     {
         return(new FileContent(PersistenceUtilities.DecodeBase64ToBinary(input)));
     }
     catch // if there is an exception of any kind decoding, fall back to standard decoding
     {
         string decodedString = PersistenceUtilities.DecodeLine(input);
         return(new FileContent(decodedString));
     }
 }
Пример #3
0
 private static FileContent Decode(string input)
 {
     try
     {
         return(new FileContent(PersistenceUtilities.DecodeBase64ToBinary(input)));
     }
     catch (FormatException)
     {
         // standard encoding
         string decodedString = PersistenceUtilities.DecodeLine(input);
         return(new FileContent(decodedString));
     }
 }
Пример #4
0
Файл: CPU.cs Проект: jenden0/KOS
        public void OnLoad(ConfigNode node)
        {
            try
            {
                var 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 = PersistenceUtilities.DecodeLine(value.value);
                            scriptBuilder.AppendLine(string.Format("set {0} to {1}.", value.name, varValue));
                        }
                    }
                }

                if (shared.ScriptHandler == null || scriptBuilder.Length <= 0)
                {
                    return;
                }

                var programBuilder = new ProgramBuilder();
                // TODO: figure out how to store the filename and reload it for arg 1 below:
                // (Possibly all of OnLoad needs work because it never seemed to bring
                // back the context fully right anyway, which is why this hasn't been
                // addressed yet).
                try
                {
                    SafeHouse.Logger.Log("Parsing Context:\n\n" + scriptBuilder);
                    programBuilder.AddRange(shared.ScriptHandler.Compile("reloaded file", 1, scriptBuilder.ToString()));
                    List <Opcode> program = programBuilder.BuildProgram();
                    RunProgram(program, true);
                }
                catch (NullReferenceException ex)
                {
                    SafeHouse.Logger.Log("program builder failed on load. " + ex.Message);
                }
            }
            catch (Exception e)
            {
                if (shared.Logger != null)
                {
                    shared.Logger.Log(e);
                }
            }
        }
        public Dump FromConfigNode(ConfigNode configNode)
        {
            Dump result = new Dump();

            foreach (ConfigNode.Value val in configNode.values)
            {
                result[val.name] = PersistenceUtilities.DecodeLine(val.value);
            }

            foreach (ConfigNode subNode in configNode.GetNodes())
            {
                result[subNode.name] = ObjectFromConfigNode(subNode);
            }

            return(result);
        }
        private List <object> ListFromConfigNode(ConfigNode configNode)
        {
            List <object> result = new List <object>();

            bool hasMoreValues = true;

            for (int i = 0; hasMoreValues; i++)
            {
                if (configNode.HasValue(i.ToString()))
                {
                    result.Add(PersistenceUtilities.DecodeLine(configNode.GetValue(i.ToString())));
                }
                else if (configNode.HasNode(i.ToString()))
                {
                    result.Add(ObjectFromConfigNode(configNode.GetNode(i.ToString())));
                }
                else
                {
                    hasMoreValues = false;
                }
            }

            return(result);
        }
Пример #7
0
        private static void Decode(ProgramFile programFile, string input)
        {
            try
            {
                string decodedString;
                try
                {
                    // base64 encoding

                    // Fix for issue #429.  See comment up in EncodeBase64() method above for an explanation:
                    string massagedInput = input.Replace(',', '/');

                    byte[]       decodedBuffer = DecodeBase64ToBinary(massagedInput);
                    FileCategory whatKind      = PersistenceUtilities.IdentifyCategory(decodedBuffer);
                    if (whatKind == FileCategory.ASCII || whatKind == FileCategory.KERBOSCRIPT)
                    {
                        decodedString             = Encoding.ASCII.GetString(decodedBuffer);
                        programFile.StringContent = decodedString;
                    }
                    else
                    {
                        programFile.BinaryContent = decodedBuffer;
                    }
                }
                catch (FormatException)
                {
                    // standard encoding
                    decodedString             = PersistenceUtilities.DecodeLine(input);
                    programFile.StringContent = decodedString;
                }
            }
            catch (Exception e)
            {
                SafeHouse.Logger.Log(string.Format("Exception decoding: {0} | {1}", e, e.Message));
            }
        }