private static int HandleVersionMode(CommandLineParser options) { var files = options.GetFileParams(); if (files.Count == 0) { return(CommandLine.ShowVersion(Quiet)); } if (files.Count > 1) { return(CommandLine.ShowInvalidCommandLineArg(files[1])); } var fIn = new FileStream(files[0], FileMode.Open, FileAccess.Read); var mbin = new MBINFile(fIn); if (!mbin.Load() || !mbin.Header.IsValid) { return(CommandLine.ShowCommandLineError("Invalid file type.\n" + "Only MBIN files can be versioned.\n" + $"\"{files[0]}\"")); } CommandLine.ShowVersion(mbin, Quiet); return((int)ErrorCode.Success); }
static void DecompileFile(string input, string output) { if (String.IsNullOrEmpty(output)) { output = Path.ChangeExtension(input, ".exml"); // emoose XML, because there's no way this XML format is compatible with MXML } if (File.Exists(output)) { File.Delete(output); // todo: ask for confirmation? } // no error checking ^^ (todo: error checking) var file = new MBINFile(input); file.Load(); var data = file.GetData(); if (data == null) { Console.WriteLine($"Failed to deserialize template \"{file.Header.GetXMLTemplateName()}\", has the structure been mapped yet?"); return; } var xmlString = EXmlFile.WriteTemplate(data); if (string.IsNullOrEmpty(xmlString)) { Console.WriteLine($"Error serializing template \"{file.Header.GetXMLTemplateName()}\" to XML!"); return; } File.WriteAllText(output, xmlString); Console.WriteLine($"XML data written to \"{output}\" successfully?"); }
// TODO: (GH) not used static void ScanMBINs(string path, ref List <string> types) { foreach (var file in Directory.GetFiles(path, "*.mbin*")) { var mbin = new MBINFile(file); mbin.Load(); types.Add($"{file} : {mbin.Header.GetXMLTemplateName()}"); } foreach (var folder in Directory.GetDirectories(path)) { ScanMBINs(folder, ref types); } }
static void DecompileFile(string inputPath, string outputPath, bool getVersion = false, bool verbose = false) { outputPath = String.IsNullOrEmpty(outputPath) ? inputPath : outputPath; if (Path.GetExtension(outputPath) == ".PC") { outputPath = Path.GetFileNameWithoutExtension(outputPath); // remove the ".PC" } outputPath = Path.ChangeExtension(outputPath, ".exml"); // emoose XML, because there's no way this XML format is compatible with MXML if (File.Exists(outputPath)) { File.Delete(outputPath); // todo: ask for confirmation? } // no error checking ^^ (todo: error checking) var file = new MBINFile(inputPath); file.Load( ); if (getVersion) { ShowMBINVersion(file, verbose); } else { var data = file.GetData(); if (data == null) { Console.WriteLine($"Failed to deserialize template \"{file.Header.GetXMLTemplateName()}\", has the structure been mapped yet?"); return; } var xmlString = EXmlFile.WriteTemplate(data); if (string.IsNullOrEmpty(xmlString)) { Console.WriteLine($"Error serializing template \"{file.Header.GetXMLTemplateName()}\" to XML!"); return; } File.WriteAllText(outputPath, xmlString); Console.WriteLine($"XML data written to \"{outputPath}\" successfully?"); } }
public static void ConvertFile(string fileIn, string fileOut, FormatType inputFormat, FormatType outputFormat) { fileOut = ChangeFileExtension(fileOut, outputFormat); FileMode fileMode = GetFileMode(fileOut); Directory.CreateDirectory(Path.GetDirectoryName(fileOut)); try { using (var indentScope = new Logger.IndentScope()) using (var fIn = new FileStream(fileIn, FileMode.Open, FileAccess.Read)) using (var ms = new MemoryStream()) { if (inputFormat == FormatType.MBIN) { var mbin = new MBINFile(fIn); if (!mbin.Load() || !mbin.Header.IsValid) { throw new InvalidDataException("Not a valid MBIN file!"); } var sw = new StreamWriter(ms); NMSTemplate data = null; try { data = mbin.GetData(); if (data is null) { throw new InvalidDataException("Invalid MBIN data."); } } catch (Exception e) { throw new MbinException($"Failed to read {mbin.Header.GetXMLTemplateName()} from MBIN.", e, fileIn, mbin); } try { sw.Write(EXmlFile.WriteTemplate(data)); sw.Flush(); if (ms.Length == 0) { throw new InvalidDataException("Invalid EXML data."); } } catch (Exception e) { throw new MbinException($"Failed serializing {mbin.Header.GetXMLTemplateName()} to EXML.", e, fileIn, mbin); } } else if (inputFormat == FormatType.EXML) { NMSTemplate data = null; try { data = EXmlFile.ReadTemplateFromStream(fIn); if (data is null) { throw new InvalidDataException($"Failed to deserialize EXML."); } if (data is TkGeometryData) { fileOut += ".PC"; } var mbin = new MBINFile(ms) { Header = new MBINHeader() }; mbin.Header.SetDefaults(data.GetType()); mbin.SetData(data); mbin.Save(); } catch (Exception e) { throw new ExmlException(e, fileIn, data); } } ms.Flush(); using (var fOut = new FileStream(fileOut, fileMode, FileAccess.Write)) ms.WriteTo(fOut); } } catch (Exception e) { File.Delete(fileOut); if (e is CompilerException) { throw; } throw new CompilerException(e, fileIn); } }
private static bool AutoDetectFormat(List <string> fileList) { // detect what types of file formats are found bool foundMBIN = false; bool foundEXML = false; foreach (var file in fileList) { if (Path.HasExtension(file)) { var ext = Path.GetExtension(file).ToUpper(); foundMBIN |= (ext == ".MBIN") || (ext == ".PC"); foundEXML |= (ext == ".EXML"); } } // TODO: this should be handled better if (!foundMBIN && !foundEXML) { if ((fileList.Count == 1) && File.Exists(fileList[0])) { using (var fIn = new FileStream(fileList[0], FileMode.Open)) { // possibly MBIN? check for a valid header using (var mbin = new MBINFile(fIn, true)) foundMBIN = (mbin.Load() && mbin.Header.IsValid); if (!foundMBIN) // possibly EXML? check for a valid xml tag { var xmlTag = "<?xml version=\"1.0\" encoding=\"utf-8\"?>".ToLower(); var bytes = new byte[xmlTag.Length]; // TODO: handle potential leading whitespace? if (fIn.Read(bytes, 0, xmlTag.Length) == xmlTag.Length) { var txt = System.Text.Encoding.ASCII.GetString(bytes).ToLower(); foundEXML = (txt == xmlTag); } } } } } if (foundMBIN && foundEXML) { const string msg = "Unable to automatically determine the --input-format type."; if (Quiet) { return(CommandLine.ShowError(msg) == (int)ErrorCode.Success); } CommandLine.ShowWarning(msg); Console.Out.WriteLine("Both MBIN and EXML file types were detected!\n"); InputFormat = Utils.PromptInputFormat(); Console.Out.WriteLine(); } else if (foundMBIN) { Logger.LogInfo("Auto-Detected --input-format=MBIN\n"); InputFormat = FormatType.MBIN; } else if (foundEXML) { Logger.LogInfo("Auto-Detected --input-format=EXML\n"); InputFormat = FormatType.EXML; } else { CommandLine.ShowError("No valid files found!"); return(false); } OutputFormat = (InputFormat == FormatType.MBIN) ? FormatType.EXML : FormatType.MBIN; Logger.LogDebug($"--input-format={InputFormat} --output-format={OutputFormat}\n"); return(true); }