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; } }
private async Task Pack(string root, string securityKey, string targetPath) { if (CancelEvent.IsSet()) return; using (MemoryStream buff = new MemoryStream(16 * 1024 * 1024)) { BinaryWriter bw = new BinaryWriter(buff); bw.Write((byte)PatchFormatVersion.V1); bw.Write((byte)FFXIIIGamePart.Part1); if (CancelEvent.IsSet()) return; await PackTexts(root, bw); if (CancelEvent.IsSet()) return; await PackImgb(root, bw); if (CancelEvent.IsSet()) return; buff.Flush(); int length = (int)buff.Position; buff.Position = 0; using (MemoryStream compressed = new MemoryStream(length + 4 + 256)) using (BinaryWriter cbw = new BinaryWriter(compressed)) { if (CancelEvent.IsSet()) return; cbw.Write(length); Position = 0; Maximum = length; ZLibHelper.Compress(buff, compressed, length, OnProgress); compressed.Position = 0; if (CancelEvent.IsSet()) return; Position = 0; Maximum = compressed.Length; using (CryptoProvider cryptoProvider = new CryptoProvider(securityKey, CancelEvent)) { cryptoProvider.Progress += OnProgress; using (FileStream output = File.Create(targetPath)) await cryptoProvider.Encrypt(compressed, output); } } } }