Skip to content

Commit

Permalink
more min max destruction
Browse files Browse the repository at this point in the history
  • Loading branch information
barncastle committed Apr 9, 2018
1 parent 454f9cf commit f62fd22
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 88 deletions.
112 changes: 55 additions & 57 deletions WDBXEditor/Reader/FileTypes/WDC1.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public class WDC1 : WDB6

public List<ColumnStructureEntry> ColumnMeta;
public RelationShipData RelationShipData;
public Dictionary<int, MinMax> MinMaxValues;
//public Dictionary<int, MinMax> MinMaxValues;

protected int[] columnSizes;
protected byte[] recordData;
Expand Down Expand Up @@ -202,9 +202,7 @@ public override void ReadHeader(ref BinaryReader dbReader, string signature)
FieldStructure.Add(new FieldStructureEntry(0, 0));
ColumnMeta.Add(new ColumnStructureEntry());
}

var max = offsetmap.OrderByDescending(x => x.Item1).First();


// Record Data
BitStream bitStream = new BitStream(recordData);
for (int i = 0; i < RecordCount; i++)
Expand Down Expand Up @@ -399,59 +397,59 @@ public void LoadDefinitionSizes(DBEntry entry)

public void SetColumnMinMaxValues(DBEntry entry)
{
MinMaxValues = new Dictionary<int, MinMax>();
int column = 0;
for (int i = 0; i < ColumnMeta.Count; i++)
{
// get the column type - skip strings
var type = entry.Data.Columns[column].DataType;
if (type == typeof(string))
{
column += ColumnMeta[i].ArraySize;
continue;
}

int bits = ColumnMeta[i].CompressionType == CompressionType.None ? FieldStructure[i].BitCount : ColumnMeta[i].BitWidth;
if ((bits & (bits - 1)) == 0 && bits >= 8) // power of two and >= sizeof(byte) means a standard type
{
column += ColumnMeta[i].ArraySize;
continue;
}

// calculate the min and max values
bool signed = Convert.ToBoolean(type.GetField("MinValue").GetValue(null));
bool isfloat = type == typeof(float);

//bool metaSigned = ColumnMeta[i].CompressionType == CompressionType.Immediate && (ColumnMeta[i].Cardinality & 1) == 1;
//if (ColumnMeta[i].CompressionType == CompressionType.Immediate && metaSigned != signed && i != IdIndex && !isfloat)
// throw new Exception($"Invalid sign for column {i}");

object max = signed ? long.MaxValue >> (64 - bits) : (object)(ulong.MaxValue >> (64 - bits));
object min = signed ? long.MinValue >> (64 - bits) : 0;
if (isfloat)
{
max = BitConverter.ToSingle(BitConverter.GetBytes((dynamic)max), 0);
min = BitConverter.ToSingle(BitConverter.GetBytes((dynamic)min), 0);
}

for (int j = 0; j < ColumnMeta[i].ArraySize; j++)
{
entry.Data.Columns[column].ExtendedProperties.Add("MaxValue", max);
if (signed || isfloat)
entry.Data.Columns[column].ExtendedProperties.Add("MinValue", min);

MinMax minmax = new MinMax()
{
Signed = signed,
MaxVal = max,
MinVal = min,
IsSingle = isfloat
};

MinMaxValues[column] = minmax;
column++;
}
}
//MinMaxValues = new Dictionary<int, MinMax>();
//int column = 0;
//for (int i = 0; i < ColumnMeta.Count; i++)
//{
// // get the column type - skip strings
// var type = entry.Data.Columns[column].DataType;
// if (type == typeof(string))
// {
// column += ColumnMeta[i].ArraySize;
// continue;
// }

// int bits = ColumnMeta[i].CompressionType == CompressionType.None ? FieldStructure[i].BitCount : ColumnMeta[i].BitWidth;
// if ((bits & (bits - 1)) == 0 && bits >= 8) // power of two and >= sizeof(byte) means a standard type
// {
// column += ColumnMeta[i].ArraySize;
// continue;
// }

// // calculate the min and max values
// bool signed = Convert.ToBoolean(type.GetField("MinValue").GetValue(null));
// bool isfloat = type == typeof(float);

// //bool metaSigned = ColumnMeta[i].CompressionType == CompressionType.Immediate && (ColumnMeta[i].Cardinality & 1) == 1;
// //if (ColumnMeta[i].CompressionType == CompressionType.Immediate && metaSigned != signed && i != IdIndex && !isfloat)
// // throw new Exception($"Invalid sign for column {i}");

// object max = signed ? long.MaxValue >> (64 - bits) : (object)(ulong.MaxValue >> (64 - bits));
// object min = signed ? long.MinValue >> (64 - bits) : 0;
// if (isfloat)
// {
// max = BitConverter.ToSingle(BitConverter.GetBytes((dynamic)max), 0);
// min = BitConverter.ToSingle(BitConverter.GetBytes((dynamic)min), 0);
// }

// for (int j = 0; j < ColumnMeta[i].ArraySize; j++)
// {
// entry.Data.Columns[column].ExtendedProperties.Add("MaxValue", max);
// if (signed || isfloat)
// entry.Data.Columns[column].ExtendedProperties.Add("MinValue", min);

// MinMax minmax = new MinMax()
// {
// Signed = signed,
// MaxVal = max,
// MinVal = min,
// IsSingle = isfloat
// };

// MinMaxValues[column] = minmax;
// column++;
// }
//}
}

public void AddRelationshipColumn(DBEntry entry)
Expand Down
62 changes: 31 additions & 31 deletions WDBXEditor/Storage/DBEntry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -837,37 +837,37 @@ private bool ValidateMinMaxValues(DataTable importTable, out string error)
{
error = "";

if (Header is WDC1 header)
{
foreach (var minmax in header.MinMaxValues)
{
Func<dynamic, dynamic, dynamic, bool> compare = (x, min, max) => x < min || x > max;

bool errored = false;

var values = importTable.Rows.Cast<DataRow>().Select(x => x.ItemArray[minmax.Key]);
if (minmax.Value.IsSingle)
{
errored = values.Any(x => compare((float)Convert.ChangeType(x, typeof(float)), minmax.Value.MinVal, minmax.Value.MaxVal));
}
else if (minmax.Value.Signed)
{
errored = values.Any(x => compare((long)Convert.ChangeType(x, typeof(long)), minmax.Value.MinVal, minmax.Value.MaxVal));
}
else
{
errored = values.Any(x => compare((ulong)Convert.ChangeType(x, typeof(ulong)), minmax.Value.MinVal, minmax.Value.MaxVal));
}

if (errored)
{
error = $"Import Failed: Imported data has out of range values for {Data.Columns[minmax.Key].ColumnName}.\n" +
$"(Min: {minmax.Value.MinVal}, Max: {minmax.Value.MaxVal})";

return false;
}
}
}
//if (Header is WDC1 header)
//{
// foreach (var minmax in header.MinMaxValues)
// {
// Func<dynamic, dynamic, dynamic, bool> compare = (x, min, max) => x < min || x > max;

// bool errored = false;

// var values = importTable.Rows.Cast<DataRow>().Select(x => x.ItemArray[minmax.Key]);
// if (minmax.Value.IsSingle)
// {
// errored = values.Any(x => compare((float)Convert.ChangeType(x, typeof(float)), minmax.Value.MinVal, minmax.Value.MaxVal));
// }
// else if (minmax.Value.Signed)
// {
// errored = values.Any(x => compare((long)Convert.ChangeType(x, typeof(long)), minmax.Value.MinVal, minmax.Value.MaxVal));
// }
// else
// {
// errored = values.Any(x => compare((ulong)Convert.ChangeType(x, typeof(ulong)), minmax.Value.MinVal, minmax.Value.MaxVal));
// }

// if (errored)
// {
// error = $"Import Failed: Imported data has out of range values for {Data.Columns[minmax.Key].ColumnName}.\n" +
// $"(Min: {minmax.Value.MinVal}, Max: {minmax.Value.MaxVal})";

// return false;
// }
// }
//}

return true;
}
Expand Down

0 comments on commit f62fd22

Please sign in to comment.