Use console IPC

This commit is contained in:
Dan Balasescu 2021-11-28 14:03:21 +09:00
parent 51a7c60eec
commit e5dcfc3113
2 changed files with 100 additions and 5 deletions

View File

@ -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; }
}
}

View File

@ -0,0 +1,52 @@
// 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.Game.Beatmaps.Formats;
using osu.Game.IO;
using osu.Game.Rulesets;
using osu.Game.Skinning;
namespace osu.Game.Beatmaps
{
/// <summary>
/// A <see cref="WorkingBeatmap"/> which can be constructed directly from a .osu file, providing an implementation for
/// <see cref="WorkingBeatmap.GetPlayableBeatmap(osu.Game.Rulesets.IRulesetInfo,System.Collections.Generic.IReadOnlyList{osu.Game.Rulesets.Mods.Mod})"/>.
/// </summary>
public class FlatFileWorkingBeatmap : WorkingBeatmap
{
private readonly Beatmap beatmap;
public FlatFileWorkingBeatmap(string file, Func<int, Ruleset> rulesetProvider, int? beatmapId = null)
: this(readFromFile(file), rulesetProvider, beatmapId)
{
}
private FlatFileWorkingBeatmap(Beatmap beatmap, Func<int, Ruleset> 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<Beatmap>(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();
}
}