Skip to content

Commit

Permalink
Implement methods for retrieving and loading sources
Browse files Browse the repository at this point in the history
  • Loading branch information
TheNathannator committed Apr 12, 2023
1 parent 1691fb3 commit 7b5da7f
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 0 deletions.
4 changes: 4 additions & 0 deletions Assets/Script/Data/SongInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,10 @@ public SongInfo Duplicate() {
/// The converted short name (gh1) into the game name (Guitar Hero 1).
/// </returns>
private static string SourceToGameName(string source) {
if (SongLibrary.SourceNames != null && SongLibrary.SourceNames.TryGetValue(source, out string name)) {
return name;
}

#pragma warning disable format
return source switch {
"gh1" or "gh" => "Guitar Hero",
Expand Down
90 changes: 90 additions & 0 deletions Assets/Script/SongLibrary.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Security.Cryptography;
using System.Threading;
using Newtonsoft.Json;
Expand Down Expand Up @@ -35,6 +36,16 @@ public static string[] SongFolders {
/// </value>
public static string CacheFolder => Path.Combine(GameManager.PersistentDataPath, "caches");

/// <value>
/// The location of the local sources file.
/// </value>
public static string SourcesFile => Path.Combine(GameManager.PersistentDataPath, "sources.txt");

/// <value>
/// The URL of the Clone Hero sources list.
/// </value>
public const string SourcesUrl = "https://sources.clonehero.net/sources.txt";

/// <value>
/// A list of all of the playable songs.<br/>
/// You must call <see cref="FetchAllSongs"/> first.
Expand All @@ -53,6 +64,15 @@ public static Dictionary<string, SongInfo> SongsByHash {
private set;
} = null;

/// <value>
/// A list of all of the playable songs, where keys are hashes.<br/>
/// You must call <see cref="FetchSongSources"/> first.
/// </value>
public static Dictionary<string, string> SourceNames {
get;
private set;
} = null;

/// <summary>
/// Should be called before you access <see cref="SongsByHash"/>.
/// </summary>
Expand Down Expand Up @@ -110,6 +130,27 @@ public static void FetchAllSongs() {
});
}

/// <summary>
/// Should be called before you access <see cref="SourceNames"/>.
/// </summary>
public static void FetchSongSources() {
SourceNames = new();

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

try {
FetchSources();
loadPercent = 0.9f;
ReadSources();
} catch (Exception e) {
Debug.LogError($"Error while fetching sources: {e}");
}

loadPercent = 1f;
});
}

/// <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 @@ -259,6 +300,55 @@ private static bool ReadCache(string cacheFile) {
return true;
}

private static bool FetchSources() {
try {
// Retrieve sources file
var request = WebRequest.Create(SourcesUrl);
request.UseDefaultCredentials = true;
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}");
return false;
}

return true;
}

/// <summary>
/// Reads the locally-cached sources file.<br/>
/// Populates <see cref="SourceNames"/>
/// </summary>
private static bool ReadSources() {
if (!File.Exists(SourcesFile)) {
return false;
}

SourceNames ??= new();
SourceNames.Clear();
var sources = File.ReadAllText(SourcesFile).Split("\n");
foreach (string source in sources) {
if (string.IsNullOrWhiteSpace(source)) {
continue;
}

// The sources are formatted as follows:
// iconName '=' Display Name
var pair = source.Split("'='", 2);
if (pair.Length < 2) {
Debug.LogWarning($"Invalid source entry when reading sources: {source}");
continue;
}
SourceNames.Add(pair[0].Trim(), pair[1].Trim());
}

return true;
}

/// <summary>
/// Force reset songs. This makes the game re-scan if needed.
/// </summary>
Expand Down

0 comments on commit 7b5da7f

Please sign in to comment.