示例#1
0
        public async Task DirectoryDoesntExist()
        {
            string nonExistantDir = Path.Combine(TestSongsDir, "DoesntExist");

            if (Directory.Exists(nonExistantDir))
            {
                Directory.Delete(nonExistantDir, true);
            }
            Assert.IsFalse(Directory.Exists(nonExistantDir));
            SongHasher <SongHashData> hasher = new SongHasher <SongHashData>(nonExistantDir);

            try
            {
                await hasher.HashDirectoryAsync().ConfigureAwait(false);

                Assert.Fail("Should have thrown exception.");
            }
            catch (DirectoryNotFoundException)
            {
            }
            catch (Exception ex)
            {
                Assert.Fail($"Expected {nameof(DirectoryNotFoundException)} but threw {ex.GetType().Name}");
            }
            if (Directory.Exists(nonExistantDir))
            {
                Directory.Delete(nonExistantDir, true);
            }
        }
示例#2
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);
        }
示例#3
0
        public async Task HashAllSongs()
        {
            SongHasher <SongHashData> hasher = new SongHasher <SongHashData>(TestSongsDir);
            int newHashes = await hasher.HashDirectoryAsync().ConfigureAwait(false);

            Assert.AreEqual(newHashes, 7);
        }
        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;
        }
示例#5
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);
        }
示例#6
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);
        }
示例#7
0
        public void ZipHash_NoFile()
        {
            string expectedHash = null;
            string zipPath      = Path.Combine(TestSongZipsDir, "DoesntExist.zip");
            string actualHash   = SongHasher.GetZippedSongHash(zipPath);

            Assert.AreEqual(expectedHash, actualHash);
        }
示例#8
0
        public void ZipMissingInfo()
        {
            string expectedHash = null;
            string zipPath      = Path.Combine(TestSongZipsDir, "MissingInfo.zip");
            string actualHash   = SongHasher.GetZippedSongHash(zipPath);

            Assert.AreEqual(expectedHash, actualHash);
        }
示例#9
0
        public void ZipMissingDiff()
        {
            string expectedHash = "BD8CB1F979B29760D4B623E65A59ACD217F093F3";
            string zipPath      = Path.Combine(TestSongZipsDir, "MissingDiff.zip");
            string actualHash   = SongHasher.GetZippedSongHash(zipPath);

            Assert.AreEqual(expectedHash, actualHash);
        }
        public void HashAllSongs()
        {
            var cacheFile = Path.Combine(TestCacheDir, "TestSongsHashData.dat");
            var hasher    = new SongHasher(TestSongsDir, cacheFile);
            var newHashes = hasher.AddMissingHashes();

            Assert.AreEqual(newHashes, 6);
        }
        public void HashCacheDoesntExist()
        {
            var nonExistantCacheFile = Path.Combine(TestCacheDir, "DoesntExist.dat");
            var hasher    = new SongHasher(TestSongsDir, nonExistantCacheFile);
            var newHashes = hasher.AddMissingHashes();

            Assert.AreEqual(newHashes, 6);
        }
示例#12
0
        public async Task HashCacheDoesntExist()
        {
            string nonExistantCacheFile      = Path.Combine(TestCacheDir, "DoesntExist.dat");
            SongHasher <SongHashData> hasher = new SongHasher <SongHashData>(TestSongsDir);
            int newHashes = await hasher.HashDirectoryAsync().ConfigureAwait(false);

            Assert.AreEqual(newHashes, 7);
        }
示例#13
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
        }
示例#14
0
 public DirectoryTarget(string songsDirectory, bool overwriteTarget, SongHasher songHasher, HistoryManager?historyManager, PlaylistManager?playlistManager)
     : base()
 {
     SongsDirectory  = Path.GetFullPath(songsDirectory);
     SongHasher      = songHasher;
     HistoryManager  = historyManager;
     PlaylistManager = playlistManager;
     OverwriteTarget = overwriteTarget;
 }
        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);
        }
示例#16
0
        public void HashSongZip()
        {
            string expectedHash = "ea2d289fb640ce8a0d7302ae36bfa3a5710d9ee8".ToUpper();
            string zipPath      = Path.Combine(TestSongZipsDir, "2cd (Yee - katiedead).zip");
            long   quickHash    = SongHasher.GetQuickZipHash(zipPath);
            string actualHash   = SongHasher.GetZippedSongHash(zipPath);

            Assert.AreEqual(expectedHash, actualHash);
        }
示例#17
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 AfterPartialCacheCoverage()
        {
            var cacheFile = Path.Combine(TestCacheDir, "TestSongsHashData_Partial.dat");
            var hasher    = new SongHasher(TestSongsDir, cacheFile);

            hasher.LoadCachedSongHashes();
            var newHashes = hasher.AddMissingHashes();

            Assert.AreEqual(newHashes, 2);
        }
        public void HashCacheDoesntExist()
        {
            var nonExistantCacheFile = Path.Combine(TestCacheDir, "DoesntExist.dat");

            Assert.IsFalse(File.Exists(nonExistantCacheFile));
            var hasher = new SongHasher(TestSongsDir, nonExistantCacheFile);

            hasher.LoadCachedSongHashes();
            Assert.AreEqual(hasher.HashDictionary.Count, 0);
        }
        public void Normal()
        {
            var cachePath = Path.Combine(TestCacheDir, "SongHashData.dat");

            Assert.IsTrue(File.Exists(cachePath));
            var hasher = new SongHasher(TestSongsDir, cachePath);

            hasher.LoadCachedSongHashes();
            Assert.AreEqual(hasher.HashDictionary.Count, 8);
        }
示例#21
0
        public static SongDownloader GetDefaultDownloader()
        {
            var hasher = new SongHasher(TestSongsDir, Path.Combine(TestCacheDir, "TestSongsHashData.dat"));

            var history = new HistoryManager(Path.Combine(HistoryTestPathDir, "TestSongsHistory.json"));

            history.Initialize();
            var downloader = new SongDownloader(BeatSync.Configs.PluginConfig.DefaultConfig, history, hasher, TestSongsDir);

            return(downloader);
        }
示例#22
0
        //private TransformBlock<PlaylistSong, JobResult> DownloadBatch;

        public SongDownloader(PluginConfig config, HistoryManager historyManager, SongHasher hashSource, string customLevelsPath)
        {
            CustomLevelsPath = customLevelsPath;
            Directory.CreateDirectory(CustomLevelsPath);
            HashSource      = hashSource;
            DownloadQueue   = new ConcurrentQueue <PlaylistSong>();
            HistoryManager  = historyManager;
            FavoriteMappers = new FavoriteMappers();
            FavoriteMappers.Initialize();
            Config = config.Clone();
        }
        public void DuplicateSong_DifferentFolders()
        {
            var cachePath = Path.Combine(TestCacheDir, "SongHashData_DuplicateSong.dat");

            Assert.IsTrue(File.Exists(cachePath));
            var hasher = new SongHasher(TestSongsDir, cachePath);

            hasher.LoadCachedSongHashes();
            Assert.AreEqual(hasher.HashDictionary.Count, 9);
            int uniqueHashes = hasher.HashDictionary.Values.Select(h => h.songHash).Distinct().Count();

            Assert.AreEqual(uniqueHashes, 8);
        }
        public void AddMissingCalledFirst()
        {
            // Completely ignores the duplicate entry.
            var cachePath = Path.Combine(TestCacheDir, "TestSongsHashData.dat");

            Assert.IsTrue(File.Exists(cachePath));
            var hasher = new SongHasher(TestSongsDir, cachePath);

            hasher.LoadCachedSongHashes();
            Assert.AreEqual(hasher.HashDictionary.Count, 6);
            int uniqueHashes = hasher.HashDictionary.Values.Select(h => h.songHash).Distinct().Count();

            Assert.AreEqual(uniqueHashes, 5);
        }
        //[TestMethod]
        public void BigTest()
        {
            var hasher = new SongHasher();

            hasher.LoadCachedSongHashes();
            var songPath = new DirectoryInfo(hasher.HashDictionary.Keys.First());

            songPath = songPath.Parent;
            hasher   = new SongHasher(songPath.FullName);
            hasher.LoadCachedSongHashes();
            var newHashes = hasher.AddMissingHashes();

            Console.WriteLine($"Hashed {newHashes} new songs");
        }
        public void SongDoesExist()
        {
            var historyManager = new HistoryManager(DefaultHistoryPath);
            var songHasher     = new SongHasher(DefaultSongsPath, DefaultHashCachePath);

            historyManager.Initialize();
            var downloader = new SongDownloader(defaultConfig, historyManager, songHasher, DefaultSongsPath);
            var exists     = new PlaylistSong("d375405d047d6a2a4dd0f4d40d8da77554f1f677", "Does Exist", "5e20", "ejiejidayo");

            historyManager.TryAdd(exists, 0); // Song is added before it gets to DownloadJob
            var result = downloader.DownloadJob(exists).Result;

            Assert.AreEqual(exists.Hash, result.HashAfterDownload);
            Assert.IsTrue(historyManager.ContainsKey(exists.Hash)); // Successful download is kept in history
        }
        public void DirectoryDoesntExist()
        {
            var nonExistantDir = Path.Combine(TestSongsDir, "DoesntExist");

            if (Directory.Exists(nonExistantDir))
            {
                Directory.Delete(nonExistantDir, true);
            }
            Assert.IsFalse(Directory.Exists(nonExistantDir));
            var hasher = new SongHasher(nonExistantDir);

            Assert.ThrowsException <DirectoryNotFoundException>(() => hasher.AddMissingHashes());
            if (Directory.Exists(nonExistantDir))
            {
                Directory.Delete(nonExistantDir, true);
            }
        }
        public void SongDoesntExist()
        {
            //Assert.AreEqual(1, SongFeedReaders.WebUtils.WebClient.Timeout);
            //var response = SongFeedReaders.WebUtils.WebClient.GetAsync(@"http://releases.ubuntu.com/18.04.3/ubuntu-18.04.3-live-server-amd64.iso").Result;
            //var dResult = response.Content.ReadAsFileAsync("ubuntu.iso", true).Result;
            var historyManager = new HistoryManager(DefaultHistoryPath);
            var songHasher     = new SongHasher(DefaultSongsPath, DefaultHashCachePath);

            historyManager.Initialize();
            var downloader  = new SongDownloader(defaultConfig, historyManager, songHasher, DefaultSongsPath);
            var doesntExist = new PlaylistSong("196be1af64958d8b5375b328b0eafae2151d46f8", "Doesn't Exist", "ffff", "Who knows");

            historyManager.TryAdd(doesntExist, 0); // Song is added before it gets to DownloadJob
            var result = downloader.DownloadJob(doesntExist).Result;

            Assert.IsTrue(historyManager.ContainsKey(doesntExist.Hash)); // Keep song in history so it doesn't try to download a non-existant song again.
        }
        public void EmptyDirectory()
        {
            var emptyDir = Path.Combine(TestSongsDir, "EmptyDirectory");
            var dInfo    = new DirectoryInfo(emptyDir);

            dInfo.Create();
            Assert.AreEqual(0, dInfo.GetFiles().Count());
            var hasher    = new SongHasher(emptyDir);
            var newHashes = hasher.AddMissingHashes();

            Assert.AreEqual(0, newHashes);

            //Clean up
            if (dInfo.Exists)
            {
                dInfo.Delete(true);
            }
        }
示例#30
0
        public async Task EmptyDirectory()
        {
            string        emptyDir = Path.Combine(TestSongsDir, "EmptyDirectory");
            DirectoryInfo dInfo    = new DirectoryInfo(emptyDir);

            dInfo.Create();
            Assert.AreEqual(0, dInfo.GetFiles().Count());
            SongHasher <SongHashData> hasher = new SongHasher <SongHashData>(emptyDir);
            int newHashes = await hasher.HashDirectoryAsync().ConfigureAwait(false);

            Assert.AreEqual(0, newHashes);

            //Clean up
            if (dInfo.Exists)
            {
                dInfo.Delete(true);
            }
        }