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);
}
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
}
}
@@ -22,14 +22,6 @@ namespace Game.DataStorage
{
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)
{
m_array = data;
@@ -43,7 +35,7 @@ namespace Game.DataStorage
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;
return result;
}
@@ -51,51 +43,23 @@ namespace Game.DataStorage
public ulong ReadUInt64(int bitWidth, int bitOffset)
{
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;
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);
if (isSigned)
{
ulong result = ReadUInt64(bitWidth, bitOffset);
if (isSigned)
{
ulong mask = 1ul << (bitWidth - 1);
result = (result ^ mask) - mask;
}
return *(Value64*)&result;
ulong mask = 1ul << (bitWidth - 1);
result = (result ^ mask) - mask;
}
}
}
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];
}
}
var ulongBytes = BitConverter.GetBytes(result);
byte[] data = new byte[NextPow2((bitWidth + 7) / 8)];
Buffer.BlockCopy(ulongBytes, 0, data, 0, data.Length);
return data;
}
@@ -111,5 +75,12 @@ namespace Game.DataStorage
v++;
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
{
for (int x = 0; x < ColumnMeta[f].ArraySize; x++)
data.AddRange(bitReader.ReadValue64(bitSize).GetBytes(bitSize));
data.AddRange(bitReader.ReadValue(bitSize));
}
break;
@@ -429,7 +429,7 @@ namespace Game.DataStorage
}
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;