Skip to content

Commit

Permalink
Song folder caching
Browse files Browse the repository at this point in the history
  • Loading branch information
EliteAsian123 committed Nov 20, 2022
1 parent 3447b7e commit 5f85328
Show file tree
Hide file tree
Showing 12 changed files with 129 additions and 28 deletions.
7 changes: 5 additions & 2 deletions Assets/Script/Game.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,17 @@
using UnityEngine;
using UnityEngine.InputSystem;
using UnityEngine.Networking;
using YARG.Serialization;

namespace YARG {
public class Game : MonoBehaviour {
public static readonly DirectoryInfo SONG_FOLDER = new(@"B:\Clone Hero Alpha\Songs");
public static readonly FileInfo CACHE_FILE = new(Path.Combine(SONG_FOLDER.ToString(), "yarg_cache.json"));
public const float HIT_MARGIN = 0.075f;

public static DirectoryInfo song = new(@"B:\Clone Hero Alpha\Songs\Jane's Addiction - Been Caught Stealing");
public static bool botMode = false;

public const float HIT_MARGIN = 0.075f;

[SerializeField]
private GameObject soundAudioPrefab;
[SerializeField]
Expand Down
8 changes: 8 additions & 0 deletions Assets/Script/Serialization.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 16 additions & 0 deletions Assets/Script/Serialization/DirectoryInfoConverter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using System;
using System.IO;
using Newtonsoft.Json;

namespace YARG.Serialization {
public sealed class DirectoryInfoConverter : JsonConverter<DirectoryInfo> {
public override DirectoryInfo ReadJson(JsonReader reader, Type objectType, DirectoryInfo existingValue, bool hasExistingValue, JsonSerializer serializer) {
var path = serializer.Deserialize<string>(reader);
return new DirectoryInfo(path);
}

public override void WriteJson(JsonWriter writer, DirectoryInfo value, JsonSerializer serializer) {
serializer.Serialize(writer, value.FullName);
}
}
}
11 changes: 11 additions & 0 deletions Assets/Script/Serialization/DirectoryInfoConverter.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
using Melanchall.DryWetMidi.Interaction;
using Melanchall.DryWetMidi.MusicTheory;

namespace YARG {
namespace YARG.Serialization {
public static class Parser {
public static void Parse(string midiFile, out List<NoteInfo> chart, out List<EventInfo> chartEvents) {
var midi = MidiFile.Read(midiFile);
Expand Down
File renamed without changes.
30 changes: 24 additions & 6 deletions Assets/Script/UI/Menu.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Linq;
using System.Threading.Tasks;
using IniParser;
using Newtonsoft.Json;
using TMPro;
using UnityEngine;
using UnityEngine.InputSystem;
Expand All @@ -26,15 +27,25 @@ public class Menu : MonoBehaviour {

private async void Start() {
if (songs == null) {
FetchSongs();
if (Game.CACHE_FILE.Exists) {
await Task.Run(() => FetchSongsFromCache());
} else {
FetchSongs();
await Task.Run(() => FetchSongInfo());
}
}

await Task.Run(() => FetchSongInfo());
songs = songs.OrderBy(song => song.SongNameNoParen).ToList();

// Spawn song infos
songInfoComponents = new();
char currentSection = ' ';
foreach (var song in songs) {
// Skip errored songs
if (song.errored) {
continue;
}

char section = SongNameToLetterSection(song.SongNameNoParen);
if (section != currentSection) {
currentSection = section;
Expand Down Expand Up @@ -73,7 +84,7 @@ public void UpdateBotMode() {
}

private static void FetchSongs() {
var songFolder = new DirectoryInfo(@"B:\Clone Hero Alpha\Songs");
var songFolder = Game.SONG_FOLDER;
var directories = songFolder.GetDirectories();

songs = new(directories.Length);
Expand All @@ -83,8 +94,8 @@ private static void FetchSongs() {
}

private static void FetchSongInfo() {
// Fetch song info manually
var parser = new FileIniDataParser();

foreach (var song in songs) {
if (song.fetched) {
return;
Expand All @@ -109,9 +120,16 @@ private static void FetchSongInfo() {
} catch {
song.errored = true;
}

break;
}

// Create cache
var json = JsonConvert.SerializeObject(songs, Formatting.Indented);
File.WriteAllText(Game.CACHE_FILE.ToString(), json.ToString());
}

private static void FetchSongsFromCache() {
string json = File.ReadAllText(Game.CACHE_FILE.ToString());
songs = JsonConvert.DeserializeObject<List<SongInfo>>(json);
}

private static char SongNameToLetterSection(string nameNoParen) {
Expand Down
9 changes: 9 additions & 0 deletions Assets/Script/UI/SongInfo.cs
Original file line number Diff line number Diff line change
@@ -1,20 +1,29 @@
using System.IO;
using Newtonsoft.Json;
using YARG.Serialization;

namespace YARG.UI {
[JsonObject(MemberSerialization.Fields)]
public class SongInfo {
[JsonConverter(typeof(DirectoryInfoConverter))]
public DirectoryInfo folder;
[JsonIgnore]
public bool fetched;
[JsonIgnore]
public bool errored;

[field: JsonProperty("bassPedal2xExpertPlus")]
public bool BassPedal2xExpertPlus {
private set;
get;
}
[field: JsonProperty("live")]
public bool Live {
private set;
get;
}

[JsonProperty("songName")]
private string _songName;
public string SongName {
set {
Expand Down
4 changes: 3 additions & 1 deletion Packages/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@
"com.unity.ide.rider": "3.0.15",
"com.unity.ide.visualstudio": "2.0.16",
"com.unity.ide.vscode": "1.2.5",
"com.unity.inputsystem": "1.4.1",
"com.unity.inputsystem": "1.4.4",
"com.unity.nuget.newtonsoft-json": "3.0.2",
"com.unity.probuilder": "5.0.6",
"com.unity.render-pipelines.universal": "14.0.3",
"com.unity.test-framework": "1.1.31",
"com.unity.textmeshpro": "3.0.6",
"com.unity.timeline": "1.7.1",
"com.unity.toolchain.win-x86_64-linux-x86_64": "2.0.3",
"com.unity.ugui": "1.0.0",
"com.unity.visualscripting": "1.7.8",
"com.unity.modules.ai": "1.0.0",
Expand Down
30 changes: 28 additions & 2 deletions Packages/packages-lock.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@
"url": "https://packages.unity.com"
},
"com.unity.inputsystem": {
"version": "1.4.1",
"version": "1.4.4",
"depth": 0,
"source": "registry",
"dependencies": {
Expand All @@ -77,7 +77,7 @@
},
"com.unity.nuget.newtonsoft-json": {
"version": "3.0.2",
"depth": 2,
"depth": 0,
"source": "registry",
"dependencies": {},
"url": "https://packages.unity.com"
Expand Down Expand Up @@ -149,6 +149,22 @@
"com.unity.searcher": "4.9.2"
}
},
"com.unity.sysroot": {
"version": "2.0.4",
"depth": 1,
"source": "registry",
"dependencies": {},
"url": "https://packages.unity.com"
},
"com.unity.sysroot.linux-x86_64": {
"version": "2.0.3",
"depth": 1,
"source": "registry",
"dependencies": {
"com.unity.sysroot": "2.0.4"
},
"url": "https://packages.unity.com"
},
"com.unity.test-framework": {
"version": "1.1.31",
"depth": 0,
Expand Down Expand Up @@ -181,6 +197,16 @@
},
"url": "https://packages.unity.com"
},
"com.unity.toolchain.win-x86_64-linux-x86_64": {
"version": "2.0.3",
"depth": 0,
"source": "registry",
"dependencies": {
"com.unity.sysroot": "2.0.4",
"com.unity.sysroot.linux-x86_64": "2.0.3"
},
"url": "https://packages.unity.com"
},
"com.unity.ugui": {
"version": "1.0.0",
"depth": 0,
Expand Down
24 changes: 8 additions & 16 deletions ProjectSettings/PackageManagerSettings.asset
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,12 @@ MonoBehaviour:
m_Script: {fileID: 13964, guid: 0000000000000000e000000000000000, type: 0}
m_Name:
m_EditorClassIdentifier:
m_EnablePreviewPackages: 0
m_EnablePackageDependencies: 0
m_EnablePreReleasePackages: 1
m_AdvancedSettingsExpanded: 1
m_ScopedRegistriesSettingsExpanded: 1
oneTimeWarningShown: 0
m_SeeAllPackageVersions: 0
m_DismissPreviewPackagesInUse: 0
oneTimeWarningShown: 1
m_Registries:
- m_Id: main
m_Name:
Expand All @@ -27,17 +28,8 @@ MonoBehaviour:
m_UserSelectedRegistryName:
m_UserAddingNewScopedRegistry: 0
m_RegistryInfoDraft:
m_ErrorMessage:
m_Original:
m_Id:
m_Name:
m_Url:
m_Scopes: []
m_IsDefault: 0
m_Capabilities: 0
m_Modified: 0
m_Name:
m_Url:
m_Scopes:
-
m_SelectedScopeIndex: 0
m_ErrorMessage:
m_UserModificationsInstanceId: -840
m_OriginalInstanceId: -842
m_LoadAssets: 0
16 changes: 16 additions & 0 deletions ProjectSettings/TimelineSettings.asset
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
%YAML 1.1
%TAG !u! tag:unity3d.com,2011:
--- !u!114 &1
MonoBehaviour:
m_ObjectHideFlags: 61
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 0}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: a287be6c49135cd4f9b2b8666c39d999, type: 3}
m_Name:
m_EditorClassIdentifier:
assetDefaultFramerate: 60
m_DefaultFrameRate: 60

0 comments on commit 5f85328

Please sign in to comment.