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

exec.py discussion #5607

Closed
furudean opened this issue Jul 1, 2024 · 6 comments
Closed

exec.py discussion #5607

furudean opened this issue Jul 1, 2024 · 6 comments

Comments

@furudean
Copy link

furudean commented Jul 1, 2024

I have a need to detect when the game autoreloads so that I can inject some code with exec.py to communicate between Ren'Py and my Visual Studio Code extension. Since this is a development aid, I don't want to ask people to add this code to the game themselves.

Currently I'm using the string Resetting cache. to detect restarts, but this does not seem very reliable, being prone to false positives or being pulled entirely in a future update of Ren'Py.

print("Resetting cache.")

If there was a way to provide this, either as something outputted natively by Ren'Py or something that's configurable that works fine for this use case.

Thanks!

@mal
Copy link
Member

mal commented Jul 1, 2024

This (and honestly the exec.py feature) feel like candidates for an rpe that you can distribute with and/or have your extension install into projects that choose to use your extension rather than trying to build two-way communication into the core engine piecemeal for each bump in the road. :/

@furudean
Copy link
Author

furudean commented Jul 1, 2024

rpe?

@qTich
Copy link
Contributor

qTich commented Jul 1, 2024

rpe?

This is a zip archive (with the extension .rpe) that contains autorun.py.

https://github.com/renpy/renpy/blob/master/renpy/main.py#L148-L156

renpytom added a commit that referenced this issue Jul 2, 2024
It's a bit too footgun, so instead we'll use .rpe.py files to
inject the same thing. See the discussion in #5607.
@renpytom
Copy link
Member

renpytom commented Jul 2, 2024

I've removed exec.py from the core engine, and put it into a .rpe.py file. (That's a new feature - .rpe.py files work like .rpes, but don't need zipping up.)

The .rpe/.rpe.py files are re-run each time Ren'Py reloads, so you can make the signal you need.

Here's the file:

import renpy
import time
import os
import threading

# The path to the exec.py file, if it exists.
exec_py_exists = False

# The thread that scans for exec_py.
exec_py_thread = None

# The delay between exec_py scans.
exec_py_delay = 0.1

# Should the thread keep running?
run_scan_thread = True

def run_exec_py():

    global exec_py_exists

    if exec_py_exists:
        exec_py_path = os.path.join(renpy.config.basedir, "exec.py")

        try:
            with open(exec_py_path, "r") as f:
                text = f.read()
        except Exception as e:
            exec_py_exists = False
            return

        try:
            os.unlink(exec_py_path)
            exec_py_exists = False
        except Exception as e:
            renpy.display.log.write("Failed to remove exec.py:")
            renpy.display.log.exception()
            return

        renpy.python.py_exec(text)


def scan_exec_py():
    global exec_py_exists

    while run_scan_thread:
        time.sleep(.1)

        exec_py_path = os.path.join(renpy.config.basedir, "exec.py")

        if os.path.exists(exec_py_path) and not exec_py_exists:
            exec_py_exists = True

            renpy.exports.invoke_in_main_thread(run_exec_py)


def end_scan_thread():

    global exec_py_thread
    global run_scan_thread

    run_scan_thread = False

    if exec_py_thread:
        exec_py_thread.join()

    exec_py_thread = None


renpy.config.quit_callbacks.append(end_scan_thread)


exec_py_thread = threading.Thread(target=scan_exec_py)
exec_py_thread.daemon = True
exec_py_thread.start()

@furudean
Copy link
Author

furudean commented Jul 2, 2024

On topic of the issue itself, I think what I was looking for was renpy.config.quit_callbacks, though I haven't tested it yet. I'll have a stab at refactoring my extension to use the new .rpe.py system and report back

@furudean furudean changed the title A reliable signal for when the game reloads (i.e ctrl+r) exec.py discussion Jul 3, 2024
@furudean
Copy link
Author

furudean commented Jul 3, 2024

going to close this as i've had success replacing the functionality i need with .rpe

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants