Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Restore pre experimental field order when serialising to exml #597

Merged
merged 1 commit into from
Apr 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Tools/auto_extract/extractor.py
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,7 @@ def __init__(self, data: bytes, nms_mem):
self.data = data
self.raw_field_type = 0x00
self.field_size = 0x0
self.field_index = 0x0

# properties which are overwritten by subclasses which need to specify
# them.
Expand Down Expand Up @@ -660,6 +661,7 @@ def extract(nms_mem: pymem.Pymem, address: int, field_count: int) -> tuple[list[
for i in range(field_count):
data = nms_mem.read_bytes(address + i * 0x60, 0x60)
field = Field.instantiate(data, nms_mem)
field.field_index = i
# As we add fields, determine if the field is an array with an
# associated enum. If it is then set a flag.
if (not has_enum_arrays
Expand Down
5 changes: 4 additions & 1 deletion Tools/auto_extract/templates/default/class_template.j2
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
{{ enum_val[1] }}{% if field.requires_values %} = {{enum_val[0] }}{% endif %},
{%- endfor %}
}
[NMS(Index = {{ field.field_index }})]
/* {{ field.field_offset }} */ public {{ field.field_name }}Enum {{ field.field_name }};
{%- elif field._is_array_field %}{# End of enum chunk, start of array chunk #}
{%- if field.local_enum %}
Expand All @@ -26,11 +27,13 @@
{%- endfor %}
}
{%- endif %}
[NMS(Size = {{ field.array_size }}{% if field.array_enum_type is not none %}, EnumType = typeof({{ field.array_enum_type }}){% endif %})]
[NMS(Index = {{ field.field_index }}, Size = {{ field.array_size }}{% if field.array_enum_type is not none %}, EnumType = typeof({{ field.array_enum_type }}){% endif %})]
/* {{ field.field_offset }} */ public {{ field.field_type }}[] {{ field.field_name }};
{%- elif field._is_list_field %}{# End of array chunk, start of list chunk #}
[NMS(Index = {{ field.field_index }})]
/* {{ field.field_offset }} */ public List<{{ field.field_type }}> {{ field.field_name }};
{%- else %}{# End of array chunk, start of normal field chunk #}
[NMS(Index = {{ field.field_index }})]
/* {{ field.field_offset }} */ public {{ field.field_type }} {{ field.field_name }};
{%- endif %}
{%- endfor %}
Expand Down
1 change: 1 addition & 0 deletions libMBIN/Source/Template/NMSAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,6 @@ public class NMSAttribute : Attribute
public ulong NameHash { get; set; }
public bool Broken { get; set; }
public bool IDField { get; set; } = false;
public int Index { get; set; }
}
}
2 changes: 1 addition & 1 deletion libMBIN/Source/Template/NMSTemplate.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1224,7 +1224,7 @@ public EXmlBase SerializeEXml(bool isChildTemplate) {
xmlData = new EXmlData { Template = type.Name };
}

var fields = type.GetFields().OrderBy(field => field.MetadataToken); // hack to get fields in order of declaration (todo: use something less hacky, this might break mono?)
var fields = type.GetFields().OrderBy(field => field.GetCustomAttribute<NMSAttribute>()?.Index ?? 0); // Order fields in declared order

foreach ( var field in fields ) {

Expand Down