From e5dcfc311390a22b0b1fd6ad880980e90bc292ca Mon Sep 17 00:00:00 2001 From: Dan Balasescu Date: Sun, 28 Nov 2021 14:03:21 +0900 Subject: [PATCH] Use console IPC --- osu.Desktop/Program.cs | 53 +++++++++++++++++++-- osu.Game/Beatmaps/FlatFileWorkingBeatmap.cs | 52 ++++++++++++++++++++ 2 files changed, 100 insertions(+), 5 deletions(-) create mode 100644 osu.Game/Beatmaps/FlatFileWorkingBeatmap.cs diff --git a/osu.Desktop/Program.cs b/osu.Desktop/Program.cs index 898f7d5105..6dd6849d78 100644 --- a/osu.Desktop/Program.cs +++ b/osu.Desktop/Program.cs @@ -3,13 +3,22 @@ using System; using System.IO; +using System.Linq; using System.Threading; using System.Threading.Tasks; using osu.Framework; using osu.Framework.Development; using osu.Framework.Logging; using osu.Framework.Platform; +using osu.Game.Beatmaps; +using osu.Game.Beatmaps.Legacy; using osu.Game.IPC; +using osu.Game.Rulesets; +using osu.Game.Rulesets.Catch; +using osu.Game.Rulesets.Mania; +using osu.Game.Rulesets.Mods; +using osu.Game.Rulesets.Osu; +using osu.Game.Rulesets.Taiko; using osu.Game.Tournament; namespace osu.Desktop @@ -19,7 +28,7 @@ namespace osu.Desktop private const string base_game_name = @"osu"; [STAThread] - public static int Main(string[] args) + public static void Main(string[] args) { // Back up the cwd before DesktopGameHost changes it string cwd = Environment.CurrentDirectory; @@ -49,6 +58,34 @@ namespace osu.Desktop gameName = $"{base_game_name}-{clientID}"; break; + + case "--osu-stable-difficulty-stream": + while (true) + { + try + { + string beatmapFile = Console.ReadLine() ?? string.Empty; + int rulesetId = int.Parse(Console.ReadLine() ?? string.Empty); + LegacyMods legacyMods = (LegacyMods)int.Parse(Console.ReadLine() ?? string.Empty); + + Ruleset ruleset = rulesetId switch + { + 0 => new OsuRuleset(), + 1 => new TaikoRuleset(), + 2 => new CatchRuleset(), + 3 => new ManiaRuleset(), + _ => throw new ArgumentException("Invalid ruleset id") + }; + + Mod[] mods = ruleset.ConvertFromLegacyMods(legacyMods).ToArray(); + WorkingBeatmap beatmap = new FlatFileWorkingBeatmap(beatmapFile, _ => ruleset); + Console.WriteLine(ruleset.CreateDifficultyCalculator(beatmap).Calculate(mods).StarRating); + } + catch + { + Console.WriteLine(0); + } + } } } @@ -69,14 +106,14 @@ namespace osu.Desktop throw new TimeoutException(@"IPC took too long to send"); } - return 0; + return; } // we want to allow multiple instances to be started when in debug. if (!DebugUtils.IsDebugBuild) { Logger.Log(@"osu! does not support multiple running instances.", LoggingTarget.Runtime, LogLevel.Error); - return 0; + return; } } @@ -84,8 +121,6 @@ namespace osu.Desktop host.Run(new TournamentGame()); else host.Run(new OsuGameDesktop(args)); - - return 0; } } @@ -107,4 +142,12 @@ namespace osu.Desktop return continueExecution; } } + + // Note: Keep in osu.Desktop namespace, or update osu!stable also. + public class DifficultyCalculationMessage + { + public string BeatmapFile { get; set; } + public int RulesetId { get; set; } + public int Mods { get; set; } + } } diff --git a/osu.Game/Beatmaps/FlatFileWorkingBeatmap.cs b/osu.Game/Beatmaps/FlatFileWorkingBeatmap.cs new file mode 100644 index 0000000000..8c915e2872 --- /dev/null +++ b/osu.Game/Beatmaps/FlatFileWorkingBeatmap.cs @@ -0,0 +1,52 @@ +// 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.Game.Beatmaps.Formats; +using osu.Game.IO; +using osu.Game.Rulesets; +using osu.Game.Skinning; + +namespace osu.Game.Beatmaps +{ + /// + /// A which can be constructed directly from a .osu file, providing an implementation for + /// . + /// + public class FlatFileWorkingBeatmap : WorkingBeatmap + { + private readonly Beatmap beatmap; + + public FlatFileWorkingBeatmap(string file, Func rulesetProvider, int? beatmapId = null) + : this(readFromFile(file), rulesetProvider, beatmapId) + { + } + + private FlatFileWorkingBeatmap(Beatmap beatmap, Func rulesetProvider, int? beatmapId = null) + : base(beatmap.BeatmapInfo, null) + { + this.beatmap = beatmap; + + beatmap.BeatmapInfo.Ruleset = rulesetProvider(beatmap.BeatmapInfo.RulesetID).RulesetInfo; + + if (beatmapId.HasValue) + beatmap.BeatmapInfo.OnlineID = beatmapId; + } + + private static Beatmap readFromFile(string filename) + { + using (var stream = File.OpenRead(filename)) + using (var reader = new LineBufferedReader(stream)) + return Decoder.GetDecoder(reader).Decode(reader); + } + + protected override IBeatmap GetBeatmap() => beatmap; + protected override Texture GetBackground() => throw new NotImplementedException(); + protected override Track GetBeatmapTrack() => throw new NotImplementedException(); + protected internal override ISkin GetSkin() => throw new NotImplementedException(); + public override Stream GetStream(string storagePath) => throw new NotImplementedException(); + } +}