Exemple #1
0
    public static object Deserialize(byte[] data)
    {
        MemoryStream ms = new MemoryStream(data);

        using (DataReader br = new DataReader(ms)) {
            ReadArg arg    = new ReadArg(br);
            int     offset = br.ReadInt32();
            if (offset > 0)
            {
                br.BaseStream.Seek(offset, SeekOrigin.Current);
                int num = br.ReadInt32();
                arg.typeRef.Capacity = arg.typeRef.Count + num;
                while (num > 0)
                {
                    string ts = br.ReadString();
                    Type   t  = Type.GetType(ts);
                    if (t == null)
                    {
                        Debug.LogError("未找到类型" + ts);
                        return(null);
                    }
                    arg.typeRef.Add(t);
                    num--;
                }
                br.BaseStream.Seek(sizeof(int), SeekOrigin.Begin);
            }
            try {
                return(ReadObj(null, arg));
            } catch (Exception e) {
                Debug.LogError("反序列化出错" + e);
                return(null);
            }
        }
    }
Exemple #2
0
    private static object ReadObj(Type t, ReadArg arg)
    {
        DataReader br = arg.br;

        if (t == null)
        {
            int ti = br.Read7BitEncodedInt();
            switch (ti)
            {
            case TYPE_NULL: return(null);

            case TYPE_REF: return(arg.objRef[br.Read7BitEncodedInt()]);
            }
            t = arg.typeRef[ti];
        }
        switch (t.Name)
        {
        case "Int32": return(br.ReadInt32());

        case "Int64": return(br.ReadInt64());

        case "Single": return(br.ReadSingle());

        case "Double": return(br.ReadDouble());

        case "String": string s = br.ReadString(); arg.objRef.Add(s); return(s);

        case "UInt32": return(br.ReadUInt32());

        case "UInt64": return(br.ReadUInt64());

        case "Byte": return(br.ReadByte());

        case "SByte": return(br.ReadSByte());

        case "Int16": return(br.ReadInt16());

        case "UInt16": return(br.ReadUInt16());

        case "Boolean": return(br.ReadBoolean());

        case "DateTime": return(DateTime.FromBinary(br.ReadInt64()));

        case "Vector2": return(new Vector2(br.ReadSingle(), br.ReadSingle()));

        case "Vector3": return(new Vector3(br.ReadSingle(), br.ReadSingle(), br.ReadSingle()));

        default: {
            if (t.IsEnum)
            {
                return(Enum.ToObject(t, br.ReadInt32()));
            }
            if (t.IsArray)
            {
                Type  t2  = t.GetElementType();
                int   num = br.Read7BitEncodedInt();
                Array arr = Array.CreateInstance(t2, num);
                arg.objRef.Add(arr);
                Type rt = t2.IsClass ? null : t2;
                for (int i = 0; i < num; i++)
                {
                    arr.SetValue(ReadObj(rt, arg), i);
                }
                return(arr);
            }
            if (typeof(IList).IsAssignableFrom(t))
            {
                IList list = (IList)Activator.CreateInstance(t);
                arg.objRef.Add(list);
                Type t2 = null;
                if (t.IsGenericType)
                {
                    t2 = t.GetGenericArguments()[0];
                    if (t2.IsClass)
                    {
                        t2 = null;
                    }
                }
                int num = br.Read7BitEncodedInt();
                for (int i = 0; i < num; i++)
                {
                    list.Add(ReadObj(t2, arg));
                }
                return(list);
            }
            if (typeof(IDictionary).IsAssignableFrom(t))
            {
                IDictionary dic = (IDictionary)Activator.CreateInstance(t);
                arg.objRef.Add(dic);
                Type tk = null;
                Type tv = null;
                if (t.IsGenericType)
                {
                    Type[] ts = t.GetGenericArguments();
                    tk = ts[0];
                    tv = ts[1];
                    if (tk.IsClass)
                    {
                        tk = null;
                    }
                    if (tv.IsClass)
                    {
                        tv = null;
                    }
                }
                int num = br.Read7BitEncodedInt();
                for (int i = 0; i < num; i++)
                {
                    object key   = ReadObj(tk, arg);
                    object value = ReadObj(tv, arg);
                    dic[key] = value;
                }
                return(dic);
            }
            {
                object obj = Activator.CreateInstance(t);
                if (t.IsClass)
                {
                    arg.objRef.Add(obj);
                }
                ClassInfo ci  = ClassInfo.Get(t);
                int       num = br.Read7BitEncodedInt();
                for (int i = 0; i < num; i++)
                {
                    string  name = (string)ReadObj(null, arg);
                    object  v    = ReadObj(null, arg);
                    FldInfo fi;
                    if (ci.fields.TryGetValue(name, out fi))
                    {
                        if (v == null)
                        {
                            if (fi.type.IsClass)
                            {
                                fi.SetValue(obj, null);
                            }
                        }
                        else
                        {
                            Type vt = v.GetType();
                            if (fi.type.IsEnum && vt == typeof(int) || fi.type.IsAssignableFrom(vt))
                            {
                                fi.SetValue(obj, v);
                            }
                        }
                    }
                }
                return(obj);
            }
        }
        }
    }