Skip to content

Commit

Permalink
Song library is now loaded at the start
Browse files Browse the repository at this point in the history
  • Loading branch information
EliteAsian123 committed Mar 17, 2023
1 parent fcef602 commit e09ddab
Show file tree
Hide file tree
Showing 9 changed files with 83 additions and 157 deletions.
42 changes: 21 additions & 21 deletions Assets/Scenes/MenuScene.unity
Original file line number Diff line number Diff line change
Expand Up @@ -8740,15 +8740,15 @@ RectTransform:
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 784751310}
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
m_LocalRotation: {x: -0, y: -0, z: -0, w: 1}
m_LocalPosition: {x: 0, y: 0, z: 0}
m_LocalScale: {x: 1, y: 1, z: 1}
m_ConstrainProportionsScale: 0
m_Children:
- {fileID: 976689291}
- {fileID: 474114361}
m_Father: {fileID: 1458336823}
m_RootOrder: 5
m_Father: {fileID: 874813133}
m_RootOrder: 4
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
m_AnchorMin: {x: 0, y: 0}
m_AnchorMax: {x: 1, y: 1}
Expand Down Expand Up @@ -9627,6 +9627,7 @@ RectTransform:
- {fileID: 852858603}
- {fileID: 464394842}
- {fileID: 1695935344}
- {fileID: 784751311}
m_Father: {fileID: 0}
m_RootOrder: 0
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
Expand Down Expand Up @@ -9654,6 +9655,8 @@ MonoBehaviour:
editPlayers: {fileID: 1471919259}
addPlayer: {fileID: 410452336}
settingsMenu: {fileID: 1695935343}
loadingScreen: {fileID: 784751310}
progressBar: {fileID: 1649890904}
--- !u!1 &882788346
GameObject:
m_ObjectHideFlags: 0
Expand Down Expand Up @@ -11567,7 +11570,7 @@ MonoBehaviour:
m_OnCullStateChanged:
m_PersistentCalls:
m_Calls: []
m_text: Export Ouvert Song File
m_text: Generate OuvertBot Song List
m_isRightToLeft: 0
m_fontAsset: {fileID: 11400000, guid: ae170e91fd29a90479e906ddffb1d8ee, type: 2}
m_sharedMaterial: {fileID: -5480317595949376055, guid: ae170e91fd29a90479e906ddffb1d8ee,
Expand Down Expand Up @@ -14763,7 +14766,6 @@ RectTransform:
- {fileID: 1339554101}
- {fileID: 63677085}
- {fileID: 882788347}
- {fileID: 784751311}
m_Father: {fileID: 0}
m_RootOrder: 1
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
Expand All @@ -14790,8 +14792,6 @@ MonoBehaviour:
songListContent: {fileID: 396272154}
selectedSongView: {fileID: 2044165076}
dropdown: {fileID: 882788348}
loadingScreen: {fileID: 784751310}
progressBar: {fileID: 1649890904}
--- !u!1 &1471919255
GameObject:
m_ObjectHideFlags: 0
Expand Down Expand Up @@ -15225,7 +15225,19 @@ MonoBehaviour:
m_CharacterLimit: 0
m_OnEndEdit:
m_PersistentCalls:
m_Calls: []
m_Calls:
- m_Target: {fileID: 1695935347}
m_TargetAssemblyTypeName: YARG.UI.SettingsMenu, Assembly-CSharp
m_MethodName: SongFolderUpdate
m_Mode: 1
m_Arguments:
m_ObjectArgument: {fileID: 0}
m_ObjectArgumentAssemblyTypeName: UnityEngine.Object, UnityEngine
m_IntArgument: 0
m_FloatArgument: 0
m_StringArgument:
m_BoolArgument: 0
m_CallState: 2
m_OnSubmit:
m_PersistentCalls:
m_Calls: []
Expand All @@ -15243,19 +15255,7 @@ MonoBehaviour:
m_Calls: []
m_OnValueChanged:
m_PersistentCalls:
m_Calls:
- m_Target: {fileID: 1695935347}
m_TargetAssemblyTypeName: YARG.UI.SettingsMenu, Assembly-CSharp
m_MethodName: SongFolderUpdate
m_Mode: 1
m_Arguments:
m_ObjectArgument: {fileID: 0}
m_ObjectArgumentAssemblyTypeName: UnityEngine.Object, UnityEngine
m_IntArgument: 0
m_FloatArgument: 0
m_StringArgument:
m_BoolArgument: 0
m_CallState: 2
m_Calls: []
m_OnTouchScreenKeyboardStatusChanged:
m_PersistentCalls:
m_Calls: []
Expand Down
8 changes: 8 additions & 0 deletions Assets/Script/ScoreManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ public static void FetchScores() {
scores = new();

// Create a dummy score file if one doesn't exist.
Directory.CreateDirectory(ScoreFile.DirectoryName);
File.WriteAllText(ScoreFile.ToString(), "{}");
}
}
Expand Down Expand Up @@ -102,5 +103,12 @@ public static List<SongInfo> SongsByPlayCount() {
.Where(i => i != null)
.ToList();
}

/// <summary>
/// Force reset scores. This makes the game re-scan if needed.
/// </summary>
public static void Reset() {
scores = null;
}
}
}
10 changes: 10 additions & 0 deletions Assets/Script/Server/Client.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.Text;
using System.Threading;
using UnityEngine;
using YARG.UI;
using YARG.Util;

namespace YARG.Server {
Expand Down Expand Up @@ -64,6 +65,15 @@ private void ClientThread() {
// When done, dump all files into remote path
ZipFile.ExtractToDirectory(pkgZipPath, remotePath);

// Refresh library
SongLibrary.Reset();
SongLibrary.FetchSongs();
SongSelect.refreshFlag = true;

// Refresh scores
ScoreManager.Reset();
ScoreManager.FetchScores();

// Delete zip
File.Delete(pkgZipPath);

Expand Down
1 change: 1 addition & 0 deletions Assets/Script/SongLibrary.cs
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ private static void ReadSongIni() {
/// </summary>
private static void CreateCache() {
var json = JsonConvert.SerializeObject(Songs, Formatting.Indented);
Directory.CreateDirectory(CacheFile.DirectoryName);
File.WriteAllText(CacheFile.ToString(), json.ToString());
}

Expand Down
34 changes: 32 additions & 2 deletions Assets/Script/UI/MainMenu.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.IO;
using UnityEngine;
using UnityEngine.UI;
using YARG.Data;
using YARG.Input;

Expand Down Expand Up @@ -27,12 +28,19 @@ public static MainMenu Instance {
[SerializeField]
private Canvas addPlayer;

[Space]
[SerializeField]
private GameObject settingsMenu;
[SerializeField]
private GameObject loadingScreen;
[SerializeField]
private Image progressBar;

private void Start() {
Instance = this;

RefreshSongLibrary();

if (!isPostSong) {
ShowMainMenu();
} else {
Expand All @@ -58,6 +66,18 @@ private void OnDisable() {
}

private void Update() {
// Update progress if loading
if (loadingScreen.activeSelf) {
progressBar.fillAmount = SongLibrary.loadPercent;

// Finish loading
if (SongLibrary.loadPercent >= 1f) {
loadingScreen.SetActive(false);
}

return;
}

// Update player navigation
foreach (var player in PlayerManager.players) {
player.inputStrategy.UpdateNavigationMode();
Expand Down Expand Up @@ -133,11 +153,21 @@ public void ShowHostServerScene() {
public void RefreshCache() {
if (SongLibrary.CacheFile.Exists) {
File.Delete(SongLibrary.CacheFile.FullName);
SongLibrary.Reset();
ShowSongSelect();
RefreshSongLibrary();
}
}

public void RefreshSongLibrary() {
SongLibrary.Reset();
ScoreManager.Reset();

bool loading = !SongLibrary.FetchSongs();
loadingScreen.SetActive(loading);
ScoreManager.FetchScores();

SongSelect.refreshFlag = true;
}

public void Quit() {
Application.Quit();
}
Expand Down
3 changes: 1 addition & 2 deletions Assets/Script/UI/SettingsMenu.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,7 @@ public void SongFolderUpdate() {
if (GameManager.client == null) {
SongLibrary.songFolder = new(songFolderInput.text);
PlayerPrefs.SetString("songFolder", songFolderInput.text);

SongLibrary.Reset();
MainMenu.Instance.RefreshSongLibrary();
}
}

Expand Down
56 changes: 10 additions & 46 deletions Assets/Script/UI/SongSelect.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
using TMPro;
using UnityEngine;
using UnityEngine.InputSystem;
using UnityEngine.UI;
using YARG.Data;
using YARG.Input;

Expand All @@ -15,6 +14,8 @@ public static SongSelect Instance {
private set;
} = null;

public static bool refreshFlag = true;

private const int SONG_VIEW_EXTRA = 6;
private const float INPUT_REPEAT_TIME = 0.035f;
private const float INPUT_REPEAT_COOLDOWN = 0.5f;
Expand All @@ -36,12 +37,6 @@ private class SongOrHeader {
[SerializeField]
private TMP_Dropdown dropdown;

[Space]
[SerializeField]
private GameObject loadingScreen;
[SerializeField]
private Image progressBar;

private List<SongOrHeader> songs;
private List<SongInfo> recommendedSongs;

Expand All @@ -53,14 +48,9 @@ private class SongOrHeader {
// Will be set in UpdateSearch
private int selectedSongIndex;

private void Start() {
private void Awake() {
Instance = this;

// Fetch info
bool loading = !SongLibrary.FetchSongs();
loadingScreen.SetActive(loading);
ScoreManager.FetchScores();

// Create before (insert backwards)
for (int i = 0; i < SONG_VIEW_EXTRA; i++) {
var gameObject = Instantiate(songViewPrefab, songListContent);
Expand All @@ -76,11 +66,6 @@ private void Start() {

songViewsAfter.Add(gameObject.GetComponent<SongView>());
}

if (!loading) {
// Automatically loads songs and updates song views
UpdateSearch();
}
}

private void OnEnable() {
Expand All @@ -89,18 +74,13 @@ private void OnEnable() {
player.inputStrategy.GenericNavigationEvent += OnGenericNavigation;
}

// Refetch if null (i.e. if a setting changed)
// Make sure that we have already initialized (hence "songViewsBefore")
if (SongLibrary.Songs == null && songViewsBefore.Count > 0) {
bool loading = !SongLibrary.FetchSongs();
loadingScreen.SetActive(loading);
ScoreManager.FetchScores();

if (!loading) {
// Automatically loads songs and updates song views
recommendedSongs = null;
UpdateSearch();
}
if (refreshFlag) {
songs = null;
recommendedSongs = null;

// Get songs
UpdateSearch();
refreshFlag = false;
}
}

Expand Down Expand Up @@ -163,22 +143,6 @@ private void UpdateSongViews() {
private void Update() {
GameManager.client?.CheckForSignals();

// Update progress if loading

if (loadingScreen.activeSelf) {
progressBar.fillAmount = SongLibrary.loadPercent;

// Finish loading
if (SongLibrary.loadPercent >= 1f) {
loadingScreen.SetActive(false);

recommendedSongs = null;
UpdateSearch();
}

return;
}

// Update input timer

inputTimer -= Time.deltaTime;
Expand Down
Loading

0 comments on commit e09ddab

Please sign in to comment.