fixed music list.

This commit is contained in:
Harrison Deng 2018-11-19 23:05:59 -06:00
parent 57e76be95d
commit 1bdda94905

View File

@ -1,6 +1,7 @@
using Microsoft.Xna.Framework.Media; using Microsoft.Xna.Framework.Media;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Diagnostics;
using System.IO; using System.IO;
using System.Linq; using System.Linq;
using System.Text; using System.Text;
@ -9,8 +10,11 @@ using System.Threading.Tasks;
namespace RhythmBullet.Zer01HD.Audio namespace RhythmBullet.Zer01HD.Audio
{ {
internal delegate void SearchListener(MusicList musicList);
internal class MusicList internal class MusicList
{ {
public event SearchListener SearchCompleteEvent;
private volatile bool work; private volatile bool work;
private List<string> list = new List<string>(); private List<string> list = new List<string>();
public List<string> List public List<string> List
@ -30,64 +34,88 @@ namespace RhythmBullet.Zer01HD.Audio
} }
} }
} }
private string path; public string path;
private Thread thread; 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) if (thread == null || !thread.IsAlive)
{ {
work = true; work = true;
thread = new Thread(Search); thread = new Thread(Search);
thread.IsBackground = true;
thread.Name = "Music Search Thread"; thread.Name = "Music Search Thread";
Debug.WriteLine("Starting music file search with path: " + path, GetType().Name);
thread.Start(); thread.Start();
} }
else
{
StopSearch();
StartSearch();
}
} }
public void Stop() public void StopSearch()
{ {
work = false; work = false;
thread.Join();
} }
private void Search() private void Search()
{ {
lock (list) lock (list)
{ {
list.Clear();
list.AddRange(recursiveMusicSearch(path)); list.AddRange(recursiveMusicSearch(path));
} }
OnSearchComplete();
Debug.WriteLine("Completed search. Found " + list.Count + " valid file(s).", GetType().Name);
} }
private List<string> recursiveMusicSearch(string path) private List<string> recursiveMusicSearch(string path)
{ {
List<string> musicFiles = new List<string>(); List<string> musicFiles = new List<string>();
string[] folders = Directory.GetDirectories(path);
string[] files = Directory.GetFiles(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<SupportedFormats>(extensionType, out format);
if (supported)
{ {
musicFiles.AddRange(recursiveMusicSearch(files[i])); musicFiles.Add(files[fileID]);
Debug.WriteLine("Music file found: " + files[fileID], GetType().Name);
} }
else else
{ {
try Debug.WriteLine("Unsupported file format: " + Path.GetFileName(files[fileID]), GetType().Name);
{
SupportedFormats format;
Enum.TryParse<SupportedFormats>(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);
}
} }
} }
return musicFiles; return musicFiles;
} }
private void OnSearchComplete()
{
SearchCompleteEvent?.Invoke(this);
}
} }
} }