/// <summary> /// Resolve external references in the record /// </summary> public virtual void ResolveReferences() { foreach (var propInfo in this.GetType().GetProperties()) { if (propInfo.GetCustomAttributes(false).Any(n => n is ExternalReference)) { var foreignKey = this.GetType().GetProperties().Where(x => x.Name == propInfo.Name.Replace("Ref", "Key")).FirstOrDefault(); Int64 fkValue; if (foreignKey.GetValue(this, null) is Int64) { fkValue = (Int64)(foreignKey.GetValue(this, null)); } else { fkValue = (Int32)(foreignKey.GetValue(this, null)); } BaseDat newVal = ReferenceManager.Instance.AllDats[propInfo.PropertyType.Name + ".dat"].Where(x => x.Key == fkValue).FirstOrDefault(); propInfo.SetValue(this, newVal, null); } if (propInfo.GetCustomAttributes(false).Any(n => n is ExternalReferenceList)) { var foreignKey = this.GetType().GetProperties().Where(x => x.Name == propInfo.Name.Replace("ListRef", "ListData")).FirstOrDefault(); object untypedFkValues = foreignKey.GetValue(this, null); if (untypedFkValues is UInt64List) { UInt64List fkValues = (untypedFkValues as UInt64List); List <BaseDat> newVals = new List <BaseDat>(); Type genericDatType = propInfo.PropertyType.GetGenericArguments()[0]; var newExternalDatList = propInfo.PropertyType.GetConstructor(new Type[0]).Invoke(new Object[0]); foreach (UInt64 fkValue in fkValues.Data) { BaseDat newVal = ReferenceManager.Instance.AllDats[genericDatType.Name + ".dat"].Where(x => x.Key == fkValue).FirstOrDefault(); newVals.Add(newVal); } newExternalDatList.GetType().GetProperty("Data").SetValue(newExternalDatList, newVals, null); propInfo.SetValue(this, newExternalDatList, null); } else if (untypedFkValues is UInt32List) { UInt32List fkValues = (untypedFkValues as UInt32List); List <BaseDat> newVals = new List <BaseDat>(); Type genericDatType = propInfo.PropertyType.GetGenericArguments()[0]; var newExternalDatList = propInfo.PropertyType.GetConstructor(new Type[0]).Invoke(new Object[0]); foreach (UInt32 fkValue in fkValues.Data) { BaseDat newVal = ReferenceManager.Instance.AllDats[genericDatType.Name + ".dat"].Where(x => x.Key == fkValue).FirstOrDefault(); newVals.Add(newVal); } newExternalDatList.GetType().GetProperty("Data").SetValue(newExternalDatList, newVals, null); propInfo.SetValue(this, newExternalDatList, null); } else { } } } }
/// <summary> /// Finds all known references to strings and other data in the data section and adds them to the DataEntries. /// Accomplished by reading the [StringIndex] and [DataIndex] attributes of our dat structure. /// </summary> /// <param name="entry">Dat parser created from parsing a single entry of the .dat file.</param> /// <param name="inStream">Stream containing contents of .dat file. Stream position not preserved.</param> private void AddDataToTable(BaseDat entry, BinaryReader inStream) { var properties = entry.GetType().GetProperties(); foreach (var prop in properties) { object[] customAttributes = prop.GetCustomAttributes(false); if (customAttributes.Length == 0) { continue; } int offset = (int)prop.GetValue(entry, null); if (DataEntries.ContainsKey(offset) && !DataEntries[offset].ToString().Equals("")) { continue; } if (customAttributes.Any(n => n is StringIndex)) { DataEntries[offset] = new UnicodeString(inStream, offset, DataTableBegin, (customAttributes.Any(n => n is UserStringIndex))); // Console.WriteLine("{0} -> {1}", offset, DataEntries[offset]); } else if (customAttributes.Any(n => n is DataIndex)) { DataEntries[offset] = new UnkownData(inStream, offset, DataTableBegin); } else if (customAttributes.Any(n => n is UInt64Index)) { var propLength = entry.GetType().GetProperties().Where(x => x.Name == prop.Name + "Length").FirstOrDefault(); DataEntries[offset] = new UInt64List(inStream, offset, DataTableBegin, (int)(propLength.GetValue(entry, null))); } else if (customAttributes.Any(n => n is UInt32Index)) { var propLength = entry.GetType().GetProperties().Where(x => x.Name == prop.Name + "Length").FirstOrDefault(); DataEntries[offset] = new UInt32List(inStream, offset, DataTableBegin, (int)(propLength.GetValue(entry, null))); } else if (customAttributes.Any(n => n is Int32Index)) { var propLength = entry.GetType().GetProperties().Where(x => x.Name == prop.Name + "Length").FirstOrDefault(); DataEntries[offset] = new Int32List(inStream, offset, DataTableBegin, (int)(propLength.GetValue(entry, null))); } } }
/// <summary> /// Reads the resources section of the file (after 0xBBbbBBbbBBbbBBbb) /// </summary> /// <returns></returns> public virtual void ReadResources(BinaryReader inStream, long dataTableBegin) { foreach (var propInfo in this.GetType().GetProperties()) { if (propInfo.GetCustomAttributes(false).Any(n => n is ResourceOnly)) { if (propInfo.PropertyType == typeof(UnicodeString)) { var propOffset = this.GetType().GetProperties().Where(x => x.Name == propInfo.Name.Replace("StringData", "StringOffset")).FirstOrDefault(); UnicodeString newVal = new UnicodeString(inStream, (int)(propOffset.GetValue(this, null)), dataTableBegin, false); propInfo.SetValue(this, newVal, null); } else if (propInfo.PropertyType == typeof(UInt64List)) { var propOffset = this.GetType().GetProperties().Where(x => x.Name == propInfo.Name.Replace("ListData", "ListOffset")).FirstOrDefault(); var propLength = this.GetType().GetProperties().Where(x => x.Name == propInfo.Name.Replace("ListData", "ListLength")).FirstOrDefault(); UInt64List newVal = new UInt64List(inStream, (int)(propOffset.GetValue(this, null)), dataTableBegin, (int)(propLength.GetValue(this, null))); propInfo.SetValue(this, newVal, null); } else if (propInfo.PropertyType == typeof(UInt32List)) { var propOffset = this.GetType().GetProperties().Where(x => x.Name == propInfo.Name.Replace("ListData", "ListOffset")).FirstOrDefault(); var propLength = this.GetType().GetProperties().Where(x => x.Name == propInfo.Name.Replace("ListData", "ListLength")).FirstOrDefault(); UInt32List newVal = new UInt32List(inStream, (int)(propOffset.GetValue(this, null)), dataTableBegin, (int)(propLength.GetValue(this, null))); propInfo.SetValue(this, newVal, null); } else if (propInfo.PropertyType == typeof(Int32List)) { var propOffset = this.GetType().GetProperties().Where(x => x.Name == propInfo.Name.Replace("ListData", "ListOffset")).FirstOrDefault(); var propLength = this.GetType().GetProperties().Where(x => x.Name == propInfo.Name.Replace("ListData", "ListLength")).FirstOrDefault(); Int32List newVal = new Int32List(inStream, (int)(propOffset.GetValue(this, null)), dataTableBegin, (int)(propLength.GetValue(this, null))); propInfo.SetValue(this, newVal, null); } else if (propInfo.PropertyType == typeof(IndirectStringList)) { var propOffset = this.GetType().GetProperties().Where(x => x.Name == propInfo.Name.Replace("ListData", "ListOffset")).FirstOrDefault(); var propLength = this.GetType().GetProperties().Where(x => x.Name == propInfo.Name.Replace("ListData", "ListLength")).FirstOrDefault(); IndirectStringList newVal = new IndirectStringList(inStream, (int)(propOffset.GetValue(this, null)), dataTableBegin, (int)(propLength.GetValue(this, null))); propInfo.SetValue(this, newVal, null); } } } }
/// <summary> /// Finds all known references to strings and other data in the data section and adds them to the DataEntries. /// Accomplished by reading the [StringIndex] and [DataIndex] attributes of our dat structure. /// </summary> /// <param name="entry">Dat parser created from parsing a single entry of the .dat file.</param> /// <param name="inStream">Stream containing contents of .dat file. Stream position not preserved.</param> private void AddDataToTable(BaseDat entry, BinaryReader inStream) { var properties = entry.GetType().GetProperties(); foreach (var prop in properties) { object[] customAttributes = prop.GetCustomAttributes(false); if (customAttributes.Length == 0) continue; int offset = (int)prop.GetValue(entry, null); if (DataEntries.ContainsKey(offset) && !DataEntries[offset].ToString().Equals("")) { continue; } if (customAttributes.Any(n => n is StringIndex)) { DataEntries[offset] = new UnicodeString(inStream, offset, DataTableBegin, (customAttributes.Any(n => n is UserStringIndex))); // Console.WriteLine("{0} -> {1}", offset, DataEntries[offset]); } else if (customAttributes.Any(n => n is DataIndex)) { DataEntries[offset] = new UnkownData(inStream, offset, DataTableBegin); } else if (customAttributes.Any(n => n is UInt64Index)) { var propLength = entry.GetType().GetProperties().Where(x => x.Name == prop.Name+"Length").FirstOrDefault(); DataEntries[offset] = new UInt64List(inStream, offset, DataTableBegin, (int)(propLength.GetValue(entry, null))); } else if (customAttributes.Any(n => n is UInt32Index)) { var propLength = entry.GetType().GetProperties().Where(x => x.Name == prop.Name + "Length").FirstOrDefault(); DataEntries[offset] = new UInt32List(inStream, offset, DataTableBegin, (int)(propLength.GetValue(entry, null))); } else if (customAttributes.Any(n => n is Int32Index)) { var propLength = entry.GetType().GetProperties().Where(x => x.Name == prop.Name + "Length").FirstOrDefault(); DataEntries[offset] = new Int32List(inStream, offset, DataTableBegin, (int)(propLength.GetValue(entry, null))); } } }