public static string GetCustomLevelHash(StandardLevelInfoSaveData level, string customLevelPath) { if (GetCachedSongData(customLevelPath, out var directoryHash, out var songHash)) { return(songHash); } byte[] combinedBytes = new byte[0]; combinedBytes = combinedBytes.Concat(File.ReadAllBytes(customLevelPath + '/' + "info.dat")).ToArray(); for (var i = 0; i < level.difficultyBeatmapSets.Length; i++) { for (var i2 = 0; i2 < level.difficultyBeatmapSets[i].difficultyBeatmaps.Length; i2++) { if (File.Exists(customLevelPath + '/' + level.difficultyBeatmapSets[i].difficultyBeatmaps[i2].beatmapFilename)) { combinedBytes = combinedBytes.Concat(File.ReadAllBytes(customLevelPath + '/' + level.difficultyBeatmapSets[i].difficultyBeatmaps[i2].beatmapFilename)).ToArray(); } } } string hash = CreateSha1FromBytes(combinedBytes.ToArray()); cachedSongHashData[customLevelPath] = new SongHashData(directoryHash, hash); return(hash); }
public SongHashData AddSongToHash(string songDirectory, bool hashImmediately = true) { if (Directory.Exists(songDirectory)) { var newSongHashData = new SongHashData() { Directory = songDirectory }; if (hashImmediately) { newSongHashData.GenerateHash(); } this.Data.Add(songDirectory, newSongHashData); return(newSongHashData); } return(null); }
public void GenerateDirectoryHash_Test() { string testDir = @"I:\Steam\SteamApps\common\Beat Saber\Beat Saber_Data\CustomLevels\1001-675 Party Rock Anthem"; var testSong = new SongHashData() { Directory = testDir }; testSong.GenerateDirectoryHash(); testSong.GenerateHash(); Console.WriteLine($"testSong hash: {testSong.DirectoryHash}"); var testSong2 = new SongHashData() { Directory = testDir }; testSong2.GenerateDirectoryHash(); testSong2.GenerateHash(); Console.WriteLine($"testSong2 hash: {testSong2.DirectoryHash}"); Assert.AreEqual(testSong.DirectoryHash, testSong2.DirectoryHash); }
/// <summary> /// Hashes songs that aren't in the cache. Returns the number of hashed songs. /// </summary> /// <returns></returns> /// <exception cref="DirectoryNotFoundException">Thrown if the set song directory doesn't exist.</exception> public int AddMissingHashes() { Stopwatch sw = new Stopwatch(); sw.Start(); var songDir = new DirectoryInfo(CustomLevelsPath); if (!songDir.Exists) { throw new DirectoryNotFoundException($"Song Hasher's song directory doesn't exist: {songDir.FullName}"); } Logger.log?.Info($"SongDir is {songDir.FullName}"); int hashedSongs = 0; songDir.GetDirectories().Where(d => !HashDictionary.ContainsKey(d.FullName)).ToList().AsParallel().ForAll(d => { SongHashData data = null; try { data = GetSongHashDataAsync(d.FullName).Result; } catch (DirectoryNotFoundException) { Logger.log?.Warn($"Directory {d.FullName} does not exist, this will [probably] never happen."); return; } catch (ArgumentNullException) { Logger.log?.Warn("Somehow the directory is null in AddMissingHashes, this will [probably] never happen."); return; } if (data == null) { Logger.log?.Warn($"GetSongHashData({d.FullName}) returned null"); return; } else if (string.IsNullOrEmpty(data.songHash)) { Logger.log?.Warn($"GetSongHashData(\"{d.Name}\") returned a null string for hash (No info.dat?)."); return; } if (!ExistingSongs.TryAdd(data.songHash, d.FullName)) { Logger.log?.Debug($"Duplicate song detected: {ExistingSongs[data.songHash].Split('\\', '/').LastOrDefault()} : {d.Name}"); } if (!HashDictionary.TryAdd(d.FullName, data)) { Logger.log?.Warn($"Couldn't add {d.FullName} to HashDictionary"); } else { hashedSongs++; } //else //{ // //Logger.log?.Info($"Added {d.Name} to the HashDictionary."); //} }); sw.Stop(); Logger.log?.Debug($"Finished hashing in {sw.ElapsedMilliseconds}ms."); return(hashedSongs); }