public Song(CustomLevelStaticData _data)
 {
     songName         = _data.songName;
     songSubName      = _data.songSubName;
     authorName       = _data.authorName;
     difficultyLevels = ConvertDifficultyLevels(_data.difficultyLevels);
 }
Ejemplo n.º 2
0
        private void DataReceived(string[] _data)
        {
            if (_data != null && _data.Length > 0)
            {
                foreach (string data in _data)
                {
                    ServerCommand command = null;

                    try
                    {
                        command = JsonUtility.FromJson <ServerCommand>(data);
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine("Can't parse received JSON! Exception: " + e);
                    }

                    try
                    {
                        if (command != null)
                        {
                            switch (command.commandType)
                            {
                            case ServerCommandType.UpdateRequired:
                            {
                                _selectText.text = "Plugin version mismatch:\nServer: " + command.version + "\nClient: " + BSMultiplayerClient.version;
                                BSMultiplayerClient._instance.DisconnectFromServer();
                                _loading = false;
                            }; break;

                            case ServerCommandType.SetServerState:
                            {
                                if (command.serverState == ServerState.Playing)
                                {
                                    _loading = false;
                                    TimeSpan timeRemaining = TimeSpan.FromSeconds(command.selectedSongDuration - command.selectedSongPlayTime);

                                    _timerText.text = timeRemaining.Minutes.ToString("00") + ":" + timeRemaining.Seconds.ToString("00");

                                    _selectedSong = SongLoader.CustomSongInfos.First(x => x.levelId == command.selectedLevelID);

                                    _selectedSongCell.gameObject.SetActive(true);
                                    (_selectedSongCell.transform as RectTransform).anchoredPosition = new Vector2(14f, 39f);
                                    _selectedSongCell.songName = _selectedSong.songName + "\n<size=80%>" + _selectedSong.songSubName + "</size>";
                                    _selectedSongCell.author   = _selectedSong.authorName;


                                    CustomLevelStaticData _songData = SongLoader.CustomLevelStaticDatas.First(x => x.levelId == _selectedSong.levelId);

                                    _selectedSongCell.coverImage = _songData.coverImage;

                                    _songPreviewPlayer.CrossfadeTo(_songData.previewAudioClip, (float)command.selectedSongPlayTime, (float)(command.selectedSongDuration - command.selectedSongPlayTime - 5f), 1f);
                                }
                            }; break;

                            case ServerCommandType.SetLobbyTimer:
                            {
                                _loading = false;
                                Console.WriteLine("Set timer text to " + command.lobbyTimer);
                                _timerText.text = command.lobbyTimer.ToString();
                                if (_multiplayerLeaderboard != null && _viewControllers.IndexOf(_multiplayerLeaderboard) >= 0)
                                {
                                    _viewControllers.Remove(_multiplayerLeaderboard);
                                    Destroy(_multiplayerLeaderboard.gameObject);
                                    _songPreviewPlayer.CrossfadeToDefault();
                                    _selectedSongCell.gameObject.SetActive(false);
                                }
                            }; break;

                            case ServerCommandType.SetSelectedSong:
                            {
                                Console.WriteLine("Set selected song " + command.selectedLevelID);

                                _loading = false;

                                if (_selectedSong == null)
                                {
                                    try
                                    {
                                        _selectedSongDifficulty = command.selectedSongDifficlty;
                                        _selectedSong           = SongLoader.CustomSongInfos.First(x => x.levelId == command.selectedLevelID);

                                        _selectText.text = "Next song:";

                                        _selectedSongCell.gameObject.SetActive(true);
                                        (_selectedSongCell.transform as RectTransform).anchoredPosition = new Vector2(-25f, 0);
                                        _selectedSongCell.songName = _selectedSong.songName + "\n<size=80%>" + _selectedSong.songSubName + "</size>";
                                        _selectedSongCell.author   = _selectedSong.authorName;


                                        CustomLevelStaticData _songData = SongLoader.CustomLevelStaticDatas.First(x => x.levelId == _selectedSong.levelId);

                                        _selectedSongCell.coverImage = _songData.coverImage;

                                        PlayPreview(_songData);
                                    }
                                    catch (Exception e)
                                    {
                                        Console.WriteLine("EXCEPTION: " + e);
                                    }
                                }

                                Console.WriteLine("Done");
                            }; break;

                            case ServerCommandType.StartSelectedSongLevel:
                            {
                                Console.WriteLine("Starting selected song! Song: " + _selectedSong.songName + ", Diff: " + ((LevelStaticData.Difficulty)_selectedSongDifficulty).ToString());

                                BSMultiplayerClient._instance.DataReceived -= DataReceived;
                                GameplayOptions gameplayOptions = new GameplayOptions();
                                gameplayOptions.noEnergy = true;
                                gameplayOptions.mirror   = false;

                                if (BSMultiplayerClient._instance._mainGameSceneSetupData != null)
                                {
                                    BSMultiplayerClient._instance._mainGameSceneSetupData.SetData(_selectedSong.levelId, (LevelStaticData.Difficulty)_selectedSongDifficulty, null, null, 0f, gameplayOptions, GameplayMode.SoloStandard, null);
                                    BSMultiplayerClient._instance._mainGameSceneSetupData.TransitionToScene(0.7f);
                                    _selectedSong = null;
                                    return;
                                }
                                else
                                {
                                    Console.WriteLine("SceneSetupData is null!");
                                }
                            }; break;

                            case ServerCommandType.DownloadSongs: {
                                if (!AllSongsDownloaded(command.songsToDownload))
                                {
                                    StartCoroutine(DownloadSongs(command.songsToDownload));
                                    BSMultiplayerClient._instance.DisconnectFromServer();
                                }
                            }; break;

                            case ServerCommandType.SetPlayerInfos: {
                                if (command.serverState == ServerState.Playing)
                                {
                                    if (_multiplayerLeaderboard != null)
                                    {
                                        TimeSpan timeRemaining = TimeSpan.FromSeconds(command.selectedSongDuration - command.selectedSongPlayTime);

                                        _timerText.text = timeRemaining.Minutes.ToString("00") + ":" + timeRemaining.Seconds.ToString("00");
                                        try
                                        {
                                            _multiplayerLeaderboard.SetLeaderboard(command.playerInfos.Select(x => JsonUtility.FromJson <PlayerInfo>(x)).ToArray());
                                        }
                                        catch (Exception e)
                                        {
                                            Console.WriteLine("Leaderboard exception: " + e);
                                        }
                                    }
                                }
                                else
                                {
                                    _playerInfos.Clear();
                                    foreach (string playerStr in command.playerInfos)
                                    {
                                        PlayerInfo player = JsonUtility.FromJson <PlayerInfo>(playerStr);
                                        if (!String.IsNullOrEmpty(player.playerAvatar))
                                        {
                                            byte[] avatar = Convert.FromBase64String(player.playerAvatar);

                                            player.rightHandPos = Serialization.ToVector3(avatar.Take(12).ToArray());
                                            player.leftHandPos  = Serialization.ToVector3(avatar.Skip(12).Take(12).ToArray());
                                            player.headPos      = Serialization.ToVector3(avatar.Skip(24).Take(12).ToArray());

                                            player.rightHandRot = Serialization.ToQuaternion(avatar.Skip(36).Take(16).ToArray());
                                            player.leftHandRot  = Serialization.ToQuaternion(avatar.Skip(52).Take(16).ToArray());
                                            player.headRot      = Serialization.ToQuaternion(avatar.Skip(68).Take(16).ToArray());
                                        }
                                        _playerInfos.Add(player);
                                    }

                                    try
                                    {
                                        if (_avatars.Count > _playerInfos.Count)
                                        {
                                            List <AvatarController> avatarsToRemove = new List <AvatarController>();
                                            for (int i = _playerInfos.Count; i < _avatars.Count; i++)
                                            {
                                                avatarsToRemove.Add(_avatars[i]);
                                            }
                                            foreach (AvatarController avatar in avatarsToRemove)
                                            {
                                                _avatars.Remove(avatar);
                                                Destroy(avatar.gameObject);
                                            }
                                        }
                                        else if (_avatars.Count < _playerInfos.Count)
                                        {
                                            for (int i = 0; i < (_playerInfos.Count - _avatars.Count); i++)
                                            {
                                                _avatars.Add(new GameObject("Avatar").AddComponent <AvatarController>());
                                            }
                                        }

                                        for (int i = 0; i < _playerInfos.Count; i++)
                                        {
                                            _avatars[i].SetPlayerInfo(_playerInfos[i], 0f, localPlayerInfo.Equals(_playerInfos[i]));
                                        }
                                    }
                                    catch (Exception e)
                                    {
                                        Console.WriteLine($"AVATARS EXCEPTION: {e}");
                                    }
                                }
                            }; break;
                            }
                            _serverState = command.serverState;
                        }
                        else
                        {
                            Console.WriteLine("Server Command is null!");
                        }
                    }catch (Exception e)
                    {
                        Console.WriteLine("Exception (parse switch): " + e);
                    }
                }
            }


            StartCoroutine(BSMultiplayerClient._instance.ReceiveFromServerCoroutine());
        }
Ejemplo n.º 3
0
        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;
            }
        }
        private void DataReceived(string[] _data)
        {
            if (_data != null && _data.Length > 0)
            {
                foreach (string data in _data)
                {
                    ServerCommand command = null;

                    try
                    {
                        command = JsonUtility.FromJson <ServerCommand>(data);
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine("Can't parse received JSON! Exception: " + e);
                    }

                    try
                    {
                        if (command != null)
                        {
                            switch (command.commandType)
                            {
                            case ServerCommandType.UpdateRequired:
                            {
                                _selectText.text = "Plugin version mismatch:\nServer: " + command.version + "\nClient: " + BSMultiplayerMain.version;
                                BSMultiplayerMain._instance.DisconnectFromServer();
                                _loading = false;
                            }; break;

                            case ServerCommandType.SetServerState:
                            {
                                if (command.serverState == ServerState.Playing)
                                {
                                    _loading = false;
                                    TimeSpan timeRemaining = TimeSpan.FromSeconds(command.selectedSongDuration - command.selectedSongPlayTime);

                                    _timerText.text = timeRemaining.Minutes.ToString("00") + ":" + timeRemaining.Seconds.ToString("00");

                                    _selectedSong = SongLoader.CustomSongInfos.First(x => x.levelId == command.selectedLevelID);

                                    _selectedSongCell.gameObject.SetActive(true);
                                    (_selectedSongCell.transform as RectTransform).anchoredPosition = new Vector2(14f, 39f);
                                    _selectedSongCell.songName = _selectedSong.songName + "\n<size=80%>" + _selectedSong.songSubName + "</size>";
                                    _selectedSongCell.author   = _selectedSong.authorName;


                                    CustomLevelStaticData _songData = SongLoader.CustomLevelStaticDatas.First(x => x.levelId == _selectedSong.levelId);

                                    _selectedSongCell.coverImage = _songData.coverImage;

                                    _songPreviewPlayer.CrossfadeTo(_songData.previewAudioClip, (float)command.selectedSongPlayTime, (float)(command.selectedSongDuration - command.selectedSongPlayTime - 5f), 1f);
                                }
                            }; break;

                            case ServerCommandType.SetLobbyTimer:
                            {
                                _loading = false;
                                Console.WriteLine("Set timer text to " + command.lobbyTimer);
                                _timerText.text = command.lobbyTimer.ToString();
                                if (_multiplayerLeaderboard != null && _viewControllers.IndexOf(_multiplayerLeaderboard) >= 0)
                                {
                                    _viewControllers.Remove(_multiplayerLeaderboard);
                                    Destroy(_multiplayerLeaderboard.gameObject);
                                    _songPreviewPlayer.CrossfadeToDefault();
                                    _selectedSongCell.gameObject.SetActive(false);
                                }
                            }; break;

                            case ServerCommandType.SetSelectedSong:
                            {
                                Console.WriteLine("Set selected song " + command.selectedLevelID);

                                _loading = false;

                                if (_selectedSong == null)
                                {
                                    try
                                    {
                                        _selectedSongDifficulty = command.selectedSongDifficlty;
                                        _selectedSong           = SongLoader.CustomSongInfos.First(x => x.levelId == command.selectedLevelID);

                                        _selectText.text = "Next song:";

                                        _selectedSongCell.gameObject.SetActive(true);
                                        (_selectedSongCell.transform as RectTransform).anchoredPosition = new Vector2(-25f, 0);
                                        _selectedSongCell.songName = _selectedSong.songName + "\n<size=80%>" + _selectedSong.songSubName + "</size>";
                                        _selectedSongCell.author   = _selectedSong.authorName;


                                        CustomLevelStaticData _songData = SongLoader.CustomLevelStaticDatas.First(x => x.levelId == _selectedSong.levelId);

                                        _selectedSongCell.coverImage = _songData.coverImage;

                                        PlayPreview(_songData);
                                    }
                                    catch (Exception e)
                                    {
                                        Console.WriteLine("EXCEPTION: " + e);
                                    }
                                }

                                Console.WriteLine("Done");
                            }; break;

                            case ServerCommandType.StartSelectedSongLevel:
                            {
                                Console.WriteLine("Starting selected song! Song: " + _selectedSong.songName + ", Diff: " + ((LevelStaticData.Difficulty)_selectedSongDifficulty).ToString());

                                BSMultiplayerMain._instance.DataReceived -= DataReceived;
                                GameplayOptions gameplayOptions = new GameplayOptions();
                                gameplayOptions.noEnergy = true;
                                gameplayOptions.mirror   = false;

                                if (BSMultiplayerMain._instance._mainGameSceneSetupData != null)
                                {
                                    BSMultiplayerMain._instance._mainGameSceneSetupData.SetData(_selectedSong.levelId, (LevelStaticData.Difficulty)_selectedSongDifficulty, null, null, 0f, gameplayOptions, GameplayMode.SoloStandard, null);
                                    BSMultiplayerMain._instance._mainGameSceneSetupData.TransitionToScene(0.7f);
                                    _selectedSong = null;
                                    return;
                                }
                                else
                                {
                                    Console.WriteLine("SceneSetupData is null!");
                                }
                            }; break;

                            case ServerCommandType.DownloadSongs:
                            {
                                if (!AllSongsDownloaded(command.songsToDownload))
                                {
                                    StartCoroutine(DownloadSongs(command.songsToDownload));
                                    BSMultiplayerMain._instance.DisconnectFromServer();
                                }
                            }; break;

                            case ServerCommandType.SetPlayerInfos:
                            {
                                if (_multiplayerLeaderboard != null)
                                {
                                    TimeSpan timeRemaining = TimeSpan.FromSeconds(command.selectedSongDuration - command.selectedSongPlayTime);

                                    _timerText.text = timeRemaining.Minutes.ToString("00") + ":" + timeRemaining.Seconds.ToString("00");
                                    try
                                    {
                                        _multiplayerLeaderboard.SetLeaderboard(command.playerInfos.Select(x => JsonUtility.FromJson <PlayerInfo>(x)).ToArray());
                                    }
                                    catch (Exception e)
                                    {
                                        Console.WriteLine("Leaderboard exception: " + e);
                                    }
                                }
                            }; break;
                            }
                        }
                        else
                        {
                            Console.WriteLine("Server Command is null!");
                        }
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine("Exception (parse switch): " + e);
                    }
                }
            }


            StartCoroutine(BSMultiplayerMain._instance.ReceiveFromServerCoroutine());
        }
Ejemplo n.º 5
0
        public override void RefreshSongs()
        {
            if (SceneManager.GetActiveScene().buildIndex != MenuIndex)
            {
                return;
            }

            var gameScenesManager = Resources.FindObjectsOfTypeAll <GameScenesManager>().FirstOrDefault();

            var gameDataModel = PersistentSingleton <GameDataModel> .instance;
            var oldData       = gameDataModel.gameStaticData.worldsData[0].levelsData.ToList();

            CustomLevelStaticDatas.Clear();
            CustomSongInfos.Clear();

            var loadedSongs = new List <CustomSongInfo>();

            SetButtonText("Loading songs");

            // TODO Find way to ensure song order
            RetrieveAllSongsAsync(song => {
                oldData.RemoveAll(x => x.levelId == song.levelId);

                var id = song.GetIdentifier();

                if (loadedSongs.Any(x => x.levelId == id && x != song))
                {
                    Log("Duplicate song found at " + song.path);
                    return;
                }

                loadedSongs.Add(song);

                CustomLevelStaticData newLevel = null;
                try {
                    newLevel = ScriptableObject.CreateInstance <CustomLevelStaticData>();
                }
                catch (Exception) {
                    //LevelStaticData.OnEnable throws null reference exception because we don't have time to set _difficultyLevels
                    Log("Unable to create new level");
                    return;
                }

                ReflectionUtil.SetPrivateField(newLevel, "_levelId", id);
                ReflectionUtil.SetPrivateField(newLevel, "_authorName", song.authorName);
                ReflectionUtil.SetPrivateField(newLevel, "_songName", song.songName);
                ReflectionUtil.SetPrivateField(newLevel, "_songSubName", song.songSubName);
                ReflectionUtil.SetPrivateField(newLevel, "_previewStartTime", song.previewStartTime);
                ReflectionUtil.SetPrivateField(newLevel, "_previewDuration", song.previewDuration);
                ReflectionUtil.SetPrivateField(newLevel, "_beatsPerMinute", song.beatsPerMinute);
                StartCoroutine(LoadSprite("file://" + song.path + "/" + song.coverImagePath, newLevel, "_coverImage"));

                var newSceneInfo = ScriptableObject.CreateInstance <SceneInfo>();
                ReflectionUtil.SetPrivateField(newSceneInfo, "_gameScenesManager", gameScenesManager);
                ReflectionUtil.SetPrivateField(newSceneInfo, "_sceneName", song.environmentName);

                ReflectionUtil.SetPrivateField(newLevel, "_environmetSceneInfo", newSceneInfo);

                var difficultyLevels = new List <LevelStaticData.DifficultyLevel>();
                foreach (var diffLevel in song.difficultyLevels)
                {
                    var newDiffLevel = new LevelStaticData.DifficultyLevel();

                    try {
                        var difficulty = diffLevel.difficulty.ToEnum(LevelStaticData.Difficulty.Normal);
                        ReflectionUtil.SetPrivateField(newDiffLevel, "_difficulty", difficulty);
                        ReflectionUtil.SetPrivateField(newDiffLevel, "_difficultyRank", diffLevel.difficultyRank);

                        if (!File.Exists(song.path + "/" + diffLevel.jsonPath))
                        {
                            Log("Couldn't find difficulty json " + song.path + "/" + diffLevel.jsonPath);
                            continue;
                        }

                        var newSongLevelData = ScriptableObject.CreateInstance <SongLevelData>();
                        var json             = File.ReadAllText(song.path + "/" + diffLevel.jsonPath);
                        try {
                            newSongLevelData.LoadFromJson(json);
                        }
                        catch (Exception e) {
                            Log("Error while parsing " + song.path + "/" + diffLevel.jsonPath);
                            Log(e.ToString());
                            continue;
                        }

                        ReflectionUtil.SetPrivateField(newDiffLevel, "_songLevelData", newSongLevelData);
                        StartCoroutine(LoadAudio("file://" + song.path + "/" + diffLevel.audioPath, newDiffLevel,
                                                 "_audioClip"));
                        difficultyLevels.Add(newDiffLevel);
                    }
                    catch (Exception e) {
                        Log("Error parsing difficulty level in song: " + song.path);
                        Log(e.Message);
                        continue;
                    }
                }

                if (difficultyLevels.IsEmpty())
                {
                    return;
                }

                ReflectionUtil.SetPrivateField(newLevel, "_difficultyLevels", difficultyLevels.ToArray());
                newLevel.OnEnable();
                oldData.Add(newLevel);
                CustomLevelStaticDatas.Add(newLevel);
            }, () => {
                SetButtonText("Tutorial");
                ReflectionUtil.SetPrivateField(gameDataModel.gameStaticData.worldsData[0], "_levelsData", oldData.ToArray());
                SongsLoaded.Invoke();
            });
        }
Ejemplo n.º 6
0
        private void DataReceived(string[] _data)
        {
            if (_data != null && _data.Length > 0)
            {
                foreach (string data in _data)
                {
                    ServerCommand command = null;

                    try
                    {
                        command = JsonUtility.FromJson <ServerCommand>(data);
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine("Can't parse received JSON! Exception: " + e);
                    }

                    try
                    {
                        if (command != null)
                        {
                            switch (command.commandType)
                            {
                            case ServerCommandType.SetServerState:
                            {
                                if (command.serverState == ServerState.Playing)
                                {
                                    BSMultiplayerMain._instance.DataReceived -= DataReceived;
                                    BSMultiplayerMain._instance.DisconnectFromServer();
                                    TimeSpan timeRemaining = TimeSpan.FromSeconds(command.selectedSongDuration - command.selectedSongPlayTime);

                                    TextMeshProUGUI _errorText = ui.CreateText(rectTransform, "Game in progress, " + timeRemaining.Minutes.ToString("00") + ":" + timeRemaining.Seconds.ToString("00") + " remaining", new Vector2(0f, -48f));
                                    _errorText.alignment = TextAlignmentOptions.Center;
                                    Destroy(_errorText.gameObject, 3f);
                                }
                            }; break;

                            case ServerCommandType.SetLobbyTimer:
                            {
                                Console.WriteLine("Set timer text to " + command.lobbyTimer);
                                _timerText.text = command.lobbyTimer.ToString();
                            }; break;

                            case ServerCommandType.SetSelectedSong:
                            {
                                Console.WriteLine("Set selected song " + command.selectedLevelID);

                                _loading = false;

                                if (_selectedSong == null)
                                {
                                    try
                                    {
                                        _selectedSongDifficulty = command.selectedSongDifficlty;
                                        _selectedSong           = SongLoader.CustomSongInfos.First(x => x.levelId == command.selectedLevelID);

                                        _selectText.text = "Next song:";

                                        _selectedSongCell.gameObject.SetActive(true);
                                        _selectedSongCell.songName = _selectedSong.songName + "\n<size=80%>" + _selectedSong.songSubName + "</size>";
                                        _selectedSongCell.author   = _selectedSong.authorName;


                                        CustomLevelStaticData _songData = SongLoader.CustomLevelStaticDatas.First(x => x.levelId == _selectedSong.levelId);

                                        _selectedSongCell.coverImage = _songData.coverImage;

                                        PlayPreview(_songData);
                                    }
                                    catch (Exception e)
                                    {
                                        Console.WriteLine("EXCEPTION: " + e);
                                    }
                                }

                                Console.WriteLine("Done");
                            }; break;

                            case ServerCommandType.StartSelectedSongLevel:
                            {
                                Console.WriteLine("Starting selected song! Song: " + _selectedSong.songName + ", Diff: " + ((LevelStaticData.Difficulty)_selectedSongDifficulty).ToString());

                                BSMultiplayerMain._instance.DataReceived -= DataReceived;
                                GameplayOptions gameplayOptions = new GameplayOptions();
                                gameplayOptions.noEnergy = true;
                                gameplayOptions.mirror   = false;

                                if (BSMultiplayerMain._instance._mainGameSceneSetupData != null)
                                {
                                    BSMultiplayerMain._instance._mainGameSceneSetupData.SetData(_selectedSong.levelId, (LevelStaticData.Difficulty)_selectedSongDifficulty, null, null, 0f, gameplayOptions, GameplayMode.SoloStandard, null);
                                    BSMultiplayerMain._instance._mainGameSceneSetupData.TransitionToScene(0.7f);
                                    _selectedSong = null;
                                    return;
                                }
                                else
                                {
                                    Console.WriteLine("SceneSetupData is null!");
                                }
                            }; break;

                            case ServerCommandType.DownloadSongs: {
                                StartCoroutine(DownloadSongs(command.songsToDownload));
                                BSMultiplayerMain._instance.DisconnectFromServer();
                            }; break;
                            }
                        }
                        else
                        {
                            Console.WriteLine("Server Command is null!");
                        }
                    }catch (Exception e)
                    {
                        Console.WriteLine("Exception (parse switch): " + e);
                    }
                }
            }


            StartCoroutine(BSMultiplayerMain._instance.ReceiveFromServerCoroutine());
        }