1
0
mirror of https://github.com/ppy/osu.git synced 2024-11-11 13:37:25 +08:00

Use IPC via TCP

This commit is contained in:
Dan Balasescu 2021-11-28 18:00:06 +09:00
parent e5dcfc3113
commit ef24780642
5 changed files with 193 additions and 32 deletions

View File

@ -0,0 +1,18 @@
// 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.
namespace osu.Desktop.LegacyIpc
{
/// <summary>
/// A difficulty calculation request from the legacy client.
/// </summary>
/// <remarks>
/// Synchronise any changes with osu!stable.
/// </remarks>
public class LegacyIpcDifficultyCalculationRequest
{
public string BeatmapFile { get; set; }
public int RulesetId { get; set; }
public int Mods { get; set; }
}
}

View File

@ -0,0 +1,16 @@
// 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.
namespace osu.Desktop.LegacyIpc
{
/// <summary>
/// A difficulty calculation response returned to the legacy client.
/// </summary>
/// <remarks>
/// Synchronise any changes with osu!stable.
/// </remarks>
public class LegacyIpcDifficultyCalculationResponse
{
public double StarRating { get; set; }
}
}

View File

@ -0,0 +1,40 @@
// 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 osu.Framework.Platform;
namespace osu.Desktop.LegacyIpc
{
/// <summary>
/// An <see cref="IpcMessage"/> that can be used to communicate to and from legacy clients.
/// </summary>
/// <remarks>
/// Synchronise any changes with osu-stable.
/// </remarks>
public class LegacyIpcMessage : IpcMessage
{
public LegacyIpcMessage()
{
// Types/assemblies are not inter-compatible, so always serialise/deserialise into objects.
base.Type = typeof(object).FullName;
}
public new string Type => base.Type; // Hide setter.
public new object Value
{
get => base.Value;
set => base.Value = new Data
{
MessageType = value.GetType().Name,
MessageData = value
};
}
public class Data
{
public string MessageType { get; set; }
public object MessageData { get; set; }
}
}
}

View File

@ -0,0 +1,65 @@
// 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.Threading.Tasks;
using Newtonsoft.Json.Linq;
using osu.Framework.Platform;
namespace osu.Desktop.LegacyIpc
{
public class LegacyTcpIpcProvider : TcpIpcProvider
{
public new Func<object, object> MessageReceived;
public LegacyTcpIpcProvider()
{
base.MessageReceived += msg =>
{
try
{
var legacyData = ((JObject)msg.Value).ToObject<LegacyIpcMessage.Data>();
object value = parseObject((JObject)legacyData.MessageData, legacyData.MessageType);
object result = MessageReceived?.Invoke(value);
return result != null ? new LegacyIpcMessage { Value = result } : null;
}
catch (Exception ex)
{
Console.WriteLine(ex);
}
return null;
};
}
public Task SendMessageAsync(object message) => base.SendMessageAsync(new LegacyIpcMessage { Value = message });
public async Task<T> SendMessageWithResponseAsync<T>(object message)
{
var result = await base.SendMessageWithResponseAsync(new LegacyIpcMessage { Value = message }).ConfigureAwait(false);
var legacyData = ((JObject)result.Value).ToObject<LegacyIpcMessage.Data>();
return (T)parseObject((JObject)legacyData.MessageData, legacyData.MessageType);
}
public new Task SendMessageAsync(IpcMessage message) => throw new InvalidOperationException("Use typed overloads.");
public new Task<IpcMessage> SendMessageWithResponseAsync(IpcMessage message) => throw new InvalidOperationException("Use typed overloads.");
private object parseObject(JObject value, string type)
{
switch (type)
{
case nameof(LegacyIpcDifficultyCalculationRequest):
return value.ToObject<LegacyIpcDifficultyCalculationRequest>();
case nameof(LegacyIpcDifficultyCalculationResponse):
return value.ToObject<LegacyIpcDifficultyCalculationResponse>();
default:
throw new ArgumentException($"Unknown type: {type}");
}
}
}
}

View File

@ -6,6 +6,7 @@ using System.IO;
using System.Linq; using System.Linq;
using System.Threading; using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using osu.Desktop.LegacyIpc;
using osu.Framework; using osu.Framework;
using osu.Framework.Development; using osu.Framework.Development;
using osu.Framework.Logging; using osu.Framework.Logging;
@ -27,6 +28,8 @@ namespace osu.Desktop
{ {
private const string base_game_name = @"osu"; private const string base_game_name = @"osu";
private static LegacyTcpIpcProvider legacyIpcProvider;
[STAThread] [STAThread]
public static void Main(string[] args) public static void Main(string[] args)
{ {
@ -59,33 +62,26 @@ namespace osu.Desktop
gameName = $"{base_game_name}-{clientID}"; gameName = $"{base_game_name}-{clientID}";
break; break;
case "--osu-stable-difficulty-stream": case "--legacy-ipc-server":
while (true) using (legacyIpcProvider = new LegacyTcpIpcProvider())
{ {
try legacyIpcProvider.MessageReceived += onLegacyIpcMessageReceived;
{ legacyIpcProvider.Bind();
string beatmapFile = Console.ReadLine() ?? string.Empty; legacyIpcProvider.StartAsync().Wait();
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);
}
} }
return;
case "--legacy-ipc-client":
using (legacyIpcProvider = new LegacyTcpIpcProvider())
{
Console.WriteLine(legacyIpcProvider.SendMessageWithResponseAsync<LegacyIpcDifficultyCalculationResponse>(new LegacyIpcDifficultyCalculationRequest
{
BeatmapFile = "/home/smgi/Downloads/osu_files/129891.osu",
}).Result.StarRating);
}
return;
} }
} }
@ -141,13 +137,39 @@ namespace osu.Desktop
return continueExecution; return continueExecution;
} }
}
// Note: Keep in osu.Desktop namespace, or update osu!stable also. private static object onLegacyIpcMessageReceived(object message)
public class DifficultyCalculationMessage {
{ switch (message)
public string BeatmapFile { get; set; } {
public int RulesetId { get; set; } case LegacyIpcDifficultyCalculationRequest req:
public int Mods { get; set; } try
{
Ruleset ruleset = req.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)req.Mods).ToArray();
WorkingBeatmap beatmap = new FlatFileWorkingBeatmap(req.BeatmapFile, _ => ruleset);
return new LegacyIpcDifficultyCalculationResponse
{
StarRating = ruleset.CreateDifficultyCalculator(beatmap).Calculate(mods).StarRating
};
}
catch
{
return new LegacyIpcDifficultyCalculationResponse();
}
}
Console.WriteLine("Type not matched.");
return null;
}
} }
} }