diff --git a/PCK-Studio/Forms/Editor/AudioEditor.cs b/PCK-Studio/Forms/Editor/AudioEditor.cs index 095ec1b5..97df4da8 100644 --- a/PCK-Studio/Forms/Editor/AudioEditor.cs +++ b/PCK-Studio/Forms/Editor/AudioEditor.cs @@ -4,6 +4,7 @@ using System.IO; using System.Linq; using System.Threading.Tasks; using System.Windows.Forms; +using System.Security.Cryptography; using System.Diagnostics; using MetroFramework.Forms; using PckStudio.Classes; @@ -199,19 +200,51 @@ namespace PckStudio.Forms.Editor if (Path.GetExtension(file) == ".binka" || Path.GetExtension(file) == ".wav") { string new_loc = Path.Combine(parent.GetDataPath(), Path.GetFileNameWithoutExtension(file) + ".binka"); - bool duplicate_song = false; // To handle if a file already in the pack is dropped back in - if (File.Exists(new_loc)) + string songName = Path.GetFileNameWithoutExtension(file); + bool is_duplicate_file = false; // To handle if a file already in the pack is dropped back in + bool loc_is_occupied = File.Exists(new_loc); + if (loc_is_occupied) { - duplicate_song = File.ReadAllBytes(file) == File.ReadAllBytes(new_loc); - if (duplicate_song) + FileStream fs1 = File.OpenRead(file); + FileStream fs2 = File.OpenRead(new_loc); + + string hash1_str = BitConverter.ToString(MD5.Create().ComputeHash(fs1)); + string hash2_str = BitConverter.ToString(MD5.Create().ComputeHash(fs2)); + + // close the file streams after calculating the hash + fs1.Close(); + fs2.Close(); + + is_duplicate_file = hash1_str == hash2_str; + + string diag_text = "A file named \"" + Path.GetFileNameWithoutExtension(file) + ".binka\" already exists in the Data folder."; + + if (is_duplicate_file) diag_text = "\"" + Path.GetFileNameWithoutExtension(file) + ".binka\" has an identical copy present in the Data folder."; + + diag_text += " Pressing yes will replace the existing file. By pressing no, the song entry will be added without affecting the file." + + "You can also cancel this operation and all files in queue."; + + DialogResult user_prompt = MessageBox.Show(diag_text, "File already exists", MessageBoxButtons.YesNoCancel); + while (user_prompt == DialogResult.None) ; // Stops the editor from adding or processing the file until the user has made their choice + if (user_prompt == DialogResult.Cancel) { - DialogResult user_prompt = MessageBox.Show("\"" + Path.GetFileNameWithoutExtension(file) + ".binka\" already exists. Continuing will replace the existing file. Are you sure you want to continue moving the file? By pressing no, the song entry will be added without moving the file. You can also cancel this operation and all files in queue.", "File already exists", MessageBoxButtons.YesNoCancel); - while (user_prompt == DialogResult.None) ; // Stops the editor from adding or processing the file until the user had made their choice - if (user_prompt == DialogResult.Cancel) + break; + } + else if (user_prompt == DialogResult.No) + { + if (treeView1.SelectedNode is TreeNode node && node.Tag is PCKAudioFile.AudioCategory cat) { - break; + //adds song without affecting the binka file + cat.SongNames.Add(songName); + treeView2.Nodes.Add(songName); } - else if (user_prompt == DialogResult.No) continue; + continue; + } + else if (user_prompt == DialogResult.Yes) + { + // deletes the file so that the copy function can happen safely + // and ignore duplicate files because well... they're duplicates lol + if (File.Exists(new_loc) && !is_duplicate_file) File.Delete(new_loc); } } @@ -234,10 +267,10 @@ namespace PckStudio.Forms.Editor } // if the file is NOT a .wav and doesn't exist, copy the file - else if (!File.Exists(Path.Combine(parent.GetDataPath(), Path.GetFileName(file)))) - File.Copy(file, Path.Combine(parent.GetDataPath(), Path.GetFileName(file))); + else if (!File.Exists(new_loc)) + File.Copy(file, new_loc); - var songName = Path.GetFileNameWithoutExtension(file); + // this is repeated again becuase this is meant to prevent any files that fail to convert from being added to the category if (treeView1.SelectedNode is TreeNode t && t.Tag is PCKAudioFile.AudioCategory category) { category.SongNames.Add(songName);