From 9cbed0a8b83bc575bbfecee4dd59d62af98be0d8 Mon Sep 17 00:00:00 2001 From: Samuele Lorefice Date: Tue, 11 Mar 2025 18:41:27 +0100 Subject: [PATCH] Imported static Kaitai.BlendFile, compressing bunch of lines --- BlendFile/Reader.cs | 3 +- BlenderSharp.sln.DotSettings.user | 1 + CodeGenerator/Program.cs | 48 ++++++++++++++++--------------- 3 files changed, 28 insertions(+), 24 deletions(-) diff --git a/BlendFile/Reader.cs b/BlendFile/Reader.cs index 0426b27..1192580 100644 --- a/BlendFile/Reader.cs +++ b/BlendFile/Reader.cs @@ -227,7 +227,7 @@ public class Reader { if (value != null) return value; //Check if the field is a pointer to another DNA structure - if (dnaTypesDb.ContainsKey(attrib.OriginalType)) { + if (dnaTypesDb.ContainsKey(attrib.OriginalType)) { if (!attrib.IsPointer) { //It's a structure //Create a new instance of the DNA structure type @@ -249,6 +249,7 @@ public class Reader { pointers.TryAdd(block.MemAddr.ToPointer() + offset, data.ToPointer()); } } + throw new NotSupportedException($"Unknown type \"{attrib.OriginalType}\""); } diff --git a/BlenderSharp.sln.DotSettings.user b/BlenderSharp.sln.DotSettings.user index 066ce2b..7f84612 100644 --- a/BlenderSharp.sln.DotSettings.user +++ b/BlenderSharp.sln.DotSettings.user @@ -13,6 +13,7 @@ ForceIncluded ForceIncluded ForceIncluded + ForceIncluded ForceIncluded ForceIncluded ForceIncluded diff --git a/CodeGenerator/Program.cs b/CodeGenerator/Program.cs index c55edc7..4b9c264 100644 --- a/CodeGenerator/Program.cs +++ b/CodeGenerator/Program.cs @@ -9,6 +9,7 @@ using System.Linq; using System.Text; using System.Threading; using Kaitai; +using static Kaitai.BlendFile; using Microsoft.CSharp; // ReSharper disable BitwiseOperatorOnEnumWithoutFlags @@ -107,14 +108,14 @@ namespace CodeGenerator { private static void ReadBlendFile() { Log("Reading empty.blend file"); - _blendfile = BlendFile.FromFile("empty.blend"); + _blendfile = FromFile("empty.blend"); Log($"Header: Blender v{_blendfile.Hdr.Version} {_blendfile.Hdr.Endian}\n" + $"DataBlocks: {_blendfile.Blocks.Count}\n" + $"DNA1: {_blendfile.SdnaStructs.Count} structures\n"); } - - public static int AddNormalField(BlendFile.DnaField field, ref CodeTypeDeclaration ctd, int index, int totalSize) + + private static int AddNormalField(DnaField field, ref CodeTypeDeclaration ctd, int index, int totalSize) { CodeMemberField cmf; string name = field.Name; @@ -184,8 +185,8 @@ namespace CodeGenerator { ns.Types.Add(ctd); //TODO: when encountering a list, run trough the fields to find a count/lenght or similar data. - List normalFields; //Fields that are not lists nor lenghts of lists - List<(BlendFile.DnaField, BlendFile.DnaField)> listFields; //Fields that are lists, and their corresponding length fields + List normalFields; //Fields that are not lists nor lengths of lists + List<(DnaField, DnaField)> listFields; //Fields that are lists, and their corresponding length fields //filter the fields we want to include in the class minus the lists FilterFields(type.Fields, out normalFields, out listFields); @@ -250,9 +251,9 @@ namespace CodeGenerator { /// /// Determines if the type has to be serialized as a class or a struct /// - /// istance to analyze + /// istance to analyze /// if there is any pointer or self reference, otherwise - private static bool IsClass(BlendFile.DnaStruct type) { + private static bool IsClass(DnaStruct type) { foreach (var field in type.Fields) { if (field.Name.Contains("*")) { Log($"Pointer detected. {field.Type} {field.Name}"); @@ -271,17 +272,16 @@ namespace CodeGenerator { /// /// Filters the fields into normal fields and list fields pairs /// - /// of s from all parameters - /// of containing all fields not part of a List - /// of (, ) collection where Item1 is the ListPointer and Item2 is the list lenght - private static void FilterFields(IEnumerable fields, - out List normalFields, - out List<(BlendFile.DnaField, BlendFile.DnaField)> listFields) { + /// of s from all parameters + /// of containing all fields not part of a List + /// of (, ) collection where Item1 is the ListPointer and Item2 is the list lenght + private static void FilterFields(IEnumerable fields, + out List normalFields, out List<(DnaField, DnaField)> listFields) { normalFields = new(); //Fields that are not lists nor lengths of lists listFields = new(); //Fields that are lists, and their corresponding length fields //Cast to array the fields to avoid multiple enumerations - var dnaFields = fields as BlendFile.DnaField[] ?? fields.ToArray(); + var dnaFields = fields as DnaField[] ?? fields.ToArray(); foreach (var field in dnaFields) { if (ListMarkerStr.Any(s => field.Name.Contains(s)) && !ListLenghtStr.Any(s2 => field.Name.Contains(s2))) { @@ -375,8 +375,9 @@ namespace CodeGenerator { } //TODO: use AttributeBuilder inside here - private static CodeAttributeDeclaration GenerateDnaFieldAttribute(int index, BlendFile.DnaField field, - BlendFile.Dna1Body body, int offset, out int size) { + private static CodeAttributeDeclaration GenerateDnaFieldAttribute(int index, DnaField field, Dna1Body body, int offset, + out int size) { + string t; size = body.Lengths[field.IdxType]; @@ -422,8 +423,9 @@ namespace CodeGenerator { return cad; } - private static CodeAttributeDeclaration GenerateDnaListAttribute(int listIndex, BlendFile.DnaField listField, - int lenghtIndex, BlendFile.DnaField lenghtField, int ptrOffset, int countOffset, out int size) { + private static CodeAttributeDeclaration GenerateDnaListAttribute(int listIndex, DnaField listField, int lenghtIndex, + DnaField lenghtField, int ptrOffset, int countOffset, out int size) { + size = 8; var cad = new CodeAttributeDeclaration("DNAListAttribute"); cad.Arguments.AddRange(new CodeAttributeArgumentCollection() { @@ -441,7 +443,7 @@ namespace CodeGenerator { return cad; } - private static CodeMemberField CreateMemberField(BlendFile.DnaField field) { + private static CodeMemberField CreateMemberField(DnaField field) { Type t = Type.GetType(field.Type.ParseFType()); CodeMemberField cmf; //Check if the type is a built-in type or a custom type @@ -454,7 +456,7 @@ namespace CodeGenerator { return cmf; } - private static CodeMemberField CreateArrayMemberField(BlendFile.DnaField field) { + private static CodeMemberField CreateArrayMemberField(DnaField field) { Type t = Type.GetType(field.Type.ParseFType()); CodeMemberField cmf; @@ -483,7 +485,7 @@ namespace CodeGenerator { return cmf; } - private static CodeMemberField CreateListMemberField(BlendFile.DnaField field, BlendFile.DnaField lenght) { + private static CodeMemberField CreateListMemberField(DnaField field, DnaField lenght) { Type t = Type.GetType(field.Type.ParseFType()); CodeMemberField cmf; CodeTypeReference ctr = new(typeof(List<>)); @@ -545,7 +547,7 @@ namespace CodeGenerator { return ctc; } - private static CodeConstructor GenerateConstructor(BlendFile.DnaStruct type, CodeTypeDeclaration ctd) { + private static CodeConstructor GenerateConstructor(DnaStruct type, CodeTypeDeclaration ctd) { //Create a normal constructor CodeConstructor cc = new CodeConstructor { Name = type.Type, @@ -575,7 +577,7 @@ namespace CodeGenerator { return cc; } - private static CodeConstructor GenerateParameterlessConstructor(BlendFile.DnaStruct type, CodeTypeDeclaration ctd) { + private static CodeConstructor GenerateParameterlessConstructor(DnaStruct type, CodeTypeDeclaration ctd) { //Create a normal constructor CodeConstructor cc = new CodeConstructor { Name = type.Type,