private bool[,] ProcessImage(Func <bool[, ], bool[, ]> imageDataProcessor, FirmwareImageMetadata imageMetadata, bool rebuildCache = false)
        {
            var processedData      = imageDataProcessor(ImagePixelGrid.Data);
            var processedImageSize = processedData.GetSize();
            var imageSizeChanged   = imageMetadata.Width != processedImageSize.Width || imageMetadata.Height != processedImageSize.Height;

            imageMetadata.Width  = (byte)processedImageSize.Width;
            imageMetadata.Height = (byte)processedImageSize.Height;

            m_firmware.WriteImage(processedData, imageMetadata);

            if (imageSizeChanged || rebuildCache)
            {
                ImageCacheManager.RebuildImageCache(m_firmware);
                ImageListBox.Invalidate();
            }
            else
            {
                var cachedImage = FirmwareImageProcessor.CreateBitmap(processedData);
                ImageCacheManager.SetImage(imageMetadata.Index, imageMetadata.BlockType, cachedImage);

                var updateCache = new Action(() =>
                {
                    ImageListBox.Invoke(new Action(() =>
                    {
                        var itemRect = ImageListBox.GetItemRectangle(imageMetadata.Index - 1);
                        ImageListBox.Invalidate(itemRect);
                    }));
                });
                updateCache.BeginInvoke(null, null);
            }

            return(processedData);
        }
        private void ImportFontMenuItem_Click(object sender, EventArgs e)
        {
            if (SelectedImageMetadata.Count == 0)
            {
                return;
            }

            using (var importFontWindow = new ImportFontWindow(SelectedImageMetadata))
            {
                if (importFontWindow.ShowDialog() != DialogResult.OK)
                {
                    return;
                }

                var importedData = importFontWindow.GetImportedData();
                foreach (var imageDataTuple in importedData)
                {
                    var metadata  = imageDataTuple.Item1;
                    var imageData = imageDataTuple.Item2;

                    ProcessImage(x => imageData, metadata);
                }
                ImageCacheManager.RebuildCache(m_firmware);
                ImageListBox.Invalidate();
                ImageListBox_SelectedValueChanged(ImageListBox, EventArgs.Empty);
            }
        }
        private void ImportImages([NotNull] IList <int> originalImageIndices, [NotNull] IList <bool[, ]> importedImages)
        {
            if (importedImages == null)
            {
                throw new ArgumentNullException("importedImages");
            }
            if (originalImageIndices == null)
            {
                throw new ArgumentNullException("originalImageIndices");
            }
            if (importedImages.Count == 0)
            {
                return;
            }

            var minimumImagesCount = Math.Min(originalImageIndices.Count, importedImages.Count);

            originalImageIndices = originalImageIndices.Take(minimumImagesCount).ToList();
            importedImages       = importedImages.Take(minimumImagesCount).ToList();

            ImageImportMode importMode;
            bool            allowResizeOriginalImages;

            using (var importWindow = new PreviewResourcePackWindow(m_firmware, originalImageIndices, importedImages, false, m_currentBlock))
            {
                importWindow.Text             = Consts.ApplicationTitleWoVersion + @" - Paste image(s)";
                importWindow.ImportButtonText = "Paste";
                if (importWindow.ShowDialog() != DialogResult.OK)
                {
                    return;
                }

                importMode = importWindow.GetImportMode();
                allowResizeOriginalImages = importWindow.AllowResizeOriginalImages;
            }

            for (var i = 0; i < minimumImagesCount; i++)
            {
                var index = i;
                var originalImageIndex = originalImageIndices[index];
                var importedImage      = importedImages[index];
                if (importMode == ImageImportMode.Block1)
                {
                    var block1ImageMetadata = m_firmware.Block1Images.First(x => x.Index == originalImageIndex);
                    if (allowResizeOriginalImages)
                    {
                        ProcessImage(x => importedImage, block1ImageMetadata);
                    }
                    else
                    {
                        ProcessImage(x => FirmwareImageProcessor.PasteImage(block1ImageMetadata.CreateImage(), importedImage), block1ImageMetadata);
                    }
                }
                else if (importMode == ImageImportMode.Block2)
                {
                    var block2ImageMetadata = m_firmware.Block2Images.First(x => x.Index == originalImageIndex);
                    if (allowResizeOriginalImages)
                    {
                        ProcessImage(x => importedImage, block2ImageMetadata);
                    }
                    else
                    {
                        ProcessImage(x => FirmwareImageProcessor.PasteImage(block2ImageMetadata.CreateImage(), importedImage), block2ImageMetadata);
                    }
                }
                else
                {
                    var block1ImageMetadata = m_firmware.Block1Images.First(x => x.Index == originalImageIndex);
                    var block2ImageMetadata = m_firmware.Block2Images.First(x => x.Index == originalImageIndex);

                    if (allowResizeOriginalImages)
                    {
                        ProcessImage(x => importedImage, block1ImageMetadata);
                        ProcessImage(x => importedImage, block2ImageMetadata);
                    }
                    else
                    {
                        ProcessImage(x => FirmwareImageProcessor.PasteImage(block1ImageMetadata.CreateImage(), importedImage), block1ImageMetadata);
                        ProcessImage(x => FirmwareImageProcessor.PasteImage(block2ImageMetadata.CreateImage(), importedImage), block2ImageMetadata);
                    }
                }
            }

            ImageCacheManager.RebuildImageCache(m_firmware);
            ImageListBox.Invalidate();
            ImageListBox_SelectedValueChanged(ImageListBox, EventArgs.Empty);
        }
示例#4
0
        private void downloadUpdates(List <Update> updates)
        {
            Logger.log(Logger.TYPE.DEBUG, "Downloading " + updates.Count + " updates...");
            // Enqueue each file to the download handler
            foreach (Update update in updates)
            {
                foreach (GhostFile file in update.getFiles())
                {
                    Boolean isArchive = file is Archive;

                    String tmpPath = Path.Combine(update.getTempDir().FullName,
                                                  isArchive ? ((Archive)file).getExtractTo() : file.getDestination());

                    dlHandler.enqueueFile(file.getUrl(), tmpPath, file.getName(),
                                          (Boolean cancelled) => {
                        if (!cancelled)
                        {
                            Logger.log(Logger.TYPE.DEBUG, "Completed downloading "
                                       + (isArchive ? "archive " : "file ") + file.getName());
                        }
                    });
                }
            }

            dlHandler.setQueueFileCallback(new QueueCallback(() => {
                Logger.log(Logger.TYPE.DEBUG, "Completed all the downloads");

                ui.getStatusLabel().Text = "Patching files...";
                foreach (Update update in updates)
                {
                    foreach (GhostFile file in update.getFiles())
                    {
                        String tmpPath = Path.Combine(update.getTempDir().FullName,
                                                      file is Archive ? ((Archive)file).getExtractTo()
                            : file.getDestination());

                        update.setSuccess(applyFileChange(file, tmpPath));
                    }

                    if (update.isSuccess())
                    {
                        reciever.stampUpdate(update);

                        ImageListBox listBox  = ui.getChangelogListBox();
                        ImageListBoxItem item = getLogItem(update, listBox);
                        if (item != null)
                        {
                            item.ImageIndex = 0;
                            listBox.Invalidate(listBox.GetItemRectangle(item.Index));
                            listBox.Update();
                        }
                    }
                    else
                    {
                        Logger.log(Logger.TYPE.FATAL, "One of the updates did not succeed, "
                                   + "cancelling update process.");

                        ui.getStatusLabel().Text = "Error while patching files, please check the "
                                                   + "log for more details.";

                        ui.getLaunchButton().BtnText = "Failed";
                        return;
                    }
                }

                ui.getStatusLabel().Text = "Finished updating "
                                           + updates.Count + "/" + updates.Count + "!";

                ui.getTickImage().Visible     = true;
                ui.getUpToDateLabel().Visible = true;

                enablePlay();
            }));

            dlHandler.startFileQueue();
        }