-
-
Notifications
You must be signed in to change notification settings - Fork 100
/
Copy pathConstantTicker.cs
115 lines (97 loc) · 3.53 KB
/
ConstantTicker.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
using HarmonyLib;
using Multiplayer.Client.Desyncs;
using Multiplayer.Common;
using RimWorld;
using Verse;
namespace Multiplayer.Client
{
public static class ConstantTicker
{
public static bool ticking;
public static void Tick()
{
ticking = true;
try
{
TickShipCountdown(); // todo control the RNG seed here?
TickNonSimulation();
}
finally
{
ticking = false;
}
}
private static void TickNonSimulation()
{
DeferredStackTracing.ignoreTraces++;
try
{
TickSyncCoordinator();
TickAutosave();
}
finally
{
DeferredStackTracing.ignoreTraces--;
}
}
private const float TicksPerMinute = 60 * 60;
private const float TicksPerIngameDay = 2500 * 24;
private static void TickAutosave()
{
if (Multiplayer.LocalServer is not { } server) return;
if (server.settings.autosaveUnit == AutosaveUnit.Minutes)
{
var session = Multiplayer.session;
session.autosaveCounter++;
if (server.settings.autosaveInterval > 0 &&
session.autosaveCounter > server.settings.autosaveInterval * TicksPerMinute)
{
session.autosaveCounter = 0;
Autosaving.DoAutosave();
}
} else if (server.settings.autosaveUnit == AutosaveUnit.Days && server.settings.autosaveInterval > 0)
{
var anyMapCounterUp =
Multiplayer.game.mapComps
.Any(m => m.autosaveCounter > server.settings.autosaveInterval * TicksPerIngameDay);
if (anyMapCounterUp)
{
Multiplayer.game.mapComps.Do(m => m.autosaveCounter = 0);
Autosaving.DoAutosave();
}
}
}
private static void TickSyncCoordinator()
{
var sync = Multiplayer.game.sync;
if (sync.ShouldCollect && TickPatch.Timer % 30 == 0 && sync.currentOpinion != null)
{
sync.currentOpinion.roundMode = RoundMode.GetCurrentRoundMode();
if (!TickPatch.Simulating && (Multiplayer.LocalServer != null || Multiplayer.arbiterInstance))
Multiplayer.Client.SendFragmented(Packets.Client_SyncInfo, sync.currentOpinion.Serialize());
sync.AddClientOpinionAndCheckDesync(sync.currentOpinion);
sync.currentOpinion = null;
}
}
// Moved from RimWorld.ShipCountdown because the original one is called from Update
private static void TickShipCountdown()
{
if (ShipCountdown.timeLeft > 0f)
{
ShipCountdown.timeLeft -= 1 / 60f;
if (ShipCountdown.timeLeft <= 0f)
ShipCountdown.CountdownEnded();
}
}
}
[HarmonyPatch(typeof(ShipCountdown), nameof(ShipCountdown.CancelCountdown))]
static class CancelCancelCountdown
{
static bool Prefix() => Multiplayer.Client == null || Current.ProgramState != ProgramState.Playing;
}
[HarmonyPatch(typeof(ShipCountdown), nameof(ShipCountdown.ShipCountdownUpdate))]
static class ShipCountdownUpdatePatch
{
static bool Prefix() => Multiplayer.Client == null;
}
}