Skip to content
This repository has been archived by the owner on May 28, 2018. It is now read-only.

Commit

Permalink
Switch to properties
Browse files Browse the repository at this point in the history
  • Loading branch information
Warpten committed Nov 19, 2017
1 parent 9774238 commit 7e690a6
Show file tree
Hide file tree
Showing 8 changed files with 49 additions and 49 deletions.
1 change: 1 addition & 0 deletions DBFilesClient.NET/DBFilesClient.NET.nuspec
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
<owners>Warpten</owners>
<requireLicenseAcceptance>false</requireLicenseAcceptance>
<description>A blazing-fast DBC and DB2 file loader for World of Warcraft files.</description>
<projectUrl>http://github.com/Warpten/DBFilesClient.NET</projectUrl>
<tags>DBC DB2 WDBC WDB2 WDB3 WDB4 WDB5 WOW</tags>
</metadata>
</package>
6 changes: 3 additions & 3 deletions DBFilesClient.NET/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.1.4")]
[assembly: AssemblyFileVersion("1.1.4")]
[assembly: AssemblyVersion("1.1.5")]
[assembly: AssemblyFileVersion("1.1.5")]

[assembly: AssemblyInformationalVersion("1.1.4")]
[assembly: AssemblyInformationalVersion("1.1.5")]
43 changes: 21 additions & 22 deletions DBFilesClient.NET/Reader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,24 +65,23 @@ internal virtual MethodInfo GetPrimitiveLoader(Type typeInfo, int fieldIndex)
return _binaryReaderMethods[Type.GetTypeCode(typeInfo)];
}

internal virtual MethodInfo GetPrimitiveLoader(FieldInfo fieldInfo, int fieldIndex)
internal virtual MethodInfo GetPrimitiveLoader(PropertyInfo propertyInfo, int fieldIndex)
{
var fieldType = fieldInfo.FieldType;
var fieldType = propertyInfo.PropertyType;
if (fieldType.IsArray)
fieldType = fieldType.GetElementType();

var typeCode = Type.GetTypeCode(fieldType);
if (typeCode == TypeCode.Object)
return null;

MethodInfo methodInfo;
_binaryReaderMethods.TryGetValue(typeCode, out methodInfo);
_binaryReaderMethods.TryGetValue(typeCode, out MethodInfo methodInfo);
return methodInfo;
}

private Expression GetSimpleReaderExpression(FieldInfo fieldInfo, int fieldIndex, Expression readerExpr)
private Expression GetSimpleReaderExpression(PropertyInfo propertyInfo, int fieldIndex, Expression readerExpr)
{
var fieldType = fieldInfo.FieldType;
var fieldType = propertyInfo.PropertyType;
if (fieldType.IsArray)
fieldType = fieldType.GetElementType();

Expand All @@ -106,7 +105,7 @@ private Expression GetSimpleReaderExpression(FieldInfo fieldInfo, int fieldIndex
Expression callExpression;
if (typeCode != TypeCode.Object)
{
var callVirt = GetPrimitiveLoader(fieldInfo, fieldIndex);
var callVirt = GetPrimitiveLoader(propertyInfo, fieldIndex);

callExpression = Expression.Call(readerExpr, callVirt);
}
Expand Down Expand Up @@ -139,36 +138,36 @@ protected virtual void GenerateRecordLoader()
// Instantiate the return value.
expressions.Add(Expression.Assign(resultExpr, Expression.New(typeof(T))));

var fields = typeof(T).GetFields(BindingFlags.Public | BindingFlags.Instance).ToArray();
var fields = typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance).ToArray();

if (fields.Length < FileHeader.FieldCount)
throw new InvalidOperationException(
$"Structure {typeof(T).Name} is missing fields ({fields.Length} found, {FileHeader.FieldCount} expected");
$"Structure {typeof(T).Name} is missing properties ({fields.Length} found, {FileHeader.FieldCount} expected");

for (var fieldIndex = 0; fieldIndex < FileHeader.FieldCount; ++fieldIndex)
{
var fieldInfo = fields[fieldIndex];
var propertyInfo = fields[fieldIndex];

var callExpression = GetSimpleReaderExpression(fieldInfo, fieldIndex, readerExpr);
var callExpression = GetSimpleReaderExpression(propertyInfo, fieldIndex, readerExpr);

if (!fieldInfo.FieldType.IsArray)
if (!propertyInfo.PropertyType.IsArray)
{
expressions.Add(Expression.Assign(
Expression.MakeMemberAccess(resultExpr, fieldInfo),
Expression.Convert(callExpression, fieldInfo.FieldType)));
Expression.MakeMemberAccess(resultExpr, propertyInfo),
Expression.Convert(callExpression, propertyInfo.PropertyType)));
}
else
{
var arraySize = GetArraySize(fieldInfo, fieldIndex);
var arraySize = GetArraySize(propertyInfo, fieldIndex);

var exitLabelExpr = Expression.Label();
var itrExpr = Expression.Variable(typeof(int));
expressions.Add(Expression.Block(
new[] { itrExpr },
// ReSharper disable once AssignNullToNotNullAttribute
Expression.Assign(
Expression.MakeMemberAccess(resultExpr, fieldInfo),
Expression.New(fieldInfo.FieldType.GetConstructor(new[] { typeof(int) }),
Expression.MakeMemberAccess(resultExpr, propertyInfo),
Expression.New(propertyInfo.PropertyType.GetConstructor(new[] { typeof(int) }),
Expression.Constant(arraySize))
),

Expand All @@ -177,9 +176,9 @@ protected virtual void GenerateRecordLoader()
Expression.IfThenElse(
Expression.LessThan(itrExpr, Expression.Constant(arraySize)),
Expression.Assign(
Expression.ArrayAccess(Expression.MakeMemberAccess(resultExpr, fieldInfo),
Expression.ArrayAccess(Expression.MakeMemberAccess(resultExpr, propertyInfo),
Expression.PostIncrementAssign(itrExpr)),
Expression.Convert(callExpression, fieldInfo.FieldType.GetElementType())),
Expression.Convert(callExpression, propertyInfo.PropertyType.GetElementType())),
Expression.Break(exitLabelExpr)),
exitLabelExpr)
));
Expand All @@ -191,11 +190,11 @@ protected virtual void GenerateRecordLoader()
RecordReader = Expression.Lambda<Func<Reader<T>, T>>(expressionBlock, readerExpr).Compile();
}

protected virtual int GetArraySize(FieldInfo fieldInfo, int fieldIndex)
protected virtual int GetArraySize(PropertyInfo propertyInfo, int fieldIndex)
{
var marshalAttr = fieldInfo.GetCustomAttribute<MarshalAsAttribute>();
var marshalAttr = propertyInfo.GetCustomAttribute<MarshalAsAttribute>();
if (marshalAttr == null)
throw new InvalidOperationException($"Field '{typeof(T).Name}.{fieldInfo.Name} is an array and needs to be decorated with MarshalAsAttribute!");
throw new InvalidOperationException($"Field '{typeof(T).Name}.{propertyInfo.Name} is an array and needs to be decorated with MarshalAsAttribute!");

return marshalAttr.SizeConst;
}
Expand Down
16 changes: 8 additions & 8 deletions DBFilesClient.NET/WDB5/Reader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,11 @@ internal override MethodInfo GetPrimitiveLoader(Type fieldType, int fieldIndex)
return base.GetPrimitiveLoader(fieldType, fieldIndex);
}

internal override MethodInfo GetPrimitiveLoader(FieldInfo fieldInfo, int fieldIndex)
internal override MethodInfo GetPrimitiveLoader(PropertyInfo propertyInfo, int fieldIndex)
{
var fieldData = FieldMeta[fieldIndex];

var fieldType = fieldInfo.FieldType;
var fieldType = propertyInfo.PropertyType;
if (fieldType.IsArray)
fieldType = fieldType.GetElementType();

Expand All @@ -78,12 +78,12 @@ internal override MethodInfo GetPrimitiveLoader(FieldInfo fieldInfo, int fieldIn
: base.GetPrimitiveLoader(typeof (byte), fieldIndex);
default:
throw new ArgumentOutOfRangeException(
$@"Field {fieldInfo.Name} has its metadata expose as an unsupported {
$@"Field {propertyInfo.Name} has its metadata expose as an unsupported {
fieldData.ByteSize}-bytes field!");
}
}

return base.GetPrimitiveLoader(fieldInfo, fieldIndex);
return base.GetPrimitiveLoader(propertyInfo, fieldIndex);
}

public override string ReadString()
Expand Down Expand Up @@ -259,23 +259,23 @@ protected virtual void LoadRecord(long recordPosition, int key, bool forceKey =
}
}

protected override int GetArraySize(FieldInfo fieldInfo, int fieldIndex)
protected override int GetArraySize(PropertyInfo propertyInfo, int fieldIndex)
{
var currentField = FieldMeta[fieldIndex];

var arraySize = 1;
if (fieldIndex + 1 < FieldMeta.Length)
arraySize = (FieldMeta[fieldIndex + 1].Position - currentField.Position) / currentField.ByteSize;
else if (fieldInfo.FieldType.IsArray)
else if (propertyInfo.PropertyType.IsArray)
{
var largestFieldSize = FieldMeta.Max(k => k.ByteSize);
var smallestFieldSize = FieldMeta.Min(k => k.ByteSize);

if (smallestFieldSize != largestFieldSize)
{
var marshalAttr = fieldInfo.GetCustomAttribute<MarshalAsAttribute>();
var marshalAttr = propertyInfo.GetCustomAttribute<MarshalAsAttribute>();
if (marshalAttr == null)
throw new InvalidStructureException($"{typeof(T).Name}.{fieldInfo.Name}'s size can't be guessed!");
throw new InvalidStructureException($"{typeof(T).Name}.{propertyInfo.Name}'s size can't be guessed!");

if (marshalAttr.SizeConst != 0)
arraySize = marshalAttr.SizeConst;
Expand Down
22 changes: 11 additions & 11 deletions DBFilesClient.NET/WDB6/Reader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,9 @@ protected override void GenerateRecordLoader()
BaseStream.Position = BaseStream.Length - FileHeader.CommonDataTableSize;

var columnCount = ReadInt32();
var fields = typeof (T).GetFields(BindingFlags.Public | BindingFlags.Instance).ToArray();
var fields = typeof (T).GetProperties(BindingFlags.Public | BindingFlags.Instance).ToArray();
for (var i = 0; i < columnCount; ++i)
_nonZeroValues[i] = new CommonData(this, fields[i].FieldType);
_nonZeroValues[i] = new CommonData(this, fields[i].PropertyType);

// Generate an Action<Reader<T>, T, int>
var expressionList = new List<Expression>();
Expand All @@ -81,12 +81,12 @@ protected override void GenerateRecordLoader()
var structureExpr = Expression.Parameter(typeof (T));
for (var i = FileHeader.FieldCount; i < FileHeader.TotalFieldCount; ++i)
{
var fieldInfo = fields[i];
if (fieldInfo.FieldType.IsArray)
var propertyInfo = fields[i];
if (propertyInfo.PropertyType.IsArray)
throw new InvalidOperationException("Array fields in off-stream data are not handled");

MethodInfo convertMethod = null;
switch (Type.GetTypeCode(fieldInfo.FieldType))
switch (Type.GetTypeCode(propertyInfo.PropertyType))
{
case TypeCode.Int32:
convertMethod = typeof(Convert).GetMethod("ToInt32", new[] {typeof(object)});
Expand Down Expand Up @@ -124,8 +124,8 @@ protected override void GenerateRecordLoader()
Expression.IfThen(
Expression.NotEqual(localExpr, Expression.Constant(null)),
Expression.Assign(
Expression.MakeMemberAccess(structureExpr, fieldInfo),
Expression.Convert(localExpr, fieldInfo.FieldType)))));
Expression.MakeMemberAccess(structureExpr, propertyInfo),
Expression.Convert(localExpr, propertyInfo.PropertyType)))));
}

var lambda = Expression.Lambda<Action<Reader<T>, T, int>>(
Expand Down Expand Up @@ -176,23 +176,23 @@ protected override void LoadRecord(long recordPosition, int key, bool forceKey =
TriggerRecordLoaded(key, record);
}

protected override int GetArraySize(FieldInfo fieldInfo, int fieldIndex)
protected override int GetArraySize(PropertyInfo propertyInfo, int fieldIndex)
{
var currentField = FieldMeta[fieldIndex];

var arraySize = 1;
if (fieldIndex + 1 < FieldMeta.Length)
arraySize = (FieldMeta[fieldIndex + 1].Position - currentField.Position) / currentField.ByteSize;
else if (fieldInfo.FieldType.IsArray)
else if (propertyInfo.PropertyType.IsArray)
{
var largestFieldSize = FieldMeta.Max(k => k.ByteSize);
var smallestFieldSize = FieldMeta.Min(k => k.ByteSize);

if (smallestFieldSize != largestFieldSize)
{
var marshalAttr = fieldInfo.GetCustomAttribute<MarshalAsAttribute>();
var marshalAttr = propertyInfo.GetCustomAttribute<MarshalAsAttribute>();
if (marshalAttr == null)
throw new InvalidStructureException($"{typeof(T).Name}.{fieldInfo.Name}'s size can't be guessed!");
throw new InvalidStructureException($"{typeof(T).Name}.{propertyInfo.Name}'s size can't be guessed!");

if (marshalAttr.SizeConst != 0)
arraySize = marshalAttr.SizeConst;
Expand Down
6 changes: 3 additions & 3 deletions DBFilesClient.NET/WDBC/Reader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@ protected override void LoadHeader()
FileHeader.HasStringTable = FileHeader.StringTableSize != 0;
FileHeader.StringTableOffset = BaseStream.Length - FileHeader.StringTableSize;

foreach (var fieldInfo in typeof (T).GetFields(BindingFlags.Public | BindingFlags.Instance))
foreach (var propertyInfo in typeof (T).GetProperties(BindingFlags.Public | BindingFlags.Instance))
{
if (fieldInfo.FieldType.IsArray)
FileHeader.FieldCount += fieldInfo.GetCustomAttribute<MarshalAsAttribute>()?.SizeConst ?? 0;
if (propertyInfo.PropertyType.IsArray)
FileHeader.FieldCount += propertyInfo.GetCustomAttribute<MarshalAsAttribute>()?.SizeConst ?? 0;
else
++FileHeader.FieldCount;
}
Expand Down
Binary file added DBFilesClient.NET/nuget.exe
Binary file not shown.
4 changes: 2 additions & 2 deletions Tests/ReaderTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,11 @@ public void SpellXSpellVisual()

private static void PrintRecord<T>(int key, T instance)
{
foreach (var field in typeof(T).GetFields())
foreach (var field in typeof(T).GetProperties())
{
var value = field.GetValue(instance);

if (field.FieldType.IsArray)
if (field.PropertyType.IsArray)
{
var enumerableValue = value as IEnumerable;
var valueBuilder = new List<string>();
Expand Down

0 comments on commit 7e690a6

Please sign in to comment.