// ---------------------------------------------------------- static void Main(string[] aArgs) { // create options var options = new Options(); if (!ParseArgs(aArgs, options)) { PrintUsage(); Environment.Exit(1); } // read defs var defsReader = new DefsReader(); var defs = defsReader.ReadDefs(options); // convert if (options.isInFileXml) { if (!options.outputBinary) { new XmlMinimizer().Minimize(options, defs.rootDef); } else { new Xml2Bin().Convert(options, defs.rootDef); } } else { } // to prevent closing console window Console.WriteLine("Done."); Console.ReadKey(true); }
// ---------------------------------------------------------- public void Minimize(Options aOptions, Def aDef) { // reader _reader = new XmlDocument(); _reader.Load(aOptions.inFile); // writter with write settings var writterSettings = new XmlWriterSettings(); if (aOptions.prettyPrint) { writterSettings.Indent = true; writterSettings.IndentChars = " "; } _writer = XmlWriter.Create(aOptions.outFile, writterSettings); _writer.WriteStartDocument(); ProcessElement(_reader.DocumentElement, aDef); _writer.WriteEndDocument(); _writer.Close(); // print warnings foreach(string warning in _warnings) { Console.WriteLine(warning); } }
// ---------------------------------------------------------- public Defs ReadDefs(Options aOptions) { // top element var defs = new Defs(); defs.rootDef = new Def(); // load defs var doc = new XmlDocument(); doc.Load(aOptions.defsFile); // read items table ReadItems(doc.GetElementsByTagName("items").Item(0) as XmlElement, defs.items); // proces elements recursively var spriterElement = doc.GetElementsByTagName("spriter").Item(0) as XmlElement; ReadSpriterDefs(spriterElement.GetElementsByTagName("element").Item(0) as XmlElement, defs.rootDef, defs.items); // output json file with defs var jsonDefsString = JsonConvert.SerializeObject(defs.rootDef, Newtonsoft.Json.Formatting.Indented); var jsonDefsFileName = Path.GetFileNameWithoutExtension(aOptions.defsFile) + ".json"; using (StreamWriter sw = new StreamWriter(jsonDefsFileName)) { sw.Write(jsonDefsString); } // return top def return defs; }
// ---------------------------------------------------------- private static bool ParseArgs(string[] aArgs, Options aOptions) { int argIdx = 0; bool inFileProcessed = false; while (argIdx < aArgs.Length) { string arg = aArgs[argIdx++]; switch (arg.ToLower()) { case "-defs": aOptions.defsFile = GetFileWithPath(aArgs[argIdx++]); break; case "-prettyprint": aOptions.prettyPrint = true; break; case "-binary": aOptions.outputBinary = true; break; case "-smalloffset": aOptions.smallOffset = true; break; default: if (!inFileProcessed) { aOptions.inFile = GetFileWithPath(arg); var extension = Path.GetExtension(aOptions.inFile).ToLower(); aOptions.isInFileXml = (extension == ".xml" || extension == ".scml"); inFileProcessed = true; } else { aOptions.outFile = GetFileWithPath(arg); } break; } } // check if output file is defined if (aOptions.outFile == null) { aOptions.outFile = Path.GetDirectoryName(aOptions.inFile) + Path.DirectorySeparatorChar + Path.GetFileNameWithoutExtension(aOptions.inFile) + "_out"; if (aOptions.outputBinary) { aOptions.outFile += ".bin"; } else { aOptions.outFile += Path.GetExtension(aOptions.inFile); } } // whether options are ok return aOptions.inFile != null && aOptions.outFile != null; }
// ---------------------------------------------------------- public void Convert(Options aOptions, Def aRootDef) { if (aOptions.smallOffset) { _bigOffset = false; _offsetSize = 2; } // reader XmlDocument reader = new XmlDocument(); reader.Load(aOptions.inFile); using (FileStream fs = new FileStream(aOptions.outFile, FileMode.Create)) { using (BinaryWriter writer = new BinaryWriter(fs)) { // write 0 for bigOffset and 1 for smallOffset writer.Write((byte)(_bigOffset ? 0 : 1)); // write all elements recursively ProcessElement(1, reader.DocumentElement, aRootDef, writer); } } }