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; }
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}"); }