fixed double loop problem and refactored field additions

This commit is contained in:
mm00
2025-03-07 18:55:17 +01:00
parent bfa185c8e4
commit d3e54246c4

View File

@@ -114,6 +114,41 @@ namespace CodeGenerator {
$"DNA1: {_blendfile.SdnaStructs.Count} structures\n"); $"DNA1: {_blendfile.SdnaStructs.Count} structures\n");
} }
public static int AddNormalField(BlendFile.DnaField field, ref CodeTypeDeclaration ctd, int index, int totalSize)
{
CodeMemberField cmf;
string name = field.Name;
if (name.Contains("()")) return 0;
if (name.Contains("[")) {
Log($"Generating array field {field.Name}");
cmf = CreateArrayMemberField(field);
}
else {
Log($"Generating field {field.Name}");
cmf = CreateMemberField(field);
}
var attributes = GenerateDnaFieldAttribute(index, field, field.M_Parent.M_Parent, totalSize, out int size);
cmf.CustomAttributes.Add(attributes);
ctd.Members.Add(cmf);
return size;
}
public static int AddListField(ref CodeTypeDeclaration ctd, int totalSize, int index,
int listLenghtOffset, BlendFile.DnaField listPointer, BlendFile.DnaField listLength, int sizeIndex)
{
var cmf = CreateListMemberField(listPointer, listLength);
var attribute = GenerateDnaListAttribute(index, listPointer, sizeIndex, listLength, totalSize,
listLenghtOffset, out int size);
cmf.CustomAttributes.Add(attribute);
ctd.Members.Add(cmf);
return size;
}
private static CodeNamespace GenerateTypes(out CodeNamespace additionalNs) { private static CodeNamespace GenerateTypes(out CodeNamespace additionalNs) {
//Initialize the namespaces //Initialize the namespaces
CodeNamespace rootNs = new CodeNamespace(Namespace); CodeNamespace rootNs = new CodeNamespace(Namespace);
@@ -157,58 +192,29 @@ namespace CodeGenerator {
var totalSize = 0; var totalSize = 0;
Dictionary<string, int> listCountOffsets = new();
//Add the fields to the class //Add the fields to the class
Log($"Fields: {type.Fields.Count}"); Log($"Fields: {type.Fields.Count}");
for (var index = 0; index < type.Fields.Count; index++) { for (var index = 0; index < type.Fields.Count; index++) {
var field = type.Fields[index]; var field = type.Fields[index];
if (!normalFields.Contains(field)) { if (normalFields.Contains(field) && !listFields.Select(f => f.Item2).Contains(field))
Log($"Field {field.Name} is part of a list, skipping"); {
continue; totalSize += AddNormalField(field, ref ctd, index, totalSize);
} } else if (listFields.Select(f => f.Item1).Contains(field)) {
CodeMemberField cmf;
string name = field.Name;
if (name.Contains("()")) continue;
if (name.Contains("[")) {
Log($"Generating array field {field.Name}");
cmf = CreateArrayMemberField(field);
}
else {
Log($"Generating field {field.Name}");
cmf = CreateMemberField(field);
}
var attributes = GenerateDnaFieldAttribute(index, field, field.M_Parent.M_Parent, totalSize, out int size);
totalSize += size;
cmf.CustomAttributes.Add(attributes);
ctd.Members.Add(cmf);
}
Dictionary<string, int> listCountOffsets = new();
//Add the lists to the class
for (var index = 0; index < type.Fields.Count; index++) {
var field = type.Fields[index];
if (listFields.Select(f => f.Item1).Contains(field)) { //Field is pointer of list
//Retrieve the list pointer and the list length fields //Retrieve the list pointer and the list length fields
var (listPointer, listLength) = listFields.FirstOrDefault(x => x.Item1 == field); var (listPointer, listLength) = listFields.FirstOrDefault(x => x.Item1 == field);
Log($"Generating list field {listPointer.Name}"); Log($"Generating list field {listPointer.Name}");
//retrieve the offset of the list length field if exists //retrieve the offset of the list length field if exists
listCountOffsets.TryGetValue(listLength.Name, out int listLenghtOffset); listCountOffsets.TryGetValue(listLength.Name.ParseFName(), out int listLenghtOffset);
//Retrieve the index of the list length field //Retrieve the index of the list length field
int sizeIndex = type.Fields.IndexOf(listLength); int sizeIndex = type.Fields.IndexOf(listLength);
var cmf = CreateListMemberField(listPointer, listLength); totalSize += AddListField(ref ctd, totalSize, index, listLenghtOffset, listPointer, listLength, sizeIndex);
} else if (listFields.Select(f => f.Item2).Contains(field)) {
var attribute = GenerateDnaListAttribute(index, listPointer, sizeIndex, listLength, totalSize, //update the size of the list attribute
listLenghtOffset, out int size);
totalSize += size;
cmf.CustomAttributes.Add(attribute);
ctd.Members.Add(cmf);
} else if (listFields.Select(f => f.Item2).Contains(field)) { // field is list length
//Retrieve the list length field
string fName = field.Name.ParseFName(); string fName = field.Name.ParseFName();
//Try seeing if the list attribute is already present //Try seeing if the list attribute is already present
var x = ctd.Members.OfType<CodeMemberField>().FirstOrDefault(member => member.Name == fName); var x = ctd.Members.OfType<CodeMemberField>().FirstOrDefault(member => member.Name == fName);
@@ -219,8 +225,8 @@ namespace CodeGenerator {
listCountOffsets.Add(fName, totalSize); listCountOffsets.Add(fName, totalSize);
totalSize += field.Type.ParseFSize(); totalSize += field.Type.ParseFSize();
} else { //Skip fields that are not part of a list } else {
Log($"Field {field.Name} is not part of a list, skipping"); Log($"Field {field.Name} is of unknown or unsupported type");
} }
} }