private void _simpleDialog_didFinishEvent(SimpleDialogPromptViewController sender, bool delete) { _freePlayFlowCoordinator.InvokePrivateMethod("DismissViewController", new object[] { _simpleDialog, null, false }); if (delete) { try { SongDownloader.Instance.DeleteSong(new Song(SongLoader.CustomLevels.First(x => x.levelID == _detailViewController.difficultyBeatmap.level.levelID))); List <IBeatmapLevel> levels = _levelListViewController.GetPrivateField <IBeatmapLevel[]>("_levels").ToList(); int selectedIndex = levels.IndexOf(_detailViewController.difficultyBeatmap.level); if (selectedIndex > -1) { int removedLevels = levels.RemoveAll(x => x == _detailViewController.difficultyBeatmap.level); Logger.Log("Removed " + removedLevels + " level(s) from song list!"); if (selectedIndex > 0) { selectedIndex--; } _levelListViewController.SetLevels(levels.ToArray()); TableView listTableView = _levelListViewController.GetPrivateField <LevelListTableView>("_levelListTableView").GetPrivateField <TableView>("_tableView"); listTableView.ScrollToRow(selectedIndex, false); listTableView.SelectRow(selectedIndex, true); } }catch (Exception e) { Logger.Error("Unable to delete song! Exception: " + e); } } }
private IEnumerator GetRatingForSong(IBeatmapLevel level) { UnityWebRequest www = UnityWebRequest.Get($"{PluginConfig.beatsaverURL}/api/songs/search/hash/{level.levelID.Substring(0, 32)}"); 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 { _firstVote = true; JSONNode node = JSON.Parse(www.downloadHandler.text); _lastBeatSaverSong = Song.FromSearchNode(node["songs"][0]); _ratingText.text = (int.Parse(_lastBeatSaverSong.upvotes) - int.Parse(_lastBeatSaverSong.downvotes)).ToString(); _upvoteButton.interactable = (PluginConfig.apiAccessToken != PluginConfig.apiTokenPlaceholder); _downvoteButton.interactable = (PluginConfig.apiAccessToken != PluginConfig.apiTokenPlaceholder); } catch (Exception e) { Logger.Exception("Unable to get song rating! Excpetion: " + e); } } }
public IEnumerator DownloadPlaylistFile(string url, Action <string> playlistDownloaded) { yield return(null); UnityWebRequest www = UnityWebRequest.Get(url); www.timeout = 15; yield return(www.SendWebRequest()); if (www.isNetworkError || www.isHttpError) { Logger.Error($"Unable to connect to BeastSaber playlist API! " + (www.isNetworkError ? $"Network error: {www.error}" : (www.isHttpError ? $"HTTP error: {www.error}" : "Unknown error"))); playlistDownloaded?.Invoke(null); } else { try { string docPath = Application.dataPath; docPath = docPath.Substring(0, docPath.Length - 5); docPath = docPath.Substring(0, docPath.LastIndexOf("/")); File.WriteAllText(docPath + "/Playlists/" + Path.GetFileName(www.uri.LocalPath), www.downloadHandler.text); playlistDownloaded?.Invoke(docPath + "/Playlists/" + Path.GetFileName(www.uri.LocalPath)); } catch (Exception e) { Logger.Exception("Unable to parse response! Exception: " + e); playlistDownloaded?.Invoke(null); } } }
public IEnumerator GetInfoForSong(Playlist playlist, PlaylistSong song, Action <Song> songCallback) { string url = $"{PluginConfig.beatsaverURL}/api/songs/detail/{song.key}"; if (!string.IsNullOrEmpty(playlist.customDetailUrl)) { url = playlist.customDetailUrl + song.key; } 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); songCallback?.Invoke(new Song(node["song"])); } catch (Exception e) { Logger.Exception("Unable to parse response! Exception: " + e); } } }
private IEnumerator VoteForSong(bool upvote) { Logger.Log($"Voting..."); _upvoteButton.interactable = false; _downvoteButton.interactable = false; UnityWebRequest voteWWW = UnityWebRequest.Get($"{PluginConfig.beatsaverURL}/api/songs/vote/{_lastBeatSaverSong.id}/{(upvote ? 1 : 0)}/{PluginConfig.apiAccessToken}"); voteWWW.timeout = 30; yield return(voteWWW.SendWebRequest()); if (voteWWW.isNetworkError || voteWWW.isHttpError) { Logger.Error(voteWWW.error); _ratingText.text = voteWWW.error; } else { if (!_firstVote) { yield return(new WaitForSecondsRealtime(3f)); } _firstVote = false; _upvoteButton.interactable = true; _downvoteButton.interactable = true; switch (voteWWW.responseCode) { case 200: { JSONNode node = JSON.Parse(voteWWW.downloadHandler.text); _ratingText.text = (int.Parse(node["upVotes"]) - int.Parse(node["downVotes"])).ToString(); }; break; case 403: { _ratingText.text = "Read-only token"; }; break; case 401: { _ratingText.text = "Token not found"; }; break; case 400: { _ratingText.text = "Bad token"; }; break; default: { _ratingText.text = "Error " + voteWWW.responseCode; }; break; } } }
private IEnumerator GetRatingForSong(IBeatmapLevel level) { UnityWebRequest www = UnityWebRequest.Get($"{PluginConfig.beatsaverURL}/api/songs/search/hash/{level.levelID.Substring(0, 32)}"); 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 { _firstVote = true; JSONNode node = JSON.Parse(www.downloadHandler.text); if (node["songs"].Count > 0) { _lastBeatSaverSong = Song.FromSearchNode(node["songs"][0]); _ratingText.text = (int.Parse(_lastBeatSaverSong.upvotes) - int.Parse(_lastBeatSaverSong.downvotes)).ToString(); bool canVote = (PluginConfig.apiAccessToken != PluginConfig.apiTokenPlaceholder || (VRPlatformHelper.instance.vrPlatformSDK == VRPlatformHelper.VRPlatformSDK.OpenVR || Environment.CommandLine.ToLower().Contains("-vrmode oculus") || Environment.CommandLine.ToLower().Contains("fpfc"))); _upvoteButton.interactable = canVote; _downvoteButton.interactable = canVote; _reviewButton.interactable = true; if (PluginConfig.votedSongs.ContainsKey(_lastLevel.levelID.Substring(0, 32))) { switch (PluginConfig.votedSongs[_lastLevel.levelID.Substring(0, 32)].voteType) { case VoteType.Upvote: { _upvoteButton.interactable = false; } break; case VoteType.Downvote: { _downvoteButton.interactable = false; } break; } } } else { Logger.Error("Song doesn't exist on BeatSaver!"); } } catch (Exception e) { Logger.Exception("Unable to get song rating! Excpetion: " + e); } } }
public IEnumerator GetPlaylists() { yield return(null); _loadingIndicator.SetActive(true); _playlistsListViewController.SetContent(null); UnityWebRequest www = UnityWebRequest.Get(playlistAPI_URL); www.timeout = 15; yield return(www.SendWebRequest()); if (www.isNetworkError || www.isHttpError) { Logger.Error($"Unable to connect to BeastSaber playlist API! " + (www.isNetworkError ? $"Network error: {www.error}" : (www.isHttpError ? $"HTTP error: {www.error}" : "Unknown error"))); _loadingIndicator.SetActive(false); } else { try { JSONNode node = JSON.Parse(www.downloadHandler.text); playlists.Clear(); for (int i = 0; i < node.Count; i++) { playlists.Add(new Playlist(node[i])); } _loadingIndicator.SetActive(false); _playlistsListViewController.SetContent(playlists); } catch (Exception e) { Logger.Exception("Unable to parse response! Exception: " + e); _loadingIndicator.SetActive(false); } } }
public IEnumerator GetSearchResults(int page, string search) { yield return(null); _moreSongsListViewController.SetLoadingState(true); _moreSongsListViewController.TogglePageUpDownButtons((page > 0), true); _moreSongsListViewController.SetContent(null); UnityWebRequest www = UnityWebRequest.Get($"{PluginConfig.beatsaverURL}/api/songs/search/all/{search}"); www.timeout = 30; 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); currentPageSongs.Clear(); for (int i = (page * songsPerPage); i < Math.Min(node["songs"].Count, ((page + 1) * songsPerPage)); i++) { currentPageSongs.Add(Song.FromSearchNode(node["songs"][i])); } _moreSongsListViewController.SetContent(currentPageSongs); } catch (Exception e) { Logger.Exception("Unable to parse response! Exception: " + e); } } _moreSongsListViewController.SetLoadingState(false); }
private void DeletePressed() { IBeatmapLevel level = _detailViewController.selectedDifficultyBeatmap.level; _simpleDialog.Init("Delete song", $"Do you really want to delete \"{ level.songName} {level.songSubName}\"?", "Delete", "Cancel", (selectedButton) => { freePlayFlowCoordinator.InvokePrivateMethod("DismissViewController", new object[] { _simpleDialog, null, false }); if (selectedButton == 0) { try { var levelsTableView = _levelListViewController.GetPrivateField <LevelPackLevelsTableView>("_levelPackLevelsTableView"); List <IPreviewBeatmapLevel> levels = levelsTableView.GetPrivateField <IBeatmapLevelPack>("_pack").beatmapLevelCollection.beatmapLevels.ToList(); int selectedIndex = levels.FindIndex(x => x.levelID == _detailViewController.selectedDifficultyBeatmap.level.levelID); SongDownloader.Instance.DeleteSong(new Song(SongLoader.CustomLevels.First(x => x.levelID == _detailViewController.selectedDifficultyBeatmap.level.levelID))); if (selectedIndex > -1) { int removedLevels = levels.RemoveAll(x => x.levelID == _detailViewController.selectedDifficultyBeatmap.level.levelID); Logger.Log("Removed " + removedLevels + " level(s) from song list!"); _levelListViewController.SetData(CustomHelpers.GetLevelPackWithLevels(levels.Cast <BeatmapLevelSO>().ToArray(), lastPlaylist?.playlistTitle ?? "Custom Songs", lastPlaylist?.icon)); TableView listTableView = levelsTableView.GetPrivateField <TableView>("_tableView"); listTableView.ScrollToCellWithIdx(selectedIndex, TableView.ScrollPositionType.Beginning, false); levelsTableView.SetPrivateField("_selectedRow", selectedIndex); listTableView.SelectCellWithIdx(selectedIndex, true); } } catch (Exception e) { Logger.Error("Unable to delete song! Exception: " + e); } } }); freePlayFlowCoordinator.InvokePrivateMethod("PresentViewController", new object[] { _simpleDialog, null, false }); }
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); } } }
private IEnumerator VoteWithSteamID(bool upvote) { if (!SteamManager.Initialized) { Logger.Error($"SteamManager is not initialized!"); } _upvoteButton.interactable = false; _downvoteButton.interactable = false; Logger.Log($"Getting a ticket..."); var steamId = SteamUser.GetSteamID(); string authTicketHexString = ""; byte[] authTicket = new byte[1024]; var authTicketResult = SteamUser.GetAuthSessionTicket(authTicket, 1024, out var length); if (authTicketResult != HAuthTicket.Invalid) { var beginAuthSessionResult = SteamUser.BeginAuthSession(authTicket, (int)length, steamId); switch (beginAuthSessionResult) { case EBeginAuthSessionResult.k_EBeginAuthSessionResultOK: var result = SteamUser.UserHasLicenseForApp(steamId, new AppId_t(620980)); SteamUser.EndAuthSession(steamId); switch (result) { case EUserHasLicenseForAppResult.k_EUserHasLicenseResultDoesNotHaveLicense: _upvoteButton.interactable = false; _downvoteButton.interactable = false; _ratingText.text = "User does not\nhave license"; yield break; case EUserHasLicenseForAppResult.k_EUserHasLicenseResultHasLicense: if (SteamHelper.m_GetAuthSessionTicketResponse == null) { SteamHelper.m_GetAuthSessionTicketResponse = Callback <GetAuthSessionTicketResponse_t> .Create(OnAuthTicketResponse); } SteamHelper.lastTicket = SteamUser.GetAuthSessionTicket(authTicket, 1024, out length); if (SteamHelper.lastTicket != HAuthTicket.Invalid) { Array.Resize(ref authTicket, (int)length); authTicketHexString = BitConverter.ToString(authTicket).Replace("-", ""); } break; case EUserHasLicenseForAppResult.k_EUserHasLicenseResultNoAuth: _upvoteButton.interactable = false; _downvoteButton.interactable = false; _ratingText.text = "User is not\nauthenticated"; yield break; } break; default: _upvoteButton.interactable = false; _downvoteButton.interactable = false; _ratingText.text = "Auth\nfailed"; yield break; } } Logger.Log("Waiting for Steam callback..."); float startTime = Time.time; yield return(new WaitWhile(() => { return SteamHelper.lastTicketResult != EResult.k_EResultOK && (Time.time - startTime) < 20f; })); if (SteamHelper.lastTicketResult != EResult.k_EResultOK) { Logger.Error($"Auth ticket callback timeout"); _upvoteButton.interactable = true; _downvoteButton.interactable = true; _ratingText.text = "Callback\ntimeout"; yield break; } SteamHelper.lastTicketResult = EResult.k_EResultRevoked; Logger.Log($"Voting..."); Dictionary <string, string> formData = new Dictionary <string, string> (); formData.Add("id", steamId.m_SteamID.ToString()); formData.Add("ticket", authTicketHexString); UnityWebRequest voteWWW = UnityWebRequest.Post($"{PluginConfig.beatsaverURL}/api/songs/voteById/{_lastBeatSaverSong.id}/{(upvote ? 1 : 0)}", formData); voteWWW.SetRequestHeader("Content-Type", "application/x-www-form-urlencoded"); voteWWW.timeout = 30; yield return(voteWWW.SendWebRequest()); if (voteWWW.isNetworkError) { Logger.Error(voteWWW.error); _ratingText.text = voteWWW.error; } else { if (!_firstVote) { yield return(new WaitForSecondsRealtime(2f)); } _firstVote = false; switch (voteWWW.responseCode) { case 200: { JSONNode node = JSON.Parse(voteWWW.downloadHandler.text); _ratingText.text = (int.Parse(node["upVotes"]) - int.Parse(node["downVotes"])).ToString(); if (upvote) { _upvoteButton.interactable = false; _downvoteButton.interactable = true; } else { _downvoteButton.interactable = false; _upvoteButton.interactable = true; } if (!PluginConfig.votedSongs.ContainsKey(_lastLevel.levelID.Substring(0, 32))) { PluginConfig.votedSongs.Add(_lastLevel.levelID.Substring(0, 32), new SongVote(_lastBeatSaverSong.id, upvote ? VoteType.Upvote : VoteType.Downvote)); PluginConfig.SaveConfig(); } else if (PluginConfig.votedSongs[_lastLevel.levelID.Substring(0, 32)].voteType != (upvote ? VoteType.Upvote : VoteType.Downvote)) { PluginConfig.votedSongs[_lastLevel.levelID.Substring(0, 32)] = new SongVote(_lastBeatSaverSong.id, upvote ? VoteType.Upvote : VoteType.Downvote); PluginConfig.SaveConfig(); } }; break; case 500: { _upvoteButton.interactable = false; _downvoteButton.interactable = false; _ratingText.text = "Steam API\nerror"; Logger.Error("Error: " + voteWWW.downloadHandler.text); }; break; case 401: { _upvoteButton.interactable = false; _downvoteButton.interactable = false; _ratingText.text = "Invalid\nauth ticket"; Logger.Error("Error: " + voteWWW.downloadHandler.text); }; break; case 403: { _upvoteButton.interactable = false; _downvoteButton.interactable = false; _ratingText.text = "Steam ID\nmismatch"; Logger.Error("Error: " + voteWWW.downloadHandler.text); }; break; case 400: { _upvoteButton.interactable = false; _downvoteButton.interactable = false; _ratingText.text = "Bad\nrequest"; Logger.Error("Error: " + voteWWW.downloadHandler.text); }; break; default: { _upvoteButton.interactable = true; _downvoteButton.interactable = true; _ratingText.text = "Error\n" + voteWWW.responseCode; Logger.Error("Error: " + voteWWW.downloadHandler.text); }; break; } } }
private IEnumerator VoteWithAccessToken(bool upvote) { Logger.Log($"Voting..."); _upvoteButton.interactable = false; _downvoteButton.interactable = false; UnityWebRequest voteWWW = UnityWebRequest.Get($"{PluginConfig.beatsaverURL}/api/songs/vote/{_lastBeatSaverSong.id}/{(upvote ? 1 : 0)}/{PluginConfig.apiAccessToken}"); voteWWW.timeout = 30; yield return(voteWWW.SendWebRequest()); if (voteWWW.isNetworkError) { Logger.Error(voteWWW.error); _ratingText.text = voteWWW.error; } else { if (!_firstVote) { yield return(new WaitForSecondsRealtime(3f)); } _firstVote = false; switch (voteWWW.responseCode) { case 200: { JSONNode node = JSON.Parse(voteWWW.downloadHandler.text); _ratingText.text = (int.Parse(node["upVotes"]) - int.Parse(node["downVotes"])).ToString(); if (upvote) { _upvoteButton.interactable = false; _downvoteButton.interactable = true; } else { _downvoteButton.interactable = false; _upvoteButton.interactable = true; } if (!PluginConfig.votedSongs.ContainsKey(_lastLevel.levelID.Substring(0, 32))) { PluginConfig.votedSongs.Add(_lastLevel.levelID.Substring(0, 32), new SongVote(_lastBeatSaverSong.id, upvote ? VoteType.Upvote : VoteType.Downvote)); PluginConfig.SaveConfig(); } else if (PluginConfig.votedSongs[_lastLevel.levelID.Substring(0, 32)].voteType != (upvote ? VoteType.Upvote : VoteType.Downvote)) { PluginConfig.votedSongs[_lastLevel.levelID.Substring(0, 32)] = new SongVote(_lastBeatSaverSong.id, upvote ? VoteType.Upvote : VoteType.Downvote); PluginConfig.SaveConfig(); } }; break; case 403: { _upvoteButton.interactable = false; _downvoteButton.interactable = false; _ratingText.text = "Read-only\ntoken"; }; break; case 401: { _upvoteButton.interactable = false; _downvoteButton.interactable = false; _ratingText.text = "Token\nnot found"; }; break; case 400: { _upvoteButton.interactable = false; _downvoteButton.interactable = false; _ratingText.text = "Bad\ntoken"; }; break; default: { _upvoteButton.interactable = true; _downvoteButton.interactable = true; _ratingText.text = "Error\n" + voteWWW.responseCode; }; break; } } }