Skip to content

Commit

Permalink
pattern and functions to change target framerate
Browse files Browse the repository at this point in the history
  • Loading branch information
Dregu committed Oct 6, 2023
1 parent bc90171 commit cf009b6
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 0 deletions.
48 changes: 48 additions & 0 deletions src/game_api/rpc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2074,3 +2074,51 @@ void update_state()
rf(state);
}
}

void set_frametime(std::optional<double> frametime)
{
static size_t offset = 0;
if (offset == 0)
offset = get_address("engine_frametime");
if (offset != 0)
{
if (frametime.has_value())
write_mem_recoverable("engine_frametime", offset, frametime.value(), true);
else
recover_mem("engine_frametime");
}
}

std::optional<double> get_frametime()
{
static size_t offset = 0;
if (offset == 0)
offset = get_address("engine_frametime");
if (offset != 0)
return memory_read<double>(offset);
return std::nullopt;
}

void set_frametime_inactive(std::optional<double> frametime)
{
static size_t offset = 0;
if (offset == 0)
offset = get_address("engine_frametime") + 0x10;
if (offset != 0)
{
if (frametime.has_value())
write_mem_recoverable("engine_frametime_inactive", offset, frametime.value(), true);
else
recover_mem("engine_frametime_inactive");
}
}

std::optional<double> get_frametime_inactive()
{
static size_t offset = 0;
if (offset == 0)
offset = get_address("engine_frametime") + 0x10;
if (offset != 0)
return memory_read<double>(offset);
return std::nullopt;
}
4 changes: 4 additions & 0 deletions src/game_api/rpc.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -144,3 +144,7 @@ void activate_crush_elevator_hack(bool activate);
void activate_hundun_hack(bool activate);
void set_boss_door_control_enabled(bool enable);
void update_state();
void set_frametime(std::optional<double> frametime);
std::optional<double> get_frametime();
void set_frametime_inactive(std::optional<double> frametime);
std::optional<double> get_frametime_inactive();
12 changes: 12 additions & 0 deletions src/game_api/script/lua_vm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2042,6 +2042,18 @@ end
/// Run state update manually, i.e. simulate one logic frame. Use in e.g. POST_UPDATE, but be mindful of infinite loops, this will cause another POST_UPDATE. Can even be called thousands of times to simulate minutes of gameplay in a few seconds.
lua["update_state"] = update_state;

/// Set engine target frametime (1/framerate, default 1/60). Set to 0 to go as fast as possible. Call without arguments to reset.
lua["set_frametime"] = set_frametime;

/// Get engine target frametime (1/framerate, default 1/60).
lua["get_frametime"] = get_frametime;

/// Set engine target frametime when game is unfocused (1/framerate, default 1/33). Set to 0 to go as fast as possible. Call without arguments to reset.
lua["set_frametime_unfocused"] = set_frametime_inactive;

/// Get engine target frametime when game is unfocused (1/framerate, default 1/33).
lua["get_frametime_unfocused"] = get_frametime_inactive;

lua.create_named_table("INPUTS", "NONE", 0, "JUMP", 1, "WHIP", 2, "BOMB", 4, "ROPE", 8, "RUN", 16, "DOOR", 32, "MENU", 64, "JOURNAL", 128, "LEFT", 256, "RIGHT", 512, "UP", 1024, "DOWN", 2048);

lua.create_named_table(
Expand Down
13 changes: 13 additions & 0 deletions src/game_api/search.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1840,6 +1840,19 @@ std::unordered_map<std::string_view, AddressRule> g_address_rules{
.at_exe()
.function_start(),
},
{
/* it's a static double, just find something that reads it
this+0x08 is clearly some kind of framerate related double, cause it's 60, but don't know what it does
this+0x10 is hopefully unfocused frametime for the other function, but maybe it needs own pattern
22d12248 00 00 00 double 0.01666666753590107
20 11 11
91 3f */
"engine_frametime"sv,
PatternCommandBuffer{}
.find_after_inst("48 8d 04 0a 48 85 d2 48 0f 44 c2 48 85 c9 48 0f 44 c1 66 0f 28 c8"_gh)
.decode_pc(4)
.at_exe(),
},
{
// Borrowed from Playlunky logger.cpp
"game_log_function"sv,
Expand Down

0 comments on commit cf009b6

Please sign in to comment.