-
Notifications
You must be signed in to change notification settings - Fork 1
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 #43 from mewlist/tickable
Tickable
- Loading branch information
Showing
15 changed files
with
379 additions
and
17 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
using System; | ||
|
||
namespace Doinject | ||
{ | ||
public enum TickableTiming | ||
{ | ||
EarlyUpdate, | ||
FixedUpdate, | ||
PreUpdate, | ||
Update, | ||
PreLateUpdate, | ||
PostLateUpdate, | ||
} | ||
|
||
[AttributeUsage(AttributeTargets.Method)] | ||
public class TickableAttribute : Attribute | ||
{ | ||
public TickableTiming Timing { get; set; } | ||
|
||
public TickableAttribute(TickableTiming timing = TickableTiming.Update) | ||
{ | ||
Timing = timing; | ||
} | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq.Expressions; | ||
using System.Reflection; | ||
using Mew.Core; | ||
using UnityEngine; | ||
|
||
namespace Doinject | ||
{ | ||
public class TickableMethod | ||
{ | ||
public Action Invoke { get; } | ||
|
||
public TickableMethod(object target, MethodBase methodInfo) | ||
{ | ||
if (target is MonoBehaviour monoBehaviour) | ||
Invoke = () => | ||
{ | ||
if (monoBehaviour) | ||
methodInfo.Invoke(monoBehaviour, null); | ||
}; | ||
else | ||
Invoke = () => methodInfo.Invoke(target, null); | ||
} | ||
|
||
} | ||
|
||
internal class Ticker : IDisposable | ||
{ | ||
private readonly Dictionary<TickableTiming, List<TickableMethod>> invokers = new(); | ||
|
||
public Ticker() | ||
{ | ||
MewLoop.Add<MewUnityEarlyUpdate>(InvokeEarlyUpdate); | ||
MewLoop.Add<MewUnityFixedUpdate>(InvokeFixedUpdate); | ||
MewLoop.Add<MewUnityPreUpdate>(InvokePreUpdate); | ||
MewLoop.Add<MewUnityUpdate>(InvokeUpdate); | ||
MewLoop.Add<MewUnityPreLateUpdate>(InvokePreLateUpdate); | ||
MewLoop.Add<MewUnityPostLateUpdate>(InvokePostLateUpdate); | ||
} | ||
|
||
public void Dispose() | ||
{ | ||
MewLoop.Remove<MewUnityEarlyUpdate>(InvokeEarlyUpdate); | ||
MewLoop.Remove<MewUnityFixedUpdate>(InvokeFixedUpdate); | ||
MewLoop.Remove<MewUnityPreUpdate>(InvokePreUpdate); | ||
MewLoop.Remove<MewUnityUpdate>(InvokeUpdate); | ||
MewLoop.Remove<MewUnityPreLateUpdate>(InvokePreLateUpdate); | ||
MewLoop.Remove<MewUnityPostLateUpdate>(InvokePostLateUpdate); | ||
invokers.Clear(); | ||
} | ||
|
||
private void InvokeEarlyUpdate() => Invoke(TickableTiming.EarlyUpdate); | ||
private void InvokeFixedUpdate() => Invoke(TickableTiming.FixedUpdate); | ||
private void InvokePreUpdate() => Invoke(TickableTiming.PreUpdate); | ||
private void InvokeUpdate() => Invoke(TickableTiming.Update); | ||
private void InvokePreLateUpdate() => Invoke(TickableTiming.PreLateUpdate); | ||
private void InvokePostLateUpdate() => Invoke(TickableTiming.PostLateUpdate); | ||
|
||
public void Add(object target, Dictionary<TickableTiming, List<MethodInfo>> methodsTickableMethods) | ||
{ | ||
foreach (var (timing, methods) in methodsTickableMethods) | ||
foreach (var methodInfo in methods) | ||
{ | ||
if (!invokers.ContainsKey(timing)) | ||
invokers[timing] = new List<TickableMethod>(); | ||
invokers[timing].Add(new TickableMethod(target, methodInfo)); | ||
} | ||
} | ||
|
||
private void Invoke(TickableTiming timing) | ||
{ | ||
if (!invokers.TryGetValue(timing, out var methods)) return; | ||
|
||
foreach (var method in methods) | ||
method.Invoke(); | ||
} | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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,71 @@ | ||
using System; | ||
using System.Threading.Tasks; | ||
using Mew.Core.TaskHelpers; | ||
using NUnit.Framework; | ||
using UnityEngine; | ||
using UnityEngine.SceneManagement; | ||
|
||
namespace Doinject.Tests | ||
{ | ||
public class TickableTest | ||
{ | ||
private DIContainer container; | ||
|
||
public int TargetFrameRate { get; set; } | ||
|
||
[SetUp] | ||
public void Setup() | ||
{ | ||
TargetFrameRate = Application.targetFrameRate; | ||
Application.targetFrameRate = 10; | ||
var scene = SceneManager.GetActiveScene(); | ||
container = new DIContainer(parent: null, scene); | ||
} | ||
|
||
|
||
[TearDown] | ||
public async Task TearDown() | ||
{ | ||
await container.DisposeAsync(); | ||
Application.targetFrameRate = TargetFrameRate; | ||
} | ||
|
||
[Test] | ||
public async Task UpdateTimingTest() | ||
{ | ||
var tickable = new TickableObject(); | ||
container.BindFromInstance(tickable); | ||
var instance = await container.ResolveAsync<TickableObject>(); | ||
await TaskHelperInternal.NextFrame(); | ||
instance.CountEnabled = true; | ||
for (var i = 0; i < 10; i++) | ||
await TaskHelperInternal.NextFrame(); | ||
await TaskHelperInternal.NextFrame(); | ||
await TaskHelperInternal.NextFrame(); | ||
instance.CountEnabled = false; | ||
Assert.That(instance.EarlyUpdateCount, Is.GreaterThan(8)); | ||
Assert.That(instance.FixedUpdateCount, Is.GreaterThan(8)); | ||
Assert.That(instance.PreUpdateCount, Is.GreaterThan(8)); | ||
Assert.That(instance.UpdateCount, Is.GreaterThan(8)); | ||
Assert.That(instance.PreLateUpdateCount, Is.GreaterThan(8)); | ||
Assert.That(instance.PostLateUpdateCount, Is.GreaterThan(8)); | ||
Assert.That(instance.FixedUpdateCount, Is.GreaterThan(instance.UpdateCount)); | ||
} | ||
|
||
[Test] | ||
public async Task InvalidTickableTest() | ||
{ | ||
try | ||
{ | ||
container.Bind<InvalidTickableObject>(); | ||
var _ = await container.ResolveAsync<InvalidTickableObject>(); | ||
} | ||
catch (Exception e) | ||
{ | ||
if (e.Message.Contains("Tickable method should not have any parameters")) | ||
Assert.Pass(); | ||
} | ||
Assert.Fail(); | ||
} | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
Oops, something went wrong.