private void HandleDidPressSkipButton()
 {
     _levelRequestNavigationController.ClearChildControllers();
     _requestInfoViewController.Init("Default song", "Default Requestor");
     _levelRequestNavigationController.PushViewController(_requestInfoViewController);
     StaticData.QueueList.RemoveAt(0);
     _song = (QueuedSong)StaticData.QueueList[0];
     CheckQueueAndUpdate();
 }
 public void SetQueuedSong(QueuedSong song)
 {
     if (!_initialized)
     {
         return;
     }
     _songName.SetText(song.BeatName);
     _requestorName.SetText("Requested By:\n" + song.RequestedBy);
 }
 public bool DoesSongExist(QueuedSong song)
 {
     try
     {
         return(SongLoader.CustomLevels.FirstOrDefault(x => x.levelID.Contains(song.SongHash)) != null);
     }
     catch (Exception e)
     {
         _logger.Debug(e); //Not a fatal error.
         return(false);
     }
 }
        private void OnSongsLoaded(SongLoader songLoader, List <CustomLevel> list)
        {
            SongLoader.SongsLoadedEvent -= OnSongsLoaded;
            _downloadButton.interactable = true;
            if (DoesSongExist(_queuedSong))
            {
                _ui.SetButtonText(ref _downloadButton, "Downloaded");
            }
            else
            {
                _ui.SetButtonText(ref _downloadButton, "Download");
            }

            _queuedSong = null;
            FindObjectOfType <LevelRequestFlowCoordinator>().CheckQueueAndUpdate(); //This kinda goes against the purpose of a flow controller, but I just want it to work.
        }
        public void CheckQueueAndUpdate()
        {
            if (StaticData.QueueList.Count <= 0)
            {
                return;
            }
            _song = (QueuedSong)StaticData.QueueList[0];
            _requestInfoViewController.SetQueuedSong(_song);

            if (!_requestInfoViewController.DoesSongExist(_song))
            {
                _requestInfoViewController.SetDownloadButtonText("Download");
                _requestInfoViewController.SetDownloadState(true);
                return;
            }

            _requestInfoViewController.SetDownloadButtonText("Downloaded");
            _requestInfoViewController.SetDownloadState(false);

            _customLevel = SongLoader.CustomLevels.Find(x => x.levelID.Contains(_song.SongHash));

            SongLoader.Instance.LoadAudioClipForLevel(_customLevel, (level) =>
            {
                try
                {
                    var songPreviewPlayer = Resources.FindObjectsOfTypeAll <SongPreviewPlayer>().First();
                    songPreviewPlayer.CrossfadeTo(_customLevel.audioClip, _customLevel.previewStartTime,
                                                  _customLevel.previewDuration);
                }
                catch (Exception e)
                {
                    _logger.Error("Unable to start song preview: " + e); // non critical
                }
            });

            if (!_levelDifficultyViewController.isInViewControllerHierarchy)
            {
                _levelDifficultyViewController.Init(_customLevel.difficultyBeatmaps, false);
                _levelRequestNavigationController.PushViewController(_levelDifficultyViewController);
            }
            else
            {
                _levelDifficultyViewController.SetDifficultyLevels(_customLevel.difficultyBeatmaps,
                                                                   _levelDifficultyViewController.selectedDifficultyLevel);
            }
        }
        public IEnumerator DownloadSongCoroutine(QueuedSong song)
        {
            _ui.SetButtonText(ref _downloadButton, "Downloading...");
            _downloadButton.interactable = false;
            _skipButton.interactable     = false;

            _logger.Debug("Web Request sent to: " + song.DownloadUrl);
            var www = UnityWebRequest.Get(song.DownloadUrl);

            var timeout = false;
            var time    = 0f;

            var asyncRequest = www.SendWebRequest();

            while (!asyncRequest.isDone || asyncRequest.progress < 1f)
            {
                yield return(null);

                time += Time.deltaTime;

                if (!(time >= 15f) || asyncRequest.progress != 0f)
                {
                    continue;
                }
                www.Abort();
                timeout = true;
            }

            if (www.isNetworkError || www.isHttpError || timeout)
            {
                www.Abort();
                _skipButton.interactable     = true;
                _downloadButton.interactable = true;
                _ui.SetButtonText(ref _downloadButton, "Download");
            }
            else
            {
                var zipPath         = "";
                var customSongsPath = "";
                try
                {
                    _logger.Debug("Download complete in: " + time);
                    var data = www.downloadHandler.data;

                    var docPath = Application.dataPath;
                    docPath         = docPath.Substring(0, docPath.Length - 5);
                    docPath         = docPath.Substring(0, docPath.LastIndexOf("/", StringComparison.Ordinal));
                    customSongsPath = docPath + "/CustomSongs/" + song.Id + "/";
                    zipPath         = customSongsPath + song.Id + ".zip";
                    if (!Directory.Exists(customSongsPath))
                    {
                        Directory.CreateDirectory(customSongsPath);
                    }
                    File.WriteAllBytes(zipPath, data);
                }
                catch (Exception e)
                {
                    _logger.Error(e);
                    _skipButton.interactable     = true;
                    _downloadButton.interactable = true;
                    _ui.SetButtonText(ref _downloadButton, "Download");
                }

                var zip = new FastZip();
                zip.ExtractZip(zipPath, customSongsPath, null);
                try
                {
                    SongLoader.Instance.RefreshSongs();
                    _queuedSong = song;
                    SongLoader.SongsLoadedEvent += OnSongsLoaded;
                }
                catch (Exception e)
                {
                    _logger.Error(e);
                }
                File.Delete(zipPath);
                _skipButton.interactable = true;
            }
        }