コード例 #1
0
        private static void Main(string[] args)
        {
            var reader      = new FileReader();
            var exporter    = new XmlExporter();
            var writer      = new FileWriter();
            var interpreter = new XmlInterpreter();


            //todo make this pretty by adjusting writing to reading flow.
            CommandLine.Parser.Default.ParseArguments <DecompressOptions, CompressOptions, InterpretOptions, toHexOptions, Decompress_Interpret_Options, Recompress_Export_Options>(args).MapResult(
                (DecompressOptions o) =>
            {
                foreach (String s in o.InputFiles)
                {
                    var doc = reader.ReadFile(s);
                    doc.Save(Path.ChangeExtension(s, "xml"));
                }
                Console.ReadKey();
                return(0);
            },
                (CompressOptions o) =>
            {
                var ext = "";
                if (o.OutputFileExtension != null)
                {
                    ext = o.OutputFileExtension;
                }
                else
                {
                    ext = ".filedb";
                }
                foreach (String s in o.InputFiles)
                {
                    writer.Export(s, ext);
                }
                Console.ReadKey();
                return(0);
            },
                (InterpretOptions o) =>
            {
                foreach (String s in o.InputFiles)
                {
                    var doc = interpreter.Interpret(s, o.Interpreter);
                    doc.Save(Path.ChangeExtension(HexHelper.AddSuffix(s, "_interpreted"), "xml"));
                }
                Console.ReadKey();
                return(0);
            },
                (toHexOptions o) =>
            {
                foreach (String s in o.InputFiles)
                {
                    var doc = exporter.Export(s, o.Interpreter);
                    doc.Save(Path.ChangeExtension(HexHelper.AddSuffix(s, "_exported"), "xml"));
                }

                Console.ReadKey();
                return(0);
            },
                (Decompress_Interpret_Options o) =>
            {
                foreach (String s in o.InputFiles)
                {
                    var interpreterDoc = new XmlDocument();
                    interpreterDoc.Load(o.Interpreter);
                    var doc = interpreter.Interpret(reader.ReadFile(s), interpreterDoc);
                    doc.Save(Path.ChangeExtension(HexHelper.AddSuffix(s, "_d_i"), "xml"));
                }
                Console.ReadKey();
                return(0);
            },
                (Recompress_Export_Options o) =>
            {
                var ext = "";
                if (o.OutputFileExtension != null)
                {
                    ext = o.OutputFileExtension;
                }
                else
                {
                    ext = ".filedb";
                }
                foreach (String s in o.InputFiles)
                {
                    var interpreterDoc = new XmlDocument();
                    interpreterDoc.Load(o.Interpreter);
                    writer.Export(exporter.Export(s, o.Interpreter), o.OutputFileExtension, s);
                }
                Console.ReadKey();
                return(0);
            },
                e => 1
                );
        }
コード例 #2
0
ファイル: HexHelper.cs プロジェクト: anno-mods/FileDBReader
        //using String.Join for performance optimization over for loops.
        public static String Join <T>(String BinaryData) where T : struct
        {
            var span = HexHelper.toSpan <T>(BinaryData).ToArray();

            return(String.Join <T>(" ", span));
        }
コード例 #3
0
        public XmlDocument Interpret(XmlDocument doc, XmlDocument interpreter)
        {
            //default type
            XmlNode defaultAttrib = null;

            defaultAttrib = interpreter.SelectSingleNode("/Converts/Default");
            var internalFileDBs = interpreter.SelectNodes("/Converts/InternalCompression/Element");
            var converts        = interpreter.SelectNodes("/Converts/Converts/Convert");

            //Convert internal FileDBs before conversion
            foreach (XmlNode n in internalFileDBs)
            {
                var nodes = doc.SelectNodes(n.Attributes["Path"].Value);
                foreach (XmlNode node in nodes)
                {
                    var span = HexHelper.toSpan <byte>(node.InnerText);



                    var filereader   = new FileReader();
                    var decompressed = filereader.ReadSpan(span);
                    node.InnerText = "";
                    node.AppendChild(doc.ImportNode(decompressed.DocumentElement, true));

                    //write the current decompressed internal filedb to a file
                    //String path = Path.Combine("tests", "lists", DateTime.Now.ToString("HH-mm-ss-ff") + "_" + node.Name + ".bin");
                    //using FileStream fs = File.Create(path);
                    //fs.Write(span);
                }
            }

            //converts
            foreach (XmlNode x in converts)
            {
                try
                {
                    String Path  = x.Attributes["Path"].Value;
                    var    Nodes = doc.SelectNodes(Path);
                    ConvertNodeSet(Nodes, x);
                }
                catch (Exception e) {
                    Console.WriteLine("Path not correctly set lol");
                }
            }

            //DefaultType
            if (defaultAttrib != null)
            {
                //get a combined xpath of all
                List <String> StringList = new List <string>();
                foreach (XmlNode convert in converts)
                {
                    StringList.Add(convert.Attributes["Path"].Value);
                }
                foreach (XmlNode internalFileDB in internalFileDBs)
                {
                    StringList.Add(internalFileDB.Attributes["Path"].Value);
                }
                String xPath = String.Join(" | ", StringList);

                //select all text that is not in the combined path
                var Base     = doc.SelectNodes("//*[text()]");
                var toFilter = doc.SelectNodes(xPath);
                var defaults = HexHelper.ExceptNodelists(Base, toFilter);
                ConvertNodeSet(defaults, defaultAttrib);
            }

            return(doc);
        }
コード例 #4
0
        public XmlDocument Export(XmlDocument doc, XmlDocument interpreter)
        {
            //default type
            XmlNode defaultAttrib = null;

            defaultAttrib = interpreter.SelectSingleNode("/Converts/Default");
            var converts        = interpreter.SelectNodes("/Converts/Converts/Convert");
            var internalFileDBs = interpreter.SelectNodes("/Converts/InternalCompression/Element");

            //converts
            foreach (XmlNode x in converts)
            {
                try
                {
                    String Path  = x.Attributes["Path"].Value;
                    var    Nodes = doc.SelectNodes(Path);
                    ConvertNodeSet(Nodes, x);
                }
                catch (Exception e)
                {
                    Console.WriteLine("Path not correctly set lol");
                    Console.WriteLine(x.Attributes["Path"].Value);
                }
            }

            //defaultType
            if (defaultAttrib != null)
            {
                //get a combined xpath of all
                List <String> StringList = new List <string>();
                foreach (XmlNode convert in converts)
                {
                    StringList.Add(convert.Attributes["Path"].Value);
                }
                foreach (XmlNode internalFileDB in internalFileDBs)
                {
                    StringList.Add(internalFileDB.Attributes["Path"].Value);
                }
                String xPath = String.Join(" | ", StringList);

                //select all text not in combined path#
                var Base     = doc.SelectNodes("//*[text()]");
                var toFilter = doc.SelectNodes(xPath);
                var defaults = HexHelper.ExceptNodelists(Base, toFilter);

                //convert that to default type
                ConvertNodeSet(defaults, defaultAttrib);
            }
            //internal filedbs
            foreach (XmlNode n in internalFileDBs)
            {
                var nodes = doc.SelectNodes(n.Attributes["Path"].Value);
                foreach (XmlNode node in nodes)
                {
                    //get internal document
                    var         contentNode = node.SelectSingleNode("./Content");
                    XmlDocument xmldoc      = new XmlDocument();
                    XmlNode     f           = xmldoc.ImportNode(contentNode, true);
                    xmldoc.AppendChild(xmldoc.ImportNode(f, true));

                    //compress the document
                    FileWriter fileWriter = new FileWriter();
                    var        stream     = fileWriter.Export(xmldoc, new MemoryStream());

                    //get this stream to hex
                    node.InnerText = ByteArrayToString(HexHelper.StreamToByteArray(stream));

                    //try to overwrite the bytesize since it's always exported the same way
                    var ByteSize = node.SelectSingleNode("./preceding-sibling::ByteCount");
                    if (ByteSize != null)
                    {
                        long BufferSize = stream.Length;
                        Type type       = typeof(int);
                        ByteSize.InnerText = ByteArrayToString(ConverterFunctions.ConversionRulesExport[type](BufferSize.ToString(), new UnicodeEncoding()));
                    }
                }
            }

            return(doc);
            //doc.Save(Path.ChangeExtension(HexHelper.AddSuffix(docpath, "_e"), "xml"));
        }