Exemplo n.º 1
0
        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;
            }
        }
Exemplo n.º 2
0
        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;
            }
        }
Exemplo n.º 3
0
        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);
            }
        }