Skip to content

Commit

Permalink
Merge pull request #6005 from peppy/fix-input-handler-disabling
Browse files Browse the repository at this point in the history
Fix keyboard handler getting disabled on window creation failure
  • Loading branch information
smoogipoo authored Sep 28, 2023
2 parents ff38299 + 4a51a13 commit 7f20b43
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 9 deletions.
34 changes: 25 additions & 9 deletions osu.Framework/Platform/GameHost.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,11 @@ public abstract class GameHost : IIpcHost, IDisposable
{
public IWindow Window { get; private set; }

/// <summary>
/// Whether <see cref="Window"/> needs to be non-null for startup to succeed.
/// </summary>
protected virtual bool RequireWindowExists => true;

public IRenderer Renderer { get; private set; }

public string RendererInfo { get; private set; }
Expand Down Expand Up @@ -724,6 +729,14 @@ public void Run(Game game)

ChooseAndSetupRenderer();

// Window creation may fail in the case of a catastrophic failure (ie. graphics driver or SDL2 level).
// In such cases, we want to throw here to immediately mark this renderer setup as failed.
if (RequireWindowExists && Window == null)
{
Logger.Log("Aborting startup as no window could be created.");
return;
}

initialiseInputHandlers();

// Prepare renderer (requires config).
Expand Down Expand Up @@ -961,17 +974,20 @@ protected void SetupRendererAndWindow(IRenderer renderer, GraphicsSurfaceType su
Renderer = renderer;
Renderer.CacheStorage = CacheStorage.GetStorageForDirectory("shaders");

// Prepare window
Window = CreateWindow(surfaceType);

if (Window == null)
{
Logger.Log("🖼️ Renderer could not be initialised, no window exists.");
return;
}

try
{
// Prepare window
Window = CreateWindow(surfaceType);

if (Window == null)
{
// Can be null, usually via Headless execution.
if (!RequireWindowExists)
return;

throw new InvalidOperationException("🖼️ Renderer could not be initialised as window creation failed.");
}

Window.SetupWindow(Config);
Window.Create();
Window.Title = $@"osu!framework (running ""{Name}"")";
Expand Down
2 changes: 2 additions & 0 deletions osu.Framework/Platform/HeadlessGameHost.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ public HeadlessGameHost(string gameName = null, HostOptions options = null, bool
this.realtime = realtime;
}

protected override bool RequireWindowExists => false;

protected override IWindow CreateWindow(GraphicsSurfaceType preferredSurface) => null;

protected override Clipboard CreateClipboard() => new HeadlessClipboard();
Expand Down

0 comments on commit 7f20b43

Please sign in to comment.