private void swapSlotsBgmSwapButton_Click(object sender, EventArgs e)
        {
            String controllerPath = swapSlotsBgmControllerTextBox.Text;
            String parentPath = Path.GetDirectoryName(controllerPath);

            if (controllerPath == null || controllerPath.Equals(""))
            {
                MessageBoxEx.Show(this, "You have to select a controller!");
                return;
            }

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

            try
            {
                // Test if the given file is a valid controller
                BgmTable table = new BgmTable(controllerPath);
                int newBgmDlcSlotNumber = (int)swapSlotsBgmSlotComboBox.SelectedIndex + 1;

                // First, try to find the text file so we can also change its slots by cracking
                // its name
                int originalBgmDlcSlotNumber = 0;
                foreach (int i in Enumerable.Range(1, 999))
                {
                    if (controllerPath.Contains(Hasher.hash(String.Format("dlc/bgm/dlc_{0}.bin", i.ToString("D3")))))
                    {
                        originalBgmDlcSlotNumber = i;
                        break;
                    }
                }
                String textHashedFileName = Hasher.hash(String.Format("text/jp/dlc/bgm_{0}t.bin", originalBgmDlcSlotNumber.ToString("D3"))) + ".edat";
                String textHashedFilePath = System.IO.Path.Combine(parentPath, textHashedFileName);
                if (!File.Exists(textHashedFilePath))
                {
                    MessageBoxEx.Show(this, "The selected BGM DLC controller doesn't have a text file!");
                    return;
                }

                // Here we should already have all the needed data, so swap slots!
                // Change DLC id to one valid with the slot if needed
                if (swapSlotsBgmChangeIdsCheckBox.Checked)
                {
                    table.generateRandomEntryNamesAndIds(newBgmDlcSlotNumber, true, false);
                }
                // Rename old files to new ones
                File.Delete(controllerPath);
                table.writeToFile(Path.Combine(parentPath, Hasher.hash(String.Format("dlc/bgm/dlc_{0}.bin", newBgmDlcSlotNumber.ToString("D3"))) + ".edat"));
                // Move text entry
                File.Move(textHashedFilePath, Path.Combine(parentPath, Hasher.hash(String.Format("text/jp/dlc/bgm_{0}t.bin", newBgmDlcSlotNumber.ToString("D3"))) + ".edat"));
                MessageBoxEx.Show(this, "Success!");

            }
            catch (Exception ex)
            {
                MessageBoxEx.Show(this, "Select a valid controller");
                Logger.Log("MainFormSwapSlotsUserControl", ex);
                return;
            }
        }
        private void button10_Click(object sender, EventArgs e)
        {
            // Restart data
            currentBgmIndex = -1;

            // Get Controller
            String controllerFilePath = FormUtils.openGenericFileDialog();

            if (controllerFilePath != null)
            {
                String controllerFileName = System.IO.Path.GetFileName(controllerFilePath);

                // Needed to locate original BGM DLC data
                String controllerFolder = Directory.GetParent(controllerFilePath).FullName;

                try
                {
                    // Getting data from BgmTable
                    BgmTable bgmTable = new BgmTable(controllerFilePath);

                    // Attempt to crack BGM DLC Slot from filename
                    int dlcSlotNumber = 0;
                    ArrayList bgmTitles = new ArrayList();
                    foreach (int i in Enumerable.Range(1, 999))
                    {
                        if (controllerFileName.Contains(Hasher.hash(String.Format("dlc/bgm/dlc_{0}.bin", i.ToString("D3")))))
                        {
                            dlcSlotNumber = i;
                            break;
                        }
                    }
                    if (dlcSlotNumber != 0)
                    {
                        // Try getting strings from text file in DLC slot
                        String textHashedFileName = Hasher.hash(String.Format("text/jp/dlc/bgm_{0}t.bin", dlcSlotNumber.ToString("D3"))) + ".edat";
                        String textHashedFilePath = System.IO.Path.Combine(controllerFolder, textHashedFileName);
                        if (File.Exists(textHashedFilePath))
                        {
                            bgmTitles = MessFileReader.decodeDLCText(@textHashedFilePath);
                        }
                    }

                    // Getting Form BGM entries
                    BindingList<FormBgmEntry> formEntries = new BindingList<FormBgmEntry>();
                    foreach (int i in Enumerable.Range(0, bgmTable.entries.Count))
                    {
                        BgmEntry entry = bgmTable.entries[i];
                        FormBgmEntry formEntry = new FormBgmEntry(entry);
                        String hashedInternalFileName = Hasher.hash(String.Format("bgm/{0}", entry.internalFileName)) + ".edat";
                        String hashedInternalFilePath = System.IO.Path.Combine(controllerFolder, hashedInternalFileName);
                        if (File.Exists(hashedInternalFilePath))
                        {
                            formEntry.filePath = hashedInternalFilePath;
                        }
                        if (bgmTitles.Count > 0 && bgmTitles.Count > i)
                        {
                            formEntry.bgmTitle = (String)bgmTitles[i];
                        }
                        formEntries.Add(formEntry);
                    }

                    // Set DLC slot if any
                    bgmGenDlcSlotComboBox.SelectedIndex = (dlcSlotNumber != -1)
                            ? dlcSlotNumber - 1
                            : 0;

                    // Bind data
                    bindDataAndEnableForm(formEntries);
                }
                catch (Exception ex)
                {
                    Logger.Log("MainFormBgmGenUserControl", ex);
                    MessageBoxEx.Show(this, "There was a problem while processing the BGM table. Make sure that you selected the right file");
                }
            }
        }
        private void saveDataToFiles()
        {
            try
            {
                // Validate data
                if (bgmGenDlcSlotComboBox.SelectedItem == null)
                {
                    MessageBoxEx.Show(this, "Select valid values in the highlighted fields.");
                    return;
                }

                int selectedBgmSlotIndex = bgmGenDlcSlotComboBox.SelectedIndex;
                if (selectedBgmSlotIndex < 0)
                {
                    MessageBoxEx.Show(this, "You have to select a valid DLC slot");
                    return;
                }
                int bgmDlcSlot = (int)bgmGenDlcSlotComboBox.SelectedIndex + 1;

                if (bgmFormEntries.Count <= 0)
                {
                    MessageBoxEx.Show(this, "You have to add at least one BGM to the list");
                    return;
                }

                // Get DLC folder
                String baseFolder = Settings.getDlcMainFolder();
                String dlcFolder = System.IO.Path.Combine(baseFolder,
                    "[BGM][Slot " + bgmGenDlcSlotComboBox.Text + "]");
                createDlcFolder(dlcFolder);

                // Prepare copy
                List<String> bgmNames = new List<String>();
                List<String> bgmFileNames = new List<String>();
                BgmTable bgmTable = new BgmTable();
                foreach (FormBgmEntry formEntry in bgmFormEntries)
                {
                    // Save text strings
                    bgmNames.Add(formEntry.bgmTitle);
                    bgmTable.addEntry(formEntry.entry);
                }

                // Ready bgmTable
                bgmTable.generateRandomEntryNamesAndIds(bgmDlcSlot);

                // Save bgm names
                String textHashedFileName = Hasher.hash(String.Format("text/jp/dlc/bgm_{0}t.bin", bgmDlcSlot.ToString("D3"))) + ".edat";
                String textHashedFilePath = System.IO.Path.Combine(dlcFolder, textHashedFileName);
                MessFileWriter.encodeDLCText(bgmNames, textHashedFilePath);

                // Save bgm controller
                String controllerHashedFileName = Hasher.hash(String.Format("dlc/bgm/dlc_{0}.bin", bgmDlcSlot.ToString("D3"))) + ".edat";
                String controllerHashedFilePath = System.IO.Path.Combine(dlcFolder, controllerHashedFileName);
                bgmTable.writeToFile(controllerHashedFilePath);

                // Copy bgm to dlc folder
                foreach (FormBgmEntry formEntry in bgmFormEntries)
                {
                    String hashedInternalFileName = Hasher.hash(String.Format("bgm/{0}", formEntry.entry.internalFileName)) + ".edat";
                    String hashedInternalFilePath = System.IO.Path.Combine(dlcFolder, hashedInternalFileName);
                    if (File.Exists(formEntry.filePath))
                    {
                        File.Copy(formEntry.filePath, hashedInternalFilePath, true);
                    }
                    bgmFileNames.Add(hashedInternalFileName);
                }

                // Generate readme
                if (Settings.getBgmReadmeEnabled())
                {
                    String readmeFilePath = System.IO.Path.Combine(dlcFolder, "readme.txt");
                    using (StreamWriter readmeFileWriter = new StreamWriter(new FileStream(readmeFilePath, FileMode.Create)))
                    {
                        readmeFileWriter.WriteLine("BGM DLC Slot " + bgmGenDlcSlotComboBox.Text);
                        readmeFileWriter.WriteLine("-----------------------");
                        for (int i = 0; i < bgmNames.Count; i++)
                        {
                            readmeFileWriter.WriteLine(bgmNames[i] + " -> " + bgmFileNames[i]);
                        }
                    }
                }

                MessageBoxEx.Show(this, "Success!");
            }
            catch (Exception ex)
            {
                MessageBoxEx.Show(this, "There was a problem saving the BGM files. Check 'log.txt' for more details");
                Logger.Log("MainFormBgmGenUserControl", ex);
                return;
            }
        }
        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");
            }
        }