Beispiel #1
0
        int WriteStructList(object value, DataBuffer db, DataTypeInfo info)
        {
            var list = value as IList;

            if (list != null)
            {
                if (list.Count > 0)
                {
                    int    el  = info.ElementLength;
                    int    dl  = info.DataLength;
                    IntPtr ptr = UnsafeOperation.GetObjectAddr(list);
                    ptr = UnsafeOperation.GetListElement(ptr, 0);

                    FakeStructArray fsa = new FakeStructArray(db, el, list.Count);
                    unsafe
                    {
                        FakeStruct fs = new FakeStruct(db, el, fsa.ip);
                        for (int i = 0; i < list.Count; i++)
                        {
                            fs.SetPoint(fsa[i]);
                            WriteStruct(info, ptr, fs);
                            ptr += dl;
                        }
                    }
                    return(db.AddData(fsa, DataType.FakeStructArray));
                }
            }
            return(0);
        }
Beispiel #2
0
        object ReadObject(DataTypeInfo dt, FakeStruct fake, int dataIndex)
        {
            for (int i = 0; i < objList.Count; i++)
            {
                if (objList[i].index == dataIndex)
                {
                    return(objList[i].instance);
                }
            }
            var          obj = dt.Construction();
            ClassContext cc  = new ClassContext();

            cc.instance = obj;
            cc.index    = dataIndex;
            objList.Add(cc);
            IntPtr ptr = UnsafeOperation.GetObjectAddr(obj);
            var    mf  = dt.matchedFields;

            if (mf != null)
            {
                for (int i = 0; i < mf.Length; i++)
                {
                    ReadField(fake, 0, mf[i], ptr);
                }
            }
            return(obj);
        }
Beispiel #3
0
        void WriteArray2(FakeStruct fake, int offset, DataFieldInfo info, IntPtr ptr)
        {
            var    buffer = fake.buffer;
            object value;

            value = UnsafeOperation.GetObject(ptr + info.MemOffset);
            if (value == null)
            {
                return;
            }
            int a = info.DBType & 31;

            switch (a)
            {
            case DataType2.Byte:
                fake[offset] = WriteArray2 <byte>(value, buffer, DataType.ByteArray);
                break;

            case DataType2.Int16:
                fake[offset] = WriteArray2 <Int16>(value, buffer, DataType.ByteArray);
                break;

            case DataType2.Int32:
                fake[offset] = WriteArray2 <Int32>(value, buffer, DataType.ByteArray);
                break;

            case DataType2.Float:
                fake[offset] = WriteArray2 <float>(value, buffer, DataType.ByteArray);
                break;

            case DataType2.String:
                fake[offset] = WriteStringArray2(value, buffer, DataType.ByteArray);
                break;

            case DataType2.Int64:
                fake[offset] = WriteArray2 <Int64>(value, buffer, DataType.ByteArray);
                break;

            case DataType2.Double:
                fake[offset] = WriteArray2 <Double>(value, buffer, DataType.ByteArray);
                break;

            case DataType2.Class:
                fake[offset] = WriteClassArray2(value, fake.buffer, info);
                break;

            case DataType2.Struct:
                fake[offset] = WriteStructArray2(value, fake.buffer, info);
                break;
            }
        }
Beispiel #4
0
        object ReadStructToObject(DataTypeInfo dt, FakeStruct fake, int start)
        {
            var    obj = dt.Construction();
            IntPtr ptr = UnsafeOperation.GetObjectAddr(obj);
            var    mf  = dt.matchedFields;

            if (mf != null)
            {
                for (int i = 0; i < mf.Length; i++)
                {
                    ReadField(fake, start, mf[i], ptr);
                }
            }
            return(obj);
        }
Beispiel #5
0
        /// <summary>
        /// 写入类的数据
        /// </summary>
        /// <param name="typeIndex"></param>
        /// <param name="instance"></param>
        /// <param name="db"></param>
        /// <returns></returns>
        int WriteObject(DataTypeInfo dt, object instance, DataBuffer db)
        {
            if (instance == null)
            {
                return(0);
            }
            for (int i = 0; i < objList.Count; i++)
            {
                if (objList[i].instance == instance)
                {
                    return(objList[i].index);
                }
            }
            int typeIndex = dt.typeIndex;

            if (HaveDerived)
            {
                string fullName = instance.ToString();
                if (fullName != dt.fullName)
                {
                    dt = dataInfo.FindTypeInfo2(fullName);
                }
                if (dt == null)
                {
                    return(0);
                }
                else
                {
                    DerivedData = true;
                    typeIndex   = dt.typeIndex;
                }
            }
            FakeStruct   fake = new FakeStruct(db, types[typeIndex].ElementLength);
            int          r    = db.AddData(fake, DataType.FakeStruct);
            ClassContext cc   = new ClassContext();

            cc.instance = instance;
            cc.index    = typeIndex << 16 | r;
            objList.Add(cc);
            var    dts = dt.dataFeilds;
            IntPtr ptr = UnsafeOperation.GetObjectAddr(instance);

            for (int i = 0; i < dts.Count; i++)
            {
                WriteField(fake, 0, dts[i], ptr);
            }
            return(cc.index);
        }
Beispiel #6
0
        void WriteField(FakeStruct fake, int start, DataFieldInfo info, IntPtr ptr)
        {
            int offset = start + info.DataOffset;

            if (info.DBType < 32)
            {
                if (info.DBType < 16)//数值
                {
                    WriteValue(fake, offset, info, ptr);
                }
                else//引用类型
                {
                    if (info.DBType == DataType2.String)
                    {
                        var obj = UnsafeOperation.GetObject(ptr + info.MemOffset);
                        if (obj != null)
                        {
                            fake[offset] = fake.buffer.AddData(obj, DataType.String);
                        }
                    }
                    else
                    {
                        var obj = UnsafeOperation.GetObject(ptr + info.MemOffset);
                        if (obj != null)
                        {
                            fake[offset] = WriteObject(info.TypeFullName, obj, fake.buffer);
                        }
                    }
                }
            }
            else if (info.DBType < 64)//数组
            {
                WriteArray(fake, offset, info, ptr);
            }
            else//二维数组
            {
                WriteArray2(fake, offset, info, ptr);
            }
        }
Beispiel #7
0
        void WriteArray(FakeStruct fake, int offset, DataFieldInfo info, IntPtr ptr)
        {
            var    buffer = fake.buffer;
            object value;

            value = UnsafeOperation.GetObject(ptr + info.MemOffset);
            if (value == null)
            {
                return;
            }
            int a = info.DBType & 31;

            if (info.TypeName == DataType2.List)
            {
                switch (a)
                {
                case DataType2.Byte:
                    fake[offset] = buffer.AddData(ToArray <byte>(value), DataType.ByteArray);
                    break;

                case DataType2.Int16:
                    fake[offset] = buffer.AddData(ToArray <Int16>(value), DataType.Int16Array);
                    break;

                case DataType2.Int32:
                    fake[offset] = buffer.AddData(ToArray <Int32>(value), DataType.Int32Array);
                    break;

                case DataType2.Float:
                    fake[offset] = buffer.AddData(ToArray <float>(value), DataType.FloatArray);
                    break;

                case DataType2.String:
                {
                    string[] str = ToArray <string>(value);
                    if (str != null)
                    {
                        fake[offset] = buffer.AddData(new FakeStringArray(str), DataType.FakeStringArray);
                    }
                }
                break;

                case DataType2.Int64:
                    fake[offset] = buffer.AddData(ToArray <Int64>(value), DataType.Int64Array);
                    break;

                case DataType2.Double:
                    fake[offset] = buffer.AddData(ToArray <Double>(value), DataType.DoubleArray);
                    break;

                case DataType2.Class:
                    fake[offset] = WriteClassArray(value, fake.buffer, info.typeInfo);
                    break;

                case DataType2.Struct:
                    fake[offset] = WriteStructList(value, fake.buffer, info.typeInfo);
                    break;
                }
            }
            else
            {
                switch (a)
                {
                case DataType2.Byte:
                    fake[offset] = buffer.AddData(value, DataType.ByteArray);
                    break;

                case DataType2.Int16:
                    fake[offset] = buffer.AddData(value, DataType.Int16Array);
                    break;

                case DataType2.Int32:
                    fake[offset] = buffer.AddData(value, DataType.Int32Array);
                    break;

                case DataType2.Float:
                    fake[offset] = buffer.AddData(value, DataType.FloatArray);
                    break;

                case DataType2.String:
                    fake[offset] = buffer.AddData(new FakeStringArray(value as string[]), DataType.FakeStringArray);
                    break;

                case DataType2.Int64:
                    fake[offset] = buffer.AddData(value, DataType.Int64Array);
                    break;

                case DataType2.Double:
                    fake[offset] = buffer.AddData(value, DataType.DoubleArray);
                    break;

                case DataType2.Class:
                    fake[offset] = WriteClassArray(value, fake.buffer, info.typeInfo);
                    break;

                case DataType2.Struct:
                    fake[offset] = WriteStructArray(value, fake.buffer, info.typeInfo);
                    break;
                }
            }
        }
Beispiel #8
0
        public void Analysis(DataInfo analysis, Type typ)
        {
            type     = typ;
            name     = type.Name;
            fullName = type.FullName;
            isStruct = typ.IsValueType;
            var fs   = typ.GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
            int len  = 0;
            int dlen = 0;

            dataFeilds = new List <DataFieldInfo>();
            for (int i = 0; i < fs.Length; i++)
            {
                if (fs[i].IsPublic | fs[i].IsDefined(typeof(SerializeField)))
                {
                    if (!fs[i].IsStatic)
                    {
                        if (!fs[i].IsNotSerialized)
                        {
                            if (fs[i].FieldType.Name != DataType2.Action)
                            {
                                DataFieldInfo df = new DataFieldInfo();
                                df.Analysis(analysis, fs[i]);
                                if (analysis.Unsafe)
                                {
                                    df.MemOffset = UnsafeOperation.GetFeildOffset(df.field.FieldHandle.Value, isStruct);
                                }
                                df.DataOffset = (Int16)len;
                                len          += df.ElementLength;
                                dlen         += df.DataLength;
                                dataFeilds.Add(df);
                            }
                        }
                    }
                }
            }
            DataLength    = dlen;
            ElementLength = (Int16)len;
            if (analysis.CreateConstruction)
            {
#if ENABLE_MONO
                try
                {
                    if (isStruct)
                    {
                        Construction = DataInfo.GetCreateStructFunc(typ);
                    }
                    else
                    {
                        Construction = DataInfo.GetCreateFunc(typ);
                    }
                }catch
                {
                    Debug.LogError("Construction Error ! " + typ.Name);
                    throw (new Exception());
                }
#else
                Construction = () => { return(Activator.CreateInstance(type)); };
#endif
            }
        }
Beispiel #9
0
 object ReadStructList(DataFieldInfo info, DataBuffer buffer, object obj)
 {
     if (obj != null)
     {
         Int16[] addr16 = obj as Int16[];
         if (addr16 != null)
         {
             var    con  = info.Construction();
             IList  list = con as IList;
             var    tmp  = info.typeInfo.Construction();
             IntPtr ptr  = UnsafeOperation.GetStructAddr(tmp);
             for (int i = 0; i < addr16.Length; i++)
             {
                 unsafe
                 {
                     byte *bp = (byte *)ptr;
                     for (int j = 0; j < info.typeInfo.DataLength; j++)
                     {
                         bp[j] = 0;
                     }
                 }
                 var fs = buffer.GetData(addr16[i]) as FakeStruct;
                 if (fs != null)
                 {
                     ReadStruct(info.typeInfo, fs, 0, ptr);
                 }
                 list.Add(tmp);
             }
             return(con);
         }
         else
         {
             Int32[] addr32 = obj as Int32[];
             if (addr32 != null)
             {
                 bool   isStruct = info.typeInfo.isStruct;
                 var    con      = info.Construction();
                 IList  list     = con as IList;
                 var    tmp      = info.typeInfo.Construction();
                 IntPtr ptr      = UnsafeOperation.GetStructAddr(tmp);
                 for (int i = 0; i < addr32.Length; i++)
                 {
                     unsafe
                     {
                         byte *bp = (byte *)ptr;
                         for (int j = 0; j < info.typeInfo.DataLength; j++)
                         {
                             bp[j] = 0;
                         }
                     }
                     int a     = addr32[i];
                     int index = a & 0xffff;
                     var fs    = buffer.GetData(index) as FakeStruct;
                     if (fs != null)
                     {
                         ReadStruct(info.typeInfo, fs, 0, ptr);
                     }
                     list.Add(tmp);
                 }
                 return(con);
             }
             else
             {
                 FakeStructArray fsa = obj as FakeStructArray;
                 if (fsa != null)
                 {
                     FakeStruct fs;
                     unsafe
                     {
                         fs = new FakeStruct(buffer, fsa.StructSize, fsa.ip);
                     }
                     var    con  = info.Construction();
                     IList  list = con as IList;
                     var    tmp  = info.typeInfo.Construction();
                     IntPtr ptr  = UnsafeOperation.GetStructAddr(tmp);
                     for (int i = 0; i < fsa.Length; i++)
                     {
                         unsafe
                         {
                             byte *bp = (byte *)ptr;
                             for (int j = 0; j < info.typeInfo.DataLength; j++)
                             {
                                 bp[j] = 0;
                             }
                         }
                         unsafe { fs.SetPoint(fsa[i]); }
                         ReadStruct(info.typeInfo, fs, 0, ptr);
                         list.Add(tmp);
                     }
                     return(con);
                 }
             }
         }
     }
     return(null);
 }
Beispiel #10
0
        void ReadField(FakeStruct fake, int start, DataFieldInfo info, IntPtr ptr)
        {
            int offset = start + info.OldFieldInfo.DataOffset;

            if (info.DBType < 11)
            {
                CompatibleReadValue(info, fake, offset, ptr);
            }
            else if (info.DBType < 32)
            {
                switch (info.DBType)
                {
                case DataType2.String:
                    string str = fake.GetData <string>(offset);
                    if (str != null)
                    {
                        UnsafeOperation.SetObject(ptr + info.MemOffset, str);
                    }
                    break;

                case DataType2.Class:
                    if (info.OldFieldInfo.DBType == DataType2.Struct)
                    {
                        UnsafeOperation.SetObject(ptr + info.MemOffset, ReadStructToObject(info.typeInfo, fake, offset));
                    }
                    else
                    {
                        FakeStruct fs = fake.GetData <FakeStruct>(offset);
                        if (fs != null)
                        {
                            if (HaveDerived)
                            {
                                int a      = fake[offset];
                                int dbtype = a >> 16;
                                int index  = a & 0xffff;
                                var dt     = oldTypes[dbtype].NewType;
                                if (dt != null)
                                {
                                    UnsafeOperation.SetObject(ptr + info.MemOffset, ReadObject(dt, fs, index));
                                }
                            }
                            else
                            {
                                UnsafeOperation.SetObject(ptr + info.MemOffset, ReadObject(info.typeInfo, fs, fake[offset] & 0xffff));
                            }
                        }
                    }
                    break;

                case DataType2.Struct:
                    if (info.OldFieldInfo.DBType == DataType2.Class)
                    {
                        var fs = fake.GetData <FakeStruct>(offset);
                        if (fs != null)
                        {
                            ReadStruct(info.typeInfo, fs, 0, ptr + info.MemOffset);
                        }
                    }
                    else
                    {
                        ReadStruct(info.typeInfo, fake, offset, ptr + info.MemOffset);
                    }
                    break;

                case DataType2.Boolean:
                    unsafe { *(byte *)(ptr + info.MemOffset) = (byte)fake[offset]; }
                    break;
                }
            }
            else if (info.DBType < 64)
            {
                if (info.TypeName == "List`1")
                {
                    UnsafeOperation.SetObject(ptr + info.MemOffset, ReadList(info, fake, offset));
                }
                else
                {
                    UnsafeOperation.SetObject(ptr + info.MemOffset, ReadArray(info, fake, offset));
                }
            }
            else
            {
                if (info.TypeName == "List`1")
                {
                    UnsafeOperation.SetObject(ptr + info.MemOffset, ReadList2(info, fake, offset));
                }
                else
                {
                    UnsafeOperation.SetObject(ptr + info.MemOffset, ReadArray2(info, fake, offset));
                }
            }
        }