Пример #1
0
        //To fix the bug explained in CustomLevelStaticData.cs
        private void OnDidSelectSongEvent(SongListViewController songListViewController)
        {
            try
            {
                CustomLevelStaticData song =
                    CustomLevelStaticDatas.FirstOrDefault(x => x.levelId == songListViewController.levelId);
                if (song == null)
                {
                    return;
                }

                if (!LoadIfNotLoaded(song))
                {
                    Logger.Log("Song was modified, updated leaderboard ID to " + song.levelId);
                    songListViewController.SelectSong(_levels.FirstIndexWhere(data => data.levelId == song.levelId));
                    return;
                }

                if (song.difficultyLevels.All(x => x.difficulty != _songSelectionView.difficulty))
                {
                    bool isDiffSelected =
                        ReflectionUtil.GetPrivateField <bool>(_difficultyView, "_difficultySelected");
                    if (!isDiffSelected)
                    {
                        return;
                    }
                    //The new selected song does not have the current difficulty selected
                    LevelStaticData.DifficultyLevel firstDiff = song.difficultyLevels.FirstOrDefault();
                    if (firstDiff == null)
                    {
                        return;
                    }
                    ReflectionUtil.SetPrivateField(_songSelectionView, "_difficulty", firstDiff.difficulty);
                }
            }
            catch (Exception e)
            {
                Logger.Log(e.ToString());
            }
        }
Пример #2
0
        public void RefreshSongs()
        {
            if (SceneManager.GetActiveScene().buildIndex != MenuIndex)
            {
                return;
            }
            Log("Refreshing songs");
            var songs = RetrieveAllSongs();

            songs = songs.OrderBy(x => x.songName).ToList();

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

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

            foreach (var customSongInfo in CustomSongInfos)
            {
                oldData.RemoveAll(x => x.levelId == customSongInfo.levelId);
            }

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

            foreach (var song in songs)
            {
                var id = song.GetIdentifier();
                if (songs.Any(x => x.levelId == id && x != song))
                {
                    Log("Duplicate song found at " + song.path);
                    continue;
                }
                CustomSongInfos.Add(song);

                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", 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.Count == 0)
                {
                    continue;
                }

                ReflectionUtil.SetPrivateField(newLevel, "_difficultyLevels", difficultyLevels.ToArray());
                newLevel.OnEnable();
                oldData.Add(newLevel);
                CustomLevelStaticDatas.Add(newLevel);
            }

            ReflectionUtil.SetPrivateField(gameDataModel.gameStaticData.worldsData[0], "_levelsData", oldData.ToArray());
            SongsLoaded.Invoke();
        }
 public DifficultyLevel(LevelStaticData.DifficultyLevel difficultyLevel)
 {
     difficulty     = LevelStaticData.GetDifficultyName(difficultyLevel.difficulty);
     difficultyRank = difficultyLevel.difficultyRank;
 }
Пример #4
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;
            }
        }
Пример #5
0
        private void Awake()
        {
            var songs = RetrieveSongs();

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

            var levelsData = new List <LevelStaticData>();

            foreach (var song in songs)
            {
                LevelStaticData newLevel = null;
                try
                {
                    newLevel = ScriptableObject.CreateInstance <LevelStaticData>();
                }
                catch (Exception e)
                {
                    //LevelStaticData.OnEnable throws null reference exception because we don't have time to set _difficultyLevels
                }
                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();
                    ReflectionUtil.SetPrivateField(newDiffLevel, "_difficulty", Enum.Parse(typeof(LevelStaticData.Difficulty), diffLevel.difficulty));
                    ReflectionUtil.SetPrivateField(newDiffLevel, "_difficultyRank", diffLevel.difficultyRank);

                    if (!File.Exists(song.path + "/" + diffLevel.jsonPath))
                    {
                        Debug.LogError("Couldn't find " + diffLevel.jsonPath + " in path " + song.path);
                        continue;
                    }

                    var newSongLevelData = ScriptableObject.CreateInstance <SongLevelData>();
                    var json             = File.ReadAllText(song.path + "/" + diffLevel.jsonPath);

                    newSongLevelData.LoadFromJson(json);
                    ReflectionUtil.SetPrivateField(newDiffLevel, "_songLevelData", newSongLevelData);
                    StartCoroutine(LoadAudio("file://" + song.path + "/" + diffLevel.audioPath, newDiffLevel, "_audioClip"));
                    difficultyLevels.Add(newDiffLevel);
                }

                ReflectionUtil.SetPrivateField(newLevel, "_difficultyLevels", difficultyLevels.ToArray());
                ReflectionUtil.InvokePrivateMethod(newLevel, "OnEnable", null);

                ReflectionUtil.SetPrivateField(newLevel, "_levelId", newLevel.GetHashCode().ToString());

                levelsData.Add(newLevel);
            }

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

            oldData.AddRange(levelsData);
            ReflectionUtil.SetPrivateField(gameDataModel.gameStaticData.worldsData[0], "_levelsData", oldData.ToArray());

            DontDestroyOnLoad(gameObject);
        }