1
0
mirror of https://github.com/ppy/osu.git synced 2025-01-18 11:02:57 +08:00

Merge pull request from bdach/osu-link-ipc

Add ability to handle `osu://` scheme links via IPC on desktop
This commit is contained in:
Dean Herbert 2022-06-21 14:40:46 +09:00 committed by GitHub
commit 1b122f88c0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 240 additions and 30 deletions

View File

@ -1,8 +1,6 @@
// 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.
#nullable disable
using System;
using System.Collections.Generic;
using System.Diagnostics;
@ -26,6 +24,7 @@ using osu.Framework.Input.Handlers.Mouse;
using osu.Framework.Input.Handlers.Tablet;
using osu.Framework.Threading;
using osu.Game.IO;
using osu.Game.IPC;
using osu.Game.Overlays.Settings;
using osu.Game.Overlays.Settings.Sections.Input;
@ -33,18 +32,20 @@ namespace osu.Desktop
{
internal class OsuGameDesktop : OsuGame
{
public OsuGameDesktop(string[] args = null)
private OsuSchemeLinkIPCChannel? osuSchemeLinkIPCChannel;
public OsuGameDesktop(string[]? args = null)
: base(args)
{
}
public override StableStorage GetStorageForStableInstall()
public override StableStorage? GetStorageForStableInstall()
{
try
{
if (Host is DesktopGameHost desktopHost)
{
string stablePath = getStableInstallPath();
string? stablePath = getStableInstallPath();
if (!string.IsNullOrEmpty(stablePath))
return new StableStorage(stablePath, desktopHost);
}
@ -57,11 +58,11 @@ namespace osu.Desktop
return null;
}
private string getStableInstallPath()
private string? getStableInstallPath()
{
static bool checkExists(string p) => Directory.Exists(Path.Combine(p, "Songs")) || File.Exists(Path.Combine(p, "osu!.cfg"));
string stableInstallPath;
string? stableInstallPath;
if (OperatingSystem.IsWindows())
{
@ -89,15 +90,15 @@ namespace osu.Desktop
}
[SupportedOSPlatform("windows")]
private string getStableInstallPathFromRegistry()
private string? getStableInstallPathFromRegistry()
{
using (RegistryKey key = Registry.ClassesRoot.OpenSubKey("osu"))
using (RegistryKey? key = Registry.ClassesRoot.OpenSubKey("osu"))
return key?.OpenSubKey(@"shell\open\command")?.GetValue(string.Empty)?.ToString()?.Split('"')[1].Replace("osu!.exe", "");
}
protected override UpdateManager CreateUpdateManager()
{
string packageManaged = Environment.GetEnvironmentVariable("OSU_EXTERNAL_UPDATE_PROVIDER");
string? packageManaged = Environment.GetEnvironmentVariable("OSU_EXTERNAL_UPDATE_PROVIDER");
if (!string.IsNullOrEmpty(packageManaged))
return new NoActionUpdateManager();
@ -124,6 +125,8 @@ namespace osu.Desktop
LoadComponentAsync(new GameplayWinKeyBlocker(), Add);
LoadComponentAsync(new ElevatedPrivilegesChecker(), Add);
osuSchemeLinkIPCChannel = new OsuSchemeLinkIPCChannel(Host, this);
}
public override void SetHost(GameHost host)
@ -159,7 +162,7 @@ namespace osu.Desktop
}
private readonly List<string> importableFiles = new List<string>();
private ScheduledDelegate importSchedule;
private ScheduledDelegate? importSchedule;
private void fileDrop(string[] filePaths)
{
@ -192,5 +195,11 @@ namespace osu.Desktop
Task.Factory.StartNew(() => Import(paths), TaskCreationOptions.LongRunning);
}
}
protected override void Dispose(bool isDisposing)
{
base.Dispose(isDisposing);
osuSchemeLinkIPCChannel?.Dispose();
}
}
}

View File

@ -11,6 +11,7 @@ using osu.Framework;
using osu.Framework.Development;
using osu.Framework.Logging;
using osu.Framework.Platform;
using osu.Game;
using osu.Game.IPC;
using osu.Game.Tournament;
using Squirrel;
@ -65,19 +66,8 @@ namespace osu.Desktop
{
if (!host.IsPrimaryInstance)
{
if (args.Length > 0 && args[0].Contains('.')) // easy way to check for a file import in args
{
var importer = new ArchiveImportIPCChannel(host);
foreach (string file in args)
{
Console.WriteLine(@"Importing {0}", file);
if (!importer.ImportAsync(Path.GetFullPath(file, cwd)).Wait(3000))
throw new TimeoutException(@"IPC took too long to send");
}
if (trySendIPCMessage(host, cwd, args))
return;
}
// we want to allow multiple instances to be started when in debug.
if (!DebugUtils.IsDebugBuild)
@ -108,6 +98,34 @@ namespace osu.Desktop
}
}
private static bool trySendIPCMessage(IIpcHost host, string cwd, string[] args)
{
if (args.Length == 1 && args[0].StartsWith(OsuGameBase.OSU_PROTOCOL, StringComparison.Ordinal))
{
var osuSchemeLinkHandler = new OsuSchemeLinkIPCChannel(host);
if (!osuSchemeLinkHandler.HandleLinkAsync(args[0]).Wait(3000))
throw new IPCTimeoutException(osuSchemeLinkHandler.GetType());
return true;
}
if (args.Length > 0 && args[0].Contains('.')) // easy way to check for a file import in args
{
var importer = new ArchiveImportIPCChannel(host);
foreach (string file in args)
{
Console.WriteLine(@"Importing {0}", file);
if (!importer.ImportAsync(Path.GetFullPath(file, cwd)).Wait(3000))
throw new IPCTimeoutException(importer.GetType());
}
return true;
}
return false;
}
[SupportedOSPlatform("windows")]
private static void setupSquirrel()
{

View File

@ -0,0 +1,86 @@
// 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.Linq;
using NUnit.Framework;
using osu.Framework;
using osu.Framework.Allocation;
using osu.Framework.Extensions;
using osu.Framework.Graphics.Containers;
using osu.Framework.Platform;
using osu.Framework.Testing;
using osu.Game.IPC;
using osu.Game.Online.API;
using osu.Game.Online.API.Requests;
using osu.Game.Online.API.Requests.Responses;
using osu.Game.Overlays;
namespace osu.Game.Tests.Visual.Navigation
{
[TestFixture]
[Ignore("This test cannot be run headless, as it requires the game host running the nested game to have IPC bound.")]
public class TestSceneInterProcessCommunication : OsuGameTestScene
{
private HeadlessGameHost ipcSenderHost = null!;
private OsuSchemeLinkIPCChannel osuSchemeLinkIPCReceiver = null!;
private OsuSchemeLinkIPCChannel osuSchemeLinkIPCSender = null!;
private const int requested_beatmap_set_id = 1;
[Resolved]
private GameHost gameHost { get; set; } = null!;
public override void SetUpSteps()
{
base.SetUpSteps();
AddStep("set up request handling", () =>
{
((DummyAPIAccess)API).HandleRequest = request =>
{
switch (request)
{
case GetBeatmapSetRequest gbr:
var apiBeatmapSet = CreateAPIBeatmapSet();
apiBeatmapSet.OnlineID = requested_beatmap_set_id;
apiBeatmapSet.Beatmaps = apiBeatmapSet.Beatmaps.Append(new APIBeatmap
{
DifficultyName = "Target difficulty",
OnlineID = 75,
}).ToArray();
gbr.TriggerSuccess(apiBeatmapSet);
return true;
}
return false;
};
});
AddStep("create IPC receiver channel", () => osuSchemeLinkIPCReceiver = new OsuSchemeLinkIPCChannel(gameHost, Game));
AddStep("create IPC sender channel", () =>
{
ipcSenderHost = new HeadlessGameHost(gameHost.Name, new HostOptions { BindIPC = true });
osuSchemeLinkIPCSender = new OsuSchemeLinkIPCChannel(ipcSenderHost);
});
}
[Test]
public void TestOsuSchemeLinkIPCChannel()
{
AddStep("open beatmap via IPC", () => osuSchemeLinkIPCSender.HandleLinkAsync($@"osu://s/{requested_beatmap_set_id}").WaitSafely());
AddUntilStep("beatmap overlay displayed", () => Game.ChildrenOfType<BeatmapSetOverlay>().FirstOrDefault()?.State.Value == Visibility.Visible);
AddUntilStep("beatmap overlay showing content", () => Game.ChildrenOfType<BeatmapSetOverlay>().FirstOrDefault()?.Header.BeatmapSet.Value.OnlineID == requested_beatmap_set_id);
}
public override void TearDownSteps()
{
AddStep("dispose IPC receiver", () => osuSchemeLinkIPCReceiver.Dispose());
AddStep("dispose IPC sender", () =>
{
osuSchemeLinkIPCSender.Dispose();
ipcSenderHost.Dispose();
});
base.TearDownSteps();
}
}
}

View File

@ -13,6 +13,7 @@ using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
using osu.Framework.Localisation;
using osu.Framework.Platform;
using osu.Game.Online;
using osu.Game.Users;
namespace osu.Game.Graphics.Containers
@ -25,7 +26,7 @@ namespace osu.Game.Graphics.Containers
}
[Resolved(CanBeNull = true)]
private OsuGame game { get; set; }
private ILinkHandler linkHandler { get; set; }
[Resolved]
private GameHost host { get; set; }
@ -81,8 +82,8 @@ namespace osu.Game.Graphics.Containers
{
if (action != null)
action();
else if (game != null)
game.HandleLink(link);
else if (linkHandler != null)
linkHandler.HandleLink(link);
// fallback to handle cases where OsuGame is not available, ie. tournament client.
else if (link.Action == LinkAction.External)
host.OpenUrlExternally(link.Argument.ToString());

View File

@ -8,6 +8,7 @@ using Markdig.Syntax.Inlines;
using osu.Framework.Allocation;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers.Markdown;
using osu.Game.Online;
using osu.Game.Online.Chat;
using osu.Game.Overlays;
@ -16,7 +17,7 @@ namespace osu.Game.Graphics.Containers.Markdown
public class OsuMarkdownLinkText : MarkdownLinkText
{
[Resolved(canBeNull: true)]
private OsuGame game { get; set; }
private ILinkHandler linkHandler { get; set; }
private readonly string text;
private readonly string title;
@ -51,7 +52,7 @@ namespace osu.Game.Graphics.Containers.Markdown
};
}
protected override void OnLinkPressed() => game?.HandleLink(Url);
protected override void OnLinkPressed() => linkHandler?.HandleLink(Url);
private class OsuMarkdownLinkCompiler : DrawableLinkCompiler
{

View File

@ -0,0 +1,15 @@
// 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;
namespace osu.Game.IPC
{
public class IPCTimeoutException : TimeoutException
{
public IPCTimeoutException(Type channelType)
: base($@"IPC took too long to send message via channel {channelType}")
{
}
}
}

View File

@ -0,0 +1,49 @@
// 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.Diagnostics;
using System.Threading.Tasks;
using osu.Framework.Platform;
using osu.Game.Online;
namespace osu.Game.IPC
{
public class OsuSchemeLinkIPCChannel : IpcChannel<OsuSchemeLinkMessage>
{
private readonly ILinkHandler? linkHandler;
public OsuSchemeLinkIPCChannel(IIpcHost host, ILinkHandler? linkHandler = null)
: base(host)
{
this.linkHandler = linkHandler;
MessageReceived += msg =>
{
Debug.Assert(linkHandler != null);
linkHandler.HandleLink(msg.Link);
return null;
};
}
public async Task HandleLinkAsync(string url)
{
if (linkHandler == null)
{
await SendMessageAsync(new OsuSchemeLinkMessage(url)).ConfigureAwait(false);
return;
}
linkHandler.HandleLink(url);
}
}
public class OsuSchemeLinkMessage
{
public string Link { get; }
public OsuSchemeLinkMessage(string link)
{
Link = link;
}
}
}

View File

@ -0,0 +1,30 @@
// 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.Allocation;
using osu.Game.Online.Chat;
namespace osu.Game.Online
{
/// <summary>
/// Handle an arbitrary URL. Displays via in-game overlays where possible.
/// Methods can be called from a non-thread-safe non-game-loaded state.
/// </summary>
[Cached]
public interface ILinkHandler
{
/// <summary>
/// Handle an arbitrary URL. Displays via in-game overlays where possible.
/// This can be called from a non-thread-safe non-game-loaded state.
/// </summary>
/// <param name="url">The URL to load.</param>
void HandleLink(string url);
/// <summary>
/// Handle a specific <see cref="LinkDetails"/>.
/// This can be called from a non-thread-safe non-game-loaded state.
/// </summary>
/// <param name="link">The link to load.</param>
void HandleLink(LinkDetails link);
}
}

View File

@ -39,6 +39,7 @@ using osu.Game.Input;
using osu.Game.Input.Bindings;
using osu.Game.IO;
using osu.Game.Localisation;
using osu.Game.Online;
using osu.Game.Online.API.Requests.Responses;
using osu.Game.Online.Chat;
using osu.Game.Overlays;
@ -69,7 +70,7 @@ namespace osu.Game
/// The full osu! experience. Builds on top of <see cref="OsuGameBase"/> to add menus and binding logic
/// for initial components that are generally retrieved via DI.
/// </summary>
public class OsuGame : OsuGameBase, IKeyBindingHandler<GlobalAction>, ILocalUserPlayInfo, IPerformFromScreenRunner, IOverlayManager
public class OsuGame : OsuGameBase, IKeyBindingHandler<GlobalAction>, ILocalUserPlayInfo, IPerformFromScreenRunner, IOverlayManager, ILinkHandler
{
/// <summary>
/// The amount of global offset to apply when a left/right anchored overlay is displayed (ie. settings or notifications).

View File

@ -75,7 +75,7 @@ namespace osu.Game.Tests.Visual
}
[TearDownSteps]
public void TearDownSteps()
public virtual void TearDownSteps()
{
if (DebugUtils.IsNUnitRunning && Game != null)
{