Skip to content

Commit

Permalink
#274 Add debug events synchronization
Browse files Browse the repository at this point in the history
  • Loading branch information
polycone committed Oct 23, 2023
1 parent b9ecd92 commit 18d8aa6
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 7 deletions.
23 changes: 23 additions & 0 deletions src/MultiplayerMod/Game/Debug/GameDebugEvents.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using HarmonyLib;
using JetBrains.Annotations;
using MultiplayerMod.ModRuntime.Context;

namespace MultiplayerMod.Game.Debug;

[HarmonyPatch]
public static class GameDebugEvents {

public static event System.Action? GameFrameStepping;
public static event System.Action? SimulationStepping;

[HarmonyPrefix, UsedImplicitly]
[HarmonyPatch(typeof(SpeedControlScreen), nameof(SpeedControlScreen.DebugStepFrame))]
[RequireExecutionLevel(ExecutionLevel.Game)]
private static void DebugStepFramePrefix() => GameFrameStepping?.Invoke();

[HarmonyPrefix, UsedImplicitly]
[HarmonyPatch(typeof(global::Game), nameof(global::Game.ForceSimStep))]
[RequireExecutionLevel(ExecutionLevel.Game)]
private static void ForceSimStepPrefix() => SimulationStepping?.Invoke();

}
40 changes: 33 additions & 7 deletions src/MultiplayerMod/Game/UI/GameSpeedControlEvents.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using HarmonyLib;
using JetBrains.Annotations;
using MultiplayerMod.AttributeProcessor;
using MultiplayerMod.Core.Patch;
using MultiplayerMod.ModRuntime.Context;

namespace MultiplayerMod.Game.UI;
Expand All @@ -11,23 +17,43 @@ public static class GameSpeedControlEvents {
public static event System.Action? GameResumed;
public static event Action<int>? SpeedChanged;

[HarmonyPostfix]
private static bool eventsEnabled = true;

[HarmonyPostfix, UsedImplicitly]
[HarmonyPatch(nameof(SpeedControlScreen.SetSpeed))]
[RequireExecutionLevel(ExecutionLevel.Game)]
[RequireEnabledEvents]
// ReSharper disable once InconsistentNaming
// ReSharper disable once UnusedMember.Local
private static void SetSpeedPostfix(int Speed) => SpeedChanged?.Invoke(Speed);

[HarmonyPostfix]
[HarmonyPostfix, UsedImplicitly]
[HarmonyPatch(nameof(SpeedControlScreen.OnPause))]
[RequireExecutionLevel(ExecutionLevel.Game)]
// ReSharper disable once UnusedMember.Local
private static void OnPausePostfix() => GamePaused?.Invoke();
private static void OnPausePostfix() {
if (!eventsEnabled) {
eventsEnabled = true;
return;
}
GamePaused?.Invoke();
}

[HarmonyPostfix]
[HarmonyPostfix, UsedImplicitly]
[HarmonyPatch(nameof(SpeedControlScreen.OnPlay))]
[RequireExecutionLevel(ExecutionLevel.Game)]
// ReSharper disable once UnusedMember.Local
[RequireEnabledEvents]
private static void OnPlayPostfix() => GameResumed?.Invoke();

[HarmonyPrefix, UsedImplicitly]
[HarmonyPatch(nameof(SpeedControlScreen.DebugStepFrame))]
[RequireExecutionLevel(ExecutionLevel.Multiplayer)]
private static void DebugStepFramePrefix() => eventsEnabled = false;

[AttributeUsage(AttributeTargets.Method)]
[ConditionalInvocation(typeof(RequireEnabledEventsAttribute), nameof(CheckEnabled))]
private class RequireEnabledEventsAttribute : Attribute {

private static bool CheckEnabled() => eventsEnabled;

}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using System;

namespace MultiplayerMod.Multiplayer.Commands.Debug;

[Serializable]
public class DebugGameFrameStep : MultiplayerCommand {

public override void Execute(MultiplayerCommandContext context) => SpeedControlScreen.Instance.DebugStepFrame();

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using System;

namespace MultiplayerMod.Multiplayer.Commands.Debug;

[Serializable]
public class DebugSimulationStep : MultiplayerCommand {

public override void Execute(MultiplayerCommandContext context) => global::Game.Instance.ForceSimStep();

}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using JetBrains.Annotations;
using MultiplayerMod.Core.Dependency;
using MultiplayerMod.Core.Logging;
using MultiplayerMod.Game.Debug;
using MultiplayerMod.Game.Mechanics.Objects;
using MultiplayerMod.Game.Mechanics.Printing;
using MultiplayerMod.Game.UI;
Expand All @@ -9,6 +10,7 @@
using MultiplayerMod.Game.UI.SideScreens;
using MultiplayerMod.Game.UI.Tools.Events;
using MultiplayerMod.Multiplayer.Commands.Alerts;
using MultiplayerMod.Multiplayer.Commands.Debug;
using MultiplayerMod.Multiplayer.Commands.Gameplay;
using MultiplayerMod.Multiplayer.Commands.Overlay;
using MultiplayerMod.Multiplayer.Commands.Player;
Expand Down Expand Up @@ -54,6 +56,7 @@ private void Bind() {
BindTools();
BindOverlays();
BindMechanics();
BindDebug();
}

private void BindSpeedControl() {
Expand Down Expand Up @@ -160,4 +163,9 @@ private void BindSideScreens() {
TimerSideScreenEvents.UpdateLogicTimeSensor += args => client.Send(new UpdateLogicTimeSensor(args));
}

private void BindDebug() {
GameDebugEvents.GameFrameStepping += () => client.Send(new DebugGameFrameStep());
GameDebugEvents.SimulationStepping += () => client.Send(new DebugSimulationStep());
}

}

0 comments on commit 18d8aa6

Please sign in to comment.