-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfuzzel-game.py
executable file
·339 lines (303 loc) · 11.5 KB
/
fuzzel-game.py
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
#!/usr/bin/python3 -u
"""
Description: Unified fuzzel game launcher
Author: thnikk
"""
import glob
import os
import sys
import json
import yaml
from subprocess import Popen, PIPE, run
def get_selection(input_list, prompt="") -> str:
""" Get selection from list with custom prompt """
length = str(min(len(input_list), 8))
with Popen(
["fuzzel", "--dmenu", "-l", length, "-p", prompt],
stdin=PIPE, stdout=PIPE, stderr=PIPE
) as fuzzel:
selection = fuzzel.communicate(
input=bytes("\n".join(input_list), 'utf-8'))[0]
if fuzzel.returncode != 0:
sys.exit(1)
return selection.decode().strip()
def run_steam(config) -> dict:
""" Run steam game """
apps = {}
blacklist = ['proton', 'steam']
if "extra" in config:
extra = config["extra"]
else:
extra = []
for path in ["~/.local/share/Steam"] + extra:
for manifest in glob.glob(
os.path.expanduser(
f"{path}/steamapps/appmanifest*.acf")):
with open(manifest, 'r', encoding='utf-8') as file:
name = ""
appid = ""
for line in file:
if "name" in line:
name = line.split('"')[-2]
if "appid" in line:
appid = line.split('"')[-2]
in_blacklist = False
for item in blacklist:
if item in name.lower():
in_blacklist = True
if not in_blacklist and name and appid:
name = f"{name} [steam]"
apps[name] = ["steam", f"steam://rungameid/{appid}"]
return apps
def run_heroic(install_dir) -> dict:
""" Run epic game """
# Generate a list of all executables, get the parent directory, and convert
# to a set and back into a list to remove redundant items.
installed = [
path.split('/')[-2] for path in glob.glob(
os.path.expanduser(f'{install_dir}/*/*.exe'))]
installed = list(set(installed))
games = {}
for library in glob.glob(os.path.expanduser(
"~/.config/heroic/store_cache/*_library.json"
)):
try:
with open(library, 'r', encoding='utf-8') as file:
library = json.load(file)
for game in library['library']:
if game['folder_name'] in installed:
runner = game['runner']
name = f"{game['title']} [{runner}]"
games[name] = \
["xdg-open", f"heroic://launch/{runner}"
f"/{game['app_name']}"]
except KeyError:
pass
return games
def name_from_path(path):
""" Clean game name """
name = path.split('/')[-1].split('.')[0]
return name.split("[")[0].split("(")[0].replace('_', ' ').rstrip()
def run_cemu(rom_dir) -> dict:
""" Run cemu games """
return {
f"{rom} [cemu]": ["cemu", "--game", f"{rom_dir}/{rom}"]
for rom in os.listdir(rom_dir)
if os.path.isdir(f"{rom_dir}/{rom}")
}
def run_switch(game_dir, command):
""" Run yuzu game"""
games = {}
if not os.path.isdir(game_dir):
print("Path does not exist.", file=sys.stderr)
sys.exit(1)
for path in glob.glob(f"{game_dir}/*"):
name = None
game = None
if ".nsp" in path:
name = name_from_path(path)
game = path
else:
try:
path = sorted({
item: os.path.getsize(item)
for item in glob.glob(f"{path}/*")
if not os.path.isdir(item)
})[0]
name = f"{name_from_path(path)} [switch]"
game = path
except IndexError:
pass
if name and game:
games[name] = command + [game]
return games
def run_rpcs3(game_dir):
games = {}
for path in glob.glob(f"{game_dir}/*"):
if os.path.isdir(path):
name = f"{path.split('/')[-1]} [rpcs3]"
games[name] = ["rpcs3", "--no-gui", "--fullscreen", path]
return games
def run_pcsx2(game_dir):
games = {}
for path in glob.glob(f"{game_dir}/*"):
if os.path.isdir(path):
name = f"{path.split('/')[-1]} [pcsx2]"
games[name] = ["pcsx2", "-fullscreen", "-bigpicture", path]
elif 'iso' in path:
name = path.split('/')[-1].split('.')[0].split('(')[0].strip()
name = f"{name} [pcsx2]"
games[name] = ["pcsx2", "-fullscreen", "-bigpicture", path]
return games
def run_retroarch(game_dir, cores) -> dict:
""" Run game with retroarch """
games = {}
for path in glob.glob(f"{os.path.expanduser(game_dir)}/*/*.*"):
ext = path.split('.')[-1]
# Extension blacklist
if ext in ["txt", "sav"]:
continue
core = path.split('/')[-2]
if core in list(cores):
name = f"{name_from_path(path)} [{core}]"
games[name] = [
# 'pygame', '-mgo',
'retroarch', '-f',
'-L', cores[core],
path
]
return games
def run_bottles(config):
if 'flatpak' in config and config['flatpak']:
command = [
'flatpak', 'run', '--command=bottles-cli',
'com.usebottles.bottles']
path = os.path.expanduser(
'~/.var/app/com.usebottles.bottles/data/bottles')
else:
command = ['bottles-cli', 'run']
path = os.path.expanduser('~/.local/share/bottles')
if 'pre-command' in config and config['pre-command']:
pre_command = config['pre-command']
else:
pre_command = []
with open(f'{path}/bottles/{config["bottle"]}/bottle.yml', 'r') as file:
try:
return {
f"{info['name']} [bottles-{config['bottle']}]":
pre_command + command +
['run', '-b', config['bottle'], '-p', info['name']]
for id, info in
yaml.safe_load(file)['External_Programs'].items()
}
except yaml.YAMLError:
return None
def sort_dict(dictionary) -> dict:
""" Sort dictionary based on values """
return dict(sorted(dictionary.items(), key=lambda x: x[1], reverse=True))
def get_frequent(cache_file) -> list:
""" Get ordered list """
try:
with open(cache_file, 'r', encoding='utf-8') as file:
cache = json.load(file)
except (FileNotFoundError, json.decoder.JSONDecodeError):
cache = []
unique = set(cache)
counted = {item: cache.count(item) for item in unique}
return list(sort_dict(counted))
def sort_some(full, some) -> list:
""" Move active VMs to top of list """
for item in some:
try:
full.remove(item)
except ValueError:
some.remove(item)
return some + full
def main() -> None:
""" Load launcher from config """
config_path = os.path.expanduser("~/.config/fuzzel/fuzzel-game.json")
try:
with open(config_path, 'r', encoding='utf-8') as config_file:
config = json.load(config_file)
except FileNotFoundError:
config = {
"steam": {"enable": True},
"heroic": {"enable": True, "path": "~/Games/Heroic"},
"switch": {
"enable": False,
"path": "/mnt/server2/Games/NSP",
"command": ["yuzu", "-f", "-g"]
},
"rpcs3": {"enable": False, "path": "/mnt/server2/Games/PS3"},
"pcsx2": {"enable": False, "path": "/mnt/server2/Games/PS2"},
"retroarch": {
"enable": True,
"cores": {
"wii": "/usr/lib/libretro/dolphin_libretro.so",
"gcn": "/usr/lib/libretro/dolphin_libretro.so",
"snes": "/usr/lib/libretro/snes9x_libretro.so"
},
"path": "~/Games/ROMs"
},
"custom": {
"enable": True,
"games": {
"Genshin Impact": ["an-anime-game-launcher", "--run-game"],
"Honkai Star Rail": [
"the-honkers-railway-launcher", "--run-game"],
"Minecraft": ["prismlauncher", "--launch", "1.20.4(1)"]
}
}
}
with open(config_path, 'w', encoding='utf-8') as config_file:
config_file.write(json.dumps(config, indent=4))
with Popen(
[
"fuzzel", "--dmenu", "-l", "0", "-p",
"Config created, please "
"edit ~/.config/fuzzel/fuzzel-game.json"]):
pass
sys.exit(1)
games = {}
for launcher, settings in config.items():
if settings["enable"]:
try:
match launcher.lower():
case "steam":
games.update(run_steam(config[launcher]))
case "heroic":
games.update(run_heroic(config[launcher]['path']))
case "yuzu":
games.update(run_switch(config[launcher]['path'],
["yuzu", "-f", "-g"]))
case "switch":
games.update(run_switch(config[launcher]['path'],
config[launcher]['command']))
case "rpcs3":
games.update(run_rpcs3(config[launcher]['path']))
case "pcsx2":
games.update(run_pcsx2(config[launcher]['path']))
case "retroarch":
games.update(
run_retroarch(config[launcher]['path'],
config[launcher]['cores']))
case "cemu":
games.update(
run_cemu(config[launcher]['path']))
case "bottles":
games.update(
run_bottles(config[launcher]))
case "custom":
games.update({
f"{item} [custom]": command for item, command in
config[launcher]['games'].items()})
case _:
pass
except KeyError as error:
print(f'Skipping {launcher} due to a configuration error:')
print(f'{type(error).__name__}: {error}', file=sys.stderr)
continue
cache_file = os.path.expanduser('~/.cache/fuzzel-game.json')
frequent = get_frequent(cache_file)
games_list = sort_some(list(games), frequent)
selection = get_selection(games_list)
try:
with open(cache_file, 'r+', encoding='utf-8') as file:
cache = json.load(file)
except (FileNotFoundError, json.decoder.JSONDecodeError):
cache = []
cache = ([selection] + cache)[:20]
with open(cache_file, 'w', encoding='utf-8') as file:
file.write(json.dumps(cache))
print(games[selection])
# Change cwd if a path to an executable is given
if '/' in games[selection][0]:
cwd = '/'.join(games[selection][0].split('/')[:-1])
print(cwd)
with Popen(games[selection], cwd=cwd):
pass
else:
with Popen(games[selection]):
pass
if __name__ == "__main__":
main()