Beispiel #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;
            }
        }
Beispiel #2
0
 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;
     }
 }
Beispiel #3
0
        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);
        }
Beispiel #4
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;
            }
        }
Beispiel #5
0
 private static DataRectangle CreateDataRectangle(SafeUnmanagedArray array, UnmanagedMemoryStream input, Int32 pitch)
 {
     return new DataRectangle(new IntPtr(array.DangerousGetHandle().ToInt64() + input.Position), pitch);
 }
Beispiel #6
0
 private static DataBox CreateDataBox(SafeUnmanagedArray array, UnmanagedMemoryStream input, Int32 rowPitch, Int32 depthPitch)
 {
     return new DataBox(new IntPtr(array.DangerousGetHandle().ToInt64() + input.Position), rowPitch, depthPitch);
 }
Beispiel #7
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);
            }
        }