commit f045967591fd08faa575558c6270b53586715765 Author: Malasaur Date: Wed Aug 27 19:34:28 2025 +0200 Genesis commit diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..aa95aa6 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +__pycache__ +modules +program.py diff --git a/data.py b/data.py new file mode 100644 index 0000000..282eada --- /dev/null +++ b/data.py @@ -0,0 +1,12 @@ +from subprocess import PIPE, STDOUT, Popen +from typing import Callable, List, Tuple +from re import Pattern + +output_binds: List[Tuple[Pattern, Callable]] = [] +proc = None + + +def initializeProc(cmd): + global proc + proc = Popen(cmd.split(), stdin=PIPE, stdout=PIPE, + stderr=STDOUT, text=True, bufsize=1) diff --git a/minecraft.py b/minecraft.py new file mode 100644 index 0000000..3593de7 --- /dev/null +++ b/minecraft.py @@ -0,0 +1,51 @@ +from subprocess import PIPE, STDOUT, Popen +from importlib import import_module +from threading import Thread +from typing import Callable +from pathlib import Path +from re import compile + +import data + + +def reader() -> None: + if data.proc.stdout: + for line in data.proc.stdout: + line = line.rstrip("\n") + print(line) + for pattern, func in data.output_binds: + match = pattern.match(line) + if match: + func(*match.groups()) + break + + +def onConsoleOutput(pattern: str) -> Callable: + def decorator(func: Callable) -> Callable: + data.output_binds.append((compile(pattern), func)) + return func + return decorator + + +def sendCommand(cmd: str) -> None: + if data.proc.stdin: + data.proc.stdin.write(cmd+"\n") + data.proc.stdin.flush() + + +if __name__ == "__main__": + data.initializeProc("python -u program.py") + + for file in Path("modules").iterdir(): + if file.name.endswith(".py"): + import_module("modules."+file.name[:-3]) + + t = Thread(target=reader, daemon=True) + t.start() + + try: + while data.proc.poll() is None: + sendCommand(input()) + except KeyboardInterrupt: + sendCommand("stop") + data.proc.wait()