Skip to content

Commit

Permalink
Misc: Small Refactoring of beatmap loader
Browse files Browse the repository at this point in the history
  • Loading branch information
Piotrekol committed Mar 31, 2021
1 parent 1c54f82 commit 9a57e3e
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 24 deletions.
56 changes: 34 additions & 22 deletions plugins/OsuSongsFolderWatcher/LazerMapLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,28 +44,9 @@ private LazerMapLoader()
/// <returns></returns>
public Beatmap LoadBeatmap(string file)
{
IBeatmap lazerBeatmap;
DifficultyAttributes difficultyAttributes;
using (var raw = File.OpenRead(file))
using (var ms = new MemoryStream())
using (var sr = new LineBufferedReader(ms))
{
raw.CopyTo(ms);
ms.Position = 0;

var decoder = Decoder.GetDecoder<osu.Game.Beatmaps.Beatmap>(sr);
lazerBeatmap = decoder.Decode(sr);

lazerBeatmap.BeatmapInfo.Path = Path.GetFileName(file);
lazerBeatmap.BeatmapInfo.MD5Hash = ms.ComputeMD5Hash();

var ruleset = Rulesets.GetOrDefault(lazerBeatmap.BeatmapInfo.RulesetID);
lazerBeatmap.BeatmapInfo.Ruleset = ruleset.RulesetInfo;
difficultyAttributes = ruleset?.CreateDifficultyCalculator(new DummyConversionBeatmap(lazerBeatmap)).Calculate();

lazerBeatmap.BeatmapInfo.StarDifficulty = Math.Round(difficultyAttributes?.StarRating ?? 0, 2);
lazerBeatmap.BeatmapInfo.Length = CalculateLength(lazerBeatmap);
}
var (lazerBeatmap, difficultyAttributes) = LoadLazerBeatmap(file);
if (lazerBeatmap == null)
return null;

short circles, sliders, spinners;
circles = sliders = spinners = 0;
Expand Down Expand Up @@ -116,6 +97,37 @@ public Beatmap LoadBeatmap(string file)
};
}

private (IBeatmap lazerBeatmap, DifficultyAttributes difficultyAttributes) LoadLazerBeatmap(string file)
{
IBeatmap lazerBeatmap;
DifficultyAttributes difficultyAttributes;
using (var raw = File.OpenRead(file))
using (var ms = new MemoryStream())
using (var sr = new LineBufferedReader(ms))
{
raw.CopyTo(ms);
ms.Position = 0;

var decoder = Decoder.GetDecoder<osu.Game.Beatmaps.Beatmap>(sr);
lazerBeatmap = decoder.Decode(sr);

lazerBeatmap.BeatmapInfo.Path = Path.GetFileName(file);
lazerBeatmap.BeatmapInfo.MD5Hash = ms.ComputeMD5Hash();

var ruleset = Rulesets.GetOrDefault(lazerBeatmap.BeatmapInfo.RulesetID);
if (ruleset == null)
return (null, null);

lazerBeatmap.BeatmapInfo.Ruleset = ruleset.RulesetInfo;
difficultyAttributes = ruleset.CreateDifficultyCalculator(new DummyConversionBeatmap(lazerBeatmap)).Calculate();

lazerBeatmap.BeatmapInfo.StarDifficulty = Math.Round(difficultyAttributes?.StarRating ?? 0, 2);
lazerBeatmap.BeatmapInfo.Length = CalculateLength(lazerBeatmap);
}

return (lazerBeatmap, difficultyAttributes);
}

private double CalculateLength(IBeatmap b)
{
if (!b.HitObjects.Any())
Expand Down
25 changes: 23 additions & 2 deletions plugins/OsuSongsFolderWatcher/osuSongsFolderWatcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -111,11 +111,31 @@ private async Task ConsumerTask(CancellationToken token)
_settings.Add(_names.LoadingRawBeatmaps.Name, true);
Interlocked.Increment(ref _numberOfBeatmapsCurrentlyBeingLoaded);
_logger.Log($">Processing beatmap located at {fsArgs.FullPath}", LogLevel.Debug);
Beatmap beatmap;
try
{
beatmap = await BeatmapHelpers.ReadBeatmap(fsArgs.FullPath);
}
catch (Exception ex)
{
ex.Data["PreventedCrash"] = 1;
_logger.Log(ex, LogLevel.Critical);
Interlocked.Decrement(ref _numberOfBeatmapsCurrentlyBeingLoaded);
continue;
}

var beatmap = await BeatmapHelpers.ReadBeatmap(fsArgs.FullPath);
if (beatmap == null)
{
var ex = new BeatmapLoadFailedException();
ex.Data["location"] = fsArgs.FullPath;
ex.Data["changeType"] = fsArgs.ChangeType;
_logger.Log(ex, LogLevel.Critical);
_logger.Log($"Failed to load beatmap located at {fsArgs.FullPath}", LogLevel.Warning);
Interlocked.Decrement(ref _numberOfBeatmapsCurrentlyBeingLoaded);
continue;
}

_databaseController.StoreTempBeatmap(beatmap);

_logger.Log(">Added new Temporary beatmap {0} - {1} [{2}]", LogLevel.Information, beatmap.ArtistRoman,
beatmap.TitleRoman, beatmap.DiffName);
if (Interlocked.Decrement(ref _numberOfBeatmapsCurrentlyBeingLoaded) == 0)
Expand Down Expand Up @@ -167,4 +187,5 @@ public IMapSearchResult FindBeatmap(IMapSearchArgs searchArgs, CancellationToken
public string SearcherName { get; } = nameof(OsuSongsFolderWatcher);
public int Priority { get; set; } = 1000;
}
internal class BeatmapLoadFailedException : Exception { }
}

0 comments on commit 9a57e3e

Please sign in to comment.