diff --git a/MDF-Manager/HelperFunctions.cs b/MDF-Manager/HelperFunctions.cs index 228ad51..710f7db 100644 --- a/MDF-Manager/HelperFunctions.cs +++ b/MDF-Manager/HelperFunctions.cs @@ -4,6 +4,7 @@ using System.IO; using System.Security.Cryptography; using System.Text; +using System.Text.RegularExpressions; using System.Windows; using System.Windows.Media; @@ -137,4 +138,12 @@ public static FileStream OpenFileStream(string filename, FileMode mode = FileMod } } } + static public class StringExtensions + { + static public string ReplaceInsensitive(this string str, string from, string to) + { + str = Regex.Replace(str, from, to, RegexOptions.IgnoreCase); + return str; + } + } } diff --git a/MDF-Manager/MainWindow.xaml.cs b/MDF-Manager/MainWindow.xaml.cs index 5da5b30..e0760d6 100644 --- a/MDF-Manager/MainWindow.xaml.cs +++ b/MDF-Manager/MainWindow.xaml.cs @@ -567,63 +567,76 @@ private void btnDelete_Click(object sender, RoutedEventArgs e) private void ExtendCompendium(OpenFileDialog dialog) { + //set some error info up here for reference if crash + string lastMDF = " "; + MDFTypes lastType = MDFTypes.RE2DMC5; if (dialog.ShowDialog() == true) { - string fullPath = dialog.FileName; - string searchPath = System.IO.Path.GetDirectoryName(fullPath); - EnumerationOptions enops = new EnumerationOptions(); - enops.RecurseSubdirectories = true; - string[] mdfs = System.IO.Directory.GetFiles(searchPath, "*.mdf2.*", enops);//brilliant - foreach (string mdf in mdfs) - { - //gonna abuse the fact everything is by reference - MDFTypes type = (MDFTypes)Convert.ToInt32(System.IO.Path.GetExtension(mdf.Replace(".stm", "").Replace(".x64", "")).Replace(".", "")); - CompendiumTopLevel parent = null; - switch (type) - { - case MDFTypes.RE7: - parent = compendium.RE7; - break; - case MDFTypes.RE2DMC5: - parent = compendium.RE2DMC5; - break; - case MDFTypes.RE3: - parent = compendium.RE3; - break; - case MDFTypes.MHRiseRE8: - parent = compendium.MHRiseRE8; - break; - default: - break; - } - if (parent != null) + try + { + string fullPath = dialog.FileName; + string searchPath = System.IO.Path.GetDirectoryName(fullPath); + EnumerationOptions enops = new EnumerationOptions(); + enops.RecurseSubdirectories = true; + string[] mdfs = System.IO.Directory.GetFiles(searchPath, "*.mdf2.*", enops);//brilliant + foreach (string mdf in mdfs) { - BinaryReader br = HelperFunctions.OpenFileR(mdf, Encoding.Unicode); - if (br != null) + //gonna abuse the fact everything is by reference + string sType = System.IO.Path.GetExtension(mdf.ReplaceInsensitive(".stm", "").ReplaceInsensitive(".x64", "")).Replace(".",""); + MDFTypes type = (MDFTypes)Convert.ToInt32(sType); + lastMDF = mdf; + lastType = type; + CompendiumTopLevel parent = null; + switch (type) { - MDFFile file = new MDFFile(mdf, br, type); - foreach (Material mat in file.Materials) + case MDFTypes.RE7: + parent = compendium.RE7; + break; + case MDFTypes.RE2DMC5: + parent = compendium.RE2DMC5; + break; + case MDFTypes.RE3: + parent = compendium.RE3; + break; + case MDFTypes.MHRiseRE8: + parent = compendium.MHRiseRE8; + break; + default: + break; + } + if (parent != null) + { + BinaryReader br = HelperFunctions.OpenFileR(mdf, Encoding.Unicode); + if (br != null) { - CompendiumEntryHeader mParent = null; - mParent = parent.FindItem(x => x.MMTRName == System.IO.Path.GetFileNameWithoutExtension(mat.MasterMaterial)); - if (mParent == null) + MDFFile file = new MDFFile(mdf, br, type); + foreach (Material mat in file.Materials) { - mParent = new CompendiumEntryHeader(System.IO.Path.GetFileNameWithoutExtension(mat.MasterMaterial)); - parent.AddChild(mParent); - } - int check = mParent.FindEntry(mdf); - if (check == -1) - { - CompendiumEntry ce = new CompendiumEntry(mdf); - mParent.Items.Add(ce); + CompendiumEntryHeader mParent = null; + mParent = parent.FindItem(x => x.MMTRName == System.IO.Path.GetFileNameWithoutExtension(mat.MasterMaterial)); + if (mParent == null) + { + mParent = new CompendiumEntryHeader(System.IO.Path.GetFileNameWithoutExtension(mat.MasterMaterial)); + parent.AddChild(mParent); + } + int check = mParent.FindEntry(mdf); + if (check == -1) + { + CompendiumEntry ce = new CompendiumEntry(mdf); + mParent.Items.Add(ce); + } } } - } + } } - + CompendiumChanged = true; + compendium.Sort(); + } + catch(Exception ex) + { + MessageBox.Show($"An error occurred during reading compendium entries:{ex}\n\n" + + $"Diagnostic Information: MDF:{lastMDF} Version:{lastType}"); } - CompendiumChanged = true; - compendium.Sort(); } } private void RebaseCompendium(object sender, RoutedEventArgs e)