forked from Tau5/Co-op-on-Linux
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreate-new-profile.sh
executable file
·203 lines (169 loc) · 6.66 KB
/
create-new-profile.sh
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
#!/usr/bin/env bash
# Enhanced script for setting up co-op gaming profiles with robust validation
# Default dialog handler
DIALOG="zenity"
DIR_CO_OP="$PWD"
MAIN_SCRIPT="Co-Op-On-Linux.sh"
# Function to handle errors
error_exit() {
$DIALOG --error --text="$1"
exit 1
}
# Verify main script exists
verify_main_script() {
if [ ! -f "$DIR_CO_OP/$MAIN_SCRIPT" ]; then
error_exit "Main script $MAIN_SCRIPT not found in:\n$DIR_CO_OP"
fi
}
# Validate positive integer
validate_positive_int() {
[[ "$1" =~ ^[1-9][0-9]*$ ]] || error_exit "Invalid number: $1 must be a positive integer"
}
# Validate resolution format
validate_resolution() {
local res=$1
local width=${res%x*}
local height=${res#*x}
[[ "$res" =~ ^[0-9]+x[0-9]+$ ]] || error_exit "Invalid resolution format: $res\nUse WIDTHxHEIGHT (e.g., 1280x720)"
validate_positive_int "$width"
validate_positive_int "$height"
}
# File selection dialog
select_file() {
local path
if [ "$DIALOG" = "kdialog" ]; then
path=$(kdialog --title="$1" --getopenfilename) || error_exit "No file selected"
else
path=$(zenity --title="$1" --file-selection) || error_exit "No file selected"
fi
echo "$path"
}
# Directory selection dialog
select_directory() {
local path
if [ "$DIALOG" = "kdialog" ]; then
path=$(kdialog --title="$1" --getexistingdirectory) || error_exit "No directory selected"
else
path=$(zenity --title="$1" --file-selection --directory) || error_exit "No directory selected"
fi
echo "$path"
}
# Check for required dialog programs
if ! command -v zenity >/dev/null 2>&1 && ! command -v kdialog >/dev/null 2>&1; then
error_exit "This script requires zenity or kdialog for graphical interface"
fi
# Verify main script exists before proceeding
verify_main_script
# Game executable selection
GAMERUN=$(select_file "Select game executable/launch script") || exit 1
[ -x "$GAMERUN" ] || error_exit "Selected file is not executable:\n$GAMERUN"
# Get default resolution
if command -v xdpyinfo >/dev/null 2>&1; then
DEFAULT_RES=$(xdpyinfo | awk '/dimensions/{print $2}')
else
DEFAULT_RES="1920x1080"
fi
# Window mode selection
MULTIWINDOW=$($DIALOG --title="Window Mode Selection" --list --radiolist \
--column "Pick" --column "Option" \
TRUE "Splitscreen Window" \
FALSE "Separate Windows") || error_exit "Window mode not selected"
declare -a RESOLUTIONS
if [ "$MULTIWINDOW" = "Separate Windows" ]; then
# Get number of windows
NUM_WINDOWS=$($DIALOG --title="Number of Windows" --entry \
--text="Enter total number of windows (e.g., 2)" \
--entry-text=2) || error_exit "Number of windows not specified"
validate_positive_int "$NUM_WINDOWS"
# Collect resolutions for each window
for ((i=1; i<=NUM_WINDOWS; i++)); do
RESOLUTION=$($DIALOG --title="Player $i Resolution" \
--entry --text="Enter resolution (WIDTHxHEIGHT)" \
--entry-text="$DEFAULT_RES") || error_exit "Resolution not specified"
validate_resolution "$RESOLUTION"
RESOLUTIONS+=("$RESOLUTION")
done
else
# Single splitscreen resolution
RESOLUTION=$($DIALOG --title="Screen Resolution" \
--entry --text="Enter splitscreen resolution (WIDTHxHEIGHT)" \
--entry-text="$DEFAULT_RES") || error_exit "Resolution not specified"
validate_resolution "$RESOLUTION"
RESOLUTIONS+=("$RESOLUTION")
fi
# Proton handling for Windows executables
if [[ "${GAMERUN,,}" == *.exe ]]; then
# Find Proton versions
declare -a PROTON_PATHS
while IFS= read -r -d $'\0' path; do
PROTON_PATHS+=("$path")
done < <(find "$HOME/.steam/steam/steamapps/common" "/usr/share/steam" \
-type d -name 'Proton*' -print0 2>/dev/null)
if [ ${#PROTON_PATHS[@]} -eq 0 ]; then
PROTON_PATH=$(select_directory "Select Proton Installation Directory") || exit 1
PROTON_VERSION=$(basename "$PROTON_PATH")
else
# Prepare dialog list
declare -a dialog_items
for index in "${!PROTON_PATHS[@]}"; do
dialog_items+=("$index" "$(basename "${PROTON_PATHS[$index]}")")
done
# Show selection dialog
if [ "$DIALOG" = "kdialog" ]; then
selected_index=$(kdialog --title="Select Proton Version" \
--menu "Available Proton versions:" "${dialog_items[@]}") || exit 1
else
IFS='|' read -ra selected <<< "$(zenity --list --title="Select Proton Version" \
--radiolist --column="Pick" --column="Index" --column="Version" \
$(for index in "${!PROTON_PATHS[@]}"; do printf "FALSE %s %s " "$index" "$(basename "${PROTON_PATHS[$index]}")"; done) \
--hide-column=2 --print-column=2)"
selected_index="${selected[0]}"
fi
[[ -n "$selected_index" ]] || error_exit "No Proton version selected"
PROTON_PATH="${PROTON_PATHS[$selected_index]}"
PROTON_VERSION=$(basename "$PROTON_PATH")
fi
else
PROTON_PATH=""
PROTON_VERSION="None"
fi
# Profile name validation
name=$($DIALOG --title="Profile Name" --entry \
--text="Enter profile name (letters, numbers, underscores/hyphens)" \
--entry-text="profile") || error_exit "Profile name not specified"
[[ "$name" =~ ^[a-zA-Z0-9_-]+$ ]] || error_exit "Invalid profile name:\nUse only A-Z, 0-9, _ and -"
# Create profile directory
PROFILE_DIR="$DIR_CO_OP/profiles"
mkdir -p "$PROFILE_DIR" || error_exit "Failed to create profiles directory"
# Generate profile script
PROFILE_FILE="$PROFILE_DIR/$name.sh"
cat > "$PROFILE_FILE" << EOF
#!/bin/bash
# Auto-generated co-op profile - $name
# Game configuration
export GAMERUN="$GAMERUN"
export MULTIWINDOW="$([ "$MULTIWINDOW" = "Separate Windows" ] && echo "1" || echo "0")"
# Resolution settings
EOF
if [ "$MULTIWINDOW" = "Separate Windows" ]; then
echo "export NUM_WINDOWS=$NUM_WINDOWS" >> "$PROFILE_FILE"
for ((i=0; i<NUM_WINDOWS; i++)); do
IFS="x" read -r width height <<< "${RESOLUTIONS[$i]}"
echo "export WIDTH$((i+1))=$width" >> "$PROFILE_FILE"
echo "export HEIGHT$((i+1))=$height" >> "$PROFILE_FILE"
done
else
IFS="x" read -r width height <<< "${RESOLUTIONS[0]}"
echo "export WIDTH=$width" >> "$PROFILE_FILE"
echo "export HEIGHT=$height" >> "$PROFILE_FILE"
fi
cat >> "$PROFILE_FILE" << EOF
# Proton configuration
export PROTON_VERSION="$PROTON_VERSION"
export PROTON_PATH="$PROTON_PATH"
# Launch main co-op script
"$DIR_CO_OP/$MAIN_SCRIPT"
EOF
chmod +x "$PROFILE_FILE" || error_exit "Failed to make profile executable"
# Success message
$DIALOG --info --text="Profile '$name' created successfully!\n\nLocation: $PROFILE_FILE\n\nTo use: Execute this profile script"