internal static bool IsDefined(CompressionFormat value)
 {
     return
         value == CompressionFormat.None
         || value == CompressionFormat.Deflate
         || value == CompressionFormat.GZip;
 }
 public static void Validate(CompressionFormat value)
 {
     if (!IsDefined(value))
     {
         throw FxTrace.Exception.AsError(new InvalidEnumArgumentException("value", (int)value, typeof(CompressionFormat)));
     }
 }
    public static void CompressionFormat_Property_Sets(CompressionFormat format)
    {
        BinaryMessageEncodingBindingElement bindingElement = new BinaryMessageEncodingBindingElement();
        bindingElement.CompressionFormat = format;
        Assert.Equal(format, bindingElement.CompressionFormat);

        // Note: invalid formats can be tested once we have a transport underneath, as it's the transport that determines 
        // whether or not the CompressionFormat is valid for it. 
    }
示例#4
0
        // Set up compression object for compression
        public Compression(Stream data, string filename, CompressionFormat format)
        {
            // Set up information
            Name       = null;
            Data       = data;
            Filename   = filename;
            Format     = format;

            // Initalize Compressor
            InitalizeCompressor();
        }
        public override void CopyFrom(ServiceModelExtensionElement from)
        {
            base.CopyFrom(from);

            BinaryMessageEncodingElement source = (BinaryMessageEncodingElement)from;
#pragma warning suppress 56506 //[....]; base.CopyFrom() checks for 'from' being null
            this.MaxSessionSize = source.MaxSessionSize;
            this.MaxReadPoolSize = source.MaxReadPoolSize;
            this.MaxWritePoolSize = source.MaxWritePoolSize;
            this.CompressionFormat = source.CompressionFormat;
        }
        internal static void CompressBuffer(ref ArraySegment<byte> buffer, BufferManager bufferManager, CompressionFormat compressionFormat)
        {
            using (BufferManagerOutputStream bufferedOutStream = new BufferManagerOutputStream(SR.MaxSentMessageSizeExceeded, 1024, int.MaxValue, bufferManager))
            {
                bufferedOutStream.Write(buffer.Array, 0, buffer.Offset);

                using (Stream ds = compressionFormat == CompressionFormat.GZip ?
                    (Stream)new GZipStream(bufferedOutStream, CompressionMode.Compress, true) :
                    (Stream)new DeflateStream(bufferedOutStream, CompressionMode.Compress, true))
                {
                    ds.Write(buffer.Array, buffer.Offset, buffer.Count);
                }

                int length = 0;
                byte[] compressedBytes = bufferedOutStream.ToArray(out length);
                bufferManager.ReturnBuffer(buffer.Array);
                buffer = new ArraySegment<byte>(compressedBytes, buffer.Offset, length - buffer.Offset);
            }
        }
        internal static void DecompressBuffer(ref ArraySegment<byte> buffer, BufferManager bufferManager, CompressionFormat compressionFormat, long maxReceivedMessageSize)
        {
            MemoryStream memoryStream = new MemoryStream(buffer.Array, buffer.Offset, buffer.Count);
            int maxDecompressedSize = (int)Math.Min(maxReceivedMessageSize, int.MaxValue);

            using (BufferManagerOutputStream bufferedOutStream = new BufferManagerOutputStream(SR.MaxReceivedMessageSizeExceeded, 1024, maxDecompressedSize, bufferManager))
            {
                bufferedOutStream.Write(buffer.Array, 0, buffer.Offset);

                byte[] tempBuffer = bufferManager.TakeBuffer(DecompressBlockSize);
                try
                {
                    using (Stream ds = compressionFormat == CompressionFormat.GZip ?
                        (Stream)new GZipStream(memoryStream, CompressionMode.Decompress) :
                        (Stream)new DeflateStream(memoryStream, CompressionMode.Decompress))
                    {
                        while (true)
                        {
                            int bytesRead = ds.Read(tempBuffer, 0, DecompressBlockSize);
                            if (bytesRead > 0)
                            {
                                bufferedOutStream.Write(tempBuffer, 0, bytesRead);
                            }
                            else
                            {
                                break;
                            }
                        }
                    }
                }
                finally
                {
                    bufferManager.ReturnBuffer(tempBuffer);
                }

                int length = 0;
                byte[] decompressedBytes = bufferedOutStream.ToArray(out length);
                bufferManager.ReturnBuffer(buffer.Array);
                buffer = new ArraySegment<byte>(decompressedBytes, buffer.Offset, length - buffer.Offset);
            }
        }
        public BinaryMessageEncoderFactory(MessageVersion messageVersion, int maxReadPoolSize, int maxWritePoolSize, int maxSessionSize,
            XmlDictionaryReaderQuotas readerQuotas, long maxReceivedMessageSize, BinaryVersion version, CompressionFormat compressionFormat)
        {
            this.messageVersion = messageVersion;
            this.maxReadPoolSize = maxReadPoolSize;
            this.maxWritePoolSize = maxWritePoolSize;
            this.maxSessionSize = maxSessionSize;
            this.thisLock = new object();
            this.onStreamedReaderClose = new OnXmlDictionaryReaderClose(ReturnStreamedReader);
            this.readerQuotas = new XmlDictionaryReaderQuotas();
            if (readerQuotas != null)
            {
                readerQuotas.CopyTo(this.readerQuotas);
            }

            this.bufferedReadReaderQuotas = EncoderHelpers.GetBufferedReadQuotas(this.readerQuotas);
            this.MaxReceivedMessageSize = maxReceivedMessageSize;

            this.binaryVersion = version;
            this.compressionFormat = compressionFormat;
            this.messageEncoder = new BinaryMessageEncoder(this, false, 0);
        }
示例#9
0
        public BinaryMessageEncoderFactory(MessageVersion messageVersion, int maxReadPoolSize, int maxWritePoolSize, int maxSessionSize,
            XmlDictionaryReaderQuotas readerQuotas, long maxReceivedMessageSize, BinaryVersion version, CompressionFormat compressionFormat)
        {
            _messageVersion = messageVersion;
            _maxReadPoolSize = maxReadPoolSize;
            _maxWritePoolSize = maxWritePoolSize;
            _maxSessionSize = maxSessionSize;
            _thisLock = new object();

            _readerQuotas = new XmlDictionaryReaderQuotas();
            if (readerQuotas != null)
            {
                readerQuotas.CopyTo(_readerQuotas);
            }

            _bufferedReadReaderQuotas = EncoderHelpers.GetBufferedReadQuotas(_readerQuotas);
            this.MaxReceivedMessageSize = maxReceivedMessageSize;

            _binaryVersion = version;
            _compressionFormat = compressionFormat;
            _messageEncoder = new BinaryMessageEncoder(this, false, 0);
        }
示例#10
0
        /// <summary>
        /// Creates texture data ready to be imported into the DATs from an external file.
        /// If format is not specified, either the incoming file's DDS format is used (DDS files),
        /// or the existing internal file's DDS format is used.
        /// </summary>
        /// <param name="internalPath"></param>
        /// <param name="externalPath"></param>
        /// <param name="texFormat"></param>
        /// <returns></returns>
        public async Task <byte[]> MakeTexData(string internalPath, string externalPath, XivTexFormat texFormat = XivTexFormat.INVALID)
        {
            // Ensure file exists.
            if (!File.Exists(externalPath))
            {
                throw new IOException($"Could not find file: {externalPath}");
            }

            var root = await XivCache.GetFirstRoot(internalPath);

            bool isDds = Path.GetExtension(externalPath).ToLower() == ".dds";

            var ddsContainer = new DDSContainer();

            try
            {
                // If no format was specified...
                if (texFormat == XivTexFormat.INVALID)
                {
                    if (isDds)
                    {
                        // If we're importing a DDS file, get the format from the incoming DDS file
                        using (var fs = new FileStream(externalPath, FileMode.Open))
                        {
                            using (var sr = new BinaryReader(fs))
                            {
                                texFormat = GetDDSTexFormat(sr);
                            }
                        }
                    }
                    else
                    {
                        // Otherwise use the current internal format.
                        var xivt = await _dat.GetType4Data(internalPath, false);

                        texFormat = xivt.TextureFormat;
                    }
                }

                // Check if the texture being imported has been imported before
                CompressionFormat compressionFormat = CompressionFormat.BGRA;

                switch (texFormat)
                {
                case XivTexFormat.DXT1:
                    compressionFormat = CompressionFormat.BC1a;
                    break;

                case XivTexFormat.DXT5:
                    compressionFormat = CompressionFormat.BC3;
                    break;

                case XivTexFormat.A8R8G8B8:
                    compressionFormat = CompressionFormat.BGRA;
                    break;

                default:
                    if (!isDds)
                    {
                        throw new Exception($"Format {texFormat} is not currently supported for BMP import\n\nPlease use the DDS import option instead.");
                    }
                    break;
                }

                if (!isDds)
                {
                    using (var surface = Surface.LoadFromFile(externalPath))
                    {
                        if (surface == null)
                        {
                            throw new FormatException($"Unsupported texture format");
                        }

                        surface.FlipVertically();

                        var maxMipCount = 1;
                        if (root != null)
                        {
                            // For things that have real roots (things that have actual models/aren't UI textures), we always want mipMaps, even if the existing texture only has one.
                            // (Ex. The Default Mat-Add textures)
                            maxMipCount = -1;
                        }

                        using (var compressor = new Compressor())
                        {
                            // UI/Paintings only have a single mipmap and will crash if more are generated, for everything else generate max levels
                            compressor.Input.SetMipmapGeneration(true, maxMipCount);
                            compressor.Input.SetData(surface);
                            compressor.Compression.Format = compressionFormat;
                            compressor.Compression.SetBGRAPixelFormat();

                            compressor.Process(out ddsContainer);
                        }
                    }
                }

                // If we're not a DDS, write the DDS to file temporarily.
                var ddsFilePath = externalPath;
                if (!isDds)
                {
                    var tempFile = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString() + ".dds");
                    ddsContainer.Write(tempFile, DDSFlags.None);
                    ddsFilePath = tempFile;
                }

                using (var br = new BinaryReader(File.OpenRead(ddsFilePath)))
                {
                    br.BaseStream.Seek(12, SeekOrigin.Begin);

                    var newHeight = br.ReadInt32();
                    var newWidth  = br.ReadInt32();
                    br.ReadBytes(8);
                    var newMipCount = br.ReadInt32();

                    if (newHeight % 2 != 0 || newWidth % 2 != 0)
                    {
                        throw new Exception("Resolution must be a multiple of 2");
                    }

                    br.BaseStream.Seek(80, SeekOrigin.Begin);

                    var textureFlags = br.ReadInt32();
                    var texType      = br.ReadInt32();

                    var uncompressedLength = (int)new FileInfo(ddsFilePath).Length - 128;
                    var newTex             = new List <byte>();

                    if (!internalPath.Contains(".atex"))
                    {
                        var DDSInfo = await DDS.ReadDDS(br, texFormat, newWidth, newHeight, newMipCount);

                        newTex.AddRange(_dat.MakeType4DatHeader(texFormat, DDSInfo.mipPartOffsets, DDSInfo.mipPartCounts, (int)uncompressedLength, newMipCount, newWidth, newHeight));
                        newTex.AddRange(MakeTextureInfoHeader(texFormat, newWidth, newHeight, newMipCount));
                        newTex.AddRange(DDSInfo.compressedDDS);

                        return(newTex.ToArray());
                    }
                    else
                    {
                        br.BaseStream.Seek(128, SeekOrigin.Begin);
                        newTex.AddRange(MakeTextureInfoHeader(texFormat, newWidth, newHeight, newMipCount));
                        newTex.AddRange(br.ReadBytes((int)uncompressedLength));
                        var data = await _dat.CreateType2Data(newTex.ToArray());

                        return(data);
                    }
                }
            }
            finally
            {
                ddsContainer.Dispose();
            }
        }
示例#11
0
 private void VerifyCompression(BindingContext context)
 {
     if (CompressionFormat != CompressionFormat.None)
     {
         ITransportCompressionSupport compressionSupport = context.GetInnerProperty <ITransportCompressionSupport>();
         if (compressionSupport == null || !compressionSupport.IsCompressionFormatSupported(CompressionFormat))
         {
             throw Fx.Exception.AsError(new NotSupportedException(SR.Format(
                                                                      SR.TransportDoesNotSupportCompression, CompressionFormat.ToString(),
                                                                      GetType().Name,
                                                                      CompressionFormat.None.ToString())));
         }
     }
 }
            public BinaryMessageEncoder(BinaryMessageEncoderFactory factory, bool isSession, int maxSessionSize)
            {
                this.factory = factory;
                this.isSession = isSession;
                this.maxSessionSize = maxSessionSize;
                this.remainingReaderSessionSize = maxSessionSize;
                this.normalContentType = isSession ? factory.binaryVersion.SessionContentType : factory.binaryVersion.ContentType;
                this.gzipCompressedContentType = isSession ? BinaryVersion.GZipVersion1.SessionContentType : BinaryVersion.GZipVersion1.ContentType;
                this.deflateCompressedContentType = isSession ? BinaryVersion.DeflateVersion1.SessionContentType : BinaryVersion.DeflateVersion1.ContentType;
                this.sessionCompressionFormat = this.factory.CompressionFormat;
                this.maxReceivedMessageSize = this.factory.MaxReceivedMessageSize;

                switch (this.factory.CompressionFormat)
                {
                    case CompressionFormat.Deflate:
                        this.contentType = this.deflateCompressedContentType;
                        break;
                    case CompressionFormat.GZip:
                        this.contentType = this.gzipCompressedContentType;
                        break;
                    default:
                        this.contentType = this.normalContentType;
                        break;
                }
            }
示例#13
0
 // Decompress a file with the specified compression format
 public static void Decompress(Stream source, Stream destination, CompressionFormat format)
 {
     Formats[format].Decompress(source, destination);
 }
示例#14
0
        private void listView_DoubleClick(object sender, EventArgs e)
        {
            int index = listView.SelectedIndices[0];

            if (openedArchives.Count > 1)
            {
                // Do we want to go to the parent archive?
                if (index == 0)
                {
                    openedArchives.Pop();
                    openedArchiveNames.RemoveAt(openedArchiveNames.Count - 1);

                    Populate(openedArchives.Peek());

                    return;
                }
                else
                {
                    // Subtract the index by 1 so we're referring to the correct files
                    index--;
                }
            }

            ArchiveEntry entry     = openedArchives.Peek().Archive.Entries[index];
            Stream       entryData = entry.Open();

            // Let's determine first if it is an archive or a texture
            ArchiveFormat archiveFormat;
            TextureFormat textureFormat;

            archiveFormat = Archive.GetFormat(entryData, entry.Name);
            if (archiveFormat != ArchiveFormat.Unknown)
            {
                // This is an archive. Let's open it.
                OpenArchive(entryData, entry.Name, archiveFormat);

                return;
            }

            textureFormat = Texture.GetFormat(entryData, entry.Name);
            if (textureFormat != TextureFormat.Unknown)
            {
                // This is a texture. Let's attempt to open it up in the texture viewer
                OpenTexture(entryData, entry.Name, textureFormat);

                return;
            }

            // It's not an archive or a texture. Maybe it's compressed?
            CompressionFormat compressionFormat = Compression.GetFormat(entryData, entry.Name);

            if (compressionFormat != CompressionFormat.Unknown)
            {
                // The file is compressed! Let's decompress it and then try to determine if it is an archive or a texture
                MemoryStream decompressedData = new MemoryStream();
                Compression.Decompress(entryData, decompressedData, compressionFormat);
                decompressedData.Position = 0;

                // Now with this decompressed data, let's determine if it is an archive or a texture
                archiveFormat = Archive.GetFormat(decompressedData, entry.Name);
                if (archiveFormat != ArchiveFormat.Unknown)
                {
                    // This is an archive. Let's open it.
                    OpenArchive(decompressedData, entry.Name, archiveFormat);

                    return;
                }

                textureFormat = Texture.GetFormat(decompressedData, entry.Name);
                if (textureFormat != TextureFormat.Unknown)
                {
                    // This is a texture. Let's attempt to open it up in the texture viewer
                    OpenTexture(decompressedData, entry.Name, textureFormat);

                    return;
                }
            }
        }
示例#15
0
		/// <summary>
		/// Create a compressed stream.
		/// The "7za" or "7z" program must be in the path for this to work.
		/// </summary>
		/// <param name="fileName">name of the file to be wrapped for writing</param>
		/// <param name="type">type of compression to use, such as "7z" or "zip"</param>
		public Z7zEncodeStream(string fileName, CompressionFormat type)
			: base(
			z7zCmdSet,
			string.Format(z7zCmdCompressToFileArgs, fileName, "", MapType(type)),
			true,
			false)
		{
			// shouldn't we test for problems and throw exceptions? At least for exe existance? ***
		}
示例#16
0
		private static string MapType(CompressionFormat t)
		{
			return MapType(t, 5);
		}
示例#17
0
		/// <summary>
		/// Decompresses the given data stream from its source ZIP or GZIP format.
		/// </summary>
		/// <param name="dataBytes"></param>
		/// <returns></returns>
		internal static byte[] Inflate(Stream dataStream, CompressionFormat format)
		{


			byte[] outputBytes = null;

			switch (format)
			{
			case CompressionFormat.Zlib:

				try 
				{
					var zipInputStream = new ZipInputStream (dataStream);

					if (zipInputStream.CanDecompressEntry) 
					{
						MemoryStream zipoutStream = new MemoryStream ();
						zipInputStream.CopyTo (zipoutStream);
						outputBytes = zipoutStream.ToArray ();
					}
					else
					{

						dataStream.Seek(0, SeekOrigin.Begin);
						var inZInputStream = new ZInputStream(dataStream);

                        var outMemoryStream = new MemoryStream();
						outputBytes = new byte[inZInputStream.BaseStream.Length];

						while (true)
                        {
							int bytesRead = inZInputStream.Read(outputBytes, 0, outputBytes.Length);
                            if (bytesRead == 0)
                                break;
							outMemoryStream.Write(outputBytes, 0, bytesRead);
                        }

					}
				}
				catch (Exception exc)
				{
					CCLog.Log("Error decompressing image data: " + exc.Message);
				}
				break;
			case CompressionFormat.Gzip:

				try
                {
                    #if !WINDOWS_PHONE
                    var gzipInputStream = new GZipInputStream(dataStream, CompressionMode.Decompress);
                    #else
                    var gzipInputStream = new GZipInputStream(dataStream);
					#endif

					MemoryStream zipoutStream = new MemoryStream ();
					gzipInputStream.CopyTo (zipoutStream);
					outputBytes = zipoutStream.ToArray ();
				} catch (Exception exc) {
					CCLog.Log ("Error decompressing image data: " + exc.Message);
				}
				break;
			}

			return outputBytes;
		}
示例#18
0
        public PatchCreator SetCompressionFormat(CompressionFormat repairPatch, CompressionFormat installerPatch, CompressionFormat incrementalPatch)
        {
            if (!IsRunning)
            {
                compressionFormatRepairPatch      = repairPatch;
                compressionFormatInstallerPatch   = installerPatch;
                compressionFormatIncrementalPatch = incrementalPatch;
            }

            return(this);
        }
 internal static Stream GetCompressStream(Stream uncompressedStream, CompressionFormat compressionFormat)
 {
     return(compressionFormat == CompressionFormat.GZip ?
            (Stream) new GZipStream(uncompressedStream, CompressionMode.Compress, true) :
            (Stream) new DeflateStream(uncompressedStream, CompressionMode.Compress, true));
 }
 internal static Stream GetDecompressStream(Stream compressedStream, CompressionFormat compressionFormat)
 {
     return(compressionFormat == CompressionFormat.GZip ?
            (Stream) new GZipStream(compressedStream, CompressionMode.Decompress, false) :
            (Stream) new DeflateStream(compressedStream, CompressionMode.Decompress, false));
 }
        internal static void CompressBuffer(ref ArraySegment <byte> buffer, BufferManager bufferManager, CompressionFormat compressionFormat)
        {
            using (BufferManagerOutputStream bufferedOutStream = new BufferManagerOutputStream(SR.MaxSentMessageSizeExceeded, 1024, int.MaxValue, bufferManager))
            {
                bufferedOutStream.Write(buffer.Array, 0, buffer.Offset);

                using (Stream ds = compressionFormat == CompressionFormat.GZip ?
                                   (Stream) new GZipStream(bufferedOutStream, CompressionMode.Compress, true) :
                                   (Stream) new DeflateStream(bufferedOutStream, CompressionMode.Compress, true))
                {
                    ds.Write(buffer.Array, buffer.Offset, buffer.Count);
                }

                byte[] compressedBytes = bufferedOutStream.ToArray(out int length);
                bufferManager.ReturnBuffer(buffer.Array);
                buffer = new ArraySegment <byte>(compressedBytes, buffer.Offset, length - buffer.Offset);
            }
        }
        internal static void DecompressBuffer(ref ArraySegment <byte> buffer, BufferManager bufferManager, CompressionFormat compressionFormat, long maxReceivedMessageSize)
        {
            MemoryStream memoryStream        = new MemoryStream(buffer.Array, buffer.Offset, buffer.Count);
            int          maxDecompressedSize = (int)Math.Min(maxReceivedMessageSize, int.MaxValue);

            using (BufferManagerOutputStream bufferedOutStream = new BufferManagerOutputStream(SR.MaxReceivedMessageSizeExceeded, 1024, maxDecompressedSize, bufferManager))
            {
                bufferedOutStream.Write(buffer.Array, 0, buffer.Offset);

                byte[] tempBuffer = bufferManager.TakeBuffer(DecompressBlockSize);
                try
                {
                    using (Stream ds = compressionFormat == CompressionFormat.GZip ?
                                       (Stream) new GZipStream(memoryStream, CompressionMode.Decompress) :
                                       (Stream) new DeflateStream(memoryStream, CompressionMode.Decompress))
                    {
                        while (true)
                        {
                            int bytesRead = ds.Read(tempBuffer, 0, DecompressBlockSize);
                            if (bytesRead > 0)
                            {
                                bufferedOutStream.Write(tempBuffer, 0, bytesRead);
                            }
                            else
                            {
                                break;
                            }
                        }
                    }
                }
                finally
                {
                    bufferManager.ReturnBuffer(tempBuffer);
                }

                byte[] decompressedBytes = bufferedOutStream.ToArray(out int length);
                bufferManager.ReturnBuffer(buffer.Array);
                buffer = new ArraySegment <byte>(decompressedBytes, buffer.Offset, length - buffer.Offset);
            }
        }
示例#23
0
        /// <exception cref = "DirectoryNotFoundException">Root path does not exist</exception>
        /// <exception cref = "UnauthorizedAccessException">A path needs admin priviledges to write</exception>
        /// <exception cref = "IOException">Output path is not empty</exception>
        /// <exception cref = "ArgumentException">An argument is empty</exception>
        /// <exception cref = "FormatException">Version is invalid</exception>
        public PatchCreator(string rootPath, string outputPath, string projectName, VersionCode version)
        {
            rootPath    = rootPath.Trim();
            outputPath  = outputPath.Trim();
            projectName = projectName.Trim();

            if (string.IsNullOrEmpty(rootPath))
            {
                throw new ArgumentException(Localization.Get(StringId.E_XCanNotBeEmpty, "'rootPath'"));
            }

            if (string.IsNullOrEmpty(outputPath))
            {
                throw new ArgumentException(Localization.Get(StringId.E_XCanNotBeEmpty, "'outputPath'"));
            }

            if (string.IsNullOrEmpty(projectName))
            {
                throw new ArgumentException(Localization.Get(StringId.E_XCanNotBeEmpty, "'projectName'"));
            }

            if (!version.IsValid)
            {
                throw new FormatException(Localization.Get(StringId.E_VersionCodeXIsInvalid, version));
            }

            if (!Directory.Exists(rootPath))
            {
                throw new DirectoryNotFoundException(Localization.Get(StringId.E_XDoesNotExist, rootPath));
            }

            if (!PatchUtils.CheckWriteAccessToFolder(rootPath))
            {
                throw new UnauthorizedAccessException(Localization.Get(StringId.E_AccessToXIsForbiddenRunInAdminMode, rootPath));
            }

            if (!PatchUtils.CheckWriteAccessToFolder(outputPath))
            {
                throw new UnauthorizedAccessException(Localization.Get(StringId.E_AccessToXIsForbiddenRunInAdminMode, outputPath));
            }

            if (Directory.Exists(outputPath))
            {
                if (Directory.GetFileSystemEntries(outputPath).Length > 0)
                {
                    throw new IOException(Localization.Get(StringId.E_DirectoryXIsNotEmpty, outputPath));
                }
            }

            Localization.Get(StringId.Done);               // Force the localization system to be initialized with the current culture/language

            generateRepairPatch    = true;
            generateInstallerPatch = true;

            previousVersionRoot = null;
            diffQuality         = 0;

            this.previousVersion = null;
            this.version         = version;

            this.rootPath    = PatchUtils.GetPathWithTrailingSeparatorChar(rootPath);
            this.outputPath  = PatchUtils.GetPathWithTrailingSeparatorChar(outputPath);
            this.projectName = projectName;

            repairPatchOutputPath      = this.outputPath + PatchParameters.REPAIR_PATCH_DIRECTORY + Path.DirectorySeparatorChar;
            installerPatchOutputPath   = this.outputPath + PatchParameters.INSTALLER_PATCH_DIRECTORY + Path.DirectorySeparatorChar;
            incrementalPatchOutputPath = this.outputPath + PatchParameters.INCREMENTAL_PATCH_DIRECTORY + Path.DirectorySeparatorChar;
            incrementalPatchTempPath   = this.outputPath + "Temp" + Path.DirectorySeparatorChar;

            ignoredPaths      = new HashSet <string>();
            ignoredPathsRegex = new List <Regex>()
            {
                PatchUtils.WildcardToRegex("*" + PatchParameters.VERSION_HOLDER_FILENAME_POSTFIX), // Ignore any version holder files
                PatchUtils.WildcardToRegex("*" + PatchParameters.LOG_FILE_NAME)                    // Or log files
            };

            compressionFormatRepairPatch      = CompressionFormat.LZMA;
            compressionFormatInstallerPatch   = CompressionFormat.LZMA;
            compressionFormatIncrementalPatch = CompressionFormat.LZMA;

            baseDownloadURL     = "";
            maintenanceCheckURL = "";

            logs = new Queue <string>();

            cancel     = false;
            silentMode = false;

            IsRunning = false;
            Result    = PatchResult.Failed;
        }
示例#24
0
 public WcfCompressMessageEncodingBindingElement(MessageEncodingBindingElement messageEncoderBindingElement, CompressionFormat compressionFormat)
 {
     InnerMessageEncodingBindingElement = messageEncoderBindingElement;
     CompressionFormat = compressionFormat;
 }
示例#25
0
        private void Run(Settings settings, ProgressDialog dialog)
        {
            for (int i = 0; i < fileList.Count; i++)
            {
                string file = fileList[i];

                // Report progress. If we only have one file to process, no need to display (x of n).
                if (fileList.Count == 1)
                {
                    dialog.ReportProgress(i * 100 / fileList.Count, String.Format("Processing {0}", Path.GetFileName(file)));
                }
                else
                {
                    dialog.ReportProgress(i * 100 / fileList.Count, String.Format("Processing {0} ({1:N0} of {2:N0})", Path.GetFileName(file), i + 1, fileList.Count));
                }

                // Let's open the file.
                // But, we're going to do this in a try catch in case any errors happen.
                try
                {
                    TextureFormat format;
                    MemoryStream  textureData = new MemoryStream();

                    using (FileStream inStream = File.OpenRead(file))
                    {
                        // Set source to inStream
                        // The reason we do it like this is because source does not always equal inStream.
                        // You'll see why very soon.
                        Stream source = inStream;

                        // Get the format of the texture
                        format = Texture.GetFormat(source, Path.GetFileName(file));
                        if (format == TextureFormat.Unknown)
                        {
                            // Maybe it's compressed? Let's check.
                            // But first, we need to make sure we want to check
                            if (settings.DecodeCompressedTextures)
                            {
                                // Get the compression format, if it is compressed that is.
                                CompressionFormat compressionFormat = Compression.GetFormat(source, Path.GetFileName(file));
                                if (compressionFormat != CompressionFormat.Unknown)
                                {
                                    // Ok, it appears to be compressed. Let's decompress it, and then check the format again
                                    source = new MemoryStream();
                                    Compression.Decompress(inStream, source, compressionFormat);

                                    source.Position = 0;
                                    format          = Texture.GetFormat(source, Path.GetFileName(file));
                                }
                            }

                            // If we still don't know what the texture format is, just skip the file.
                            if (format == TextureFormat.Unknown)
                            {
                                continue;
                            }
                        }

                        // Alright, let's decode the texture now
                        TextureBase texture = Texture.GetModule(format);
                        try
                        {
                            texture.Read(source, textureData, (int)source.Length);
                            //Texture.Read(source, textureData, (int)source.Length, format);
                        }
                        catch (TextureNeedsPaletteException)
                        {
                            // It appears that we need to load an external palette.
                            // Let's get the filename for this palette file, see if it exists, and load it in
                            string paletteName = Path.Combine(Path.GetDirectoryName(file), Path.GetFileNameWithoutExtension(file)) + Texture.GetModule(format).PaletteFileExtension;

                            if (!File.Exists(paletteName))
                            {
                                // If the palette file doesn't exist, just skip over this texture.
                                continue;
                            }

                            source.Position = 0;                  // We need to reset the position
                            textureData     = new MemoryStream(); // Just incase some data was written
                            using (FileStream paletteData = File.OpenRead(paletteName))
                            {
                                texture.PaletteStream = paletteData;
                                texture.PaletteLength = (int)paletteData.Length;
                                texture.Read(source, textureData, (int)source.Length);
                            }

                            // Delete the palette file if the user chose to delete the source texture
                            if (settings.DeleteSource)
                            {
                                File.Delete(paletteName);
                            }
                        }
                    }

                    // Get the output path and create it if it does not exist.
                    string outPath;
                    if (settings.OutputToSourceDirectory)
                    {
                        outPath = Path.GetDirectoryName(file);
                    }
                    else
                    {
                        outPath = Path.Combine(Path.GetDirectoryName(file), "Decoded Textures");
                    }

                    if (!Directory.Exists(outPath))
                    {
                        Directory.CreateDirectory(outPath);
                    }

                    // Time to write out the file
                    using (FileStream destination = File.Create(Path.Combine(outPath, Path.GetFileNameWithoutExtension(file) + ".png")))
                    {
                        textureData.WriteTo(destination);
                    }

                    // Delete the source if the user chose to
                    if (settings.DeleteSource)
                    {
                        File.Delete(file);
                    }
                }
                catch
                {
                    // Meh, just ignore the error.
                }
            }
        }
示例#26
0
		/// <summary>
		/// Create a stream for a particular file in a compressed archive.
		/// The "7za" or "7z" program must be in the path for this to work.
		/// </summary>
		/// <remarks>
		/// The compression format will be based on the file extension. The 7z
		/// format will be used for unknown extentions.
		/// </remarks>
		/// <param name="archiveName">name of the archive to be wrapped for writing</param>
		/// <param name="fileName">name of the file to write in the archive</param>
		/// <param name="type">type of compression to use, such as "7z" or "zip"</param>
		/// <param name="compressionLevel">level of compression to use, from 1 (low) to 9 (high)</param>
		public Z7zEncodeStream(string archiveName, string fileName, CompressionFormat type, int compressionLevel)
			: base(
			z7zCmdSet,
			string.Format(z7zCmdCompressToFileArgs, archiveName, fileName == null ? "" : fileName, MapType(type, compressionLevel)),
			true,
			false)
		{
			// shouldn't we test for problems and throw exceptions? At least for exe existance? ***
		}
示例#27
0
 public static void SetCompressedState(SafeFileHandle handle, CompressionFormat format)
 {
     if (handle == null)
     {
         throw new ArgumentNullException("handle");
     }
     if (handle.IsClosed || handle.IsInvalid)
     {
         throw new ArgumentException("handle is closed or invalid");
     }
     IntPtr ptr = Marshal.AllocHGlobal(2);
     try
     {
         uint num;
         Marshal.WriteInt16(ptr, (short) format);
         if (!Microsoft.Win32.IOCTL.IOCTL.DeviceIoControl(handle, FSCTL.FSCTL_SET_COMPRESSION, ptr, 2, IntPtr.Zero, 0, out num, IntPtr.Zero))
         {
             throw IoHelper.GetIOException();
         }
     }
     finally
     {
         Marshal.FreeHGlobal(ptr);
     }
 }
示例#28
0
		private static string MapType(CompressionFormat t, int compressionLevel)
		{
			//if (!Enum.IsDefined(typeof(CompressionFormat), t))  return "";
			switch (t)
			{
				case CompressionFormat.Z7z:
					return "-t7z -ms=off -mx=" + Math.Min(9, Math.Max(0, compressionLevel));

				case CompressionFormat.Zip:
					return "-tzip -mx=" + Math.Min(9, Math.Max(0, compressionLevel));

				case CompressionFormat.Gzip:
					if (compressionLevel <= 5)
					{
						// This makes it much bigger, but it is also twice as
						// fast as the default:
						return "-tgzip -mfb=3 -mx=" + Math.Min(9, Math.Max(1, compressionLevel));
					}
					else
					{
						return "-tgzip -mx=" + Math.Min(9, Math.Max(1, compressionLevel));
					}

				case CompressionFormat.Bzip2:
					return "-tbzip2 -mx=" + Math.Min(9, Math.Max(1, compressionLevel));

				case CompressionFormat.Tar:
					return "-ttar";

				case CompressionFormat.Unspecified:
					return "-mx=" + Math.Min(9, Math.Max(1, compressionLevel));

				default:
					return "-ms=off -mx=" + Math.Min(9, Math.Max(0, compressionLevel));
			}
		}
示例#29
0
 public static void SetCompressedState(string fileName, CompressionFormat format)
 {
     if (fileName == null)
     {
         throw new ArgumentNullException("fileName");
     }
     if (fileName == string.Empty)
     {
         throw new ArgumentException("fileName is empty");
     }
     using (FileStream stream = System.IO.File.Open(fileName, FileMode.Open, FileAccess.ReadWrite, FileShare.None))
     {
         SetCompressedState(stream.SafeFileHandle, format);
     }
 }
示例#30
0
 // Returns the module for this compression format.
 public static CompressionBase GetModule(CompressionFormat format)
 {
     return Formats[format];
 }
示例#31
0
		/// <summary>
		/// Decompresses the given data stream from its source ZIP or GZIP format.
		/// </summary>
		/// <param name="dataBytes"></param>
		/// <returns></returns>
        internal static byte[] Inflate (Stream dataStream, CompressionFormat format)
        {


            byte[] outputBytes = null;

            switch (format) 
            {
            case CompressionFormat.Zlib:

                try {
                    try {
                        using (var deflateStream = new ZlibStream (dataStream, MonoGame.Utilities.CompressionMode.Decompress)) 
                        {
                            int length = (int)deflateStream.BufferSize;
                            if (length == 0)
                                return new byte[0];

                            outputBytes = new byte[length];
                            deflateStream.Read(outputBytes, 0, length);
                        }
                    } 
                    catch (Exception exc) 
                    {
                        CCLog.Log ("Error decompressing image data: " + exc.Message);
                    }

                } 
                catch (Exception exc) 
                {
                    CCLog.Log ("Error decompressing image data: " + exc.Message);
                }
                break;
            case CompressionFormat.Gzip:

                try 
                {
#if !WINDOWS_PHONE
                    using (var gzipInputStream = new GZipInputStream(dataStream, System.IO.Compression.CompressionMode.Decompress))
#else
                    using (var gzipInputStream = new GZipInputStream(dataStream))
#endif
                    {
                        int length = (int)gzipInputStream.BaseStream.Length;
                        if (length == 0)
                            return new byte[0];

                        outputBytes = new byte[length];
                        gzipInputStream.Read(outputBytes, 0, length);
                    }
                } 
                catch (Exception exc) 
                {
                    CCLog.Log ("Error decompressing image data: " + exc.Message);
                }
                break;
            }

            return outputBytes;
        }
示例#32
0
 // Compress a file with the specified compression format
 public static void Compress(Stream source, Stream destination, CompressionFormat format)
 {
     Formats[format].Compress(source, destination);
 }
 internal static Stream GetDecompressStream(Stream compressedStream, CompressionFormat compressionFormat)
 {
     return compressionFormat == CompressionFormat.GZip ?
             (Stream)new GZipStream(compressedStream, CompressionMode.Decompress, false) :
             (Stream)new DeflateStream(compressedStream, CompressionMode.Decompress, false);
 }
示例#34
0
            public BinaryMessageEncoder(BinaryMessageEncoderFactory factory, bool isSession, int maxSessionSize)
            {
                _factory = factory;
                _isSession = isSession;
                _maxSessionSize = maxSessionSize;
                _remainingReaderSessionSize = maxSessionSize;
                _normalContentType = isSession ? factory._binaryVersion.SessionContentType : factory._binaryVersion.ContentType;
                _gzipCompressedContentType = isSession ? BinaryVersion.GZipVersion1.SessionContentType : BinaryVersion.GZipVersion1.ContentType;
                _deflateCompressedContentType = isSession ? BinaryVersion.DeflateVersion1.SessionContentType : BinaryVersion.DeflateVersion1.ContentType;
                _sessionCompressionFormat = _factory.CompressionFormat;
                _maxReceivedMessageSize = _factory.MaxReceivedMessageSize;

                switch (_factory.CompressionFormat)
                {
                    case CompressionFormat.Deflate:
                        _contentType = _deflateCompressedContentType;
                        break;
                    case CompressionFormat.GZip:
                        _contentType = _gzipCompressedContentType;
                        break;
                    default:
                        _contentType = _normalContentType;
                        break;
                }
            }
示例#35
0
 public static void CompressFolder(string inFolder, string outFile, CompressionFormat format)
 {
     CompressFolder(inFolder, outFile, format, new List <Regex>(0));
 }
示例#36
0
 public void SetSessionContentType(string contentType)
 {
     if (base.IsContentTypeSupported(contentType, _gzipCompressedContentType, _gzipCompressedContentType))
     {
         _sessionCompressionFormat = CompressionFormat.GZip;
     }
     else if (base.IsContentTypeSupported(contentType, _deflateCompressedContentType, _deflateCompressedContentType))
     {
         _sessionCompressionFormat = CompressionFormat.Deflate;
     }
     else
     {
         _sessionCompressionFormat = CompressionFormat.None;
     }
 }
示例#37
0
 internal TextureFormat(SurfaceFormat surfaceFormat, CompressionFormat compressionFormat)
 {
     SurfaceFormat     = surfaceFormat;
     CompressionFormat = compressionFormat;
 }
        private void DrawCreateTab()
        {
            c_RootPath   = PathField("Root path: ", c_RootPath, true);
            c_PrevRoot   = PathField("Previous version path: ", c_PrevRoot, true);
            c_OutputPath = PathField("Output path: ", c_OutputPath, true);

            c_Name    = EditorGUILayout.TextField("Project name: ", c_Name);
            c_Version = EditorGUILayout.TextField("Project version: ", c_Version);

            c_CreateRepair    = EditorGUILayout.Toggle("Create repair patch: ", c_CreateRepair);
            c_CreateInstaller = EditorGUILayout.Toggle("Create installer patch: ", c_CreateInstaller);

            c_RepairComp      = (CompressionFormat)EditorGUILayout.EnumPopup("Repair patch compression: ", c_RepairComp);
            c_IncrementalComp = (CompressionFormat)EditorGUILayout.EnumPopup("Incremental patch compression: ", c_IncrementalComp);
            c_InstallerComp   = (CompressionFormat)EditorGUILayout.EnumPopup("Installer patch compression: ", c_InstallerComp);

            GUILayout.Label("Ignored paths (one path per line): ");
            c_IgnoredPaths = EditorGUILayout.TextArea(c_IgnoredPaths);

            GUILayout.Space(10f);

            if (GUILayout.Button("Create Patch", GUILayout.Height(35f)))
            {
                c_RootPath     = c_RootPath.Trim();
                c_PrevRoot     = c_PrevRoot.Trim();
                c_OutputPath   = c_OutputPath.Trim();
                c_IgnoredPaths = c_IgnoredPaths.Trim();
                c_Name         = c_Name.Trim();
                c_Version      = c_Version.Trim();

                if (c_RootPath.Length == 0 || c_OutputPath.Length == 0 || c_Name.Length == 0 || c_Version.Length == 0)
                {
                    return;
                }

                patchCreator = new PatchCreator(c_RootPath, c_OutputPath, c_Name, c_Version);
                patchCreator.CreateIncrementalPatch(c_PrevRoot.Length > 0, c_PrevRoot).CreateRepairPatch(c_CreateRepair).CreateInstallerPatch(c_CreateInstaller).
                SetCompressionFormat(c_RepairComp, c_InstallerComp, c_IncrementalComp);

                if (c_IgnoredPaths.Length > 0)
                {
                    patchCreator.AddIgnoredPaths(c_IgnoredPaths.Replace("\r", "").Split('\n'));
                }

                if (patchCreator.Run())
                {
                    Debug.Log("<b>Patch creator started</b>");

                    EditorApplication.update -= OnUpdate;
                    EditorApplication.update += OnUpdate;
                }
                else
                {
                    Debug.LogWarning("<b>Couldn't start patch creator. Maybe it is already running?</b>");
                }
            }

            if (GUILayout.Button("Generate Console Command", GUILayout.Height(25f)))
            {
                string command = string.Format("Patcher create -root=\"{0}\" -out=\"{1}\" -name=\"{2}\" -version=\"{3}\" -compressionRepair=\"{4}\" -compressionIncremental=\"{5}\" -compressionInstaller=\"{6}\"", c_RootPath, c_OutputPath, c_Name, c_Version, c_RepairComp, c_IncrementalComp, c_InstallerComp);
                if (c_PrevRoot.Length > 0)
                {
                    command += string.Concat(" -prevRoot=\"", c_PrevRoot, "\"");
                }
                if (c_IgnoredPaths.Length > 0)
                {
                    command += string.Concat(" -ignoredPaths=\"", CONSOLE_COMMAND_IGNORED_PATHS_HOLDER, "\"");
                }
                if (!c_CreateRepair)
                {
                    command += " -dontCreateRepairPatch";
                }
                if (!c_CreateInstaller)
                {
                    command += " -dontCreateInstallerPatch";
                }

                Debug.Log(command);

                if (c_IgnoredPaths.Length > 0)
                {
                    Debug.Log(string.Concat("You have to insert the following ignored path(s) to \"", CONSOLE_COMMAND_IGNORED_PATHS_HOLDER, "\":\n", c_IgnoredPaths));
                }
            }
        }
示例#39
0
 public bool IsCompressionFormatSupported(CompressionFormat compressionFormat)
 {
     return(true);
 }
示例#40
0
 // Returns the module for this compression format.
 public static CompressionBase GetModule(CompressionFormat format)
 {
     return(Formats[format]);
 }
示例#41
0
 private static long Decompress(MemoryStream inputStream, MemoryStream output, CompressionFormat format)
 {
     if (!format.Supports(inputStream, inputStream.Length))
         return -1;
     try
     {
         return format.Decompress(inputStream, inputStream.Length, output);
     }
     catch (TooMuchInputException e)
     {
         Console.WriteLine(e.Message);
         return output.Length;
     }
     catch (Exception e)
     {
         Console.WriteLine("Could not decompress using the " + format.ShortFormatString + " format; " + e.Message);
         return -1;
     }
 }
 internal static Stream GetCompressStream(Stream uncompressedStream, CompressionFormat compressionFormat)
 {
     return compressionFormat == CompressionFormat.GZip ?
             (Stream)new GZipStream(uncompressedStream, CompressionMode.Compress, true) :
             (Stream)new DeflateStream(uncompressedStream, CompressionMode.Compress, true);
 }
示例#43
0
        public static byte[] Encode(byte[] data, int width, int height, TextureFormat format, int quality = 5)
        {
            switch (format)
            {
            case TextureFormat.RGB9e5Float:     //pls don't use (what is this?)
                return(null);

            //crunch-unity
            //case TextureFormat.DXT1Crunched:
            //case TextureFormat.DXT5Crunched:
            //case TextureFormat.ETC_RGB4Crunched:
            //case TextureFormat.ETC2_RGBA8Crunched:
            //{
            //    byte[] dest = new byte[data.Length]; //just to be safe, buf is same size as original
            //    uint size = 0;
            //    unsafe
            //    {
            //        fixed (byte* dataPtr = data)
            //        fixed (byte* destPtr = dest)
            //        {
            //            IntPtr dataIntPtr = (IntPtr)dataPtr;
            //            IntPtr destIntPtr = (IntPtr)destPtr;
            //            size = PInvoke.EncodeByCrunch(dataIntPtr, destIntPtr, (int)format, quality, (uint)width, (uint)height);
            //        }
            //    }
            //    if (size > 0)
            //    {
            //        byte[] resizedDest = new byte[size];
            //        Buffer.BlockCopy(dest, 0, resizedDest, 0, (int)size);
            //        dest = null;
            //        return resizedDest;
            //    }
            //    else
            //    {
            //        dest = null;
            //        return null;
            //    }
            //}
            //pvrtexlib
            case TextureFormat.ARGB32:
            case TextureFormat.BGRA32New:
            case TextureFormat.RGBA32:
            case TextureFormat.RGB24:
            case TextureFormat.ARGB4444:
            case TextureFormat.RGBA4444:
            case TextureFormat.RGB565:
            case TextureFormat.Alpha8:
            case TextureFormat.R8:
            case TextureFormat.R16:
            case TextureFormat.RG16:
            case TextureFormat.RHalf:
            case TextureFormat.RGHalf:
            case TextureFormat.RGBAHalf:
            case TextureFormat.RFloat:
            case TextureFormat.RGFloat:
            case TextureFormat.RGBAFloat:
            /////////////////////////////////
            case TextureFormat.YUV2:     //looks like this should be YUY2 and the api has a typo
            case TextureFormat.EAC_R:
            case TextureFormat.EAC_R_SIGNED:
            case TextureFormat.EAC_RG:
            case TextureFormat.EAC_RG_SIGNED:
            case TextureFormat.ETC_RGB4_3DS:
            case TextureFormat.ETC_RGBA8_3DS:
            case TextureFormat.ETC2_RGB4:
            case TextureFormat.ETC2_RGBA1:
            case TextureFormat.ETC2_RGBA8:
            case TextureFormat.PVRTC_RGB2:
            case TextureFormat.PVRTC_RGBA2:
            case TextureFormat.PVRTC_RGB4:
            case TextureFormat.PVRTC_RGBA4:
            case TextureFormat.ASTC_RGB_4x4:
            case TextureFormat.ASTC_RGB_5x5:
            case TextureFormat.ASTC_RGB_6x6:
            case TextureFormat.ASTC_RGB_8x8:
            case TextureFormat.ASTC_RGB_10x10:
            case TextureFormat.ASTC_RGB_12x12:
            case TextureFormat.ASTC_RGBA_4x4:
            case TextureFormat.ASTC_RGBA_5x5:
            case TextureFormat.ASTC_RGBA_8x8:
            case TextureFormat.ASTC_RGBA_10x10:
            case TextureFormat.ASTC_RGBA_12x12:
            {
                byte[] dest = new byte[width * height * 4];
                uint   size = 0;
                unsafe
                {
                    fixed(byte *dataPtr = data)
                    fixed(byte *destPtr = dest)
                    {
                        IntPtr dataIntPtr = (IntPtr)dataPtr;
                        IntPtr destIntPtr = (IntPtr)destPtr;

                        size = PInvoke.EncodeByPVRTexLib(dataIntPtr, destIntPtr, (int)format, quality, (uint)width, (uint)height);
                    }
                }
                if (size > 0)
                {
                    byte[] resizedDest = new byte[size];
                    Buffer.BlockCopy(dest, 0, resizedDest, 0, (int)size);
                    dest = null;
                    return(resizedDest);
                }
                else
                {
                    dest = null;
                    return(null);
                }
            }

            case TextureFormat.DXT1:
            case TextureFormat.DXT5:
            case TextureFormat.BC4:
            case TextureFormat.BC5:
            case TextureFormat.BC7:
                CompressionFormat bcFmt = CompressionFormat.Bc1;
                switch (format)
                {
                case TextureFormat.DXT1: bcFmt = CompressionFormat.Bc1; break;

                case TextureFormat.DXT5: bcFmt = CompressionFormat.Bc3; break;

                case TextureFormat.BC4:  bcFmt = CompressionFormat.Bc4; break;

                case TextureFormat.BC5:  bcFmt = CompressionFormat.Bc5; break;

                case TextureFormat.BC7:  bcFmt = CompressionFormat.Bc7; break;
                }
                BcEncoder enc = new BcEncoder(bcFmt);
                enc.OutputOptions.GenerateMipMaps = false;
                return(enc.EncodeToRawBytes(data, width, height, PixelFormat.Rgba32, 0, out int _, out int _));

            case TextureFormat.BC6H:     //pls don't use
                return(null);

            default:
                return(null);
            }
        }
示例#44
0
 /// <summary>
 /// Saves this image to the specified stream in specified compression format (default compression is PNG).
 /// </summary>
 /// <param name="stream">The stream where the image will be saved.</param>
 /// <param name="compression">Jpeg or Png.</param>
 public void SaveTo(Stream stream, CompressionFormat compression = CompressionFormat.Png)
 {
     CheckValidity();
     implementation.SaveTo(stream, compression);
 }
示例#45
0
        private void openToolStripMenuItem_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();

            ofd.Filter = "All Files (*.*)|*.*";
            ofd.Title  = "Open Archive";

            DialogResult result = ofd.ShowDialog();

            if (result == DialogResult.OK)
            {
                if (archiveStream != null)
                {
                    archiveStream.Close();
                }

                archiveStream = File.OpenRead(ofd.FileName);

                // Let's determine first if it is an archive
                ArchiveFormat archiveFormat;

                archiveFormat = Archive.GetFormat(archiveStream, ofd.SafeFileName);
                if (archiveFormat != ArchiveFormat.Unknown)
                {
                    // This is an archive. Let's open it.
                    openedArchives.Clear();
                    openedArchiveNames.Clear();

                    OpenArchive(archiveStream, ofd.SafeFileName, archiveFormat);

                    archiveInfoPanel.Visible = true;
                    return;
                }

                // It's not an archive. Maybe it's compressed?
                CompressionFormat compressionFormat = Compression.GetFormat(archiveStream, ofd.SafeFileName);
                if (compressionFormat != CompressionFormat.Unknown)
                {
                    // The file is compressed! Let's decompress it and then try to determine if it is an archive
                    MemoryStream decompressedData = new MemoryStream();
                    Compression.Decompress(archiveStream, decompressedData, compressionFormat);
                    decompressedData.Position = 0;

                    // Now with this decompressed data, let's determine if it is an archive
                    archiveFormat = Archive.GetFormat(decompressedData, ofd.SafeFileName);
                    if (archiveFormat != ArchiveFormat.Unknown)
                    {
                        // This is an archive. Let's open it.
                        openedArchives.Clear();
                        openedArchiveNames.Clear();

                        OpenArchive(decompressedData, ofd.SafeFileName, archiveFormat);

                        archiveInfoPanel.Visible = true;
                        return;
                    }
                }

                // maybe let's actually tell people why their shit won't open
                // rather than just sitting there like a duck doing nothing?
                MessageBox.Show("Unknown or unsupported archive format!");
            }
        }