Core/DBStorage: Cleanup DBStorage reading

This commit is contained in:
hondacrx
2018-03-07 22:17:59 -05:00
parent c2ecb9fb2e
commit ed31c7b171
3 changed files with 19 additions and 55 deletions
-7
View File
@@ -352,13 +352,6 @@ namespace System
return FastStruct<T>.ArrayToStructure(result); return FastStruct<T>.ArrayToStructure(result);
} }
public static T Read<T>(this BinaryReader reader, int byteCount) where T : struct
{
byte[] result = reader.ReadBytes(byteCount == 0 ? FastStruct<T>.Size : byteCount);
return FastStruct<T>.ArrayToStructure(result);
}
#endregion #endregion
} }
} }
@@ -22,14 +22,6 @@ namespace Game.DataStorage
{ {
public class BitReader public class BitReader
{ {
private byte[] m_array;
private int m_readPos;
private int m_readOffset;
public int Position { get => m_readPos; set => m_readPos = value; }
public int Offset { get => m_readOffset; set => m_readOffset = value; }
public byte[] Data { get => m_array; set => m_array = value; }
public BitReader(byte[] data) public BitReader(byte[] data)
{ {
m_array = data; m_array = data;
@@ -43,7 +35,7 @@ namespace Game.DataStorage
public uint ReadUInt32(int numBits) public uint ReadUInt32(int numBits)
{ {
uint result = FastStruct<uint>.ArrayToStructure(ref m_array[m_readOffset + (m_readPos >> 3)]) << (32 - numBits - (m_readPos & 7)) >> (32 - numBits); uint result = BitConverter.ToUInt32(m_array, m_readOffset + (m_readPos >> 3)) << (32 - numBits - (m_readPos & 7)) >> (32 - numBits);
m_readPos += numBits; m_readPos += numBits;
return result; return result;
} }
@@ -51,14 +43,12 @@ namespace Game.DataStorage
public ulong ReadUInt64(int bitWidth, int bitOffset) public ulong ReadUInt64(int bitWidth, int bitOffset)
{ {
int bitsToRead = bitOffset & 7; int bitsToRead = bitOffset & 7;
ulong result = FastStruct<ulong>.ArrayToStructure(ref m_array[m_readOffset + (m_readPos >> 3)]) << (64 - bitsToRead - bitWidth) >> (64 - bitWidth); ulong result = BitConverter.ToUInt64(m_array, m_readOffset + (m_readPos >> 3)) << (64 - bitsToRead - bitWidth) >> (64 - bitWidth);
m_readPos += bitWidth; m_readPos += bitWidth;
return result; return result;
} }
public Value64 ReadValue64(int bitWidth, int bitOffset = 0, bool isSigned = false) public byte[] ReadValue(int bitWidth, int bitOffset = 0, bool isSigned = false)
{
unsafe
{ {
ulong result = ReadUInt64(bitWidth, bitOffset); ulong result = ReadUInt64(bitWidth, bitOffset);
if (isSigned) if (isSigned)
@@ -67,35 +57,9 @@ namespace Game.DataStorage
result = (result ^ mask) - mask; result = (result ^ mask) - mask;
} }
return *(Value64*)&result; var ulongBytes = BitConverter.GetBytes(result);
} byte[] data = new byte[NextPow2((bitWidth + 7) / 8)];
} Buffer.BlockCopy(ulongBytes, 0, data, 0, data.Length);
}
public struct Value64
{
unsafe fixed byte Value[8];
public T GetValue<T>() where T : struct
{
unsafe
{
fixed (byte* ptr = Value)
return FastStruct<T>.ArrayToStructure(ref ptr[0]);
}
}
public byte[] GetBytes(int bitSize)
{
byte[] data = new byte[NextPow2((bitSize + 7) / 8)];
unsafe
{
fixed (byte* ptr = Value)
{
for (var i = 0; i < data.Length; ++i)
data[i] = ptr[i];
}
}
return data; return data;
} }
@@ -111,5 +75,12 @@ namespace Game.DataStorage
v++; v++;
return Math.Max(v, 1); return Math.Max(v, 1);
} }
public int Position { get => m_readPos; set => m_readPos = value; }
public int Offset { get => m_readOffset; set => m_readOffset = value; }
private byte[] m_array;
private int m_readPos;
private int m_readOffset;
} }
} }
@@ -416,7 +416,7 @@ namespace Game.DataStorage
else else
{ {
for (int x = 0; x < ColumnMeta[f].ArraySize; x++) for (int x = 0; x < ColumnMeta[f].ArraySize; x++)
data.AddRange(bitReader.ReadValue64(bitSize).GetBytes(bitSize)); data.AddRange(bitReader.ReadValue(bitSize));
} }
break; break;
@@ -429,7 +429,7 @@ namespace Game.DataStorage
} }
else else
{ {
data.AddRange(bitReader.ReadValue64(bitWidth, ColumnMeta[f].BitOffset, ColumnMeta[f].Cardinality == 1).GetBytes(bitWidth)); data.AddRange(bitReader.ReadValue(bitWidth, ColumnMeta[f].BitOffset, ColumnMeta[f].Cardinality == 1));
} }
break; break;