From a972d62010d0ab65168d77b6b8142cc50639766c Mon Sep 17 00:00:00 2001 From: xjose93 Date: Fri, 26 Oct 2018 05:49:55 +0200 Subject: [PATCH] Check for a valid path of DBC files before loading main form and allow change DbcPath if path/dbc not found. --- SpellWork/DBC/DBC.cs | 1 - SpellWork/DBC/DBCReader.cs | 89 +++++++++++++++++++++++++++----------- 2 files changed, 63 insertions(+), 27 deletions(-) diff --git a/SpellWork/DBC/DBC.cs b/SpellWork/DBC/DBC.cs index dc8e05d2..cc611310 100644 --- a/SpellWork/DBC/DBC.cs +++ b/SpellWork/DBC/DBC.cs @@ -6,7 +6,6 @@ namespace SpellWork.DBC public static class DBC { public const string Version = "SpellWork 3.3.5a (12340)"; - public const string DbcPath = @"dbc"; public const int MaxDbcLocale = 16; public const int MaxReagentCount = 8; diff --git a/SpellWork/DBC/DBCReader.cs b/SpellWork/DBC/DBCReader.cs index c2efe481..424a204d 100644 --- a/SpellWork/DBC/DBCReader.cs +++ b/SpellWork/DBC/DBCReader.cs @@ -14,45 +14,82 @@ static class DBCReader public static Dictionary ReadDBC(Dictionary strDict) where T : struct { var dict = new Dictionary(); - var fileName = Path.Combine(DBC.DbcPath, typeof(T).Name + ".dbc").Replace("Entry", String.Empty); + var fileName = Path.Combine(Properties.Settings.Default.DbcPath, typeof(T).Name + ".dbc").Replace("Entry", String.Empty); - using (var reader = new BinaryReader(new FileStream(fileName, FileMode.Open, FileAccess.Read), Encoding.UTF8)) + try { - if (!File.Exists(fileName)) - throw new FileNotFoundException(); - // read dbc header - var header = reader.ReadStruct(); - var size = Marshal.SizeOf(typeof(T)); + using (var reader = new BinaryReader(new FileStream(fileName, FileMode.Open, FileAccess.Read), Encoding.UTF8)) + { + if (!File.Exists(fileName)) + CheckDirectory(); + // read dbc header + var header = reader.ReadStruct(); + var size = Marshal.SizeOf(typeof(T)); - if (!header.IsDBC) - throw new Exception(fileName + " is not DBC files!"); + if (!header.IsDBC) + throw new Exception(fileName + " is not DBC files!"); - if (header.RecordSize != size) - throw new Exception(string.Format("Size of row in DBC file ({0}) != size of DBC struct ({1}) in DBC: {2}", header.RecordSize, size, fileName)); + if (header.RecordSize != size) + throw new Exception(string.Format("Size of row in DBC file ({0}) != size of DBC struct ({1}) in DBC: {2}", header.RecordSize, size, fileName)); - // read dbc data - for (var r = 0; r < header.RecordsCount; ++r) - { - var key = reader.ReadUInt32(); - reader.BaseStream.Position -= 4; + // read dbc data + for (var r = 0; r < header.RecordsCount; ++r) + { + var key = reader.ReadUInt32(); + reader.BaseStream.Position -= 4; - var entry = reader.ReadStruct(); + var entry = reader.ReadStruct(); - dict.Add(key, entry); - } + dict.Add(key, entry); + } - // read dbc strings - if (strDict != null) - { - while (reader.BaseStream.Position != reader.BaseStream.Length) + // read dbc strings + if (strDict != null) { - var offset = (uint)(reader.BaseStream.Position - header.StartStringPosition); - var str = reader.ReadCString(); - strDict.Add(offset, str); + while (reader.BaseStream.Position != reader.BaseStream.Length) + { + var offset = (uint)(reader.BaseStream.Position - header.StartStringPosition); + var str = reader.ReadCString(); + strDict.Add(offset, str); + } } } } + catch (Exception e) + { + if (e is System.IO.DirectoryNotFoundException || e is System.IO.FileNotFoundException) + { + CheckDirectory(); + return ReadDBC(strDict); + } + + throw; + } + return dict; } + + private static void CheckDirectory() + { + System.Windows.Forms.FolderBrowserDialog fbd = new System.Windows.Forms.FolderBrowserDialog() + { + Description = "Select your DBC files path:", + ShowNewFolderButton = false, + RootFolder = Environment.SpecialFolder.MyComputer + }; + + if (fbd.ShowDialog() == System.Windows.Forms.DialogResult.OK) + { + Properties.Settings.Default.DbcPath = fbd.SelectedPath; + + var fileName = Path.Combine(Properties.Settings.Default.DbcPath, typeof(T).Name + ".dbc").Replace("Entry", String.Empty); + if (!File.Exists(fileName)) + throw new FileNotFoundException(); + + Properties.Settings.Default.Save(); + } + else + throw new Exception("You didn't select a valid path!"); + } } }