-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #241 from Eddio0141/234-pause-coroutines-on-monobe…
…haviour-pause 234 pause coroutines on monobehaviour pause
- Loading branch information
Showing
7 changed files
with
129 additions
and
58 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
UniTAS/Patcher/Implementations/GameRestart/CoroutinesStopOnRestart.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
using System.Collections.Generic; | ||
using UniTAS.Patcher.Interfaces.DependencyInjection; | ||
using UniTAS.Patcher.Interfaces.Events.SoftRestart; | ||
using UniTAS.Patcher.Services.Trackers; | ||
using UnityEngine; | ||
|
||
namespace UniTAS.Patcher.Implementations.GameRestart; | ||
|
||
[Singleton] | ||
public class CoroutinesStopOnRestart : ICoroutineRunningObjectsTracker, IOnPreGameRestart | ||
{ | ||
private readonly List<MonoBehaviour> _instances = new(); | ||
|
||
public void NewCoroutine(MonoBehaviour instance) | ||
{ | ||
if (!_instances.Contains(instance) && instance != null) | ||
{ | ||
_instances.Add(instance); | ||
} | ||
} | ||
|
||
public void OnPreGameRestart() | ||
{ | ||
foreach (var coroutine in _instances) | ||
{ | ||
if (coroutine == null) continue; | ||
coroutine.StopAllCoroutines(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 43 additions & 0 deletions
43
UniTAS/Patcher/Patches/Harmony/CoroutineRunningObjectsTrackerPatch.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Diagnostics.CodeAnalysis; | ||
using System.Linq; | ||
using System.Reflection; | ||
using HarmonyLib; | ||
using UniTAS.Patcher.Interfaces.Patches.PatchTypes; | ||
using UniTAS.Patcher.Services.Trackers; | ||
using UniTAS.Patcher.Utils; | ||
using UnityEngine; | ||
|
||
namespace UniTAS.Patcher.Patches.Harmony; | ||
|
||
[RawPatch] | ||
[SuppressMessage("ReSharper", "ClassNeverInstantiated.Global")] | ||
[SuppressMessage("ReSharper", "UnusedMember.Local")] | ||
[SuppressMessage("ReSharper", "InconsistentNaming")] | ||
public class CoroutineRunningObjectsTrackerPatch | ||
{ | ||
private static readonly ICoroutineRunningObjectsTracker Tracker = | ||
ContainerStarter.Kernel.GetInstance<ICoroutineRunningObjectsTracker>(); | ||
|
||
[HarmonyPatch] | ||
private class RunCoroutine | ||
{ | ||
private static Exception Cleanup(MethodBase original, Exception ex) | ||
{ | ||
return PatchHelper.CleanupIgnoreFail(original, ex); | ||
} | ||
|
||
private static IEnumerable<MethodBase> TargetMethods() | ||
{ | ||
// just patch them all, duplicate instances will be detected anyway | ||
return AccessTools.GetDeclaredMethods(typeof(MonoBehaviour)) | ||
.Where(x => !x.IsStatic && x.Name == "StartCoroutine").Select(x => (MethodBase)x); | ||
} | ||
|
||
private static void Prefix(MonoBehaviour __instance) | ||
{ | ||
Tracker.NewCoroutine(__instance); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 8 additions & 0 deletions
8
UniTAS/Patcher/Services/Trackers/ICoroutineRunningObjectsTracker.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
using UnityEngine; | ||
|
||
namespace UniTAS.Patcher.Services.Trackers; | ||
|
||
public interface ICoroutineRunningObjectsTracker | ||
{ | ||
void NewCoroutine(MonoBehaviour instance); | ||
} |