bool Lock(object srcObj, string szLock) { if (srcObj == null) { return(false); } Type t = srcObj.GetType(); FieldInfo[] fiarr = t.GetFields(); if (fiarr == null) { return(false); } int nIn = 0; CUniStruct <object> pUnkownObj = new CUniStruct <object>(); uint nCount = (uint)fiarr.GetLength(0); for (uint i = 0; i < nCount; i++) { bool bIsLock = (szLock[nIn] == '1'); object poValue = fiarr[i].GetValue(srcObj); Type itt = poValue.GetType(); if (itt == typeof(UniSZ)) { UniSZ pValue = (UniSZ)poValue; if (bIsLock) { pValue.Lock(); } else { pValue.UnLock(); } nIn++; } else if (itt == typeof(UniDW)) { UniDW pValue = (UniDW)poValue; if (bIsLock) { pValue.Lock(); } else { pValue.UnLock(); } nIn++; } else { if (pUnkownObj.Lock(poValue, szLock.Substring(nIn, szLock.Length - nIn)) == false) { return(false); } } } return(true); }
public CUniStructArray(uint nLength) { if (nLength == 0) { m_Data = new Hashtable(); } else { m_Data = new Hashtable((int)nLength); for (uint i = 0; i < nLength; i++) { m_Data[i] = new CUniStruct <T>(); } } }
public override byte[] Export() { uint nExportSize = 8; uint nDindex = 0; byte[] retBytes = null; byte[] bytes = null; uint nLen = 0; if (m_Data != null) { for (uint i = 0; i < m_Data.Count; i++) { if (m_Data[i] == null) { continue; } CUniStruct <T> vrItem = (CUniStruct <T>)m_Data[i]; nExportSize += vrItem.ExportSize(); nLen++; } } retBytes = new byte[nExportSize]; CUniStruct <T> .uint2byte((uint)UNISMAG.UNISARRAY, ref retBytes, nDindex); nDindex += 4; CUniStruct <T> .uint2byte((uint)nLen, ref retBytes, nDindex); nDindex += 4; if (m_Data != null) { for (uint i = 0; i < m_Data.Count; i++) { if (m_Data[i] == null) { continue; } CUniStruct <T> vrItem = (CUniStruct <T>)m_Data[i]; bytes = vrItem.Export(); Array.Copy(bytes, 0, retBytes, nDindex, bytes.Length); nDindex += (uint)bytes.Length; } } return(retBytes); }
public override uint Import(byte[] pBuffer, uint _index) { uint nIndex = _index; try { if (pBuffer == null || pBuffer.Length < 8) { throw(new Exception("0")); } uint nMag = CUniStruct <T> .byte2uint(pBuffer, nIndex); if (nMag != (uint)UNISMAG.UNISARRAY) { throw(new Exception("1")); } nIndex += 4; uint nLength = CUniStruct <T> .byte2uint(pBuffer, nIndex); nIndex += 4; m_Data = null; m_Data = new Hashtable((int)nLength); for (uint i = 0; i < nLength; i++) { CUniStruct <T> vrItem = new CUniStruct <T>(); uint nImported = vrItem.Import(pBuffer, nIndex); if (nImported == 0) { m_Data = null; throw(new Exception("2,index=" + i)); } m_Data[i] = vrItem; nIndex += nImported; } }catch (Exception e) { Logger.Trace("UniStructArray faild:" + e.Message); return(0); } return((uint)nIndex - _index); }
public uint ExportSize(T srcObj, bool bHavMag) { if (srcObj == null) { return(0); } Type t = srcObj.GetType(); FieldInfo[] fiarr = t.GetFields(); if (fiarr == null) { return(0); } uint nExportSize = 0; if (bHavMag) { nExportSize += 4; } CUniStruct <object> pUnkownObj = new CUniStruct <object>(); uint nCount = (uint)fiarr.GetLength(0); for (uint i = 0; i < nCount; i++) { object poValue = fiarr[i].GetValue(srcObj); Type itt = fiarr[i].FieldType; if ((poValue != null) && ((itt == typeof(UniSZ)) || (itt == typeof(UniDW)))) { UniVarItem puiValue = (UniVarItem)poValue; if (!puiValue.IsValid()) { continue; } } nExportSize += 4; if (poValue == null) { continue; } if (itt == typeof(UniSZ)) { UniSZ pValue = (UniSZ)poValue; if (!pValue.IsEmpty() && !pValue.IsLock()) { nExportSize += pValue.Size(); } } else if (itt == typeof(UniDW)) { UniDW pValue = (UniDW)poValue; if (!pValue.IsEmpty() && !pValue.IsLock()) { nExportSize += 4; } } else { nExportSize -= 4; uint nSize = pUnkownObj.ExportSize(poValue, false); nExportSize += nSize; } } return((uint)nExportSize); }
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); }
public uint Export(T srcObj, bool bHavMag, ref byte[] pOut, uint _nIndex) { if (srcObj == null || pOut == null || pOut.Length < _nIndex + 4) { return(0); } Type t = srcObj.GetType(); FieldInfo[] fiarr = t.GetFields(); if (fiarr == null) { return(0); } uint nIndex = _nIndex; if (bHavMag) { uint2byte((uint)UNISMAG.UNISTRUCT, ref pOut, nIndex); nIndex += 4; } CUniStruct <object> pUnkownObj = new CUniStruct <object>(); uint nCount = (uint)fiarr.GetLength(0); for (uint i = 0; i < nCount; i++) { object poValue = fiarr[i].GetValue(srcObj); Type itt = fiarr[i].FieldType; if ((poValue != null) && ((itt == typeof(UniSZ)) || (itt == typeof(UniDW)))) { UniVarItem puiValue = (UniVarItem)poValue; if (!puiValue.IsValid()) { continue; } } if (itt == typeof(UniSZ)) { if (pOut.Length < nIndex + 4) { return(0); } if (poValue != null) { UniSZ pValue = (UniSZ)poValue; uint2byte((uint)pValue.m_dwType, ref pOut, nIndex); nIndex += 4; if (!pValue.IsEmpty() && !pValue.IsLock()) { if (pOut.Length < nIndex + pValue.Size()) { return(0); } Array.Copy(pValue.Get(), 0, pOut, nIndex, pValue.Size()); nIndex += pValue.Size(); } } else { uint2byte((uint)UTFLAG.DEFUNISZTYPE, ref pOut, nIndex); nIndex += 4; } } else if (itt == typeof(UniDW)) { if (pOut.Length < nIndex + 4) { return(0); } if (poValue != null) { UniDW pValue = (UniDW)poValue; uint2byte((uint)pValue.m_dwType, ref pOut, nIndex); nIndex += 4; if (!pValue.IsEmpty() && !pValue.IsLock()) { if (pOut.Length < nIndex + 4) { return(0); } uint2byte((uint)pValue.m_dwValue, ref pOut, nIndex); nIndex += 4; } } else { uint2byte((uint)UTFLAG.DEFUNIDWTYPE, ref pOut, nIndex); nIndex += 4; } } else { uint nSize = pUnkownObj.Export(poValue, false, ref pOut, nIndex); nIndex += nSize; } } return(nIndex - _nIndex); }