public void Read(PPDPack.V2.PackReader reader) { foreach (var propertyInfo in GetType().GetProperties()) { if (!propertyInfo.CanWrite || propertyInfo.GetCustomAttributes(false).Any(a => a.GetType() == typeof(IgnoreAttribute))) { continue; } var streamReader = reader.Read(propertyInfo.Name); if (propertyInfo.PropertyType.IsArray) { var elementType = propertyInfo.PropertyType.GetElementType(); var arrayLength = ParseInt(streamReader); var array = Array.CreateInstance(elementType, arrayLength); for (int i = 0; i < arrayLength; i++) { array.SetValue(Read(elementType, streamReader), i); } propertyInfo.SetValue(this, array, null); } else { propertyInfo.SetValue(this, Read(propertyInfo.PropertyType, streamReader), null); } } }
private byte[] WorkImpl(byte[] bytes) { try { using (MemoryStream stream = new MemoryStream(bytes)) using (PPDPack.V2.PackReader reader = new PPDPack.V2.PackReader(stream)) { var dataType = (DataType)ParseInt(reader, "DataType"); var packable = PackableBase.Create(dataType); if (packable != null) { packable.Read(reader); OnDataReceived(packable); } byte[] restBytes = new byte[bytes.Length - reader.FileSize]; if (restBytes.Length > 0) { Array.Copy(bytes, reader.FileSize, restBytes, 0, restBytes.Length); } return(restBytes); } } catch { return(bytes); } }
protected int ParseInt(PPDPack.V2.PackReader reader, string keyName) { if (reader.Files.Any(f => f == keyName)) { using (PPDPackStreamReader packStreamReader = reader.Read(keyName)) { byte[] data = new byte[sizeof(int)]; packStreamReader.Read(data, 0, data.Length); var ret = BitConverter.ToInt32(data, 0); return(ret); } } return(0); }