Imported static Kaitai.BlendFile, compressing bunch of lines

This commit is contained in:
Samuele Lorefice
2025-03-11 18:41:27 +01:00
parent 37ebc71cf3
commit 9cbed0a8b8
3 changed files with 28 additions and 24 deletions

View File

@@ -227,7 +227,7 @@ public class Reader {
if (value != null) return value; if (value != null) return value;
//Check if the field is a pointer to another DNA structure //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 if (!attrib.IsPointer) { //It's a structure
//Create a new instance of the DNA structure type //Create a new instance of the DNA structure type
@@ -249,6 +249,7 @@ public class Reader {
pointers.TryAdd(block.MemAddr.ToPointer() + offset, data.ToPointer()); pointers.TryAdd(block.MemAddr.ToPointer() + offset, data.ToPointer());
} }
} }
throw new NotSupportedException($"Unknown type \"{attrib.OriginalType}\""); throw new NotSupportedException($"Unknown type \"{attrib.OriginalType}\"");
} }

View File

@@ -13,6 +13,7 @@
<s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=7020124F_002D9FFC_002D4AC3_002D8F3D_002DAAB8E0240759_002Ff_003ADefaultInterpolatedStringHandler_002Ecs_002Fl_003AC_0021_003FUsers_003Fairon_003FAppData_003FRoaming_003FJetBrains_003FRider2024_002E3_003Fresharper_002Dhost_003FSourcesCache_003Fc1c946eaa6d8ddaaea1b63e936ca8ed2791d9316c5b025a41d445891e8a59ecd_003FDefaultInterpolatedStringHandler_002Ecs/@EntryIndexedValue">ForceIncluded</s:String> <s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=7020124F_002D9FFC_002D4AC3_002D8F3D_002DAAB8E0240759_002Ff_003ADefaultInterpolatedStringHandler_002Ecs_002Fl_003AC_0021_003FUsers_003Fairon_003FAppData_003FRoaming_003FJetBrains_003FRider2024_002E3_003Fresharper_002Dhost_003FSourcesCache_003Fc1c946eaa6d8ddaaea1b63e936ca8ed2791d9316c5b025a41d445891e8a59ecd_003FDefaultInterpolatedStringHandler_002Ecs/@EntryIndexedValue">ForceIncluded</s:String>
<s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=7020124F_002D9FFC_002D4AC3_002D8F3D_002DAAB8E0240759_002Ff_003AIntPtr_002Ecs_002Fl_003A_002E_002E_003F_002E_002E_003F_002Econfig_003FJetBrains_003FRider2024_002E3_003Fresharper_002Dhost_003FSourcesCache_003F82ab53226b38d0ff1abfabfc5e951361c82d37b012cc2966b6358b4d6b5e7_003FIntPtr_002Ecs/@EntryIndexedValue">ForceIncluded</s:String> <s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=7020124F_002D9FFC_002D4AC3_002D8F3D_002DAAB8E0240759_002Ff_003AIntPtr_002Ecs_002Fl_003A_002E_002E_003F_002E_002E_003F_002Econfig_003FJetBrains_003FRider2024_002E3_003Fresharper_002Dhost_003FSourcesCache_003F82ab53226b38d0ff1abfabfc5e951361c82d37b012cc2966b6358b4d6b5e7_003FIntPtr_002Ecs/@EntryIndexedValue">ForceIncluded</s:String>
<s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=7020124F_002D9FFC_002D4AC3_002D8F3D_002DAAB8E0240759_002Ff_003AList_002Ecs_002Fl_003AC_0021_003FUsers_003Fairon_003FAppData_003FRoaming_003FJetBrains_003FRider2024_002E3_003Fresharper_002Dhost_003FSourcesCache_003Fb7208b3f72528d22781d25fde9a55271bdf2b5aade4f03b1324579a25493cd8_003FList_002Ecs/@EntryIndexedValue">ForceIncluded</s:String> <s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=7020124F_002D9FFC_002D4AC3_002D8F3D_002DAAB8E0240759_002Ff_003AList_002Ecs_002Fl_003AC_0021_003FUsers_003Fairon_003FAppData_003FRoaming_003FJetBrains_003FRider2024_002E3_003Fresharper_002Dhost_003FSourcesCache_003Fb7208b3f72528d22781d25fde9a55271bdf2b5aade4f03b1324579a25493cd8_003FList_002Ecs/@EntryIndexedValue">ForceIncluded</s:String>
<s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=7020124F_002D9FFC_002D4AC3_002D8F3D_002DAAB8E0240759_002Ff_003ARuntimeType_002Ecs_002Fl_003AC_0021_003FUsers_003Fairon_003FAppData_003FRoaming_003FJetBrains_003FRider2024_002E3_003Fresharper_002Dhost_003FSourcesCache_003F3791ade316feeb2be344648da3c7d31719ff492f7733b643bf8c24d7629883_003FRuntimeType_002Ecs/@EntryIndexedValue">ForceIncluded</s:String>
<s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=7020124F_002D9FFC_002D4AC3_002D8F3D_002DAAB8E0240759_002Ff_003ARuntimeType_002Ecs_002Fl_003A_002E_002E_003F_002E_002E_003F_002Econfig_003FJetBrains_003FRider2024_002E3_003Fresharper_002Dhost_003FDecompilerCache_003Fdecompiler_003F0beb96d31db641cf82014cb1a758a330b2dc00_003F5b_003F039af867_003FRuntimeType_002Ecs/@EntryIndexedValue">ForceIncluded</s:String> <s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=7020124F_002D9FFC_002D4AC3_002D8F3D_002DAAB8E0240759_002Ff_003ARuntimeType_002Ecs_002Fl_003A_002E_002E_003F_002E_002E_003F_002Econfig_003FJetBrains_003FRider2024_002E3_003Fresharper_002Dhost_003FDecompilerCache_003Fdecompiler_003F0beb96d31db641cf82014cb1a758a330b2dc00_003F5b_003F039af867_003FRuntimeType_002Ecs/@EntryIndexedValue">ForceIncluded</s:String>
<s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=7020124F_002D9FFC_002D4AC3_002D8F3D_002DAAB8E0240759_002Ff_003ARuntimeType_002Ecs_002Fl_003A_002E_002E_003F_002E_002E_003F_002Econfig_003FJetBrains_003FRider2024_002E3_003Fresharper_002Dhost_003FSourcesCache_003F7139fe40e31cec9ddaf3bf550e2d74b6564e2b3838816bf103f9f8063aa1fe8_003FRuntimeType_002Ecs/@EntryIndexedValue">ForceIncluded</s:String> <s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=7020124F_002D9FFC_002D4AC3_002D8F3D_002DAAB8E0240759_002Ff_003ARuntimeType_002Ecs_002Fl_003A_002E_002E_003F_002E_002E_003F_002Econfig_003FJetBrains_003FRider2024_002E3_003Fresharper_002Dhost_003FSourcesCache_003F7139fe40e31cec9ddaf3bf550e2d74b6564e2b3838816bf103f9f8063aa1fe8_003FRuntimeType_002Ecs/@EntryIndexedValue">ForceIncluded</s:String>
<s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=7020124F_002D9FFC_002D4AC3_002D8F3D_002DAAB8E0240759_002Ff_003ASortedDictionary_002Ecs_002Fl_003AC_0021_003FUsers_003Fairon_003FAppData_003FRoaming_003FJetBrains_003FRider2024_002E3_003Fresharper_002Dhost_003FSourcesCache_003Febdb3cec3b3875204585daa9fc42159a24cae33b2087ff4dc114d0e6a5a3e9_003FSortedDictionary_002Ecs/@EntryIndexedValue">ForceIncluded</s:String> <s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=7020124F_002D9FFC_002D4AC3_002D8F3D_002DAAB8E0240759_002Ff_003ASortedDictionary_002Ecs_002Fl_003AC_0021_003FUsers_003Fairon_003FAppData_003FRoaming_003FJetBrains_003FRider2024_002E3_003Fresharper_002Dhost_003FSourcesCache_003Febdb3cec3b3875204585daa9fc42159a24cae33b2087ff4dc114d0e6a5a3e9_003FSortedDictionary_002Ecs/@EntryIndexedValue">ForceIncluded</s:String>

View File

@@ -9,6 +9,7 @@ using System.Linq;
using System.Text; using System.Text;
using System.Threading; using System.Threading;
using Kaitai; using Kaitai;
using static Kaitai.BlendFile;
using Microsoft.CSharp; using Microsoft.CSharp;
// ReSharper disable BitwiseOperatorOnEnumWithoutFlags // ReSharper disable BitwiseOperatorOnEnumWithoutFlags
@@ -107,14 +108,14 @@ namespace CodeGenerator {
private static void ReadBlendFile() { private static void ReadBlendFile() {
Log("Reading empty.blend file"); 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" + Log($"Header: Blender v{_blendfile.Hdr.Version} {_blendfile.Hdr.Endian}\n" +
$"DataBlocks: {_blendfile.Blocks.Count}\n" + $"DataBlocks: {_blendfile.Blocks.Count}\n" +
$"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) private static int AddNormalField(DnaField field, ref CodeTypeDeclaration ctd, int index, int totalSize)
{ {
CodeMemberField cmf; CodeMemberField cmf;
string name = field.Name; string name = field.Name;
@@ -184,8 +185,8 @@ namespace CodeGenerator {
ns.Types.Add(ctd); ns.Types.Add(ctd);
//TODO: when encountering a list, run trough the fields to find a count/lenght or similar data. //TODO: when encountering a list, run trough the fields to find a count/lenght or similar data.
List<BlendFile.DnaField> normalFields; //Fields that are not lists nor lenghts of lists List<DnaField> normalFields; //Fields that are not lists nor lengths of lists
List<(BlendFile.DnaField, BlendFile.DnaField)> listFields; //Fields that are lists, and their corresponding length fields 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 //filter the fields we want to include in the class minus the lists
FilterFields(type.Fields, out normalFields, out listFields); FilterFields(type.Fields, out normalFields, out listFields);
@@ -250,9 +251,9 @@ namespace CodeGenerator {
/// <summary> /// <summary>
/// Determines if the type has to be serialized as a class or a struct /// Determines if the type has to be serialized as a class or a struct
/// </summary> /// </summary>
/// <param name="type"><see cref="BlendFile.DnaStruct"/> istance to analyze</param> /// <param name="type"><see cref="DnaStruct"/> istance to analyze</param>
/// <returns> <see langword="true"/> if there is any pointer or self reference, otherwise <see langword="false"/></returns> /// <returns> <see langword="true"/> if there is any pointer or self reference, otherwise <see langword="false"/></returns>
private static bool IsClass(BlendFile.DnaStruct type) { private static bool IsClass(DnaStruct type) {
foreach (var field in type.Fields) { foreach (var field in type.Fields) {
if (field.Name.Contains("*")) { if (field.Name.Contains("*")) {
Log($"Pointer detected. {field.Type} {field.Name}"); Log($"Pointer detected. {field.Type} {field.Name}");
@@ -271,17 +272,16 @@ namespace CodeGenerator {
/// <summary> /// <summary>
/// Filters the fields into normal fields and list fields pairs /// Filters the fields into normal fields and list fields pairs
/// </summary> /// </summary>
/// <param name="fields"><see cref="IEnumerable{T}"/> of <see cref="BlendFile.DnaField"/>s from all parameters</param> /// <param name="fields"><see cref="IEnumerable{T}"/> of <see cref="DnaField"/>s from all parameters</param>
/// <param name="normalFields"><see cref="List{T}"/> of <see cref="BlendFile.DnaField"/> containing all fields not part of a List</param> /// <param name="normalFields"><see cref="List{T}"/> of <see cref="DnaField"/> containing all fields not part of a List</param>
/// <param name="listFields"><see cref="List{T}"/> of (<see cref="BlendFile.DnaField"/>, <see cref="BlendFile.DnaField"/>) collection where Item1 is the ListPointer and Item2 is the list lenght</param> /// <param name="listFields"><see cref="List{T}"/> of (<see cref="DnaField"/>, <see cref="DnaField"/>) collection where Item1 is the ListPointer and Item2 is the list lenght</param>
private static void FilterFields(IEnumerable<BlendFile.DnaField> fields, private static void FilterFields(IEnumerable<DnaField> fields,
out List<BlendFile.DnaField> normalFields, out List<DnaField> normalFields, out List<(DnaField, DnaField)> listFields) {
out List<(BlendFile.DnaField, BlendFile.DnaField)> listFields) {
normalFields = new(); //Fields that are not lists nor lengths of lists normalFields = new(); //Fields that are not lists nor lengths of lists
listFields = new(); //Fields that are lists, and their corresponding length fields listFields = new(); //Fields that are lists, and their corresponding length fields
//Cast to array the fields to avoid multiple enumerations //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) { foreach (var field in dnaFields) {
if (ListMarkerStr.Any(s => field.Name.Contains(s)) && if (ListMarkerStr.Any(s => field.Name.Contains(s)) &&
!ListLenghtStr.Any(s2 => field.Name.Contains(s2))) { !ListLenghtStr.Any(s2 => field.Name.Contains(s2))) {
@@ -375,8 +375,9 @@ namespace CodeGenerator {
} }
//TODO: use AttributeBuilder inside here //TODO: use AttributeBuilder inside here
private static CodeAttributeDeclaration GenerateDnaFieldAttribute(int index, BlendFile.DnaField field, private static CodeAttributeDeclaration GenerateDnaFieldAttribute(int index, DnaField field, Dna1Body body, int offset,
BlendFile.Dna1Body body, int offset, out int size) { out int size) {
string t; string t;
size = body.Lengths[field.IdxType]; size = body.Lengths[field.IdxType];
@@ -422,8 +423,9 @@ namespace CodeGenerator {
return cad; return cad;
} }
private static CodeAttributeDeclaration GenerateDnaListAttribute(int listIndex, BlendFile.DnaField listField, private static CodeAttributeDeclaration GenerateDnaListAttribute(int listIndex, DnaField listField, int lenghtIndex,
int lenghtIndex, BlendFile.DnaField lenghtField, int ptrOffset, int countOffset, out int size) { DnaField lenghtField, int ptrOffset, int countOffset, out int size) {
size = 8; size = 8;
var cad = new CodeAttributeDeclaration("DNAListAttribute"); var cad = new CodeAttributeDeclaration("DNAListAttribute");
cad.Arguments.AddRange(new CodeAttributeArgumentCollection() { cad.Arguments.AddRange(new CodeAttributeArgumentCollection() {
@@ -441,7 +443,7 @@ namespace CodeGenerator {
return cad; return cad;
} }
private static CodeMemberField CreateMemberField(BlendFile.DnaField field) { private static CodeMemberField CreateMemberField(DnaField field) {
Type t = Type.GetType(field.Type.ParseFType()); Type t = Type.GetType(field.Type.ParseFType());
CodeMemberField cmf; CodeMemberField cmf;
//Check if the type is a built-in type or a custom type //Check if the type is a built-in type or a custom type
@@ -454,7 +456,7 @@ namespace CodeGenerator {
return cmf; return cmf;
} }
private static CodeMemberField CreateArrayMemberField(BlendFile.DnaField field) { private static CodeMemberField CreateArrayMemberField(DnaField field) {
Type t = Type.GetType(field.Type.ParseFType()); Type t = Type.GetType(field.Type.ParseFType());
CodeMemberField cmf; CodeMemberField cmf;
@@ -483,7 +485,7 @@ namespace CodeGenerator {
return cmf; 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()); Type t = Type.GetType(field.Type.ParseFType());
CodeMemberField cmf; CodeMemberField cmf;
CodeTypeReference ctr = new(typeof(List<>)); CodeTypeReference ctr = new(typeof(List<>));
@@ -545,7 +547,7 @@ namespace CodeGenerator {
return ctc; return ctc;
} }
private static CodeConstructor GenerateConstructor(BlendFile.DnaStruct type, CodeTypeDeclaration ctd) { private static CodeConstructor GenerateConstructor(DnaStruct type, CodeTypeDeclaration ctd) {
//Create a normal constructor //Create a normal constructor
CodeConstructor cc = new CodeConstructor { CodeConstructor cc = new CodeConstructor {
Name = type.Type, Name = type.Type,
@@ -575,7 +577,7 @@ namespace CodeGenerator {
return cc; return cc;
} }
private static CodeConstructor GenerateParameterlessConstructor(BlendFile.DnaStruct type, CodeTypeDeclaration ctd) { private static CodeConstructor GenerateParameterlessConstructor(DnaStruct type, CodeTypeDeclaration ctd) {
//Create a normal constructor //Create a normal constructor
CodeConstructor cc = new CodeConstructor { CodeConstructor cc = new CodeConstructor {
Name = type.Type, Name = type.Type,