public static TRaw NormalToRawObject <TRaw, TSource>(TSource source, Dictionary <string, SectionPointer> stringSectionTable, SortedDictionary <long, SectionPointer> stringRelocTable = null, long baseOffset = 0) where TRaw : struct where TSource : struct { object rawObject = new TRaw(); foreach (FieldInfo rawField in typeof(TRaw).GetFields()) { FieldInfo field = typeof(TSource).GetField(rawField.Name); if (field == null) { throw new Exception($"Didn't find field `{rawField.Name}` in type {nameof(TSource)}"); } if (rawField.FieldType == typeof(Pointer) && (field.FieldType == typeof(string) || StringEnumAttribute.IsStringEnum(field.FieldType))) { string str = StringEnumAttribute.IsStringEnum(field.FieldType) ? StringEnumAttribute.GetIdentifier(field.GetValue(source), field.FieldType) : (string)field.GetValue(source); SectionPointer stringPointer = str != null ? stringSectionTable[str] : SectionPointer.NULL; if (stringRelocTable != null) { stringRelocTable.Add(rawField.GetFieldOffset() + baseOffset, stringPointer); } else { rawField.SetValue(rawObject, stringPointer); } } else if (rawField.FieldType == typeof(Pointer) && field.FieldType == typeof(Pointer)) { Pointer pointer = (Pointer)field.GetValue(source); if (stringRelocTable != null) { stringRelocTable.Add(rawField.GetFieldOffset() + baseOffset, pointer != Pointer.NULL ? new SectionPointer(pointer, 0x700000101) : SectionPointer.NULL); } else { rawField.SetValue(rawObject, pointer.AsLong); } } else if (field.FieldType.BaseType == typeof(Enum)) { rawField.SetValue(rawObject, field.GetValue(source)); } else if (field.FieldType == typeof(bool)) { rawField.SetValue(rawObject, (bool)field.GetValue(source) ? 1 : 0); } else if (field.FieldType == rawField.FieldType) { rawField.SetValue(rawObject, field.GetValue(source)); } else { throw new Exception($"Types `{field.FieldType}` and `{rawField.FieldType}` didn't match on field `{field.Name}`"); } } return((TRaw)rawObject); }
public bool Equals(SectionPointer other) { return(this == other); }