Skip to content

Commit

Permalink
Merge pull request #1 from XAce1337manX/bookmark-bpm
Browse files Browse the repository at this point in the history
Fix for bookmarks that use bpm events
  • Loading branch information
ModdingPink authored Oct 15, 2023
2 parents 6f593c8 + 84cc5f0 commit 93f409a
Showing 1 changed file with 29 additions and 2 deletions.
31 changes: 29 additions & 2 deletions BookmarkViewer/Patches/BookmarkPatches.cs
Original file line number Diff line number Diff line change
Expand Up @@ -147,14 +147,21 @@ static void ObtainBookmarksFromLevel(IBeatmapLevel level, BeatmapCharacteristicS

if (mapData != null)
{
var bookmarksUseBpmEvents = mapData.version2_6_0AndEarlier
? mapData.customData.Get<bool?>("_bookmarksUseOfficialBpmEvents")
: mapData.customData.Get<bool?>("bookmarksUseOfficialBpmEvents");

var bookmarkList = mapData.customData.Get<List<object>>(mapData.version2_6_0AndEarlier ? "_bookmarks" : "bookmarks")?.Cast<CustomData>();
if (bookmarkList != null)
{
foreach (var bookmarkItem in bookmarkList)
{
{
var bookmark = new Bookmark();

bookmark.timeInSeconds = BeatsToSeconds(level.beatsPerMinute, bookmarkItem.Get<float>(mapData.version2_6_0AndEarlier ? "_time" : "b"));
var bookmarkBeat = bookmarkItem.Get<float>(mapData.version2_6_0AndEarlier ? "_time" : "b");
bookmark.timeInSeconds = (bookmarksUseBpmEvents == true)
? BeatsToSecondsWithBpmEvents(level.beatsPerMinute, bookmarkBeat, mapData.bpmEvents)
: BeatsToSeconds(level.beatsPerMinute, bookmarkBeat);
bookmark.name = bookmarkItem.Get<string>(mapData.version2_6_0AndEarlier ? "_name" : "n");

List<object>? colorArray = bookmarkItem.Get<List<object>>(mapData.version2_6_0AndEarlier ? "_color" : "c");
Expand Down Expand Up @@ -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<BeatmapSaveDataVersion3.BeatmapSaveData.BpmChangeEventData> 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<BeatmapSaveDataVersion3.BeatmapSaveData.BpmChangeEventData> { 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));
Expand Down

0 comments on commit 93f409a

Please sign in to comment.