public XmlWritingListener(BabyCompile owner, BabyScriptParser parser) { Owner = owner; Parser = parser; Writer = XmlWriter.Create(Owner.outputStream, new XmlWriterSettings { Indent = true, IndentChars = " " }); Error = false; }
static void Run(string[] args) { OptionProcessor[] processors = new OptionProcessor[] { new OptionProcessor("nameconfig", delegate(Queue <string> optArgs) { string nameConfigPath = optArgs.Dequeue(); }), new OptionProcessor("anonattrconfig", delegate(Queue <string> optArgs) { string attrConfigPath = optArgs.Dequeue(); }), new OptionProcessor("o", delegate(Queue <string> optArgs) { outFilePath = optArgs.Dequeue(); }), new OptionProcessor("comp", delegate(Queue <string> optArgs) { if (mode != ProgramMode.Unspecified) { throw new ArgumentException("Can't specify both compile and decompile"); } mode = ProgramMode.Compile; }), new OptionProcessor("decomp", delegate(Queue <string> optArgs) { if (mode != ProgramMode.Unspecified) { throw new ArgumentException("Can't specify both compile and decompile"); } mode = ProgramMode.Decompile; }), new OptionProcessor("h", delegate(Queue <string> optArgs) { helpMode = true; }) }; Queue <string> argQueue = new Queue <string>(args); //Console.Error.WriteLine("I have a queue of size " + argQueue.Count); //process command line arguments to set up controls try { ProcessArgs(argQueue, processors); } catch (ProcessOptionException e) { Console.Error.WriteLine(e.Message); Environment.ExitCode = 1; return; } mode = mode == ProgramMode.Unspecified ? ProgramMode.Compile : mode; if (helpMode) { Console.Error.WriteLine("Help text goes here"); return; } //try to get input paths inFilePaths = argQueue.ToArray(); if (inFilePaths.Length == 0) { Console.Error.WriteLine("At least one input file path must be specified"); Environment.ExitCode = 1; return; } bool multipleInputFiles = inFilePaths.Length > 1; //load in config files AnonAttributeConfig attrConfig = null; NameShortcutConfig nameConfig = null; try { attrConfigPath = attrConfigPath ?? "anonAttributes.txt"; nameConfigPath = nameConfigPath ?? "tagNameShortcuts.txt"; Utils.ReadConfigs(nameConfigPath, attrConfigPath, out nameConfig, out attrConfig); } catch (ArgumentException e) { Console.Error.WriteLine(e.Message); Environment.ExitCode = 2; return; } bool outputIsDirectory = false; //think about the output path based on the specified input path[s] if (multipleInputFiles) { //if more than one input file is specified, the output path must be a directory outputIsDirectory = true; if (outFilePath == null) { outFilePath = "."; } else { if (!Directory.Exists(outFilePath)) { Console.Error.WriteLine(outFilePath + " does not refer to a valid output directory"); Environment.ExitCode = 3; return; } } } else { //if one input file is specified, the output path can be a directory with or without a filename if (outFilePath == null) { outFilePath = mode == ProgramMode.Compile ? "out.xml" : "out.txt"; outputIsDirectory = false; } else { /* * if (Directory.Exists(outFilePath)) * { * outFilePath = Path.Combine(outFilePath, mode == ProgramMode.Compile ? "out.xml" : "out.txt"); * outputIsDirectory = true; * } */ outputIsDirectory = Directory.Exists(outFilePath); } } //go through and [de]compile each file bool totalSuccess = true; foreach (string path in inFilePaths) { string curOutPath; if (outputIsDirectory) { string extension = mode == ProgramMode.Compile ? ".xml" : ".txt"; string outputFileName = Path.GetFileNameWithoutExtension(path) + extension; curOutPath = Path.Combine(outFilePath, outputFileName); } else { curOutPath = outFilePath; } FileStream inputStream = null; try { inputStream = new FileStream(path, FileMode.Open, FileAccess.Read); } catch (Exception e) { Console.Error.WriteLine(string.Format("Failed to open {0} for reading: {1}", path, e.Message)); continue; } FileStream outputStream = null; try { outputStream = new FileStream(curOutPath, FileMode.Create, FileAccess.Write); } catch (Exception e) { Console.Error.WriteLine(string.Format("Failed to open {0} for writing: {1}", curOutPath, e.Message)); } Console.Error.WriteLine(string.Format("Converting {0} to make {1}", path, curOutPath)); bool success; if (mode == ProgramMode.Compile) { success = new BabyCompile(path, inputStream, outputStream, nameConfig, attrConfig).Convert(); } else { success = new BabyDecompile(path, inputStream, outputStream, nameConfig, attrConfig).Convert(); } inputStream.Close(); if (!success) { totalSuccess = false; continue; } //TODO: ask for confirmation when the output file already exists try { outputStream.Flush(); outputStream.Close(); } catch (Exception e) { Console.Error.WriteLine(string.Format("Failed to close output file {0}: {1}", curOutPath, e.Message)); } } Environment.ExitCode = totalSuccess ? 0 : 4; }