Skip to content

Instantly share code, notes, and snippets.

@teekennedy
Last active November 30, 2022 07:47
Show Gist options
  • Save teekennedy/9b3c1bf21495eca2971fa9527080bb68 to your computer and use it in GitHub Desktop.
Save teekennedy/9b3c1bf21495eca2971fa9527080bb68 to your computer and use it in GitHub Desktop.
Hotfix for freezing RenPy games that run on the Steam Deck
# This script hot patches RenPy's Steam integration for users who encounter game freezes on the Steam Deck.
# It is meant as a temporary fix until the underlying cause is addressed.
# See https://github.com/renpy/renpy/issues/3978 for details and updates.
# To use, download this file and place it under the games/ folder of your local RenPy installation.
init python in steam_deck_hotfix:
from datetime import datetime
import steamapi
hotfix_applied = False
last_called = 0
last_result = False
last_logged = 0
call_count = 0
def rate_limited_overlay_needs_present():
"""
Rate limited replacement for steam.overlay_needs_present.
Avoids calling the underlying steamapi.BOverlayNeedsPresent too often by returning a cached result.
"""
global last_called
global last_result
now = datetime.today().timestamp()
global last_logged
global call_count
if now > last_logged + 1:
last_logged = now
renpy.write_log("Overlay check called %r times in the last second.", call_count)
call_count = 0
# Steamworks SDK doc says that 33 hertz is the ideal rate to call BOverlayNeedsPresent
if now > last_called + (1.0 / 33):
last_called = now
call_count += 1
last_result = steamapi.SteamUtils().BOverlayNeedsPresent()
return last_result
def apply_hotfix():
"""
Applies the hotfix by replacing the default overlay callback function with a rate-limited alternative.
"""
# Once the underlying issue is fixed in RenPy, this hotfix code may break.
# To prevent errors in the hotfix from crashing the game,
# all hotfix logic is wrapped in a try..except block.
try:
renpy.write_log("Loading Steam Deck hotfix..")
global hotfix_applied
if hotfix_applied:
renpy.write_log("Steam Deck hotfix already applied.")
return
steam = renpy.store.achievement.steam
if steam is None:
renpy.write_log("Steam integration is not loaded. Nothing to hotfix.")
return
callbacks = renpy.config.needs_redraw_callbacks
try:
callbacks.remove(steam.overlay_needs_present)
renpy.write_log("Removing original overlay check callback.")
except ValueError:
renpy.write_log("Overlay check callback not found.")
return
if steam.is_overlay_enabled():
renpy.write_log("Adding rate limited overlay check callback.")
callbacks.append(rate_limited_overlay_needs_present)
hotfix_applied = True
except Exception as e:
renpy.write_log("Hotfix ran into an unexpected error: %r", e)
renpy.write_log("Done loading Steam Deck hotfix.")
apply_hotfix()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment