Skip to content

Commit

Permalink
Update for Beat Saber 1.37.0
Browse files Browse the repository at this point in the history
  • Loading branch information
nicoco007 committed Jun 11, 2024
1 parent 6686b21 commit ee04928
Show file tree
Hide file tree
Showing 29 changed files with 280 additions and 239 deletions.
71 changes: 5 additions & 66 deletions PlaylistManager/AffinityPatches/LevelCollectionCellSetDataPatch.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
using System;
using UnityEngine.UI;
using BeatSaberPlaylistsLib.Types;
using System.Runtime.CompilerServices;
using PlaylistManager.Utilities;
using HMUI;
using HMUI;
using PlaylistManager.Configuration;
using PlaylistManager.Utilities;
using SiraUtil.Affinity;
using UnityEngine.UI;

/*
* Original Author: Auros
Expand All @@ -16,7 +13,6 @@ namespace PlaylistManager.AffinityPatches
{
internal class LevelCollectionCellSetDataPatch : IAffinity
{
private readonly ConditionalWeakTable<IStagedSpriteLoad, AnnotatedBeatmapLevelCollectionCell> eventTable = new();
private readonly HoverHintController hoverHintController;

public LevelCollectionCellSetDataPatch(HoverHintController hoverHintController)
Expand All @@ -25,26 +21,8 @@ public LevelCollectionCellSetDataPatch(HoverHintController hoverHintController)
}

[AffinityPatch(typeof(AnnotatedBeatmapLevelCollectionCell), nameof(AnnotatedBeatmapLevelCollectionCell.SetData))]
private void Patch(AnnotatedBeatmapLevelCollectionCell __instance, ref IAnnotatedBeatmapLevelCollection annotatedBeatmapLevelCollection, ref Image ____coverImage)
private void Patch(AnnotatedBeatmapLevelCollectionCell __instance, ref BeatmapLevelPack beatmapLevelPack, ref Image ____coverImage)
{
var cell = __instance;
if (annotatedBeatmapLevelCollection is IStagedSpriteLoad stagedSpriteLoad)
{
if (stagedSpriteLoad.SmallSpriteWasLoaded)
{
#if DEBUG
//Plugin.Log.Debug($"Sprite was already loaded for {(deferredSpriteLoad as IAnnotatedBeatmapLevelCollection).collectionName}");
#endif
}
if (eventTable.TryGetValue(stagedSpriteLoad, out var existing))
{
eventTable.Remove(stagedSpriteLoad);
}
eventTable.Add(stagedSpriteLoad, cell);
stagedSpriteLoad.SpriteLoaded -= OnSpriteLoaded;
stagedSpriteLoad.SpriteLoaded += OnSpriteLoaded;
}

if (PluginConfig.Instance.PlaylistHoverHints)
{
var hoverHint = __instance.GetComponent<HoverHint>();
Expand All @@ -55,46 +33,7 @@ private void Patch(AnnotatedBeatmapLevelCollectionCell __instance, ref IAnnotate
Accessors.HoverHintControllerAccessor(ref hoverHint) = hoverHintController;
}

hoverHint.text = annotatedBeatmapLevelCollection.collectionName;
}
}

private void OnSpriteLoaded(object sender, EventArgs e)
{
if (sender is IStagedSpriteLoad stagedSpriteLoad)
{
if (eventTable.TryGetValue(stagedSpriteLoad, out var tableCell))
{
if (tableCell == null)
{
stagedSpriteLoad.SpriteLoaded -= OnSpriteLoaded;
return;
}

var collection = tableCell._annotatedBeatmapLevelCollection;
if (collection == stagedSpriteLoad)
{
#if DEBUG
//Plugin.Log.Debug($"Updating image for {collection.collectionName}");
#endif
tableCell._coverImage.sprite = stagedSpriteLoad.SmallSprite;
}
else
{
//Plugin.Log.Warn($"Collection '{collection.collectionName}' is not {(deferredSpriteLoad as IAnnotatedBeatmapLevelCollection).collectionName}");
eventTable.Remove(stagedSpriteLoad);
stagedSpriteLoad.SpriteLoaded -= OnSpriteLoaded;
}
}
else
{
//Plugin.Log.Warn($"{(deferredSpriteLoad as IAnnotatedBeatmapLevelCollection).collectionName} is not in the EventTable.");
stagedSpriteLoad.SpriteLoaded -= OnSpriteLoaded;
}
}
else
{
//Plugin.Log.Warn($"Wrong sender type for deferred sprite load: {sender?.GetType().Name ?? "<NULL>"}");
hoverHint.text = beatmapLevelPack.packName;
}
}
}
Expand Down
3 changes: 2 additions & 1 deletion PlaylistManager/Downloaders/PlaylistSequentialDownloader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ internal void OnQueueClear()
}
}

private void OnSongsLoaded(Loader arg1, ConcurrentDictionary<string, CustomPreviewBeatmapLevel> arg2)
private void OnSongsLoaded(Loader loader, ConcurrentDictionary<string, BeatmapLevel> beatmapLevels)
{
Loader.SongsLoadedEvent -= OnSongsLoaded;
foreach (var playlist in coversToRefresh)
Expand Down Expand Up @@ -239,6 +239,7 @@ private async Task DownloadPlaylist(DownloadQueueEntry downloadQueueEntry)
}
}

downloadQueueEntry.playlist.RaisePlaylistChanged();
downloadQueueEntry.parentManager.StorePlaylist(downloadQueueEntry.playlist);

if (downloadQueueEntry.playlist is BeatSaberPlaylistsLib.Types.Playlist playlist)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ namespace PlaylistManager.HarmonyPatches
[HarmonyPatch(typeof(AnnotatedBeatmapLevelCollectionCell), nameof(AnnotatedBeatmapLevelCollectionCell.RefreshAvailabilityAsync))]
internal class AnnotatedBeatmapLevelCollectionCell_RefreshAvailabilityAsync
{
private static void Postfix(AnnotatedBeatmapLevelCollectionCell __instance, IAnnotatedBeatmapLevelCollection ____annotatedBeatmapLevelCollection)
private static void Postfix(AnnotatedBeatmapLevelCollectionCell __instance, BeatmapLevelPack ____beatmapLevelPack)
{
if (____annotatedBeatmapLevelCollection is BeatSaberPlaylistsLib.Types.IPlaylist playlist)
if (____beatmapLevelPack is BeatSaberPlaylistsLib.Types.IPlaylist playlist)
{
__instance.SetDownloadIconVisible(PluginConfig.Instance.ShowDownloadIcon && PlaylistLibUtils.GetMissingSongs(playlist).Count > 0);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
using BeatSaberPlaylistsLib.Types;
using HarmonyLib;
using System;
using System.Collections.Generic;
using System.Linq;

namespace PlaylistManager.HarmonyPatches
{
[HarmonyPatch(typeof(AnnotatedBeatmapLevelCollectionsViewController), nameof(AnnotatedBeatmapLevelCollectionsViewController.SetData))]
internal class AnnotatedBeatmapLevelCollectionsViewController_SetData
{
private static readonly Dictionary<IPlaylist, AnnotatedBeatmapLevelCollectionsViewController> eventTable = new();

private static void Postfix(AnnotatedBeatmapLevelCollectionsViewController __instance, IReadOnlyList<BeatmapLevelPack> annotatedBeatmapLevelCollections)
{
foreach (PlaylistLevelPack playlistLevelPack in annotatedBeatmapLevelCollections.OfType<PlaylistLevelPack>())
{
IPlaylist playlist = playlistLevelPack.playlist;

if (playlist.SmallSpriteWasLoaded)
{
continue;
}

eventTable.Remove(playlist);
eventTable.Add(playlist, __instance);
playlist.SpriteLoaded -= OnSpriteLoaded;
playlist.SpriteLoaded += OnSpriteLoaded;
}
}

private static void OnSpriteLoaded(object sender, EventArgs e)
{
if (sender is not IPlaylist playlist)
{
return;
}

playlist.SpriteLoaded -= OnSpriteLoaded;

if (!eventTable.TryGetValue(playlist, out var vc) || vc == null)
{
return;
}

var annotatedBeatmapLevelCollections = CloneAndOverwriteEntry(vc._annotatedBeatmapLevelCollections, playlist.PlaylistLevelPack);
vc._annotatedBeatmapLevelCollections = annotatedBeatmapLevelCollections;
vc._annotatedBeatmapLevelCollectionsGridView.SetData(annotatedBeatmapLevelCollections);
}

private static IReadOnlyList<BeatmapLevelPack> CloneAndOverwriteEntry(IReadOnlyList<BeatmapLevelPack> original, BeatmapLevelPack item)
{
BeatmapLevelPack[] beatmapLevelPackCollection = new BeatmapLevelPack[original.Count];

for (int i = 0; i < beatmapLevelPackCollection.Length; ++i)
{
if (original[i].packID == item.packID)
{
beatmapLevelPackCollection[i] = item;
}
else
{
beatmapLevelPackCollection[i] = original[i];
}
}

return beatmapLevelPackCollection;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
using BeatSaberPlaylistsLib.Types;
using HarmonyLib;
using HarmonyLib;
using HMUI;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Reflection.Emit;

namespace PlaylistManager.HarmonyPatches
{
Expand All @@ -14,7 +10,7 @@ namespace PlaylistManager.HarmonyPatches
typeof(TableView), typeof(int)})]
public class LevelCollectionTableView_HandleDidSelectRowEvent
{
internal static event Action<IPreviewBeatmapLevel> DidSelectLevelEvent;
internal static event Action<BeatmapLevel> DidSelectLevelEvent;
internal static void Prefix(int row, bool ____showLevelPackHeader)
{
if (____showLevelPackHeader)
Expand Down
34 changes: 0 additions & 34 deletions PlaylistManager/HarmonyPatches/LevelCollectionTableView_SetData.cs

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using HarmonyLib;

namespace PlaylistManager.HarmonyPatches
Expand All @@ -7,25 +8,11 @@ namespace PlaylistManager.HarmonyPatches
[HarmonyPatch("SetData", MethodType.Normal)]
public class LevelCollectionViewController_SetData
{
internal static IReadOnlyCollection<IPreviewBeatmapLevel> beatmapLevels;
internal static void Prefix(ref IBeatmapLevelCollection beatmapLevelCollection)
internal static IReadOnlyCollection<BeatmapLevel> beatmapLevels { get; private set; }

internal static void Prefix(ref BeatmapLevel[] beatmapLevels)
{
if (beatmapLevelCollection == null)
{
beatmapLevels = new IPreviewBeatmapLevel[0];
}
else if (beatmapLevelCollection is BeatSaberPlaylistsLib.Legacy.LegacyPlaylist legacyPlaylist)
{
beatmapLevels = legacyPlaylist.BeatmapLevels;
}
else if (beatmapLevelCollection is BeatSaberPlaylistsLib.Blist.BlistPlaylist blistPlaylist)
{
beatmapLevels = blistPlaylist.BeatmapLevels;
}
else
{
beatmapLevels = beatmapLevelCollection.beatmapLevels;
}
LevelCollectionViewController_SetData.beatmapLevels = beatmapLevels ?? Array.Empty<BeatmapLevel>();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,12 @@ namespace PlaylistManager.HarmonyPatches
public class LevelFilteringNavigationController_ShowPacksInChildController
{
internal static event Action AllPacksViewSelectedEvent;
internal static void Prefix(ref IReadOnlyList<IBeatmapLevelPack> beatmapLevelPacks, ref SelectLevelCategoryViewController ____selectLevelCategoryViewController)

internal static void Prefix(ref IReadOnlyList<BeatmapLevelPack> beatmapLevelPacks, ref SelectLevelCategoryViewController ____selectLevelCategoryViewController)
{
if (____selectLevelCategoryViewController.selectedLevelCategory == SelectLevelCategoryViewController.LevelCategory.CustomSongs)
{
beatmapLevelPacks = beatmapLevelPacks.ToArray().AddRangeToArray(PlaylistLibUtils.TryGetAllPlaylists());
beatmapLevelPacks = beatmapLevelPacks.ToArray().AddRangeToArray(PlaylistLibUtils.TryGetAllPlaylistsAsLevelPacks());
AllPacksViewSelectedEvent?.Invoke();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace PlaylistManager.HarmonyPatches
[HarmonyPatch(typeof(LevelPackDetailViewController), nameof(LevelPackDetailViewController.ShowContent))]
internal class LevelPackDetailViewController_ShowContent
{
private static bool Prefix(LevelPackDetailViewController.ContentType contentType, IBeatmapLevelPack ____pack, ImageView ____packImage, Sprite ____blurredPackArtwork, GameObject ____detailWrapper, LoadingControl ____loadingControl)
private static bool Prefix(LevelPackDetailViewController.ContentType contentType, BeatmapLevelPack ____pack, ImageView ____packImage, Sprite ____blurredPackArtwork, GameObject ____detailWrapper, LoadingControl ____loadingControl)
{
if (contentType == LevelPackDetailViewController.ContentType.Owned && ____pack is BeatSaberPlaylistsLib.Types.IPlaylist)
{
Expand Down
2 changes: 1 addition & 1 deletion PlaylistManager/Interfaces/ILevelCollectionUpdater.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@
{
interface ILevelCollectionUpdater
{
public void LevelCollectionUpdated(IAnnotatedBeatmapLevelCollection annotatedBeatmapLevelCollection, BeatSaberPlaylistsLib.PlaylistManager parentManager);
public void LevelCollectionUpdated(BeatmapLevelPack annotatedBeatmapLevelCollection, BeatSaberPlaylistsLib.PlaylistManager parentManager);
}
}
3 changes: 2 additions & 1 deletion PlaylistManager/Interfaces/ILevelCollectionsTableUpdater.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
using System;
using System.Collections.Generic;

namespace PlaylistManager.Interfaces
{
interface ILevelCollectionsTableUpdater
{
public event Action<IAnnotatedBeatmapLevelCollection[], int> LevelCollectionTableViewUpdatedEvent;
public event Action<IReadOnlyList<BeatmapLevelPack>, int> LevelCollectionTableViewUpdatedEvent;
}
}
2 changes: 1 addition & 1 deletion PlaylistManager/Interfaces/IPreviewBeatmapLevelUpdater.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@
{
interface IPreviewBeatmapLevelUpdater
{
public void PreviewBeatmapLevelUpdated(IPreviewBeatmapLevel beatmapLevel);
public void PreviewBeatmapLevelUpdated(BeatmapLevel beatmapLevel);
}
}
Loading

0 comments on commit ee04928

Please sign in to comment.