add External support
authorNiki Roo <niki@nikiroo.be>
Sun, 23 Mar 2025 19:21:03 +0000 (20:21 +0100)
committerNiki Roo <niki@nikiroo.be>
Sun, 23 Mar 2025 19:21:03 +0000 (20:21 +0100)
gamiki/builder.py
gamiki/support/__init__.py
gamiki/support/support_ext.py [new file with mode: 0644]

index 1f58394b27ed0b9e2cccd7ee38aa7adea3efba35..943143d31c4b2ebfd90e85f339abd9a8880fb217 100644 (file)
@@ -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()
index 21f11f0a41d657bc987578ee74e630a5bfe51747..095447b8e5d4e8b0895c2a42888b9e84ff2fd64d 100644 (file)
@@ -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 (file)
index 0000000..ff01430
--- /dev/null
@@ -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()
+