Skip to content

Commit

Permalink
Merge pull request #186 from BlazingTwist/vanilla-sorting
Browse files Browse the repository at this point in the history
Implement Vanilla Sorting Mode (sort custom songs in alphabetical order of level paths)
  • Loading branch information
halsafar authored Mar 18, 2022
2 parents 179a673 + 938a95e commit 38c04f1
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 0 deletions.
1 change: 1 addition & 0 deletions SongBrowserPlugin/Configuration/SongSortMode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public enum SongSortMode
Stars,
Bpm,
Length,
Vanilla,

// Allow mods to extend functionality.
Custom,
Expand Down
29 changes: 29 additions & 0 deletions SongBrowserPlugin/DataAccess/SongBrowserModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ public class SongBrowserModel
private double _customSongDirLastWriteTime = 0;
private readonly Dictionary<String, DateTime> _cachedLastWriteTimes;
private Dictionary<string, int> _levelIdToPlayCount;
private Dictionary<string, int> _cachedFileSystemOrder;

public BeatmapCharacteristicSO CurrentBeatmapCharacteristicSO;

Expand Down Expand Up @@ -62,6 +63,7 @@ public String LastSelectedLevelId
public SongBrowserModel()
{
_cachedLastWriteTimes = new Dictionary<String, DateTime>();
_cachedFileSystemOrder = new Dictionary<string, int>();
_levelIdToPlayCount = new Dictionary<string, int>();

LastScrollIndex = 0;
Expand Down Expand Up @@ -128,6 +130,17 @@ public void UpdateLevelRecords()
lastWriteTimer.Stop();
Logger.Info("Determining song download time and determining mappings took {0}ms", lastWriteTimer.ElapsedMilliseconds);

if (customSongDirChanged) {
Stopwatch fileSystemTimer = new Stopwatch();
fileSystemTimer.Start();
_cachedFileSystemOrder = SongCore.Loader.CustomLevels
.OrderBy(level => level.Value.customLevelPath)
.Select((level, index) => new { Identifier = level.Value.levelID, Index = index })
.ToDictionary(kvp => kvp.Identifier, kvp => kvp.Index);
fileSystemTimer.Stop();
Logger.Info("Determining filesystem song order took {0}ms", fileSystemTimer.ElapsedMilliseconds);
}

// Update song Infos, directory tree, and sort
this.UpdatePlayCounts();

Expand Down Expand Up @@ -303,6 +316,9 @@ public void ProcessSongList(IAnnotatedBeatmapLevelCollection selectedBeatmapColl
case SongSortMode.Mapper:
sortedSongs = SortMapper(filteredSongs);
break;
case SongSortMode.Vanilla:
sortedSongs = SortVanilla(filteredSongs);
break;
case SongSortMode.UpVotes:
sortedSongs = SortUpVotes(filteredSongs);
break;
Expand Down Expand Up @@ -597,6 +613,19 @@ private List<IPreviewBeatmapLevel> SortOriginal(List<IPreviewBeatmapLevel> level
Logger.Info("Sorting song list as original");
return levels;
}

/// <summary>
/// Sorting returns list sorted by alphabetical order of the directories they are contained in.
/// </summary>
/// <param name="levels"></param>
/// <returns></returns>
private List<IPreviewBeatmapLevel> SortVanilla(List<IPreviewBeatmapLevel> levels)
{
Logger.Info("Sorting song list by vanilla ordering.");
return levels
.OrderBy(level => _cachedFileSystemOrder.TryGetValue(level.levelID, out int index) ? index : int.MaxValue)
.ToList();
}

/// <summary>
/// Sorting by newest (file time, creation+modified).
Expand Down
1 change: 1 addition & 0 deletions SongBrowserPlugin/UI/Browser/SongBrowserUI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,7 @@ private void CreateSortButtons()
new KeyValuePair<string, SongSortMode>("Author", SongSortMode.Author),
new KeyValuePair<string, SongSortMode>("Mapper", SongSortMode.Mapper),
new KeyValuePair<string, SongSortMode>("Newest", SongSortMode.Newest),
new KeyValuePair<string, SongSortMode>("Vanilla", SongSortMode.Vanilla),
new KeyValuePair<string, SongSortMode>("#Plays", SongSortMode.YourPlayCount),
new KeyValuePair<string, SongSortMode>("BPM", SongSortMode.Bpm),
new KeyValuePair<string, SongSortMode>("Time", SongSortMode.Length),
Expand Down

0 comments on commit 38c04f1

Please sign in to comment.