Exemplo n.º 1
0
        public IEnumerator DownloadSong(string levelId, Action <float> downloadProgress, Action <string> songDownloaded, Action <string> downloadError)
        {
            levelId = levelId.Substring(0, 32);
            Data.Logger.Info($"Starting download for {levelId}");
            using (UnityWebRequest www = UnityWebRequest.Get($"https://beatsaver.com/api/songs/search/hash/{levelId}"))
            {
                yield return(www.SendWebRequest());

                if (www.isNetworkError || www.isHttpError)
                {
                    Logger.Error(www.error);
                    downloadError?.Invoke(www.error);
                    yield break;
                }

                JSONNode result = JSON.Parse(www.downloadHandler.text);
                Logger.Debug($"Result: {result}");
                if (result["total"].AsInt == 0)
                {
                    downloadError?.Invoke("song not found");
                    yield break;
                }
                foreach (JSONObject song in result["songs"].AsArray)
                {
                    FileUtils.EmptyDirectory(".mpdownloadcache");

                    string zipPath   = Path.Combine(Environment.CurrentDirectory, ".mpdownloadcache", $"{song["version"].Value}.zip");
                    string finalPath = Path.Combine(Environment.CurrentDirectory, "CustomSongs", Plugin.instance.Name, song["version"].Value);

                    if (Directory.Exists(finalPath))
                    {
                        Directory.Delete(finalPath, true);
                    }

                    yield return(FileUtils.DownloadFile(song["downloadUrl"].Value, zipPath, downloadProgress));

                    yield return(FileUtils.ExtractZip(zipPath, finalPath, ".mpdownloadcache", false));

                    SongLoader.Instance.RefreshSongs(false);
                    float initTime = new DateTimeOffset(DateTime.UtcNow).ToUnixTimeSeconds();

                    while (SongLoader.AreSongsLoading)
                    {
                        yield return(null);

                        if (initTime - new DateTimeOffset(DateTime.UtcNow).ToUnixTimeSeconds() > 5)
                        {
                            downloadError?.Invoke("timeout");
                            yield break;
                        }
                    }
                    FileUtils.EmptyDirectory(".mpdownloadcache", true);
                    songDownloaded?.Invoke(song["hashMd5"]);
                    break;
                }
            }
        }
 private void didSelectDifficultyBeatmap(StandardLevelDetailViewController controller, IDifficultyBeatmap difficultyBeatmap)
 {
     Logger.Debug($"select difficulty {difficultyBeatmap.level.songName} - {difficultyBeatmap.difficulty} - {difficultyBeatmap.difficultyRank}");
     if (!_partyFlowCoordinator || !_partyFlowCoordinator.isActivated)
     {
         toggleButtons(true);
         return;
     }
     toggleButtons(false);
     SteamAPI.SetDifficulty((byte)difficultyBeatmap.difficultyRank);
 }
        protected void didSelectLevel(IDifficultyBeatmap difficultyBeatmap)
        {
            IBeatmapLevel level = difficultyBeatmap.level;

            Logger.Debug($"select level {level.songName} - {difficultyBeatmap.difficulty} - {difficultyBeatmap.difficultyRank}");
            if (!_partyFlowCoordinator || !_partyFlowCoordinator.isActivated)
            {
                toggleButtons(true);
                return;
            }
            toggleButtons(false);
            SteamAPI.SetSong(level.levelID, level.songName);
            SteamAPI.SetDifficulty((byte)difficultyBeatmap.difficultyRank);
            SongDownloader.Instance.StartCoroutine(SongDownloader.CheckSongExists(level.levelID, doesSongExist));
        }
 public void didPressPlay(StandardLevelDetailViewController controller)
 {
     Logger.Debug("press play");
     try
     {
         if (!SteamAPI.IsHost() || !Controllers.PlayerController.Instance.AllPlayersInMenu())
         {
             return;
         }
         if (!_partyFlowCoordinator || !_partyFlowCoordinator.isActivated)
         {
             toggleButtons(true);
             return;
         }
         if (songExists)
         {
             toggleButtons(false);
             SteamAPI.RequestPlay(new GameplayModifiers(_gameplaySetupViewController.gameplayModifiers));
         }
     } catch (Exception e)
     {
         Logger.Error(e);
     }
 }