private void attachControllerToId(String baseControllerFile, UInt16 attachmentId)
        {
            // Set attachment ID to chosen base controller
            ObjectTable baseTable = new ObjectTable(baseControllerFile);
            ObjectEntry entry = (ObjectEntry)baseTable.entries[0];

            if (entry.addAttachment(attachmentId))
            {
                File.Copy(baseControllerFile, baseControllerFile + ".bak", true);
                baseTable.writeToFile(baseControllerFile);
                MessageBoxEx.Show(this, "Success!!");
            }
            else
            {
                MessageBoxEx.Show(this, "You have surpassed the limit of attachments to add to this controller");
            }
        }
        private void swapSlotsCharacterSwapButton_Click(object sender, EventArgs e)
        {
            String controllerPath = swapSlotsCharacterControllerTextBox.Text;
            if (controllerPath == null || controllerPath.Equals(""))
            {
                MessageBoxEx.Show(this, "You have to select a controller!");
                return;
            }

            if (swapSlotsCharacterSlotComboBox.SelectedItem == null)
            {
                MessageBoxEx.Show(this, "Select valid values in the highlighted fields");
                return;
            }

            // After validating the data, swap slots!
            try
            {
                // Test if the given file is a valid controller
                ObjectTable table = new ObjectTable(controllerPath);
                int dlcSlotNumber = (int)swapSlotsCharacterSlotComboBox.SelectedIndex + 1;
                UInt16 dlcSlotId = (UInt16)swapSlotsCharacterSlotComboBox.SelectedValue;

                ObjectEntry entry = (ObjectEntry)table.entries[0];
                // Change DLC id to one valid with the slot if needed
                if (swapSlotsCharacterChangeIdCheckBox.Checked)
                {
                    entry.id = dlcSlotId;
                }
                entry.objectEntrySlot = (byte)dlcSlotNumber;
                // Delete old file and create a new one
                String parentPath = Path.GetDirectoryName(controllerPath);
                File.Delete(controllerPath);
                String objectTableHashFileName = Hasher.hash("dlc/obj/dlc_" + dlcSlotNumber.ToString("d3") + "oe.bin") + ".edat";
                table.writeToFile(System.IO.Path.Combine(parentPath, objectTableHashFileName));
                MessageBoxEx.Show(this, "Success!");

            }
            catch (Exception ex)
            {
                MessageBoxEx.Show(this, "Select a valid controller");
                Logger.Log("MainFormSwapSlotsUserControl", ex);
                return;
            }
        }
 private void attachmentLinkGenerateButton_Click(object sender, EventArgs e)
 {
     try
     {
         String data = attachmentLinkDataTextBox.Text;
         String baseControllerFile = attachmentLinkBaseControllerTextBox.Text;
         if (!baseControllerFile.Equals(""))
         {
             if (attachmentLinkControllerRadioButton.Checked)
             {
                 // Attach base controller to attachment controller
                 ObjectTable attachmentTable = new ObjectTable(data);
                 ObjectEntry entry = (ObjectEntry)attachmentTable.entries[0];
                 attachControllerToId(baseControllerFile, entry.id);
             }
             else if (attachmentLinkIdRadioButton.Checked)
             {
                 // Attach base controller to entered ID
                 UInt16 id;
                 Boolean result = UInt16.TryParse(data.ToUpper(), NumberStyles.HexNumber,
                     CultureInfo.InvariantCulture, out id);
                 attachControllerToId(baseControllerFile, MiscUtils.swapEndianness(id));
             }
         }
         else
         {
             MessageBoxEx.Show(this, "You need to select a base controller in order to link an attachment");
         }
     }
     catch (Exception ex)
     {
         MessageBoxEx.Show(this, "There was an error linking the attachment");
         Logger.Log(TAG, ex);
     }
 }
        private void reportSelectedFolder(String folder)
        {
            // First check: check if there are files in the folder, and check if the folder exists, just in case
            DirectoryInfo dirInfo = new DirectoryInfo(folder);
            if (!dirInfo.Exists)
            {
                MessageBoxEx.Show(this, "The selected folder does not exist");
                return;
            }
            else if (dirInfo.GetFiles().Length == 0)
            {
                MessageBoxEx.Show(this, "The selected folder hasn't got any files");
                return;
            }

            // Clean TextBox
            reporterDataTextBox.Text = "";

            // Get a worksheet to write excel values
            initExcelWorkbook();
            int characterExcelRow = 2;
            int bgmExcelRow = 2;

            // Character DLC checking
            // Iterate through all possible DLC Object Table names
            try
            {
                foreach (int i in Enumerable.Range(1, 999))
                {
                    String objectTableHashFileName = Hasher.hash("dlc/obj/dlc_" + i.ToString("d3") + "oe.bin") + ".edat";
                    String pathToSearch = System.IO.Path.Combine(folder, objectTableHashFileName);
                    if (File.Exists(pathToSearch))
                    {
                        // We have found an object table inside the folder; scan it
                        ObjectTable hashedFileObjectTable = new ObjectTable(pathToSearch);
                        foreach (ObjectEntry entry in hashedFileObjectTable.entries)
                        {
                            // Print results in TextBox
                            String textData = getCharacterDlcTextDataForEntry(folder, i, objectTableHashFileName, entry);
                            reporterDataTextBox.AppendText(textData);
                            // And add them to Excel sheet
                            addCharacterDlcExcelDataForEntry(folder, i, objectTableHashFileName, entry, characterDlcWorksheet, characterExcelRow);
                            characterDlcWorksheet.Cells[characterDlcWorksheet.Dimension.Address].AutoFitColumns();
                            characterExcelRow++;
                        }
                    }
                }
            }
            catch (Exception e)
            {
                MessageBoxEx.Show(this, "There was a problem with one or some of the files in the selected folder");
                Logger.Log(TAG, e);
                return;
            }

            // BGM DLC Checking
            try
            {
                foreach (int i in Enumerable.Range(1, 999))
                {
                    String bgmTableHashFileName = Hasher.hash(String.Format("dlc/bgm/dlc_{0}.bin", i.ToString("D3"))) + ".edat";
                    String pathToSearch = System.IO.Path.Combine(folder, bgmTableHashFileName);
                    if (File.Exists(pathToSearch))
                    {
                        // We have found a Bgm table inside the folder; try to find the corresponding
                        // strings from text file in DLC slot
                        ArrayList bgmTitles = new ArrayList();
                        String textHashedFileName = Hasher.hash(String.Format("text/jp/dlc/bgm_{0}t.bin", i.ToString("D3"))) + ".edat";
                        String textHashedFilePath = System.IO.Path.Combine(folder, textHashedFileName);
                        if (File.Exists(textHashedFilePath))
                        {
                            bgmTitles = MessFileReader.decodeDLCText(@textHashedFilePath);
                        }

                        BgmTable bgmTable = new BgmTable(pathToSearch);

                        // Getting Form BGM entries
                        List<FormBgmEntry> formEntries = new List<FormBgmEntry>();
                        foreach (int j in Enumerable.Range(0, bgmTable.entries.Count))
                        {
                            BgmEntry entry = bgmTable.entries[j];
                            FormBgmEntry formEntry = new FormBgmEntry(entry);
                            if (bgmTitles.Count > j)
                            {
                                formEntry.bgmTitle = (String)bgmTitles[j];
                            }
                            formEntries.Add(formEntry);
                        }

                        foreach (FormBgmEntry entry in formEntries)
                        {
                            // Print results in TextBox
                            String textData = getBgmDlcTextDataForEntry(i, entry);
                            reporterDataTextBox.AppendText(textData);
                            // And add them to Excel sheet
                            addBgmDlcExcelDataForEntry(i, entry, bgmDlcWorksheet, bgmExcelRow);
                            bgmDlcWorksheet.Cells[bgmDlcWorksheet.Dimension.Address].AutoFitColumns();
                            bgmExcelRow++;
                        }
                    }
                }
            }
            catch (Exception e)
            {
                MessageBoxEx.Show(this, "There was a problem with one or some of the files in the selected folder");
                Logger.Log(TAG, e);
                return;
            }

            // If the reporter text box has any data, then that means that there's data in the folder
            if (!reporterDataTextBox.Text.Trim().Equals(""))
            {
                reporterSaveToExcelButton.Enabled = true;
                reporterFolderLabel.Text = folder;
                MessageBoxEx.Show(this, "DLC read OK!!");
            }
            else
            {
                MessageBoxEx.Show(this, "Couldn't find DLC in this folder");
            }
        }