Skip to content

Commit

Permalink
added exclusion types to stop UniTAS threads from being killed
Browse files Browse the repository at this point in the history
  • Loading branch information
Eddio0141 committed Aug 16, 2024
1 parent 089f5f4 commit 9eb89b2
Showing 1 changed file with 13 additions and 13 deletions.
26 changes: 13 additions & 13 deletions UniTAS/Patcher/Implementations/ThreadSoftRestartHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,34 +4,33 @@
using System.Linq;
using System.Threading;
using BepInEx.Logging;
using UniTAS.Patcher.Implementations.Customization;
using UniTAS.Patcher.Implementations.TASRenderer;
using UniTAS.Patcher.Interfaces.DependencyInjection;
using UniTAS.Patcher.Interfaces.Events.SoftRestart;
using UniTAS.Patcher.Interfaces.Movie;
using UniTAS.Patcher.Services;
using UniTAS.Patcher.Services.Logging;
using UniTAS.Patcher.Utils;

namespace UniTAS.Patcher.Implementations;

[Singleton]
public class ThreadSoftRestartHandler : IThreadTracker, IOnPreGameRestart
public class ThreadSoftRestartHandler(ILogger logger) : IThreadTracker, IOnPreGameRestart
{
private readonly List<Thread> _threads = [];
private readonly ILogger _logger;

private readonly Type[] _excludeTypes =
[
typeof(DiskLogListener),
typeof(GameVideoRenderer),
typeof(NativeAudioRenderer),
typeof(LoggingUtils.DiskLogger)
typeof(LoggingUtils.DiskLogger),
typeof(RemoteControl),
typeof(ReadOnlyFieldDescriptor),
typeof(Config)
];

public ThreadSoftRestartHandler(ILogger logger)
{
_logger = logger;
}

public void ThreadStart(Thread thread)
{
if (_threads.Contains(thread)) return;
Expand All @@ -44,14 +43,15 @@ public void ThreadStart(Thread thread)
if (allFrames == null) return;
var frames = allFrames.Skip(3).ToArray();

// intentionally don't check if UniTAS namespace is included, any exclusions for UniTAS is to be included in _excludeTypes manually
if (frames.Any(x => _excludeTypes.Contains(x.GetMethod()?.DeclaringType)))
{
_logger.LogDebug(
logger.LogDebug(
$"Ignoring thread start, name: {thread.Name}, ID: {thread.ManagedThreadId}, stack trace: {trace}");
return;
}

_logger.LogDebug(
logger.LogDebug(
$"Tracking thread start, name: {thread.Name}, ID: {thread.ManagedThreadId}, stack trace: {trace}");
_threads.Add(thread);
}
Expand All @@ -71,7 +71,7 @@ public void OnPreGameRestart()
}
catch (Exception e)
{
_logger.LogDebug($"Exception thrown while aborting thread: {e}");
logger.LogDebug($"Exception thrown while aborting thread: {e}");
}
}

Expand All @@ -81,9 +81,9 @@ public void OnPreGameRestart()
{
thread.Join();

_logger.LogDebug($"Interrupted game thread, name: {thread.Name}, ID: {thread.ManagedThreadId}");
logger.LogDebug($"Interrupted game thread, name: {thread.Name}, ID: {thread.ManagedThreadId}");
}

_logger.LogDebug("All game threads interrupted");
logger.LogDebug("All game threads interrupted");
}
}

0 comments on commit 9eb89b2

Please sign in to comment.