public static int ShowVersion(MBINFile mbin, bool quiet = false) { using (var forceConsole = new ForceConsoleOutput()) { Logger.LogInfo(Version.GetVersionString(mbin, quiet)); } return(0); }
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?"); }
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); }
// 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 CompileFile(string inputPath, string outputPath) { outputPath = String.IsNullOrEmpty(outputPath) ? inputPath : outputPath; outputPath = Path.ChangeExtension(outputPath, ".MBIN"); var data = EXmlFile.ReadTemplate(inputPath); if (data == null) { Console.WriteLine("Failed to deserialize EXML file, is it formatted correctly?"); return; } if (data.GetType() == typeof(TkGeometryData)) { outputPath = Path.ChangeExtension(outputPath, ".MBIN.PC"); } if (File.Exists(outputPath)) { File.Delete(outputPath); // todo: ask for confirmation? } using (var file = new MBINFile(outputPath)) { file.Header = new MBINHeader(); file.Header.SetDefaults(); if (data.GetType() == typeof(TkGeometryData)) { file.Header.Magic = 0xDDDDDDDD; // only used by TkGeometryData / .MBIN.PC files, maybe used to signal the file is PC only? } if (data.GetType() == typeof(TkAnimMetadata)) { file.Header.Tag = 0xFFFFFFFFFFFFFFFF; file.Header.MbinVersion = 0x9B251350AE1ABCA7; file.Header.EndPadding = 0xFEFEFEFEFEFEFEFE; } file.SetData(data); // this will also get the length of the data if (data.GetType() != typeof(TkAnimMetadata)) { file.Header.EndPadding = file.FileLength; } file.Save(); } Console.WriteLine($"MBIN data written to \"{outputPath}\" successfully?"); }
/// <summary>Get the version info for a specified MBIN <paramref name="mbin"/>.</summary> /// <param name="mbin">The MBIN file to display the version info for.</param> /// <param name="quiet"> /// If true then a plain, script-friendly version string is displayed. /// If false then a human-friendly message is displayed. /// </param> public static string GetVersionString(MBINFile mbin = null, bool quiet = false) { if (mbin == null) { return(GetVersionString(quiet)); } System.Version mbinVersion = mbin.Header.GetMBINVersion(); string versionString = mbinVersion.ToString(); return(!quiet ? (versionString != "0.0.0.0") ? "Compiled with MBINCompiler v" + versionString : "Unknown MBIN version!\nNot compiled by MBINCompiler." : versionString); }
static void ShowMBINVersion(MBINFile file, bool verbose = false) { System.Version mbinVersion = file.Header.GetMBINVersion(); string versionString = mbinVersion.ToString(); if (verbose) { if (versionString != "0.0.0.0") { versionString = "Compiled with MBINCompiler v" + versionString; } else { versionString = "Unknown MBIN version!\nNot compiled by MBINCompiler."; } } Console.WriteLine(versionString); }
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?"); } }
static void CompileFile(string input, string output) { if (String.IsNullOrEmpty(output)) { output = input; } output = Path.ChangeExtension(output, ".MBIN"); var data = EXmlFile.ReadTemplate(input); if (data == null) { Console.WriteLine("Failed to deserialize EXML file, is it formatted correctly?"); return; } if (data.GetType() == typeof(Models.Structs.TkGeometryData)) { output = Path.ChangeExtension(output, ".MBIN.PC"); } if (File.Exists(output)) { File.Delete(output); // todo: ask for confirmation? } using (var file = new MBINFile(output)) { file.Header = new Models.MBINHeader(); file.Header.SetDefaults(); if (data.GetType() == typeof(Models.Structs.TkGeometryData)) { file.Header.Magic = 0xDDDDDDDD; // only used by TkGeometryData / .MBIN.PC files, maybe used to signal the file is PC only? } file.SetData(data); file.Save(); } Console.WriteLine($"MBIN data written to \"{output}\" successfully?"); }
public MbinException(string message, Exception innerException, MBINFile mbin) : this(message, innerException, "", mbin) { }
public MbinException(Exception innerException, string fileName, MBINFile mbin) : this(DEFAULT_MESSAGE, innerException, fileName, mbin) { }
public MbinException(Exception innerException, MBINFile mbin) : this(DEFAULT_MESSAGE, innerException, "", mbin) { }
public MbinException(string message, string fileName, MBINFile mbin) : this(message, null, fileName, mbin) { }
public MbinException(string message, MBINFile mbin) : this(message, null, "", mbin) { }
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); } }
public MbinException(string message, Exception innerException, string fileName, MBINFile mbin) : base(message, innerException, fileName) { this.Mbin = mbin; }
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); }
public static string GetFileInfo(MBINFile mbin) { return($"MBIN\tversion:\t{mbin.Header.GetMBINVersion()}\tguid:\t{mbin.Header.TemplateGUID:X}\ttemplate:\t{mbin.Header.TemplateName}"); }
public MbinException(string message, string fileName, MBINFile mbin) : base(message, null, fileName) { }