fixed double loop problem and refactored field additions
This commit is contained in:
@@ -114,6 +114,41 @@ namespace CodeGenerator {
|
||||
$"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) {
|
||||
//Initialize the namespaces
|
||||
CodeNamespace rootNs = new CodeNamespace(Namespace);
|
||||
@@ -157,58 +192,29 @@ namespace CodeGenerator {
|
||||
|
||||
var totalSize = 0;
|
||||
|
||||
Dictionary<string, int> listCountOffsets = new();
|
||||
|
||||
//Add the fields to the class
|
||||
Log($"Fields: {type.Fields.Count}");
|
||||
for (var index = 0; index < type.Fields.Count; index++) {
|
||||
var field = type.Fields[index];
|
||||
if (!normalFields.Contains(field)) {
|
||||
Log($"Field {field.Name} is part of a list, skipping");
|
||||
continue;
|
||||
}
|
||||
|
||||
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
|
||||
if (normalFields.Contains(field) && !listFields.Select(f => f.Item2).Contains(field))
|
||||
{
|
||||
totalSize += AddNormalField(field, ref ctd, index, totalSize);
|
||||
} else if (listFields.Select(f => f.Item1).Contains(field)) {
|
||||
//Retrieve the list pointer and the list length fields
|
||||
var (listPointer, listLength) = listFields.FirstOrDefault(x => x.Item1 == field);
|
||||
Log($"Generating list field {listPointer.Name}");
|
||||
|
||||
//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
|
||||
int sizeIndex = type.Fields.IndexOf(listLength);
|
||||
|
||||
var cmf = CreateListMemberField(listPointer, listLength);
|
||||
|
||||
var attribute = GenerateDnaListAttribute(index, listPointer, sizeIndex, listLength, totalSize,
|
||||
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
|
||||
totalSize += AddListField(ref ctd, totalSize, index, listLenghtOffset, listPointer, listLength, sizeIndex);
|
||||
} else if (listFields.Select(f => f.Item2).Contains(field)) {
|
||||
//update the size of the list attribute
|
||||
string fName = field.Name.ParseFName();
|
||||
//Try seeing if the list attribute is already present
|
||||
var x = ctd.Members.OfType<CodeMemberField>().FirstOrDefault(member => member.Name == fName);
|
||||
@@ -219,8 +225,8 @@ namespace CodeGenerator {
|
||||
listCountOffsets.Add(fName, totalSize);
|
||||
|
||||
totalSize += field.Type.ParseFSize();
|
||||
} else { //Skip fields that are not part of a list
|
||||
Log($"Field {field.Name} is not part of a list, skipping");
|
||||
} else {
|
||||
Log($"Field {field.Name} is of unknown or unsupported type");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user