示例#1
0
        /* Create the archive */
        //private void run(object sender, DoWorkEventArgs e)
        private void run(string output_filename)
        {
            try
            {
                // Get archive and compression format
                int ArchiveFormatIndex = archiveFormatList.SelectedIndex;
                int CompressionFormatIndex = compressionFormatList.SelectedIndex;

                // Get output filename
                //string output_filename = FileSelectionDialog.SaveFile("Create Archive", String.Empty, String.Format("{0} ({1})|{1}", ArchiveFilters[archiveFormatList.SelectedIndex][0], ArchiveFilters[archiveFormatList.SelectedIndex][1]));
                //if (output_filename == null || output_filename == String.Empty)
                //    return;

                // Disable the window and show the status box
                PanelContent.Enabled = false;
                //status.Visible = true;

                /* Start creating the archive */
                Archive archive = new Archive(ArchiveFormats[archiveFormatList.SelectedIndex], Path.GetFileName(output_filename));

                using (FileStream outputStream = new FileStream(output_filename, FileMode.Create, FileAccess.ReadWrite))
                {
                    // Get the block size
                    int blockSize;
                    if (blockSizes[ArchiveFormatIndex].Enabled && blockSizes[ArchiveFormatIndex].Text.Length > 0)
                        blockSize = int.Parse(blockSizes[ArchiveFormatIndex].Text);
                    else
                        blockSize = ArchiveBlockSizes[ArchiveFormatIndex][0];

                    /* Create and write the header */
                    bool[] settings = GetSettings(ArchiveFormatIndex);
                    uint[] offsetList;
                    MemoryStream header = archive.CreateHeader(fileList.ToArray(), archiveFilenames.ToArray(), blockSize, settings, out offsetList);
                    outputStream.Write(header);

                    /* Add the files */
                    for (int i = 0; i < fileList.Count; i++)
                    {
                        // Set the current file
                        status.CurrentFile = i;

                        /* Pad file so we can have the correct block offset */
                        while (outputStream.Position < offsetList[i])
                            outputStream.WriteByte(archive.PaddingByte);

                        using (Stream inputStream = new FileStream(fileList[i], FileMode.Open, FileAccess.Read))
                        {
                            /* Format the file so we can add it */
                            Stream inputFile = inputStream;
                            inputFile = archive.FormatFileToAdd(ref inputFile);
                            if (inputFile == null)
                                throw new Exception();

                            /* Write the data to the file */
                            outputStream.Write(inputFile);
                        }
                    }

                    /* Pad file so we can have the correct block offset */
                    while (outputStream.Position % blockSize != 0)
                        outputStream.WriteByte(archive.PaddingByte);

                    /* Write the footer */
                    MemoryStream footer = archive.CreateFooter(fileList.ToArray(), archiveFilenames.ToArray(), blockSize, settings, ref header);
                    if (footer != null)
                    {
                        outputStream.Write(footer);

                        /* Pad file so we can have the correct block offset */
                        while (outputStream.Position % blockSize != 0)
                            outputStream.WriteByte(archive.PaddingByte);
                    }

                    /* Compress the data if we want to */
                    if (compressionFormatList.SelectedIndex != 0)
                    {
                        Compression compression = new Compression(outputStream, output_filename, CompressionFormats[CompressionFormatIndex - 1]);
                        MemoryStream compressedData = compression.Compress();
                        if (compressedData != null)
                        {
                            /* Clear the output stream and write the compressed data */
                            outputStream.Position = 0;
                            outputStream.SetLength(compressedData.Length);
                            outputStream.Write(compressedData);
                        }
                    }
                }

                this.Close();
            }
            catch
            {
                this.Close();
            }
            finally
            {
                status.Close();
            }
        }
示例#2
0
        /* Convert the images */
        private void run(object sender, DoWorkEventArgs e)
        {
            /* Create the file list */
            List<string> fileList = new List<string>();
            foreach (string i in files)
                fileList.Add(i);

            for (int i = 0; i < files.Length; i++)
            {
                /* Set the current file */
                status.CurrentFile = i;

                try
                {
                    /* Open up the file */
                    Stream data = null;
                    string outputFilename  = Path.GetFileName(fileList[i]);
                    string outputDirectory = Path.GetDirectoryName(fileList[i]);
                    using (Stream inputStream = new FileStream(fileList[i], FileMode.Open, FileAccess.Read))
                    {
                        /* Set up the PVR creator */
                        data = inputStream;
                        PVR encoder = new PVR();

                        switch (paletteFormat.SelectedIndex)
                        {
                            case 0: encoder.PixelFormat = PvrPixelFormat.Argb1555; break;
                            case 1: encoder.PixelFormat = PvrPixelFormat.Rgb565; break;
                            case 2: encoder.PixelFormat = PvrPixelFormat.Argb4444; break;
                        }
                        switch (dataFormat.SelectedIndex)
                        {
                            case 0: encoder.DataFormat = PvrDataFormat.SquareTwiddled; break;
                            case 1: encoder.DataFormat = PvrDataFormat.Rectangle; break;
                            case 2: encoder.DataFormat = PvrDataFormat.RectangleTwiddled; break;
                        }

                        // Set the global index and gbix header
                        // There shouldn't be an error when parsing anymore, so we won't do checks
                        encoder.GbixHeader = addGbix.Checked;
                        if (addGbix.Checked && globalIndex.Text.Length > 0)
                            encoder.GlobalIndex = uint.Parse(globalIndex.Text);
                        else
                            encoder.GlobalIndex = 0;

                        /* Set up our input and output image */
                        string inputImage  = outputDirectory + Path.DirectorySeparatorChar + outputFilename;
                        string outputImage = outputDirectory + Path.DirectorySeparatorChar + (convertSameDir.Checked ? String.Empty : "PVR Created" + Path.DirectorySeparatorChar) + Path.GetFileNameWithoutExtension(outputFilename) + (compressFile.Checked && compressionFormat.SelectedIndex == 0 ? ".pvz" : ".pvr");
                        outputFilename     = outputImage;

                        /* Create image */
                        MemoryStream imageData = (MemoryStream)encoder.Pack(ref data);

                        /* Don't continue if an image wasn't created */
                        if (imageData == null)
                            continue;

                        // Compress image?
                        if (compressFile.Checked)
                        {
                            Compression compression = new Compression(imageData, outputFilename, CompressionFormats[compressionFormat.SelectedIndex]);

                            MemoryStream compressedData = compression.Compress();
                            if (compressedData != null)
                            {
                                imageData = compressedData;
                                outputImage = Path.GetDirectoryName(outputImage) + Path.DirectorySeparatorChar + compression.CompressFilename;
                            }
                        }

                        /* Create the output directory if it does not exist */
                        if (!Directory.Exists(Path.GetDirectoryName(outputImage)))
                            Directory.CreateDirectory(Path.GetDirectoryName(outputImage));

                        /* Output the image */
                        using (FileStream outputStream = new FileStream(outputImage, FileMode.Create, FileAccess.Write))
                            outputStream.Write(imageData);
                    }

                    /* Delete the source image if we want to */
                    if (deleteSourceImage.Checked && File.Exists(fileList[i]) && File.Exists(outputFilename))
                        File.Delete(fileList[i]);
                }
                catch
                {
                    /* Something went wrong. Continue please. */
                    continue;
                }
            }

            /* Close the status box now */
            status.Close();
            this.Close();
        }
示例#3
0
        /* Decompress the files */
        private void run(object sender, DoWorkEventArgs e)
        {
            /* Create the file list */
            List<string> fileList = new List<string>();
            foreach (string i in files)
                fileList.Add(i);

            // Get file size restriction size
            uint restrictSize = 0;
            if (filesizeRestriction.Checked && !uint.TryParse(filesizeRestrictionSize.Text, out restrictSize)) // For some reason if this happens
                restrictSize = 0;

            for (int i = 0; i < files.Length; i++)
            {
                /* Set the current file */
                status.CurrentFile = i;

                try
                {
                    /* Open up the file */
                    MemoryStream data;
                    string outputDirectory, outputFilename;
                    using (FileStream inputStream = new FileStream(fileList[i], FileMode.Open, FileAccess.Read))
                    {
                        // Check to see if we want to compress this file (filesize restriction)
                        if (filesizeRestriction.Checked && inputStream.Length < restrictSize)
                            continue;

                        // Setup the decompressor
                        Compression compression = new Compression(inputStream, Path.GetFileName(fileList[i]), CompressionFormats[compressionFormat.SelectedIndex]);

                        /* Set up the output directories and file names */
                        outputDirectory = Path.GetDirectoryName(fileList[i]) + (compressSameDir.Checked ? String.Empty : Path.DirectorySeparatorChar + "Compressed");
                        outputFilename  = Path.GetFileName(fileList[i]);

                        // Decompress data and get decompressed filename
                        MemoryStream compressedData = compression.Compress();
                        outputFilename = compression.CompressFilename;

                        /* Check to make sure the decompression was successful */
                        if (compressedData == null)
                            continue;
                        else
                            data = compressedData;
                    }

                    /* Create the output directory if it does not exist */
                    if (!Directory.Exists(outputDirectory))
                        Directory.CreateDirectory(outputDirectory);

                    /* Write file data */
                    using (FileStream outputStream = new FileStream(outputDirectory + Path.DirectorySeparatorChar + outputFilename, FileMode.Create, FileAccess.Write))
                        outputStream.Write(data);
                }
                catch
                {
                    /* Something went wrong. Continue please. */
                    continue;
                }
            }

            /* Close the status box now */
            status.Close();
            this.Close();
        }