Nanook.QueenBee.EditorForm.btnLoadPak_Click C# (CSharp) Method

btnLoadPak_Click() private method

private btnLoadPak_Click ( object sender, EventArgs e ) : void
sender object
e System.EventArgs
return void
        private void btnLoadPak_Click(object sender, EventArgs e)
        {
            try
            {
                this.Cursor = Cursors.WaitCursor;

                if (txtPakFile.Text.Trim().Length == 0)
                {
                    showError("Pak File Error", "The PAK filename is blank");
                    return;
                }

                _pakFormat = new PakFormat(txtPakFile.Text, txtPabFile.Text, txtDebugFile.Text, formatToPakFormatType(cboFormatType.Text), false);

                if (_pakFormat.IsCompressed)
                {
                    if (!_pakFormat.CompressedPakFileExists)
                    {
                        showError("Pak File Error", string.Format("The PAK file does not exist '{0}'", txtPakFile.Text));
                        return;
                    }

                    if (txtPabFile.Text.Length != 0 && !_pakFormat.CompressedPabFileExists)
                    {
                        showError("Pab File Error", string.Format("The PAB file does not exist '{0}'", txtPabFile.Text));
                        return;
                    }

                    if (txtDebugFile.Text.Length != 0 && !_pakFormat.CompressedDebugFileExists)
                    {
                        showError("Debug File Error", string.Format("The Debug file does not exist '{0}'", txtDebugFile.Text));
                        return;
                    }
                }
                else
                {
                    if (!_pakFormat.PakFileExists)
                    {
                        showError("Pak File Error", string.Format("The PAK file does not exist '{0}'", txtPakFile.Text));
                        return;
                    }

                    if (txtPabFile.Text.Length != 0 && !_pakFormat.PabFileExists)
                    {
                        showError("Pab File Error", string.Format("The PAB file does not exist '{0}'", txtPabFile.Text));
                        return;
                    }

                    if (txtDebugFile.Text.Length != 0 && !_pakFormat.DebugFileExists)
                    {
                        showError("Debug File Error", string.Format("The Debug file does not exist '{0}'", txtDebugFile.Text));
                        return;
                    }
                }

                clearInterface();

                AppState.InputFormat = cboFormatType.Text;
                AppState.PakFilename = txtPakFile.Text;
                AppState.PabFilename = txtPabFile.Text;
                AppState.DebugFilename = txtDebugFile.Text;
                AppState.Backup = chkBackup.Checked;

                try
                {
                    if (chkBackup.Checked)
                    {
                        if (_pakFormat.PakFormatType == PakFormatType.XBox)
                        {
                            backup(_pakFormat.FullCompressedPakFilename);
                            if (_pakFormat.PabFileExists)
                                backup(_pakFormat.FullCompressedPabFilename);
                        }
                        else
                        {
                            backup(_pakFormat.FullPakFilename);
                            if (_pakFormat.PabFileExists)
                                backup(_pakFormat.FullPabFilename);
                        }
                    }
                }
                catch (Exception ex)
                {
                    showException("PAK Backup Error", ex);
                    clearInterface();
                    return;
                }

                try
                {
                    _pakFile = new PakEditor(_pakFormat);
                }
                catch (Exception ex)
                {
                    showException("PAK File Load/Parse Error", ex);
                    clearInterface();
                    return;
                }

                if (_pakFile.RequiresPab && !_pakFormat.PabFileExists)
                {
                    showError("PAK Error", "The data for this PAK is not present, it may require a PAB");
                    clearInterface();
                    return;
                }

                try
                {
                    if (_pakFormat.DebugFileExists)
                        _dbgFile = new PakEditor(_pakFormat, true);
                    else
                        _dbgFile = null;
                }
                catch (Exception ex)
                {
                    showException("Debug File Load/Parse Error", ex);
                    clearInterface();
                    return;
                }

                try
                {
                    ListViewItem li;
                    string[] fn;
                    char[] sc = new char[] { '\\' };

                    lstPakContents.BeginUpdate();
                    lstPakContents.ListViewItemSorter = null;
                    lstPakContents.Items.Clear();
                    foreach (PakHeaderItem phi in _pakFile.Headers.Values)
                    {
                        fn = phi.Filename.Split(sc);
                        li = new ListViewItem(fn[fn.Length - 1]);
                        li.SubItems.Add(phi.Filename);
                        li.SubItems.Add(string.Format("{0} ({1})", (phi.HeaderStart + phi.FileOffset).ToString("X").PadLeft(8, '0'), (phi.HeaderStart + phi.FileOffset).ToString()));
                        li.SubItems.Add(phi.FileLength.ToString());
                        li.SubItems.Add(phi.FileType.Text);
                        li.ImageIndex = getPakFileImageIndex(phi.PakFileType);
                        li.Tag = phi;
                        lstPakContents.Items.Add(li);
                    }

                    lstPakContents.Focus();

                    updateStatusItems();
                }
                catch (Exception ex)
                {
                    showException("PAK List Population Error", ex);
                    clearInterface();
                    return;
                }
                finally
                {
                    lstPakContents.ListViewItemSorter = _lvwPakColumnSorter;
                    lstPakContents.EndUpdate();
                }

                lstPakContents.Sort();

                if (lstPakContents.Items.Count > 0)
                {
                    lstPakContents.Items[0].Selected = true;
                    lstPakContents.Items[0].Focused = true;
                }

                tabPak.Text = string.Format("PAK: {0}", (new FileInfo(_pakFile.Filename)).Name);
                mnuEditQBFile.Enabled = true;
            }
            finally
            {
                this.Cursor = Cursors.Default;

                try
                {
                    if (_pakFile.StructItemChildrenType == StructItemChildrenType.NotSet)
                    {
                        if (MessageBox.Show(this,
            @"Unable to detect StructItem Children Type.

            Is this a newer PAK file (GH:WT onwards)?

            New PAK files have array IDs within Struct types. Queen Bee can load these
            types without needing to know the type. It is required when creating new types
            This PAK has no StructItem children so this setting could not be detected.", "StructItem Children Type", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                        {
                            _pakFormat.StructItemChildrenType = StructItemChildrenType.ArrayItems;
                        }
                        else
                        {
                            _pakFormat.StructItemChildrenType = StructItemChildrenType.StructItems;
                        }
                    }
                }
                catch
                {
                }
            }
        }
EditorForm