Compare commits
3 Commits
9cbed0a8b8
...
0bf0aeab09
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
0bf0aeab09 | ||
|
|
8678ee6c85 | ||
|
|
fbc0f9be49 |
@@ -222,9 +222,23 @@ public class Reader {
|
||||
var data = new byte[size];
|
||||
Array.Copy((byte[])block.Body, offset, data, 0, size);
|
||||
|
||||
|
||||
|
||||
//Check if it's an array
|
||||
if (Type.GetType(attrib.UnderlyingType) is { IsArray: true }) {
|
||||
int itemLenght = attrib.OriginalType.ParseFSize();
|
||||
var array = new object?[attrib.Size / itemLenght];
|
||||
for (int i = 0; i < attrib.Size; i += itemLenght) {
|
||||
var itemData = new byte[itemLenght];
|
||||
Array.Copy(data, i, itemData, 0, itemLenght);
|
||||
array[i / itemLenght] = ConvertFieldData(itemData, attrib.OriginalType);
|
||||
}
|
||||
return array;
|
||||
} else {
|
||||
//Convert the data to the correct type if it's a base type
|
||||
object? value = ConvertFieldData(data, attrib.OriginalType);
|
||||
if (value != null) return value;
|
||||
}
|
||||
|
||||
//Check if the field is a pointer to another DNA structure
|
||||
if (dnaTypesDb.ContainsKey(attrib.OriginalType)) {
|
||||
|
||||
@@ -120,10 +120,10 @@ namespace CodeGenerator {
|
||||
CodeMemberField cmf;
|
||||
string name = field.Name;
|
||||
if (name.Contains("()")) return 0;
|
||||
if (name.Contains("[")) {
|
||||
/*if (name.Contains("[")) {
|
||||
Log($"Generating array field {field.Name}");
|
||||
cmf = CreateArrayMemberField(field);
|
||||
}
|
||||
}*/
|
||||
else {
|
||||
Log($"Generating field {field.Name}");
|
||||
cmf = CreateMemberField(field);
|
||||
@@ -135,9 +135,26 @@ namespace CodeGenerator {
|
||||
return size;
|
||||
}
|
||||
|
||||
public static int AddListField(ref CodeTypeDeclaration ctd, int totalSize, int index,
|
||||
int listLenghtOffset, BlendFile.DnaField listPointer, BlendFile.DnaField listLength, int sizeIndex)
|
||||
private static int AddArrayField(ref CodeTypeDeclaration ctd, DnaField field, int index, int totalSize)
|
||||
{
|
||||
Log($"Generating Array field {field.Name}");
|
||||
var cmf = CreateArrayMemberField(field);
|
||||
var attribute = new CodeAttributeDeclaration("DNAArrayAttribute");
|
||||
attribute.Arguments.AddRange(new CodeAttributeArgumentCollection() {
|
||||
new(new CodePrimitiveExpression(totalSize)),
|
||||
new(new CodePrimitiveExpression(field.Type)),
|
||||
new(new CodePrimitiveExpression(index)),
|
||||
new(new CodePrimitiveExpression(field.Name)),
|
||||
new(new CodePrimitiveExpression(field.Type)),
|
||||
new(new CodePrimitiveExpression(0))
|
||||
});
|
||||
cmf.CustomAttributes.Add(attribute);
|
||||
ctd.Members.Add(cmf);
|
||||
return 0;
|
||||
}
|
||||
|
||||
private static int AddListField(ref CodeTypeDeclaration ctd, int totalSize, int index, int listLenghtOffset,
|
||||
DnaField listPointer, DnaField listLength, int sizeIndex) {
|
||||
var cmf = CreateListMemberField(listPointer, listLength);
|
||||
|
||||
var attribute = GenerateDnaListAttribute(index, listPointer, sizeIndex, listLength, totalSize,
|
||||
@@ -199,8 +216,12 @@ namespace CodeGenerator {
|
||||
Log($"Fields: {type.Fields.Count}");
|
||||
for (var index = 0; index < type.Fields.Count; index++) {
|
||||
var field = type.Fields[index];
|
||||
if (normalFields.Contains(field) && !listFields.Select(f => f.Item2).Contains(field))
|
||||
{
|
||||
//Check if the field is a normal field or a list field
|
||||
if (normalFields.Contains(field) && !listFields.Select(f => f.Item2).Contains(field)) {
|
||||
//check if the field is an array
|
||||
if (field.Name.Contains("["))
|
||||
totalSize += AddArrayField(ref ctd, field, index, totalSize);
|
||||
else
|
||||
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
|
||||
@@ -345,6 +366,19 @@ namespace CodeGenerator {
|
||||
.AddPropertiesConstructor()
|
||||
.AddBaseConstructorParams(["OriginalIndex", "OriginalName"])
|
||||
.Build(),
|
||||
attributeBuilder.New().SetName("DNAArrayAttribute")
|
||||
.SetAttributeUsage(new (new CodeSnippetExpression("AttributeTargets.Field")))
|
||||
.DeriveFromClass($"{Namespace}.DNAAttribute")
|
||||
.AddAutoProperty(typeof(int), "Size")
|
||||
.AddAutoProperty(typeof(string), "OriginalType")
|
||||
.AddAutoProperty(typeof(int), "OriginalIndex")
|
||||
.AddAutoProperty(typeof(string), "OriginalName")
|
||||
.AddAutoProperty(typeof(string), "UnderlyingType")
|
||||
.AddAutoProperty(typeof(int), "ArrayLenght")
|
||||
.AddAutoProperty(typeof(int), "MemoryOffset")
|
||||
.AddPropertiesConstructor()
|
||||
.AddBaseConstructorParams(["OriginalIndex", "OriginalName"])
|
||||
.Build(),
|
||||
attributeBuilder.New().SetName("DNAClassAttribute")
|
||||
.SetAttributeUsage(new (new CodeSnippetExpression("AttributeTargets.Class | AttributeTargets.Struct")))
|
||||
.DeriveFromClass($"{Namespace}.DNAAttribute")
|
||||
|
||||
@@ -30,7 +30,7 @@ namespace CodeGenerator {
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static int ParseFSize(this string str) {
|
||||
return str switch {
|
||||
"char" => sizeof(char),
|
||||
"char" => sizeof(sbyte),
|
||||
"short" => sizeof(short),
|
||||
"int" => sizeof(int),
|
||||
"float" => sizeof(float),
|
||||
|
||||
Reference in New Issue
Block a user