public void GetBeatmaps(LocalBeatmaps localBeatmaps) { MainWindow.ToggleLoading(true, "Loading local beatmaps"); _ = Task.Run(async() => LocalBeatmaps = await App.BeatSaverApi.GetLocalBeatmaps(localBeatmaps)); }
public async Task <LocalBeatmaps> GetLocalBeatmaps(LocalBeatmaps cachedLocalBeatmaps = null) { LocalBeatmaps localBeatmaps = cachedLocalBeatmaps is null ? new LocalBeatmaps() : new LocalBeatmaps(cachedLocalBeatmaps); List <string> songs = Directory.GetDirectories(SongsPath).ToList(); foreach (LocalBeatmap beatmap in localBeatmaps.Maps.ToList()) { string song = songs.FirstOrDefault(x => new DirectoryInfo(x).Name.Split(" ")[0] == beatmap.Identifier.Value); if (song != null) { songs.Remove(song); } } for (int i = 0; i < songs.Count; i++) { if (i > 0 && i % 10 == 0) { localBeatmaps.LastPage++; } } foreach (string songFolder in songs) { string infoFile = Path.Combine(songFolder, "info.dat"); if (!File.Exists(infoFile)) { continue; } string[] folderName = new DirectoryInfo(songFolder).Name.Split(" "); LocalIdentifier identifier = folderName.Length == 1 ? new LocalIdentifier(false, folderName[0]) : new LocalIdentifier(true, folderName[0]); string json = await File.ReadAllTextAsync(infoFile); LocalBeatmap beatmap = JsonConvert.DeserializeObject <LocalBeatmap>(json); beatmap.Identifier = identifier; beatmap.FolderPath = songFolder; string coverImagePath = Path.Combine(songFolder, beatmap.CoverImageFilename); if (File.Exists(coverImagePath)) { beatmap.CoverImagePath = coverImagePath; } else { if (beatmap.Errors is null) { beatmap.Errors = new List <string>(); } beatmap.Errors.Add($"The cover image file '{beatmap.CoverImageFilename}' couldn't be found"); } string songFilePath = Path.Combine(songFolder, beatmap.SongFilename); if (File.Exists(songFilePath)) { MediaInfo mediaInfo = ffProbe.GetMediaInfo(songFilePath); beatmap.Duration = mediaInfo.Duration; } else { if (beatmap.Errors is null) { beatmap.Errors = new List <string>(); } beatmap.Errors.Add($"The song file '{beatmap.SongFilename}' couldn't be found"); } foreach (DifficultyBeatmapSet difficultyBeatmapSet in beatmap.DifficultyBeatmapSets) { if (!beatmap.Easy && difficultyBeatmapSet.DifficultyBeatmaps.Any(x => x.Difficulty == "Easy")) { beatmap.Easy = true; } if (!beatmap.Normal && difficultyBeatmapSet.DifficultyBeatmaps.Any(x => x.Difficulty == "Normal")) { beatmap.Normal = true; } if (!beatmap.Hard && difficultyBeatmapSet.DifficultyBeatmaps.Any(x => x.Difficulty == "Hard")) { beatmap.Hard = true; } if (!beatmap.Expert && difficultyBeatmapSet.DifficultyBeatmaps.Any(x => x.Difficulty == "Expert")) { beatmap.Expert = true; } if (!beatmap.ExpertPlus && difficultyBeatmapSet.DifficultyBeatmaps.Any(x => x.Difficulty == "ExpertPlus")) { beatmap.ExpertPlus = true; } } _ = Task.Run(async() => { try { beatmap.OnlineBeatmap = await GetBeatmap(identifier); } catch (Exception e) { if (beatmap.Errors is null) { beatmap.Errors = new List <string>(); } if (e.InnerException is null || string.Equals(e.Message, e.InnerException.Message)) { beatmap.Errors.Add(e.Message); }