summarylogtreecommitdiffstats
path: root/rbdoom-3-bfg-launcher
blob: a37db6404b1ba0b786fd1f60d4f7837b34b0ae61 (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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#!/usr/bin/python
# -*- coding: utf-8 -*-

import configparser
import os
import sys

from PySide6.QtCore import Qt, QProcess, QProcessEnvironment, QStandardPaths
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)
mount_dir = os.path.join(data_dir, "data")


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)

    if not os.path.exists(mount_dir):
        os.mkdir(mount_dir)
    elif not os.path.isdir(mount_dir):
        QMessageBox.critical(None, f"{base_name}", f"Path <b>{mount_dir}</b> exists and it is not a directory.")
        app.exit()
        return 1

    mergerfs = QStandardPaths.findExecutable("mergerfs")
    if not mergerfs:
        QMessageBox.critical(None, f"{base_name}", f"<b>{os.path.basename(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 Exception as e:
            sys.stderr.write(str(e))
            config.add_section(base_name)
            # 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.StandardButton.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 = []
    proc = QProcess(app)
    proc.setProcessEnvironment(QProcessEnvironment.systemEnvironment())

    if install_dir:
        proc.setProgram(mergerfs)
        proc.setArguments(["-o", f"fsname={exe_name}", f"{doom_dir}:{install_dir}", mount_dir])
        proc.start()
        proc.waitForFinished(-1)
        if proc.exitStatus() == QProcess.ExitStatus.CrashExit or proc.exitCode() != 0:
            stderr = proc.readAllStandardError().data().decode('utf-8', 'ignore')
            sys.stderr.write(stderr)
            QMessageBox.critical(
                None, f"{base_name}",
                f"<b>{os.path.basename(mergerfs)}</b> failed to execute correctly:\n"
                f"{stderr}"
            )
            proc.deleteLater()
            app.exit()
            return 1
        base_path = ["+set", "fs_basepath", mount_dir]

    proc.setProgram(QStandardPaths.findExecutable(exe_name))
    proc.setArguments(["+set", "in_tty", "0", *base_path, *sys.argv])
    proc.start()
    proc.waitForFinished(-1)
    if proc.exitStatus() == QProcess.ExitStatus.CrashExit or proc.exitCode() != 0:
        sys.stderr.write(proc.readAllStandardOutput().data().decode('utf-8', 'ignore'))

    if os.path.ismount(mount_dir):
        proc.setProgram(QStandardPaths.findExecutable("umount"))
        proc.setArguments([mount_dir])
        proc.start()
        proc.waitForFinished(-1)
        if proc.exitStatus() == QProcess.ExitStatus.CrashExit or proc.exitCode() != 0:
            sys.stderr.write(proc.readAllStandardError().data().decode('utf-8', 'ignore'))

    proc.deleteLater()
    app.exit()
    return 0


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