Removed redundant statements

This commit is contained in:
Samuele Lorefice
2025-03-12 00:55:23 +01:00
parent 5b8158ea1a
commit c0bc1f0a0f
4 changed files with 19 additions and 24 deletions

View File

@@ -30,7 +30,7 @@ public class Reader {
public List<object> GetObjects() => objects.Values.ToList(); public List<object> GetObjects() => objects.Values.ToList();
public List<T> GetObjects<T>() => objects.Values.OfType<T>().ToList(); public List<T> GetObjects<T>() => objects.Values.OfType<T>().ToList();
private SortedDictionary<long, Kaitai.BlendFile.FileBlock> memBlocks = new(); private SortedDictionary<long, FileBlock> memBlocks = new();
/// <summary> /// <summary>
/// Gets the block at the specified memory address /// Gets the block at the specified memory address
@@ -73,7 +73,7 @@ public class Reader {
Console.WriteLine($"Start offset: 0x{blend.Blocks[0].MemAddr.ToPointer():X}"); Console.WriteLine($"Start offset: 0x{blend.Blocks[0].MemAddr.ToPointer():X}");
bool isLe = blend.Hdr.Endian == Kaitai.BlendFile.Endian.Le; bool isLe = blend.Hdr.Endian == Endian.Le;
//TODO: two blocks somehow have the same mem address... this sounds wrong. //TODO: two blocks somehow have the same mem address... this sounds wrong.
blend.Blocks.ForEach(block => memBlocks.TryAdd(block.MemAddr.ToMemAddr(isLe), block)); blend.Blocks.ForEach(block => memBlocks.TryAdd(block.MemAddr.ToMemAddr(isLe), block));
@@ -242,11 +242,10 @@ public class Reader {
array[i / itemLenght] = ConvertFieldData(itemData, attrib.OriginalType); array[i / itemLenght] = ConvertFieldData(itemData, attrib.OriginalType);
} }
return array; 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;
} }
//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 //Check if the field is a pointer to another DNA structure
if (dnaTypesDb.ContainsKey(attrib.OriginalType)) { if (dnaTypesDb.ContainsKey(attrib.OriginalType)) {

View File

@@ -1,6 +1,8 @@
using System; using System;
using System.Linq; using System.Linq;
using BlendFile; using BlendFile;
using BlendFile.DNA;
using Object = BlendFile.DNA.Object;
var reader = new Reader("cube.blend"); var reader = new Reader("cube.blend");
reader.Read(); reader.Read();
@@ -9,7 +11,7 @@ var counts = reader.Objects.GroupBy(x => x.Key.Item2).ToList();
foreach (var count in counts) Console.WriteLine($"{count.Key}: {count.Count()}"); foreach (var count in counts) Console.WriteLine($"{count.Key}: {count.Count()}");
var Meshes = reader.GetObjects<BlendFile.DNA.Mesh>(); var Meshes = reader.GetObjects<Mesh>();
var Objects = reader.GetObjects<BlendFile.DNA.Object>(); var Objects = reader.GetObjects<Object>();
Console.WriteLine($"Meshes: {Meshes.Count}"); Console.WriteLine($"Meshes: {Meshes.Count}");
Console.WriteLine($"Objects: {Objects.Count}"); Console.WriteLine($"Objects: {Objects.Count}");

View File

@@ -2,7 +2,6 @@ using System;
using System.CodeDom; using System.CodeDom;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using Microsoft.CodeAnalysis.CSharp.Syntax;
namespace CodeGenerator { namespace CodeGenerator {
// ReSharper disable always BitwiseOperatorOnEnumWithoutFlags // ReSharper disable always BitwiseOperatorOnEnumWithoutFlags
@@ -93,7 +92,7 @@ namespace CodeGenerator {
/// </summary> /// </summary>
/// <param name="usageArgs">List of arguments for <see cref="AttributeUsageAttribute"/></param> /// <param name="usageArgs">List of arguments for <see cref="AttributeUsageAttribute"/></param>
public AttributeBuilder SetAttributeUsage(CodeAttributeArgument usageArgs) { public AttributeBuilder SetAttributeUsage(CodeAttributeArgument usageArgs) {
var attrUsage = new CodeAttributeDeclaration() { var attrUsage = new CodeAttributeDeclaration {
Name = nameof(AttributeUsageAttribute), Name = nameof(AttributeUsageAttribute),
Arguments = { usageArgs } Arguments = { usageArgs }
}; };

View File

@@ -9,8 +9,8 @@ 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;
using static Kaitai.BlendFile;
// ReSharper disable BitwiseOperatorOnEnumWithoutFlags // ReSharper disable BitwiseOperatorOnEnumWithoutFlags
namespace CodeGenerator { namespace CodeGenerator {
@@ -123,14 +123,9 @@ namespace CodeGenerator {
CodeMemberField cmf; CodeMemberField cmf;
string name = field.Name; string name = field.Name;
if (name.Contains("()")) return 0; if (name.Contains("()")) return 0;
/*if (name.Contains("[")) {
Log($"Generating array field {field.Name}"); Log($"Generating field {field.Name}");
cmf = CreateArrayMemberField(field); cmf = CreateMemberField(field);
}*/
else {
Log($"Generating field {field.Name}");
cmf = CreateMemberField(field);
}
var attributes = GenerateDnaFieldAttribute(index, field, field.M_Parent.M_Parent, totalSize, out int size); var attributes = GenerateDnaFieldAttribute(index, field, field.M_Parent.M_Parent, totalSize, out int size);
cmf.CustomAttributes.Add(attributes); cmf.CustomAttributes.Add(attributes);
@@ -305,7 +300,7 @@ namespace CodeGenerator {
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))) {
Log($"Found list field {field.Name}"); Log($"Found list field {field.Name}");
Log($"Searching for list length field"); Log("Searching for list length field");
var listLengthField = dnaFields.FirstOrDefault(f => f.Name.Contains(field.Name.ParseFName()) && var listLengthField = dnaFields.FirstOrDefault(f => f.Name.Contains(field.Name.ParseFName()) &&
ListLenghtStr.Any(s2 => f.Name.Contains(s2))); ListLenghtStr.Any(s2 => f.Name.Contains(s2)));
@@ -418,7 +413,7 @@ namespace CodeGenerator {
} }
CodeAttributeDeclaration cad = new("DNAFieldAttribute"); CodeAttributeDeclaration cad = new("DNAFieldAttribute");
cad.Arguments.AddRange(new CodeAttributeArgumentCollection() { cad.Arguments.AddRange(new CodeAttributeArgumentCollection {
new(new CodePrimitiveExpression(size)), new(new CodePrimitiveExpression(size)),
new(new CodePrimitiveExpression(field.Type)), new(new CodePrimitiveExpression(field.Type)),
new(new CodePrimitiveExpression(index)), new(new CodePrimitiveExpression(index)),
@@ -455,7 +450,7 @@ namespace CodeGenerator {
} }
CodeAttributeDeclaration cad = new("DNAArrayAttribute"); CodeAttributeDeclaration cad = new("DNAArrayAttribute");
cad.Arguments.AddRange(new CodeAttributeArgumentCollection() { cad.Arguments.AddRange(new CodeAttributeArgumentCollection {
new(new CodePrimitiveExpression(size)), new(new CodePrimitiveExpression(size)),
new(new CodePrimitiveExpression(field.Type)), new(new CodePrimitiveExpression(field.Type)),
new(new CodePrimitiveExpression(index)), new(new CodePrimitiveExpression(index)),
@@ -471,7 +466,7 @@ namespace CodeGenerator {
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 {
new(new CodeSnippetExpression("8")), //pointer size new(new CodeSnippetExpression("8")), //pointer size
new(new CodePrimitiveExpression(listField.Type)), new(new CodePrimitiveExpression(listField.Type)),
new(new CodePrimitiveExpression(listField.Name)), new(new CodePrimitiveExpression(listField.Name)),