Skip to content

Commit

Permalink
Fix: Lazer realm load times
Browse files Browse the repository at this point in the history
  • Loading branch information
Piotrekol committed Jan 31, 2025
1 parent 46055f0 commit 12d7104
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 24 deletions.
11 changes: 8 additions & 3 deletions CollectionManagerDll/DataTypes/LazerBeatmap.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
namespace CollectionManager.DataTypes;
using CollectionManager.Extensions;
using System.Linq;

namespace CollectionManager.DataTypes;

public class LazerBeatmap
: BeatmapExtension
{
public string AudioRelativeFilePath { get; set; }
public string BackgroundRelativeFilePath { get; set; }
public string BackgroundFileName { get; set; }
public string AudioRelativeFilePath => SetFiles.FirstOrDefault(f => f.FileName == Mp3Name)?.RelativeRealmFilePath;
public string BackgroundRelativeFilePath => SetFiles.FirstOrDefault(f => f.FileName == BackgroundFileName)?.RelativeRealmFilePath;
public string MapHash { get; set; }
public LazerFile[] SetFiles { get; set; }
}
11 changes: 11 additions & 0 deletions CollectionManagerDll/DataTypes/LazerFile.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using System.IO;

namespace CollectionManager.DataTypes;

public class LazerFile(string Hash, string FileName)
{
public string Hash { get; } = Hash;
public string FileName { get; } = FileName;

public string RelativeRealmFilePath => Path.Combine(Hash.Remove(1), Hash.Remove(2), Hash);
}
29 changes: 15 additions & 14 deletions CollectionManagerDll/Extensions/BeatmapInfoExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,28 @@
using CollectionManager.Modules.FileIO.OsuLazerDb.RealmModels;
using System;
using System.IO;
using System.Linq;
using System.Collections.Generic;

namespace CollectionManager.Extensions;

internal static class BeatmapInfoExtensions
{
public static LazerBeatmap ToLazerBeatmap(this BeatmapInfo beatmapInfo, IScoreDataManager scoreDataManager)
public static LazerBeatmap ToLazerBeatmap(this BeatmapInfo beatmapInfo, IScoreDataManager scoreDataManager, LazerFile[] setFiles)
{
LazerBeatmap lazerBeatmap = new()
{
AudioRelativeFilePath = beatmapInfo.AudioFile is not null ? GetRelativePath(beatmapInfo.AudioFile.File) : string.Empty,
BackgroundRelativeFilePath = beatmapInfo.BackgroundFile is not null ? GetRelativePath(beatmapInfo.BackgroundFile.File) : string.Empty,
SetFiles = setFiles,
BackgroundFileName = beatmapInfo.Metadata.BackgroundFile,
TitleUnicode = beatmapInfo.Metadata.TitleUnicode,
TitleRoman = beatmapInfo.Metadata.Title,
ArtistUnicode = beatmapInfo.Metadata.ArtistUnicode,
ArtistRoman = beatmapInfo.Metadata.Artist,
Creator = beatmapInfo.Metadata.Author.Username,
DiffName = beatmapInfo.DifficultyName,
Mp3Name = beatmapInfo.AudioFile?.Filename ?? string.Empty,
Md5 = beatmapInfo.MD5Hash,
MapHash = beatmapInfo.Hash,
OsuFileName = beatmapInfo.File?.File.Hash ?? string.Empty,
Mp3Name = beatmapInfo.Metadata.AudioFile,
Tags = beatmapInfo.Metadata.Tags,
State = (byte)ToLocalState(beatmapInfo.Status),
EditDate = beatmapInfo.BeatmapSet.DateAdded,
Expand All @@ -38,16 +39,10 @@ public static LazerBeatmap ToLazerBeatmap(this BeatmapInfo beatmapInfo, IScoreDa
PreviewTime = beatmapInfo.Metadata.PreviewTime,
MapId = beatmapInfo.OnlineID,
MapSetId = beatmapInfo.BeatmapSet.OnlineID,
Offset = beatmapInfo.UserSettings.Offset,
StackLeniency = -1, // TODO: Lazer StackLeniency
PlayMode = ToPlayMode(beatmapInfo.Ruleset.ShortName),
Source = beatmapInfo.Metadata.Source,
LastPlayed = beatmapInfo.LastPlayed,
LastSync = beatmapInfo.LastOnlineUpdate,
DisableHitsounds = false,
DisableSkin = false,
DisableSb = false,
BgDim = 0,
MainBpm = beatmapInfo.BPM,
OsuGrade = scoreDataManager.GetTopReplayGrade(beatmapInfo.Hash, PlayMode.Osu),
TaikoGrade = scoreDataManager.GetTopReplayGrade(beatmapInfo.Hash, PlayMode.Taiko),
Expand All @@ -57,6 +52,10 @@ public static LazerBeatmap ToLazerBeatmap(this BeatmapInfo beatmapInfo, IScoreDa
// not relevant in lazer
//Dir = string.Empty,

// Never used anywhere
//OsuFileName = beatmapInfo.Hash,
//Offset = beatmapInfo.UserSettings.Offset,

// Not stored in realm
//DrainingTime = 0,
//Circles = 0,
Expand All @@ -70,6 +69,11 @@ public static LazerBeatmap ToLazerBeatmap(this BeatmapInfo beatmapInfo, IScoreDa
//IsOsz2 = false,
//MinBpm = 0,
//MaxBpm = 0,
//StackLeniency = -1,
//DisableHitsounds = false,
//DisableSkin = false,
//DisableSb = false,
//BgDim = 0,
};

lazerBeatmap.ModPpStars.Add(
Expand All @@ -79,9 +83,6 @@ public static LazerBeatmap ToLazerBeatmap(this BeatmapInfo beatmapInfo, IScoreDa
return lazerBeatmap;
}

private static string GetRelativePath(RealmFile realmFile)
=> Path.Combine(realmFile.Hash.Remove(1), realmFile.Hash.Remove(2), realmFile.Hash);

private static PlayMode ToPlayMode(string rulesetShortName)
=> rulesetShortName.ToLowerInvariant() switch
{
Expand Down
16 changes: 16 additions & 0 deletions CollectionManagerDll/Extensions/BeatmapSetInfoExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using CollectionManager.DataTypes;
using CollectionManager.Interfaces;
using CollectionManager.Modules.FileIO.OsuLazerDb.RealmModels;
using System.Collections.Generic;
using System.Linq;

namespace CollectionManager.Extensions;
internal static class BeatmapSetInfoExtensions
{
public static IEnumerable<LazerBeatmap> ToLazerBeatmaps(this BeatmapSetInfo beatmapSetInfo, IScoreDataManager scoreDataManager)
{
LazerFile[] setFiles = beatmapSetInfo.Files.Select(namedFile => namedFile.ToLazerFile()).ToArray();

return beatmapSetInfo.Beatmaps.Select(beatmap => beatmap.ToLazerBeatmap(scoreDataManager, setFiles));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using CollectionManager.DataTypes;
using CollectionManager.Modules.FileIO.OsuLazerDb.RealmModels;

namespace CollectionManager.Extensions;
internal static class RealmNamedFileUsageExtensions
{
public static LazerFile ToLazerFile(this RealmNamedFileUsage realmNamedFile)
=> new(realmNamedFile.File.Hash, realmNamedFile.Filename);
}
27 changes: 20 additions & 7 deletions CollectionManagerDll/Modules/FileIO/OsuLazerDb/OsuLazerDatabase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System;
using System.Linq;
using System.Threading;
using System.Collections.Generic;

namespace CollectionManager.Modules.FileIO.OsuLazerDb;

Expand All @@ -14,7 +15,6 @@ public sealed class OsuLazerDatabase
{
private readonly IMapDataManager _mapDataManager;
private readonly IScoreDataManager _scoresDatabase;
private readonly LazerBeatmap _baseBeatmap = new();

public OsuLazerDatabase(IMapDataManager mapDataManager, IScoreDataManager scoresDatabase)
{
Expand Down Expand Up @@ -47,17 +47,30 @@ private void LoadScores(Realm realm, IProgress<string> progress)

private void LoadBeatmaps(Realm realm, IProgress<string> progress)
{
IQueryable<BeatmapInfo> allLazerBeatmaps = realm.All<BeatmapInfo>();
int beatmapsCount = allLazerBeatmaps.Count();
progress?.Report($"Loading {beatmapsCount} beatmaps");
var allLazerBeatmapSets = realm.All<BeatmapSetInfo>().ToList();
int beatmapSetCount = allLazerBeatmapSets.Count;
progress?.Report($"Loading {beatmapSetCount} beatmap sets");
_mapDataManager.StartMassStoring();
var totalBeatmapCount = 0;

foreach (BeatmapInfo beatmapInfo in allLazerBeatmaps)
for (int i = 0; i < allLazerBeatmapSets.Count; i++)
{
_mapDataManager.StoreBeatmap(beatmapInfo.ToLazerBeatmap(_scoresDatabase));
var beatmapSetInfo = allLazerBeatmapSets[i];
IEnumerable<LazerBeatmap> lazerBeatmaps = beatmapSetInfo.ToLazerBeatmaps(_scoresDatabase);

foreach (var lazerBeatmap in lazerBeatmaps)
{
totalBeatmapCount++;
_mapDataManager.StoreBeatmap(lazerBeatmap);
}

if (i % 100 == 0)
{
progress?.Report($"Loaded {i} of {beatmapSetCount} beatmap sets ({totalBeatmapCount} beatmaps)");
}
}

_mapDataManager.EndMassStoring();
progress?.Report($"Loaded {beatmapsCount} beatmaps");
progress?.Report($"Loaded {beatmapSetCount} beatmap sets ({totalBeatmapCount} beatmaps)");
}
}

0 comments on commit 12d7104

Please sign in to comment.