private const uint WDC3FmtSig = 0x33434457; // WDC3 public static DB6Storage <T> Read <T>(string fileName, 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 {fileName} not found."); return(storage); } DBReader reader = new DBReader(); using (var stream = new FileStream(CliDB.DataPath + fileName, FileMode.Open)) { if (!reader.Load(stream)) { Log.outError(LogFilter.ServerLoading, $"Error loading {fileName}."); return(storage); } } foreach (var b in reader._records) { storage.Add((uint)b.Key, b.Value.As <T>()); } storage.LoadData(reader.Header.IdIndex, preparedStatement, preparedStatementLocale); Global.DB2Mgr.AddDB2(reader.Header.TableHash, storage); CliDB.LoadedFileCount++; return(storage); }
public static DB6Storage <T> Read <T>(string fileName, HotfixStatements preparedStatement, HotfixStatements preparedStatementLocale = 0) where T : new() { ClearData(); 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(); DB6FieldInfo[] fieldsInfo = new DB6FieldInfo[fields.Length]; for (var i = 0; i < fields.Length; ++i) { fieldsInfo[i] = new DB6FieldInfo(fields[i]); } using (var fileReader = new BinaryReader(new MemoryStream(File.ReadAllBytes(CliDB.DataPath + fileName)))) { ReadHeader(fileReader); var records = ReadData(fileReader); foreach (var pair in records) { using (MemoryStream ms = new MemoryStream(pair.Value)) using (BinaryReader dataReader = new BinaryReader(ms, System.Text.Encoding.UTF8)) { var obj = new T(); int objectFieldIndex = 0; //First check if index is in data if (Header.HasIndexTable()) { fieldsInfo[objectFieldIndex++].SetValue(obj, (uint)pair.Key); } for (var dataFieldIndex = 0; dataFieldIndex < Header.FieldCount; ++dataFieldIndex) { int arrayLength = ColumnMeta[dataFieldIndex].ArraySize; if (arrayLength > 1) { for (var arrayIndex = 0; arrayIndex < arrayLength; ++arrayIndex) { var fieldInfo = fieldsInfo[objectFieldIndex++]; if (fieldInfo.IsArray) { Array array = (Array)fieldInfo.Getter(obj); SetArrayValue(obj, (uint)array.Length, fieldInfo, dataReader); arrayLength -= array.Length; } else { //Only Data is Array if (Type.GetTypeCode(fieldInfo.FieldType) == TypeCode.Object) { switch (fieldInfo.FieldType.Name) { case "Vector2": fieldInfo.SetValue(obj, dataReader.Read <Vector2>()); arrayLength -= 2; break; case "Vector3": fieldInfo.SetValue(obj, dataReader.Read <Vector3>()); arrayLength -= 3; break; case "LocalizedString": LocalizedString locString = new LocalizedString(); locString[Global.WorldMgr.GetDefaultDbcLocale()] = GetString(dataReader); fieldInfo.SetValue(obj, locString); arrayLength -= 1; break; case "FlagArray128": fieldInfo.SetValue(obj, new FlagArray128(dataReader.ReadUInt32(), dataReader.ReadUInt32(), dataReader.ReadUInt32(), dataReader.ReadUInt32())); 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); } } dataReader.BaseStream.Position += GetPadding(fieldInfo.FieldType, dataFieldIndex); } } else { var fieldInfo = fieldsInfo[objectFieldIndex++]; if (fieldInfo.IsArray) { Array array = (Array)fieldInfo.Getter(obj); SetArrayValue(obj, (uint)array.Length, fieldInfo, dataReader); dataFieldIndex += array.Length - 1; } else { SetValue(obj, fieldInfo, dataReader); } dataReader.BaseStream.Position += GetPadding(fieldInfo.FieldType, dataFieldIndex); } } //Check if there is parent ids and fill them if (objectFieldIndex < fieldsInfo.Length && Header.LookupColumnCount > 0) { fieldsInfo[objectFieldIndex].SetValue(obj, dataReader.ReadUInt32()); } storage.Add((uint)pair.Key, obj); } } storage.LoadData(Header.IdIndex, fieldsInfo, preparedStatement, preparedStatementLocale); } Global.DB2Mgr.AddDB2(Header.TableHash, storage); CliDB.LoadedFileCount++; return(storage); }
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 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); } } } } }