Skip to content

Commit

Permalink
Merge pull request rithik-b#27 from rithik-b/dev/more-optimizations
Browse files Browse the repository at this point in the history
Hotfix for cover loading performance
  • Loading branch information
Zingabopp authored Apr 4, 2022
2 parents a3fa1e1 + 87d733e commit 813d003
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 26 deletions.
66 changes: 41 additions & 25 deletions BeatSaberPlaylistsLib.BeatSaber/Types/Playlist.BeatSaber.cs
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,9 @@ public BeatSaber.IPreviewBeatmapLevel[] BeatmapLevels


#region Default Cover

private static SemaphoreSlim _defaultCoverSemaphore = new SemaphoreSlim(1, 1);

/// <inheritdoc cref="IPlaylist.GetDefaultCoverStream" />
public async Task<Stream?> GetDefaultCoverStream()
{
Expand All @@ -317,39 +320,52 @@ public BeatSaber.IPreviewBeatmapLevel[] BeatmapLevels
return null;
}

await _defaultCoverSemaphore.WaitAsync();
var ms = new MemoryStream();
var beatmapLevels = ((IBeatmapLevelCollection) this).beatmapLevels;

if (beatmapLevels.Count == 1)
{
using var coverStream = Utilities.GetStreamFromBeatmap(beatmapLevels[0]);
if (coverStream != null) await coverStream.CopyToAsync(ms);
}
else if (beatmapLevels.Count == 2)
try
{
using var imageStream1 = Utilities.GetStreamFromBeatmap(beatmapLevels[0]);
using var imageStream2 = Utilities.GetStreamFromBeatmap(beatmapLevels[1]);
using var coverStream = await ImageUtilities.GenerateCollage(imageStream1 ?? Stream.Null, imageStream2 ?? Stream.Null);
await coverStream.CopyToAsync(ms);
var beatmapLevels = ((IBeatmapLevelCollection) this).beatmapLevels;

if (beatmapLevels.Count == 1)
{
using var coverStream = Utilities.GetStreamFromBeatmap(beatmapLevels[0]);
if (coverStream != null) await coverStream.CopyToAsync(ms);
}
else if (beatmapLevels.Count == 2)
{
using var imageStream1 = Utilities.GetStreamFromBeatmap(beatmapLevels[0]);
using var imageStream2 = Utilities.GetStreamFromBeatmap(beatmapLevels[1]);
using var coverStream = await ImageUtilities.GenerateCollage(imageStream1 ?? Stream.Null, imageStream2 ?? Stream.Null);
await coverStream.CopyToAsync(ms);
}
else if (beatmapLevels.Count == 3)
{
using var imageStream1 = Utilities.GetStreamFromBeatmap(beatmapLevels[0]);
using var imageStream2 = Utilities.GetStreamFromBeatmap(beatmapLevels[1]);
using var imageStream3 = Utilities.GetStreamFromBeatmap(beatmapLevels[2]);
using var coverStream = await ImageUtilities.GenerateCollage(imageStream1 ?? Stream.Null, imageStream2 ?? Stream.Null, imageStream3 ?? Stream.Null);
await coverStream.CopyToAsync(ms);
}
else
{
using var imageStream1 = Utilities.GetStreamFromBeatmap(beatmapLevels[0]);
using var imageStream2 = Utilities.GetStreamFromBeatmap(beatmapLevels[1]);
using var imageStream3 = Utilities.GetStreamFromBeatmap(beatmapLevels[2]);
using var imageStream4 = Utilities.GetStreamFromBeatmap(beatmapLevels[3]);
using var coverStream = await ImageUtilities.GenerateCollage(imageStream1 ?? Stream.Null, imageStream2 ?? Stream.Null, imageStream3 ?? Stream.Null, imageStream4 ?? Stream.Null);
await coverStream.CopyToAsync(ms);
}
}
else if (beatmapLevels.Count == 3)
catch (Exception)
{
using var imageStream1 = Utilities.GetStreamFromBeatmap(beatmapLevels[0]);
using var imageStream2 = Utilities.GetStreamFromBeatmap(beatmapLevels[1]);
using var imageStream3 = Utilities.GetStreamFromBeatmap(beatmapLevels[2]);
using var coverStream = await ImageUtilities.GenerateCollage(imageStream1 ?? Stream.Null, imageStream2 ?? Stream.Null, imageStream3 ?? Stream.Null);
await coverStream.CopyToAsync(ms);
// ignored
}
else
finally
{
using var imageStream1 = Utilities.GetStreamFromBeatmap(beatmapLevels[0]);
using var imageStream2 = Utilities.GetStreamFromBeatmap(beatmapLevels[1]);
using var imageStream3 = Utilities.GetStreamFromBeatmap(beatmapLevels[2]);
using var imageStream4 = Utilities.GetStreamFromBeatmap(beatmapLevels[3]);
using var coverStream = await ImageUtilities.GenerateCollage(imageStream1 ?? Stream.Null, imageStream2 ?? Stream.Null, imageStream3 ?? Stream.Null, imageStream4 ?? Stream.Null);
await coverStream.CopyToAsync(ms);
_defaultCoverSemaphore.Release();
}

_defaultCoverData = ms.ToArray();
return ms;
}
Expand Down
2 changes: 1 addition & 1 deletion Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<Project>
<PropertyGroup>
<Authors>Zingabopp</Authors>
<Version>1.6.5</Version>
<Version>1.6.6</Version>
<Copyright>Copyright © Zingabopp 2021</Copyright>
<PackageProjectUrl>https://github.com/Zingabopp/BeatSaberPlaylistsLib</PackageProjectUrl>
<RepositoryUrl>https://github.com/Zingabopp/BeatSaberPlaylistsLib</RepositoryUrl>
Expand Down

0 comments on commit 813d003

Please sign in to comment.