-
Notifications
You must be signed in to change notification settings - Fork 0
/
nxm-handler
145 lines (115 loc) · 2.78 KB
/
nxm-handler
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
#!/bin/bash
# This script tries to find an instance of Mod Organizer 2 running on Wine/Proton,
# then calls it again passing the nxm:// link so it starts downloading.
main() {
local title
local url
local mo2_pid
local mo2_cmdline
local mo2_env_vars
local success
title=$(basename "$0")
url=$1
if [[ -z "$url" ]]; then
message_box error 'No URL provided.'
return 1
fi
mo2_pid=$(find_mo2)
success=$?
if [[ $success -ne 0 ]]; then
message_box error \
"Failed to find Mod Organizer 2, make sure it's running and try again!"
return 1
fi
mo2_env_vars=$(get_env_vars_from_pid "$mo2_pid")
mo2_cmdline=$(
get_cmdline_from_pid "$mo2_pid" | \
sed 's/waitforexitandrun/run/' # Hack to fix Proton.
)
export_env_var "$mo2_env_vars" STEAM_COMPAT_CLIENT_INSTALL_PATH
export_env_var "$mo2_env_vars" STEAM_COMPAT_DATA_PATH
#export_env_var "$mo2_env_vars" WINEPREFIX
eval "WINEPREFIX=${STEAM_COMPAT_DATA_PATH}/pfx $mo2_cmdline '$url'"
}
find_mo2() {
# Finds the first process id that started ModOrganizer.exe, which may be
# Wine or Proton.
local pid
pid=$(pgrep -f ModOrganizer.exe | head -n 1)
if [[ -z "$pid" ]]; then
return 1
fi
echo "$pid"
}
get_cmdline_from_pid() {
# Usage: get_cmdline_from_pid <process_id>
#
# Gets a sanitized command line for a given process id.
# Spaces are escaped by quoting each argument.
sed -E -e 's/.*--//g' < "/proc/$1/cmdline" | \
strings -n 1 | \
while read -r line; do
printf "'%s' " "$line"
done
}
get_env_vars_from_pid() {
# Usage: get_env_vars_from_pid <process_id>
#
# Emits a list of environment variables for a given process id separated by
# newlines.
strings < "/proc/$1/environ"
}
export_env_var() {\
# Usage: export_env_var <variables> <name>
#
# Takes a list of variable assignments split by newlines and exports the one
# that matches a given name.
local var=$2
eval "$var=$(sed -nE -e "s/^$2=(.*)/\1/p" <<< "$1")"
echo "$var=${!var}"
export "${var?}"
}
message_box() {
# Usage: message_box [info | error] <text>
if [[ -z "$2" ]]; then
set -- 'info' "$1"
fi
case "$XDG_CURRENT_DESKTOP" in
'KDE' | 'LXQt')
try_kdialog "$@" || try_zenity "$@" || console_log "$@"
;;
*)
try_zenity "$@" || try_kdialog "$@" || console_log "$@"
;;
esac
}
try_kdialog() {
if ! type kdialog &>/dev/null; then
return 1
fi
read -ra flags < <(get_kdialog_flags "$1")
kdialog --title "$title" "${flags[@]}" "$2"
}
get_kdialog_flags() {
case "$1" in
'error') echo '--error' ;;
*) echo '--msgbox' ;;
esac
}
try_zenity() {
if type zenity &>/dev/null; then
return 1
fi
read -ra flags < <(get_zenity_flags "$1")
zenity --title "$title" "${flags[@]}" "$2"
}
get_zenity_flags() {
case "$1" in
'error') echo '--error --text' ;;
*) echo '--info --text' ;;
esac
}
console_log() {
echo "$1: $2"
}
main "$@"