Skip to content

Commit

Permalink
Fix
Browse files Browse the repository at this point in the history
  • Loading branch information
tomrus88 committed May 22, 2016
1 parent f7801cc commit 3c09796
Show file tree
Hide file tree
Showing 17 changed files with 119 additions and 84 deletions.
2 changes: 1 addition & 1 deletion DBC Viewer/DBC Viewer.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@
</ItemGroup>
<ItemGroup>
<Compile Include="BinaryFormatter.cs" />
<Compile Include="BinaryReaderExtensions.cs" />
<Compile Include="Extensions.cs" />
<Compile Include="FilterExpressions.cs" />
<Compile Include="Forms\DefinitionCatalog.cs">
<SubType>Form</SubType>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public string GetCoordsAsString()
}
#endregion

static class BinaryReaderExtensions
static class Extensions
{
public static BinaryReader FromFile(string fileName)
{
Expand Down Expand Up @@ -313,5 +313,17 @@ public static T Read<T>(this BinaryReader reader, ColumnMeta meta) where T : str

return (T)value;
}

public static void AppendFormatLine(this StringBuilder sb, string format, params object[] args)
{
sb.AppendFormat(format, args);
sb.AppendLine();
}

public static void AppendFormatLine(this StringBuilder sb, IFormatProvider provider, string format, params object[] args)
{
sb.AppendFormat(provider, format, args);
sb.AppendLine();
}
}
}
14 changes: 7 additions & 7 deletions DBC Viewer/Forms/MainForm.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

57 changes: 26 additions & 31 deletions DBC Viewer/Forms/MainForm.EventHandlers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,6 @@ private void dataGridView1_CellToolTipTextNeeded(object sender, DataGridViewCell
CultureInfo culture = CultureInfo.InvariantCulture;
object value = dataGridView1[e.ColumnIndex, e.RowIndex].Value;

//val = (ulong)Convert.ChangeType(value, dataType);

if (dataType != typeof(string))
{
if (dataType == typeof(sbyte))
Expand Down Expand Up @@ -72,19 +70,20 @@ private void dataGridView1_CellToolTipTextNeeded(object sender, DataGridViewCell
}

StringBuilder sb = new StringBuilder();
sb.AppendFormat(culture, "Integer: {0:D}{1}", val, Environment.NewLine);
sb.AppendFormat(new BinaryFormatter(), "HEX: {0:X}{1}", val, Environment.NewLine);
sb.AppendFormat(new BinaryFormatter(), "BIN: {0:B}{1}", val, Environment.NewLine);
sb.AppendFormat(culture, "Float: {0}{1}", BitConverter.ToSingle(BitConverter.GetBytes(val), 0), Environment.NewLine);
sb.AppendFormat(culture, "Double: {0}{1}", BitConverter.ToDouble(BitConverter.GetBytes(val), 0), Environment.NewLine);

try
sb.AppendFormatLine(culture, "Integer: {0:D}", val);
sb.AppendFormatLine(new BinaryFormatter(), "HEX: {0:X}", val);
sb.AppendFormatLine(new BinaryFormatter(), "BIN: {0:B}", val);
sb.AppendFormatLine(culture, "Float: {0}", BitConverter.ToSingle(BitConverter.GetBytes(val), 0));
sb.AppendFormatLine(culture, "Double: {0}", BitConverter.ToDouble(BitConverter.GetBytes(val), 0));

string strValue;
if( m_dbreader.StringTable != null && m_dbreader.StringTable.TryGetValue((int)val, out strValue))
{
sb.AppendFormat(culture, "String: {0}{1}", !(m_dbreader is WDBReader) ? m_dbreader.StringTable[(int)val] : string.Empty, Environment.NewLine);
sb.AppendFormatLine(culture, "String: {0}", strValue);
}
catch
else
{
sb.AppendFormat(culture, "String: <empty>{0}", Environment.NewLine);
sb.AppendFormatLine(culture, "String: <empty>");
}

e.ToolTipText = sb.ToString();
Expand All @@ -98,18 +97,11 @@ private void dataGridView1_CurrentCellChanged(object sender, EventArgs e)

private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
m_startTime = DateTime.Now;

string file = (string)e.Argument;

try
{
m_dbreader = DBReaderFactory.GetReader(file, m_definition);
}
catch (Exception ex)
{
ShowErrorMessageBox(ex.Message);
e.Cancel = true;
return;
}
m_dbreader = DBReaderFactory.GetReader(file, m_definition);

m_fields = new List<Field>(m_definition.Fields);

Expand Down Expand Up @@ -315,18 +307,21 @@ private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerComple

if (e.Error != null)
{
ShowErrorMessageBox(e.Error.ToString());
toolStripStatusLabel1.Text = "Error.";
}
else if (e.Cancelled == true)
{
toolStripStatusLabel1.Text = "Error in definitions.";
StartEditor();
if (e.Error is InvalidDataException)
{
ShowErrorMessageBox(e.Error.ToString());
statusToolStripLabel.Text = "Error.";
}
else
{
statusToolStripLabel.Text = "Error in definitions.";
StartEditor();
}
}
else
{
TimeSpan total = DateTime.Now - m_startTime;
toolStripStatusLabel1.Text = string.Format(CultureInfo.InvariantCulture, "Ready. Loaded in {0} sec", total.TotalSeconds);
statusToolStripLabel.Text = string.Format(CultureInfo.InvariantCulture, "Ready. Loaded in {0} sec", total.TotalSeconds);
Text = string.Format(CultureInfo.InvariantCulture, "DBC Viewer - {0}", e.Result);
SetDataSource(m_dataTable.DefaultView);
InitColumnsFilter();
Expand Down Expand Up @@ -390,7 +385,7 @@ private void runPluginToolStripMenuItem_Click(object sender, EventArgs e)
if (selector.NewPlugin != null)
m_catalog.Catalogs.Add(new AssemblyCatalog(selector.NewPlugin));

toolStripStatusLabel1.Text = "Plugin working...";
statusToolStripLabel.Text = "Plugin working...";
Thread pluginThread = new Thread(() => RunPlugin(selector.PluginIndex));
pluginThread.Start();
}
Expand Down
5 changes: 2 additions & 3 deletions DBC Viewer/Forms/MainForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public partial class MainForm : Form
public void PluginFinished(int result)
{
var msg = string.Format("Plugin finished! {0} rows affected.", result);
toolStripStatusLabel1.Text = msg;
statusToolStripLabel.Text = msg;
MessageBox.Show(msg);
}

Expand Down Expand Up @@ -79,9 +79,8 @@ private void LoadFile(string file)
}

toolStripProgressBar1.Visible = true;
toolStripStatusLabel1.Text = "Loading...";
statusToolStripLabel.Text = "Loading...";

m_startTime = DateTime.Now;
backgroundWorker1.RunWorkerAsync(file);
}

Expand Down
5 changes: 4 additions & 1 deletion DBC Viewer/Readers/ADBReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,13 @@ public IEnumerable<BinaryReader> Rows
}

public bool IsSparseTable { get { return false; } }
public string FileName { get; private set; }

public ADBReader(string fileName)
{
using (var reader = BinaryReaderExtensions.FromFile(fileName))
FileName = fileName;

using (var reader = Extensions.FromFile(fileName))
{
if (reader.BaseStream.Length < HeaderSize)
{
Expand Down
7 changes: 5 additions & 2 deletions DBC Viewer/Readers/DB2Reader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace DBCViewer
class DB2Reader : IClientDBReader
{
private const int HeaderSize = 48;
private const uint DB2FmtSig = 0x32424457; // WDB2
public const uint DB2FmtSig = 0x32424457; // WDB2

public int RecordsCount { get; private set; }
public int FieldsCount { get; private set; }
Expand All @@ -31,10 +31,13 @@ public IEnumerable<BinaryReader> Rows
}

public bool IsSparseTable { get { return false; } }
public string FileName { get; private set; }

public DB2Reader(string fileName)
{
using (var reader = BinaryReaderExtensions.FromFile(fileName))
FileName = fileName;

using (var reader = Extensions.FromFile(fileName))
{
if (reader.BaseStream.Length < HeaderSize)
{
Expand Down
7 changes: 5 additions & 2 deletions DBC Viewer/Readers/DB3Reader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace DBCViewer
class DB3Reader : IClientDBReader
{
private const int HeaderSize = 48;
private const uint DB3FmtSig = 0x33424457; // WDB3
public const uint DB3FmtSig = 0x33424457; // WDB3

public int RecordsCount { get; private set; }
public int FieldsCount { get; private set; }
Expand All @@ -32,10 +32,13 @@ public IEnumerable<BinaryReader> Rows
}

public bool IsSparseTable { get { return false; } }
public string FileName { get; private set; }

public DB3Reader(string fileName)
{
using (var reader = BinaryReaderExtensions.FromFile(fileName))
FileName = fileName;

using (var reader = Extensions.FromFile(fileName))
{
if (reader.BaseStream.Length < HeaderSize)
{
Expand Down
16 changes: 11 additions & 5 deletions DBC Viewer/Readers/DB4Reader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace DBCViewer
class DB4Reader : IClientDBReader
{
private const int HeaderSize = 52;
private const uint DB4FmtSig = 0x34424457; // WDB4
public const uint DB4FmtSig = 0x34424457; // WDB4

public int RecordsCount => Lookup.Count;
public int FieldsCount { get; private set; }
Expand All @@ -32,19 +32,20 @@ public IEnumerable<BinaryReader> Rows
}

public bool IsSparseTable { get; private set; }
public string FileName { get; private set; }

public DB4Reader(string fileName)
public DB4Reader(Stream stream)
{
using (var reader = BinaryReaderExtensions.FromFile(fileName))
using (var reader = new BinaryReader(stream, Encoding.UTF8))
{
if (reader.BaseStream.Length < HeaderSize)
{
throw new InvalidDataException(string.Format("File {0} is corrupted!", fileName));
throw new InvalidDataException(string.Format("File {0} is corrupted!", FileName));
}

if (reader.ReadUInt32() != DB4FmtSig)
{
throw new InvalidDataException(string.Format("File {0} isn't valid DB2 file!", fileName));
throw new InvalidDataException(string.Format("File {0} isn't valid DB2 file!", FileName));
}

int recordsCount = reader.ReadInt32();
Expand Down Expand Up @@ -177,5 +178,10 @@ public DB4Reader(string fileName)
}
}
}

public DB4Reader(string fileName) : this(new FileStream(fileName, FileMode.Open))
{
FileName = fileName;
}
}
}
16 changes: 11 additions & 5 deletions DBC Viewer/Readers/DB5Reader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class ColumnMeta
class DB5Reader : IClientDBReader
{
private const int HeaderSize = 48;
private const uint DB5FmtSig = 0x35424457; // WDB5
public const uint DB5FmtSig = 0x35424457; // WDB5
List<ColumnMeta> columnMeta;

public int RecordsCount => Lookup.Count;
Expand All @@ -41,19 +41,20 @@ public IEnumerable<BinaryReader> Rows
}

public bool IsSparseTable { get; private set; }
public string FileName { get; private set; }

public DB5Reader(string fileName, Table def)
public DB5Reader(Stream stream)
{
using (var reader = BinaryReaderExtensions.FromFile(fileName))
using (var reader = new BinaryReader(stream, Encoding.UTF8))
{
if (reader.BaseStream.Length < HeaderSize)
{
throw new InvalidDataException(string.Format("File {0} is corrupted!", fileName));
throw new InvalidDataException(string.Format("File {0} is corrupted!", FileName));
}

if (reader.ReadUInt32() != DB5FmtSig)
{
throw new InvalidDataException(string.Format("File {0} isn't valid DB2 file!", fileName));
throw new InvalidDataException(string.Format("File {0} isn't valid DB2 file!", FileName));
}

int recordsCount = reader.ReadInt32();
Expand Down Expand Up @@ -207,5 +208,10 @@ public DB5Reader(string fileName, Table def)
}
}
}

public DB5Reader(string fileName) : this(new FileStream(fileName, FileMode.Open))
{
FileName = fileName;
}
}
}
5 changes: 4 additions & 1 deletion DBC Viewer/Readers/DBCReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,13 @@ public IEnumerable<BinaryReader> Rows
}

public bool IsSparseTable { get { return false; } }
public string FileName { get; private set; }

public DBCReader(string fileName)
{
using (var reader = BinaryReaderExtensions.FromFile(fileName))
FileName = fileName;

using (var reader = Extensions.FromFile(fileName))
{
if (reader.BaseStream.Length < HeaderSize)
{
Expand Down
2 changes: 1 addition & 1 deletion DBC Viewer/Readers/DBCReaderGeneric.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public T this[int row]

public DBCReaderGeneric(string fileName)
{
using (var reader = BinaryReaderExtensions.FromFile(fileName))
using (var reader = Extensions.FromFile(fileName))
{
if (reader.BaseStream.Length < HeaderSize)
{
Expand Down
Loading

0 comments on commit 3c09796

Please sign in to comment.