Пример #1
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();
        }
Пример #2
0
        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();
        }