diff --git a/SongPlayHistory/Patches/DiTailsVotePatch.cs b/SongPlayHistory/Patches/DiTailsVotePatch.cs index 0c69077..4cf49da 100644 --- a/SongPlayHistory/Patches/DiTailsVotePatch.cs +++ b/SongPlayHistory/Patches/DiTailsVotePatch.cs @@ -25,12 +25,12 @@ private static bool Prepare() } [HarmonyPrefix] - public static void Prefix(bool upvote, IDifficultyBeatmap? ____activeBeatmap) + public static void Prefix(bool upvote, BeatmapLevel? ____activeBeatmap) //TODO: update when DiTails Update { if (____activeBeatmap == null) return; var vote = upvote ? VoteType.Upvote : VoteType.Downvote; - Plugin.Log.Debug($"DiTails voted {vote} to {____activeBeatmap.level.levelID}"); - InMenuVoteTrackingHelper.Instance?.Vote(____activeBeatmap.level, vote); + Plugin.Log.Debug($"DiTails voted {vote} to {____activeBeatmap.levelID}"); + InMenuVoteTrackingHelper.Instance?.Vote(____activeBeatmap, vote); } } } \ No newline at end of file diff --git a/SongPlayHistory/Patches/HarmonyPatches.cs b/SongPlayHistory/Patches/HarmonyPatches.cs index f531796..002e887 100644 --- a/SongPlayHistory/Patches/HarmonyPatches.cs +++ b/SongPlayHistory/Patches/HarmonyPatches.cs @@ -30,7 +30,7 @@ public static bool Prepare() } [HarmonyAfter("com.kyle1413.BeatSaber.SongCore")] - public static void Postfix(LevelListTableCell __instance, IPreviewBeatmapLevel? level, bool isFavorite, + public static void Postfix(LevelListTableCell __instance, BeatmapLevel? level, bool isFavorite, Image ____favoritesBadgeImage, TextMeshProUGUI? ____songBpmText) { if (!PluginConfig.Instance.ShowVotes) return; diff --git a/SongPlayHistory/Utils/Utils.cs b/SongPlayHistory/Utils/Utils.cs index ffff0f6..b61f5cc 100644 --- a/SongPlayHistory/Utils/Utils.cs +++ b/SongPlayHistory/Utils/Utils.cs @@ -1,4 +1,5 @@ -using System.Collections.Generic; +using System; +using System.Collections.Generic; using System.Linq; using System.Reflection; using System.Text; @@ -32,9 +33,9 @@ internal static bool IsInReplay() } #endregion - internal static string GetCustomLevelHash(CustomPreviewBeatmapLevel level) + internal static string? GetCustomLevelHash(BeatmapLevel level) { - return Hashing.GetCustomLevelHash(level).ToLower(); + return level.levelID.StartsWith("custom_level_") ? Hashing.GetCustomLevelHash(level) : null; } internal static IList Copy(this IEnumerable records) diff --git a/SongPlayHistory/VoteTracker/BeatSaverVotingTracker.cs b/SongPlayHistory/VoteTracker/BeatSaverVotingTracker.cs index d66f34c..c4139c4 100644 --- a/SongPlayHistory/VoteTracker/BeatSaverVotingTracker.cs +++ b/SongPlayHistory/VoteTracker/BeatSaverVotingTracker.cs @@ -31,12 +31,16 @@ internal class BeatSaverVotingTracker: IVoteTracker } } - public void Vote(IPreviewBeatmapLevel level, VoteType voteType) + public void Vote(BeatmapLevel level, VoteType voteType) { - if (!(level is CustomPreviewBeatmapLevel customLevel)) return; try { - var hash = Utils.Utils.GetCustomLevelHash(customLevel); + var hash = Utils.Utils.GetCustomLevelHash(level); + if (hash == null) + { + return; + } + var bsvType = voteType == VoteType.Upvote ? BSVType.Upvote : BSVType.Downvote; if (_votes == null) { @@ -55,13 +59,17 @@ public void Vote(IPreviewBeatmapLevel level, VoteType voteType) } } - public bool TryGetVote(IPreviewBeatmapLevel level, out VoteType voteType) + public bool TryGetVote(BeatmapLevel level, out VoteType voteType) { voteType = VoteType.Downvote; - if (!(level is CustomPreviewBeatmapLevel customLevel)) return false; try { - var hash = Utils.Utils.GetCustomLevelHash(customLevel); + var hash = Utils.Utils.GetCustomLevelHash(level); + if (hash == null) + { + return false; + } + var voteStatus = BeatSaverVoting.Plugin.CurrentVoteStatus(hash); if (voteStatus == null) { diff --git a/SongPlayHistory/VoteTracker/IVoteTracker.cs b/SongPlayHistory/VoteTracker/IVoteTracker.cs index 03c4bb9..0d32f07 100644 --- a/SongPlayHistory/VoteTracker/IVoteTracker.cs +++ b/SongPlayHistory/VoteTracker/IVoteTracker.cs @@ -4,9 +4,9 @@ namespace SongPlayHistory.VoteTracker { public interface IVoteTracker { - public bool TryGetVote(IPreviewBeatmapLevel level, out VoteType voteType); + public bool TryGetVote(BeatmapLevel level, out VoteType voteType); - public void Vote(IPreviewBeatmapLevel level, VoteType voteType); + public void Vote(BeatmapLevel level, VoteType voteType); } } \ No newline at end of file diff --git a/SongPlayHistory/VoteTracker/InMenuVoteTrackingHelper.cs b/SongPlayHistory/VoteTracker/InMenuVoteTrackingHelper.cs index 76b470b..bf1eaf1 100644 --- a/SongPlayHistory/VoteTracker/InMenuVoteTrackingHelper.cs +++ b/SongPlayHistory/VoteTracker/InMenuVoteTrackingHelper.cs @@ -43,13 +43,13 @@ private void OnPlayResultDismiss(ResultsViewController _) _tableView.RefreshCellsContent(); } - internal void Vote(IPreviewBeatmapLevel level, VoteType voteType) + internal void Vote(BeatmapLevel level, VoteType voteType) { _voteTracker.Vote(level, voteType); _tableView.RefreshCellsContent(); } - internal bool TryGetVote(IPreviewBeatmapLevel level, out VoteType voteType) + internal bool TryGetVote(BeatmapLevel level, out VoteType voteType) { return _voteTracker.TryGetVote(level, out voteType); } diff --git a/SongPlayHistory/VoteTracker/InternalVoteTracker.cs b/SongPlayHistory/VoteTracker/InternalVoteTracker.cs index 6768609..acb378a 100644 --- a/SongPlayHistory/VoteTracker/InternalVoteTracker.cs +++ b/SongPlayHistory/VoteTracker/InternalVoteTracker.cs @@ -100,14 +100,13 @@ public void Dispose() } } - public bool TryGetVote(IPreviewBeatmapLevel level, out VoteType voteType) + public bool TryGetVote(BeatmapLevel level, out VoteType voteType) { voteType = VoteType.Downvote; try { - if (!(level is CustomPreviewBeatmapLevel customLevel)) return false; - var hash = Utils.Utils.GetCustomLevelHash(customLevel); - if (Votes?.TryGetValue(hash.ToLower(), out var vote) == true) + var hash = Utils.Utils.GetCustomLevelHash(level); + if (hash != null && Votes?.TryGetValue(hash.ToLower(), out var vote) == true) { voteType = vote.VoteType; return true; @@ -122,16 +121,14 @@ public bool TryGetVote(IPreviewBeatmapLevel level, out VoteType voteType) return false; } - public void Vote(IPreviewBeatmapLevel level, VoteType voteType) + public void Vote(BeatmapLevel level, VoteType voteType) { Task.Run(() => { - if (!(level is CustomPreviewBeatmapLevel customLevel)) return; - lock (_voteWriteLock) { - var hash = Utils.Utils.GetCustomLevelHash(customLevel); - if (Votes != null) + var hash = Utils.Utils.GetCustomLevelHash(level); + if (hash != null && Votes != null) { hash = hash.ToLower(); if (!Votes.ContainsKey(hash) || Votes[hash].VoteType != voteType)