aboutsummaryrefslogtreecommitdiff
path: root/soibox
blob: 461489068a951b4c495f7f68cd097259b94eac49 (plain) (blame)
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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
#!/bin/sh

soibox_version="0.1"
config_dir="$HOME/.config/soibox"
store_dir="$HOME/.local/share/soibox"
cache_dir="$HOME/.cache/soibox"

usage()
{
	cat <<- EOF
	Usage: ${0##*/} command [argument] [options]
	A game launcher for the command line
	Commands:
	  -r <game.sb>     Play the game specified in the game profile
	  -h | --help      Show this help cruft
	  --version        Echo the ${0##*/} version
	Options:
	  -i               Ignore global config
	  -l 0-3	   Loglevel, 1 being nothing, 2 being errors, and 3 all
	  -m [1-4]	   Enable performance overlay, 4 being the most verbose
	  -g		   Use feral's gamemode
	  -s		   Use gamescope
	  -d		   Dryrun, shows you errors in config
	  -b		   Use BindToInterface to block wan activity
	  -f [profile]     Use firejail for sandboxing games
	EOF
}

warn()
{
	: "${1:?}"

	printf "\033[33;1m---> warn: %b\033[0m\n" "$*" >&2
}


note()
{
	: "${1:?}"

	printf "\033[32;1m---> %b\033[0m\n" "$*"
}

die()
{
	# usage: die "reason" [exit_status]
	: "${1:?}"

	printf "\033[31;1merr: %b\033[0m\n" "$1"
	exit "${2:-1}"
}

dep_check()
{
	# usage: cmd_check cmd_1 ...
	: "${1:?}"

	for dep; do
		command -v "$dep" 1>/dev/null ||
			die "$dep not found, please install it" 127
	done

	unset dep
}

parse_config()
{
	# usage: parse_config path_to_config
	## TODO: gpu oem specifc conf
	## TODO: vram, ram limitation, give proper reasons when the games fails to run
	## TODO: gpu version specifc conf or allow if available, dlss, rtx, etc..
	## TODO: parse and use proton gamefixes localted at proton_release/protonfixes, possible?
	: "${1:?}"

	olf_ifs="$IFS"

	while IFS="$(printf ' :\t')" read -r key val
	do
		# skip lines containing comments, #
		[ -z "${key##\#*}" ] &&
			continue

		: "${val:?config: can"'"t be empty}"

		case "$key" in
		mangohud)
			: "${mangohud:=$val}"
			;;
		loglevel)
			: "${loglevel:=$val}"
			;;
		banner)
			: "${banner:=$val}"
			;;
		gamemode)
			: "${gamemode:=$val}"
			;;
		gamescope)
			: "${gamescope:=$val}"
			;;
		firejail)
			: "${firejail:=$val}"
			;;
		bind_interface)
			: "${bind_interface:=$val}"
			;;
		wine)
			: "${wine:=$val}"
			;;
		winedll_ovrids)
			winedll_ovrids="${winedll_ovrids:+${winedll_ovrids};}${val}"
			;;
		wine_tag)
			: "${wine_tag:=$val}"
			;;
		wine_prefix)
			: "${wine_prefix:=$val}"
			;;
		dxvk)
			: "${dxvk:=$val}"
			;;
		dxvk_tag)
			: "${dxvk_tag:=$val}"
			;;
		vkd3d)
			: "${vkd3d:=$val}"
			;;
		vkd3d_tag)
			: "${vkd3d_tag:=$val}"
			;;
		groot)
			: "${groot:=$val}"
			;;
		executable)
			: "${executable:=$val}"
			;;
		offline_mode)
			: "${offline_mode:=$val}"
			;;
		*)
			warn "config, $key, invalid option"
			;;
		esac
	done < "$1"

	IFS="$olf_ifs"
	unset old_ifs
}

parse_opts()
{
	if [ "$1" = "--version" ]
	then
		echo "soibox $soibox_version"
		echo "Copyright (c) 2023 Sinan Muhammed"
		exit 0
	elif [ "$1" = "--help" ] || [ "$1" = "-h" ] || [ "$#" -eq 0 ]
	then
		usage
		exit 0
	elif [ -f "$1" ]
	then
		local_config="$(readlink -f "$1")"
		shift
	fi

	while getopts ":r:m:il:gsfdb" f
	do
		case "$f" in
		r)
			local_config="$(readlink -f "$OPTARG")"

			[ -f "$local_config" ] ||
				die "$local_config, no such file"
			;;
		m)
			mangohud="${OPTARG:-on}"
			;;
		i)
			ignore_globalconf=true
			;;
		l)
			loglevel="${OPTARG:?l: loglevel cant be empty}"
			;;
		g)
			gamemode=on
			;;
		s)
			gamescope=on
			;;
		f)
			firejail=on
			firejail_prof="$OPTARG"
			;;
		b)
			bind_interface=on
			;;
		d)
			dry_run=true
			;;
		*)
			usage
			exit 1
			;;
		esac
	done

	[ "$local_config" ] || die "no local config passed"
}

github_dl_release()
{
	# usage: github_dl_release "gh_username/project" [tag]
	: "${1:?}"

	release=${2:+tags/$2}
	: "${release:=latest}"
	release_link=
	release_id=0
	release_gittag="$2"
	latest_id=0
	fetch=
	offline=true

	dep_check curl

	# check github for new releases
	if curl -Is "https://github.com" 1> /dev/null
	then
		fetch="$(curl -s --write-out "%{http_code}" "https://api.github.com/repos/$1/releases/$release")"

		if [ -n "${fetch##*200}" ]
		then
			warn "non 200 http status code, fetch failed for github.com/$1"
		else
			: "${release_gittag:="$(echo "$fetch" | grep -m1 tag_name | cut -d'"' -f4)"}"
			release_id="$(echo "$fetch" | grep -m1 id  | grep -o "[0-9]*")"
			offline=false
		fi
	fi

	# compare github releases with local cache
	for local_id in "$store_dir"/*
	do
		case "${local_id##*/}" in
		*[[:alpha:]])
			# skip over symlinks
			continue
			;;
		"${1##*/}":"$release_gittag"-*)
			dl_path="${local_id}/"
			return 0
			;;
		"${1##*/}"*)
			[ "$release" = "latest" ] && [ "${local_id##*-}" -gt "$latest_id" ] &&
				latest_id="${local_id##*-}"

			if [ -n "${release##tags*}" ] && [ "$offline" = "true" ]
			then
				offline=noprb
				dl_path="${local_id}/"
			fi
			;;
		esac
	done


	# download necessary releases if not available locally
	[ "$offline" = "true" ] &&
		die "can't connect to github.com:443, failed to download ${1##*/}, $release" 101

	if [ "$offline" = "noprb" ]
	then
		[ "$offline_mode" != on ] &&
			warn "offline, you might be running a outdated version of ${1##*/}"
	else
		release_link="$(echo "$fetch" |
			grep -E -m1 -o "https://github.com/$1/releases/download/.*/.*\.tar\.([xg]z|zst)")"

		note "downloading ${1##*/}, ${release##*/}"
		curl -LO "$release_link" --output-dir "${3:-$cache_dir}"

		tar -xaf "${cache_dir}/${release_link##*/}" -C "$store_dir" \
			--one-top-level="${1##*/}:${release_gittag}-${release_id}"  --strip-components 1 ||
			die "failed to extract $cache_dir/${release_link##*/}"

		if [ "$latest_id" -lt "$release_id" ]
		then
			rm "${store_dir}/${1##*/}" 2> /dev/null
			ln -s "${store_dir}/${1##*/}:${release_gittag}-${release_id}" "${store_dir}/${1##*/}"
			dl_path="${store_dir}/${1##*/}/"
		fi
	fi

	unset offline
	unset fetch
	unset latest_id
	unset release
	unset release_id
	unset release_gittag
	unset release_link
}

setup_wine()
{
	# usage: setup_wine "[gh_username/project | system]" [tag]
	: "${1:?}"
	dl_path=

	[ "$loglevel" = 3 ] &&
		note "setting up wine"

	if [ "$1" = "system" ]
	then
		dep_check "wine" "wineboot"
		wine_exe="$(command -v wine)"
		return
	elif [ "$2" = "latest" ]; then
		github_dl_release "$1"
		wine_exe="$(find "$dl_path" -type f -name wine | grep -m1 "bin/wine")"
	else
		github_dl_release "$1" "$2"
		wine_exe="$(find "$dl_path" -type f -name wine | grep -m1 "bin/wine")"
	fi

	[ -n "$WINEPREFIX" ] && ! [ -f "$WINEPREFIX/system.reg" ] &&
		if [ "$loglevel" != 3 ]
		then
			"${wine_exe%wine}wineboot" -u  > /dev/null 2>&1
		else
			"${wine_exe%wine}wineboot" -u
		fi

	unset dl_path
}

setup_dxvk()
{
	# usage: setup_dxvk "gh_username/project" [tag]
	: "${1:?}"
	dl_path=
	dxvk_exe=
	dxvk_sh="https://gist.githubusercontent.com/doitsujin/1652e0e3382f0e0ff611e70142684d01/raw/setup_dxvk.sh"
	export WINE_LARGE_ADDRESS_AWARE=1

	dep_check "wine"

	[ "$loglevel" = 3 ] &&
		note "setting up dxvk"

	if [ -z "$2" ] || [ "$2" = "latest" ]
	then
		github_dl_release "$1"
	else
		github_dl_release "$1" "$2"
	fi

	dxvk_exe="$(find "$dl_path" -type f -name setup_dxvk.sh)"

	# author removed setup_dxvk.sh in v2.1
	if [ -z "$dxvk_exe" ]
	then
		note "downloading dxvk setup"
		curl -LO "$dxvk_sh" --output-dir "$dl_path" ||
			die "failed to download setup_dxvk.sh"
		chmod +x "${dl_path}/setup_dxvk.sh"
		dxvk_exe="${dl_path}/setup_dxvk.sh"
	fi

	if [ "$loglevel" != 3 ]
	then
		"$dxvk_exe" "${dxvk_exe%/*}" install > /dev/null 2>&1
	else
		"$dxvk_exe" "${dxvk_exe%/*}" install
	fi

	unset dxvk_exe
	unset dxvk_sh
	unset dl_path
}

setup_vkd3d()
{
	# usage: setup_vkd3d "gh_username/project" [tag]
	: "${1:?}"
	dl_path=
	vkd3d_exe=

	dep_check "wine"

	[ "$loglevel" = 3 ] &&
		note "setting up vkd3d"

	if [ -z "$2" ] || [ "$2" = "latest" ]
	then
		github_dl_release "$1"
	else
		github_dl_release "$1" "$2"
	fi

	vkd3d_exe="$(find "$dl_path" -type f -name setup_vkd3d_proton.sh)"

	if [ "$loglevel" != 3 ]
	then
		"$vkd3d_exe" install > /dev/null 2>&1
	else
		"$vkd3d_exe" install
	fi

	unset vkd3d_exe
	unset dl_path
}

print_banner()
{
	: "${1:?}"

	if [ "${1%%*.gz}" ]
	then
		cat "$1"
	else
		zcat "$1"
	fi
}

setup_groot()
{
	if [ -z "${1%%*.tar.zst}" ] || [ -z "${1%%*.tar.xz}" ] ||
		[ -z "${1%%*.tar}" ] || [ -z "${1%%*.tar.gz}" ]
	then
		groot="${1%/*}/groot"

		[ -d "$groot" ] &&
			return

		note "extracting ${1##*/}"
		dep_check "tar"

		tar -xaf "$1" -C "${1%/*}" --one-top-level="groot"  --strip-components 1 ||
			die "failed to extract $1"
	elif [ -z "${1%%*.dwarfs}" ]; then
		# TODO: support mounting dwarfs archives
		groot="${1%/*}/groot"

		[ -d "$groot" ] &&
			return

		note "extracting ${1##*/}"
		dep_check "dwarfs"  "dwarfsextract"

		mkdir -p "${1%/*}/groot"
		dwarfsextract -i "$1" -o "${1%/*}/groot" > /dev/null ||
			die "failed to extract $1"
	fi
}

setup_mangohud()
{
        : "${1:?setup_mangohud: empty \$1}"

	MANGOHUD_CONFIG=
	mangohud=on
	dep_check "mangohud"

	case "$1" in
	1)
		MANGOHUD_CONFIG="frame_timing=0,cpu_stats=0,gpu_stats=0,fps=1,fps_only,legacy_layout=0,width=40,frametime=0"
		;;
	2)
		MANGOHUD_CONFIG="battery"
		;;
	3)
		MANGOHUD_CONFIG="cpu_temp,gpu_temp,ram,vram,io_read,io_write,arch,gpu_name,cpu_power,gpu_power,wine,frametime,battery"
		;;
	4)
		MANGOHUD_CONFIG="full,cpu_temp,gpu_temp,ram,vram,io_read,io_write,arch,gpu_name,cpu_power,gpu_power,wine,frametime,battery"
		;;
	on)
		# use the default config
		;;
	*)
		if [ -f "$(absolute_path "$mangohud" "$local_config" )" ]
		then
			MANGOHUD_CONFIGFILE="$(absolute_path "$mangohud" "$local_config" )"
			export MANGOHUD_CONFIGFILE
		else
			warn "config, mangohud, ${1}, invalid option"
			mangohud=off
		fi
		;;
	esac

	[ -n "$MANGOHUD_CONFIG" ] &&
		export MANGOHUD_CONFIG
}

absolute_path()
{
        # usage: absolute_path "path" "local_conf"
        : "${1:?absolute_path: empty \$1}"
        : "${2:?absolute_path: empty \$2}"

	rltv_path=
        abs_path=

	# solve local_config path
	if [ -z "${2##*/*}" ]
	then
		rltv_path="${2%/*}"
	else
		rltv_path="$PWD"
	fi

        [ -z "${1##/*}" ] &&
                abs_path="$1"
        [ -z "${1##\~/*}" ] &&
                abs_path="${HOME}/${1##\~/}"
        [ -z "$abs_path" ] &&
                abs_path="${rltv_path}/${1}"

	echo "$abs_path"
        unset abs_path
	unset rltv_path
}

main()
{
	[ "$(id -u)" -eq 0 ] &&
		die "dont run soibox as root"
	[ -d "$config_dir" ] ||
		mkdir -p "$config_dir"
	[ -d "$store_dir" ] ||
		mkdir -p "$store_dir"
	[ -d "$cache_dir" ] ||
		mkdir -p "$cache_dir"

	# soibox vars
	cmd=
	wine_exe=
	dry_run=
	firejail_prof=

	# user vars
	ignore_globalconf=
	local_config=
	mangohud=
	loglevel=
	banner=
	gamemode=
	gamescope=
	firejail=
	wine=
	wine_tag=
	wine_prefix=
	dxvk=
	dxvk_tag=
	vkd3d=
	vkd3d_tag=
	groot=
	executable=
	offline_mode=
	winedll_ovrids=
	bind_interface=

	# parse input
	parse_opts "$@"
	[ -f "$config_dir/soibox.conf" ] && [ "$ignore_globalconf" != true ]
		parse_config "$config_dir/soibox.conf"
	parse_config "$local_config"
	: "${loglevel:=1}"

	# validate input
	if [ -n "$firejail" ] && [ "$firejail" != on ]
	then
		[ -f "$(absolute_path "$firejail" "$local_config")" ] &&
			firejail_prof="$(absolute_path "$firejail" "$local_config")"
		[ -f "${HOME}/.config/firejail/${firejail}.profile" ] &&
			firejail_prof="${HOME}/.config/firejail/${firejail}.profile"
		[ -f "/etc/firejail/${firejail}.profile" ] &&
			firejail_prof="/etc/firejail/${firejail}.profile"

		if [ -n "$firejail_prof" ]
		then
			firejail=on
		else
			die "unable to locate firejail profile"
		fi
	fi

	if [ "$firejail" = on ] && [ "$bind_interface" = on ]
	then
		warn "disabling bind to interface, not needed when using firejail"
		bind_interface=off
	fi

	if [ "$bind_interface" = on ]
	then
		[ -f "/usr/lib/bindToInterface.so" ] ||
			die "unable to locate /usr/lib/bindToInterface.so"
		export BIND_INTERFACE=lo BIND_EXCLUDE=10.,172.16.,192.168. LD_PRELOAD="/usr/lib/bindToInterface.so"
	fi

	groot="$(absolute_path "$groot" "$local_config")"
	setup_groot "$groot"
	if [ -f "${groot}/${executable}" ]
	then
		cmd="${groot}/${executable}"
	else
		die "unable to locate the game executable"
	fi

	[ -n "$mangohud" ] &&
		setup_mangohud "$mangohud"

	[ "$dry_run" = "true" ] &&
		exit

	# print banner
	[ "$loglevel" != 0 ] && [ -n "$banner" ] &&
		print_banner "$(absolute_path "$banner" "$local_config")"

	WINEPREFIX="${store_dir}/prefix"
	[ -n "$wine_prefix" ] &&
		WINEPREFIX="$(absolute_path "$wine_prefix" "$local_config")"
	export WINEPREFIX

	[ -n "$winedll_ovrids" ] &&
		export WINEDLLOVERRIDES="$winedll_ovrids"
	[ -n "$wine" ] &&
		setup_wine "$wine" "$wine_tag"
	[ -n "$dxvk" ] &&
		setup_dxvk "$dxvk" "$dxvk_tag"
	[ -n "$vkd3d" ] &&
		setup_vkd3d "$vkd3d" "$vkd3d_tag"

	[ -n "$wine" ] &&
		cmd="${wine_exe} ${cmd}"
	[ "$mangohud" = on ] &&
		cmd="mangohud ${cmd}"
	[ "$gamemode" = on ] && dep_check "gamemoderun" &&
		cmd="gamemoderun ${cmd}"
	## gamemode wont work without --ignore=noroot, https://github.com/netblue30/firejail/issues/5035
	if [ "$firejail" = on ] && dep_check "firejail"
	then
		cmd="--whitelist=${WINEPREFIX} --whitelist=${store_dir} --whitelist=${groot} ${cmd}"
		cmd="firejail --net=none --ignore=noroot --profile=${firejail_prof:-steam} ${cmd}"
	fi
	## gamemode wont recognize gamescope, but it's necessary to work with firejail
	## TODO: https://github.com/netblue30/firejail/issues/5587
	[ "$gamescope" = on ] && dep_check "gamescope" &&
		cmd="gamescope -f -- ${cmd}"

	# wait for system wine called by vkd3d and dxvk
	[ "$loglevel" = 3 ] &&
		note "waiting for vkd3d and dxvk"
	while grep --text "$WINEPREFIX" /proc/"$(pgrep wineserver)"/environ > /dev/null 2>&1
	do
		sleep .5
	done

	# start the game
	[ "$loglevel" -le 1 ] &&
		exec > /dev/null 2>&1
	[ "$loglevel" = 2 ] &&
		exec > /dev/null

	[ "$loglevel" = 3 ] &&
		note "starting the game"
	cd "$groot" && eval "$cmd"
}

main "$@"