private void Worker(object threadInfo) { string[] invalidString = { "\\", "/", ":", "*", "?", "\"", "<", ">", "|" }; IsWorking = true; var info = (RateChangerThreadInput)threadInfo; try { ThreadData.Directory = info.Path; ThreadData.OutputDir = info.OutPutDir; ThreadData.OsuNameList = Directory.GetFiles(ThreadData.Directory, "*.osu").ToList(); ThreadData.Map = new List <Beatmap>(); foreach (var cur in ThreadData.OsuNameList) { var map = Parser.LoadOsuFile(Path.Combine(ThreadData.Directory, cur)); if (map == null) { _exceptFileList.Add(Path.GetFileNameWithoutExtension(cur).Split('[')[1].Split(']')[0]); } else { ThreadData.Map.Add(map); } } ThreadData.Mp3NameList = Directory.GetFiles(ThreadData.Directory, "*.mp3").ToList(); ThreadData.Rate = info.Rate; ThreadData.Nightcore = false; ThreadData.NewOsuNameList = new List <string>(); foreach (var map in ThreadData.Map) { var tempName = map.Meta.Artist + " - " + map.Meta.Title + " (" + map.Meta.Creator + ") [" + map.Meta.Version + " x" + ThreadData.Rate + (ThreadData.Nightcore ? "_P" : "") + "].osu"; tempName = invalidString.Aggregate(tempName, (current, cur) => current.Replace(cur, "")); ThreadData.NewOsuNameList.Add(tempName); } } catch (Exception e) { IsErrorOccurred = true; Log.Error(e, "Error occurred in parsing ThreadData."); throw; } var mp3ThreadList = ThreadData.Mp3NameList.Select(cur => new Thread(() => Mp3Change(cur))).ToList(); var patternThreadList = ThreadData.Map.Select((map, i) => new Thread(() => PatternChange(map, ThreadData.NewOsuNameList[i]))).ToList(); foreach (var thread in mp3ThreadList) { thread.Start(); } foreach (var thread in patternThreadList) { thread.Start(); } foreach (var thread in mp3ThreadList) { thread.Join(); } foreach (var thread in patternThreadList) { thread.Join(); } var delFiles = new List <string>(ThreadData.NewOsuNameList); delFiles.AddRange(ThreadData.Mp3NameList.Select(cur => Path.GetFileNameWithoutExtension(cur) + "_" + ThreadData.Rate + (ThreadData.Nightcore ? "_P" : "") + ".mp3")); if (IsErrorOccurred) { foreach (var cur in delFiles) { File.Delete(Path.Combine(ThreadData.Directory, cur)); } } else { string[] exts = { ".osu", ".mp3", ".osb" }; if (info.OszChecked) { var newDir = Path.GetFileName(ThreadData.Directory) + " x" + ThreadData.Rate + (ThreadData.Nightcore ? "_P" : ""); var zipFile = newDir + ".osz"; string newPath, zipPath; foreach (var cur in invalidString) { newDir = newDir.Replace(cur, ""); zipFile = zipFile.Replace(cur, ""); } if (ThreadData.OutputDir == ThreadData.Directory) { newPath = Path.Combine(Path.GetDirectoryName(ThreadData.Directory), newDir); zipPath = Path.Combine(Path.GetDirectoryName(ThreadData.Directory), zipFile); } else { newPath = Path.Combine(ThreadData.OutputDir, newDir); zipPath = Path.Combine(ThreadData.OutputDir, zipFile); } DirectoryUtil.CopyFolder(ThreadData.Directory, newPath, exts); foreach (var cur in delFiles) { File.Move(Path.Combine(ThreadData.Directory, cur), Path.Combine(newPath, cur)); } ZipUtil.CreateFromDirectory(newPath, zipPath, true); Directory.Delete(newPath, true); _resultFile = zipPath; } else if (ThreadData.OutputDir != ThreadData.Directory) { DirectoryUtil.CopyFolder(ThreadData.Directory, ThreadData.OutputDir, exts); foreach (var cur in delFiles) { File.Move(Path.Combine(ThreadData.Directory, cur), Path.Combine(ThreadData.OutputDir, cur)); } } } IsErrorOccurred = IsWorking = false; }
private void Worker() { var path = ""; bool?oszChecked = false; bool?endSnapChecked = false; int beatDivisior = 4; string[] invalidString = { "\\", "/", ":", "*", "?", "\"", "<", ">", "|" }; _isWorking = true; try { Dispatcher.Invoke(DispatcherPriority.Normal, new Action(delegate { path = PathTextBox.Text; oszChecked = OszCheckBox.IsChecked; endSnapChecked = EndSnapCheckBox.IsChecked; if (Beat16.IsChecked == true) { beatDivisior = 4; } else if (Beat24.IsChecked == true) { beatDivisior = 6; } else if (Beat32.IsChecked == true) { beatDivisior = 8; } GlobalData.OutputDir = string.IsNullOrEmpty(DirTextBox.Text) ? Path.GetDirectoryName(path) : DirTextBox.Text; })); GlobalData.Directory = Path.GetDirectoryName(path); GlobalData.OsuName = Path.GetFileName(path); GlobalData.Map = new BeatmapInfo(path); GlobalData.NewOsuName = GlobalData.Map.Meta.Artist + " - " + GlobalData.Map.Meta.Title + " (" + GlobalData.Map.Meta.Creator + ") [" + GlobalData.Map.Meta.Version + "_J].osu"; foreach (var cur in invalidString) { GlobalData.NewOsuName = GlobalData.NewOsuName.Replace(cur, ""); } } catch (Exception ex) { MessageBox.Show(ex.Message, @"Error occurred!", MessageBoxButtons.OK, MessageBoxIcon.Error); _isErrorOccurred = _isWorking = false; return; } if (GlobalData.Map.Gen.Mode != 3) { MessageBox.Show(@"This program is ONLY for Mania maps!", @"LN2Jack", MessageBoxButtons.OK); _isErrorOccurred = _isWorking = false; return; } _isErrorOccurred = PatternChange(endSnapChecked ?? false, beatDivisior); string[] delFiles = { GlobalData.NewOsuName }; if (_isErrorOccurred) { MessageBox.Show(@"An error occurred. Please try again.", @"LN2Jack", MessageBoxButtons.OK, MessageBoxIcon.Error); foreach (var cur in delFiles) { File.Delete(Path.Combine(GlobalData.Directory, cur)); } } else { string[] exts = { ".osu" }; if (oszChecked == true) { var newDir = GlobalData.Map.Meta.Artist + " - " + GlobalData.Map.Meta.Title + "_J"; var zipFile = GlobalData.Map.Meta.Artist + " - " + GlobalData.Map.Meta.Title + "_J.osz"; string newPath, zipPath; foreach (var cur in invalidString) { newDir = newDir.Replace(cur, ""); zipFile = zipFile.Replace(cur, ""); } if (GlobalData.OutputDir == GlobalData.Directory) { newPath = Path.Combine(Path.GetDirectoryName(GlobalData.Directory), newDir); zipPath = Path.Combine(Path.GetDirectoryName(GlobalData.Directory), zipFile); } else { newPath = Path.Combine(GlobalData.OutputDir, newDir); zipPath = Path.Combine(GlobalData.OutputDir, zipFile); } DirectoryUtil.CopyFolder(GlobalData.Directory, newPath, exts); foreach (var cur in delFiles) { File.Move(Path.Combine(GlobalData.Directory, cur), Path.Combine(newPath, cur)); } ZipFile.CreateFromDirectory(newPath, zipPath); Directory.Delete(newPath, true); } else if (GlobalData.OutputDir != GlobalData.Directory) { DirectoryUtil.CopyFolder(GlobalData.Directory, GlobalData.OutputDir, exts); foreach (var cur in delFiles) { File.Move(Path.Combine(GlobalData.Directory, cur), Path.Combine(GlobalData.OutputDir, cur)); } } MessageBox.Show(@"Finished!", @"LN2Jack", MessageBoxButtons.OK, MessageBoxIcon.None); } Dispatcher.Invoke(DispatcherPriority.Normal, new Action(delegate { Ln2JackWindow.Title = "LN2Jack by CloudHolic"; PathTextBox.Text = ""; if (GlobalData.OutputDir == GlobalData.Directory) { DirTextBox.Text = ""; } })); _isErrorOccurred = _isWorking = false; }