Exemplo n.º 1
0
    /// <summary>
    /// Takes a byte array and attempts to deserialize a single object at the given offset.
    /// The deserialized object will be returned.
    /// </summary>
    /// <param name="bytes">Bytes previously serialized by the SerializationCtrl.</param>
    /// <param name="offset">An index within the array from which deserialization will be attempted.</param>
    public static object Deserialize(byte[] bytes, int offset)
    {
        DeserializationData data = new DeserializationData();

        data.bytes  = bytes;
        data.offset = offset;
        return(Deserialize(data));
    }
Exemplo n.º 2
0
        public override object CreateNewInstance(DeserializationData data, Type type)
        {
            int   elementTypeID = data.ReadInt4B();
            Type  elementType   = GetTypeBySerializeID(elementTypeID);
            int   arrLength     = data.ReadInt4B();
            Array arr           = Array.CreateInstance(elementType, arrLength);

            return(arr);
        }
Exemplo n.º 3
0
        public override void ReadBytes(DeserializationData data, object obj)
        {
            IList list  = (IList)obj;
            int   count = data.ReadInt4B();

            for (int i = 0; i < count; i++)
            {
                object val = SerializationCtrl.Deserialize(data);
                list.Add(val);
            }
        }
Exemplo n.º 4
0
        public override void ReadBytes(DeserializationData data, object obj)
        {
            Array arr = (Array)obj;

            long[] indices = { 0 };
            for (int i = 0; i < arr.Length; i++)
            {
                indices[0] = i;
                object val = SerializationCtrl.Deserialize(data);
                arr.SetValue(val, indices);
            }
        }
Exemplo n.º 5
0
        public override object CreateNewInstance(DeserializationData data, Type type)
        {
            int byteCount = data.ReadInt4B();

            byte[] strBytes = new byte[byteCount];
            for (int i = 0; i < byteCount; i++)
            {
                strBytes[i] = data.bytes[data.offset + i];
            }
            data.offset += byteCount;
            return(utf8.GetString(strBytes.ToArray()));
        }
Exemplo n.º 6
0
        void IUONSerialization.OnDeserialized(DeserializationData data)
        {
            if (this.image == null && this.asset != null)
            {
                string path = data.GetRawValue("image");

                if (string.IsNullOrEmpty(path) == false)
                {
                    this.image = Utility.GetIcon(this.asset.GetInstanceID());
                }
            }
        }
Exemplo n.º 7
0
        public override void ReadBytes(DeserializationData data, object obj)
        {
            FieldInfo[] props = obj.GetType().GetFields(
                BindingFlags.NonPublic
                | BindingFlags.Public
                | BindingFlags.Instance);

            foreach (var info in props)
            {
                object val = SerializationCtrl.Deserialize(data);
                info.SetValue(obj, val);
            }
        }
Exemplo n.º 8
0
 public override object CreateNewInstance(DeserializationData data, Type type)
 {
     byte[] arr = new Byte[sizeof(float)];
     for (int i = 0; i < sizeof(float); i++)
     {
         arr[i] = data.bytes[data.offset++];
     }
     if (!BitConverter.IsLittleEndian)
     {
         Array.Reverse(arr);
     }
     return(BitConverter.ToSingle(arr, 0));
 }
Exemplo n.º 9
0
    /// <summary>
    /// Takes a byte array, attempts to deserializes it and adds each deserialized
    /// object to the given output List.
    /// </summary>
    /// <param name="bytes">Bytes previously serialized by the SerializationCtrl.</param>
    /// <param name="output">A List to which deserialized objects will be added.</param>
    public static List <object> Deserialize(byte[] bytes, List <object> output)
    {
        DeserializationData data = new DeserializationData();

        data.bytes  = bytes;
        data.offset = 0;

        while (data.offset < bytes.Length)
        {
            object obj = Deserialize(data);
            output.Add(obj);
        }
        return(output);
    }
Exemplo n.º 10
0
        void IUONSerialization.OnDeserialized(DeserializationData data)
        {
            try
            {
                string path = this.GetStoreRowPath();

                if (File.Exists(path) == true)
                {
                    byte[] raw = File.ReadAllBytes(path);

                    if (raw != null && raw.Length > 0)
                    {
                        this.rows = Utility.DeserializeField <List <Row> >(raw);
                    }
                }
            }
            catch (Exception ex)
            {
                InternalNGDebug.VerboseLogException(ex);
            }
        }
Exemplo n.º 11
0
 public override object CreateNewInstance(DeserializationData data, Type type)
 {
     return(data.ReadInt8B());
 }
Exemplo n.º 12
0
 public override object CreateNewInstance(DeserializationData data, Type type)
 {
     return(Activator.CreateInstance(type));
 }
Exemplo n.º 13
0
        public DeserializeData Deserialize(byte[] datas, int dataLen)
        {
            DeserializeData result = null;

            if (datas != null)
            {
                using (MemoryStream memoryStream = new MemoryStream(datas, 0, dataLen))
                    using (BinaryReader binaryReader = new BinaryReader(memoryStream))
                    {
                        byte tID        = binaryReader.ReadByte();
                        int  bodyLength = binaryReader.ReadInt32();
                        if (!littleEnd)
                        {
                            bodyLength = bodyLength.SwapInt32();
                        }

                        byte gID = binaryReader.ReadByte();
                        byte uID = binaryReader.ReadByte();

                        if (Config.Detail_Debug_Log())
                        {
                            Debug.Log("---------net adapter deserialize msg data->" + dataLen + "^" + (bodyLength + NetUtils.MSG_HEADER_LEN));
                        }

                        if (dataLen == (bodyLength + NetUtils.MSG_HEADER_LEN))
                        {
                            result = new DeserializeData();
                            NetMsg msg = NetMsgPool.GenNetMsg(bodyLength);
                            if (msg == null)
                            {
                                Debugger.LogError("net adapter gen net msg failed->" + bodyLength);
                                return(null);
                            }
                            msg.set_tgu(tID, gID, uID);
                            if (Config.Detail_Debug_Log())
                            {
                                Debug.LogError("deserialize msg t g u->" + tID + "^" + gID + "^" + uID + "^" + bodyLength);
                            }
                            msg.copy_data(datas, NetUtils.MSG_HEADER_LEN, bodyLength);
                            result.data = msg;

                            if (gID <= NetUtils.SCRIPTTOP_GROUP)
                            {
#if JSSCRIPT
                                sb.Remove(0, sb.Length);
                                //sb.Append(protoNumber.ToString());
                                sb.Append(gID);
                                sb.Append(">");
                                sb.Append(uID);
                                sb.Append(">");
                                sb.Append(Convert.ToBase64String(rawBytes));

                                // util.Log.Log(sb.ToString());

                                result = new DeserializationData()
                                {
                                    Data     = sb.ToString(),
                                    toScript = true,
                                    gID      = gID,
                                    uID      = uID
                                               //protoNumber = protoNumber
                                };
#elif LUASCRIPT
                                result.toScript = true;
#endif
                            }
                            else
                            {
                                result.toScript = false;
                            }
                        }
                    }
            }

            return(result);
        }
Exemplo n.º 14
0
    /// <summary>
    /// Takes DeserializationData which includes a byte array and an offset into the array
    /// from where to start deserializing and attempts to deserialize a single object.
    /// The deserialized object or null will be returned.
    ///
    /// For more details on the serialization format refer to the documentation of the
    /// Serialize(SerializationData, object) method.
    ///
    /// This method may push errors to the internal error buffer when attempting to deserialize
    /// objects of unknown type, illegal data or data not generated by the SerializationCtrl.
    /// By the nature of the serialization process attempting to deserialize data not generated
    /// by the SerializationCtrl, manually modifying the raw data, providing an illegal offset
    /// or garbage data will result in undefined behavior. Any random byte array might randomly
    /// happen to be identical to valid, serialized data and is indistinguishable from actual data.
    /// For more information on the error handling process refer to the documentation of
    /// GetErrorBuffer().
    ///
    /// The following return values are possible:
    ///     if the serialized data is illegal
    ///         null is returned and an error is generated
    ///     if the serialized data was unknown to the serializer
    ///         null is returned and an error is generated
    ///     if the serialized data is unknown to the deserializer
    ///         null is returned and an error is generated
    ///     else
    ///         the deserialized object (or null) is returned
    /// </summary>
    /// <param name="data">The serialized data used during this deserialization process.</param>
    public static object Deserialize(DeserializationData data)
    {
        ChunkType type = data.ReadChunkType();

        // if something went wrong with the serialization process
        if (type == ChunkType.ILLEGAL)
        {
            errBuffer.Add(new ErrorDeserializeIllegalChunk());
            return(null);
        }
        // the serialized "object" was null
        if (type == ChunkType.NULL)
        {
            return(null);
        }
        // the deserializd object was already read before
        if (type == ChunkType.ID_REF)
        {
            short  objRefID = data.ReadInt2B();
            object refObj;
            bool   success = data.objToID.TryGetValue(objRefID, out refObj);
            if (success)
            {
                return(refObj);
            }
            // the serialized data is corrupted
            errBuffer.Add(new ErrorDeserializeUnknownObjectRef(objRefID));
            return(null);
        }
        int typeID = data.ReadInt3B();

        if (typeID == UNKNOWN_TYPE_ID)
        {
            errBuffer.Add(new ErrorDeserializeUnknownType(typeID));
            return(null);
        }

        TypeHelpTuple typeTuple;
        bool          isKnownType = typeIdMap.TryGetValue(typeID, out typeTuple);

        // this can happen if the SerializationCtrl on both devices has desynchronized type IDs.
        if (!isKnownType)
        {
            errBuffer.Add(new ErrorDeserializeUnknownType(typeID));
            return(null);
        }
        Serializer ser   = typeTuple.ser;
        short      objID = 0;

        if (!ser.IsPrimitive())
        {
            objID = data.nextID++;
        }
        object obj = ser.CreateNewInstance(data, typeTuple.type);

        if (!ser.IsPrimitive())
        {
            data.RegisterObject(obj, objID);
        }
        ser.ReadBytes(data, obj);
        return(obj);
    }
Exemplo n.º 15
0
 // Deserializes the bytes from data and creates a new instance of the given type
 public virtual object CreateNewInstance(DeserializationData data, Type type)
 {
     return(null);
 }
Exemplo n.º 16
0
        public override object CreateNewInstance(DeserializationData data, Type type)
        {
            byte i = data.bytes[data.offset++];

            return(i != bFalse);
        }
Exemplo n.º 17
0
 // Deserializes the bytes from data and modifies the given object accordingly
 public virtual void ReadBytes(DeserializationData data, object obj)
 {
 }