|
1 | 1 | using System; |
| 2 | +using System.Diagnostics; |
2 | 3 | using System.IO; |
3 | 4 | using System.Linq; |
| 5 | +using RimBridgeServer.Core; |
4 | 6 | using RimWorld; |
5 | 7 | using UnityEngine; |
6 | 8 | using Verse; |
@@ -63,6 +65,94 @@ public object SetTimeSpeed(string speed = "Normal") |
63 | 65 | }; |
64 | 66 | } |
65 | 67 |
|
| 68 | + public object PlayFor(int durationMs, string speed = "Normal", int pollIntervalMs = 25) |
| 69 | + { |
| 70 | + if (durationMs <= 0) |
| 71 | + { |
| 72 | + return new |
| 73 | + { |
| 74 | + success = false, |
| 75 | + message = "durationMs must be greater than 0.", |
| 76 | + state = RimWorldState.ToolStateSnapshot() |
| 77 | + }; |
| 78 | + } |
| 79 | + |
| 80 | + if (pollIntervalMs < 0) |
| 81 | + { |
| 82 | + return new |
| 83 | + { |
| 84 | + success = false, |
| 85 | + message = "pollIntervalMs cannot be negative.", |
| 86 | + state = RimWorldState.ToolStateSnapshot() |
| 87 | + }; |
| 88 | + } |
| 89 | + |
| 90 | + if (Enum.TryParse<TimeSpeed>(speed, ignoreCase: true, out var parsedSpeed) == false) |
| 91 | + { |
| 92 | + return new |
| 93 | + { |
| 94 | + success = false, |
| 95 | + message = $"Unknown time speed '{speed}'. Supported values: Normal, Fast, Superfast, Ultrafast.", |
| 96 | + state = RimWorldState.ToolStateSnapshot() |
| 97 | + }; |
| 98 | + } |
| 99 | + |
| 100 | + if (parsedSpeed == TimeSpeed.Paused) |
| 101 | + { |
| 102 | + return new |
| 103 | + { |
| 104 | + success = false, |
| 105 | + message = "play_for requires an active play speed. Use Normal, Fast, Superfast, or Ultrafast.", |
| 106 | + state = RimWorldState.ToolStateSnapshot() |
| 107 | + }; |
| 108 | + } |
| 109 | + |
| 110 | + try |
| 111 | + { |
| 112 | + Stopwatch stopwatch = null; |
| 113 | + var controller = new TimedPlaybackController(); |
| 114 | + var result = controller.PlayFor( |
| 115 | + durationMs, |
| 116 | + pollIntervalMs, |
| 117 | + getElapsedMs: () => stopwatch?.ElapsedMilliseconds ?? 0L, |
| 118 | + readState: () => RimBridgeMainThread.Invoke(CapturePlaybackState, timeoutMs: 5000), |
| 119 | + startPlayback: () => RimBridgeMainThread.Invoke(() => |
| 120 | + { |
| 121 | + stopwatch = Stopwatch.StartNew(); |
| 122 | + EnsurePlaybackRunning(parsedSpeed); |
| 123 | + }, timeoutMs: 5000), |
| 124 | + pausePlayback: () => RimBridgeMainThread.Invoke(PauseIfNeeded, timeoutMs: 5000)); |
| 125 | + |
| 126 | + return new |
| 127 | + { |
| 128 | + success = result.Success, |
| 129 | + requestedDurationMs = durationMs, |
| 130 | + elapsedMs = result.ElapsedMs, |
| 131 | + pollIntervalMs, |
| 132 | + timeSpeed = parsedSpeed.ToString(), |
| 133 | + paused = result.PausedAtEnd, |
| 134 | + initiallyPaused = result.InitiallyPaused, |
| 135 | + startTick = result.StartTick, |
| 136 | + endTick = result.EndTick, |
| 137 | + advancedTicks = result.AdvancedTicks, |
| 138 | + attempts = result.Attempts, |
| 139 | + probeFailureCount = result.ProbeFailureCount, |
| 140 | + lastProbeError = string.IsNullOrWhiteSpace(result.LastProbeError) ? null : result.LastProbeError, |
| 141 | + message = result.Message, |
| 142 | + state = result.Snapshot |
| 143 | + }; |
| 144 | + } |
| 145 | + catch (Exception ex) |
| 146 | + { |
| 147 | + return new |
| 148 | + { |
| 149 | + success = false, |
| 150 | + message = $"Failed to play for {durationMs}ms: {ex.Message}", |
| 151 | + state = RimWorldState.ToolStateSnapshot() |
| 152 | + }; |
| 153 | + } |
| 154 | + } |
| 155 | + |
66 | 156 | public object StartDebugGame() |
67 | 157 | { |
68 | 158 | if (LongEventHandler.AnyEventNowOrWaiting) |
@@ -247,4 +337,46 @@ public object LoadGame(string saveName) |
247 | 337 | state = RimWorldState.ToolStateSnapshot() |
248 | 338 | }; |
249 | 339 | } |
| 340 | + |
| 341 | + private static TimedPlaybackState CapturePlaybackState() |
| 342 | + { |
| 343 | + var hasPlayableGame = Current.ProgramState == ProgramState.Playing |
| 344 | + && Current.Game != null |
| 345 | + && Find.TickManager != null; |
| 346 | + var hasLongEvent = LongEventHandler.AnyEventNowOrWaiting; |
| 347 | + var atMainMenu = GenScene.InEntryScene || Current.ProgramState == ProgramState.Entry; |
| 348 | + |
| 349 | + return new TimedPlaybackState |
| 350 | + { |
| 351 | + Available = hasPlayableGame && !hasLongEvent, |
| 352 | + Paused = hasPlayableGame && Find.TickManager.Paused, |
| 353 | + TickCount = hasPlayableGame ? Find.TickManager.TicksGame : 0, |
| 354 | + SessionToken = Current.Game, |
| 355 | + Snapshot = RimWorldState.ToolStateSnapshot(), |
| 356 | + Message = hasPlayableGame |
| 357 | + ? (hasLongEvent ? "RimWorld is busy with a long event." : string.Empty) |
| 358 | + : (atMainMenu ? "RimWorld returned to the main menu." : "No playable game is currently loaded.") |
| 359 | + }; |
| 360 | + } |
| 361 | + |
| 362 | + private static void EnsurePlaybackRunning(TimeSpeed speed) |
| 363 | + { |
| 364 | + if (Current.ProgramState != ProgramState.Playing || Current.Game == null || Find.TickManager == null) |
| 365 | + throw new InvalidOperationException("No playable game is currently loaded."); |
| 366 | + if (LongEventHandler.AnyEventNowOrWaiting) |
| 367 | + throw new InvalidOperationException("RimWorld is busy with a long event."); |
| 368 | + |
| 369 | + Find.TickManager.CurTimeSpeed = speed; |
| 370 | + if (Find.TickManager.Paused) |
| 371 | + Find.TickManager.TogglePaused(); |
| 372 | + } |
| 373 | + |
| 374 | + private static void PauseIfNeeded() |
| 375 | + { |
| 376 | + if (Current.Game == null || Find.TickManager == null) |
| 377 | + return; |
| 378 | + |
| 379 | + if (!Find.TickManager.Paused) |
| 380 | + Find.TickManager.TogglePaused(); |
| 381 | + } |
250 | 382 | } |
0 commit comments