1
0
Fork 0
mirror of https://github.com/ppy/osu-tools.git synced 2025-06-07 23:07:01 +09:00
osu-tools/PerformanceCalculator/ProcessorWorkingBeatmap.cs
2025-02-20 09:42:05 +01:00

81 lines
3 KiB
C#

// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
// See the LICENCE file in the repository root for full licence text.
using System;
using System.IO;
using osu.Framework.Audio.Track;
using osu.Framework.Graphics.Textures;
using osu.Framework.IO.Network;
using osu.Game.Beatmaps;
using osu.Game.Beatmaps.Formats;
using osu.Game.IO;
using osu.Game.Skinning;
namespace PerformanceCalculator
{
/// <summary>
/// A <see cref="WorkingBeatmap"/> which reads from a .osu file.
/// </summary>
public class ProcessorWorkingBeatmap : WorkingBeatmap
{
private readonly Beatmap beatmap;
/// <summary>
/// Constructs a new <see cref="ProcessorWorkingBeatmap"/> from a .osu file.
/// </summary>
/// <param name="file">The .osu file.</param>
/// <param name="beatmapId">An optional beatmap ID (for cases where .osu file doesn't have one).</param>
public ProcessorWorkingBeatmap(string file, int? beatmapId = null)
: this(readFromFile(file), beatmapId)
{
}
private ProcessorWorkingBeatmap(Beatmap beatmap, int? beatmapId = null)
: base(beatmap.BeatmapInfo, null)
{
this.beatmap = beatmap;
beatmap.BeatmapInfo.Ruleset = LegacyHelper.GetRulesetFromLegacyID(beatmap.BeatmapInfo.Ruleset.OnlineID).RulesetInfo;
if (beatmapId.HasValue)
beatmap.BeatmapInfo.OnlineID = beatmapId.Value;
}
private static Beatmap readFromFile(string filename)
{
using (var stream = File.OpenRead(filename))
using (var reader = new LineBufferedReader(stream))
return Decoder.GetDecoder<Beatmap>(reader).Decode(reader);
}
public static ProcessorWorkingBeatmap FromFileOrId(string fileOrId)
{
if (fileOrId.EndsWith(".osu", StringComparison.Ordinal))
{
if (!File.Exists(fileOrId))
throw new ArgumentException($"Beatmap file {fileOrId} does not exist.");
return new ProcessorWorkingBeatmap(fileOrId);
}
if (!int.TryParse(fileOrId, out int beatmapId))
throw new ArgumentException("Could not parse provided beatmap ID.");
string cachePath = Path.Combine("cache", $"{beatmapId}.osu");
if (!File.Exists(cachePath))
{
Console.WriteLine($"Downloading {beatmapId}.osu...");
new FileWebRequest(cachePath, $"{Program.ENDPOINT_CONFIGURATION.WebsiteUrl}/osu/{beatmapId}").Perform();
}
return new ProcessorWorkingBeatmap(cachePath, beatmapId);
}
protected override IBeatmap GetBeatmap() => beatmap;
public override Texture GetBackground() => null;
protected override Track GetBeatmapTrack() => null;
protected override ISkin GetSkin() => null;
public override Stream GetStream(string storagePath) => null;
}
}