Skip to content

Commit

Permalink
Fixed SongHasher breaking on invalid JSON
Browse files Browse the repository at this point in the history
  • Loading branch information
Zingabopp committed Jul 24, 2020
1 parent 75ed9e2 commit bf8eb1a
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
19 changes: 16 additions & 3 deletions BeatSyncLib/Hashing/SongHasher.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using BeatSyncLib.Utilities;
using Newtonsoft.Json;
using System;
using System.Collections.Concurrent;
using System.Diagnostics;
Expand Down Expand Up @@ -64,20 +65,31 @@ public async Task<int> HashDirectoryAsync()
Logger.log?.Warn("Somehow the directory is null in AddMissingHashes, this will [probably] never happen.");
return;
}
catch (JsonException ex)
{
Logger.log?.Warn($"Invalid JSON in beatmap at '{d.FullName}', skipping. {ex.Message}");
return;
}
catch (Exception ex)
{
Logger.log?.Warn($"Unhandled exception hashing beatmap at '{d.FullName}', skipping. {ex.Message}");
Logger.log?.Debug(ex);
return;
}

if (data == null)
{
Logger.log?.Warn($"GetSongHashData({d.FullName}) returned null");
return;
}
else if (string.IsNullOrEmpty(data.songHash))
else if (data.songHash == null || data.songHash.Length == 0)
{
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}");
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");
Expand Down Expand Up @@ -145,7 +157,8 @@ private static int GetStringHashSafe(string str)
s += 2;
}
return hash1 + (hash2 * 1566083941);
}catch(Exception)
}
catch (Exception)
{

return 0;
Expand Down
2 changes: 2 additions & 0 deletions BeatSyncLib/Utilities/Util.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Newtonsoft.Json.Linq;
using Newtonsoft.Json;
using System;
using System.IO;
using System.Linq;
Expand Down Expand Up @@ -165,6 +166,7 @@ public enum ByteUnit
/// <returns>Hash of the song files. Null if the info.dat file doesn't exist</returns>
/// <exception cref="DirectoryNotFoundException"></exception>
/// <exception cref="ArgumentNullException"></exception>
/// <exception cref="JsonException"
public static string? GenerateHash(string songDirectory, string existingHash = "")
{
if (string.IsNullOrEmpty(songDirectory))
Expand Down

0 comments on commit bf8eb1a

Please sign in to comment.