mirror of
https://github.com/ppy/osu.git
synced 2024-12-13 08:32:57 +08:00
Move exception soft-handling logic to OsuGameBase
This commit is contained in:
parent
886a4815fa
commit
bcd91ac743
@ -4,8 +4,6 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
using System.Runtime.Versioning;
|
using System.Runtime.Versioning;
|
||||||
using System.Threading;
|
|
||||||
using System.Threading.Tasks;
|
|
||||||
using osu.Desktop.LegacyIpc;
|
using osu.Desktop.LegacyIpc;
|
||||||
using osu.Framework;
|
using osu.Framework;
|
||||||
using osu.Framework.Development;
|
using osu.Framework.Development;
|
||||||
@ -63,8 +61,6 @@ namespace osu.Desktop
|
|||||||
|
|
||||||
using (DesktopGameHost host = Host.GetSuitableDesktopHost(gameName, new HostOptions { BindIPC = true }))
|
using (DesktopGameHost host = Host.GetSuitableDesktopHost(gameName, new HostOptions { BindIPC = true }))
|
||||||
{
|
{
|
||||||
host.ExceptionThrown += handleException;
|
|
||||||
|
|
||||||
if (!host.IsPrimaryInstance)
|
if (!host.IsPrimaryInstance)
|
||||||
{
|
{
|
||||||
if (args.Length > 0 && args[0].Contains('.')) // easy way to check for a file import in args
|
if (args.Length > 0 && args[0].Contains('.')) // easy way to check for a file import in args
|
||||||
@ -131,23 +127,5 @@ namespace osu.Desktop
|
|||||||
// tools.SetProcessAppUserModelId();
|
// tools.SetProcessAppUserModelId();
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
private static int allowableExceptions = DebugUtils.IsDebugBuild ? 0 : 1;
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Allow a maximum of one unhandled exception, per second of execution.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="arg"></param>
|
|
||||||
private static bool handleException(Exception arg)
|
|
||||||
{
|
|
||||||
bool continueExecution = Interlocked.Decrement(ref allowableExceptions) >= 0;
|
|
||||||
|
|
||||||
Logger.Log($"Unhandled exception has been {(continueExecution ? $"allowed with {allowableExceptions} more allowable exceptions" : "denied")} .");
|
|
||||||
|
|
||||||
// restore the stock of allowable exceptions after a short delay.
|
|
||||||
Task.Delay(1000).ContinueWith(_ => Interlocked.Increment(ref allowableExceptions));
|
|
||||||
|
|
||||||
return continueExecution;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -7,6 +7,7 @@ using System.IO;
|
|||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Reflection;
|
using System.Reflection;
|
||||||
using System.Threading;
|
using System.Threading;
|
||||||
|
using System.Threading.Tasks;
|
||||||
using osu.Framework.Allocation;
|
using osu.Framework.Allocation;
|
||||||
using osu.Framework.Audio;
|
using osu.Framework.Audio;
|
||||||
using osu.Framework.Bindables;
|
using osu.Framework.Bindables;
|
||||||
@ -180,9 +181,16 @@ namespace osu.Game
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
protected DatabaseContextFactory EFContextFactory { get; private set; }
|
protected DatabaseContextFactory EFContextFactory { get; private set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Number of exceptions to allow before aborting execution.
|
||||||
|
/// </summary>
|
||||||
|
protected virtual int SoftHandledExceptions => 0;
|
||||||
|
|
||||||
public OsuGameBase()
|
public OsuGameBase()
|
||||||
{
|
{
|
||||||
Name = @"osu!";
|
Name = @"osu!";
|
||||||
|
|
||||||
|
allowableExceptions = SoftHandledExceptions;
|
||||||
}
|
}
|
||||||
|
|
||||||
[BackgroundDependencyLoader]
|
[BackgroundDependencyLoader]
|
||||||
@ -408,6 +416,8 @@ namespace osu.Game
|
|||||||
LocalConfig ??= UseDevelopmentServer
|
LocalConfig ??= UseDevelopmentServer
|
||||||
? new DevelopmentOsuConfigManager(Storage)
|
? new DevelopmentOsuConfigManager(Storage)
|
||||||
: new OsuConfigManager(Storage);
|
: new OsuConfigManager(Storage);
|
||||||
|
|
||||||
|
host.ExceptionThrown += onExceptionThrown;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@ -505,6 +515,23 @@ namespace osu.Game
|
|||||||
AvailableMods.Value = dict;
|
AvailableMods.Value = dict;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private int allowableExceptions;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Allows a maximum of one unhandled exception, per second of execution.
|
||||||
|
/// </summary>
|
||||||
|
private bool onExceptionThrown(Exception _)
|
||||||
|
{
|
||||||
|
bool continueExecution = Interlocked.Decrement(ref allowableExceptions) >= 0;
|
||||||
|
|
||||||
|
Logger.Log($"Unhandled exception has been {(continueExecution ? $"allowed with {SoftHandledExceptions} more allowable exceptions" : "denied")} .");
|
||||||
|
|
||||||
|
// restore the stock of allowable exceptions after a short delay.
|
||||||
|
Task.Delay(1000).ContinueWith(_ => Interlocked.Increment(ref allowableExceptions));
|
||||||
|
|
||||||
|
return continueExecution;
|
||||||
|
}
|
||||||
|
|
||||||
protected override void Dispose(bool isDisposing)
|
protected override void Dispose(bool isDisposing)
|
||||||
{
|
{
|
||||||
base.Dispose(isDisposing);
|
base.Dispose(isDisposing);
|
||||||
@ -514,6 +541,9 @@ namespace osu.Game
|
|||||||
LocalConfig?.Dispose();
|
LocalConfig?.Dispose();
|
||||||
|
|
||||||
realm?.Dispose();
|
realm?.Dispose();
|
||||||
|
|
||||||
|
if (Host != null)
|
||||||
|
Host.ExceptionThrown -= onExceptionThrown;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user