Skip to content

Commit e6f97f5

Browse files
authored
Sync "Drop pod raid at location" (#578)
* Sync new 1.6 debug option Drop pod raid at... #556 This PR fixes #556 and synchronizes the last step in "Drop pod raid at location..." debug option that utilizes the `Targeter.BeginTarget` method. This method isn't used elsewhere in the debug system, but it is invoked by certain other systems. It basically takes a action and invokes it on mouse click. I implemented the fix in a similiar fession to the other `DebugSource`. The main challenge I faced was ensuring that my prefix is executed _only_ when `Targeter.BeginTarget` is called from the `DebugActionsIncidents.ExecuteDropPodRaidAtLocation` method. To achieve this, I used a bit of reflection to check where the action was created. I'm not entirely sure if this is the optimal approach. Feedback on better alternatives is welcome. * Track current executing command type #556 Tracking the currently executing command type allows me to run the Targeter.BeginTargeting prefix only when it's executed within DebugToolCmd. Additionally, this change made some of the logic that checks whether a cmd is currently running redundant, so I removed it."
1 parent 14fefbb commit e6f97f5

File tree

5 files changed

+61
-10
lines changed

5 files changed

+61
-10
lines changed

Source/Client/AsyncTime/AsyncTimeComp.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,7 @@ public void ExecuteCmd(ScheduledCommand cmd)
261261

262262
executingCmdMap = map;
263263
TickPatch.currentExecutingCmdIssuedBySelf = cmd.issuedBySelf && !TickPatch.Simulating;
264+
TickPatch.currentExecutingCmdType = cmdType;
264265

265266
PreContext();
266267
map.PushFaction(cmd.GetFaction());
@@ -326,6 +327,7 @@ public void ExecuteCmd(ScheduledCommand cmd)
326327
PostContext();
327328

328329
TickPatch.currentExecutingCmdIssuedBySelf = false;
330+
TickPatch.currentExecutingCmdType = null;
329331
executingCmdMap = null;
330332

331333
if (!keepTheMap)

Source/Client/AsyncTime/AsyncWorldTimeComp.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ namespace Multiplayer.Client.AsyncTime;
1818
public class AsyncWorldTimeComp : IExposable, ITickable
1919
{
2020
public static bool tickingWorld;
21-
public static bool executingCmdWorld;
2221
private TimeSpeed timeSpeedInt;
2322

2423
public float TimeToTickThrough { get; set; }
@@ -172,8 +171,8 @@ public void ExecuteCmd(ScheduledCommand cmd)
172171
LoggingByteReader data = new LoggingByteReader(cmd.data);
173172
data.Log.Node($"{cmdType} Global");
174173

175-
executingCmdWorld = true;
176174
TickPatch.currentExecutingCmdIssuedBySelf = cmd.issuedBySelf && !TickPatch.Simulating;
175+
TickPatch.currentExecutingCmdType = cmdType;
177176

178177
PreContext();
179178
FactionExtensions.PushFaction(null, cmd.GetFaction());
@@ -252,7 +251,7 @@ public void ExecuteCmd(ScheduledCommand cmd)
252251
FactionExtensions.PopFaction();
253252
PostContext();
254253
TickPatch.currentExecutingCmdIssuedBySelf = false;
255-
executingCmdWorld = false;
254+
TickPatch.currentExecutingCmdType = null;
256255

257256
Multiplayer.game.sync.TryAddCommandRandomState(randState);
258257

Source/Client/Debug/DebugSync.cs

Lines changed: 54 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
1-
using System;
2-
using System.Collections.Generic;
31
using HarmonyLib;
42
using LudeonTK;
53
using Multiplayer.Client.Util;
64
using Multiplayer.Common;
7-
85
using RimWorld;
96
using RimWorld.Planet;
7+
using System;
8+
using System.Collections.Generic;
9+
using System.Reflection;
1010
using UnityEngine;
1111
using Verse;
12-
using static HarmonyLib.AccessTools;
1312

1413
namespace Multiplayer.Client
1514
{
@@ -92,6 +91,13 @@ public static void HandleCmd(ByteReader data)
9291
{
9392
(state.currentData as List<FloatMenuOption>)?.FirstOrDefault(o => o.Hash() == currentHash)?.action();
9493
}
94+
else if (source == DebugSource.Targeter)
95+
{
96+
var targetCell = new IntVec3(cursorX, 0, cursorZ);
97+
var handleTargetSelected = (state.currentData as Action<LocalTargetInfo>);
98+
99+
handleTargetSelected?.Invoke(new LocalTargetInfo(targetCell));
100+
}
95101
}
96102
finally
97103
{
@@ -117,13 +123,18 @@ public static void HandleCmd(ByteReader data)
117123
}
118124
}
119125

120-
public static void SendCmd(DebugSource source, int hash, string path, Map map)
126+
public static void SendCmd(DebugSource source, int hash, string path, Map map, IntVec3? forcedCell = null)
121127
{
122128
var writer = new LoggingByteWriter();
123129
writer.Log.Node($"Debug tool {source}, map {map.ToStringSafe()}");
124130
int cursorX = 0, cursorZ = 0;
125131

126-
if (map != null)
132+
if (forcedCell.HasValue)
133+
{
134+
cursorX = forcedCell.Value.x;
135+
cursorZ = forcedCell.Value.z;
136+
}
137+
else if (map != null)
127138
{
128139
cursorX = UI.MouseCell().x;
129140
cursorZ = UI.MouseCell().z;
@@ -216,6 +227,43 @@ public enum DebugSource
216227
Lister,
217228
Tool,
218229
FloatMenu,
230+
Targeter
231+
}
232+
233+
[HarmonyPatch]
234+
static class DebugExecuteDropPodRaidAtLocation
235+
{
236+
static MethodBase TargetMethod()
237+
{
238+
return AccessTools.Method(
239+
typeof(Targeter),
240+
nameof(Targeter.BeginTargeting),
241+
[
242+
typeof(TargetingParameters), typeof(Action<LocalTargetInfo>),
243+
typeof(Pawn), typeof(Action), typeof(Texture2D), typeof(bool)
244+
]);
245+
}
246+
247+
[HarmonyPrefix, HarmonyPriority(Priority.First)]
248+
static bool Prefix(ref Action<LocalTargetInfo> action)
249+
{
250+
if (Multiplayer.Client == null) return true;
251+
if (!Multiplayer.GameComp.debugMode) return true;
252+
if (!Multiplayer.ExecutingCmdDebugTool) return true;
253+
if (!DebugSync.ShouldHandle()) return true;
254+
255+
DebugSync.CurrentPlayerState.currentData = action;
256+
257+
if (Multiplayer.ExecutingCmds && !TickPatch.currentExecutingCmdIssuedBySelf)
258+
return false;
259+
260+
action = targetInfo =>
261+
{
262+
DebugSync.SendCmd(DebugSource.Targeter, 0, null, null, targetInfo.Cell);
263+
};
264+
265+
return true;
266+
}
219267
}
220268

221269
[HarmonyPatch(typeof(DebugActionNode), nameof(DebugActionNode.Enter))]

Source/Client/Multiplayer.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,8 @@ public static class Multiplayer
4949

5050
public static Faction RealPlayerFaction => Client != null ? game.RealPlayerFaction : Faction.OfPlayer;
5151

52-
public static bool ExecutingCmds => AsyncWorldTimeComp.executingCmdWorld || AsyncTimeComp.executingCmdMap != null;
52+
public static bool ExecutingCmds => TickPatch.currentExecutingCmdType != null;
53+
public static bool ExecutingCmdDebugTool => TickPatch.currentExecutingCmdType == CommandType.DebugTools;
5354
public static bool Ticking => AsyncWorldTimeComp.tickingWorld || AsyncTimeComp.tickingMap != null || ConstantTicker.ticking;
5455
public static Map MapContext => AsyncTimeComp.tickingMap ?? AsyncTimeComp.executingCmdMap;
5556

Source/Client/Patches/TickPatch.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ public static class TickPatch
2121
public static int tickUntil; // Ticks < tickUntil can be simulated
2222
public static int workTicks;
2323
public static bool currentExecutingCmdIssuedBySelf;
24+
public static CommandType? currentExecutingCmdType;
2425
public static bool serverFrozen;
2526
public static int frozenAt;
2627

0 commit comments

Comments
 (0)