Exemple #1
0
        private void SceneManagerOnActiveSceneChanged(Scene arg0, Scene scene)
        {
            SongListUtils.Initialize();

            if (scene.name == "GameCore")
            {
                _isInGame = true;
            }
            if (scene.name != "Menu")
            {
                return;
            }
            _isInGame = false;

            _standardLevelSelectionFlowCoordinator = Resources.FindObjectsOfTypeAll <SoloFreePlayFlowCoordinator>().First();
            if (!_standardLevelSelectionFlowCoordinator)
            {
                return;
            }

            _standardLevelListViewController = ReflectionUtil.GetPrivateField <LevelListViewController>(_standardLevelSelectionFlowCoordinator, "_levelListViewController");
            if (!_standardLevelListViewController)
            {
                return;
            }

            _standardLevelListViewController.didSelectLevelEvent += standardLevelListViewController_didSelectLevelEvent;
        }
Exemple #2
0
        public static bool ScrollToLevel(string levelID)
        {
            var table = ReflectionUtil.GetPrivateField <LevelListTableView>(_standardLevelListViewController, "_levelListTableView");

            if (table)
            {
                RemoveDuplicates();

                TableView tableView = table.GetComponentInChildren <TableView>();
                tableView.ReloadData();

                var levels = CurrentLevels.Where(l => l.levelID == levelID).ToArray();
                if (levels.Length > 0)
                {
                    int row = table.RowNumberForLevelID(levelID);
                    tableView.SelectRow(row, true);
                    tableView.ScrollToRow(row, true);
                    return(true);
                }
            }
            var tempLevels = SongLoader.CustomLevels.Where(l => l.levelID == levelID).ToArray();

            foreach (CustomLevel l in tempLevels)
            {
                SongLoader.CustomLevels.Remove(l);
            }

            Plugin.Log($"Failed to scroll to {levelID}!");
            return(false);
        }
Exemple #3
0
        private IEnumerator DelayedActiveSceneChanged(Scene scene)
        {
            yield return(new WaitForSeconds(0.1f));

            SongListUtils.Initialize();

            if (scene.name == "GameCore")
            {
                _isInGame = true;
            }
            if (scene.name != "MenuCore")
            {
                yield break;
            }
            _isInGame = false;

            _standardLevelSelectionFlowCoordinator = Resources.FindObjectsOfTypeAll <SoloFreePlayFlowCoordinator>().First();
            if (!_standardLevelSelectionFlowCoordinator)
            {
                yield break;
            }

            _standardLevelListViewController = ReflectionUtil.GetPrivateField <LevelPackLevelsViewController>(_standardLevelSelectionFlowCoordinator, "_levelPackLevelsViewController");
            if (!_standardLevelListViewController)
            {
                yield break;
            }

            _standardLevelListViewController.didSelectLevelEvent -= standardLevelListViewController_didSelectLevelEvent;
            _standardLevelListViewController.didSelectLevelEvent += standardLevelListViewController_didSelectLevelEvent;
        }
Exemple #4
0
        private IEnumerator RefreshSongs(bool fullRefresh = false, bool selectOldLevel = true)
        {
            if (_isRefreshing)
            {
                yield break;
            }
            _isRefreshing = true;

            if (!SongLoader.AreSongsLoaded)
            {
                yield break;
            }

            // Grab the currently selected level id so we can restore it after refreshing
            string selectedLevelId = _standardLevelListViewController.selectedLevel?.levelID;

            // Wait until song loader is finished loading, then refresh the song list
            while (SongLoader.AreSongsLoading)
            {
                yield return(null);
            }
            SongLoader.Instance.RefreshSongs(fullRefresh);
            while (SongLoader.AreSongsLoading)
            {
                yield return(null);
            }

            // If song browser is installed, update/refresh it
            if (_songBrowserInstalled)
            {
                RefreshSongBrowser();
            }

            var table = ReflectionUtil.GetPrivateField <LevelListTableView>(_standardLevelListViewController, "_levelListTableView");

            if (table)
            {
                // Set the row index to the previously selected song
                if (selectOldLevel)
                {
                    int       row       = table.RowNumberForLevelID(selectedLevelId);
                    TableView tableView = table.GetComponentInChildren <TableView>();
                    tableView.SelectRow(row, true);
                    tableView.ScrollToRow(row, true);
                }
            }
            _isRefreshing = false;
        }
Exemple #5
0
        private bool ScrollToLevel(string levelID)
        {
            var table = ReflectionUtil.GetPrivateField <LevelListTableView>(_standardLevelListViewController, "_levelListTableView");

            if (table)
            {
                TableView tableView = table.GetComponentInChildren <TableView>();
                tableView.ReloadData();

                var levels = CurrentLevels.Where(l => l.levelID == levelID).ToArray();
                if (levels.Length > 0)
                {
                    int row = table.RowNumberForLevelID(levelID);
                    tableView.SelectRow(row, true);
                    tableView.ScrollToRow(row, true);
                    Plugin.Log($"Success scrolling to {levelID}!");
                    return(true);
                }
            }
            Plugin.Log($"Failed to scroll to {levelID}!");
            return(false);
        }
Exemple #6
0
        private IEnumerator UpdateSong(KeyValuePair <JSONObject, CustomLevel> songUpdateInfo)
        {
            _downloaderRunning = true;
            JSONObject  song      = songUpdateInfo.Key;
            CustomLevel oldLevel  = songUpdateInfo.Value;
            string      songIndex = song["version"];
            string      songHash  = ((string)song["hashMd5"]).ToUpper();

            Utilities.EmptyDirectory(".songcache", false);

            var table = ReflectionUtil.GetPrivateField <LevelListTableView>(_standardLevelListViewController, "_levelListTableView");

            if (Config.DeleteOldVersions)
            {
                string        songPath = oldLevel.customSongInfo.path;
                DirectoryInfo parent   = Directory.GetParent(songPath);
                while (parent.Name != "CustomSongs")
                {
                    songPath = parent.FullName;
                    parent   = parent.Parent;
                }

                // Only delete the old song after the new one is downloaded and extracted
                Utilities.EmptyDirectory(songPath, true);
                SongLoader.Instance.RemoveSongWithLevelID(oldLevel.levelID);
            }

            string currentSongDirectory = $"{Environment.CurrentDirectory}\\CustomSongs\\{songIndex}";

            if (Directory.Exists(currentSongDirectory))
            {
                Utilities.EmptyDirectory(currentSongDirectory, true);
            }

            // Download and extract the update
            string localPath = $"{Environment.CurrentDirectory}\\.songcache\\{songIndex}.zip";

            yield return(Utilities.DownloadFile(song["downloadUrl"], localPath));

            yield return(Utilities.ExtractZip(localPath, currentSongDirectory));

            _standardLevelListViewController.didSelectLevelEvent -= standardLevelListViewController_didSelectLevelEvent;
            yield return(SongListUtils.RefreshSongs(false, false));

            _standardLevelListViewController.didSelectLevelEvent += standardLevelListViewController_didSelectLevelEvent;

            Plugin.Log("Finished refreshing songs!");
            // Try to scroll to the newly updated level, if it exists in the list
            var levels = SongLoader.CustomLevels.Where(l => l.levelID.StartsWith(songHash)).ToArray();

            if (levels.Length > 0)
            {
                Plugin.Log($"Scrolling to level {levels[0].levelID}");
                if (!SongListUtils.ScrollToLevel(levels[0].levelID))
                {
                    if (table)
                    {
                        var lvls = CurrentLevels;
                        lvls.Add(levels[0]);
                        table.SetLevels(lvls.ToArray());
                    }
                    SongListUtils.ScrollToLevel(levels[0].levelID);
                }
            }

            // Write our download history to file
            if (!_songDownloadHistory.Contains(songIndex))
            {
                _songDownloadHistory.Add(songIndex);
            }
            Utilities.WriteStringListSafe(_historyPath, _songDownloadHistory.Distinct().ToList());

            DisplayNotification("Song update complete!");
            Plugin.Log($"Success updating song {songIndex}");
            _downloaderRunning = false;
        }
Exemple #7
0
        private IEnumerator UpdateSong(KeyValuePair <string, CustomLevel> songInfo)
        {
            string      songIndex = songInfo.Key;
            CustomLevel oldLevel  = songInfo.Value;

            Utilities.EmptyDirectory(".songcache", false);

            // Download and extract the update
            string localPath = $"{Environment.CurrentDirectory}\\.songcache\\{songIndex}.zip";

            yield return(Utilities.DownloadFile($"https://beatsaver.com/download/{songIndex}", localPath));

            yield return(Utilities.ExtractZip(localPath, $"{Environment.CurrentDirectory}\\CustomSongs\\{songIndex}"));

            var table = ReflectionUtil.GetPrivateField <LevelListTableView>(_standardLevelListViewController, "_levelListTableView");

            if (Config.DeleteOldVersions)
            {
                // Only delete the old song after the new one is downloaded and extracted
                Directory.Delete(oldLevel.customSongInfo.path, true);
                SongLoader.Instance.RemoveSongWithLevelID(oldLevel.levelID);

                if (_songBrowserInstalled)
                {
                    if (table)
                    {
                        var levels = CurrentLevels;
                        levels.Remove(oldLevel);
                        table.SetLevels(levels.ToArray());
                    }
                }
            }

            // Disable our didSelectLevel event, then refresh the song list
            _standardLevelListViewController.didSelectLevelEvent -= standardLevelListViewController_didSelectLevelEvent;
            yield return(RefreshSongs(!_songBrowserInstalled, false));

            _standardLevelListViewController.didSelectLevelEvent += standardLevelListViewController_didSelectLevelEvent;

            Plugin.Log("Finished refreshing songs!");
            try
            {
                // Try to scroll to the newly updated level, if it exists in the list
                CustomLevel newLevel = (CustomLevel)CurrentLevels.Where(x => x is CustomLevel && ((CustomLevel)x).customSongInfo.path.Contains(songIndex))?.FirstOrDefault();
                if (newLevel)
                {
                    Plugin.Log("Found new level!");
                    if (table)
                    {
                        // Set the row index to the previously selected song
                        int       row       = table.RowNumberForLevelID(newLevel.levelID);
                        TableView tableView = table.GetComponentInChildren <TableView>();
                        tableView.SelectRow(row, true);
                        tableView.ScrollToRow(row, true);
                    }
                }
                else
                {
                    Plugin.Log("Failed to find new level!");
                }
            }
            catch (Exception ex)
            {
                Plugin.Log($"Exception when attempting to find new song! {ex.ToString()}");
            }

            // Write our download history to file
            if (!_songDownloadHistory.Contains(songIndex))
            {
                _songDownloadHistory.Add(songIndex);
            }
            Utilities.WriteStringListSafe(_historyPath, _songDownloadHistory.Distinct().ToList());

            DisplayNotification("Song update complete!");
            Plugin.Log($"Success updating song {songIndex}");
        }