public static void Main() { var seconds = CoroutineHandler.Start(WaitSeconds(), "Awesome Waiting Coroutine"); CoroutineHandler.Start(PrintEvery10Seconds(seconds)); CoroutineHandler.Start(EmptyCoroutine()); CoroutineHandler.InvokeLater(new Wait(5), () => { Console.WriteLine("Raising test event"); CoroutineHandler.RaiseEvent(TestEvent); }); CoroutineHandler.InvokeLater(new Wait(TestEvent), () => Console.WriteLine("Example event received")); CoroutineHandler.InvokeLater(new Wait(TestEvent), () => Console.WriteLine("I am invoked after 'Example event received'"), priority: -5); CoroutineHandler.InvokeLater(new Wait(TestEvent), () => Console.WriteLine("I am invoked before 'Example event received'"), priority: 2); var lastTime = DateTime.Now; while (true) { var currTime = DateTime.Now; CoroutineHandler.Tick(currTime - lastTime); lastTime = currTime; Thread.Sleep(1); } }
public void TestEventBasedCoroutine() { var counter = 0; var myEvent = new Event(); IEnumerator <Wait> OnEventTriggered() { counter++; yield return(new Wait(myEvent)); counter++; } var cr = CoroutineHandler.Start(OnEventTriggered()); Assert.AreEqual(1, counter, "instruction before yield is not executed."); CoroutineHandler.Tick(1); CoroutineHandler.RaiseEvent(myEvent); Assert.AreEqual(2, counter, "instruction after yield is not executed."); CoroutineHandler.Tick(1); CoroutineHandler.RaiseEvent(myEvent); Assert.AreEqual(2, counter, "instruction after yield is not executed."); Assert.AreEqual(true, cr.IsFinished, "Incorrect IsFinished value."); Assert.AreEqual(false, cr.WasCanceled, "Incorrect IsCanceled value."); Assert.AreEqual(cr.MoveNextCount, 2, "Incorrect MoveNextCount value."); }
public void TestOnFinishedEventExecuted() { var myEvent = new Event(); var counter = 0; IEnumerator <Wait> OnEvent() { counter++; yield return(new Wait(myEvent)); } void SetCounterToUnreachableValue(ActiveCoroutine coroutine) { counter = -100; } var cr = CoroutineHandler.Start(OnEvent()); CoroutineHandler.Tick(1); cr.OnFinished += SetCounterToUnreachableValue; for (var i = 0; i < 10; i++) { CoroutineHandler.RaiseEvent(myEvent); } Assert.AreEqual(-100, counter, "Incorrect counter value."); }
public void InvokeLaterAndNameTest() { var myEvent = new Event(); var counter = 0; var cr = CoroutineHandler.InvokeLater(new Wait(myEvent), () => { counter++; }, "Bird"); CoroutineHandler.InvokeLater(new Wait(myEvent), () => { counter++; }); CoroutineHandler.InvokeLater(new Wait(myEvent), () => { counter++; }); Assert.AreEqual(0, counter, "Incorrect counter value after 5 seconds."); CoroutineHandler.Tick(1); CoroutineHandler.RaiseEvent(myEvent); Assert.AreEqual(3, counter, "Incorrect counter value after 10 seconds."); Assert.AreEqual(true, cr.IsFinished, "Incorrect IsFinished value."); Assert.AreEqual(false, cr.WasCanceled, "Incorrect IsCanceled value."); Assert.AreEqual(cr.MoveNextCount, 2, "Incorrect MoveNextCount value."); Assert.AreEqual(cr.Name, "Bird", "Incorrect name of the coroutine."); }
public void TestNestedRaiseEvent() { var event1 = new Event(); var event2 = new Event(); var event3 = new Event(); var coroutineCreated = new Event(); var counterCoroutineA = 0; var counter = 0; var infinite = CoroutineHandler.Start(OnCoroutineCreatedInfinite()); CoroutineHandler.Start(OnEvent1()); CoroutineHandler.Tick(1); CoroutineHandler.RaiseEvent(event1); CoroutineHandler.Tick(1); CoroutineHandler.RaiseEvent(event2); CoroutineHandler.Tick(1); CoroutineHandler.RaiseEvent(event3); Assert.AreEqual(3, counter); Assert.AreEqual(2, counterCoroutineA); infinite.Cancel(); IEnumerator <Wait> OnCoroutineCreatedInfinite() { while (true) { yield return(new Wait(coroutineCreated)); counterCoroutineA++; } } IEnumerator <Wait> OnEvent1() { yield return(new Wait(event1)); counter++; CoroutineHandler.Start(OnEvent2()); CoroutineHandler.RaiseEvent(coroutineCreated); } IEnumerator <Wait> OnEvent2() { yield return(new Wait(event2)); counter++; CoroutineHandler.Start(OnEvent3()); CoroutineHandler.RaiseEvent(coroutineCreated); } IEnumerator <Wait> OnEvent3() { yield return(new Wait(event3)); counter++; } }
protected override Task Render() { CoroutineHandler.Tick(ImGui.GetIO().DeltaTime); if (data % 5 == 1) { CoroutineHandler.RaiseEvent(myevent); } ImGui.Begin("Sample Overlay", ref isRunning, ImGuiWindowFlags.AlwaysAutoResize); ImGui.Text($"Total Time/Delta Time: {ImGui.GetTime():F3}/{ImGui.GetIO().DeltaTime:F3}"); ImGui.NewLine(); ImGui.Text($"Counter: {this.data}"); ImGui.Text($"{this.data2}"); ImGui.NewLine(); ImGui.Text($"Event Coroutines: {CoroutineHandler.EventCount}"); ImGui.Text($"Ticking Coroutines: {CoroutineHandler.TickingCount}"); ImGui.NewLine(); ImGui.Text($"Coroutine Name: {myRoutine1.Name}"); ImGui.Text($"Avg Execution Time: {myRoutine1.AverageMoveNextTime.TotalMilliseconds}"); ImGui.Text($"Total Executions: {myRoutine1.MoveNextCount}"); ImGui.Text($"Total Execution Time: {myRoutine1.TotalMoveNextTime.TotalMilliseconds}"); ImGui.NewLine(); ImGui.Text($"Coroutine Name: {myRoutine2.Name}"); ImGui.Text($"Avg Execution Time: {myRoutine2.AverageMoveNextTime.TotalMilliseconds}"); ImGui.Text($"Total Executions: {myRoutine2.MoveNextCount}"); ImGui.Text($"Total Execution Time: {myRoutine2.TotalMoveNextTime.TotalMilliseconds}"); if (ImGui.Button("Show/Hide Demo Window")) { demoWindow = !demoWindow; } ImGui.End(); if (!isRunning) { Close(); } if (demoWindow) { ImGui.ShowDemoWindow(ref demoWindow); } return(Task.CompletedTask); }
public void TestInfiniteCoroutineNeverFinishesUnlessCanceled() { var myEvent = new Event(); var myOtherEvent = new Event(); var counter = 0; IEnumerator <Wait> OnEventTriggeredInfinite() { while (true) { counter++; yield return(new Wait(myEvent)); } } void SetCounterToUnreachableValue(ActiveCoroutine coroutine) { counter = -100; } var cr = CoroutineHandler.Start(OnEventTriggeredInfinite()); CoroutineHandler.Tick(1); cr.OnFinished += SetCounterToUnreachableValue; for (var i = 0; i < 50; i++) { CoroutineHandler.RaiseEvent(myOtherEvent); } for (var i = 0; i < 50; i++) { CoroutineHandler.RaiseEvent(myEvent); } Assert.AreEqual(51, counter, "Incorrect counter value."); Assert.AreEqual(false, cr.IsFinished, "Incorrect IsFinished value."); Assert.AreEqual(false, cr.WasCanceled, "Incorrect IsCanceled value."); Assert.AreEqual(51, cr.MoveNextCount, "Incorrect MoveNextCount value."); cr.Cancel(); Assert.AreEqual(true, cr.WasCanceled, "Incorrect IsCanceled value after canceling."); Assert.AreEqual(-100, counter, "OnFinished event not triggered when canceled."); Assert.AreEqual(51, cr.MoveNextCount, "Incorrect MoveNextCount value."); Assert.AreEqual(true, cr.IsFinished, "Incorrect IsFinished value."); }
public void MovingCoroutineTest() { var evt = new Event(); IEnumerator <Wait> MovingCoroutine() { while (true) { yield return(new Wait(evt)); yield return(new Wait(0d)); } } var moving = CoroutineHandler.Start(MovingCoroutine(), "MovingCoroutine"); CoroutineHandler.RaiseEvent(evt); CoroutineHandler.RaiseEvent(evt); CoroutineHandler.RaiseEvent(evt); CoroutineHandler.RaiseEvent(evt); CoroutineHandler.Tick(1d); CoroutineHandler.Tick(1d); CoroutineHandler.Tick(1d); CoroutineHandler.Tick(1d); CoroutineHandler.RaiseEvent(evt); CoroutineHandler.Tick(1d); CoroutineHandler.RaiseEvent(evt); CoroutineHandler.Tick(1d); CoroutineHandler.RaiseEvent(evt); CoroutineHandler.Tick(1d); CoroutineHandler.Tick(1d); CoroutineHandler.RaiseEvent(evt); CoroutineHandler.Tick(1d); CoroutineHandler.RaiseEvent(evt); CoroutineHandler.Tick(1d); CoroutineHandler.RaiseEvent(evt); moving.Cancel(); }
protected override Task Render() { // Render CoroutineHandler.Tick(ImGui.GetIO().DeltaTime); if (data % 5 == 1) { CoroutineHandler.RaiseEvent(myevent); } Process[] game = Process.GetProcessesByName("Lords Mobile"); if (game.Length == 1) { ImGui.Begin("Neki_play Engine for Lords Mobile", ref isRunning, ImGuiWindowFlags.AlwaysAutoResize); Rect r = new Rect(); if (WinAPI.GetWindowRect((game[0].MainWindowHandle), ref r)) { if (!first) { ImGui.SetWindowPos(new System.Numerics.Vector2(r.left, r.top)); ImGui.SetWindowSize(new System.Numerics.Vector2(r.bottom, r.right)); first = true; } } VAMemory memory = new VAMemory(game[0]); var energyadress = Utils.ProcessUtils.PointRead(Utils.ProcessUtils.getModuleAdress("GameAssembly.dll", Process.GetProcessesByName("Lords Mobile").FirstOrDefault()), new[] { 0x02657640, 0x88, 0x0, 0x40, 0x20, 0x490 }); ImGui.Text("Energy: " + memory.ReadInt32(energyadress)); ImGui.NewLine(); ImGui.Text("By: Neki_play and SkillShop Techonology"); ImGui.End(); if (!isRunning) { Close(); } } else { first = false; } return(Task.CompletedTask); }
/// <summary> /// Infinitely renders the over (and execute co-routines) till it's closed. /// </summary> public static void RunInfiniteLoop() { DateTime previous = DateTime.Now; DateTime current; TimeSpan interval; float sec; while (window.Exists && !Close) { InputSnapshot snapshot = window.PumpEvents(); if (!window.Exists) { break; } current = DateTime.Now; interval = current - previous; sec = (float)interval.TotalSeconds; previous = current; imController.Update(sec > 0 ? sec : 0.001f, snapshot, window.Handle); CoroutineHandler.Tick(interval.TotalSeconds); if (Visible) { CoroutineHandler.RaiseEvent(OnRender); } commandList.Begin(); commandList.SetFramebuffer(graphicsDevice.MainSwapchain.Framebuffer); commandList.ClearColorTarget(0, new RgbaFloat(clearColor.X, clearColor.Y, clearColor.Z, clearColor.W)); imController.Render(graphicsDevice, commandList); commandList.End(); graphicsDevice.SubmitCommands(commandList); graphicsDevice.SwapBuffers(graphicsDevice.MainSwapchain); } Dispose(); }
public void TestPriority() { var myEvent = new Event(); var counterShouldExecuteBefore0 = 0; IEnumerator <Wait> ShouldExecuteBefore0() { while (true) { yield return(new Wait(myEvent)); counterShouldExecuteBefore0++; } } var counterShouldExecuteBefore1 = 0; IEnumerator <Wait> ShouldExecuteBefore1() { while (true) { yield return(new Wait(myEvent)); counterShouldExecuteBefore1++; } } var counterShouldExecuteAfter = 0; IEnumerator <Wait> ShouldExecuteAfter() { while (true) { yield return(new Wait(myEvent)); if (counterShouldExecuteBefore0 == 1 && counterShouldExecuteBefore1 == 1) { counterShouldExecuteAfter++; } } } var counterShouldExecuteFinally = 0; IEnumerator <Wait> ShouldExecuteFinally() { while (true) { yield return(new Wait(myEvent)); if (counterShouldExecuteAfter > 0) { counterShouldExecuteFinally++; } } } const int highPriority = int.MaxValue; var before1 = CoroutineHandler.Start(ShouldExecuteBefore1(), priority: highPriority); var after = CoroutineHandler.Start(ShouldExecuteAfter()); var before0 = CoroutineHandler.Start(ShouldExecuteBefore0(), priority: highPriority); var @finally = CoroutineHandler.Start(ShouldExecuteFinally(), priority: -1); CoroutineHandler.Tick(1); CoroutineHandler.RaiseEvent(myEvent); Assert.AreEqual(1, counterShouldExecuteAfter, $"ShouldExecuteAfter counter {counterShouldExecuteAfter} is invalid."); Assert.AreEqual(1, counterShouldExecuteFinally, $"ShouldExecuteFinally counter {counterShouldExecuteFinally} is invalid."); before1.Cancel(); after.Cancel(); before0.Cancel(); @finally.Cancel(); }
public void TestNestedCoroutine() { var onChildCreated = new Event(); var onParentCreated = new Event(); var myEvent = new Event(); var counterAlwaysRunning = 0; IEnumerator <Wait> AlwaysRunning() { while (true) { yield return(new Wait(myEvent)); counterAlwaysRunning++; } } var counterChild = 0; IEnumerator <Wait> Child() { yield return(new Wait(myEvent)); counterChild++; } var counterParent = 0; IEnumerator <Wait> Parent() { yield return(new Wait(myEvent)); counterParent++; // OnFinish I will start child. } var counterGrandParent = 0; IEnumerator <Wait> GrandParent() { yield return(new Wait(myEvent)); counterGrandParent++; // Nested corotuine starting. var p = CoroutineHandler.Start(Parent()); CoroutineHandler.RaiseEvent(onParentCreated); // Nested corotuine starting in OnFinished. p.OnFinished += ac => { CoroutineHandler.Start(Child()); CoroutineHandler.RaiseEvent(onChildCreated); }; } var always = CoroutineHandler.Start(AlwaysRunning()); CoroutineHandler.Start(GrandParent()); Assert.AreEqual(0, counterAlwaysRunning, "Always running counter is invalid at event 0."); Assert.AreEqual(0, counterGrandParent, "Grand Parent counter is invalid at event 0."); Assert.AreEqual(0, counterParent, "Parent counter is invalid at event 0."); Assert.AreEqual(0, counterChild, "Child counter is invalid at event 0."); CoroutineHandler.Tick(1); CoroutineHandler.RaiseEvent(myEvent); Assert.AreEqual(1, counterAlwaysRunning, "Always running counter is invalid at event 1."); Assert.AreEqual(1, counterGrandParent, "Grand Parent counter is invalid at event 1."); Assert.AreEqual(0, counterParent, "Parent counter is invalid at event 1."); Assert.AreEqual(0, counterChild, "Child counter is invalid at event 1."); CoroutineHandler.Tick(1); CoroutineHandler.RaiseEvent(myEvent); Assert.AreEqual(2, counterAlwaysRunning, "Always running counter is invalid at event 2."); Assert.AreEqual(1, counterGrandParent, "Grand Parent counter is invalid at event 2."); Assert.AreEqual(1, counterParent, "Parent counter is invalid at event 2."); Assert.AreEqual(0, counterChild, "Child counter is invalid at event 2."); CoroutineHandler.Tick(1); CoroutineHandler.RaiseEvent(myEvent); Assert.AreEqual(3, counterAlwaysRunning, "Always running counter is invalid at event 3."); Assert.AreEqual(1, counterGrandParent, "Grand Parent counter is invalid at event 3."); Assert.AreEqual(1, counterParent, "Parent counter is invalid at event 3."); Assert.AreEqual(1, counterChild, "Child counter is invalid at event 3."); CoroutineHandler.Tick(1); CoroutineHandler.RaiseEvent(myEvent); Assert.AreEqual(4, counterAlwaysRunning, "Always running counter is invalid at event 4."); Assert.AreEqual(1, counterGrandParent, "Grand Parent counter is invalid at event 4."); Assert.AreEqual(1, counterParent, "Parent counter is invalid at event 4."); Assert.AreEqual(1, counterChild, "Child counter is invalid at event 4."); always.Cancel(); }