private async Task<SafeUnmanagedArray> Decompress() { _userName = ((MainWindow)this.GetRootElement()).GetUserName(); string securityKey = await ((MainWindow)this.GetRootElement()).GetSecurityKeyAsync(true); if (CancelEvent.IsSet()) return null; using (FileStream input = File.OpenRead(PatcherService.ArchiveFileName)) using (MemoryStream ms = new MemoryStream((int)input.Length)) { if (CancelEvent.IsSet()) return null; Maximum = input.Length; using (CryptoProvider cryptoProvider = new CryptoProvider(securityKey, CancelEvent)) { cryptoProvider.Progress += OnProgress; await cryptoProvider.Decrypt(input, ms); } if (CancelEvent.IsSet()) return null; Position = ms.Position = 0; BinaryReader br = new BinaryReader(ms); int uncompressedSize = br.ReadInt32(); byte[] buff = new byte[Math.Min(32 * 1024, uncompressedSize)]; if (CancelEvent.IsSet()) return null; SafeUnmanagedArray result = new SafeUnmanagedArray(uncompressedSize); try { if (CancelEvent.IsSet()) return null; Maximum = uncompressedSize; using (UnmanagedMemoryStream output = result.OpenStream(FileAccess.Write)) ZLibHelper.Uncompress(ms, output, uncompressedSize, buff, CancellationToken.None, OnProgress); } catch { result.SafeDispose(); throw; } return result; } }
public static void Read(this Stream input, SafeUnmanagedArray buffer, long offset, int length) { try { using (UnmanagedMemoryStream output = new UnmanagedMemoryStream(buffer, offset, length, FileAccess.Write)) { byte[] buff = new byte[Math.Min(32 * 1024, length)]; input.CopyToStream(output, length, buff); } } catch { buffer.Dispose(); throw; } }
public static SafeUnmanagedArray ReadBuff(this Stream input, int size) { SafeUnmanagedArray handle = new SafeUnmanagedArray(size); try { using (UnmanagedMemoryStream output = new UnmanagedMemoryStream(handle, 0, size, FileAccess.Write)) { byte[] buff = new byte[Math.Min(32 * 1024, size)]; input.CopyToStream(output, size, buff); } } catch { handle.Dispose(); throw; } return(handle); }
public void OnWritingCompleted(ArchiveEntry entry, MemoryStream ms, bool? compression) { ms.Position = 0; int compressedSize = 0; int uncompressedSize = (int)ms.Length; byte[] copyBuff = new byte[Math.Min(uncompressedSize, 32 * 1024)]; try { bool compress = uncompressedSize > 256 && (compression ?? entry.IsCompressed); if (compress) { using (SafeUnmanagedArray buff = new SafeUnmanagedArray(uncompressedSize + 256)) using (UnmanagedMemoryStream buffStream = buff.OpenStream(FileAccess.ReadWrite)) { compressedSize = ZLibHelper.Compress(ms, buffStream, uncompressedSize); if (uncompressedSize - compressedSize > 256) { using (Stream output = OpenOrAppendBinary(entry, compressedSize)) { buffStream.Position = 0; buffStream.CopyToStream(output, compressedSize, copyBuff); } return; } } } ms.Position = 0; compressedSize = uncompressedSize; using (Stream output = OpenOrAppendBinary(entry, uncompressedSize)) ms.CopyToStream(output, uncompressedSize, copyBuff); } finally { entry.Size = compressedSize; entry.UncompressedSize = uncompressedSize; } }
private static DataRectangle CreateDataRectangle(SafeUnmanagedArray array, UnmanagedMemoryStream input, Int32 pitch) { return new DataRectangle(new IntPtr(array.DangerousGetHandle().ToInt64() + input.Position), pitch); }
private static DataBox CreateDataBox(SafeUnmanagedArray array, UnmanagedMemoryStream input, Int32 rowPitch, Int32 depthPitch) { return new DataBox(new IntPtr(array.DangerousGetHandle().ToInt64() + input.Position), rowPitch, depthPitch); }
private static DxTexture ReadTextureCubeFromStream(GtexData gtex, Stream input) { Texture2DDescription descriptor = GetTextureCubeDescription(gtex); using (SafeUnmanagedArray array = new SafeUnmanagedArray(gtex.MipMapData.Sum(d => d.Length))) { DataRectangle[] rects = new DataRectangle[gtex.MipMapData.Length]; using (UnmanagedMemoryStream io = array.OpenStream(FileAccess.Write)) { byte[] buff = new byte[32 * 1024]; for (int index = 0; index < gtex.MipMapData.Length; index++) { GtexMipMapLocation mimMap = gtex.MipMapData[index]; Int32 pitch = GetPitch(descriptor, index); rects[index] = CreateDataRectangle(array, io, pitch); input.SetPosition(mimMap.Offset); input.CopyToStream(io, mimMap.Length, buff); } } Texture2D texture = new Texture2D(_device, descriptor, rects); // Workaround _textureCreatingWorkaround(_device.ImmediateContext, texture, ImageFileFormat.Dds); return new DxTexture(texture, descriptor); } }