Skip to content

Commit

Permalink
Better song cache file
Browse files Browse the repository at this point in the history
  • Loading branch information
EliteAsian123 committed Mar 26, 2023
1 parent 5049018 commit 556ebce
Show file tree
Hide file tree
Showing 10 changed files with 172 additions and 161 deletions.
98 changes: 25 additions & 73 deletions Assets/Script/Data/SongInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@
namespace YARG.Data {
[JsonObject(MemberSerialization.OptIn)]
public class SongInfo {
public enum DrumType {
FOUR_LANE,
FIVE_LANE,
UNKNOWN
}

private static readonly Dictionary<string, int> DEFAULT_DIFFS = new() {
{"guitar", -1},
{"bass", -1},
Expand All @@ -31,10 +37,7 @@ public class SongInfo {
/// </summary>
public DirectoryInfo realFolderRemote;

public bool BassPedal2xExpertPlus {
private set;
get;
}
[JsonProperty("live")]
public bool Live {
private set;
get;
Expand All @@ -44,12 +47,21 @@ public bool Live {
private string _songName;
public string SongNameWithFlags {
set {
const string BASS_PEDAL_SUFFIX = " (2x bass pedal expert+)";
BassPedal2xExpertPlus = value.ToLower().EndsWith(BASS_PEDAL_SUFFIX);
if (BassPedal2xExpertPlus) {
// We don't care about this...
const string BASS_PEDAL_EXPERT_SUFFIX = " (2x bass pedal expert+)";
bool present = value.ToLower().EndsWith(BASS_PEDAL_EXPERT_SUFFIX);
if (present) {
value = value[..^BASS_PEDAL_EXPERT_SUFFIX.Length];
}

// Or this...
const string BASS_PEDAL_SUFFIX = " (2x bass pedal)";
present = value.ToLower().EndsWith(BASS_PEDAL_SUFFIX);
if (present) {
value = value[..^BASS_PEDAL_SUFFIX.Length];
}

// But we do care about this!
const string LIVE_SUFFIX = " (live)";
Live = value.ToLower().EndsWith(LIVE_SUFFIX);
if (Live) {
Expand All @@ -65,70 +77,6 @@ public string SongName {
}
public string SongNameNoParen => SongName.Replace("(", "").Replace(")", "");

public bool WaveGroup {
private set;
get;
}

[JsonProperty("artist")]
private string _artistName;
public string ArtistName {
set {
const string WAVEGROUP = " (WaveGroup)";
WaveGroup = value.EndsWith(WAVEGROUP);
if (WaveGroup) {
value = value[..^WAVEGROUP.Length];
}

_artistName = value;
}
get => _artistName;
}

/// <value>
/// Used for JSON. Compresses flags (<see cref="BassPedal2xExpertPlus"/>, etc.) into a small string.
/// </value>
[JsonProperty("flags")]
public string JsonFlags {
get {
string o = "";

if (BassPedal2xExpertPlus) {
o += "B";
}
if (Live) {
o += "L";
}
if (WaveGroup) {
o += "W";
}

return o;
}
set {
string i = value;

if (i.StartsWith('B')) {
BassPedal2xExpertPlus = true;
i = i.Remove(0, 1);
} else {
BassPedal2xExpertPlus = false;
}
if (i.StartsWith('L')) {
Live = true;
i = i.Remove(0, 1);
} else {
Live = false;
}
if (i.StartsWith('W')) {
WaveGroup = true;
// i = i.Remove(0, 1);
} else {
WaveGroup = false;
}
}
}

/// <value>
/// Used for JSON. Compresses <see cref="partDifficulties"/> by getting rid of <c>-1</c>s.
/// </value>
Expand All @@ -149,7 +97,11 @@ public Dictionary<string, int> JsonDiffs {
public float songLength;
[JsonProperty]
public float delay;
[JsonProperty]
public DrumType drumType;

[JsonProperty]
public string artistName;
[JsonProperty]
public string album;
[JsonProperty]
Expand All @@ -171,10 +123,10 @@ public SongInfo(DirectoryInfo folder) {
var split = dirName.Split(" - ");
if (split.Length == 2) {
SongNameWithFlags = split[1];
ArtistName = split[0];
artistName = split[0];
} else {
SongNameWithFlags = dirName;
ArtistName = "Unknown";
artistName = "Unknown";
}

partDifficulties = new(DEFAULT_DIFFS);
Expand Down
2 changes: 1 addition & 1 deletion Assets/Script/Serialization/OuvertExport.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public static void ExportOuvertSongsTo(string path) {
foreach (var song in SongLibrary.Songs) {
songs.Add(new SongData {
songName = song.SongName,
artistName = song.ArtistName,
artistName = song.artistName,
album = song.album,
genre = song.genre,
charter = song.charter,
Expand Down
15 changes: 14 additions & 1 deletion Assets/Script/Serialization/SongIni.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public static SongInfo CompleteSongInfo(SongInfo song) {

// Set basic info
song.SongName = section["name"];
song.ArtistName = section["artist"];
song.artistName = section["artist"];

// Get other metadata
song.album = section.GetKeyData("album")?.Value;
Expand Down Expand Up @@ -76,6 +76,19 @@ public static SongInfo CompleteSongInfo(SongInfo song) {
LoadSongLengthFromAudio(song);
}

// Get drum type
if (section.ContainsKey("pro_drums") &&
section["pro_drums"].ToLowerInvariant() == "true") {

song.drumType = SongInfo.DrumType.FOUR_LANE;
} else if (section.ContainsKey("five_lane_drums") &&
section["five_lane_drums"].ToLowerInvariant() == "true") {

song.drumType = SongInfo.DrumType.FIVE_LANE;
} else {
song.drumType = SongInfo.DrumType.UNKNOWN;
}

// Get song delay (0 if none)
if (section.ContainsKey("delay")) {
int rawDelay = int.Parse(section["delay"]);
Expand Down
58 changes: 40 additions & 18 deletions Assets/Script/SongLibrary.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@

namespace YARG {
public static class SongLibrary {
private const int CACHE_VERSION = 1;
private class SongCacheJson {
public int version = CACHE_VERSION;
public List<SongInfo> songs;
}

public static DirectoryInfo songFolder = new(GetSongFolder());

public static float loadPercent = 0f;
Expand Down Expand Up @@ -57,22 +63,24 @@ public static bool FetchSongs() {
}

if (CacheFile.Exists || GameManager.client != null) {
ReadCache();
return true;
} else {
ThreadPool.QueueUserWorkItem(_ => {
Songs = new();

loadPercent = 0f;
CreateSongInfoFromFiles(songFolder);
loadPercent = 0.1f;
ReadSongIni();
loadPercent = 0.9f;
CreateCache();
loadPercent = 1f;
});
return false;
var success = ReadCache();
if (success) {
return true;
}
}

ThreadPool.QueueUserWorkItem(_ => {
Songs = new();

loadPercent = 0f;
CreateSongInfoFromFiles(songFolder);
loadPercent = 0.1f;
ReadSongIni();
loadPercent = 0.9f;
CreateCache();
loadPercent = 1f;
});
return false;
}

/// <summary>
Expand Down Expand Up @@ -114,7 +122,11 @@ private static void ReadSongIni() {
/// <see cref="Songs"/> is expected to be populated and filled with <see cref="ReadSongIni"/>.
/// </summary>
private static void CreateCache() {
var json = JsonConvert.SerializeObject(Songs, Formatting.Indented);
var jsonObj = new SongCacheJson {
songs = Songs
};

var json = JsonConvert.SerializeObject(jsonObj);
Directory.CreateDirectory(CacheFile.DirectoryName);
File.WriteAllText(CacheFile.ToString(), json.ToString());
}
Expand All @@ -123,9 +135,19 @@ private static void CreateCache() {
/// Reads the song cache so we don't need to read of a the <c>song.ini</c> files.<br/>
/// <see cref="CacheFile"/> should exist. If not, call <see cref="CreateCache"/>.
/// </summary>
private static void ReadCache() {
private static bool ReadCache() {
string json = File.ReadAllText(CacheFile.ToString());
Songs = JsonConvert.DeserializeObject<List<SongInfo>>(json);

try {
var jsonObj = JsonConvert.DeserializeObject<SongCacheJson>(json);
if (jsonObj.version != CACHE_VERSION) {
return false;
}
} catch (JsonException) {
return false;
}

return true;
}

/// <summary>
Expand Down
2 changes: 1 addition & 1 deletion Assets/Script/UI/GameUI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ private void Awake() {
}

private void Start() {
songTitle.text = $"{Play.song.SongName} - {Play.song.ArtistName}";
songTitle.text = $"{Play.song.SongName} - {Play.song.artistName}";
}

private void Update() {
Expand Down
2 changes: 1 addition & 1 deletion Assets/Script/UI/PostSong.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public class PostSong : MonoBehaviour {
private Transform scoreContainer;

private void OnEnable() {
header.text = $"{Play.song.SongName} - {Play.song.ArtistName}";
header.text = $"{Play.song.SongName} - {Play.song.artistName}";

// Create a score to push

Expand Down
4 changes: 2 additions & 2 deletions Assets/Script/UI/SelectedSongView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public void UpdateSongView(SongInfo songInfo) {

// Basic info
songName.text = songInfo.SongName;
artist.text = $"<i>{songInfo.ArtistName}</i>";
artist.text = $"<i>{songInfo.artistName}</i>";

// Song score
var score = ScoreManager.GetScore(songInfo);
Expand Down Expand Up @@ -176,7 +176,7 @@ public void PlaySong() {
}

public void SearchArtist() {
SongSelect.Instance.searchField.text = $"artist:{songInfo.ArtistName}";
SongSelect.Instance.searchField.text = $"artist:{songInfo.artistName}";
}

public void SearchSource() {
Expand Down
8 changes: 4 additions & 4 deletions Assets/Script/UI/SongSelect.cs
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ private void OnGenericNavigation(NavigationType navigationType, bool firstPresse
Back();
} else if (navigationType == NavigationType.TERTIARY) {
if (songs.Count > 0) {
searchField.text = $"artist:{songs[selectedSongIndex].song.ArtistName}";
searchField.text = $"artist:{songs[selectedSongIndex].song.artistName}";
}
}
}
Expand Down Expand Up @@ -261,7 +261,7 @@ private int FuzzySearch(string text, SongInfo song) {
if (dropdown.value == 0) {
return Fuzz.PartialRatio(song.SongName, text);
} else {
return Fuzz.PartialRatio(song.ArtistName, text);
return Fuzz.PartialRatio(song.artistName, text);
}
}

Expand Down Expand Up @@ -294,7 +294,7 @@ public void UpdateSearch() {

// Look all songs by artist
var sameArtistSongs = SongLibrary.Songs
.Where(i => i.ArtistName?.ToLower() == baseSong.ArtistName?.ToLower())
.Where(i => i.artistName?.ToLower() == baseSong.artistName?.ToLower())
.ToList();
if (sameArtistSongs.Count <= 1) {
continue;
Expand Down Expand Up @@ -361,7 +361,7 @@ public void UpdateSearch() {
// Artist filter
var artist = arg[7..];
songsOut = SongLibrary.Songs
.Where(i => i.ArtistName.ToLower() == artist.ToLower());
.Where(i => i.artistName.ToLower() == artist.ToLower());
} else if (arg.StartsWith("source:")) {
// Source filter
var source = arg[7..];
Expand Down
2 changes: 1 addition & 1 deletion Assets/Script/UI/SongView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public void UpdateSongView(SongInfo songInfo) {
background.SetActive(false);

songName.text = songInfo.SongName;
artist.text = $"<i>{songInfo.ArtistName}</i>";
artist.text = $"<i>{songInfo.artistName}</i>";

var score = ScoreManager.GetScore(songInfo);
if (score == null || score.highestPercent.Count <= 0) {
Expand Down
Loading

0 comments on commit 556ebce

Please sign in to comment.