Skip to content

Commit

Permalink
Add FetchEverything method to SongLibrary
Browse files Browse the repository at this point in the history
  • Loading branch information
TheNathannator committed Apr 12, 2023
1 parent 6e17aa3 commit 2cadc29
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 49 deletions.
129 changes: 81 additions & 48 deletions Assets/Script/SongLibrary.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,57 +73,39 @@ public static Dictionary<string, string> SourceNames {
private set;
} = null;

/// <summary>
/// Should be called before you access <see cref="SongsByHash"/>.
/// </summary>
public static void FetchAllSongs() {
if (!Directory.Exists(CacheFolder)) {
Directory.CreateDirectory(CacheFolder);
}
public static void FetchEverything() {
ThreadPool.QueueUserWorkItem(_ => {
loadPercent = 0f;
try {
LoadSongs();
} catch (Exception e) {
Debug.LogError($"Error while loading songs: {e}");
}

SongsByHash = new();
songFoldersToLoad = new(SongFolders.Where(i => !string.IsNullOrEmpty(i)));
loadPercent = 0f;
try {
FetchSources();
loadPercent = 0.9f;
ReadSources();
} catch (Exception e) {
Debug.LogError($"Error while fetching sources: {e}");
}

if (songFoldersToLoad.Count <= 0) {
loadPercent = 1f;
return;
}
});
}

/// <summary>
/// Should be called before you access <see cref="SongsByHash"/>.
/// </summary>
public static void FetchAllSongs() {
ThreadPool.QueueUserWorkItem(_ => {
while (songFoldersToLoad.Count > 0) {
string folderPath = songFoldersToLoad.Dequeue();

Debug.Log($"Fetching songs from: `{folderPath}`.");

string folderHash = Utils.Hash(folderPath);
string cachePath = Path.Combine(CacheFolder, folderHash + ".json");

if (File.Exists(cachePath)) {
var success = ReadCache(cachePath);
if (success) {
continue;
}
}

try {
songsTemp = new();

// Find songs
loadPercent = 0f;
CreateSongInfoFromFiles(folderPath, new(folderPath));

// Read song.ini and hashes
loadPercent = 0.1f;
ReadSongIni();
GetSongHashes();
loadPercent = 0f;

// Populate SongsByHash, and create cache
loadPercent = 0.9f;
PopulateSongByHashes();
CreateCache(folderPath, cachePath);
} catch (Exception e) {
Debug.LogException(e);
}
try {
LoadSongs();
} catch (Exception e) {
Debug.LogError($"Error while loading songs: {e}");
}

loadPercent = 1f;
Expand All @@ -134,8 +116,6 @@ public static void FetchAllSongs() {
/// Should be called before you access <see cref="SourceNames"/>.
/// </summary>
public static void FetchSongSources() {
SourceNames = new();

ThreadPool.QueueUserWorkItem(_ => {
loadPercent = 0f;

Expand All @@ -151,6 +131,56 @@ public static void FetchSongSources() {
});
}

private static void LoadSongs() {
if (!Directory.Exists(CacheFolder)) {
Directory.CreateDirectory(CacheFolder);
}

SongsByHash = new();
songFoldersToLoad = new(SongFolders.Where(i => !string.IsNullOrEmpty(i)));

if (songFoldersToLoad.Count <= 0) {
loadPercent = 1f;
return;
}

while (songFoldersToLoad.Count > 0) {
string folderPath = songFoldersToLoad.Dequeue();

Debug.Log($"Fetching songs from: `{folderPath}`.");

string folderHash = Utils.Hash(folderPath);
string cachePath = Path.Combine(CacheFolder, folderHash + ".json");

if (File.Exists(cachePath)) {
var success = ReadCache(cachePath);
if (success) {
continue;
}
}

try {
songsTemp = new();

// Find songs
loadPercent = 0f;
CreateSongInfoFromFiles(folderPath, new(folderPath));

// Read song.ini and hashes
loadPercent = 0.1f;
ReadSongIni();
GetSongHashes();

// Populate SongsByHash, and create cache
loadPercent = 0.9f;
PopulateSongByHashes();
CreateCache(folderPath, cachePath);
} catch (Exception e) {
Debug.LogException(e);
}
}
}

/// <summary>
/// Populate <see cref="SongsByHash"/> with <see cref="SongFolder"/> contents.<br/>
/// This is create a basic <see cref="SongInfo"/> object for each song.<br/>
Expand Down Expand Up @@ -301,18 +331,21 @@ private static bool ReadCache(string cacheFile) {
}

private static bool FetchSources() {
Debug.Log("Fetching list of sources.");

try {
// Retrieve sources file
var request = WebRequest.Create(SourcesUrl);
request.UseDefaultCredentials = true;
request.Timeout = 10000;
using var response = request.GetResponse();
using var responseReader = new StreamReader(response.GetResponseStream());

// Store sources locally and load them
string text = responseReader.ReadToEnd();
File.WriteAllText(SourcesFile, text);
} catch (Exception e) {
Debug.LogError($"Error while fetching sources: {e}");
Debug.LogException(e);
return false;
}

Expand Down
2 changes: 1 addition & 1 deletion Assets/Script/UI/MainMenu.cs
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ public void RefreshSongLibrary() {
SongLibrary.Reset();
ScoreManager.Reset();

SongLibrary.FetchAllSongs();
SongLibrary.FetchEverything();
loadingScreen.SetActive(true);
ScoreManager.FetchScores();

Expand Down

0 comments on commit 2cadc29

Please sign in to comment.