Example #1
0
 /// <summary>
 /// 解码快照
 /// </summary>
 public virtual void DecodeSnapshot(ulong rid, bool isNew, CodedInputStream reader)
 {
     this.rid = rid;
     this.id  = reader.ReadInt32();
     if (isNew)
     {
         this.LoadDefs();
         this.OnInit();
     }
     this.markToDestroy = reader.ReadBool();
     this.position      = new FVec2(( Fix64 )reader.ReadDouble(), ( Fix64 )reader.ReadDouble());
     this.direction     = new FVec2(( Fix64 )reader.ReadDouble(), ( Fix64 )reader.ReadDouble());
 }
Example #2
0
        public override void DecodeSnapshot(ulong rid, bool isNew, CodedInputStream reader)
        {
            base.DecodeSnapshot(rid, isNew, reader);
            this.team             = reader.ReadInt32();
            this.name             = reader.ReadString();
            this.hp               = reader.ReadInt32();
            this.mhp              = reader.ReadInt32();
            this.mp               = ( Fix64 )reader.ReadDouble();
            this.mmp              = reader.ReadInt32();
            this.mpRecover        = reader.ReadInt32();
            this.atk              = reader.ReadInt32();
            this.def              = reader.ReadInt32();
            this.disableMove      = reader.ReadInt32();
            this.disableTurn      = reader.ReadInt32();
            this.disableSkill     = reader.ReadInt32();
            this.disableCollision = reader.ReadInt32();
            this.supperArmor      = reader.ReadInt32();
            this.invulnerAbility  = reader.ReadInt32();
            this.moveDirection    = new FVec2(( Fix64 )reader.ReadDouble(), ( Fix64 )reader.ReadDouble());
            this.intersectVector  = new FVec2(( Fix64 )reader.ReadDouble(), ( Fix64 )reader.ReadDouble());
            this.phyxSpeed        = new FVec2(( Fix64 )reader.ReadDouble(), ( Fix64 )reader.ReadDouble());
            this.velocity         = ( Fix64 )reader.ReadDouble();
            this.isDead           = reader.ReadBool();
            this.t_hp_add         = reader.ReadInt32();
            this.t_mp_add         = reader.ReadInt32();
            this.t_atk_add        = reader.ReadInt32();
            this.t_def_add        = reader.ReadInt32();
            this.t_speed_add      = reader.ReadInt32();

            this.fsm.DecodeSnapshot(reader);
            reader.ReadBytes();
        }
Example #3
0
        /// <summary>
        /// Read value from <see cref="System.Protobuf.Object"/>
        /// </summary>
        /// <param name="obj"><see cref="System.Protobuf.Object"/> to read from</param>
        /// <returns>Value in the correct type - null if not capable of converting</returns>
        public static object ToCLR(this System.Protobuf.Object obj)
        {
            var    type  = (Types)obj.Type;
            object value = null;

            using (var stream = new CodedInputStream(obj.Content.ToByteArray()))
            {
                switch (type)
                {
                case Types.String: value = stream.ReadString(); break;

                case Types.Int32: value = stream.ReadInt32(); break;

                case Types.Int64: value = stream.ReadInt64(); break;

                case Types.UInt32: value = stream.ReadUInt32(); break;

                case Types.UInt64: value = stream.ReadUInt64(); break;

                case Types.Float: value = stream.ReadFloat(); break;

                case Types.Double: value = stream.ReadDouble(); break;

                case Types.Boolean: value = stream.ReadBool(); break;

                case Types.DateTime: value = stream.ReadInt64().ToDateTime(); break;

                case Types.DateTimeOffset: value = stream.ReadInt64().ToDateTimeOffset(); break;

                case Types.Guid: value = new Guid(stream.ReadBytes().ToByteArray()); break;
                }
            }
            return(value);
        }
        object ReadValue(CodedInputStream inputStream, object value, Type type, Type targetType, IValueConverter converter)
        {
            if (type == typeof(Guid))
            {
                _ = inputStream.ReadLength();
                var guidAsBytes = inputStream.ReadBytes();
                value = new Guid(guidAsBytes.ToByteArray());
            }
            else if (type == typeof(string))
            {
                _     = inputStream.ReadLength();
                value = inputStream.ReadString();
            }
            else if (type == typeof(int))
            {
                value = inputStream.ReadInt32();
            }
            else if (type == typeof(long))
            {
                value = inputStream.ReadInt64();
            }
            else if (type == typeof(uint))
            {
                value = inputStream.ReadUInt32();
            }
            else if (type == typeof(ulong))
            {
                value = inputStream.ReadUInt64();
            }
            else if (type == typeof(float))
            {
                value = inputStream.ReadFloat();
            }
            else if (type == typeof(double))
            {
                value = inputStream.ReadDouble();
            }
            else if (type == typeof(bool))
            {
                value = inputStream.ReadBool();
            }
            else if (type == typeof(DateTimeOffset) || type == typeof(DateTime))
            {
                value = DateTimeOffset.FromUnixTimeMilliseconds(inputStream.ReadInt64());
                if (type == typeof(DateTime))
                {
                    value = ((DateTimeOffset)value).UtcDateTime;
                }
            }

            if (converter != null)
            {
                value = converter.ConvertFrom(targetType, value);
            }
            return(value);
        }
Example #5
0
        public double ParseRawDouble()
        {
            const int        encodedSize = sizeof(double);
            CodedInputStream cis         = new CodedInputStream(doubleInputBuffer);
            double           sum         = 0;

            for (int i = 0; i < BytesToParse / encodedSize; i++)
            {
                sum += cis.ReadDouble();
            }
            return(sum);
        }
Example #6
0
    public Dictionary <Type, object> ReadParam()
    {
        Dictionary <Type, object> pairs = new Dictionary <Type, object>();

        foreach (var item in Params)
        {
            object o = null;
            Type   t = Type.GetType(item.Name);

            if (t != null)
            {
                CodedInputStream cis = new CodedInputStream(item.Data.ToByteArray());
                if (t == typeof(int))
                {
                    o = cis.ReadInt32();
                }
                else if (t == typeof(long))
                {
                    o = cis.ReadInt64();
                }
                else if (t == typeof(float))
                {
                    o = cis.ReadFloat();
                }
                else if (t == typeof(double))
                {
                    o = cis.ReadDouble();
                }
                else if (t == typeof(bool))
                {
                    o = cis.ReadBool();
                }
                else if (t == typeof(string))
                {
                    o = cis.ReadString();
                }
                else if (typeof(IMessage).IsAssignableFrom(t))
                {
                    o = Activator.CreateInstance(t);
                    cis.ReadMessage((IMessage)o);
                }

                pairs.Add(t, o);
            }
        }

        return(pairs);
    }
Example #7
0
        /// <summary>
        /// Convert a Protocol Buffer value type, encoded as a byte string, to a C# value.
        /// </summary>
        public static object ReadValue(ByteString value, Type type)
        {
            if (value.Length == 0)
            {
                throw new ArgumentException("Value is empty");
            }
            var stream = new CodedInputStream(value.ToByteArray());

            if (type == typeof(double))
            {
                return(stream.ReadDouble());
            }
            else if (type == typeof(float))
            {
                return(stream.ReadFloat());
            }
            else if (type == typeof(int))
            {
                return(stream.ReadInt32());
            }
            else if (type == typeof(long))
            {
                return(stream.ReadInt64());
            }
            else if (type == typeof(uint))
            {
                return(stream.ReadUInt32());
            }
            else if (type == typeof(ulong))
            {
                return(stream.ReadUInt64());
            }
            else if (type == typeof(bool))
            {
                return(stream.ReadBool());
            }
            else if (type == typeof(string))
            {
                return(stream.ReadString());
            }
            else if (type == typeof(byte[]))
            {
                return(stream.ReadBytes().ToByteArray());
            }
            throw new ArgumentException(type + " is not a Protocol Buffer value type");
        }
Example #8
0
        /// <summary>
        /// Decode a value of the given type.
        /// Should not be called directly. This interface is used by service client stubs.
        /// </summary>
        public static object Decode(ByteString value, Type type, Connection client)
        {
            var stream = new CodedInputStream(value.ToByteArray());

            if (type == typeof(double))
            {
                return(stream.ReadDouble());
            }
            else if (type == typeof(float))
            {
                return(stream.ReadFloat());
            }
            else if (type == typeof(int))
            {
                return(stream.ReadInt32());
            }
            else if (type == typeof(long))
            {
                return(stream.ReadInt64());
            }
            else if (type == typeof(uint))
            {
                return(stream.ReadUInt32());
            }
            else if (type == typeof(ulong))
            {
                return(stream.ReadUInt64());
            }
            else if (type == typeof(bool))
            {
                return(stream.ReadBool());
            }
            else if (type == typeof(string))
            {
                return(stream.ReadString());
            }
            else if (type == typeof(byte[]))
            {
                return(stream.ReadBytes().ToByteArray());
            }
            else if (type.IsEnum)
            {
                return(stream.ReadInt32());
            }
            else if (typeof(RemoteObject).IsAssignableFrom(type))
            {
                if (client == null)
                {
                    throw new ArgumentException("Client not passed when decoding remote object");
                }
                var id = stream.ReadUInt64();
                if (id == 0)
                {
                    return(null);
                }
                return((RemoteObject)Activator.CreateInstance(type, client, id));
            }
            else if (typeof(IMessage).IsAssignableFrom(type))
            {
                IMessage message = (IMessage)Activator.CreateInstance(type);
                message.MergeFrom(stream);
                return(message);
            }
            else if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(IList <>))
            {
                return(DecodeList(value, type, client));
            }
            else if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(IDictionary <,>))
            {
                return(DecodeDictionary(value, type, client));
            }
            else if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(ISet <>))
            {
                return(DecodeSet(value, type, client));
            }
            else if (type.IsGenericType &&
                     (type.GetGenericTypeDefinition() == typeof(Tuple <>) ||
                      type.GetGenericTypeDefinition() == typeof(Tuple <,>) ||
                      type.GetGenericTypeDefinition() == typeof(Tuple <, ,>) ||
                      type.GetGenericTypeDefinition() == typeof(Tuple <, , ,>) ||
                      type.GetGenericTypeDefinition() == typeof(Tuple <, , , ,>) ||
                      type.GetGenericTypeDefinition() == typeof(Tuple <, , , , ,>)))
            {
                return(DecodeTuple(value, type, client)); // TODO: ugly handing of tuple types
            }
            throw new ArgumentException(type + " is not a serializable type");
        }
 public override double Read(CodedInputStream stream)
 {
     return(stream.ReadDouble());
 }