protected override void DidActivate() { _parentMasterViewController = transform.parent.GetComponent <BeatSaverMasterViewController>(); ui = BeatSaverUI._instance; if (_searchKeyboard == null) { _searchKeyboard = Instantiate(Resources.FindObjectsOfTypeAll <UIKeyboard>().First(), rectTransform, false); _searchKeyboard.uiKeyboardKeyEvent = delegate(char input) { _inputString += input; UpdateInputText(); }; _searchKeyboard.uiKeyboardDeleteEvent = delegate() { _inputString = _inputString.Substring(0, _inputString.Length - 1); UpdateInputText(); }; } if (_inputText == null) { _inputText = ui.CreateText(rectTransform, "Search...", new Vector2(0f, -17.5f)); _inputText.alignment = TextAlignmentOptions.Center; _inputText.fontSize = 6f; } else { _inputString = ""; UpdateInputText(); } if (_searchButton == null) { _searchButton = ui.CreateUIButton(rectTransform, "ApplyButton"); ui.SetButtonText(ref _searchButton, "Submit"); (_searchButton.transform as RectTransform).sizeDelta = new Vector2(30f, 10f); (_searchButton.transform as RectTransform).anchoredPosition = new Vector2(-15f, 5f); _searchButton.onClick.RemoveAllListeners(); _searchButton.onClick.AddListener(delegate() { DismissModalViewController(null, false); }); } base.DidActivate(); }
public IEnumerator DownloadSongCoroutine(Song songInfo, int row) { _loading = true; ui.SetButtonText(ref _downloadButton, "Downloading..."); _downloadButton.interactable = false; UnityWebRequest www = UnityWebRequest.Get("https://beatsaver.com/dl.php?id=" + (songInfo.id)); yield return(www.SendWebRequest()); if (www.isNetworkError || www.isHttpError) { log.Error(www.error); } else { string zipPath = ""; string docPath = ""; string customSongsPath = ""; try { byte[] data = www.downloadHandler.data; docPath = Application.dataPath; docPath = docPath.Substring(0, docPath.Length - 5); docPath = docPath.Substring(0, docPath.LastIndexOf("/")); customSongsPath = docPath + "/CustomSongs/"; zipPath = customSongsPath + songInfo.beatname + ".zip"; File.WriteAllBytes(zipPath, data); }catch (Exception e) { log.Exception("FATAL EXCEPTION: " + e); _songListViewController._songsTableView.SelectRow(row); RefreshDetails(row); _loading = false; _downloadButton.interactable = true; yield break; } bool isOverwriting = false; using (var zf = new ZipFile(zipPath)) { foreach (ZipEntry ze in zf) { if (ze.IsFile) { if (Directory.Exists(customSongsPath + ze.Name.Substring(0, ze.Name.IndexOf('/')))) { yield return(PromptOverwriteFiles(ze.Name.Substring(0, ze.Name.IndexOf('/')))); break; } else { isOverwriting = true; } } else if (ze.IsDirectory) { if (Directory.Exists(customSongsPath + ze.Name)) { yield return(PromptOverwriteFiles(ze.Name.Trim('\\', '/'))); break; } else { isOverwriting = true; } } } } if (_confirmOverwriteState == FastZip.Overwrite.Always || isOverwriting) { FastZip zip = new FastZip(); zip.ExtractZip(zipPath, customSongsPath, null); UpdateAlreadyDownloadedSongs(); _songListViewController.RefreshScreen(); } _confirmOverwriteState = FastZip.Overwrite.Prompt; File.Delete(zipPath); } _songListViewController._songsTableView.SelectRow(row); RefreshDetails(row); _loading = false; _downloadButton.interactable = true; }
public IEnumerator DownloadSongCoroutine(Song songInfo, int row) { _loading = true; ui.SetButtonText(ref _downloadButton, "Downloading..."); _downloadButton.interactable = false; if (_deleteButton != null) { _deleteButton.interactable = false; } string downloadedSongPath = ""; UnityWebRequest www = UnityWebRequest.Get(songInfo.downloadUrl); www.timeout = 10; yield return(www.SendWebRequest()); log.Log("Received response from BeatSaver.com..."); if (www.isNetworkError || www.isHttpError) { log.Error(www.error); TextMeshProUGUI _errorText = ui.CreateText(_songDetailViewController.rectTransform, String.Format(www.error), new Vector2(18f, -64f)); Destroy(_errorText.gameObject, 2f); } else { string zipPath = ""; string docPath = ""; string customSongsPath = ""; try { byte[] data = www.downloadHandler.data; docPath = Application.dataPath; docPath = docPath.Substring(0, docPath.Length - 5); docPath = docPath.Substring(0, docPath.LastIndexOf("/")); customSongsPath = docPath + "/CustomSongs/" + songInfo.id + "/"; zipPath = customSongsPath + songInfo.beatname + ".zip"; if (!Directory.Exists(customSongsPath)) { Directory.CreateDirectory(customSongsPath); } File.WriteAllBytes(zipPath, data); log.Log("Downloaded zip file!"); }catch (Exception e) { log.Exception("EXCEPTION: " + e); _songListViewController._songsTableView.SelectRow(row); RefreshDetails(row); _loading = false; if (_deleteButton != null) { _downloadButton.interactable = true; } yield break; } bool isOverwriting = false; using (var zf = new ZipFile(zipPath)) { foreach (ZipEntry ze in zf) { if (ze.IsFile) { if (string.IsNullOrEmpty(downloadedSongPath) && ze.Name.IndexOf('/') != -1) { downloadedSongPath = customSongsPath + ze.Name.Substring(0, ze.Name.IndexOf('/')); } if (Directory.Exists(customSongsPath + ze.Name.Substring(0, ze.Name.IndexOf('/')))) { yield return(PromptOverwriteFiles(ze.Name.Substring(0, ze.Name.IndexOf('/')))); break; } else { isOverwriting = true; } } else if (ze.IsDirectory) { downloadedSongPath = customSongsPath + ze.Name; if (Directory.Exists(customSongsPath + ze.Name)) { yield return(PromptOverwriteFiles(ze.Name.Trim('\\', '/'))); break; } else { isOverwriting = true; } } } } if (_confirmOverwriteState == Prompt.Yes || isOverwriting) { FastZip zip = new FastZip(); log.Log("Extractibg..."); zip.ExtractZip(zipPath, customSongsPath, null); try { CustomSongInfo downloadedSong = GetCustomSongInfo(downloadedSongPath); CustomLevelStaticData newLevel = null; try { newLevel = ScriptableObject.CreateInstance <CustomLevelStaticData>(); } catch (Exception e) { //LevelStaticData.OnEnable throws null reference exception because we don't have time to set _difficultyLevels } ReflectionUtil.SetPrivateField(newLevel, "_levelId", downloadedSong.GetIdentifier()); ReflectionUtil.SetPrivateField(newLevel, "_authorName", downloadedSong.authorName); ReflectionUtil.SetPrivateField(newLevel, "_songName", downloadedSong.songName); ReflectionUtil.SetPrivateField(newLevel, "_songSubName", downloadedSong.songSubName); ReflectionUtil.SetPrivateField(newLevel, "_previewStartTime", downloadedSong.previewStartTime); ReflectionUtil.SetPrivateField(newLevel, "_previewDuration", downloadedSong.previewDuration); ReflectionUtil.SetPrivateField(newLevel, "_beatsPerMinute", downloadedSong.beatsPerMinute); List <LevelStaticData.DifficultyLevel> difficultyLevels = new List <LevelStaticData.DifficultyLevel>(); LevelStaticData.DifficultyLevel newDiffLevel = new LevelStaticData.DifficultyLevel(); StartCoroutine(LoadAudio("file://" + downloadedSong.path + "/" + downloadedSong.difficultyLevels[0].audioPath, newDiffLevel, "_audioClip")); difficultyLevels.Add(newDiffLevel); ReflectionUtil.SetPrivateField(newLevel, "_difficultyLevels", difficultyLevels.ToArray()); newLevel.OnEnable(); _notUpdatedSongs.Add(newLevel); }catch (Exception e) { log.Exception("Can't play preview! Exception: " + e); } UpdateAlreadyDownloadedSongs(); _songListViewController.RefreshScreen(); log.Log("Downloaded!"); } _confirmOverwriteState = Prompt.NotSelected; File.Delete(zipPath); } try { _songListViewController._songsTableView.SelectRow(row); RefreshDetails(row); } catch (Exception e) { log.Exception(e.ToString()); } _loading = false; if (_deleteButton != null) { _downloadButton.interactable = true; } }
protected override void DidActivate() { ui = BeatSaverUI._instance; _parentMasterViewController = transform.parent.GetComponent <BeatSaverMasterViewController>(); try { if (_pageUpButton == null) { _pageUpButton = Instantiate(Resources.FindObjectsOfTypeAll <Button>().First(x => (x.name == "PageUpButton")), rectTransform, false); (_pageUpButton.transform as RectTransform).anchorMin = new Vector2(0.5f, 1f); (_pageUpButton.transform as RectTransform).anchorMax = new Vector2(0.5f, 1f); (_pageUpButton.transform as RectTransform).anchoredPosition = new Vector2(0f, -14f); _pageUpButton.interactable = true; _pageUpButton.onClick.AddListener(delegate() { if (_currentPage > 0) { if (!_parentMasterViewController._loading) { _parentMasterViewController._loading = true; _currentPage -= 1; _parentMasterViewController.GetPage(_currentPage); } } }); } if (_pageDownButton == null) { _pageDownButton = Instantiate(Resources.FindObjectsOfTypeAll <Button>().First(x => (x.name == "PageDownButton")), rectTransform, false); (_pageDownButton.transform as RectTransform).anchorMin = new Vector2(0.5f, 0f); (_pageDownButton.transform as RectTransform).anchorMax = new Vector2(0.5f, 0f); (_pageDownButton.transform as RectTransform).anchoredPosition = new Vector2(0f, 8f); _pageDownButton.interactable = true; _pageDownButton.onClick.AddListener(delegate() { if (!_parentMasterViewController._loading) { _parentMasterViewController._loading = true; _currentPage += 1; _parentMasterViewController.GetPage(_currentPage); } }); } if (_sortByButton == null) { _sortByButton = ui.CreateUIButton(rectTransform, "ApplyButton"); ui.SetButtonText(ref _sortByButton, "Sort by"); ui.SetButtonTextSize(ref _sortByButton, 3f); (_sortByButton.transform as RectTransform).sizeDelta = new Vector2(30f, 6f); (_sortByButton.transform as RectTransform).anchoredPosition = new Vector2(0f, 73f); _sortByButton.onClick.RemoveAllListeners(); _sortByButton.onClick.AddListener(delegate() { SelectTopButtons(TopButtonsState.SortBy); }); } if (_sortByText == null) { _sortByText = ui.CreateText(rectTransform, "SORT BY", new Vector2(-36f, -4.75f)); _sortByText.fontSize = 3.5f; _sortByText.rectTransform.sizeDelta = new Vector2(10f, 6f); _sortByText.gameObject.SetActive(false); } if (_topButton == null) { _topButton = ui.CreateUIButton(rectTransform, "ApplyButton"); ui.SetButtonText(ref _topButton, "Downloads"); ui.SetButtonTextSize(ref _topButton, 3f); (_topButton.transform as RectTransform).sizeDelta = new Vector2(20f, 6f); (_topButton.transform as RectTransform).anchoredPosition = new Vector2(-30f, 73f); _topButton.onClick.RemoveAllListeners(); _topButton.onClick.AddListener(delegate() { if (!_parentMasterViewController._loading) { _parentMasterViewController._loading = true; _parentMasterViewController._sortBy = "top"; _currentPage = 0; _parentMasterViewController.ClearSearchInput(); _parentMasterViewController.GetPage(_currentPage); SelectTopButtons(TopButtonsState.Select); } }); _topButton.gameObject.SetActive(false); } if (_newButton == null) { _newButton = ui.CreateUIButton(rectTransform, "ApplyButton"); ui.SetButtonText(ref _newButton, "Upload Time"); ui.SetButtonTextSize(ref _newButton, 3f); (_newButton.transform as RectTransform).sizeDelta = new Vector2(20f, 6f); (_newButton.transform as RectTransform).anchoredPosition = new Vector2(-10f, 73f); _newButton.onClick.RemoveAllListeners(); _newButton.onClick.AddListener(delegate() { if (!_parentMasterViewController._loading) { _parentMasterViewController._loading = true; _parentMasterViewController._sortBy = "new"; _currentPage = 0; _parentMasterViewController.ClearSearchInput(); _parentMasterViewController.GetPage(_currentPage); SelectTopButtons(TopButtonsState.Select); } }); _newButton.gameObject.SetActive(false); } if (_starButton == null) { _starButton = ui.CreateUIButton(rectTransform, "ApplyButton"); ui.SetButtonText(ref _starButton, "Plays"); ui.SetButtonTextSize(ref _starButton, 3f); (_starButton.transform as RectTransform).sizeDelta = new Vector2(20f, 6f); (_starButton.transform as RectTransform).anchoredPosition = new Vector2(10f, 73f); _starButton.onClick.RemoveAllListeners(); _starButton.onClick.AddListener(delegate() { if (!_parentMasterViewController._loading) { _parentMasterViewController._sortBy = "plays"; _currentPage = 0; _parentMasterViewController.ClearSearchInput(); _parentMasterViewController.GetPage(_currentPage); SelectTopButtons(TopButtonsState.Select); } }); _starButton.gameObject.SetActive(false); } if (_searchButton == null) { _searchButton = ui.CreateUIButton(rectTransform, "ApplyButton"); ui.SetButtonText(ref _searchButton, "Search"); ui.SetButtonTextSize(ref _searchButton, 3f); (_searchButton.transform as RectTransform).sizeDelta = new Vector2(30f, 6f); (_searchButton.transform as RectTransform).anchoredPosition = new Vector2(-30f, 73f); _searchButton.onClick.RemoveAllListeners(); _searchButton.onClick.AddListener(delegate() { _parentMasterViewController.ShowSearchKeyboard(); SelectTopButtons(TopButtonsState.Search); _currentPage = 0; }); } if (_loadingIndicator == null) { try { _loadingIndicator = ui.CreateLoadingIndicator(rectTransform); (_loadingIndicator.transform as RectTransform).anchorMin = new Vector2(0.5f, 0.5f); (_loadingIndicator.transform as RectTransform).anchorMax = new Vector2(0.5f, 0.5f); (_loadingIndicator.transform as RectTransform).anchoredPosition = new Vector2(0f, 0f); _loadingIndicator.SetActive(true); }catch (Exception e) { log.Exception("EXCEPTION: " + e); } } _songListTableCellInstance = Resources.FindObjectsOfTypeAll <SongListTableCell>().First(x => (x.name == "SongListTableCell")); if (_songsTableView == null) { _songsTableView = new GameObject().AddComponent <TableView>(); _songsTableView.transform.SetParent(rectTransform, false); _songsTableView.dataSource = this; (_songsTableView.transform as RectTransform).anchorMin = new Vector2(0f, 0.5f); (_songsTableView.transform as RectTransform).anchorMax = new Vector2(1f, 0.5f); (_songsTableView.transform as RectTransform).sizeDelta = new Vector2(0f, 60f); (_songsTableView.transform as RectTransform).position = new Vector3(0f, 0f, 2.4f); (_songsTableView.transform as RectTransform).anchoredPosition = new Vector3(0f, -3f); _songsTableView.DidSelectRowEvent += _songsTableView_DidSelectRowEvent; } else { _songsTableView.ReloadData(); } } catch (Exception e) { log.Exception("EXCEPTION IN DidActivate: " + e); } }