static void ExtractMBinFile(FileInfo inputFile, string outputFileName) { using var mbinFile = new MBINFile(inputFile.FullName); using var outputFile = File.Create(outputFileName); using var sw = new StreamWriter(outputFile); if (!(mbinFile.Load() && mbinFile.Header.IsValid)) { throw new InvalidDataException("Invalid File"); } var type = NMSTemplate.GetTemplateType(mbinFile.Header.GetXMLTemplateName()); if (type == null) { throw new NullReferenceException(); } var nms = (NMSAttribute)(type.GetCustomAttributes(typeof(NMSAttribute), false)?[0] ?? null); var broken = nms.Broken; // GUID's for the old files ulong[] UnsupportedGUIDs = new ulong[] { }; var mismatch = (mbinFile.Header.TemplateGUID != nms.GUID); bool unsupported = (UnsupportedGUIDs.Contains(mbinFile.Header.TemplateGUID)); if (broken) { var previousColour = Console.ForegroundColor; Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine("File is broken"); Console.ForegroundColor = previousColour; } else if (unsupported) { var previousColour = Console.ForegroundColor; Console.ForegroundColor = ConsoleColor.DarkRed; Console.WriteLine("File is Unsupported"); Console.ForegroundColor = previousColour; } else if (mismatch) { var previousColour = Console.ForegroundColor; Console.ForegroundColor = ConsoleColor.Yellow; Console.WriteLine("File is Mismatched"); Console.ForegroundColor = previousColour; } var data = mbinFile.GetData(); sw.Write(EXmlFile.WriteTemplate(data)); sw.Flush(); }
public static NMSTemplate LoadNMSFileOLD(string filepath) { int load_mode = 0; NMSTemplate template; string exmlpath = Path.ChangeExtension(filepath, "exml"); exmlpath = exmlpath.ToUpper(); //Make upper case if (File.Exists(exmlpath)) { load_mode = 0; } else { load_mode = 1; } //Load Exml try { if (load_mode == 0) { string xml = File.ReadAllText(exmlpath); template = EXmlFile.ReadTemplateFromString(xml); } else { if (!File.Exists(filepath)) { throw new FileNotFoundException("File not found\n " + filepath); } libMBIN.MBINFile mbinf = new libMBIN.MBINFile(filepath); mbinf.Load(); template = mbinf.GetData(); mbinf.Dispose(); } } catch (Exception ex) { if (ex is System.IO.DirectoryNotFoundException || ex is System.IO.FileNotFoundException) { Util.showError("File " + filepath + " Not Found...", "Error"); } else if (ex is System.Reflection.TargetInvocationException) { Util.showError("libMBIN failed to decompile the file. Try to update the libMBIN.dll (File->updateLibMBIN). If the issue persists contact the developer", "Error"); } return(null); } return(template); }
/// <summary>Convert EXML to MBIN</summary> /// <param name="fIn">Source file</param> /// <param name="msOut">Output stream</param> /// <param name="fileOut">Output file path. Passed through as the return value. For geometry files, ".PC" will be appended.</param> /// <returns>fileOut</returns> private static string ConvertEXML(string inputPath, FileStream fIn, MemoryStream msOut, string fileOut) { string templateName; NMSTemplate data = null; try { data = EXmlFile.ReadTemplateFromStream(fIn, out templateName); Type type = NMSTemplate.GetTemplateType(templateName); var nms = (NMSAttribute)(data.GetType().GetCustomAttributes(typeof(NMSAttribute), false)?[0] ?? null); if (nms.Broken) { FileIsBroken(inputPath, data); } if (data is null) { throw new InvalidDataException($"Failed to deserialize EXML."); } if (data is libMBIN.NMS.Toolkit.TkGeometryData | data is libMBIN.NMS.Toolkit.TkGeometryStreamData) { fileOut += ".PC"; } var mbin = new MBINFile(msOut) { Header = new MBINHeader() }; mbin.Header.SetDefaults(data.GetType(), FormatVersion); mbin.SetData(data); mbin.Save(); } catch (Exception e) { throw new ExmlException(e, fIn.Name, data); } return(fileOut); }
public static NMSTemplate LoadNMSTemplate(string filepath, ref ResourceManager resMgr) { int load_mode = 0; NMSTemplate template = null; filepath = filepath.Replace('\\', '/'); string effective_filepath = filepath; //Checks to prevent malformed paths from further processing if (filepath.Contains(' ')) { return(null); } string exmlpath = Path.ChangeExtension(filepath, "exml"); exmlpath = exmlpath.ToUpper(); //Make upper case if (File.Exists(Path.Combine(RenderState.settings.UnpackDir, exmlpath))) { load_mode = 0; //Load Exml } else if (File.Exists(Path.Combine(RenderState.settings.UnpackDir, filepath))) { load_mode = 1; //Load MBIN from file } else if (resMgr.NMSFileToArchiveMap.ContainsKey(filepath)) { load_mode = 2; //Extract file from archive } else if (resMgr.NMSFileToArchiveMap.ContainsKey("/" + filepath)) //AMUMSS BULLSHIT { effective_filepath = "/" + filepath; load_mode = 2; //Extract file from archive } else { CallBacks.Log("File: " + filepath + " Not found in PAKs or local folders. "); Util.showError("File: " + filepath + " Not found in PAKs or local folders. ", "Error"); return(null); } try { switch (load_mode) { case 0: //Load EXML { string xml = File.ReadAllText(Path.Combine(RenderState.settings.UnpackDir, exmlpath)); template = EXmlFile.ReadTemplateFromString(xml); break; } case 1: //Load MBIN { string eff_path = Path.Combine(RenderState.settings.UnpackDir, filepath); MBINFile mbinf = new MBINFile(eff_path); mbinf.Load(); template = mbinf.GetData(); mbinf.Dispose(); break; } case 2: //Load File from Archive { Stream file = resMgr.NMSFileToArchiveMap[effective_filepath].ExtractFile(effective_filepath); MBINFile mbinf = new MBINFile(file); mbinf.Load(); template = mbinf.GetData(); mbinf.Dispose(); break; } } } catch (Exception ex) { if (ex is System.IO.DirectoryNotFoundException || ex is System.IO.FileNotFoundException) { Util.showError("File " + effective_filepath + " Not Found...", "Error"); } else if (ex is System.IO.IOException) { Util.showError("File " + effective_filepath + " problem...", "Error"); } else if (ex is System.Reflection.TargetInvocationException) { Util.showError("libMBIN failed to decompile file. If this is a vanilla file, contact the MbinCompiler developer", "Error"); } else { Util.showError("Unhandled Exception " + ex.Message, "Error"); } return(null); } #if DEBUG //Save NMSTemplate to exml string data = EXmlFile.WriteTemplate(template); string path = Path.Combine("Temp", filepath + ".exml"); Directory.CreateDirectory(Path.GetDirectoryName(path)); File.WriteAllText(path, data); #endif return(template); }
/// <summary>Convert MBIN to EXML</summary> /// <param name="fIn">Source file</param> /// <param name="msOut">Output stream</param> /// <param name="fileOut">Output file path. Passed through as the return value. Not actually used.</param> /// <returns>fileOut</returns> private static string ConvertMBIN(string inputPath, FileStream fIn, MemoryStream msOut, string fileOut) { var mbin = new MBINFile(fIn); if (!(mbin.Load() && mbin.Header.IsValid)) { throw new InvalidDataException("Not a valid MBIN file!"); } var type = NMSTemplate.GetTemplateType(mbin.Header.GetXMLTemplateName()); var nms = (NMSAttribute)(type.GetCustomAttributes(typeof(NMSAttribute), false)?[0] ?? null); var broken = nms.Broken; // GUID's for the old files ulong[] UnsupportedGUIDs = new ulong[] { }; var mismatch = (mbin.Header.TemplateGUID != nms.GUID); bool unsupported = (UnsupportedGUIDs.Contains(mbin.Header.TemplateGUID)); //if ( broken && mismatch ) { // FileIsUnsupported( fIn.Name, mbin ); //} else if (broken) { FileIsBroken(inputPath, mbin); } else if (unsupported) { FileIsUnused(inputPath, mbin); } else if (mismatch) { FileIsUnrecognized(inputPath, mbin, nms.GUID); } var sw = new StreamWriter(msOut); NMSTemplate data = null; string msg = ""; try { msg = $"Failed to read {mbin.Header.GetXMLTemplateName()} from MBIN."; data = mbin.GetData(); if (data is null) { throw new InvalidDataException("Invalid MBIN data."); } msg = $"Failed serializing {mbin.Header.GetXMLTemplateName()} to EXML."; string exml = EXmlFile.WriteTemplate(data, HideVersionInfo); if (StreamToConsole) { EmitInfo($""); EmitInfo($"[INPUT]: {inputPath}"); EmitInfo($"{exml}"); } else { sw.Write(exml); sw.Flush(); if (msOut.Length == 0) { throw new InvalidDataException("Invalid EXML data."); } } } catch (Exception e) { throw new MbinException(msg, e, fIn.Name, mbin); } return(fileOut); }