Пример #1
0
        public uint Import(out object destObj, byte[] pBuffer, uint _nIndex, bool bHavMag, Type objType)
        {
            destObj = Activator.CreateInstance(objType);
            uint nIndex = _nIndex;

            try
            {
                if (pBuffer == null)
                {
                    throw(new Exception("0"));
                }
                Type     t      = objType;
                string[] sfiarr = UniWebLib.Struct_ST.GetStructMember(t.Name);

                if (sfiarr == null)
                {
                    FieldInfo[] ttfiarr = objType.GetFields();
                    sfiarr = new string[ttfiarr.Length];
                    for (int fi = 0; fi < ttfiarr.Length; fi++)
                    {
                        sfiarr[fi] = ttfiarr[fi].Name;
                    }
                    //throw (new Exception("1," + t.Name));
                }
                if (bHavMag)
                {
                    if (pBuffer.Length < nIndex + 4)
                    {
                        throw(new Exception("2"));
                    }
                    uint nValue = byte2uint(pBuffer, nIndex); nIndex += 4;
                    if (nValue != (uint)UNISMAG.UNISTRUCT)
                    {
                        throw(new Exception("3,nIndex=" + nIndex));
                    }
                }
                uint nCount = (uint)sfiarr.Length;
                for (uint i = 0; i < nCount; i++)
                {
                    if (sfiarr[i] == "")
                    {
                        continue;
                    }

                    FieldInfo fino = t.GetField(sfiarr[i]);
                    if (fino == null)
                    {
                        continue;
                    }

                    Type itt = fino.FieldType;

                    if (!itt.IsPublic)
                    {
                        continue;
                    }
                    //一般Reserved只放在第一个成员,为加速优化
                    if (i == 0)
                    {
                        if (itt == typeof(Reserved))
                        {
                            continue;
                        }
                    }

                    if (pBuffer.Length < nIndex + 4)
                    {
                        throw(new Exception("4"));
                    }
                    UTFLAG dwFType = (UTFLAG)byte2uint(pBuffer, nIndex); nIndex += 4;

                    //Logger.Trace("Type="+itt.ToString()+",Name="+fino.Name+",dwFType="+dwFType.ToString("x"));
                    if (itt == typeof(string))
                    {
                        if (((uint)dwFType & (uint)UTFLAG.UTSTREAM) == 0)
                        {
                            throw (new Exception("5," + objType.Name + "." + fino.Name + "(i=" + i.ToString() + ":nIndex=" + nIndex.ToString() + ")类型错误:dwFType=" + dwFType.ToString("x")));
                        }

                        uint nSize = (uint)(dwFType & (UTFLAG)UTMASK.UTSIZEMASK);;
                        if ((dwFType & UTFLAG.UTFILL) == 0)
                        {
                            nSize = 0;
                        }
                        if (pBuffer.Length < nIndex + nSize)
                        {
                            throw(new Exception("6,pBuffer.Length=" + pBuffer.Length + ",nIndex=" + nIndex + ",dwFType=" + dwFType));
                        }
                        if (((dwFType & UTFLAG.UTFILL) != 0) &&
                            ((dwFType & UTFLAG.UTLOCK) == 0)
                            )
                        {
                            UniStruct.UniSZ newValue = new UniStruct.UniSZ();
                            newValue.Set(pBuffer, nIndex, nSize); nIndex += nSize;
                            //todo
                            //对html进行解码
                            //  string newValueTemp = newValue.ToString();
                            // newValueTemp = System.Web.HttpContext.Current.Server.HtmlDecode(newValueTemp);
                            // fino.SetValueDirect(__makeref(destObj), newValueTemp);

                            fino.SetValueDirect(__makeref(destObj), newValue.ToString());
                        }
                        else
                        {
                            if (((dwFType & UTFLAG.UTFILL) != 0) &&
                                ((dwFType & UTFLAG.UTLOCK) == 0))
                            {
                                nIndex += nSize;
                            }
                        }
                    }
                    else if (itt == typeof(uint) || itt == typeof(uint?) || itt.IsEnum || (itt.IsSerializable && !itt.IsArray))
                    {
                        if (((dwFType & UTFLAG.UTFILL) != 0) &&
                            ((dwFType & UTFLAG.UTLOCK) == 0)
                            )
                        {
                            if (pBuffer.Length < nIndex + 4)
                            {
                                throw(new Exception("8"));
                            }
                            uint nValue = byte2uint(pBuffer, nIndex); nIndex += 4;
                            if (itt.IsEnum)
                            {
                                Object enumobj = System.Enum.ToObject(itt, nValue);
                                fino.SetValueDirect(__makeref(destObj), enumobj);
                            }
                            else
                            {
                                fino.SetValueDirect(__makeref(destObj), nValue);
                            }
                        }
                        else
                        {
                            if (((dwFType & UTFLAG.UTFILL) != 0) &&
                                ((dwFType & UTFLAG.UTLOCK) == 0))
                            {
                                nIndex += 4;
                            }
                        }
                    }
                    else
                    {
                        object subobj;
                        uint   nSize = (uint)(dwFType & (UTFLAG)UTMASK.UTSIZEMASK);
                        if (nSize > 0)
                        {
                            if (itt.IsArray)
                            {
                                CUniStructArrayCS CSUSA = new CUniStructArrayCS();
                                if (CSUSA.Import(out subobj, pBuffer, nIndex, itt) != nSize)
                                {
                                    //
                                }
                            }
                            else
                            {
                                if (Import(out subobj, pBuffer, nIndex, true, itt) != nSize)
                                {
                                    //
                                }
                            }
                            fino.SetValueDirect(__makeref(destObj), subobj);
                        }
                        nIndex += nSize;
                    }
                }
            }
            catch (Exception e)
            {
                Logger.Trace("UniStruct.Import faild:" + e.Message + "," + e.ToString());
                return(0);
            }
            return((uint)nIndex - _nIndex);
        }
Пример #2
0
        public uint Import(ref T destObj, byte[] pBuffer, uint _nIndex, bool bHavMag)
        {
            uint nIndex = _nIndex;

            try
            {
                if (pBuffer == null)
                {
                    throw(new Exception("0"));
                }
                if (destObj == null)
                {
                    destObj = new T();
                }
                Type        t     = destObj.GetType();
                FieldInfo[] fiarr = t.GetFields();
                if (fiarr == null)
                {
                    throw(new Exception("1"));
                }
                if (bHavMag)
                {
                    if (pBuffer.Length < nIndex + 4)
                    {
                        throw(new Exception("2"));
                    }
                    uint nValue = byte2uint(pBuffer, nIndex); nIndex += 4;
                    if (nValue != (uint)UNISMAG.UNISTRUCT)
                    {
                        throw(new Exception("3,nIndex=" + nIndex));
                    }
                }
                CUniStruct <object> pUnkownObj = new CUniStruct <object>();
                uint       nCount     = (uint)fiarr.GetLength(0);
                UniVarItem emptyValue = new UniVarItem();
                for (uint i = 0; i < nCount; i++)
                {
                    object     poValue  = fiarr[i].GetValue(destObj);
                    Type       itt      = fiarr[i].FieldType;
                    UniVarItem puiValue = emptyValue;
                    if (pBuffer.Length < nIndex + 4)
                    {
                        throw(new Exception("4"));
                    }
                    UTFLAG dwFType = (UTFLAG)byte2uint(pBuffer, nIndex); nIndex += 4;

                    //Logger.Trace("Type="+itt.ToString()+",Name="+fiarr[i].Name+",dwFType="+dwFType.ToString("x"));
                    if ((poValue != null) && ((itt == typeof(UniSZ)) || (itt == typeof(UniDW))))
                    {
                        puiValue = (UniVarItem)poValue;
                        if (!puiValue.IsValid())
                        {
                            continue;
                        }
                        if (((uint)puiValue.m_dwType & (uint)UTMASK.UTTYPEMASK) != ((uint)dwFType & (uint)UTMASK.UTTYPEMASK))
                        {
                            throw(new Exception("5," + fiarr[i].Name + "(i=" + i.ToString() + ":nIndex=" + nIndex.ToString() + ")类型错误:dwFType=" + dwFType.ToString("x")));
                        }
                    }
                    if (itt == typeof(UniSZ))
                    {
                        UniSZ newValue = new UniSZ();
                        uint  nSize    = (uint)(dwFType & (UTFLAG)UTMASK.UTSIZEMASK);
                        if ((dwFType & UTFLAG.UTFILL) == 0)
                        {
                            nSize = 0;
                        }
                        if (pBuffer.Length < nIndex + nSize)
                        {
                            throw(new Exception("6,pBuffer.Length=" + pBuffer.Length + ",nIndex=" + nIndex + ",dwFType=" + dwFType));
                        }
                        if (!puiValue.IsLock() &&
                            ((dwFType & UTFLAG.UTFILL) != 0) &&
                            ((dwFType & UTFLAG.UTLOCK) == 0)
                            )
                        {
                            newValue.Set(pBuffer, nIndex, nSize); nIndex += nSize;
                            fiarr[i].SetValueDirect(__makeref(destObj), newValue);
                        }
                        else
                        {
                            if (((dwFType & UTFLAG.UTFILL) != 0) &&
                                ((dwFType & UTFLAG.UTLOCK) == 0))
                            {
                                nIndex += nSize;
                            }
                            if (!puiValue.IsLock())
                            {
                                fiarr[i].SetValueDirect(__makeref(destObj), newValue);
                            }
                        }
                    }
                    else if (itt == typeof(UniDW))
                    {
                        UniDW newValue = new UniDW();
                        if (!puiValue.IsLock() &&
                            ((dwFType & UTFLAG.UTFILL) != 0) &&
                            ((dwFType & UTFLAG.UTLOCK) == 0)
                            )
                        {
                            if (pBuffer.Length < nIndex + 4)
                            {
                                throw(new Exception("8"));
                            }
                            uint nValue = byte2uint(pBuffer, nIndex); nIndex += 4;
                            newValue.Set(nValue);
                            fiarr[i].SetValueDirect(__makeref(destObj), newValue);
                            //fiarr[i].SetValue(destObj,newValue);
                        }
                        else
                        {
                            if (((dwFType & UTFLAG.UTFILL) != 0) &&
                                ((dwFType & UTFLAG.UTLOCK) == 0))
                            {
                                nIndex += 4;
                            }
                            if (!puiValue.IsLock())
                            {
                                fiarr[i].SetValueDirect(__makeref(destObj), newValue);
                            }
                        }
                    }
                    else
                    {
                        nIndex -= 4;
                        uint nSize = pUnkownObj.Import(ref poValue, pBuffer, nIndex, false);
                        fiarr[i].SetValueDirect(__makeref(destObj), poValue);
                        if (nSize == 0)
                        {
                            throw(new Exception("9"));
                        }
                        nIndex += nSize;
                    }
                }
            }
            catch (Exception e)
            {
                Logger.Trace("UniStruct.Import faild:" + e.Message + "," + e.ToString());
                return(0);
            }
            return((uint)nIndex - _nIndex);
        }