예제 #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));
        }
예제 #4
0
        void Update()
        {
            uint size;

            try
            {
                while (SteamUser.GetAvailableVoice(out size) == EVoiceResult.k_EVoiceResultOK && size > 1024)
                {
                    byte[] buffer = new byte[size];
                    uint   bytesWritten;
                    if (SteamUser.GetVoice(true, buffer, size, out bytesWritten) == EVoiceResult.k_EVoiceResultOK && bytesWritten > 0)
                    {
                        Controllers.PlayerController.Instance._playerInfo.voip = buffer;
                    }
                }
            }
            catch (Exception e)
            {
                Logger.Error(e);
            }
        }
예제 #5
0
 public static void refreshLobbyList()
 {
     availableLobbies.Clear();
     middleViewController.Data.Clear();
     try
     {
         Dictionary <ulong, LobbyPacket> lobbies = SteamAPI.LobbyData;
         foreach (KeyValuePair <ulong, LobbyPacket> entry in lobbies)
         {
             LobbyPacket info = SteamAPI.LobbyData[entry.Key];
             availableLobbies.Add(entry.Key, info.Joinable);
             middleViewController.Data.Add(new CustomCellInfo($"{(info.Joinable && info.TotalSlots - info.UsedSlots > 0 ? "":"[LOCKED]")}[{info.UsedSlots}/{info.TotalSlots}] {info.HostName}'s Lobby", $"{info.Status}"));
         }
     }
     catch (Exception e)
     {
         Logger.Error(e);
     }
     middleViewController._customListTableView.ReloadData();
     middleViewController._customListTableView.ScrollToCellWithIdx(0, TableView.ScrollPositionType.Beginning, false);
     middleViewController.DidSelectRowEvent = (TableView view, int row) =>
     {
         ulong       clickedID = availableLobbies.Keys.ToArray()[row];
         LobbyPacket info      = SteamAPI.LobbyData[clickedID];
         if (clickedID != 0 && availableLobbies.Values.ToArray()[row] && info.TotalSlots - info.UsedSlots > 0)
         {
             Scoreboard.Instance.UpsertScoreboardEntry(Controllers.PlayerController.Instance._playerInfo.playerId, Controllers.PlayerController.Instance._playerInfo.playerName);
             Instance.Dismiss();
             MultiplayerLobby.Instance.Present();
             SteamAPI.JoinLobby(new CSteamID(clickedID));
         }
     };
     if (refresh)
     {
         refresh.interactable = true;
     }
 }
 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);
     }
 }
예제 #7
0
        public static IEnumerator DownloadAvatarCoroutine(string hash, Action <CustomAvatar.CustomAvatar> callback)
        {
            queuedAvatars.Add(hash);
            string          downloadUrl = "";
            string          avatarName  = "";
            UnityWebRequest www         = UnityWebRequest.Get("https://modelsaber.assistant.moe/api/v1/avatar/get.php?filter=hash:" + hash);

            www.timeout = 10;

            yield return(www.SendWebRequest());


            if (www.isNetworkError || www.isHttpError)
            {
                Logger.Error(www.error);
                queuedAvatars.Remove(hash);
                callback?.Invoke(null);
                yield break;
            }
            else
            {
                JSONNode node = JSON.Parse(www.downloadHandler.text);

                if (node.Count == 0)
                {
                    Logger.Error($"Avatar with hash {hash} doesn't exist on ModelSaber!");
                    cachedAvatars.Add(hash, null);
                    queuedAvatars.Remove(hash);
                    callback?.Invoke(null);
                    yield break;
                }

                downloadUrl = node[0]["download"].Value;
                avatarName  = downloadUrl.Substring(downloadUrl.LastIndexOf("/") + 1);
            }

            if (string.IsNullOrEmpty(downloadUrl))
            {
                queuedAvatars.Remove(hash);
                callback?.Invoke(null);
                yield break;
            }


            bool  timeout = false;
            float time    = 0f;
            UnityWebRequestAsyncOperation asyncRequest;

            try
            {
                www         = UnityWebRequest.Get(downloadUrl);
                www.timeout = 0;

                asyncRequest = www.SendWebRequest();
            }
            catch (Exception e)
            {
                Logger.Error(e);
                queuedAvatars.Remove(hash);
                callback?.Invoke(null);
                yield break;
            }

            while (!asyncRequest.isDone)
            {
                yield return(null);

                time += Time.deltaTime;

                if ((time >= 5f && asyncRequest.progress == 0f))
                {
                    www.Abort();
                    timeout = true;
                    Logger.Error("Connection timed out!");
                }
            }


            if (www.isNetworkError || www.isHttpError || timeout)
            {
                queuedAvatars.Remove(hash);
                Logger.Error("Unable to download avatar! " + (www.isNetworkError ? $"Network error: {www.error}" : (www.isHttpError ? $"HTTP error: {www.error}" : "Unknown error")));
                callback?.Invoke(null);
            }
            else
            {
                string docPath          = "";
                string customAvatarPath = "";

                byte[] data = www.downloadHandler.data;

                try
                {
                    docPath          = Application.dataPath;
                    docPath          = docPath.Substring(0, docPath.Length - 5);
                    docPath          = docPath.Substring(0, docPath.LastIndexOf("/"));
                    customAvatarPath = docPath + "/CustomAvatars/" + avatarName;

                    File.WriteAllBytes(customAvatarPath, data);

                    CustomAvatar.CustomAvatar downloadedAvatar = Controllers.AvatarController.CreateInstance <CustomAvatar.CustomAvatar>(customAvatarPath);

                    queuedAvatars.Remove(hash);
                    cachedAvatars.Add(hash, downloadedAvatar);

                    downloadedAvatar.Load((CustomAvatar.CustomAvatar avatar, CustomAvatar.AvatarLoadResult result) => { if (result == CustomAvatar.AvatarLoadResult.Completed)
                                                                                                                        {
                                                                                                                            callback?.Invoke(avatar); avatarDownloaded?.Invoke(hash, avatar);
                                                                                                                        }
                                                                                                                        else
                                                                                                                        {
                                                                                                                            callback?.Invoke(null);
                                                                                                                        } });
                }
                catch (Exception e)
                {
                    Logger.Error(e);
                    queuedAvatars.Remove(hash);
                    callback?.Invoke(null);
                    yield break;
                }
            }
        }