1
0
Fork 0
mirror of https://github.com/ppy/osu-tools.git synced 2025-06-08 07:17:01 +09:00
osu-tools/PerformanceCalculatorGUI/ProcessorWorkingBeatmap.cs
2022-04-20 20:15:01 +03:00

82 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;
using PerformanceCalculatorGUI.API;
namespace PerformanceCalculatorGUI
{
/// <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"))
{
if (!File.Exists(fileOrId))
throw new ArgumentException($"Beatmap file {fileOrId} does not exist.");
return new ProcessorWorkingBeatmap(fileOrId);
}
if (!int.TryParse(fileOrId, out var 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, $"{APIManager.ENDPOINT_CONFIGURATION.WebsiteRootUrl}/osu/{beatmapId}").Perform();
}
return new ProcessorWorkingBeatmap(cachePath, beatmapId);
}
protected override IBeatmap GetBeatmap() => beatmap;
protected override Texture GetBackground() => null;
protected override Track GetBeatmapTrack() => null;
protected override ISkin GetSkin() => null;
public override Stream GetStream(string storagePath) => null;
}
}