From 1bdda949054b06037465b94809c2ce71da2354ee Mon Sep 17 00:00:00 2001 From: Recrown Date: Mon, 19 Nov 2018 23:05:59 -0600 Subject: [PATCH] fixed music list. --- RhythmBullet/Zer01HD/Audio/MusicList.cs | 64 ++++++++++++++++++------- 1 file changed, 46 insertions(+), 18 deletions(-) diff --git a/RhythmBullet/Zer01HD/Audio/MusicList.cs b/RhythmBullet/Zer01HD/Audio/MusicList.cs index 0f94897..877c6ef 100644 --- a/RhythmBullet/Zer01HD/Audio/MusicList.cs +++ b/RhythmBullet/Zer01HD/Audio/MusicList.cs @@ -1,6 +1,7 @@ using Microsoft.Xna.Framework.Media; using System; using System.Collections.Generic; +using System.Diagnostics; using System.IO; using System.Linq; using System.Text; @@ -9,8 +10,11 @@ using System.Threading.Tasks; namespace RhythmBullet.Zer01HD.Audio { + internal delegate void SearchListener(MusicList musicList); + internal class MusicList { + public event SearchListener SearchCompleteEvent; private volatile bool work; private List list = new List(); public List List @@ -30,64 +34,88 @@ namespace RhythmBullet.Zer01HD.Audio } } } - private string path; + public string path; private Thread thread; + public bool Searched + { + get + { + return (thread != null && !thread.IsAlive); + } + } - public MusicList() + internal MusicList() { } - public void Start() + public void StartSearch() { if (thread == null || !thread.IsAlive) { work = true; thread = new Thread(Search); + thread.IsBackground = true; thread.Name = "Music Search Thread"; + Debug.WriteLine("Starting music file search with path: " + path, GetType().Name); thread.Start(); } + else + { + StopSearch(); + StartSearch(); + } } - public void Stop() + public void StopSearch() { work = false; + thread.Join(); } private void Search() { lock (list) { + list.Clear(); list.AddRange(recursiveMusicSearch(path)); } + OnSearchComplete(); + Debug.WriteLine("Completed search. Found " + list.Count + " valid file(s).", GetType().Name); } private List recursiveMusicSearch(string path) { List musicFiles = new List(); + string[] folders = Directory.GetDirectories(path); string[] files = Directory.GetFiles(path); - for (int i = 0; i < files.Length && work; i++) + + for (int folderID = 0; folderID < folders.Length && work; folderID++) { - if (Directory.Exists(files[i])) + musicFiles.AddRange(recursiveMusicSearch(folders[folderID])); + } + + for (int fileID = 0; fileID < files.Length && work; fileID++) + { + string extensionType = Path.GetExtension(files[fileID]).ToUpper().Substring(1); + SupportedFormats format; + bool supported = Enum.TryParse(extensionType, out format); + if (supported) { - musicFiles.AddRange(recursiveMusicSearch(files[i])); + musicFiles.Add(files[fileID]); + Debug.WriteLine("Music file found: " + files[fileID], GetType().Name); } else { - try - { - SupportedFormats format; - Enum.TryParse(Path.GetExtension(files[i]).ToUpper(), out format); - musicFiles.Add(files[i]); - } - catch (ArgumentException e) - { - Console.WriteLine("MusicList", "Unsupported file format: " + Path.GetFileName(files[i])); - Console.WriteLine(e); - } + Debug.WriteLine("Unsupported file format: " + Path.GetFileName(files[fileID]), GetType().Name); } } return musicFiles; } + + private void OnSearchComplete() + { + SearchCompleteEvent?.Invoke(this); + } } }