From 84cc5f01f58064607dd4ecbb819d7eb8e14c665a Mon Sep 17 00:00:00 2001 From: Bullet <68104413+XAce1337manX@users.noreply.github.com> Date: Sat, 16 Sep 2023 16:35:48 +1000 Subject: [PATCH] Fix for bookmarks that use bpm events --- BookmarkViewer/Patches/BookmarkPatches.cs | 31 +++++++++++++++++++++-- 1 file changed, 29 insertions(+), 2 deletions(-) diff --git a/BookmarkViewer/Patches/BookmarkPatches.cs b/BookmarkViewer/Patches/BookmarkPatches.cs index 1596224..8c36aba 100644 --- a/BookmarkViewer/Patches/BookmarkPatches.cs +++ b/BookmarkViewer/Patches/BookmarkPatches.cs @@ -147,14 +147,21 @@ static void ObtainBookmarksFromLevel(IBeatmapLevel level, BeatmapCharacteristicS if (mapData != null) { + var bookmarksUseBpmEvents = mapData.version2_6_0AndEarlier + ? mapData.customData.Get("_bookmarksUseOfficialBpmEvents") + : mapData.customData.Get("bookmarksUseOfficialBpmEvents"); + var bookmarkList = mapData.customData.Get>(mapData.version2_6_0AndEarlier ? "_bookmarks" : "bookmarks")?.Cast(); if (bookmarkList != null) { foreach (var bookmarkItem in bookmarkList) - { + { var bookmark = new Bookmark(); - bookmark.timeInSeconds = BeatsToSeconds(level.beatsPerMinute, bookmarkItem.Get(mapData.version2_6_0AndEarlier ? "_time" : "b")); + var bookmarkBeat = bookmarkItem.Get(mapData.version2_6_0AndEarlier ? "_time" : "b"); + bookmark.timeInSeconds = (bookmarksUseBpmEvents == true) + ? BeatsToSecondsWithBpmEvents(level.beatsPerMinute, bookmarkBeat, mapData.bpmEvents) + : BeatsToSeconds(level.beatsPerMinute, bookmarkBeat); bookmark.name = bookmarkItem.Get(mapData.version2_6_0AndEarlier ? "_name" : "n"); List? colorArray = bookmarkItem.Get>(mapData.version2_6_0AndEarlier ? "_color" : "c"); @@ -232,6 +239,26 @@ public static float BeatsToSeconds(float bpm, float beat) return (60.0f / bpm) * beat; } + private static float BeatsToSecondsWithBpmEvents(float levelBpm, float beat, IList bpmEvents) + { + // Start with level bpm if the bpm at beat 0 is missing for some reason + var defaultBpmEvent = new BeatmapSaveDataVersion3.BeatmapSaveData.BpmChangeEventData(0, levelBpm); + var bpmEventsBeforeBookmark = new List { defaultBpmEvent }; + bpmEventsBeforeBookmark.AddRange(bpmEvents.Where(x => x.beat <= beat)); + + var timeInSeconds = 0f; + for (var i = 0; i < bpmEventsBeforeBookmark.Count - 1; i++) + { + var bpmEvent = bpmEventsBeforeBookmark[i]; + var nextBpmEvent = bpmEventsBeforeBookmark[i + 1]; + + var timeDiff = nextBpmEvent.beat - bpmEvent.beat; + timeInSeconds += timeDiff * (60f / bpmEvent.bpm); + } + timeInSeconds += (beat - bpmEventsBeforeBookmark.Last().beat) * (60f / bpmEventsBeforeBookmark.Last().bpm); + return timeInSeconds; + } + static float GetBookmarkXPosition(float time, IBeatmapLevel level) { return Mathf.Lerp(_minX, _maxX, Mathf.InverseLerp(0, level.songDuration, time));