Fixed list length value returning always zero.

Added separated pointer and count memory offsets to list attributes.
Tried handling them
This commit is contained in:
Samuele Lorefice
2025-03-06 19:11:03 +01:00
parent 0bc7f73aee
commit fb50e3fa44
12 changed files with 75 additions and 48 deletions

View File

@@ -186,26 +186,40 @@ namespace CodeGenerator {
//Add the lists to the class
for (var index = 0; index < type.Fields.Count; index++) {
Dictionary<string, int> listCountOffsets = new();
var field = type.Fields[index];
//Skip fields that are not part of a list
if (!listFields.Select(f => f.Item1).Contains(field)) {
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);
//retrieve the offset of the list length field if exists
listCountOffsets.TryGetValue(listLength.Name, out int listLenghtOffset);
//Retrieve the index of the list length field
int sizeIndex = type.Fields.IndexOf(listLength);
Log($"Generating list field {listPointer.Name}");
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)) {
//Retrieve the list length field
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);
if (x != null) //Update the existing list attribute
x.CustomAttributes[0].Arguments[5] = new(new CodePrimitiveExpression(totalSize));
else //Store the data for when the list attribute is made
listCountOffsets.Add(fName, totalSize);
} else {
Log($"Field {field.Name} is not part of a list, skipping");
continue;
}
//Retrieve the list pointer and the list length fields
var (listPointer, listLength) = listFields.FirstOrDefault(x => x.Item1 == field);
//Retrieve the index of the list length field
int sizeIndex = type.Fields.IndexOf(listLength);
Log($"Generating list field {listPointer.Name}");
var cmf = CreateListMemberField(listPointer, listLength);
var attribute = GenerateDnaListAttribute(index, listPointer, listPointer.M_Parent.M_Parent, sizeIndex,
listLength, totalSize, out int size);
totalSize += size;
cmf.CustomAttributes.Add(attribute);
ctd.Members.Add(cmf);
}
ctd.CustomAttributes[0].Arguments.Add(new (new CodePrimitiveExpression(totalSize)));
@@ -340,7 +354,8 @@ namespace CodeGenerator {
.AddAutoProperty(typeof(string), "UnderlyingType")
.AddAutoProperty(typeof(string), "CountFieldName")
.AddAutoProperty(typeof(int), "CountFieldIndex")
.AddAutoProperty(typeof(int), "MemoryOffset")
.AddAutoProperty(typeof(int), "PtrMemoryOffset")
.AddAutoProperty(typeof(int), "CountMemoryOffset")
.AddPropertiesConstructor()
.AddBaseConstructorParams(["OriginalIndex", "OriginalName"])
.Build()
@@ -397,8 +412,8 @@ namespace CodeGenerator {
}
private static CodeAttributeDeclaration GenerateDnaListAttribute(int listIndex, BlendFile.DnaField listField,
BlendFile.Dna1Body body, int lenghtIndex, BlendFile.DnaField lenghtField, int offset, out int size) {
size = 0;
int lenghtIndex, BlendFile.DnaField lenghtField, int ptrOffset, int countOffset, out int size) {
size = 8;
var cad = new CodeAttributeDeclaration("DNAListAttribute");
cad.Arguments.AddRange(new CodeAttributeArgumentCollection() {
new(new CodeSnippetExpression("8")),//pointer size
@@ -408,7 +423,8 @@ namespace CodeGenerator {
new(new CodePrimitiveExpression(listField.Type)),//TODO: double check this
new(new CodePrimitiveExpression(lenghtField.Name)),
new(new CodePrimitiveExpression(lenghtIndex)),
new(new CodePrimitiveExpression(offset))
new(new CodePrimitiveExpression(ptrOffset)),
new(new CodePrimitiveExpression(countOffset))
});
return cad;
}