Fixed DB2 loading and world login!!!!!

This commit is contained in:
hondacrx
2018-12-14 18:43:35 -05:00
parent 8e20114e10
commit f414068883
44 changed files with 779 additions and 852 deletions
@@ -15,72 +15,58 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
using Framework.IO;
using System;
using System.Runtime.CompilerServices;
using System.Text;
namespace Game.DataStorage
{
public class BitReader
{
private byte[] m_data;
private int m_bitPosition;
private int m_offset;
public int Position { get => m_bitPosition; set => m_bitPosition = value; }
public int Offset { get => m_offset; set => m_offset = value; }
public byte[] Data { get => m_data; set => m_data = value; }
public BitReader(byte[] data)
{
m_array = data;
m_data = data;
}
public BitReader(byte[] data, int offset)
{
m_array = data;
m_readOffset = offset;
m_data = data;
m_offset = offset;
}
public uint ReadUInt32(int numBits)
public T Read<T>(int numBits) where T : unmanaged
{
uint result = BitConverter.ToUInt32(m_array, m_readOffset + (m_readPos >> 3)) << (32 - numBits - (m_readPos & 7)) >> (32 - numBits);
m_readPos += numBits;
ulong result = Unsafe.As<byte, ulong>(ref m_data[m_offset + (m_bitPosition >> 3)]) << (64 - numBits - (m_bitPosition & 7)) >> (64 - numBits);
m_bitPosition += numBits;
return Unsafe.As<ulong, T>(ref result);
}
public T ReadSigned<T>(int numBits) where T : unmanaged
{
ulong result = Unsafe.As<byte, ulong>(ref m_data[m_offset + (m_bitPosition >> 3)]) << (64 - numBits - (m_bitPosition & 7)) >> (64 - numBits);
m_bitPosition += numBits;
ulong signedShift = (1UL << (numBits - 1));
result = (signedShift ^ result) - signedShift;
return Unsafe.As<ulong, T>(ref result);
}
public string ReadCString()
{
int start = m_bitPosition;
while (m_data[m_offset + (m_bitPosition >> 3)] != 0)
m_bitPosition += 8;
string result = Encoding.UTF8.GetString(m_data, m_offset + (start >> 3), (m_bitPosition - start) >> 3);
m_bitPosition += 8;
return result;
}
public ulong ReadUInt64(int bitWidth, int bitOffset)
{
int bitsToRead = bitOffset & 7;
ulong result = BitConverter.ToUInt64(m_array, m_readOffset + (m_readPos >> 3)) << (64 - bitsToRead - bitWidth) >> (64 - bitWidth);
m_readPos += bitWidth;
return result;
}
public byte[] ReadValue(int bitWidth, int bitOffset = 0, bool isSigned = false)
{
ulong result = ReadUInt64(bitWidth, bitOffset);
if (isSigned)
{
ulong mask = 1ul << (bitWidth - 1);
result = (result ^ mask) - mask;
}
var ulongBytes = BitConverter.GetBytes(result);
byte[] data = new byte[NextPow2((bitWidth + 7) / 8)];
Buffer.BlockCopy(ulongBytes, 0, data, 0, data.Length);
return data;
}
private int NextPow2(int v)
{
v--;
v |= v >> 1;
v |= v >> 2;
v |= v >> 4;
v |= v >> 8;
v |= v >> 16;
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;
}
}
@@ -17,6 +17,7 @@
using Framework.Constants;
using Framework.Database;
using Framework.Dynamic;
using Framework.GameMath;
using Framework.IO;
using System;
@@ -37,146 +38,130 @@ namespace Game.DataStorage
[Serializable]
public class DB6Storage<T> : Dictionary<uint, T>, IDB2Storage where T : new()
{
public void LoadData(int indexField, DB6FieldInfo[] helpers, HotfixStatements preparedStatement, HotfixStatements preparedStatementLocale)
public void LoadData(int indexField, HotfixStatements preparedStatement, HotfixStatements preparedStatementLocale)
{
SQLResult result = DB.Hotfix.Query(DB.Hotfix.GetPreparedStatement(preparedStatement));
if (!result.IsEmpty())
{
do
{
var idValue = result.Read<uint>(indexField == -1 ? 0 : indexField);
var id = result.Read<uint>(indexField == -1 ? 0 : indexField);
var obj = new T();
int index = 0;
for (var fieldIndex = 0; fieldIndex < helpers.Length; fieldIndex++)
int dbIndex = 0;
foreach (var f in typeof(T).GetFields())
{
var helper = helpers[fieldIndex];
if (helper.IsArray)
Type type = f.FieldType;
if (type.IsArray)
{
Array array = (Array)helper.Getter(obj);
for (var i = 0; i < array.Length; ++i)
Type arrayElementType = type.GetElementType();
if (arrayElementType.IsEnum)
arrayElementType = arrayElementType.GetEnumUnderlyingType();
Array array = (Array)f.GetValue(obj);
switch (Type.GetTypeCode(arrayElementType))
{
switch (Type.GetTypeCode(helper.FieldType))
{
case TypeCode.SByte:
helper.SetValue(array, result.Read<sbyte>(index++), i);
break;
case TypeCode.Byte:
helper.SetValue(array, result.Read<byte>(index++), i);
break;
case TypeCode.Int16:
helper.SetValue(array, result.Read<short>(index++), i);
break;
case TypeCode.UInt16:
helper.SetValue(array, result.Read<ushort>(index++), i);
break;
case TypeCode.Int32:
helper.SetValue(array, result.Read<int>(index++), i);
break;
case TypeCode.UInt32:
helper.SetValue(array, result.Read<uint>(index++), i);
break;
case TypeCode.Single:
helper.SetValue(array, result.Read<float>(index++), i);
break;
case TypeCode.String:
helper.SetValue(array, result.Read<string>(index++), i);
break;
case TypeCode.Object:
switch (helper.FieldType.Name)
{
case "Vector2":
var vector2 = new Vector2();
vector2.X = result.Read<float>(index++);
vector2.Y = result.Read<float>(index++);
helper.SetValue(array, vector2, i);
break;
case "Vector3":
var vector3 = new Vector3();
vector3.X = result.Read<float>(index++);
vector3.Y = result.Read<float>(index++);
vector3.Z = result.Read<float>(index++);
helper.SetValue(array, vector3, i);
break;
case "LocalizedString":
LocalizedString locString = new LocalizedString();
locString[Global.WorldMgr.GetDefaultDbcLocale()] = result.Read<string>(index++);
helper.SetValue(array, locString, i);
break;
default:
Log.outError(LogFilter.ServerLoading, "Wrong Array Type: {0}", helper.FieldType.Name);
break;
}
break;
default:
Log.outError(LogFilter.ServerLoading, "Wrong Array Type: {0}", helper.FieldType.Name);
break;
}
case TypeCode.SByte:
f.SetValue(obj, ReadArray<sbyte>(result, dbIndex, array.Length));
break;
case TypeCode.Byte:
f.SetValue(obj, ReadArray<byte>(result, dbIndex, array.Length));
break;
case TypeCode.Int16:
f.SetValue(obj, ReadArray<short>(result, dbIndex, array.Length));
break;
case TypeCode.UInt16:
f.SetValue(obj, ReadArray<ushort>(result, dbIndex, array.Length));
break;
case TypeCode.Int32:
f.SetValue(obj, ReadArray<int>(result, dbIndex, array.Length));
break;
case TypeCode.UInt32:
f.SetValue(obj, ReadArray<uint>(result, dbIndex, array.Length));
break;
case TypeCode.Single:
f.SetValue(obj, ReadArray<float>(result, dbIndex, array.Length));
break;
case TypeCode.String:
f.SetValue(obj, ReadArray<string>(result, dbIndex, array.Length));
break;
case TypeCode.Object:
if (arrayElementType == typeof(Vector3))
f.SetValue(obj, new Vector3(ReadArray<float>(result, dbIndex, 3)));
break;
default:
Log.outError(LogFilter.ServerLoading, "Wrong Array Type: {0}", arrayElementType.Name);
break;
}
dbIndex += array.Length;
}
else
{
switch (Type.GetTypeCode(helper.FieldType))
if (type.IsEnum)
type = type.GetEnumUnderlyingType();
switch (Type.GetTypeCode(type))
{
case TypeCode.SByte:
helper.SetValue(obj, result.Read<sbyte>(index++));
f.SetValue(obj, result.Read<sbyte>(dbIndex++));
break;
case TypeCode.Byte:
helper.SetValue(obj, result.Read<byte>(index++));
f.SetValue(obj, result.Read<byte>(dbIndex++));
break;
case TypeCode.Int16:
helper.SetValue(obj, result.Read<short>(index++));
f.SetValue(obj, result.Read<short>(dbIndex++));
break;
case TypeCode.UInt16:
helper.SetValue(obj, result.Read<ushort>(index++));
f.SetValue(obj, result.Read<ushort>(dbIndex++));
break;
case TypeCode.Int32:
helper.SetValue(obj, result.Read<int>(index++));
f.SetValue(obj, result.Read<int>(dbIndex++));
break;
case TypeCode.UInt32:
helper.SetValue(obj, result.Read<uint>(index++));
f.SetValue(obj, result.Read<uint>(dbIndex++));
break;
case TypeCode.Single:
helper.SetValue(obj, result.Read<float>(index++));
f.SetValue(obj, result.Read<float>(dbIndex++));
break;
case TypeCode.String:
string str = result.Read<string>(index++);
helper.SetValue(obj, str);
string str = result.Read<string>(dbIndex++);
f.SetValue(obj, str);
break;
case TypeCode.Object:
switch (helper.FieldType.Name)
if (type == typeof(LocalizedString))
{
case "Vector2":
var vector2 = new Vector2();
vector2.X = result.Read<float>(index++);
vector2.Y = result.Read<float>(index++);
helper.SetValue(obj, vector2);
break;
case "Vector3":
var vector3 = new Vector3();
vector3.X = result.Read<float>(index++);
vector3.Y = result.Read<float>(index++);
vector3.Z = result.Read<float>(index++);
helper.SetValue(obj, vector3);
break;
case "LocalizedString":
LocalizedString locString = new LocalizedString();
locString[Global.WorldMgr.GetDefaultDbcLocale()] = result.Read<string>(index++);
helper.SetValue(obj, locString);
break;
default:
Log.outError(LogFilter.ServerLoading, "Wrong Array Type: {0}", helper.FieldType.Name);
break;
LocalizedString locString = new LocalizedString();
locString[Global.WorldMgr.GetDefaultDbcLocale()] = result.Read<string>(dbIndex++);
f.SetValue(obj, locString);
}
else if (type == typeof(Vector2))
{
f.SetValue(obj, new Vector2(ReadArray<float>(result, dbIndex, 2)));
dbIndex += 2;
}
else if (type == typeof(Vector3))
{
f.SetValue(obj, new Vector3(ReadArray<float>(result, dbIndex, 3)));
dbIndex += 3;
}
else if (type == typeof(FlagArray128))
{
f.SetValue(obj, new FlagArray128(ReadArray<uint>(result, dbIndex, 4)));
dbIndex += 4;
}
break;
default:
Log.outError(LogFilter.ServerLoading, "Wrong Array Type: {0}", helper.FieldType.Name);
Log.outError(LogFilter.ServerLoading, "Wrong Type: {0}", type.Name);
break;
}
}
}
base[idValue] = obj;
base[id] = obj;
}
while (result.NextRow());
}
@@ -202,19 +187,27 @@ namespace Game.DataStorage
if (obj == null)
continue;
for (var i = 0; i < helpers.Length; i++)
foreach (var f in typeof(T).GetFields())
{
var fieldInfo = helpers[i];
if (fieldInfo.FieldType != typeof(LocalizedString))
if (f.FieldType != typeof(LocalizedString))
continue;
LocalizedString locString = (LocalizedString)fieldInfo.Getter(obj);
LocalizedString locString = (LocalizedString)f.GetValue(obj);
locString[locale] = localeResult.Read<string>(index++);
}
} while (localeResult.NextRow());
}
}
TValue[] ReadArray<TValue>(SQLResult result, int dbIndex, int arrayLength)
{
TValue[] values = new TValue[arrayLength];
for (int i = 0; i < arrayLength; ++i)
values[i] = result.Read<TValue>(dbIndex + i);
return values;
}
public bool HasRecord(uint id)
{
return ContainsKey(id);
File diff suppressed because it is too large Load Diff