Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check for a valid path of DBC files before loading main form and allow change DbcPath if path/dbc not found #27

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion SpellWork/DBC/DBC.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
89 changes: 63 additions & 26 deletions SpellWork/DBC/DBCReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,45 +14,82 @@ static class DBCReader
public static Dictionary<uint, T> ReadDBC<T>(Dictionary<uint, string> strDict) where T : struct
{
var dict = new Dictionary<uint, T>();
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<DbcHeader>();
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<T>();
// read dbc header
var header = reader.ReadStruct<DbcHeader>();
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<T>();
var entry = reader.ReadStruct<T>();

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<T>();
return ReadDBC<T>(strDict);
}

throw;
}

return dict;
}

private static void CheckDirectory<T>()
{
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!");
}
}
}