// Copyright (c) ppy Pty Ltd . 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 { /// /// A which reads from a .osu file. /// public class ProcessorWorkingBeatmap : WorkingBeatmap { private readonly Beatmap beatmap; /// /// Constructs a new from a .osu file. /// /// The .osu file. /// An optional beatmap ID (for cases where .osu file doesn't have one). 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(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; } }