public bool CanHandle(TypeHandlingContext context) { var classType = context.Type.GetClassType(); if (classType != null) { if (classType.Constructors.Any(c => c.Parameters.Count == 0)) { var generators = MethodGenerators.All; var found = new Boolean[generators.Length]; foreach (var method in classType.Methods) { for (Int32 i = 0, c = generators.Length; i < c; i++) { if (!found[i]) { found[i] = generators[i].IsGenerationTarget(method); } } } if (found.All(any => any)) { return(true); } } } return(false); }
/// <summary> /// Downloads an album. /// </summary> /// <param name="album">The album to download.</param> /// <param name="downloadsFolder">The downloads folder.</param> /// <param name="tagTracks">True to tag tracks; false otherwise.</param> /// <param name="saveCoverArtInTags">True to save cover art in tags; false otherwise.</param> /// <param name="saveCovertArtInFolder">True to save cover art in the downloads folder; false otherwise.</param> /// <param name="convertCoverArtToJpg">True to convert the cover art to jpg; false otherwise.</param> /// <param name="resizeCoverArt">True to resize the covert art; false otherwise.</param> /// <param name="coverArtMaxSize">The maximum width/height of the cover art when resizing.</param> private void DownloadAlbum(Album album, String downloadsFolder, Boolean tagTracks, Boolean saveCoverArtInTags, Boolean saveCovertArtInFolder, Boolean convertCoverArtToJpg, Boolean resizeCoverArt, int coverArtMaxSize) { if (this.userCancelled) { // Abort return; } // Create directory to place track files String directoryPath = downloadsFolder + "\\" + album.Title.ToAllowedFileName() + "\\"; try { Directory.CreateDirectory(directoryPath); } catch { Log("An error occured when creating the album folder. Make sure you have the rights to write files in the folder you chose", LogType.Error); return; } TagLib.Picture artwork = null; // Download artwork if (saveCoverArtInTags || saveCovertArtInFolder) { artwork = DownloadCoverArt(album, downloadsFolder, saveCovertArtInFolder, convertCoverArtToJpg, resizeCoverArt, coverArtMaxSize); } // Download & tag tracks Task[] tasks = new Task[album.Tracks.Count]; Boolean[] tracksDownloaded = new Boolean[album.Tracks.Count]; for (int i = 0; i < album.Tracks.Count; i++) { // Temporarily save the index or we will have a race condition exception when i hits its maximum value int currentIndex = i; tasks[currentIndex] = Task.Factory.StartNew(() => tracksDownloaded[currentIndex] = DownloadAndTagTrack(directoryPath, album, album.Tracks[currentIndex], tagTracks, saveCoverArtInTags, artwork)); } // Wait for all tracks to be downloaded before saying the album is downloaded Task.WaitAll(tasks); if (!this.userCancelled) { // Tasks have not been aborted if (tracksDownloaded.All(x => x == true)) { Log("Successfully downloaded album \"" + album.Title + "\"", LogType.Success); } else { Log("Finished downloading album \"" + album.Title + "\". Some tracks could not be downloaded", LogType.Success); } } }
/// <summary> /// Downloads an album. /// </summary> /// <param name="album">The album to download.</param> private async Task DownloadAlbumAsync(Album album) { if (_cancelDownloads) { // Abort return; } // Create directory to place track files try { Directory.CreateDirectory(album.Path); } catch { LogAdded(this, new LogArgs("An error occured when creating the album folder. Make sure you have the rights to write files in the folder you chose", LogType.Error)); return; } TagLib.Picture artwork = null; // Download artwork if ((App.UserSettings.SaveCoverArtInTags || App.UserSettings.SaveCoverArtInFolder) && album.HasArtwork) { artwork = await DownloadCoverArtAsync(album); } // Download & tag tracks Boolean[] tracksDownloaded = new Boolean[album.Tracks.Count]; Int32[] indexes = Enumerable.Range(0, album.Tracks.Count).ToArray(); await Task.WhenAll(indexes.Select(async i => tracksDownloaded[i] = await DownloadAndTagTrackAsync(album, album.Tracks[i], artwork))); // Create playlist file if (App.UserSettings.CreatePlaylist && !_cancelDownloads) { new PlaylistCreator(album).SavePlaylistToFile(); LogAdded(this, new LogArgs($"Saved playlist for album \"{album.Title}\"", LogType.IntermediateSuccess)); } if (!_cancelDownloads) { // Tasks have not been aborted if (tracksDownloaded.All(x => x == true)) { LogAdded(this, new LogArgs($"Successfully downloaded album \"{album.Title}\"", LogType.Success)); } else { LogAdded(this, new LogArgs($"Finished downloading album \"{album.Title}\". Some tracks were not downloaded", LogType.Success)); } } }