public IJobBuilder CreateJobBuilder(BeatSyncConfig config) { //string tempDirectory = "Temp"; //Directory.CreateDirectory(tempDirectory); IDownloadJobFactory downloadJobFactory = new DownloadJobFactory(song => { return(new MemoryDownloadContainer()); //return new FileDownloadContainer(Path.Combine(tempDirectory, (song.Key ?? song.Hash) + ".zip")); }); IJobBuilder jobBuilder = new JobBuilder().SetDownloadJobFactory(downloadJobFactory); bool overwriteTarget = false; Plugin.log.Info($"Adding target for '{CustomLevelsDirectory}'"); SongTarget songTarget = new DirectoryTarget(CustomLevelsDirectory, overwriteTarget, true, songHasher, historyManager, playlistManager); //SongTarget songTarget = new MockSongTarget(); jobBuilder.AddTarget(songTarget); JobFinishedAsyncCallback jobFinishedCallback = new JobFinishedAsyncCallback(async(JobResult c) => { HistoryEntry entry = c.CreateHistoryEntry(); if (c.TargetResults != null && c.TargetResults.Length > 0) { foreach (TargetResult targetResult in c.TargetResults) { if (targetResult == null) { Plugin.log?.Warn($"TargetResult is null."); continue; } else if (targetResult.Success) { Plugin.log?.Info($"Target transfer successful for {targetResult.Target.DestinationId}|{targetResult.SongState}"); } else { Plugin.log?.Warn($"Target transfer unsuccessful for {targetResult.Target.DestinationId}"); if (targetResult.Exception != null) { Plugin.log?.Debug(targetResult.Exception); } } // Add entry to history, this should only succeed for jobs that didn't get to the targets. if (targetResult.Target is ITargetWithHistory targetWithHistory && targetWithHistory.HistoryManager != null) { targetWithHistory.HistoryManager.TryAdd(c.Song.Hash, entry); } Plugin.log?.Info($"Song {c.Song} transferred to {targetResult.Target.DestinationId}."); } } else { Plugin.log?.Warn($"{c.Song} has no target results."); } if (c.Successful) { if (c.DownloadResult != null && c.DownloadResult.Status == DownloadResultStatus.Skipped) { Plugin.log?.Info($" Job skipped: {c.Song} not wanted by any targets."); } else { Plugin.log?.Info($" Job completed successfully: {c.Song}"); } } else { Plugin.log?.Info($" Job failed: {c.Song}"); } }); jobBuilder.SetDefaultJobFinishedAsyncCallback(jobFinishedCallback); return(jobBuilder); }
public static async Task <IJobBuilder> CreateJobBuilderAsync(Config config) { string tempDirectory = "Temp"; Directory.CreateDirectory(tempDirectory); IDownloadJobFactory downloadJobFactory = new DownloadJobFactory(song => { // return new DownloadMemoryContainer(); return(new FileDownloadContainer(Path.Combine(tempDirectory, (song.Key ?? song.Hash) + ".zip"))); }); IJobBuilder jobBuilder = new JobBuilder().SetDownloadJobFactory(downloadJobFactory); List <ISongLocation> songLocations = new List <ISongLocation>(); songLocations.AddRange(config.BeatSaberInstallLocations.Where(l => l.Enabled && l.IsValid())); songLocations.AddRange(config.AlternateSongsPaths.Where(l => l.Enabled && l.IsValid())); foreach (ISongLocation location in songLocations) { bool overwriteTarget = false; HistoryManager? historyManager = null; SongHasher? songHasher = null; PlaylistManager?playlistManager = null; if (!string.IsNullOrEmpty(location.HistoryPath)) { string historyPath = location.HistoryPath; if (!Path.IsPathFullyQualified(historyPath)) { historyPath = Path.Combine(location.BasePath, historyPath); } string historyDirectory = Path.GetDirectoryName(historyPath) ?? string.Empty; try { Directory.CreateDirectory(historyDirectory); historyManager = new HistoryManager(historyPath); historyManager.Initialize(); } catch (Exception ex) { Logger.log.Info($"Unable to initialize HistoryManager at '{historyPath}': {ex.Message}"); } } if (!string.IsNullOrEmpty(location.PlaylistDirectory)) { string playlistDirectory = location.PlaylistDirectory; if (!Path.IsPathFullyQualified(playlistDirectory)) { playlistDirectory = Path.Combine(location.BasePath, playlistDirectory); } Directory.CreateDirectory(playlistDirectory); playlistManager = new PlaylistManager(playlistDirectory, new LegacyPlaylistHandler(), new BlistPlaylistHandler()); } string songsDirectory = location.SongsDirectory; if (!Path.IsPathFullyQualified(songsDirectory)) { songsDirectory = Path.Combine(location.BasePath, songsDirectory); } Directory.CreateDirectory(songsDirectory); songHasher = new SongHasher <SongHashData>(songsDirectory); Stopwatch sw = new Stopwatch(); Logger.log.Info($"Hashing songs in '{Paths.ReplaceWorkingDirectory(songsDirectory)}'..."); sw.Start(); await songHasher.InitializeAsync().ConfigureAwait(false); sw.Stop(); Logger.log.Info($"Hashed {songHasher.HashDictionary.Count} songs in {Paths.ReplaceWorkingDirectory(songsDirectory)} in {sw.Elapsed.Seconds}sec."); SongTarget songTarget = new DirectoryTarget(songsDirectory, overwriteTarget, songHasher, historyManager, playlistManager); //SongTarget songTarget = new MockSongTarget(); jobBuilder.AddTarget(songTarget); } #pragma warning disable CS1998 // Async method lacks 'await' operators and will run synchronously JobFinishedAsyncCallback jobFinishedCallback = new JobFinishedAsyncCallback(async(JobResult c) => #pragma warning restore CS1998 // Async method lacks 'await' operators and will run synchronously { HistoryEntry entry = c.CreateHistoryEntry(); foreach (SongTarget target in jobBuilder.SongTargets) { // Add entry to history, this should only succeed for jobs that didn't get to the targets. if (target is ITargetWithHistory targetWithHistory && targetWithHistory.HistoryManager != null && c.Song != null) { targetWithHistory.HistoryManager.TryAdd(c.Song.Hash, entry); } } if (c.Successful) { if (c.DownloadResult != null && c.DownloadResult.Status == DownloadResultStatus.Skipped) { //Logger.log.Info($" Job skipped: {c.Song} not wanted by any targets."); } else { Logger.log.Info($" Job completed successfully: {c.Song}"); } } else { Logger.log.Info($" Job failed: {c.Song}"); } }); jobBuilder.SetDefaultJobFinishedAsyncCallback(jobFinishedCallback); return(jobBuilder); }