static string GetString(DB6BinaryReader reader, int field) { if (_stringTable != null) { return(_stringTable.LookupByKey(reader.GetUInt32(_header.GetFieldBytes(field)))); } return(reader.ReadCString()); }
internal static DB6Storage <T> Read <T>(string fileName, DB6Meta meta, HotfixStatements preparedStatement, HotfixStatements preparedStatementLocale = 0) where T : new() { DB6Storage <T> storage = new DB6Storage <T>(); if (!File.Exists(CliDB.DataPath + fileName)) { Log.outError(LogFilter.ServerLoading, "File {0} not found.", fileName); return(storage); } //First lets load field Info var fields = typeof(T).GetFields(); DBClientHelper[] fieldsInfo = new DBClientHelper[fields.Length]; for (var i = 0; i < fields.Length; ++i) { fieldsInfo[i] = new DBClientHelper(fields[i]); } using (var fileReader = new BinaryReader(new MemoryStream(File.ReadAllBytes(CliDB.DataPath + fileName)))) { _header = ReadHeader(fileReader); var data = LoadData(fileReader); int commonDataFieldIndex = 0; foreach (var pair in data) { var dataReader = new DB6BinaryReader(pair.Value); var obj = new T(); int fieldIndex = 0; for (var x = 0; x < _header.FieldCount; ++x) { int arrayLength = meta.ArraySizes[x]; if (arrayLength > 1) { for (var z = 0; z < arrayLength; ++z) { var fieldInfo = fieldsInfo[fieldIndex++]; if (fieldInfo.IsArray) { //Field is Array Array array = (Array)fieldInfo.Getter(obj); for (var y = 0; y < array.Length; ++y) { SetArrayValue(array, y, fieldInfo, dataReader, x); } arrayLength -= array.Length; } else { //Only Data is Array if (Type.GetTypeCode(fieldInfo.FieldType) == TypeCode.Object) { switch (fieldInfo.FieldType.Name) { case "Vector2": fieldInfo.SetValue(obj, new Vector2(dataReader.ReadSingle(), dataReader.ReadSingle())); arrayLength -= 2; break; case "Vector3": fieldInfo.SetValue(obj, new Vector3(dataReader.ReadSingle(), dataReader.ReadSingle(), dataReader.ReadSingle())); arrayLength -= 3; break; case "LocalizedString": LocalizedString locString = new LocalizedString(); locString[Global.WorldMgr.GetDefaultDbcLocale()] = GetString(dataReader, x); fieldInfo.SetValue(obj, locString); arrayLength -= 1; break; case "FlagArray128": FlagArray128 flagArray128 = new FlagArray128(dataReader.GetUInt32(_header.GetFieldBytes(x)), dataReader.GetUInt32(_header.GetFieldBytes(x)), dataReader.GetUInt32(_header.GetFieldBytes(x)), dataReader.GetUInt32(_header.GetFieldBytes(x))); fieldInfo.SetValue(obj, flagArray128); arrayLength -= 4; break; default: Log.outError(LogFilter.ServerLoading, "Unknown Array Type {0} in DBClient File", fieldInfo.FieldType.Name, nameof(T)); break; } } else { SetValue(obj, fieldInfo, dataReader, x); } } } } else { var fieldInfo = fieldsInfo[fieldIndex++]; if (fieldInfo.IsArray) { Array array = (Array)fieldInfo.Getter(obj); for (var y = 0; y < array.Length; ++y) { SetArrayValue(array, y, fieldInfo, dataReader, x + y); } x += array.Length - 1; } else { SetValue(obj, fieldInfo, dataReader, x); } } } commonDataFieldIndex = fieldIndex; storage.Add((uint)pair.Key, obj); } //Get DB field Index uint index = 0; for (uint i = 0; i < _header.FieldCount && i < _header.IndexField; ++i) { index += meta.ArraySizes[i]; } ReadCommonData(commonDataFieldIndex, storage, meta, fieldsInfo); storage.LoadData(index, fieldsInfo, preparedStatement, preparedStatementLocale); } Global.DB2Mgr.AddDB2(_header.TableHash, storage); CliDB.LoadedFileCount++; return(storage); }
static void SetArrayValue(Array array, int arrayIndex, DBClientHelper helper, DB6BinaryReader reader, int field) { switch (Type.GetTypeCode(helper.RealType)) { case TypeCode.SByte: helper.SetValue(array, reader.ReadSByte(), arrayIndex); break; case TypeCode.Byte: helper.SetValue(array, reader.ReadByte(), arrayIndex); break; case TypeCode.Int16: helper.SetValue(array, reader.ReadInt16(), arrayIndex); break; case TypeCode.UInt16: helper.SetValue(array, reader.ReadUInt16(), arrayIndex); break; case TypeCode.Int32: helper.SetValue(array, reader.GetInt32(_header.GetFieldBytes(field)), arrayIndex); break; case TypeCode.UInt32: helper.SetValue(array, reader.GetUInt32(_header.GetFieldBytes(field)), arrayIndex); break; case TypeCode.Single: helper.SetValue(array, reader.ReadSingle(), arrayIndex); break; case TypeCode.String: helper.SetValue(array, GetString(reader, field), arrayIndex); break; default: Log.outError(LogFilter.ServerLoading, "Wrong Array Type: {0}", helper.FieldType.Name); break; } }
static void SetValue(object obj, DBClientHelper helper, DB6BinaryReader reader, int field) { switch (Type.GetTypeCode(helper.RealType)) { case TypeCode.SByte: helper.SetValue(obj, reader.ReadSByte()); break; case TypeCode.Byte: helper.SetValue(obj, reader.ReadByte()); break; case TypeCode.Int16: helper.SetValue(obj, reader.ReadInt16()); break; case TypeCode.UInt16: helper.SetValue(obj, reader.ReadUInt16()); break; case TypeCode.Int32: helper.SetValue(obj, reader.GetInt32(_header.GetFieldBytes(field))); break; case TypeCode.UInt32: helper.SetValue(obj, reader.GetUInt32(_header.GetFieldBytes(field))); break; case TypeCode.Single: helper.SetValue(obj, reader.ReadSingle()); break; case TypeCode.String: string str = GetString(reader, field); helper.SetValue(obj, str); break; case TypeCode.Object: switch (helper.FieldType.Name) { case "Vector2": helper.SetValue(obj, new Vector2(reader.ReadSingle(), reader.ReadSingle())); break; case "Vector3": helper.SetValue(obj, new Vector3(reader.ReadSingle(), reader.ReadSingle(), reader.ReadSingle())); break; case "LocalizedString": LocalizedString locString = new LocalizedString(); locString[Global.WorldMgr.GetDefaultDbcLocale()] = GetString(reader, field); helper.SetValue(obj, locString); 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; } }
static void ReadCommonData <T>(int fieldIndex, DB6Storage <T> storage, DB6Meta meta, DBClientHelper[] helper) where T : new() { for (int x = (int)_header.FieldCount; x < _header.TotalFieldCount; ++x) { var fieldInfo = helper[fieldIndex++]; int arrayLength = meta.ArraySizes[x]; foreach (var recordId in _commandData[x]) { var dataReader = new DB6BinaryReader(recordId.Value); var record = storage.LookupByKey(recordId.Key); if (arrayLength > 1) { for (var z = 0; z < arrayLength; ++z) { if (fieldInfo.IsArray) { //Field is Array Array array = (Array)fieldInfo.Getter(record); for (var y = 0; y < array.Length; ++y) { SetArrayValue(array, y, fieldInfo, dataReader, x); } arrayLength -= array.Length; } else { //Only Data is Array if (Type.GetTypeCode(fieldInfo.FieldType) == TypeCode.Object) { Log.outError(LogFilter.Server, "We should not have custom classes in common data"); switch (fieldInfo.FieldType.Name) { case "Vector2": fieldInfo.SetValue(record, new Vector2(dataReader.ReadSingle(), dataReader.ReadSingle())); arrayLength -= 2; break; case "Vector3": fieldInfo.SetValue(record, new Vector3(dataReader.ReadSingle(), dataReader.ReadSingle(), dataReader.ReadSingle())); arrayLength -= 3; break; case "LocalizedString": LocalizedString locString = new LocalizedString(); locString[Global.WorldMgr.GetDefaultDbcLocale()] = GetString(dataReader, x); fieldInfo.SetValue(record, locString); arrayLength -= 1; break; case "FlagArray128": FlagArray128 flagArray128 = new FlagArray128(dataReader.ReadUInt32(), dataReader.ReadUInt32(), dataReader.ReadUInt32(), dataReader.ReadUInt32()); fieldInfo.SetValue(record, flagArray128); arrayLength -= 4; break; default: Log.outError(LogFilter.ServerLoading, "Unknown Array Type {0} in DBClient File", fieldInfo.FieldType.Name, nameof(T)); break; } } else { SetValue(record, fieldInfo, dataReader, x); } } } } else { if (fieldInfo.IsArray) { Array array = (Array)fieldInfo.Getter(record); for (var y = 0; y < array.Length; ++y) { SetArrayValue(array, y, fieldInfo, dataReader, x + y); } x += array.Length - 1; } else { SetValue(record, fieldInfo, dataReader, x); } } } } }