Пример #1
0
        public void MissingInfoDat()
        {
            var hasher   = new SongHasher <SongHashData>(TestSongsDir);
            var songDir  = @"Data\Songs\0 (Missing Info.dat)";
            var hashData = SongHasher.GetSongHashDataAsync(songDir).Result;

            Assert.IsNull(hashData.songHash);
        }
Пример #2
0
        public void MissingExpectedDifficultyFile()
        {
            var hasher   = new SongHasher <SongHashData>(TestSongsDir);
            var songDir  = @"Data\Songs\0 (Missing ExpectedDiff)";
            var hashData = SongHasher.GetSongHashDataAsync(songDir).Result;

            Assert.IsNotNull(hashData.songHash);
        }
Пример #3
0
        public override async Task <TargetResult> TransferAsync(ISong song, Stream sourceStream, CancellationToken cancellationToken)
        {
            if (song == null)
            {
                throw new ArgumentNullException(nameof(song), "Song cannot be null for TransferAsync.");
            }
            string?          directoryPath = null;
            ZipExtractResult?zipResult     = null;
            string           directoryName;

            if (song.Name != null && song.LevelAuthorName != null)
            {
                directoryName = Util.GetSongDirectoryName(song.Key, song.Name, song.LevelAuthorName);
            }
            else if (song.Key != null)
            {
                directoryName = song.Key;
            }
            else
            {
                directoryName = song.Hash ?? Path.GetRandomFileName();
            }
            try
            {
                if (cancellationToken.IsCancellationRequested)
                {
                    return(new TargetResult(this, SongState.Wanted, false, new OperationCanceledException()));
                }
                directoryPath = Path.Combine(SongsDirectory, directoryName);
                if (!Directory.Exists(SongsDirectory))
                {
                    throw new SongTargetTransferException($"Parent directory doesn't exist: '{SongsDirectory}'");
                }
                zipResult = await Task.Run(() => FileIO.ExtractZip(sourceStream, directoryPath, OverwriteTarget)).ConfigureAwait(false);

                if (!string.IsNullOrEmpty(song.Hash) && zipResult.OutputDirectory != null)
                {
                    string?hashAfterDownload = (await SongHasher.GetSongHashDataAsync(zipResult.OutputDirectory).ConfigureAwait(false)).songHash;
                    if (hashAfterDownload == null)
                    {
                        Logger.log.Warn($"Unable to get hash for '{song}', hasher returned null.");
                    }
                    else if (hashAfterDownload != song.Hash)
                    {
                        throw new SongTargetTransferException($"Extracted song hash doesn't match expected hash: {song.Hash} != {hashAfterDownload}");
                    }
                }
                TargetResult = new DirectoryTargetResult(this, SongState.Wanted, zipResult.ResultStatus == ZipExtractResultStatus.Success, zipResult, zipResult.Exception);
                return(TargetResult);
            }
#pragma warning disable CA1031 // Do not catch general exception types
            catch (Exception ex)
            {
                TargetResult = new DirectoryTargetResult(this, SongState.Wanted, false, zipResult, ex);
                return(TargetResult);
            }
#pragma warning restore CA1031 // Do not catch general exception types
        }
Пример #4
0
        public void ValidDir()
        {
            var hasher       = new SongHasher <SongHashData>(@"Data\Songs");
            var songDir      = @"Data\Songs\5d02 (Sail - baxter395)";
            var expectedHash = "A955A84C6974761F5E1600998C7EC202DB7810B1".ToUpper();
            var hashData     = SongHasher.GetSongHashDataAsync(songDir).Result;

            Assert.AreEqual(expectedHash, hashData.songHash);
        }
        public void ValidDir()
        {
            var hasher       = new SongHasher(@"Data\Songs");
            var songDir      = @"Data\Songs\5d02 (Sail - baxter395)";
            var expectedHash = "d6f3f15484fe169f4593718f50ef6d049fcaa72e".ToUpper();
            var hashData     = SongHasher.GetSongHashDataAsync(songDir).Result;

            Assert.AreEqual(expectedHash, hashData.songHash);
        }
Пример #6
0
        public async Task DirectoryDoesntExist()
        {
            var songDir = Path.GetFullPath(@"Data\DoesntExistSongs");
            var hasher  = new SongHasher <SongHashData>(TestSongsDir);

            try
            {
                await SongHasher.GetSongHashDataAsync(songDir).ConfigureAwait(false);

                Assert.Fail("Should have thrown exception.");
            }
            catch (DirectoryNotFoundException)
            {
            }
            catch (Exception ex)
            {
                Assert.Fail($"Expected {nameof(DirectoryNotFoundException)} but threw {ex.GetType().Name}");
            }
        }
Пример #7
0
        /// <summary>
        /// Attempts to delete the downloaded zip when finished.
        /// </summary>
        /// <param name="song"></param>
        /// <returns></returns>
        public async Task <JobResult> DownloadJob(PlaylistSong song)
        {
            bool      directoryCreated = false;
            JobResult result           = new JobResult()
            {
                Song = song
            };
            bool   overwrite        = true;
            string extractDirectory = null;

            try
            {
                if (string.IsNullOrEmpty(song?.Key))
                {
                    await song.UpdateSongKeyAsync().ConfigureAwait(false);
                }
                var songDirPath = Path.GetFullPath(Path.Combine(CustomLevelsPath, song.DirectoryName));
                directoryCreated = !Directory.Exists(songDirPath);
                // Won't remove if it fails, why bother with the HashDictionary TryAdd check if we're overwriting/incrementing folder name
                // This doesn't guarantee the song isn't already downloaded
                //if (HashSource.HashDictionary.TryAdd(songDirPath, new SongHashData(0, song.Hash)))
                //{
                if (BeatSync.Paused)
                {
                    await SongFeedReaders.Utilities.WaitUntil(() => !BeatSync.Paused, 500).ConfigureAwait(false);
                }
                var downloadResult = await DownloadSongAsync(song, SongTempPath).ConfigureAwait(false);

                result.DownloadResult = downloadResult;
                if (downloadResult.Status == DownloadResultStatus.Success)
                {
                    if (BeatSync.Paused)
                    {
                        await SongFeedReaders.Utilities.WaitUntil(() => !BeatSync.Paused, 500).ConfigureAwait(false);
                    }
                    var zipResult = await Task.Run(() => FileIO.ExtractZip(downloadResult.FilePath, songDirPath, overwrite)).ConfigureAwait(false);

                    // Try to delete zip file
                    try
                    {
                        var deleteSuccessful = await FileIO.TryDeleteAsync(downloadResult.FilePath).ConfigureAwait(false);
                    }
                    catch (IOException ex)
                    {
                        Logger.log?.Warn($"Unable to delete zip file after extraction: {downloadResult.FilePath}.\n{ex.Message}");
                    }

                    result.ZipResult = zipResult;
                    extractDirectory = Path.GetFullPath(zipResult.OutputDirectory);
                    if (!overwrite && !songDirPath.Equals(extractDirectory))
                    {
                        Logger.log?.Debug($"songDirPath {songDirPath} != {extractDirectory}, updating dictionary.");
                        directoryCreated = true;
                        HashSource.ExistingSongs[song.Hash] = extractDirectory;
                    }
                    Logger.log?.Info($"Finished downloading and extracting {song}");
                    var extractedHash = await SongHasher.GetSongHashDataAsync(extractDirectory).ConfigureAwait(false);

                    result.HashAfterDownload = extractedHash.songHash;
                    if (!song.Hash.Equals(extractedHash.songHash))
                    {
                        Logger.log?.Warn($"Extracted hash doesn't match Beat Saver hash for {song}");
                    }
                    else
                    {
                        Logger.log?.Debug($"Extracted hash matches Beat Saver hash for {song}");
                    }
                }
                //}
            }
            catch (Exception ex)
            {
                Logger.log?.Error($"Error downloading {song.Key ?? song.Hash}: {ex.Message}");
                Logger.log?.Debug(ex);
                if (result.Exception == null)
                {
                    result.Exception = ex;
                }
            }
            finally
            {
                if (File.Exists(result.DownloadResult?.FilePath))
                {
                    await FileIO.TryDeleteAsync(result.DownloadResult?.FilePath).ConfigureAwait(false);
                }
            }
            return(result);
        }
        public void DirectoryDoesntExist()
        {
            var songDir = Path.GetFullPath(@"Data\DoesntExistSongs");
            var hasher  = new SongHasher();

            var test = Assert.ThrowsExceptionAsync <DirectoryNotFoundException>(async() => await SongHasher.GetSongHashDataAsync(songDir).ConfigureAwait(false)).Result;
        }