dschu012 / D2SLib

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Read binary with struct

tytannial opened this issue · comments

`public static class BinaryReaderExt
{
public static T ReadStruct(this BinaryReader reader)
{
byte[] bytes = reader.ReadBytes(Marshal.SizeOf(typeof(T)));

    GCHandle handle = GCHandle.Alloc(bytes, GCHandleType.Pinned);
    T theStructure = (T)Marshal.PtrToStructure(handle.AddrOfPinnedObject(), typeof(T));
    handle.Free();

    return theStructure;
}

}`

It will fill the struct with binary.

Once you get to the skills section of the binary the data is no longer byte aligned. To my knowledge that'd only work for fields that are byte aligned.

Once you get to the skills section of the binary the data is no longer byte aligned. To my knowledge that'd only work for fields that are byte aligned.

You're right. dotnet/csharplang#465
But I will take time for bit base layouts and find a solution. 😀

Also the size in bits for each stat is variable. For example if you look at https://github.com/dschu012/D2SLib/blob/c74bb90b861251b3674c04c454f4fd0ff715ff60/src/Resources/ItemStatCost.txt the Csv bits column controls how many bits the stat is. So strength is 10 bits while hp and mana are 21. Then the stats on item are similar (variable bit size) but looking at different columns in that txt.