From ff9cb5e5bd6098c6028ab177630da20aa2885be0 Mon Sep 17 00:00:00 2001 From: Niki Roo Date: Tue, 25 Mar 2025 16:38:10 +0100 Subject: [PATCH] new custom_commands --- gamiki/support/commands.py | 66 ++++++++++++------------------- gamiki/support/custom_commands.py | 44 +++++++++++++++++++++ 2 files changed, 70 insertions(+), 40 deletions(-) create mode 100644 gamiki/support/custom_commands.py diff --git a/gamiki/support/commands.py b/gamiki/support/commands.py index 9b4f987..613b7dd 100644 --- a/gamiki/support/commands.py +++ b/gamiki/support/commands.py @@ -3,26 +3,16 @@ from shutil import which from os import environ from pathlib import PurePath -__programs: dict[str, str] = None - -def program(program: str) -> str: - global __programs - - if (__programs == None): - __programs = {} - - if (program not in __programs): - __programs[program] = which(program) - - return __programs[program] +import gamiki.support.custom_commands as custom def dosbox(dir: PurePath, link: PurePath) -> int: - if (program("app.sh") != None): - cmd = [ program("app.sh"),"--wait","dosbox","--link",link.as_posix() ] - elif(program("dosbox") != None): - cmd = [ program("dosbox") ] - else: - raise RuntimeError("DosBox not found") + + cmd, env = custom.cmd("dosbox", link, None) + if (not cmd): + if (prog := custom.program("dosbox")): + cmd = [ prog ] + else: + raise RuntimeError("DosBox not found") base = dir.joinpath("base.conf") if (base.exists()): @@ -31,32 +21,28 @@ def dosbox(dir: PurePath, link: PurePath) -> int: cmd.append("-conf") cmd.append(dir.joinpath("start.conf").as_posix()) - return run(cmd, cwd=dir).returncode - -def start_sh(start: PurePath) -> int: - env = environ.copy() - - if (program("launch.sh") != None): - cmd = [ program("launch.sh"), "games", "--dlink", start.as_posix() ] - env["OPTS"] = "--entrypoint=bash" - else: - cmd = [ start.as_posix() ] - - return run(cmd, cwd=start.parent, env=env).returncode + return run(cmd, cwd=dir, env=env).returncode def wine(dir: PurePath) -> int: - env = environ.copy() - wine_bat = dir.joinpath("wine.bat").as_posix() - wine_prefix = dir.joinpath("wine.prefix").as_posix() + wine_bat = dir.joinpath("wine.bat") + wine_prefix = dir.joinpath("wine.prefix") - if (program("launch.sh") != None): - cmd = [ program("launch.sh"), "wine", "--dlink", wine_bat ] - env["OPTS"] = "-e WINEPREFIX='" + wine_prefix.replace("'", "\\'") + "'" - elif (program("wine") != None): - cmd = [ "wine", wine_bat ] - else: - raise RuntimeError("wine not found") + cmd, env = custom.cmd("wine", wine_bat, wine_prefix) + if (not cmd): + if (prog := custom.program("wine")): + cmd = [ prog, wine_bat.as_posix() ] + env["WINEPREFIX"] = wine_prefix.as_posix() + else: + raise RuntimeError("wine not found") return run(cmd, cwd=dir, env=env).returncode +def start_sh(start: PurePath) -> int: + + cmd, env = custom.cmd("start_sh", start, None) + if (not cmd): + cmd = [ start.as_posix() ] + + return run(cmd, cwd=start.parent, env=env).returncode + diff --git a/gamiki/support/custom_commands.py b/gamiki/support/custom_commands.py new file mode 100644 index 0000000..e033557 --- /dev/null +++ b/gamiki/support/custom_commands.py @@ -0,0 +1,44 @@ +from typing import Optional +from subprocess import run +from shutil import which +from os import environ +from pathlib import PurePath + +__programs__: dict[str, Optional[str]] = { } + +def program(program: str) -> Optional[str]: + global __programs__ + + if (program not in __programs__): + __programs__[program] = which(program) + + return __programs__[program] + +def cmd ( + prog: str, p1: PurePath, p2: Optional[PurePath] + ) -> (Optional[list[str]], dict[str, str]): + + cmd = None + env = environ.copy() + + match prog: + case "dosbox": + if (program("app.sh")): + cmd = [ + program("app.sh"),"--wait","dosbox","--link",p1.as_posix() + ] + case "wine": + if (program("launch.sh")): + cmd = [ program("launch.sh"), "wine", "--dlink", p1.as_posix() ] + env["OPTS"] = ( + "-e WINEPREFIX='" + p2.as_posix().replace("'", "\\'") + "'" + ) + case "start_sh": + if (program("launch.sh")): + cmd = [ + program("launch.sh"), "games", "--dlink", p1.as_posix() + ] + env["OPTS"] = "--entrypoint=bash" + + return cmd, env + -- 2.27.0