Esempio n. 1
0
        static private void writeData(object obj, FieldInfo fInfo, DataOffsetAttribute attr, byte[] data)
        {
            object  o     = fInfo.GetValue(obj);
            IDataIO idata = o as IDataIO;

            if (idata != null)
            {
                idata.Write(data, attr.Offset);
            }
            else
            {
                switch (attr.Type)
                {
                case DataType.SByte:
                    data[attr.Offset] = (byte)(sbyte)o;
                    break;

                case DataType.IByte:
                    data[attr.Offset] = (byte)(int)o;
                    break;

                case DataType.Byte:
                    data[attr.Offset] = (byte)o;
                    break;

                case DataType.SWord:
                {
                    short s = (short)o;
                    data[attr.Offset]     = (byte)(s & 0xff);
                    data[attr.Offset + 1] = (byte)((s >> 8) & 0xff);
                }
                break;

                case DataType.Word:
                {
                    ushort s = (ushort)o;
                    data[attr.Offset]     = (byte)(s & 0xff);
                    data[attr.Offset + 1] = (byte)((s >> 8) & 0xff);
                }
                break;

                case DataType.Int:
                {
                    int i      = (int)o;
                    int offset = attr.Offset;
                    data[offset++] = (byte)(i & 0xff);
                    data[offset++] = (byte)((i >> 8) & 0xff);
                    data[offset++] = (byte)((i >> 16) & 0xff);
                    data[offset++] = (byte)((i >> 24) & 0xff);
                }
                break;

                case DataType.Bool:
                    data[attr.Offset] = (bool)o ? (byte)1 : (byte)0;
                    break;

                case DataType.ByteArray:
                {
                    byte[] values = (byte[])o;

                    System.Array.Copy(values, 0, data, attr.Offset, attr.Size);
                }
                break;

                case DataType.ShortArray:
                {
                    short[] values = (short[])o;
                    int     offset = attr.Offset;
                    for (int i = 0; i < attr.Size; i++)
                    {
                        short s = values[i];
                        data[offset++] = (byte)(s & 0xff);
                        data[offset++] = (byte)((s >> 8) & 0xff);
                    }
                }
                break;

                case DataType.WordArray:
                {
                    ushort[] values = (ushort[])o;
                    int      offset = attr.Offset;
                    for (int i = 0; i < attr.Size; i++)
                    {
                        ushort s = values[i];
                        data[offset++] = (byte)(s & 0xff);
                        data[offset++] = (byte)((s >> 8) & 0xff);
                    }
                }
                break;

                case DataType.PString:
                {
                    byte   b = (byte)attr.Size;
                    string s = (string)o;
                    if (s.Length < b)
                    {
                        b = (byte)s.Length;
                    }

                    data[attr.Offset] = b;
                    for (int i = 0; i < b; i++)
                    {
                        data[attr.Offset + i + 1] = (byte)s[i];
                    }
                }
                break;

                case DataType.Cust1Array:
                {
                    int         i  = attr.Offset;
                    StatValue[] sv = (StatValue[])o;
                    foreach (StatValue v in sv)
                    {
                        data[i++] = (byte)v.cur;
                        data[i++] = (byte)v.full;
                    }
                }
                break;

                default:
                    throw new NotImplementedException();
                }
            }
        }
Esempio n. 2
0
 internal void Save()
 {
     _dataIO.Write(_pairWrapper);
 }