Struct Deserialization Variable Array Length Inside Struct

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • PauloD
    New Member
    • Aug 2010
    • 1

    Struct Deserialization Variable Array Length Inside Struct

    Code:
    [StructLayou(LayoutKind = Sequential,Pack=1,Size = 18)]
    public struct Character_Items
    {
    public short Armor;
    public short Helm;
    public short WeaponL;
    public short WeaponR;
    public short Pants;
    public short Boots;
    public short Gloves;
    public short Levels;
    public byte Glow;
    public byte Guardian;
    }
    
    [StructLayou(LayoutKind = Sequential,Pack=1,Size = 34)]
    public struct Character_Block
    {
    public byte Position;
    [MarshalAs(UnmanagedType.ByValArray,SizeConst = 10)]
    public short Level;
    public byte Class;
    public byte CtrlCode;
    public Character_Items Items;
    public byte GuildStatus;
    }
    
    So here I have a problem when deserealize
    [StructLayou(LayoutKind = Sequential,Pack=1)]
    public struct Character_List
    {
    public byte Head;
    public byte Length;
    public byte Code;
    public byte OpCode;
    public byte SubCode;
    public byte Separator;
    public byte Count;
    //Here the problem i dont know how to set the MarshalAs
    [MarshalAs(UnmanagedType.ByValArray)]
    public Character_Block[] Characters
    
    public explicit operator Character_List(byte[] Value)
    {
    return Deserialize<Character_List>(Value);
    }
    
    }
    
    //I use it for Deserialize
    public static T Deserialize<T>(byte[] Data)
    {
    IntPtr TmpData = Marshal.AllocHGlobal(Data.Length);
    Marshal.Copy(Data, 0, TmpData, Data.Length);
    T TmpStruct = Marshal.PtrToStructure(TmpData, typeof(T));
    Marshal.FreeHGlobal(TmpData);
    return TmpStruct;
    }
    I receive the data from server via socket and on receive if case of Character List opCode I do
    Code:
    OnCharListRecv((Character_List)Data));
    Data is splitted and it have only the packet length
    because length of packet is set in Data[1]

    But the number of characters is variable
    its set on Count inside struct or same as Data[6]

    Ill need set the max number of characters thats 5?
    Or there is a way to set a variable array size?

    Thanks, and sorry for bad english/code ;D hehe
Working...