示例#1
0
 public void Add(IAmAnEndemeItem item)
 {
     if (item == null)
     {
         return;
     }
     if (item.GetType() == typeof(EndemeItem))
     {
         ListField.Add((EndemeItem)item);
     }
     else
     {
         RegField.Add((EndemeObject)item);
     }
 }
        public T ReadField <T>(int tag)
        {
            Type type = typeof(T);

            if (type == typeof(ByteField))
            {
                return((T)(object)new ByteField(tag, Read <byte>(tag)));
            }
            else if (type == typeof(ShortField))
            {
                return((T)(object)new ShortField(tag, Read <short>(tag)));
            }
            else if (type == typeof(IntField))
            {
                return((T)(object)new IntField(tag, Read <int>(tag)));
            }
            else if (type == typeof(LongField))
            {
                return((T)(object)new LongField(tag, Read <long>(tag)));
            }
            else if (type == typeof(FloatField))
            {
                return((T)(object)new FloatField(tag, Read <float>(tag)));
            }
            else if (type == typeof(DoubleField))
            {
                return((T)(object)new DoubleField(tag, Read <double>(tag)));
            }
            else if (type == typeof(StringField))
            {
                return((T)(object)new StringField(tag, Read <string>(tag)));
            }
            else
            {
                if (SkipToTag(tag))
                {
                    Header header = ReadHeader();
                    switch (header.Type)
                    {
                    case Type_Map:
                        if (type == typeof(MapField))
                        {
                            int count = Read <int>(0);
                            if (count < 0)
                            {
                                throw new Exception("Count invalid.");
                            }
                            MapField result = new MapField(header.Tag);
                            for (int i = 0; i < count; i++)
                            {
                                result.Add(ReadField(), ReadField());
                            }
                            return((T)(object)result);
                        }
                        break;

                    case Type_List:
                        if (type == typeof(ListField))
                        {
                            int count = Read <int>(0);
                            if (count < 0)
                            {
                                throw new Exception("Count invalid.");
                            }
                            ListField result = new ListField(header.Tag);
                            for (int i = 0; i < count; i++)
                            {
                                result.Add(ReadField());
                            }
                            return((T)(object)result);
                        }
                        break;

                    case Type_StructBegin:
                        if (type == typeof(StructField))
                        {
                            StructField result = new StructField(header.Tag);
                            while (true)
                            {
                                Header test = PeakHeader();
                                if (test.Type == Type_StructEnd)
                                {
                                    Seek(test.Count);
                                    break;
                                }
                                JceField field = ReadField();
                                result.Set(field.Tag, field);
                            }
                            return((T)(object)result);
                        }
                        break;

                    case Type_StructEnd:
                        throw new Exception("Got struct end field without struct begin tag.");

                    case Type_Zero:
                        if (type == typeof(ZeroField))
                        {
                            return((T)(object)new ZeroField(header.Tag));
                        }
                        break;

                    case Type_ByteArray:
                        if (type == typeof(ByteArrayField))
                        {
                            Header check = ReadHeader();
                            if (check.Type != Type_Byte)
                            {
                                throw new Exception("Data incorrect in type " + Type_ByteArray + ",except " + Type_Byte + " but got " + header.Type);
                            }
                            int length = Read <int>(0);
                            if (length < 0)
                            {
                                throw new Exception("Length error.");
                            }
                            return((T)(object)new ByteArrayField(header.Tag, Read(length)));
                        }
                        break;

                    default:
                        throw new ArgumentException("Unknown type " + header.Type + ".");
                    }
                    throw new ArgumentException("Type mismatch.");
                }
                throw new Exception("Tag not exists.");
            }
        }
示例#3
0
 public void Add(Endeme profile, EndemeValue value)
 {
     ListField.Add(new EndemeItem(value.Label, profile, value));
 }