public static IEnumerator <List <BeatmapDetails> > GetBeatmapDetailsFromCacheCoroutine(string path) { if (!File.Exists(path)) { Logger.log.Notice($"Cache file could not be found in the path: '{path}'"); yield break; } string fileContents = null; IEnumerator <string> cacheLoader = UnityMediaLoader.LoadTextCoroutine(path); while (cacheLoader.MoveNext()) { fileContents = cacheLoader.Current; if (fileContents == null) { yield return(null); } } if (string.IsNullOrEmpty(fileContents)) { Logger.log.Warn("Beatmap details cache is empty"); yield break; } BeatmapDetailsCache cache = null; try { cache = JsonConvert.DeserializeObject <BeatmapDetailsCache>(fileContents); } catch (JsonSerializationException) { Logger.log.Warn("Unable to deserialize cache file. Could be an older bersion of the cache file (will be replaced after the in-memory cache is rebuilt)"); yield break; } catch (Exception e) { Logger.log.Warn("Unexpected exception occurred when trying to deserialize beatmap details cache"); Logger.log.Debug(e); yield break; } if (cache.Version < CURRENT_CACHE_VERSION) { Logger.log.Warn("Beatmap details cache is outdated. Forcing the cache to be rebuilt"); } else { Logger.log.Info("Successfully loaded details cache from storage"); yield return(cache.Cache); } }
public static IEnumerator <BeatmapDetails> CreateBeatmapDetailsFromFilesCoroutine(CustomPreviewBeatmapLevel customLevel) { StandardLevelInfoSaveData infoData = customLevel.standardLevelInfoSaveData; BeatmapDetails beatmapDetails = new BeatmapDetails(); beatmapDetails.LevelID = BeatmapDetailsLoader.GetSimplifiedLevelID(customLevel); beatmapDetails.SongName = customLevel.songName; beatmapDetails.BeatsPerMinute = infoData.beatsPerMinute; // load difficulties beatmapDetails.DifficultyBeatmapSets = new SimplifiedDifficultyBeatmapSet[infoData.difficultyBeatmapSets.Length]; for (int i = 0; i < infoData.difficultyBeatmapSets.Length; ++i) { var currentSimplifiedSet = new SimplifiedDifficultyBeatmapSet(); beatmapDetails.DifficultyBeatmapSets[i] = currentSimplifiedSet; var currentSet = infoData.difficultyBeatmapSets[i]; currentSimplifiedSet.CharacteristicName = currentSet.beatmapCharacteristicName; currentSimplifiedSet.DifficultyBeatmaps = new SimplifiedDifficultyBeatmap[currentSet.difficultyBeatmaps.Length]; for (int j = 0; j < currentSet.difficultyBeatmaps.Length; ++j) { var currentSimplifiedDiff = new SimplifiedDifficultyBeatmap(); currentSimplifiedSet.DifficultyBeatmaps[j] = currentSimplifiedDiff; var currentDiff = currentSet.difficultyBeatmaps[j]; currentDiff.difficulty.BeatmapDifficultyFromSerializedName(out currentSimplifiedDiff.Difficulty); currentSimplifiedDiff.NoteJumpMovementSpeed = currentDiff.noteJumpMovementSpeed; string diffFilePath = Path.Combine(customLevel.customLevelPath, currentDiff.beatmapFilename); string fileContents = null; IEnumerator <string> textLoader = UnityMediaLoader.LoadTextCoroutine(diffFilePath); while (textLoader.MoveNext()) { fileContents = textLoader.Current; if (fileContents == null) { yield return(null); } } if (string.IsNullOrEmpty(fileContents)) { yield break; } BeatmapSaveData beatmapSaveData = null; try { beatmapSaveData = BeatmapSaveData.DeserializeFromJSONString(fileContents); } catch (Exception e) { Logger.log.Warn($"Exception occurred while trying to deserialize difficulty beatmap to BeatmapSaveData for '{customLevel.songName}'"); Logger.log.Debug(e); yield break; } // missing difficulty files if (beatmapSaveData == null) { yield break; } // count notes and bombs currentSimplifiedDiff.NotesCount = 0; currentSimplifiedDiff.BombsCount = 0; foreach (var note in beatmapSaveData.notes) { if (note.type.IsBasicNote()) { ++currentSimplifiedDiff.NotesCount; } else if (note.type == NoteType.Bomb) { ++currentSimplifiedDiff.BombsCount; } } // count rotation events currentSimplifiedDiff.SpawnRotationEventsCount = 0; foreach (var mapEvent in beatmapSaveData.events) { if (mapEvent.type.IsRotationEvent()) { ++currentSimplifiedDiff.SpawnRotationEventsCount; } } currentSimplifiedDiff.ObstaclesCount = beatmapSaveData.obstacles.Count; } } // load audio string audioFilePath = Path.Combine(customLevel.customLevelPath, infoData.songFilename); AudioClip audioClip = null; IEnumerator <AudioClip> audioLoader = UnityMediaLoader.LoadAudioClipCoroutine(audioFilePath); while (audioLoader.MoveNext()) { audioClip = audioLoader.Current; if (audioClip == null) { yield return(null); } } if (audioClip == null) { yield break; } beatmapDetails.SongDuration = audioClip.length; yield return(beatmapDetails); }
/// <summary> /// Loads files associated with a custom beatmap and creates a BeatmapDetails object with the information contained in the files. /// </summary> /// <param name="customLevel">A custom level to create the BeatmapDetails object for.</param> /// <returns>BeatmapDetails object on success, otherwise null.</returns> public static BeatmapDetails CreateBeatmapDetailsFromFiles(CustomPreviewBeatmapLevel customLevel) { StandardLevelInfoSaveData infoData = customLevel.standardLevelInfoSaveData; BeatmapDetails beatmapDetails = new BeatmapDetails(); beatmapDetails.LevelID = BeatmapDetailsLoader.GetSimplifiedLevelID(customLevel); beatmapDetails.SongName = customLevel.songName; beatmapDetails.BeatsPerMinute = infoData.beatsPerMinute; // load difficulties for note info beatmapDetails.DifficultyBeatmapSets = new SimplifiedDifficultyBeatmapSet[infoData.difficultyBeatmapSets.Length]; for (int i = 0; i < infoData.difficultyBeatmapSets.Length; ++i) { var currentSimplifiedSet = new SimplifiedDifficultyBeatmapSet(); beatmapDetails.DifficultyBeatmapSets[i] = currentSimplifiedSet; var currentSet = infoData.difficultyBeatmapSets[i]; currentSimplifiedSet.CharacteristicName = currentSet.beatmapCharacteristicName; currentSimplifiedSet.DifficultyBeatmaps = new SimplifiedDifficultyBeatmap[currentSet.difficultyBeatmaps.Length]; for (int j = 0; j < currentSet.difficultyBeatmaps.Length; ++j) { var currentSimplifiedDiff = new SimplifiedDifficultyBeatmap(); currentSimplifiedSet.DifficultyBeatmaps[j] = currentSimplifiedDiff; var currentDiff = currentSet.difficultyBeatmaps[j]; currentDiff.difficulty.BeatmapDifficultyFromSerializedName(out currentSimplifiedDiff.Difficulty); currentSimplifiedDiff.NoteJumpMovementSpeed = currentDiff.noteJumpMovementSpeed; string diffFilePath = Path.Combine(customLevel.customLevelPath, currentDiff.beatmapFilename); if (!File.Exists(diffFilePath)) { return(null); } BeatmapSaveData beatmapSaveData = null; try { beatmapSaveData = BeatmapSaveData.DeserializeFromJSONString(File.ReadAllText(diffFilePath)); } catch (Exception e) { Logger.log.Debug("Unable to create BeatmapDetails object from files (unexpected exception occurred trying to load BeatmapSaveData from file)"); Logger.log.Debug(e); return(null); } if (beatmapSaveData == null) { Logger.log.Debug("Unable to create BeatmapDetails object from files (could not load BeatmapSaveData from file)"); return(null); } // count notes and bombs currentSimplifiedDiff.NotesCount = 0; currentSimplifiedDiff.BombsCount = 0; foreach (var note in beatmapSaveData.notes) { if (note.type.IsBasicNote()) { ++currentSimplifiedDiff.NotesCount; } else if (note.type == NoteType.Bomb) { ++currentSimplifiedDiff.BombsCount; } } // count rotation events currentSimplifiedDiff.SpawnRotationEventsCount = 0; foreach (var mapEvent in beatmapSaveData.events) { if (mapEvent.type.IsRotationEvent()) { ++currentSimplifiedDiff.SpawnRotationEventsCount; } } currentSimplifiedDiff.ObstaclesCount = beatmapSaveData.obstacles.Count; } } // load audio for map length string audioFilePath = Path.Combine(customLevel.customLevelPath, infoData.songFilename); AudioClip audioClip = UnityMediaLoader.LoadAudioClip(audioFilePath); if (audioClip == null) { return(null); } beatmapDetails.SongDuration = audioClip.length; return(beatmapDetails); }