public bool LoadIfNotLoaded(CustomLevelStaticData song)
        {
            string newId     = CustomSongInfo.FromPath(song.jsonPath).GetIdentifier();
            bool   idMatched = newId == song.levelId;

            if (!idMatched)
            {
                Logger.Log("Song has been modified, old ID was " + song.levelId + " updating song content and ID");
                song.wasLoaded = false;
                _database.UpdateSongID(song.levelId, newId);
                ReflectionUtil.SetPrivateField(song, "_levelId", newId);
            }

            if (!song.wasLoaded)
            {
                foreach (CustomLevelStaticData.CustomDifficultyLevel difficultyLevel in song.difficultyLevels)
                {
                    StartCoroutine(LoadAudio("file://" + difficultyLevel.audioPath, difficultyLevel,
                                             "_audioClip"));
                    ReflectionUtil.SetPrivateField(difficultyLevel, "_songLevelData",
                                                   ParseDifficulty(difficultyLevel.jsonPath));
                }
                song.wasLoaded = true;
            }
            return(idMatched);
        }
        //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());
            }
        }
Example #3
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 void RefreshSongs(bool fullRefresh)
        {
            if (SceneManager.GetActiveScene().buildIndex != MenuIndex)
            {
                return;
            }

            List <CustomSongInfo>  songs    = _database.GetSongs();
            List <string>          playlist = GetPlaylist(); // TODO: Put this in DB
            List <LevelStaticData> newLevelData;

            GameScenesManager gameScenesManager =
                Resources.FindObjectsOfTypeAll <GameScenesManager>().FirstOrDefault();
            GameDataModel gameDataModel = PersistentSingleton <GameDataModel> .instance;

            if (OriginalLevelStaticDatas.Count == 0)
            {
                foreach (LevelStaticData level in gameDataModel.gameStaticData.worldsData[0].levelsData
                         .ToList())
                {
                    OriginalLevelStaticDatas.Add(level);
                }
            }

            if (playlist == null || !_playlistEnabled)
            {
                newLevelData = new List <LevelStaticData>(OriginalLevelStaticDatas);
            }
            else
            {
                songs        = FilterByPlaylist(songs, playlist);
                newLevelData = new List <LevelStaticData>();
            }

            if (fullRefresh)
            {
                CustomLevelStaticDatas.Clear();
                CustomSongInfos.Clear();
            }
            else
            {
                int beforeCount = CustomLevelStaticDatas.Count;
                CustomSongInfos.RemoveAll(x => !songs.Contains(x));
                CustomLevelStaticDatas.RemoveAll(x => !songs.Any(y => y.levelId == x.levelId));

                // Check why info and data are not the same
                if (CustomSongInfos.Count != CustomLevelStaticDatas.Count)
                {
                    foreach (CustomSongInfo song in CustomSongInfos)
                    {
                        if (!CustomLevelStaticDatas.Any(x => x.levelId == song.levelId))
                        {
                            Logger.Log("Song at " + song.path + " has some issue");
                        }
                    }
                }
            }

            Resources.UnloadUnusedAssets();

            foreach (CustomSongInfo song in songs)
            {
                // Add existing copy of song
                if (CustomSongInfos.Contains(song))
                {
                    foreach (CustomLevelStaticData level in CustomLevelStaticDatas)
                    {
                        if (level.levelId == song.levelId)
                        {
                            //Logger.Log("Readded: " + song.songName);
                            newLevelData.Add(level);
                            break;
                        }
                    }
                    continue;
                }

                // Add new songs
                //Logger.Log("New song found: " + song.songName);
                CustomLevelStaticData newLevel = LoadNewSong(song, gameScenesManager);
                if (newLevel != null)
                {
                    CustomSongInfos.Add(song);
                    newLevel.OnEnable();
                    newLevelData.Add(newLevel);
                    CustomLevelStaticDatas.Add(newLevel);
                }
            }

            _levels = newLevelData.ToArray();
            ReflectionUtil.SetPrivateField(gameDataModel.gameStaticData.worldsData[0], "_levelsData", _levels);
            SongsLoaded.Invoke();
        }
        private CustomLevelStaticData LoadNewSong(CustomSongInfo song, GameScenesManager gameScenesManager)
        {
            CustomLevelStaticData newLevel = null;

            try
            {
                newLevel          = ScriptableObject.CreateInstance <CustomLevelStaticData>();
                newLevel.jsonPath = song.path;
            }
            catch (NullReferenceException)
            {
                //LevelStaticData.OnEnable throws null reference exception because we don't have time to set _difficultyLevels
            }

            ReflectionUtil.SetPrivateField(newLevel, "_levelId", song.levelId);
            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"));

            SceneInfo newSceneInfo = ScriptableObject.CreateInstance <SceneInfo>();

            ReflectionUtil.SetPrivateField(newSceneInfo, "_gameScenesManager", gameScenesManager);
            ReflectionUtil.SetPrivateField(newSceneInfo, "_sceneName", song.environmentName);

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

            List <CustomLevelStaticData.CustomDifficultyLevel> difficultyLevels =
                new List <CustomLevelStaticData.CustomDifficultyLevel>();

            foreach (CustomSongInfo.DifficultyLevel diffLevel in song.difficultyLevels)
            {
                CustomLevelStaticData.CustomDifficultyLevel newDiffLevel =
                    new CustomLevelStaticData.CustomDifficultyLevel();
                try
                {
                    LevelStaticData.Difficulty 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))
                    {
                        Logger.Log("Couldn't find difficulty json " + song.path + "/" + diffLevel.jsonPath);
                        continue;
                    }

                    newDiffLevel.jsonPath  = song.path + "/" + diffLevel.jsonPath;
                    newDiffLevel.audioPath = song.path + "/" + diffLevel.audioPath;
                    difficultyLevels.Add(newDiffLevel);
                }
                catch (Exception e)
                {
                    Logger.Log("Error parsing difficulty level in song: " + song.path);
                    Logger.Log(e.Message);
                    continue;
                }
            }

            if (difficultyLevels.Count == 0)
            {
                return(null);
            }

            ReflectionUtil.SetPrivateField(newLevel, "_difficultyLevels", difficultyLevels.ToArray());
            return(newLevel);
        }