Loader: the title's game step allowed past 1/15 s (LEONIDA_SIM_MAX_STEP_MS); the scripts told 59 with 60 fps

- LEONIDA_SIM_MAX_STEP_MS=100|125|200|250 (unset: the title's own): the title's game timers step
  clamp(real time, 1 / rate, 1/15 s) a frame (+0x2226040, run from the frame's timer at +0x22264d0), so a frame
  longer than 66.7 ms moves game time only 66.7 ms: under 15 frames a second the world runs slower than the wall
  clock. The two loads of that ceiling (vmovss at +0x2226045 and +0x2226833) are pointed at another constant of
  the image's .rdata holding the asked value, the constants checked first (all or none). After
  kylepelham/El1xr 9372c47f (GPL-3.0), whose runs measured game time 0.92 and 0.89 of the wall clock with the
  title's ceiling and 0.97 and 0.98 with 100 ms. The step function's clamp was read in a local decompile
  (fast-decomp) of the 1.0.2.1 executable.
- SCRIPTS_59 (vipatch.rs, always on with the 60 fps patch): the function that tells the scripts the frame rate
  (+0x3eaf5d0, one million over the interval at +0x98f84ac, truncated) answers 59: with FPS_60's interval of
  16 666 it said 60, from which the title halves a per-frame count it hands the scripts (+0x234490). After
  trustno1-coder/VIctory-NVIDIA PR #65 (0.985 of real time at 30-36 fps) and El1xr's sim_target.rs (GPL-3.0).

Every place is checked against the 1.0.2.1 executable: the tests the_game_step_loads_point_at_the_ceiling_asked_for,
the_scripts_are_told_fifty_nine, and the retail-executable checks of hooks.rs and vipatch.rs (LEONIDA_GAME_DIR),
all passed; a run with LEONIDA_SIM_MAX_STEP_MS=100 logged every game-step patch applied.

AI-assisted (Claude). Tested on an RTX 4060 Ti 16 GB, driver 616.64, Ryzen 5 7600X.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
NotRexed
2026-11-29 23:24:01 -05:00
parent 998fcbcac0
commit 936843f590
3 changed files with 168 additions and 2 deletions

View File

@ -266,6 +266,99 @@ fn render_scale_patches(base: usize, scale: f32, fsr: bool) -> Vec<OwnedPatch> {
patches
}
/// Leonida: environment variable of the longest step the title's game time takes in one frame, in milliseconds
/// (`100`, `125`, `200` or `250`; unset, `0` or `67`: the title's own 1/15 s).
pub(crate) const MAX_STEP_ENV: &str = "LEONIDA_SIM_MAX_STEP_MS";
/// The title's game timers step `clamp(real time, 1 / rate, 1/15 s)` a frame (+0x2226040, which the frame's own
/// timer at +0x22264d0 runs): a frame longer than 66.7 ms moves game time 66.7 ms, so below 15 frames a second,
/// and at every hitch longer than that, the world runs slower than the wall clock and the sound. The ceiling is
/// the constant at [`MAX_STEP_TITLE`], loaded in these two places (`vmovss xmm, [rip + disp32]`, the displacement
/// its last four bytes: xmm3 in the step, xmm0 in the frame's timer). After kylepelham/El1xr 9372c47f (GPL-3.0),
/// whose runs measured game time against the wall clock at 0.92 and 0.89 with the title's ceiling and 0.97 and
/// 0.98 with 100 ms; the places and constants are checked here against the 1.0.2.1 executable.
const MAX_STEP_LOADS: [(usize, [u8; 4]); 2] = [(0x2226045, [0xC5, 0xFA, 0x10, 0x1D]), (0x2226833, [0xC5, 0xFA, 0x10, 0x05])];
/// The title's ceiling of a frame's game step: its value and the `.rdata` constant that holds it.
const MAX_STEP_TITLE: (f32, usize) = (1.0 / 15.0, 0x6159ec0);
/// The ceilings that can be asked for: milliseconds, the value, and a constant of the image's `.rdata` that holds
/// it (constants the title has, so nothing is added to the image).
const MAX_STEPS: [(u32, f32, usize); 4] =
[(100, 0.1, 0x6159dfc), (125, 0.125, 0x615a6b8), (200, 0.2, 0x6159f8c), (250, 0.25, 0x6159ca4)];
/// The ceiling `LEONIDA_SIM_MAX_STEP_MS` asks for: `Some(None)` for the title's own, `None` if not one of them.
fn parse_max_step(text: &str) -> Option<Option<(u32, f32, usize)>> {
match text.trim() {
"" | "0" | "67" => Some(None),
v => {
let ms = v.parse::<u32>().ok()?;
MAX_STEPS.iter().copied().find(|s| s.0 == ms).map(Some)
}
}
}
/// The patches that make the title's game step stop at `value` seconds, the constant at `constant`: the title's
/// ceiling and the new one checked to hold their values (the same bytes put back), then the two loads pointed at
/// the new one. Applied all or none ([`apply_all`]).
fn max_step_patches(value: f32, constant: usize) -> Vec<OwnedPatch> {
let load = |at: usize, op: [u8; 4], target: usize| {
let disp = (target as i64 - (at as i64 + 8)) as i32;
[&op[..], &disp.to_le_bytes()].concat()
};
let mut patches = vec![
OwnedPatch {
what: "game step: the title's longest step is 1/15 s",
rva: MAX_STEP_TITLE.1,
expect: MAX_STEP_TITLE.0.to_le_bytes().to_vec(),
new: MAX_STEP_TITLE.0.to_le_bytes().to_vec(),
},
OwnedPatch {
what: "game step: the constant of the longest step asked for holds it",
rva: constant,
expect: value.to_le_bytes().to_vec(),
new: value.to_le_bytes().to_vec(),
},
];
for (at, op) in MAX_STEP_LOADS {
patches.push(OwnedPatch {
what: "game step: a load of the longest step a frame",
rva: at,
expect: load(at, op, MAX_STEP_TITLE.1),
new: load(at, op, constant),
});
}
patches
}
/// Leonida: [`MAX_STEP_ENV`] for the 1.0.2.1 executable mapped at `base`. Returns how many places were written.
///
/// # Safety
/// As `disarm_watchdog`.
pub(crate) unsafe fn max_step(base: usize, hash: &blake3::Hash) -> usize {
let Ok(text) = std::env::var(MAX_STEP_ENV) else {
return 0;
};
let asked = match parse_max_step(&text) {
Some(Some(asked)) => asked,
Some(None) => return 0,
None => {
tracing::warn!(text, "{MAX_STEP_ENV} wants 100, 125, 200 or 250 (ms), or 0 for the title's 67");
return 0;
}
};
// Every image the loader maps comes here (the title's DLLs too); only the 1.0.2.1 executable has the places.
if !is_build_1_0_2_1(hash) {
tracing::debug!("{MAX_STEP_ENV}: not the 1.0.2.1 executable; nothing patched");
return 0;
}
let (ms, value, constant) = asked;
// SAFETY: forwarded from this function's contract.
let applied = unsafe { apply_all(base, &max_step_patches(value, constant)) };
tracing::info!(ms, applied, "the longest step the title's game time takes a frame ({MAX_STEP_ENV})");
applied
}
/// The scale `LEONIDA_RENDER_SCALE` asks for, if valid.
fn parse_scale(text: &str) -> Option<f32> {
let v: f32 = text.trim().parse().ok()?;
@ -546,6 +639,33 @@ mod tests {
for (value, rva) in FSR_SCALES {
assert_eq!(at(rva, 4), value.to_le_bytes(), "{value} at {rva:#x}");
}
// The game step's places and its constants (`LEONIDA_SIM_MAX_STEP_MS`).
for (_, value, constant) in MAX_STEPS {
for p in max_step_patches(value, constant) {
assert_eq!(at(p.rva, p.expect.len()), p.expect, "{} at {:#x}", p.what, p.rva);
}
}
}
/// The game step's loads are pointed from the title's 1/15 s at the constant asked for, the displacement
/// counted from the next instruction; the two constants are checked by bytes put back unchanged.
#[test]
fn the_game_step_loads_point_at_the_ceiling_asked_for() {
assert_eq!(parse_max_step(""), Some(None));
assert_eq!(parse_max_step("67"), Some(None));
assert_eq!(parse_max_step(" 100 "), Some(Some((100, 0.1, 0x6159dfc))));
assert_eq!(parse_max_step("150"), None);
let p = max_step_patches(0.1, 0x6159dfc);
assert_eq!(p.len(), 4);
assert_eq!((p[0].rva, p[0].expect.as_slice()), (0x6159ec0, (1.0f32 / 15.0).to_le_bytes().as_slice()));
assert_eq!(p[0].new, p[0].expect);
assert_eq!((p[1].rva, p[1].expect.as_slice()), (0x6159dfc, 0.1f32.to_le_bytes().as_slice()));
assert_eq!(p[1].new, p[1].expect);
// The title's bytes at the two loads, and the displacements to 0.1 from +0x222604d and +0x222683b.
assert_eq!(p[2].expect, [0xC5, 0xFA, 0x10, 0x1D, 0x73, 0x3E, 0xF3, 0x03]);
assert_eq!(p[2].new, [0xC5, 0xFA, 0x10, 0x1D, 0xAF, 0x3D, 0xF3, 0x03]);
assert_eq!(p[3].expect, [0xC5, 0xFA, 0x10, 0x05, 0x85, 0x36, 0xF3, 0x03]);
assert_eq!(p[3].new, [0xC5, 0xFA, 0x10, 0x05, 0xC1, 0x35, 0xF3, 0x03]);
}
#[test]

View File

@ -152,6 +152,30 @@ pub(crate) const FPS_60: GamePatch = GamePatch {
],
};
/// `mov eax, 59`, then `ret`: over the first six bytes of the function that tells the scripts the frame rate.
const FIFTY_NINE: &[u8] = &[0xB8, 59, 0, 0, 0, 0xC3];
/// With [`FPS_60`], the scripts told 59 frames a second, not 60. Always on, as [`FPS_60`] is.
///
/// The function at +0x3eaf5d0 tells the title's scripts the frame rate: one million over the frame interval in
/// microseconds (+0x98f84ac), truncated. With [`FPS_60`] the interval is 1 000 000 / 60 = 16 666 (an integer
/// division) and the answer 60, and from 60 on the title halves a per-frame count it hands the scripts
/// (+0x234490: 1.0 below 60, 0.5 from 60), made for twice the frames it draws here. Told 59 the count is the
/// title's own. After trustno1-coder/VIctory-NVIDIA PR #65 (59 told, 0.985 of real time measured at 30 to 36 frames
/// a second) and kylepelham/El1xr's `sim_target.rs` (GPL-3.0), whose notes describe the halving; the function's
/// bytes and the interval it reads are checked here against the 1.0.2.1 executable.
pub(crate) const SCRIPTS_59: GamePatch = GamePatch {
name: "60 fps: the scripts told 59",
env: None,
sites: &[Site {
what: "60 fps: the frame rate the scripts are told",
pattern: "8B 05 D6 8E A4 05 C5 FA 10 0D ?? ?? ?? ?? C4 E1 FA 2A C0 C5 F2 5E C0 C4 E1 FA 2C C0 C3",
at: 0,
new: FIFTY_NINE,
rva: 0x3eaf5d0,
}],
};
/// Environment variable of [`NO_VIGNETTE`].
pub(crate) const NO_VIGNETTE_ENV: &str = "LEONIDA_VIPATCH_NO_VIGNETTE";
@ -188,7 +212,7 @@ pub(crate) const NO_VIGNETTE: GamePatch = GamePatch {
};
/// Every patch, in the order they are applied.
pub(crate) const PATCHES: &[&GamePatch] = &[&FPS_60, &NO_VIGNETTE];
pub(crate) const PATCHES: &[&GamePatch] = &[&FPS_60, &SCRIPTS_59, &NO_VIGNETTE];
/// One change to make: `new` over `expect` at `rva` of the image.
#[derive(Debug, PartialEq, Eq)]
@ -478,8 +502,23 @@ mod tests {
switches.push(var);
}
}
// The frame-rate patch has no switch.
// The frame-rate patch has no switch, and neither has the rate the scripts are told, which goes with it.
assert!(FPS_60.env.is_none() && FPS_60.wanted());
assert!(SCRIPTS_59.env.is_none() && SCRIPTS_59.wanted());
}
/// The function that tells the scripts the frame rate answers 59 at once: `mov eax, 59; ret` over its load of
/// the frame interval (`mov eax, [rip + disp32]`), which with [`FPS_60`]'s interval of 16 666 gave 60.
#[test]
fn the_scripts_are_told_fifty_nine() {
let site = &SCRIPTS_59.sites[0];
let old = pattern(site.pattern).fixed(0, 6).expect("the load");
assert_eq!((old[0], old[1]), (0x8B, 0x05), "mov eax, [rip + disp32]");
assert_eq!(site.new, [0xB8, 59, 0, 0, 0, 0xC3]);
// One million over the interval, truncated as `vcvttss2si` does: 60 at FPS_60's interval, 59 told instead.
assert_eq!((1_000_000f32 / (1_000_000 / 60) as f32) as i32, 60);
// The interval at +0x98f84ac: the load's displacement counts from the next instruction.
assert_eq!(0x3eaf5d0 + 6 + 0x05a4_8ed6, 0x98f84ac);
}
#[test]

View File

@ -105,6 +105,13 @@ pub fn load(prepared: &PreparedImage, dir: &Path) -> Result<LoadedImage> {
crate::hooks::OUTPUT_ENV
);
}
// Leonida: the longest step the title's game time takes a frame (`LEONIDA_SIM_MAX_STEP_MS`), so that frames
// under 15 a second do not run in slow motion.
// SAFETY: just mapped, and no title code has run.
let stepped = unsafe { crate::hooks::max_step(loaded.base(), &prepared.original_hash) };
if stepped > 0 {
tracing::info!(places = stepped, "game step ({})", crate::hooks::MAX_STEP_ENV);
}
// Leonida: the game patches after VI-Patch (`vipatch.rs`, docs/vi_patch.md), for the title's executable
// only (its DLLs come here too): 60 frames a second always, the others by their switches.
if !loaded.info.is_dll() {