From: Niki Roo Date: Sun, 23 Mar 2025 19:21:03 +0000 (+0100) Subject: add External support X-Git-Url: http://git.nikiroo.be/?a=commitdiff_plain;h=e8b6682792f3f5115e34c88f10dfb0e160b4f608;p=gamiki.git add External support --- diff --git a/gamiki/builder.py b/gamiki/builder.py index 1f58394..943143d 100644 --- a/gamiki/builder.py +++ b/gamiki/builder.py @@ -1,7 +1,7 @@ from pathlib import Path, PurePath from . import Library, Game -from .support import Support, SupportDos, SupportWin31, SupportGog +from .support import Support, SupportDos, SupportWin31, SupportGog, SupportExt class Builder: """Manage all the resources needed to run games.""" @@ -9,10 +9,11 @@ class Builder: self.libraries : list[Library] = [] self.games : list[Game] = [] - supports = [] + supports = [] # be careful about the order supports.append(SupportGog()) - supports.append(SupportWin31()) # prio over DOS + supports.append(SupportWin31()) supports.append(SupportDos()) + supports.append(SupportExt()) targets = [] config = Path("~/.games.cfg").expanduser() diff --git a/gamiki/support/__init__.py b/gamiki/support/__init__.py index 21f11f0..095447b 100644 --- a/gamiki/support/__init__.py +++ b/gamiki/support/__init__.py @@ -2,3 +2,4 @@ from .support import Support from .support_dos import SupportDos from .support_win31 import SupportWin31 from .support_gog import SupportGog +from .support_ext import SupportExt diff --git a/gamiki/support/support_ext.py b/gamiki/support/support_ext.py new file mode 100644 index 0000000..ff01430 --- /dev/null +++ b/gamiki/support/support_ext.py @@ -0,0 +1,51 @@ +from random import random +from math import floor +from os import environ +from pathlib import Path +from subprocess import run +from shutil import which + +from . import Support +from ..game import Game + +class SupportExt(Support): + """Supports external games via the 'games' docker or natively.""" + def __init__(self): + pass + + def supports(self, game: Game): + return game.dir.joinpath("start.sh").exists() + + def start(self, game: Game, params: list[str] = []): + dir = game.dir.resolve() + + env = environ.copy() + + ffile = None + if (Support.program("launch.sh") != None): + tfile = Path( + "/tmp/shared/.game-" + + "{0:6d}".format(floor(random() * 1000000)) + + ".sh" + ) + with open(tfile, 'w', encoding="utf-8") as data: + data.write("#!/bin/sh\n") + data.write("cd '" + dir.as_posix() + "'\n") + data.write("./start.sh\n") + tfile.chmod(0o777) + env["OPTS"] = "--entrypoint=" + tfile.as_posix() + cmd = [ + Support.program("launch.sh"), + "games", "--link", dir.as_posix() + ] + else: + cmd = [ dir.joinpath("start.sh").as_posix() ] + + print("Running", game.name) + rep = run(cmd, cwd=dir, env=env) + if (rep.returncode != 0): + print("\nError when running", game.name, ": RC", rep.returncode) + + if (tfile != ""): + tfile.unlink() +