Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix GameConditionManager.MapBrightnessTracker (Unnatural Darkness) desyncs #453

Merged
merged 1 commit into from
May 24, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions Source/Client/Patches/Determinism.cs
Original file line number Diff line number Diff line change
Expand Up @@ -491,4 +491,37 @@ private static int NewCacheStatus(int gameTick)
}
}

[HarmonyPatch(typeof(GameConditionManager.MapBrightnessTracker), nameof(GameConditionManager.MapBrightnessTracker.Tick))]
static class MapBrightnessLerpPatch
{
static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstruction> instr)
{
var patchCount = 0;

// The method is using delta time for darkness changes,
// which is not good for MP since it's tied to the FPS.
var target = AccessTools.DeclaredPropertyGetter(typeof(Time), nameof(Time.deltaTime));

foreach (var ci in instr)
{
if (ci.Calls(target))
{
// Replace deltaTime with a constant value.
// We use 1/60 since 1 second at speed 1 the deltaTime
// should (in perfect situation) be 60 ticks.
ci.opcode = OpCodes.Ldc_R4;
ci.operand = 1f / 60f;

patchCount++;
}

yield return ci;
}

const int expectedPatches = 1;
if (patchCount != expectedPatches)
Log.Error($"Replaced an incorrect amount of Time.deltaTime calls for GameConditionManager.MapBrightnessTracker:Tick (expected: {expectedPatches}, patched: {patchCount}). Was the original method changed?");
}
}

}
Loading