Files
BlenderSharp/BlendFile/Reader.cs

94 lines
3.7 KiB
C#

using System.Reflection;
using System.Text;
using Kaitai;
namespace BlendFile;
public class Reader {
readonly string _path;
private readonly Dictionary<int, Type> dnaTypes = new();
private List<object> objects = new();
public List<object> Objects => objects;
public Reader(string path) {
_path = path;
var types = Assembly.GetExecutingAssembly().DefinedTypes;
foreach (var type in types) {
var attrib = type.GetCustomAttribute<DNAClassAttribute>();
if (attrib ==null) continue;
dnaTypes.Add(attrib.OriginalIndex, type);
}
}
public void Read() {
var file = new KaitaiStream(_path);
var blend = new Kaitai.BlendFile(file);
foreach (var block in blend.Blocks)
{
//We need to read all blocks of data regardeless of the type
//if (block.Code != "DATA") continue;
//Checks if the block has a known SDNA type, meaning that we know how it is structured
if(!dnaTypes.ContainsKey((int)block.SdnaIndex)) continue;
Type t = dnaTypes.Values.First(
x => x.GetCustomAttribute<DNAClassAttribute>()!.OriginalName == block.SdnaStruct.Type);
// How many objects are in the block
var count = block.Count;
//offset for the next object in the block
var blockOffset = 0;
//for each expected object in the block
for(var i=0; i<count; i++) {
//create an instance of type "t" (the dna type we inferred before)
var obj = Activator.CreateInstance(t);
if(obj == null) continue; //should never happen
objects.Add(obj); //add the object to the list of objects
//fill the object with the data from the block
FillObject(block, ref obj, t.GetFields(), blockOffset);
//move the offset to the next object in the block
blockOffset += t.GetCustomAttribute<DNAClassAttribute>()!.Size;
}
}
}
private void FillObject(Kaitai.BlendFile.FileBlock block, ref object obj, FieldInfo[] fieldMetadata, int startOffset = 0) {
foreach (var field in fieldMetadata) {
var attrib = field.GetCustomAttribute<DNAFieldAttribute>();
if (attrib == null) continue;
int offset = fieldMetadata.Where(f => f.GetCustomAttribute<DNAFieldAttribute>()!.OriginalIndex < attrib.OriginalIndex)
.Sum(f => f.GetCustomAttribute<DNAFieldAttribute>()!.Size) + startOffset;
int size = attrib.Size;
var data = new byte[size];
Array.Copy((byte[])block.Body, offset, data, 0, size);
object? value = ConvertFieldData(data, attrib.OriginalType);
if(value == null) continue;
field.SetValue(obj, value);
}
}
private object? ConvertFieldData(byte[] data, string type)
{
return type switch
{
"char" => (char)data[0],
"short" => BitConverter.ToInt16(data, 0),
"int" => BitConverter.ToInt32(data, 0),
"float" => BitConverter.ToSingle(data, 0),
"double" => BitConverter.ToDouble(data, 0),
"string" => Encoding.UTF8.GetString(data), // utf8?
"void" => null, // object?
"ushort" => BitConverter.ToUInt16(data, 0),
"uchar" => data[0],
"int64_t" => BitConverter.ToInt64(data, 0),
"int8_t" => (sbyte)data[0],
"uint64_t" => BitConverter.ToUInt64(data, 0),
_ => null
};
}}