Example #1
0
        /// <summary>
        /// Shows preview for selected file.
        /// </summary>
        private void Preview()
        {
            if (LstFiles.SelectedIndices.Count == 0)
            {
                return;
            }

            var selected = LstFiles.SelectedItems[0];
            var fileName = (string)selected.Tag;
            var ipfFile  = _files[fileName];
            var ext      = Path.GetExtension(fileName).ToLowerInvariant();

            var previewType = PreviewType.None;
            var lexer       = Lexer.Null;

            FileFormat fileType;

            if (_fileTypes.TryGetValue(ext, out fileType))
            {
                previewType = fileType.PreviewType;
                lexer       = fileType.Lexer;
            }

            ThreadPool.QueueUserWorkItem(state =>
            {
                switch (previewType)
                {
                case PreviewType.Text:
                    var txtData = ipfFile.GetData();
                    var text    = Encoding.UTF8.GetString(txtData);

                    SetTextPreviewStyle(lexer);

                    Invoke((MethodInvoker) delegate
                    {
                        TxtPreview.ReadOnly = false;
                        TxtPreview.Text     = text;
                        TxtPreview.ReadOnly = true;
                        TxtPreview.Visible  = true;
                    });
                    break;

                case PreviewType.Image:
                    var imgData = ipfFile.GetData();

                    Invoke((MethodInvoker) delegate
                    {
                        using (var ms = new MemoryStream(imgData))
                            ImgPreview.Image = Image.FromStream(ms);
                        ImgPreview.Size         = ImgPreview.Image.Size;
                        PnlImagePreview.Visible = true;
                    });
                    break;

                case PreviewType.DdsImage:
                    var ddsData = ipfFile.GetData();

                    DDSImage ddsImage = null;
                    try
                    {
                        ddsImage = new DDSImage(ddsData);
                    }
                    catch (Exception)
                    {
                        Invoke((MethodInvoker) delegate
                        {
                            LblPreview.Text = "Preview failed";
                        });
                        break;
                    }

                    Invoke((MethodInvoker) delegate
                    {
                        ImgPreview.Image        = ddsImage.BitmapImage;
                        ImgPreview.Size         = ImgPreview.Image.Size;
                        PnlImagePreview.Visible = true;
                    });
                    break;

                case PreviewType.TgaImage:
                    var tgaData = ipfFile.GetData();

                    TargaImage tgaImage = null;
                    try
                    {
                        using (var ms = new MemoryStream(tgaData))
                            tgaImage = new TargaImage(ms);
                    }
                    catch (Exception)
                    {
                        Invoke((MethodInvoker) delegate
                        {
                            LblPreview.Text = "Preview failed";
                        });
                        break;
                    }

                    Invoke((MethodInvoker) delegate
                    {
                        ImgPreview.Image        = tgaImage.Image;
                        ImgPreview.Size         = ImgPreview.Image.Size;
                        PnlImagePreview.Visible = true;
                    });
                    break;

                case PreviewType.IesTable:
                    var iesData = ipfFile.GetData();
                    var iesFile = new IesFile(iesData);

                    Invoke((MethodInvoker) delegate
                    {
                        GridPreview.SuspendDrawing();

                        foreach (var iesColumn in iesFile.Columns)
                        {
                            GridPreview.Columns.Add(iesColumn.Name, iesColumn.Name);
                        }

                        foreach (var iesRow in iesFile.Rows)
                        {
                            var row = new DataGridViewRow();
                            row.CreateCells(GridPreview);

                            var i = 0;
                            foreach (var iesColumn in iesFile.Columns)
                            {
                                row.Cells[i++].Value = iesRow[iesColumn.Name];
                            }

                            GridPreview.Rows.Add(row);
                        }

                        GridPreview.ResumeDrawing();

                        GridPreview.Visible = true;
                    });
                    break;

                case PreviewType.TtfFont:
                    var pfc = new PrivateFontCollection();

                    try
                    {
                        var ttfData = ipfFile.GetData();
                        using (var ms = new MemoryStream(ttfData))
                        {
                            var fontdata = new byte[ms.Length];
                            ms.Read(fontdata, 0, (int)ms.Length);

                            unsafe
                            {
                                fixed(byte *pFontData = fontdata)
                                pfc.AddMemoryFont((IntPtr)pFontData, fontdata.Length);
                            }
                        }
                    }
                    catch (Exception)
                    {
                        Invoke((MethodInvoker) delegate
                        {
                            LblPreview.Text = "Preview failed";
                        });
                        break;
                    }

                    var fontFamily = pfc.Families.First();
                    var font       = new Font(fontFamily, 18, FontStyle.Regular, GraphicsUnit.Pixel);
                    var arialFont  = new Font("Arial", 12);

                    var fontInfo = "Name: " + fontFamily.Name;
                    var example1 = "abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ\n1234567890.:,;'\" (!?) +-*/=";
                    var example2 = "Lorem ipsum dolor sit amet.";

                    var bmp = new Bitmap(600, 500);
                    using (var graphics = Graphics.FromImage(bmp))
                    {
                        var infoHeight     = graphics.MeasureString(fontInfo, arialFont).Height;
                        var example1Height = graphics.MeasureString(example1, font).Height;

                        graphics.TextRenderingHint = TextRenderingHint.AntiAlias;
                        graphics.FillRectangle(Brushes.White, new Rectangle(0, 0, bmp.Width, bmp.Height));

                        graphics.DrawString(fontInfo, arialFont, Brushes.Black, new Point(0, 0));
                        graphics.DrawString(example1, font, Brushes.Black, new PointF(0, infoHeight + 10));

                        var point = new PointF(10, infoHeight + 10 + example1Height + 20);
                        foreach (var size in new int[] { 12, 18, 24, 36, 48, 60, 72 })
                        {
                            font = new Font(fontFamily, size, FontStyle.Regular, GraphicsUnit.Pixel);
                            graphics.DrawString(example2, font, Brushes.Black, point);
                            point.Y += font.Height + 5;
                        }
                    }

                    Invoke((MethodInvoker) delegate
                    {
                        ImgPreview.Image        = bmp;
                        ImgPreview.Size         = ImgPreview.Image.Size;
                        PnlImagePreview.Visible = true;
                    });
                    break;

                default:
                    Invoke((MethodInvoker) delegate
                    {
                        LblPreview.Text = "No Preview";
                    });
                    break;
                }
            });
        }
Example #2
0
        /// <summary>
        /// Opens given IPF file.
        /// </summary>
        /// <param name="filePath"></param>
        private void Open(string filePath)
        {
            if (Path.GetExtension(filePath) == ".ies")
            {
                this.ResetPreview();

                var iesData = File.ReadAllBytes(filePath);
                var iesFile = new IesFile(iesData);

                Invoke((MethodInvoker) delegate
                {
                    GridPreview.SuspendDrawing();

                    foreach (var iesColumn in iesFile.Columns)
                    {
                        GridPreview.Columns.Add(iesColumn.Name, iesColumn.Name);
                    }

                    foreach (var iesRow in iesFile.Rows)
                    {
                        var row = new DataGridViewRow();
                        row.CreateCells(GridPreview);

                        var i = 0;
                        foreach (var iesColumn in iesFile.Columns)
                        {
                            row.Cells[i++].Value = iesRow[iesColumn.Name];
                        }

                        GridPreview.Rows.Add(row);
                    }

                    GridPreview.ResumeDrawing();

                    GridPreview.Visible = true;
                });

                SplMain.Visible = true;

                return;
            }

            // Reset everything
            TreeFolders.Nodes.Clear();
            LstFiles.Items.Clear();
            ResetPreview();

            _folders.Clear();
            _files.Clear();

            LblVersion.Text  = "";
            LblFileName.Text = "";

            // Open IPF
            try
            {
                _openedIpf = new Ipf(filePath);
                _openedIpf.Load();
            }
            catch (IOException)
            {
                _openedIpf = null;
                MessageBox.Show("Failed to open file, it's already in use.", Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            // Create file list
            var paths = new List <string>();

            foreach (var ipfFile in _openedIpf.Files)
            {
                paths.Add(ipfFile.FullPath);
                _files.Add(ipfFile.FullPath, ipfFile);
            }

            // Create fil tree
            PopulateTreeView(TreeFolders, paths, '/');

            // Status info
            LblVersion.Text  = "Version " + _openedIpf.Footer.NewVersion;
            LblFileName.Text = filePath;

            // Open first node if there only is one
            if (TreeFolders.Nodes.Count == 1)
            {
                TreeFolders.SelectedNode = TreeFolders.Nodes[0];
                TreeFolders.SelectedNode.Toggle();
            }

            // Show lists and enabled pack extract button
            BtnExtractPack.Enabled = true;
            SplMain.Visible        = true;
        }