Exemplo n.º 1
0
        public override async Task <SongState> CheckSongExistsAsync(string songHash)
        {
            SongState state = SongState.Wanted;

            if (SongHasher != null)
            {
                if (!SongHasher.Initialized)
                {
                    await SongHasher.InitializeAsync().ConfigureAwait(false);
                }
                if (SongHasher.ExistingSongs.ContainsKey(songHash))
                {
                    state = SongState.Exists;
                }
            }
            if (state == SongState.Wanted && HistoryManager != null)
            {
                if (!HistoryManager.IsInitialized)
                {
                    HistoryManager.Initialize();
                }
                if (HistoryManager.TryGetValue(songHash, out HistoryEntry entry))
                {
                    if (!entry.AllowRetry)
                    {
                        state      = SongState.NotWanted;
                        entry.Flag = HistoryFlag.Deleted;
                    }
                }
            }
            return(state);
        }
Exemplo n.º 2
0
        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);
        }