public IEnumerator GetInfoForSong(Playlist playlist, PlaylistSong song, Action <Song> songCallback) { string url = ""; bool _usingHash = false; if (!string.IsNullOrEmpty(song.key)) { url = $"{PluginConfig.beatsaverURL}/api/songs/detail/{song.key}"; if (!string.IsNullOrEmpty(playlist.customDetailUrl)) { url = playlist.customDetailUrl + song.key; } } else if (!string.IsNullOrEmpty(song.hash)) { url = $"{PluginConfig.beatsaverURL}/api/songs/search/hash/{song.hash}"; _usingHash = true; } else if (!string.IsNullOrEmpty(song.levelId)) { string hash = CustomHelpers.CheckHex(song.levelId.Substring(0, Math.Min(32, song.levelId.Length))); url = $"{PluginConfig.beatsaverURL}/api/songs/search/hash/{hash}"; _usingHash = true; } else { yield break; } UnityWebRequest www = UnityWebRequest.Get(url); www.timeout = 15; yield return(www.SendWebRequest()); if (www.isNetworkError || www.isHttpError) { Logger.Error($"Unable to connect to {PluginConfig.beatsaverURL}! " + (www.isNetworkError ? $"Network error: {www.error}" : (www.isHttpError ? $"HTTP error: {www.error}" : "Unknown error"))); } else { try { JSONNode node = JSON.Parse(www.downloadHandler.text); if (_usingHash) { if (node["songs"].Count == 0) { Logger.Error($"Song {song.songName} doesn't exist on BeatSaver!"); songCallback?.Invoke(null); yield break; } songCallback?.Invoke(Song.FromSearchNode(node["songs"][0])); } else { songCallback?.Invoke(new Song(node["song"])); } } catch (Exception e) { Logger.Exception("Unable to parse response! Exception: " + e); } } }
/// <summary> /// Get the song cache from the game. /// </summary> public void UpdateLevelRecords() { Stopwatch timer = new Stopwatch(); timer.Start(); // Calculate some information about the custom song dir String customSongsPath = Path.Combine(Environment.CurrentDirectory, CUSTOM_SONGS_DIR); String revSlashCustomSongPath = customSongsPath.Replace('\\', '/'); double currentCustomSongDirLastWriteTIme = (File.GetLastWriteTimeUtc(customSongsPath) - EPOCH).TotalMilliseconds; bool customSongDirChanged = false; if (_customSongDirLastWriteTime != currentCustomSongDirLastWriteTIme) { customSongDirChanged = true; _customSongDirLastWriteTime = currentCustomSongDirLastWriteTIme; } if (!Directory.Exists(customSongsPath)) { Logger.Error("CustomSong directory is missing..."); return; } IEnumerable <string> directories = Directory.EnumerateDirectories(customSongsPath, "*.*", SearchOption.AllDirectories); // Get LastWriteTimes Stopwatch lastWriteTimer = new Stopwatch(); lastWriteTimer.Start(); foreach (var level in SongLoader.CustomLevels) { // If we already know this levelID, don't both updating it. // SongLoader should filter duplicates but in case of failure we don't want to crash if (!_cachedLastWriteTimes.ContainsKey(level.levelID) || customSongDirChanged) { // Always use the newest date. var lastWriteTime = File.GetLastWriteTimeUtc(level.customSongInfo.path); var lastCreateTime = File.GetCreationTimeUtc(level.customSongInfo.path); var lastTime = lastWriteTime > lastCreateTime ? lastWriteTime : lastCreateTime; _cachedLastWriteTimes[level.levelID] = (lastTime - EPOCH).TotalMilliseconds; } if (!_levelIdToCustomLevel.ContainsKey(level.levelID)) { _levelIdToCustomLevel.Add(level.levelID, level); } if (!_levelIdToSongVersion.ContainsKey(level.levelID)) { DirectoryInfo info = new DirectoryInfo(level.customSongInfo.path); string currentDirectoryName = info.Name; String version = level.customSongInfo.path.Replace(revSlashCustomSongPath, "").Replace(currentDirectoryName, "").Replace("/", ""); if (!String.IsNullOrEmpty(version)) { _levelIdToSongVersion.Add(level.levelID, version); if (!_keyToSong.ContainsKey(version)) { _keyToSong.Add(version, level); } } } } lastWriteTimer.Stop(); Logger.Info("Determining song download time and determining mappings took {0}ms", lastWriteTimer.ElapsedMilliseconds); // Update song Infos, directory tree, and sort this.UpdateScoreSaberDataMapping(); this.UpdatePlayCounts(); // Check if we need to upgrade settings file favorites try { this.Settings.ConvertFavoritesToPlaylist(_levelIdToCustomLevel, _levelIdToSongVersion); } catch (Exception e) { Logger.Exception("FAILED TO CONVERT FAVORITES TO PLAYLIST!", e); } // load the current editing playlist or make one if (CurrentEditingPlaylist == null && !String.IsNullOrEmpty(this.Settings.currentEditingPlaylistFile)) { Logger.Debug("Loading playlist for editing: {0}", this.Settings.currentEditingPlaylistFile); CurrentEditingPlaylist = Playlist.LoadPlaylist(this.Settings.currentEditingPlaylistFile); PlaylistsCollection.MatchSongsForPlaylist(CurrentEditingPlaylist); } if (CurrentEditingPlaylist == null) { Logger.Debug("Current editing playlist does not exit, create..."); CurrentEditingPlaylist = new Playlist { playlistTitle = "Song Browser Favorites", playlistAuthor = "SongBrowser", fileLoc = this.Settings.currentEditingPlaylistFile, image = Base64Sprites.PlaylistIconB64, songs = new List <PlaylistSong>(), }; } CurrentEditingPlaylistLevelIds = new HashSet <string>(); foreach (PlaylistSong ps in CurrentEditingPlaylist.songs) { // Sometimes we cannot match a song if (ps.level == null) { continue; } CurrentEditingPlaylistLevelIds.Add(ps.level.levelID); } // Actually sort and filter //this.ProcessSongList(); // Signal complete if (SongLoader.CustomLevels.Count > 0) { didFinishProcessingSongs?.Invoke(SongLoader.CustomLevels); } timer.Stop(); Logger.Info("Updating songs infos took {0}ms", timer.ElapsedMilliseconds); }
/// <summary> /// Get the song cache from the game. /// </summary> public void UpdateLevelRecords() { Stopwatch timer = new Stopwatch(); timer.Start(); // Calculate some information about the custom song dir String customSongsPath = Path.Combine(Environment.CurrentDirectory, CUSTOM_SONGS_DIR); String revSlashCustomSongPath = customSongsPath.Replace('\\', '/'); double currentCustomSongDirLastWriteTIme = (File.GetLastWriteTimeUtc(customSongsPath) - EPOCH).TotalMilliseconds; bool customSongDirChanged = false; if (_customSongDirLastWriteTime != currentCustomSongDirLastWriteTIme) { customSongDirChanged = true; _customSongDirLastWriteTime = currentCustomSongDirLastWriteTIme; } if (!Directory.Exists(customSongsPath)) { Logger.Error("CustomSong directory is missing..."); return; } // Map some data for custom songs Regex r = new Regex(@"(\d+-\d+)", RegexOptions.IgnoreCase); Stopwatch lastWriteTimer = new Stopwatch(); lastWriteTimer.Start(); foreach (KeyValuePair <string, CustomPreviewBeatmapLevel> level in SongCore.Loader.CustomLevels) { // If we already know this levelID, don't both updating it. // SongLoader should filter duplicates but in case of failure we don't want to crash if (!_cachedLastWriteTimes.ContainsKey(level.Value.levelID) || customSongDirChanged) { double lastWriteTime = GetSongUserDate(level.Value); _cachedLastWriteTimes[level.Value.levelID] = lastWriteTime; } } lastWriteTimer.Stop(); Logger.Info("Determining song download time and determining mappings took {0}ms", lastWriteTimer.ElapsedMilliseconds); // Update song Infos, directory tree, and sort this.UpdatePlayCounts(); // Check if we need to upgrade settings file favorites try { this.Settings.ConvertFavoritesToPlaylist(SongCore.Loader.CustomLevels); } catch (Exception e) { Logger.Exception("FAILED TO CONVERT FAVORITES TO PLAYLIST!", e); } // load the current editing playlist or make one if (CurrentEditingPlaylist == null && !String.IsNullOrEmpty(this.Settings.currentEditingPlaylistFile)) { Logger.Debug("Loading playlist for editing: {0}", this.Settings.currentEditingPlaylistFile); CurrentEditingPlaylist = Playlist.LoadPlaylist(this.Settings.currentEditingPlaylistFile); PlaylistsCollection.MatchSongsForPlaylist(CurrentEditingPlaylist, true); } if (CurrentEditingPlaylist == null) { Logger.Debug("Current editing playlist does not exit, create..."); CurrentEditingPlaylist = new Playlist { playlistTitle = "Song Browser Favorites", playlistAuthor = "SongBrowser", fileLoc = this.Settings.currentEditingPlaylistFile, image = Base64Sprites.SpriteToBase64(Base64Sprites.BeastSaberLogo), songs = new List <PlaylistSong>(), }; } CurrentEditingPlaylistLevelIds = new HashSet <string>(); foreach (PlaylistSong ps in CurrentEditingPlaylist.songs) { // Sometimes we cannot match a song string levelId = null; if (ps.level != null) { levelId = ps.level.levelID; } else if (!String.IsNullOrEmpty(ps.levelId)) { levelId = ps.levelId; } else { //Logger.Debug("MISSING SONG {0}", ps.songName); continue; } CurrentEditingPlaylistLevelIds.Add(levelId); } // Signal complete if (SongCore.Loader.CustomLevels.Count > 0) { didFinishProcessingSongs?.Invoke(SongCore.Loader.CustomLevels); } timer.Stop(); Logger.Info("Updating songs infos took {0}ms", timer.ElapsedMilliseconds); }
public IEnumerator DownloadSongCoroutine(Song songInfo) { songInfo.songQueueState = SongQueueState.Downloading; UnityWebRequest www; bool timeout = false; float time = 0f; UnityWebRequestAsyncOperation asyncRequest; try { www = UnityWebRequest.Get(songInfo.downloadUrl); asyncRequest = www.SendWebRequest(); } catch (Exception e) { Logger.Error(e); songInfo.songQueueState = SongQueueState.Error; songInfo.downloadingProgress = 1f; yield break; } while ((!asyncRequest.isDone || songInfo.downloadingProgress < 1f) && songInfo.songQueueState != SongQueueState.Error) { yield return(null); time += Time.deltaTime; if (time >= 5f && asyncRequest.progress <= float.Epsilon) { www.Abort(); timeout = true; Logger.Error("Connection timed out!"); } songInfo.downloadingProgress = asyncRequest.progress; } if (songInfo.songQueueState == SongQueueState.Error && (!asyncRequest.isDone || songInfo.downloadingProgress < 1f)) { www.Abort(); } if (www.isNetworkError || www.isHttpError || timeout || songInfo.songQueueState == SongQueueState.Error) { songInfo.songQueueState = SongQueueState.Error; Logger.Error("Unable to download song! " + (www.isNetworkError ? $"Network error: {www.error}" : (www.isHttpError ? $"HTTP error: {www.error}" : "Unknown error"))); } else { Logger.Log("Received response from BeatSaver.com..."); string docPath = ""; string customSongsPath = ""; byte[] data = www.downloadHandler.data; Stream zipStream = null; try { docPath = Application.dataPath; docPath = docPath.Substring(0, docPath.Length - 5); docPath = docPath.Substring(0, docPath.LastIndexOf("/")); customSongsPath = docPath + "/CustomSongs/" + songInfo.id + "/"; if (!Directory.Exists(customSongsPath)) { Directory.CreateDirectory(customSongsPath); } zipStream = new MemoryStream(data); Logger.Log("Downloaded zip!"); } catch (Exception e) { Logger.Exception(e); songInfo.songQueueState = SongQueueState.Error; yield break; } 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); } }
protected override void DidActivate(bool firstActivation, ActivationType type) { if (firstActivation && type == ActivationType.AddedToHierarchy) { gameObject.SetActive(true); rectTransform.sizeDelta = new Vector2(60f, 0f); _levelDetails = GetComponentsInChildren <StandardLevelDetailView>(true).First(x => x.name == "LevelDetail"); _levelDetails.gameObject.SetActive(true); RemoveCustomUIElements(rectTransform); Destroy(GetComponentsInChildren <LevelParamsPanel>().First(x => x.name == "LevelParamsPanel").gameObject); RectTransform yourStats = GetComponentsInChildren <RectTransform>(true).First(x => x.name == "Stats"); yourStats.gameObject.SetActive(true); //RectTransform buttonsRect = GetComponentsInChildren<RectTransform>().First(x => x.name == "PlayButtons"); //buttonsRect.anchoredPosition = new Vector2(0f, 6f); TextMeshProUGUI[] _textComponents = GetComponentsInChildren <TextMeshProUGUI>(); try { songNameText = _textComponents.First(x => x.name == "SongNameText"); _textComponents.First(x => x.name == "Title").text = "Playlist"; songNameText.enableWordWrapping = true; _textComponents.First(x => x.name == "Title" && x.transform.parent.name == "MaxCombo").text = "Author"; authorText = _textComponents.First(x => x.name == "Value" && x.transform.parent.name == "MaxCombo"); authorText.rectTransform.sizeDelta = new Vector2(24f, 0f); _textComponents.First(x => x.name == "Title" && x.transform.parent.name == "Highscore").text = "Total songs"; totalSongsText = _textComponents.First(x => x.name == "Value" && x.transform.parent.name == "Highscore"); _textComponents.First(x => x.name == "Title" && x.transform.parent.name == "MaxRank").text = "Downloaded"; downloadedSongsText = _textComponents.First(x => x.name == "Value" && x.transform.parent.name == "MaxRank"); } catch (Exception e) { Logger.Exception("Unable to convert detail view controller! Exception: " + e); } _selectButton = _levelDetails.playButton; _selectButton.SetButtonText(_selectButtonText); _selectButton.ToggleWordWrapping(false); _selectButton.onClick.RemoveAllListeners(); _selectButton.onClick.AddListener(() => { selectButtonPressed?.Invoke(_currentPlaylist); }); if (addDownloadButton) { _downloadButton = _levelDetails.practiceButton; _downloadButton.SetButtonIcon(Sprites.DownloadIcon); _downloadButton.onClick.RemoveAllListeners(); _downloadButton.onClick.AddListener(() => { downloadButtonPressed?.Invoke(_currentPlaylist); }); } else { Destroy(_levelDetails.practiceButton.gameObject); } coverImage = _levelDetails.GetPrivateField <RawImage>("_coverImage"); } }
public Playlist(JSONNode playlistNode) { string image = playlistNode["image"].Value; if (!string.IsNullOrEmpty(image)) { try { icon = Sprites.Base64ToSprite(image.Substring(image.IndexOf(",") + 1)); } catch { Logger.Exception("Unable to convert playlist image to sprite!"); icon = Sprites.BeastSaberLogo; } } else { icon = Sprites.BeastSaberLogo; } playlistTitle = playlistNode["playlistTitle"]; playlistAuthor = playlistNode["playlistAuthor"]; customDetailUrl = playlistNode["customDetailUrl"]; customArchiveUrl = playlistNode["customArchiveUrl"]; if (!string.IsNullOrEmpty(customDetailUrl)) { if (!customDetailUrl.EndsWith("/")) { customDetailUrl += "/"; } Logger.Log("Found playlist with customDetailUrl! Name: " + playlistTitle + ", CustomDetailUrl: " + customDetailUrl); } if (!string.IsNullOrEmpty(customArchiveUrl) && customArchiveUrl.Contains("[KEY]")) { Logger.Log("Found playlist with customArchiveUrl! Name: " + playlistTitle + ", CustomArchiveUrl: " + customArchiveUrl); } songs = new List <PlaylistSong>(); foreach (JSONNode node in playlistNode["songs"].AsArray) { PlaylistSong song = new PlaylistSong(); song.key = node["key"]; song.songName = node["songName"]; song.hash = node["hash"]; song.levelId = node["levelId"]; songs.Add(song); } if (playlistNode["playlistSongCount"] != null) { playlistSongCount = playlistNode["playlistSongCount"].AsInt; } if (playlistNode["fileLoc"] != null) { fileLoc = playlistNode["fileLoc"]; } if (playlistNode["playlistURL"] != null) { fileLoc = playlistNode["playlistURL"]; } }
public static void ReloadPlaylists(bool fullRefresh = true) { try { List <string> playlistFiles = new List <string>(); if (PluginConfig.beatDropInstalled) { String beatDropPath = Path.Combine(PluginConfig.beatDropPlaylistsLocation, "playlists"); if (Directory.Exists(beatDropPath)) { string[] beatDropJSONPlaylists = Directory.GetFiles(beatDropPath, "*.json"); string[] beatDropBPLISTPlaylists = Directory.GetFiles(beatDropPath, "*.bplist"); playlistFiles.AddRange(beatDropJSONPlaylists); playlistFiles.AddRange(beatDropBPLISTPlaylists); Logger.Log($"Found {beatDropJSONPlaylists.Length + beatDropBPLISTPlaylists.Length} playlists in BeatDrop folder"); } } string[] localJSONPlaylists = Directory.GetFiles(Path.Combine(Environment.CurrentDirectory, "Playlists"), "*.json"); string[] localBPLISTPlaylists = Directory.GetFiles(Path.Combine(Environment.CurrentDirectory, "Playlists"), "*.bplist"); playlistFiles.AddRange(localJSONPlaylists); playlistFiles.AddRange(localBPLISTPlaylists); Logger.Log($"Found {localJSONPlaylists.Length + localBPLISTPlaylists.Length} playlists in Playlists folder"); if (fullRefresh) { loadedPlaylists.Clear(); foreach (string path in playlistFiles) { try { Playlist playlist = Playlist.LoadPlaylist(path); if (Path.GetFileName(path) == "favorites.json" && playlist.playlistTitle == "Your favorite songs") { continue; } loadedPlaylists.Add(playlist); Logger.Log($"Found \"{playlist.playlistTitle}\" by {playlist.playlistAuthor}"); } catch (Exception e) { Logger.Log($"Unable to parse playlist @ {path}! Exception: {e}"); } } } else { foreach (string path in playlistFiles) { if (!loadedPlaylists.Any(x => x.fileLoc == path)) { Logger.Log("Found new playlist! Path: " + path); try { Playlist playlist = Playlist.LoadPlaylist(path); if (Path.GetFileName(path) == "favorites.json" && playlist.playlistTitle == "Your favorite songs") { continue; } loadedPlaylists.Add(playlist); Logger.Log($"Found \"{playlist.playlistTitle}\" by {playlist.playlistAuthor}"); if (SongLoader.AreSongsLoaded) { MatchSongsForPlaylist(playlist); } } catch (Exception e) { Logger.Log($"Unable to parse playlist @ {path}! Exception: {e}"); } } } } } catch (Exception e) { Logger.Exception("Unable to load playlists! Exception: " + e); } }
public Playlist(JSONNode playlistNode) { string image = playlistNode["image"].Value; // If we cannot find an image or parse the provided one correctly, fall back to anything. // It will never be displayed by SongBrowser. if (!string.IsNullOrEmpty(image)) { try { icon = Sprites.Base64ToSprite(image.Substring(image.IndexOf(",") + 1)); } catch { Logger.Exception("Unable to convert playlist image to sprite!"); icon = Sprites.StarFullIcon; } } else { icon = Sprites.StarFullIcon; } playlistTitle = playlistNode["playlistTitle"]; playlistAuthor = playlistNode["playlistAuthor"]; customDetailUrl = playlistNode["customDetailUrl"]; customArchiveUrl = playlistNode["customArchiveUrl"]; if (!string.IsNullOrEmpty(customDetailUrl)) { if (!customDetailUrl.EndsWith("/")) { customDetailUrl += "/"; } Logger.Log("Found playlist with customDetailUrl! Name: " + playlistTitle + ", CustomDetailUrl: " + customDetailUrl); } if (!string.IsNullOrEmpty(customArchiveUrl) && customArchiveUrl.Contains("[KEY]")) { Logger.Log("Found playlist with customArchiveUrl! Name: " + playlistTitle + ", CustomArchiveUrl: " + customArchiveUrl); } songs = new List <PlaylistSong>(); foreach (JSONNode node in playlistNode["songs"].AsArray) { PlaylistSong song = new PlaylistSong(); song.key = node["key"]; song.songName = node["songName"]; song.hash = node["hash"]; song.levelId = node["levelId"]; songs.Add(song); } if (playlistNode["playlistSongCount"] != null) { playlistSongCount = playlistNode["playlistSongCount"].AsInt; } if (playlistNode["fileLoc"] != null) { fileLoc = playlistNode["fileLoc"]; } if (playlistNode["playlistURL"] != null) { fileLoc = playlistNode["playlistURL"]; } }
private async Task ExtractZipAsync(Song songInfo, Stream zipStream, string customSongsPath) { try { Logger.Info("Extracting..."); _extractingZip = true; ZipArchive archive = new ZipArchive(zipStream, ZipArchiveMode.Read); string path = customSongsPath + "/" + songInfo.key + " (" + songInfo.songName + " - " + songInfo.levelAuthorName + ")"; if (Directory.Exists(path)) { int pathNum = 1; while (Directory.Exists(path + $" ({pathNum})")) { ++pathNum; } path += $" ({pathNum})"; } await Task.Run(() => archive.ExtractToDirectory(path)).ConfigureAwait(false); archive.Dispose(); songInfo.path = path; } catch (Exception e) { Logger.Exception($"Unable to extract ZIP! Exception: {e}"); songInfo.songQueueState = SongQueueState.Error; _extractingZip = false; return; } zipStream.Close(); //Correct subfolder /* * try * { * string path = Directory.GetDirectories(customSongsPath).FirstOrDefault(); * SongCore.Utilities.Utils.GrantAccess(path); * DirectoryInfo subfolder = new DirectoryInfo(path).GetDirectories().FirstOrDefault(); * if(subfolder != null) * { * Console.WriteLine(path); * Console.WriteLine(subfolder.FullName); * string newPath = CustomLevelPathHelper.customLevelsDirectoryPath + "/" + songInfo.id + " " + subfolder.Name; * if (Directory.Exists(newPath)) * { * int pathNum = 1; * while (Directory.Exists(newPath + $" ({pathNum})")) ++pathNum; * newPath = newPath + $" ({pathNum})"; * } * Console.WriteLine(newPath); * Directory.Move(subfolder.FullName, newPath); * if (SongCore.Utilities.Utils.IsDirectoryEmpty(path)) * { * Directory.Delete(path); * } * songInfo.path = newPath; * } * else * Console.WriteLine("subfoldern null"); * } * catch(Exception ex) * { * Logger.Error($"Unable to prepare Extracted Zip! \n {ex}"); * } */ if (string.IsNullOrEmpty(songInfo.path)) { songInfo.path = customSongsPath; } _extractingZip = false; songInfo.songQueueState = SongQueueState.Downloaded; _alreadyDownloadedSongs.Add(songInfo); Logger.Info($"Extracted {songInfo.songName} {songInfo.songSubName}!"); HMMainThreadDispatcher.instance.Enqueue(() => { try { // string dirName = new DirectoryInfo(customSongsPath).Name; SongCore.Loader.SongsLoadedEvent -= Plugin.Instance.SongCore_SongsLoadedEvent; Action <SongCore.Loader, Dictionary <string, CustomPreviewBeatmapLevel> > songsLoadedAction = null; songsLoadedAction = (arg1, arg2) => { SongCore.Loader.SongsLoadedEvent -= songsLoadedAction; SongCore.Loader.SongsLoadedEvent += Plugin.Instance.SongCore_SongsLoadedEvent; }; SongCore.Loader.SongsLoadedEvent += songsLoadedAction; SongCore.Loader.Instance.RefreshSongs(false); } catch (Exception e) { Logger.Exception("Unable to load song! Exception: " + e); } }); }