Bläddra i källkod

Fixed crash on settings change.
Ported some fixes from Downloader regarding Playlists.
Version bump.

Stephen Damm 6 år sedan
förälder
incheckning
3cfd906d0c

+ 8 - 23
SongBrowserPlugin/DataAccess/Network/Downloader.cs

@@ -143,23 +143,23 @@ namespace SongBrowserPlugin
                 {
                     yield return new WaitForSecondsRealtime(0.25f);
                 }
-                ExtractZipAsync(songInfo, zipStream, customSongsPath);
+
+                yield return new WaitWhile(() => _extractingZip); //because extracting several songs at once sometimes hangs the game
+
+                Task extract = ExtractZipAsync(songInfo, zipStream, customSongsPath);
+                yield return new WaitWhile(() => !extract.IsCompleted);
+                songDownloaded?.Invoke(songInfo);
             }
         }
 
-        private async void ExtractZipAsync(Song songInfo, Stream zipStream, string customSongsPath)
+        private async Task ExtractZipAsync(Song songInfo, Stream zipStream, string customSongsPath)
         {
-
             try
             {
-                while (_extractingZip)
-                {
-                    Thread.Sleep(250);
-                }
                 Logger.Info("Extracting...");
                 _extractingZip = true;
                 ZipArchive archive = new ZipArchive(zipStream, ZipArchiveMode.Read);
-                await Task.Run(() => archive.ExtractToDirectory(customSongsPath)); //ZipFile.ExtractToDirectory(zipPath, customSongsPath));
+                await Task.Run(() => archive.ExtractToDirectory(customSongsPath)).ConfigureAwait(false);
                 archive.Dispose();
                 zipStream.Close();
             }
@@ -178,25 +178,10 @@ namespace SongBrowserPlugin
                 songInfo.path = customSongsPath;
             }
 
-            /*
-            try
-            {
-                await Task.Run(() => File.Delete(zipPath));
-            }
-            catch (IOException e)
-            {
-                Logger.Warning($"Unable delete zip! Exception: {e}");
-                songInfo.songQueueState = SongQueueState.Error;
-                _extractingZip = false;
-                return;
-            }*/
-
             _extractingZip = false;
             songInfo.songQueueState = SongQueueState.Downloaded;
             _alreadyDownloadedSongs.Add(songInfo);
             Logger.Info($"Extracted {songInfo.songName} {songInfo.songSubName}!");
-
-            songDownloaded?.Invoke(songInfo);
         }
 
         public bool DeleteSong(Song song)

+ 18 - 16
SongBrowserPlugin/Plugin.cs

@@ -2,12 +2,13 @@
 using IllusionPlugin;
 using UnityEngine;
 using SongBrowserPlugin.UI;
+using Logger = SongBrowserPlugin.Logging.Logger;
 
 namespace SongBrowserPlugin
 {
     public class Plugin : IPlugin
     {
-        public const string VERSION_NUMBER = "v2.4.3";
+        public const string VERSION_NUMBER = "v2.4.4";
 
         public string Name
         {
@@ -21,39 +22,40 @@ namespace SongBrowserPlugin
 
         public void OnApplicationStart()
         {
-            SceneEvents _sceneEvents;
-            _sceneEvents = new GameObject("menu-signal").AddComponent<SceneEvents>();
-            _sceneEvents.MenuSceneEnabled += OnMenuSceneEnabled;
+            SceneManager.sceneLoaded += SceneManager_sceneLoaded;
+            SceneManager.activeSceneChanged += SceneManager_activeSceneChanged;
 
             Base64Sprites.Init();
         }
 
-        private void OnMenuSceneEnabled()
+        private void SceneManager_activeSceneChanged(Scene from, Scene to)
         {
-            SongBrowserApplication.OnLoad();
+            Logger.Info($"Active scene changed from \"{from.name}\" to \"{to.name}\"");
+
+            if (from.name == "EmptyTransition" && to.name.Contains("Menu"))
+            {
+                OnMenuSceneEnabled();
+            }
         }
 
-        public void OnApplicationQuit()
+        private void SceneManager_sceneLoaded(Scene to, LoadSceneMode loadMode)
         {
-
+            Logger.Debug($"Loaded scene \"{to.name}\"");
         }
 
-        private void SceneManagerOnActiveSceneChanged(Scene arg0, Scene scene)
+        private void OnMenuSceneEnabled()
         {
-        
+            SongBrowserApplication.OnLoad();
         }
 
-        private void SceneManager_sceneLoaded(Scene arg0, LoadSceneMode arg1)
+        public void OnApplicationQuit()
         {
+
         }
 
         public void OnLevelWasLoaded(int level)
         {
-            /*if (SceneManager.GetSceneByBuildIndex(level).name == "Menu")
-            {
-                SongBrowserApplication.OnLoad();
-                Downloader.OnLoad();
-            }*/
+
         }
 
         public void OnLevelWasInitialized(int level)

+ 21 - 0
SongBrowserPlugin/SongBrowserApplication.cs

@@ -225,6 +225,27 @@ namespace SongBrowserPlugin
             {
                 InvokeBeatSaberButton("SoloFreePlayButton");
             }
+
+            if (Input.GetKeyDown(KeyCode.Alpha0))
+            {
+                InvokeBeatSaberButton("SettingsButton");
+            }
+
+            if (Input.GetKeyDown(KeyCode.Alpha9))
+            {
+                InvokeBeatSaberButton("ApplyButton");
+            }
+
+            if (Input.GetKeyDown(KeyCode.Alpha8))
+            {
+                Console.WriteLine("CLICKING OK BUTTON");
+                var settings = Resources.FindObjectsOfTypeAll<VRUI.VRUIViewController>().First(x => x.name == "SettingsViewController");
+                var button = settings.GetComponentsInChildren<Button>().Where(x => x.name == "OkButton");
+                foreach (Button b in button)
+                {
+                    b.onClick.Invoke();
+                }                
+            }
         }
     }
 }

+ 17 - 3
SongBrowserPlugin/UI/DownloadQueue/DownloadQueueViewController.cs

@@ -3,6 +3,7 @@ using CustomUI.Utilities;
 using HMUI;
 using SongBrowserPlugin.DataAccess.BeatSaverApi;
 using System;
+using System.Collections;
 using System.Collections.Generic;
 using System.Linq;
 using TMPro;
@@ -33,6 +34,8 @@ namespace SongBrowserPlugin.UI.DownloadQueue
         {
             if (firstActivation && type == ActivationType.AddedToHierarchy)
             {
+                Downloader.Instance.songDownloaded += SongDownloaded;
+
                 _songListTableCellInstance = Resources.FindObjectsOfTypeAll<LevelListTableCell>().First(x => (x.name == "LevelListTableCell"));
 
                 _titleText = BeatSaberUI.CreateText(rectTransform, "DOWNLOAD QUEUE", new Vector2(0f, 36f));
@@ -126,10 +129,19 @@ namespace SongBrowserPlugin.UI.DownloadQueue
             }
         }
 
-        void DownloadSong(Song song)
+        IEnumerator DownloadSong(Song song)
+        {
+            yield return Downloader.Instance.DownloadSongCoroutine(song);
+            Refresh();
+        }
+
+        private void SongDownloaded(Song obj)
         {
-            StartCoroutine(Downloader.Instance.DownloadSongCoroutine(song));
             Refresh();
+            if (queuedSongs.Count(x => x.songQueueState == SongQueueState.Downloading) < PluginConfig.MaxSimultaneousDownloads && queuedSongs.Any(x => x.songQueueState == SongQueueState.Queued))
+            {
+                StartCoroutine(DownloadSong(queuedSongs.First(x => x.songQueueState == SongQueueState.Queued)));
+            }
         }
 
         public void Refresh()
@@ -148,7 +160,9 @@ namespace SongBrowserPlugin.UI.DownloadQueue
             }
 
             if (queuedSongs.Count(x => x.songQueueState == SongQueueState.Downloading) < PluginConfig.MaxSimultaneousDownloads && queuedSongs.Any(x => x.songQueueState == SongQueueState.Queued))
-                DownloadSong(queuedSongs.First(x => x.songQueueState == SongQueueState.Queued));
+            {
+                StartCoroutine(DownloadSong(queuedSongs.First(x => x.songQueueState == SongQueueState.Queued)));
+            }
         }
 
         public float RowHeight()

+ 20 - 7
SongBrowserPlugin/UI/Playlists/PlaylistFlowCoordinator.cs

@@ -50,12 +50,15 @@ namespace SongBrowserPlugin.UI
         /// </summary>
         public void Awake()
         {
-            _playlistNavigationController = BeatSaberUI.CreateViewController<BackButtonNavigationController>();
+            if (_playlistNavigationController == null)
+            {
+                _playlistNavigationController = BeatSaberUI.CreateViewController<BackButtonNavigationController>();
 
-            GameObject _playlistDetailGameObject = Instantiate(Resources.FindObjectsOfTypeAll<StandardLevelDetailViewController>().First(), _playlistNavigationController.rectTransform, false).gameObject;
-            _playlistDetailViewController = _playlistDetailGameObject.AddComponent<PlaylistDetailViewController>();
-            Destroy(_playlistDetailGameObject.GetComponent<StandardLevelDetailViewController>());
-            _playlistDetailViewController.name = "PlaylistDetailViewController";
+                GameObject _playlistDetailGameObject = Instantiate(Resources.FindObjectsOfTypeAll<StandardLevelDetailViewController>().First(), _playlistNavigationController.rectTransform, false).gameObject;
+                _playlistDetailViewController = _playlistDetailGameObject.AddComponent<PlaylistDetailViewController>();
+                Destroy(_playlistDetailGameObject.GetComponent<StandardLevelDetailViewController>());
+                _playlistDetailViewController.name = "PlaylistDetailViewController";
+            }
         }
 
         /// <summary>
@@ -101,7 +104,7 @@ namespace SongBrowserPlugin.UI
                 }
 
                 _downloadingPlaylist = false;
-                _playlistListViewController.SetContent(_playlistsReader.Playlists, _lastPlaylist);
+                _playlistListViewController.SetContent(_playlistsReader.Playlists);
 
                 DownloadQueueViewController.allSongsDownloaded += HandleAllSongsDownloaded;
             }
@@ -153,6 +156,11 @@ namespace SongBrowserPlugin.UI
                 {
                     DownloadQueueViewController.AbortDownloads();
 
+                    if (_playlistNavigationController.viewControllers.IndexOf(_playlistDetailViewController) >= 0)
+                    {
+                        PopViewControllerFromNavigationController(_playlistNavigationController, null, true);
+                    }
+
                     ParentFlowCoordinator.InvokePrivateMethod("DismissFlowCoordinator", new object[] { this, null, false });
                     didFinishEvent?.Invoke(p);
                 }
@@ -175,6 +183,11 @@ namespace SongBrowserPlugin.UI
                     DownloadQueueViewController.AbortDownloads();
                     SongLoader.Instance.RefreshSongs(false);
 
+                    if (_playlistNavigationController.viewControllers.IndexOf(_playlistDetailViewController) >= 0)
+                    {
+                        PopViewControllerFromNavigationController(_playlistNavigationController, null, true);
+                    }
+
                     ParentFlowCoordinator.InvokePrivateMethod("DismissFlowCoordinator", new object[] { this, null, false });
                     didFinishEvent?.Invoke(null);
                 }
@@ -275,7 +288,7 @@ namespace SongBrowserPlugin.UI
                         songName = item.SongName,
                         id = item.Key,
                         downloadingProgress = 0f,
-                        hash = item.LevelId,
+                        hash = (item.LevelId == null ? "" : item.LevelId),
                         downloadUrl = archiveUrl
                     };
                 }