summarylogtreecommitdiffstats
path: root/rbdoom-3-bfg-launcher
blob: 0e800dfcf255385b1c1271afbf55e60824e676bc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#!/usr/bin/python
# -*- coding: utf-8 -*-

import configparser
import os
import shutil
import subprocess
import sys

from PySide6.QtCore import Qt
from PySide6.QtWidgets import QApplication, QFileDialog, QMessageBox
from xdg import BaseDirectory

exe_name = "RBDoom3BFG"
doom_dir = "/usr/share/games/doom3bfg"

base_name = f"{exe_name.lower()}-launcher"
conf_dir = BaseDirectory.save_config_path(base_name)
data_dir = BaseDirectory.save_data_path(base_name)


def fix_config(parser, path):
    with open(path, "r+") as f:
        contents = f.read()
        f.seek(0, 0)
        f.write(f"[{base_name}]\n" + contents)
    parser.read(path)
    for section in parser.sections():
        for key, val in parser.items(section):
            parser.set(section, key, val.replace('"', ""))


def main() -> int:
    app = QApplication(sys.argv)
    app.setAttribute(Qt.AA_DontUseNativeDialogs, True)
    app.setApplicationName(base_name)
    app.setOrganizationName(base_name)

    mergerfs = shutil.which("mergerfs")
    if not mergerfs:
        QMessageBox.critical(None, f"{base_name}", "<b>mergerfs</b> is not installed.")
        app.exit()
        return 1

    conf_path = os.path.join(conf_dir, "launcher.conf")
    config = configparser.ConfigParser()
    if os.path.exists(conf_path):
        try:
            config.read(conf_path)
        except configparser.MissingSectionHeaderError:
            fix_config(config, conf_path)
            config.read(conf_path)
    else:
        config.add_section(base_name)

    install_dir = config.get(base_name, "install_dir", fallback=None)
    if not install_dir:
        ans = QMessageBox.question(
            None, f"{base_name}",
            f"install_dir is not set in the configuration file \n"
            f"{conf_path}\n"
            f"Without it, the default locations will be searched for game files.\n\n"
            f"Do you want to set it now?\n"
        )
        if ans == QMessageBox.Yes:
            install_dir = QFileDialog().getExistingDirectory(None, base_name)
            config.set(base_name, "install_dir", install_dir)

    with open(conf_path, 'w') as conf_file:
        config.write(conf_file)

    base_path = []
    if install_dir is not None:
        subprocess.run(
            [mergerfs, "-o", f"fsname={exe_name}", f"{doom_dir}:{install_dir}", data_dir]
        )
        base_path = ["+set", "fs_basepath", data_dir]

    subprocess.run(
        [shutil.which(exe_name), "+set", "in_tty", "0", *base_path, *sys.argv]
    )

    if os.path.ismount(data_dir):
        subprocess.run(["umount", data_dir])

    app.exit()
    return 0


if __name__ == '__main__':
    sys.exit(main())