-from os import listdir
-from os.path import isfile, isdir, join, expanduser, realpath, exists
+from pathlib import Path, PurePath
from . import Library, Game
from .Support import Support, SupportDos, SupportWin31, SupportGog
supports.append(SupportDos())
targets = []
- if (exists(expanduser("~/.games.cfg"))):
- with open(file, encoding="utf-8") as data:
+ config = Path("~/.games.cfg").expanduser()
+ if (config.exists()):
+ with open(config, encoding="utf-8") as data:
ln = data.strip()
if (not ln.startswith("#")):
- targets.append(ln)
+ targets.append(Path(ln))
else:
- targets.append(expanduser("~/Games"))
+ targets.append(Path("~/Games").expanduser())
for target in targets:
self.load_libs(supports, target)
for game in lib:
self.games.append(game)
- def load_libs(self, supports: list[Support], dir: str):
- for path in listdir(dir):
- fullpath = join(dir, path)
- if (not isdir(realpath(fullpath))):
+ def load_libs(self, supports: list[Support], dir: PurePath):
+ for path in dir.iterdir():
+ if (not path.is_dir()):
continue
try:
- self.libraries.append(Library(fullpath, supports))
+ self.libraries.append(Library(path, supports))
except FileNotFoundError:
pass
-from os.path import basename as basename
+from pathlib import PurePath
class Game(dict[str, str]):
"""Game object generated from a directory."""
- def __init__(self, library, dir: str):
- # TODO: doc: dir = full path
- self.dir = dir.removesuffix("/")
- self.name = basename(self.dir)
- self.code = basename(self.dir)
+ def __init__(self, library, dir: PurePath):
+ self.dir = dir
+ self.name = dir.name
+ self.code = dir.name
self.src = ""
self.tags = []
self.desc = ""
self.support = None
self.library = library
- self._read_info(self.dir + "/gameinfo.txt")
+ self._read_info(self.dir.joinpath("gameinfo.txt"))
if ("Name" in self):
self.name = self["Name"]
if ("Desc" in self):
self.desc = self["Desc"].replace("\\n", "\n").strip()
- def _read_info(self, file: str):
+ def _read_info(self, file: PurePath):
try:
with open(file, encoding="utf-8") as data:
for ln in data:
-from os import listdir
-from os.path import isdir, join, realpath, exists
+from pathlib import PurePath
from .Support import Support
from .Game import Game
class Library(list[Game]):
"""Manage a library (a folder) of Games."""
- def __init__(self, dir: str, supports: list[Support]):
- self.name = '' # TODO
+ def __init__(self, dir: PurePath, supports: list[Support]):
+ self.name = dir.name
self.dir = dir
self.supports = supports
self.preferredSupport = None
- # TODO: dir = fullpath
- config = join(self.dir, "games.cfg")
- if (not exists(config)):
+ config = self.dir.joinpath("games.cfg")
+ if (not config.exists()):
raise FileNotFoundError("Library is not configured (games.cfg)")
# TODO: get name from config?
- for path in listdir(self.dir):
- fullpath = join(self.dir, path)
- if (not isdir(realpath(fullpath))):
+ for path in self.dir.iterdir():
+ if (not path.is_dir()):
continue
- game = Game(self, fullpath)
+ game = Game(self, path)
for support in self.supports:
if (support.supports(game)):
game.set_support(support)
from subprocess import run
from shutil import which
-#from typing import Any, Self
from ..Game import Game
-from os.path import exists, realpath
from subprocess import run
from shutil import which
pass
def supports(self, game: Game):
- return (exists(game.dir + "/start.conf"))
+ return game.dir.joinpath("start.conf").exists()
def start(self, game: Game, params: list[str] = []):
- dir = realpath(game.dir)
- link_dir = realpath(self.get_link_dir(game))
+ dir = game.dir.resolve()
+ link_dir = self.get_link_dir(game).resolve()
if (Support.program("app.sh") != None):
cmd = [
Support.program("app.sh"),
- "--wait", "dosbox", "--link", link_dir
+ "--wait", "dosbox", "--link", link_dir.as_posix()
]
elif (Support.program("dosbox") != None):
cmd = [ Support.program("dosbox") ]
else:
raise RuntimeError("DosBox not found")
- if (exists(dir + "/base.conf")):
+ base = dir.joinpath("base.conf")
+ if (base.exists()):
cmd.append("-conf")
- cmd.append(dir + "/base.conf")
+ cmd.append(base.as_posix())
cmd.append("-conf")
- cmd.append(dir + "/start.conf")
+ cmd.append(dir.joinpath("start.conf").as_posix())
print("Running", game.name)
rep = run(cmd, cwd=dir)
print("\nError when running", game.name, ": RC", rep.returncode)
def get_link_dir(self, game: Game) -> str:
- return (game.dir)
+ return game.dir
from random import random
from math import floor
-from os.path import exists, realpath
from os import environ
from pathlib import Path
from subprocess import run
pass
def supports(self, game: Game):
- return (exists(game.dir + "/gameinfo"))
+ return game.dir.joinpath("gameinfo").exists()
def start(self, game: Game, params: list[str] = []):
- dir = realpath(game.dir)
+ dir = game.dir.resolve()
- if (not exists(dir + "/start.sh")):
+ if (not dir.joinpath("start.sh").exists()):
raise RuntimeError("GoG game without a start.sh script")
env = environ.copy()
ffile = None
if (Support.program("launch.sh") != None):
- ffile = Path(
+ tfile = Path(
"/tmp/shared/.game-"
+ "{0:6d}".format(floor(random() * 1000000))
+ ".sh"
)
- with open(ffile, 'w', encoding="utf-8") as data:
+ with open(tfile, 'w', encoding="utf-8") as data:
data.write("#!/bin/sh\n")
- data.write("cd '" + dir + "'\n")
+ data.write("cd '" + dir.as_posix() + "'\n")
data.write("./start.sh\n")
- ffile.chmod(0o777)
- env["OPTS"] = "--entrypoint=" + ffile.as_posix()
+ tfile.chmod(0o777)
+ env["OPTS"] = "--entrypoint=" + tfile.as_posix()
cmd = [
Support.program("launch.sh"),
- "games", "--link", dir
+ "games", "--link", dir.as_posix()
]
else:
- cmd = [ dir + "/start.sh" ]
-
- if (exists(dir + "/base.conf")):
- cmd.append("-conf")
- cmd.append(dir + "/base.conf")
- cmd.append("-conf")
- cmd.append(dir + "/start.conf")
+ cmd = [ dir.joinpath("start.sh").as_posix() ]
# GoG launcher already does this:
# print("Running", game.name)
rep = run(cmd, cwd=dir, env=env)
- if (ffile != ""):
- ffile.unlink()
if (rep.returncode != 0):
print("\nError when running", game.name, ": RC", rep.returncode)
+
+ if (tfile != ""):
+ tfile.unlink()
+
-from os.path import exists, realpath
+from pathlib import PurePath
from subprocess import run
from shutil import which
# TODO: must be tried befoer SupportDos
def supports(self, game: Game):
- return (exists(game.dir + "/start.conf")
- and exists(game.dir + "/C/WINDOWS"))
+ return (
+ game.dir.joinpath("start.conf").exists()
+ and game.dir.joinpath("C", "WINDOWS").exists()
+ )
- def get_link_dir(self, game: Game) -> str:
+ def get_link_dir(self, game: Game) -> PurePath:
return game.library.dir
#!/usr/bin/env python3
-import argparse
-from os.path import basename
-from sys import stderr
+from argparse import ArgumentParser
+from os.path import basename
+from sys import stderr
from Gamiki import Builder, Library
-parser = argparse.ArgumentParser(
+parser = ArgumentParser(
description="Game launcher and simple management system."
)