From 0689bfa0555b7efccfd838cff0a6878cfd63ad90 Mon Sep 17 00:00:00 2001 From: Niki Roo Date: Fri, 25 Apr 2025 14:24:46 +0200 Subject: [PATCH] new: intro videos --- gamiki/game.py | 15 ++++++++++++++- gamiki/support/commands.py | 13 +++++++++++++ gamiki/support/custom_commands.py | 8 ++++++++ gamiki/support/support.py | 5 +++++ 4 files changed, 40 insertions(+), 1 deletion(-) diff --git a/gamiki/game.py b/gamiki/game.py index cd8b15f..541d7a3 100644 --- a/gamiki/game.py +++ b/gamiki/game.py @@ -3,6 +3,7 @@ from typing import Any from pathlib import PurePath, Path from time import time from datetime import datetime +from random import choice class Game(dict): """Game object generated from a directory.""" @@ -19,6 +20,7 @@ class Game(dict): sessions : list = None # list of (YYYY-MM-DD, HH:MM, time in sec) icon : Path = None # "main" icon icons : dict = None # all found icons + intro : list = None # all found intro videos support : Any = None # should be gamiki.Support library : Any = None # should be gamiki.Library @@ -48,10 +50,14 @@ class Game(dict): if (":" in opt): self.opts[opt.split(":")[0]] = opt.split(":", maxsplit=1)[1] + self.intro = [] + if (self.get("Intr")): + self.intro = self["Intr"].split(";") + def _init_icons(self): self.icon = self._read_icon() self.icons = { "main": self.icon } - for key in [ "square", "icon" ]: + for key in [ "square", "icon", "vertical", "banner", "logo" ]: icon = self._read_icon("-" + key) if (icon): self.icons[key] = icon @@ -193,6 +199,13 @@ class Game(dict): begin = time() today = datetime.now() + if (self.intro): + intro = choice(self.intro) + print("Selected intro video:", intro) + rc = self.support._video(self.dir.joinpath(intro)) + if (rc != 0): + print("Video player failed with RC", rc) + self.support._start(self, opt) elapsed = int(time() - begin) diff --git a/gamiki/support/commands.py b/gamiki/support/commands.py index edf0f74..ec0b07d 100644 --- a/gamiki/support/commands.py +++ b/gamiki/support/commands.py @@ -5,6 +5,19 @@ from pathlib import PurePath import gamiki.support.custom_commands as custom +def video(file: PurePath) -> int: + cmd, env = custom.video(file) + if (not cmd): + prog = custom.program("mpv") + if (prog): + cmd = [ prog, "--fullscreen" ] + else: + raise RuntimeError("Video player not found") + + cmd.append(file.as_posix()) + + return run(cmd, env=env).returncode + def dosbox(dir: PurePath, link: PurePath, opt: str = None) -> int: cmd, env = custom.cmd("dosbox", link, None) diff --git a/gamiki/support/custom_commands.py b/gamiki/support/custom_commands.py index c4a9916..2af010f 100644 --- a/gamiki/support/custom_commands.py +++ b/gamiki/support/custom_commands.py @@ -13,6 +13,14 @@ def program(program: str) -> str: return __programs__[program] +def video(file: PurePath) -> (list, dict): + cmd = None + env = environ.copy() + if (program("local.sh")): + cmd = [ "local.sh", "mpv", "--fullscreen", file.as_posix() ] + + return cmd, env + def cmd(prog:str, t:PurePath, lnk:PurePath, pre:PurePath=None) -> (list, dict): cmd = None diff --git a/gamiki/support/support.py b/gamiki/support/support.py index a437099..20b8a0e 100644 --- a/gamiki/support/support.py +++ b/gamiki/support/support.py @@ -1,8 +1,10 @@ from typing import Optional from subprocess import run from shutil import which +from pathlib import PurePath from gamiki import Game +from gamiki.support.commands import video class Support: """Can detect and start games.""" @@ -22,4 +24,7 @@ class Support: def _error(self, game: Game, rc: int): print("\nError when running", game.name + ":", "RC", rc) + + def _video(self, file: PurePath) -> int: + return video(file) -- 2.27.0