2022-05-09 13:42:24 +02:00
#!/bin/bash
2022-06-04 22:29:30 +02:00
set -o pipefail
2022-07-31 20:11:27 +02:00
version = 2.4.0-rc.5
2022-05-09 13:42:24 +02:00
aid = 221100
game = "dayz"
2022-07-17 18:06:41 +02:00
workshop = "steam://url/CommunityFilePage/"
2022-05-09 13:42:24 +02:00
api = "https://api.battlemetrics.com/servers"
2022-05-22 21:20:34 +02:00
sd_res = "--width=1280 --height=800"
config_path = " $HOME /.config/dztui/ "
config_file = " ${ config_path } dztuirc "
tmp = /tmp/dztui.tmp
2022-05-29 07:41:56 +02:00
separator = "%%"
2022-07-24 15:20:06 +02:00
git_url = "https://github.com/aclist/dztui/issues"
2022-07-18 10:58:38 +02:00
stable_url = "https://raw.githubusercontent.com/aclist/dztui/dzgui/dzgui.sh"
testing_url = "https://raw.githubusercontent.com/aclist/dztui/testing/dzgui.sh"
2022-06-20 04:26:13 +02:00
help_url = "https://aclist.github.io/dzgui/dzgui"
2022-06-15 12:32:10 +02:00
check_config_msg = "Check config values and restart."
2022-07-24 15:20:06 +02:00
news_url = "https://raw.githubusercontent.com/aclist/dztui/dzgui/news"
2022-05-09 13:42:24 +02:00
2022-07-24 15:20:06 +02:00
update_last_seen( ) {
mv $config_file ${ config_path } dztuirc.old
nr = $( awk '/seen_news=/ {print NR}' ${ config_path } dztuirc.old)
seen_news = " seen_news=\" $sum \" "
awk -v " var= $seen_news " -v " nr= $nr " 'NR==nr {$0=var}{print}' ${ config_path } dztuirc.old > $config_file
printf " [DZGUI] Updated last seen news item to ' $sum '\n "
source $config_file
}
check_news( ) {
result = $( curl -Ls " $news_url " )
sum = $( echo -n " $result " | md5sum | awk '{print $1}' )
}
print_news( ) {
check_news
if [ [ $sum = = $seen_news || -z $result ] ] ; then
hchar = ""
news = ""
else
hchar = "─"
news = " $result \n $( awk -v var = " $hchar " 'BEGIN{for(c=0;c<90;c++) printf var;}' ) \n "
update_last_seen
fi
}
2022-07-17 18:06:41 +02:00
#TODO: prevent connecting to offline servers
#TODO: abstract zenity title params
2022-05-09 13:42:24 +02:00
declare -A deps
2022-07-24 15:20:06 +02:00
deps = ( [ awk] = "5.1.1" [ curl] = "7.80.0" [ jq] = "1.6" [ tr] = "9.0" [ zenity] = "3.42.1" [ steam] = "1.0.0" )
2022-06-12 06:44:30 +02:00
changelog( ) {
2022-07-24 15:20:06 +02:00
if [ [ $branch = = "stable" ] ] ; then
md = "https://raw.githubusercontent.com/aclist/dztui/dzgui/changelog.md"
else
md = "https://raw.githubusercontent.com/aclist/dztui/testing/changelog.md"
fi
2022-06-12 06:44:30 +02:00
prefix = "This window can be scrolled."
echo $prefix
echo ""
2022-06-12 09:17:23 +02:00
curl -Ls " $md " | awk '/Unreleased/ {flag=1}flag'
2022-06-12 06:44:30 +02:00
}
2022-05-09 13:42:24 +02:00
depcheck( ) {
for dep in " ${ !deps[@] } " ; do
2022-07-24 15:20:06 +02:00
command -v $dep 2>& 1>/dev/null || ( printf "Requires %s >=%s\n" $dep ${ deps [ $dep ] } ; exit 1)
2022-05-09 13:42:24 +02:00
done
}
2022-06-22 15:33:52 +02:00
init_items( ) {
2022-07-17 18:06:41 +02:00
#array order determines menu selector; this is destructive
2022-05-22 21:20:34 +02:00
items = (
"Launch server list"
2022-06-04 22:29:30 +02:00
"Quick connect to favorite server"
"Add server by ID"
2022-06-15 12:32:10 +02:00
"Add favorite server"
2022-07-24 15:20:06 +02:00
"Delete server"
2022-06-15 12:32:10 +02:00
"List installed mods"
2022-06-22 15:33:52 +02:00
"Toggle debug mode"
2022-06-15 12:32:10 +02:00
"Report bug (opens in browser)"
"Help file (opens in browser)"
"View changelog"
2022-05-22 21:20:34 +02:00
)
2022-06-22 15:33:52 +02:00
}
2022-07-17 18:06:41 +02:00
exit_and_cleanup( ) {
#TODO: this is currently unused
rm $tmp
rm $link_file
}
2022-06-04 22:29:30 +02:00
warn_and_exit( ) {
zenity --info --title= "DZGUI" --text= " $1 " --icon-name= "dialog-warning" 2>/dev/null
2022-06-15 12:32:10 +02:00
printf "[DZGUI] %s\n" " $check_config_msg "
2022-06-04 22:29:30 +02:00
exit
}
2022-05-22 21:20:34 +02:00
warn( ) {
zenity --info --title= "DZGUI" --text= " $1 " --icon-name= "dialog-warning" 2>/dev/null
2022-05-09 13:42:24 +02:00
}
2022-05-22 21:20:34 +02:00
info( ) {
2022-06-15 12:32:10 +02:00
zenity --info --title= "DZGUI" --text= " $1 " 2>/dev/null
2022-05-22 21:20:34 +02:00
}
2022-05-09 13:42:24 +02:00
query_api( ) {
2022-06-15 12:32:10 +02:00
#TODO: prevent drawing list if null values returned without API error
if [ [ $one_shot_launch -eq 1 ] ] ; then
list_of_ids = " $fav "
else
if [ [ -n $fav ] ] ; then
list_of_ids = " $whitelist , $fav "
else
list_of_ids = " $whitelist "
fi
fi
2022-05-22 21:20:34 +02:00
response = $( curl -s " $api " -H "Authorization: Bearer " $api_key "" -G -d "sort=-players" \
2022-06-15 12:32:10 +02:00
-d " filter[game]= $game " -d " filter[ids][whitelist]= $list_of_ids " )
2022-05-09 13:42:24 +02:00
if [ [ " $( jq -r 'keys[]' <<< " $response " ) " = = "errors" ] ] ; then
code = $( jq -r '.errors[] .status' <<< $response )
#TODO: fix granular api codes
2022-06-15 12:32:10 +02:00
if [ [ $code -eq 401 ] ] ; then
echo " $code " >> outfile
warn_and_exit " Error $code : malformed API key "
elif [ [ $code -eq 500 ] ] ; then
warn_and_exit " Error $code : malformed server list "
fi
fi
if [ [ -z $( echo $response | jq '.data[]' ) ] ] ; then
warn_and_exit "API returned empty response. Check config file."
2022-05-09 13:42:24 +02:00
fi
2022-05-22 21:20:34 +02:00
}
write_config( ) {
2022-06-15 12:32:10 +02:00
cat <<-END
#Path to DayZ installation
steam_path = " $steam_path "
2022-05-22 21:20:34 +02:00
#Your unique API key
2022-06-15 12:32:10 +02:00
api_key = " $api_key "
2022-05-22 21:20:34 +02:00
#Comma-separated list of server IDs
2022-06-15 12:32:10 +02:00
whitelist = " $whitelist "
2022-05-22 21:20:34 +02:00
#Favorite server to fast-connect to (limit one)
2022-06-15 12:32:10 +02:00
fav = " $fav "
2022-05-22 21:20:34 +02:00
#Custom player name (optional, required by some servers)
2022-06-15 12:32:10 +02:00
name = " $name "
2022-05-22 21:20:34 +02:00
#Set to 1 to perform dry-run and print launch options
2022-07-31 20:01:58 +02:00
debug = " $debug "
2022-07-18 10:58:38 +02:00
#Toggle stable/testing branch
2022-07-31 19:52:59 +02:00
branch = " $branch "
2022-07-24 15:20:06 +02:00
#Last seen news item
2022-07-31 20:01:58 +02:00
seen_news = " $seen_news "
2022-05-22 21:20:34 +02:00
END
}
2022-06-15 12:32:10 +02:00
guess_path( ) {
if [ [ $is_steam_deck -eq 1 ] ] ; then
steam_path = "/home/deck/.local/share/Steam"
else
echo "# Checking for default DayZ path"
path = $( find $HOME -path "*.local/share/Steam/steamapps/common/DayZ" | wc -c)
if [ [ ! $path -eq 0 ] ] ; then
2022-06-17 02:15:39 +02:00
steam_path = " $HOME /.local/share/Steam "
2022-06-15 12:32:10 +02:00
else
echo "# Searching for alternate DayZ path"
path = $( find / -path "*/steamapps/common/DayZ" 2>/dev/null)
if [ [ $( echo " $path " | wc -l) -gt 1 ] ] ; then
path_sel = $( echo -e " $path " | zenity --list --title= "DZGUI" --text= "Multiple paths found. Select correct DayZ path" --column= "Paths" --width 1200 --height 800)
clean_path = $( echo -e " $path_sel " | awk -F"/steamapps" '{print $1}' )
steam_path = " $clean_path "
elif [ [ ! $( echo $path | wc -c) -eq 0 ] ] ; then
clean_path = $( echo -e " $path " | awk -F"/steamapps" '{print $1}' )
steam_path = " $clean_path "
else
steam_path = ""
fi
fi
fi
echo " [DZGUI] Set Steam path to $steam_path "
}
2022-05-22 21:20:34 +02:00
create_config( ) {
2022-06-15 12:32:10 +02:00
player_input = " $( zenity --forms --add-entry= "Player name (required for some servers)" --add-entry= "API key" --add-entry= "Server 1 (you can add more later)" --title= DZGUI --text= DZGUI --add-entry= "Server 2" --add-entry= "Server 3" --add-entry= "Server 4" $sd_res --separator= "│" ) "
name = $( echo " $player_input " | awk -F│ '{print $1}' )
api_key = $( echo " $player_input " | awk -F│ '{print $2}' )
whitelist = $( echo " $player_input " | awk -F"│" '{OFS=","}{print $3,$4,$5}' | sed 's/,*$//g' | sed 's/^,*//g' )
guess_path > >( zenity --progress --auto-close --pulsate)
2022-05-22 21:20:34 +02:00
mkdir -p $config_path ; write_config > $config_file
2022-06-15 12:32:10 +02:00
info " Config file created at $config_file . "
2022-05-22 21:20:34 +02:00
}
err( ) {
printf "[ERROR] %s\n" " $1 "
}
varcheck( ) {
[ [ -z $api_key ] ] && ( err "Error in key: 'api_key'" )
[ [ -z $whitelist ] ] && ( err "Error in key: 'whitelist'" )
[ [ ! -d $game_dir ] ] && ( err "Malformed game path" )
[ [ $whitelist = ~ [ [ :space:] ] ] ] && ( err "Separate whitelist values with commas" )
}
2022-06-15 12:32:10 +02:00
run_depcheck( ) {
2022-05-22 21:20:34 +02:00
if [ [ -z $( depcheck) ] ] ; then
:
else
2022-07-24 15:20:06 +02:00
zenity --warning --ok-label= "Exit" --title= "DZGUI" --text= " $( depcheck) "
2022-06-04 22:29:30 +02:00
exit
2022-05-22 21:20:34 +02:00
fi
2022-06-15 12:32:10 +02:00
}
run_varcheck( ) {
source $config_file
workshop_dir = " $steam_path /steamapps/workshop/content/ $aid "
game_dir = " $steam_path /steamapps/common/DayZ "
2022-05-22 21:20:34 +02:00
if [ [ -z $( varcheck) ] ] ; then
:
else
2022-06-15 12:32:10 +02:00
zenity --warning --ok-label= "Exit" --text= " $( varcheck) " 2>/dev/null
printf "[DZGUI] %s\n" " $check_config_msg "
2022-06-04 22:29:30 +02:00
exit
2022-05-22 21:20:34 +02:00
fi
}
config( ) {
if [ [ ! -f $config_file ] ] ; then
2022-06-04 22:29:30 +02:00
zenity --question --cancel-label= "Exit" --text= "Config file not found. Should DZGUI create one for you?" 2>/dev/null
2022-05-22 21:20:34 +02:00
code = $?
if [ [ $code -eq 1 ] ] ; then
exit
else
create_config
fi
else
source $config_file
fi
2022-06-15 12:32:10 +02:00
}
open_mod_links( ) {
link_file = $( mktemp)
echo "<html>" > $link_file
echo "<title>DZGUI</title>" >> $link_file
echo "<h1>DZGUI</h1>" >> $link_file
echo "<p>Open these links and subscribe to them on the Steam Workshop, then continue with the application prompts.<br><b>Note:</b> it may take some time for mods to synchronize before DZGUI can see them.<br>It can help to have Steam in an adjacent window so that you can see the downloads completing.</p>" >> $link_file
2022-07-18 10:58:38 +02:00
n = 1
for i in $diff ; do
echo " $n . <a href=\" ${ workshop } $i \"> ${ workshop } $i </a><br> "
let n++
2022-06-15 12:32:10 +02:00
done >> $link_file
echo "</html>" >> $link_file
2022-07-17 18:06:41 +02:00
browser " $link_file " &
2022-06-15 12:32:10 +02:00
2022-05-22 21:20:34 +02:00
}
2022-07-24 15:20:06 +02:00
steam_deck_mods( ) {
until [ [ -z $diff ] ] ; do
next = $( echo -e " $diff " | head -n1)
zenity --question --ok-label= "Open" --cancel-label= "Cancel" --title= "DZGUI" --text= " Missing mods. Click [Open] to open mod $next in Steam Workshop and subscribe to it by clicking the green Subscribe button. After the mod is downloaded, return to this menu to continue validation. " 2>/dev/null
rc = $?
if [ [ $rc -eq 0 ] ] ; then
echo " [DZGUI] Opening ${ workshop } $next "
steam steam://url/CommunityFilePage/$next 2>/dev/null &
zenity --info --title= "DZGUI" --ok-label= "Next" --text= "Click [Next] to continue mod check." 2>/dev/null
else
return
fi
compare
done
}
2022-06-04 22:29:30 +02:00
manual_mod_install( ) {
l = 0
2022-06-15 12:32:10 +02:00
if [ [ $is_steam_deck -eq 0 ] ] ; then
open_mod_links
until [ [ -z $diff ] ] ; do
zenity --question --title= "DZGUI" --ok-label= "Next" --cancel-label= "Cancel" --text= "Opened mod links in browser. Click [Next] when all mods have been subscribed to. This dialog may reappear if clicking [Next] too soon before mods are synchronized in the background." 2>/dev/null
rc = $?
if [ [ $rc -eq 0 ] ] ; then
compare
2022-06-16 05:24:10 +02:00
open_mod_links
2022-06-15 12:32:10 +02:00
else
return
fi
done
else
2022-07-24 15:20:06 +02:00
steam_deck_mods
2022-06-15 12:32:10 +02:00
fi
2022-06-04 22:29:30 +02:00
passed_mod_check
}
2022-07-24 15:20:06 +02:00
encode( ) {
echo " $1 " | awk '{printf("%c",$1)}' | base64 | sed 's/\//_/g; s/=//g; s/+/]/g'
}
stale_symlinks( ) {
for l in $( find " $game_dir " -xtype l) ; do
unlink $l
done
}
legacy_symlinks( ) {
for d in " $game_dir " /*; do
if [ [ $d = ~ @[ 0-9] +-.+ ] ] ; then
unlink " $d "
fi
done
}
2022-06-04 22:29:30 +02:00
symlinks( ) {
for d in " $workshop_dir " /*; do
id = $( awk -F"= " '/publishedid/ {print $2}' " $d " /meta.cpp | awk -F\; '{print $1}' )
2022-07-24 15:20:06 +02:00
encoded_id = $( encode " $id " )
2022-06-04 22:29:30 +02:00
mod = $( awk -F\" '/name/ {print $2}' " $d " /meta.cpp | sed -E 's/[^[:alpha:]0-9]+/_/g; s/^_|_$//g' )
2022-07-24 15:20:06 +02:00
link = " @ $encoded_id "
2022-06-04 22:29:30 +02:00
if [ [ -h " $game_dir / $link " ] ] ; then
2022-07-24 15:20:06 +02:00
:
else
printf " [DZGUI] Creating symlink for $mod \n "
2022-06-04 22:29:30 +02:00
ln -fs " $d " " $game_dir / $link "
2022-07-24 15:20:06 +02:00
fi
2022-07-17 18:06:41 +02:00
done
2022-06-04 22:29:30 +02:00
}
passed_mod_check( ) {
echo "[DZGUI] Passed mod check"
2022-07-24 15:20:06 +02:00
stale_symlinks
legacy_symlinks
2022-06-04 22:29:30 +02:00
symlinks
launch
2022-05-22 21:20:34 +02:00
}
2022-06-04 22:29:30 +02:00
connect( ) {
#TODO: sanitize/validate input
2022-07-18 10:58:38 +02:00
readarray -t qport_arr <<< " $qport_list "
if [ [ -z ${ qport_arr [@] } ] ] ; then
err "Failed to process favorite server"
return
fi
2022-06-04 22:29:30 +02:00
ip = $( echo " $1 " | awk -F" $separator " '{print $1}' )
2022-07-18 10:58:38 +02:00
#TODO: deprecated (for now)
2022-06-04 22:29:30 +02:00
bid = $( echo " $1 " | awk -F" $separator " '{print $2}' )
2022-07-17 18:06:41 +02:00
fetch_mods_sa " $ip "
2022-06-04 22:29:30 +02:00
validate_mods
rc = $?
[ [ $rc -eq 1 ] ] && return
compare
if [ [ -n $diff ] ] ; then
manual_mod_install
else
passed_mod_check
fi
}
2022-07-17 18:06:41 +02:00
fetch_mods_sa( ) {
sa_ip = $( echo $ip | awk -F: '{print $1}' )
for i in ${ qport_arr [@] } ; do
2022-07-18 10:58:38 +02:00
if [ [ -n $( echo " $i " | awk -v ip = $ip '$0 ~ ip' ) ] ] ; then
sa_port = $( echo $i | awk -v ip = $ip -F$separator '$0 ~ ip {print $2}' )
2022-07-17 18:06:41 +02:00
fi
done
echo " [DZGUI] Querying modlist on ${ sa_ip } : ${ sa_port } "
remote_mods = $( curl -Ls " https://dayzsalauncher.com/api/v1/query/ $sa_ip / $sa_port " | jq -r '.result.mods[].steamWorkshopId' )
2022-07-18 10:58:38 +02:00
rc = $?
if [ [ $rc -eq 1 ] ] ; then
err "Failed to fetch modlist"
return
fi
2022-07-17 18:06:41 +02:00
}
2022-05-22 21:20:34 +02:00
fetch_mods( ) {
2022-07-17 18:06:41 +02:00
#TODO: broken upstream
2022-05-22 21:20:34 +02:00
remote_mods = $( curl -s " $api " -H "Authorization: Bearer " $api_key "" -G -d filter[ ids] [ whitelist] = " $1 " -d "sort=-players" \
| jq -r '.data[] .attributes .details .modIds[]' )
2022-05-09 13:42:24 +02:00
}
2022-06-04 22:29:30 +02:00
check_workshop( ) {
2022-07-17 18:06:41 +02:00
#TODO: legacy method is defunct
2022-06-04 22:29:30 +02:00
curl -Ls " $url ${ modlist [ $i ] } " | grep data-appid | awk -F\" '{print $8}'
}
2022-06-12 06:44:30 +02:00
query_defunct( ) {
max = ${# modlist [@] }
concat( ) {
for ( ( i = 0; i<$max ; i++) ) ; do
echo " publishedfileids[ $i ]= ${ modlist [ $i ] } & "
done | awk '{print}' ORS = ''
}
payload( ) {
echo -e " itemcount= ${ max } & $( concat) "
}
post( ) {
curl -s -X POST -H "Content-Type:application/x-www-form-urlencoded" -d " $( payload) " 'https://api.steampowered.com/ISteamRemoteStorage/GetPublishedFileDetails/v1/?format=json'
}
readarray -t newlist <<< $( post | jq -r '.[].publishedfiledetails[] | select(.result==1) .publishedfileid' )
}
2022-06-04 22:29:30 +02:00
validate_mods( ) {
url = "https://steamcommunity.com/sharedfiles/filedetails/?id="
newlist = ( )
readarray -t modlist <<< $remote_mods
2022-06-12 06:44:30 +02:00
query_defunct
2022-06-04 22:29:30 +02:00
}
server_modlist( ) {
for i in " ${ newlist [@] } " ; do
printf " $i \n "
done
}
compare( ) {
diff = $( comm -23 <( server_modlist | sort) <( installed_mods | sort) )
}
installed_mods( ) {
ls -1 " $workshop_dir "
}
2022-05-09 13:42:24 +02:00
concat_mods( ) {
2022-07-17 18:06:41 +02:00
if [ [ -z ${ remote_mods [@] } ] ] ; then
return 1
else
readarray -t serv <<< " $( server_modlist) "
for i in " ${ serv [@] } " ; do
id = $( awk -F"= " '/publishedid/ {print $2}' " $workshop_dir " /$i /meta.cpp | awk -F\; '{print $1}' )
2022-07-24 15:20:06 +02:00
encoded_id = $( encode $id )
link = " @ $encoded_id ; "
2022-07-17 18:06:41 +02:00
echo -e " $link "
done | tr -d '\n' | perl -ple 'chop'
fi
2022-05-09 13:42:24 +02:00
}
launch( ) {
mods = $( concat_mods)
if [ [ $debug -eq 1 ] ] ; then
2022-07-24 15:20:06 +02:00
launch_options = " steam -applaunch $aid -connect= $ip -nolauncher -nosplash -skipintro \"-mod= $mods \" "
print_launch_options = " $( printf " [DEBUG] This is a dry run. These options would have been used to launch the game:\n\n $launch_options \n " | fold -w 60) "
zenity --question --title= "DZGUI" --ok-label= "Write to file" --cancel-label= "Back" \
--text= " $print_launch_options " 2>/dev/null
if [ [ $? -eq 0 ] ] ; then
source_script = $( realpath " $0 " )
source_dir = $( dirname " $source_script " )
echo " $launch_options " > " $source_dir " /options.log
echo " [DZGUI] Wrote 'options.log' to $source_dir "
fi
2022-05-09 13:42:24 +02:00
else
2022-06-13 03:16:03 +02:00
echo "[DZGUI] All OK. Launching DayZ"
2022-06-15 12:32:10 +02:00
zenity --title= "DZGUI" --info --text= "Launch conditions satisfied.\nDayZ will now launch after clicking [OK]." 2>/dev/null
2022-05-09 13:42:24 +02:00
steam -applaunch $aid -connect= $ip -nolauncher -nosplash -skipintro -name= $name \" -mod= $mods \"
exit
fi
2022-06-15 12:32:10 +02:00
one_shot_launch = 0
}
browser( ) {
if [ [ -n " $BROWSER " ] ] ; then
" $BROWSER " " $1 " 2>/dev/null
else
xdg-open " $1 " 2>/dev/null
fi
2022-05-09 13:42:24 +02:00
}
2022-06-04 22:29:30 +02:00
report_bug( ) {
2022-06-15 12:32:10 +02:00
echo "[DZGUI] Opening issues page in browser"
if [ [ $is_steam_deck -eq 1 ] ] ; then
steam steam://openurl/" $git_url " 2>/dev/null
elif [ [ $is_steam_deck -eq 0 ] ] ; then
2022-07-24 15:20:06 +02:00
browser " $git_url " 2>/dev/null &
2022-06-15 12:32:10 +02:00
fi
}
help_file( ) {
echo "[DZGUI] Opening help file in browser"
if [ [ $is_steam_deck -eq 1 ] ] ; then
steam steam://openurl/" $help_url " 2>/dev/null
elif [ [ $is_steam_deck -eq 0 ] ] ; then
2022-07-24 15:20:06 +02:00
browser " $help_url " 2>/dev/null &
2022-06-15 12:32:10 +02:00
fi
2022-05-22 21:20:34 +02:00
}
set_mode( ) {
if [ [ $debug -eq 1 ] ] ; then
2022-06-04 22:29:30 +02:00
mode = debug
2022-05-22 21:20:34 +02:00
else
mode = normal
fi
}
2022-07-24 15:20:06 +02:00
delete_by_id( ) {
new_whitelist = " whitelist=\" $( echo " $whitelist " | sed " s/, $server_id $//;s/^ $server_id ,//;s/, $server_id ,/,/ " ) \" "
mv $config_file ${ config_path } dztuirc.old
nr = $( awk '/whitelist=/ {print NR}' ${ config_path } dztuirc.old)
awk -v " var= $new_whitelist " -v " nr= $nr " 'NR==nr {$0=var}{print}' ${ config_path } dztuirc.old > ${ config_path } dztuirc
echo " [DZGUI] Removed $server_id from key 'whitelist' "
zenity --info --title= "DZGUI" --text= "Removed " $server_id " from:\n ${ config_path } dztuirc\nIf errors occur, you can restore the file:\n ${ config_path } dztuirc.old " 2>/dev/null
source $config_file
}
delete_or_connect( ) {
if [ [ $delete -eq 1 ] ] ; then
server_name = $( echo " $sel " | awk -F"%%" '{print $1}' )
server_id = $( echo " $sel " | awk -F"%%" '{print $2}' )
zenity --question --text= " Delete this server? \n $server_name "
if [ [ $? -eq 0 ] ] ; then
delete_by_id $server_id
fi
else
connect $sel
fi
}
2022-06-04 22:29:30 +02:00
populate( ) {
2022-05-22 21:20:34 +02:00
while true; do
2022-07-24 15:20:06 +02:00
if [ [ $delete -eq 1 ] ] ; then
cols = "--column=" Server" --column=" ID""
set_header "delete"
else
cols = "--column=" Server" --column=" IP" --column=" Players" --column=" Gametime" --column=" Status" --column=" ID" --column=" Ping""
set_header ${ FUNCNAME [0] }
fi
2022-06-04 22:29:30 +02:00
rc = $?
2022-07-18 10:58:38 +02:00
if [ [ $rc -eq 0 ] ] ; then
if [ [ -z $sel ] ] ; then
2022-06-04 22:29:30 +02:00
warn "No item was selected."
else
2022-07-24 15:20:06 +02:00
delete_or_connect
return
2022-06-04 22:29:30 +02:00
fi
else
2022-07-24 15:20:06 +02:00
delete = 0
2022-06-04 22:29:30 +02:00
return
fi
done
}
list_mods( ) {
2022-06-15 12:32:10 +02:00
if [ [ -z $( installed_mods) || -z $( find $workshop_dir -maxdepth 2 -name "*.cpp" | grep .cpp) ] ] ; then
zenity --info --text= "No mods currently installed or incorrect path given" $sd_res 2>/dev/null
else
2022-07-24 15:20:06 +02:00
for d in $( find $game_dir /* -maxdepth 1 -type l) ; do
dir = $( basename $d )
awk -v d = $dir -F\" '/name/ {printf "%s\t%s\n", $2,d}' " $gamedir " /$d /meta.cpp
done | sort | sed 's/\t/\n/g' | zenity --list --column= "Mod" --column= "Symlink" --title= "DZGUI" $sd_res --print-column= "" 2>/dev/null
2022-06-15 12:32:10 +02:00
fi
2022-06-04 22:29:30 +02:00
}
2022-07-24 15:20:06 +02:00
fetch_query_ports( ) {
qport_list = $( echo " $response " | jq -r '.data[] .attributes | "\(.ip):\(.port)%%\(.portQuery)"' )
}
2022-06-04 22:29:30 +02:00
connect_to_fav( ) {
2022-06-15 12:32:10 +02:00
if [ [ -n $fav ] ] ; then
one_shot_launch = 1
query_api
2022-07-24 15:20:06 +02:00
fetch_query_ports
2022-06-15 12:32:10 +02:00
echo " [DZGUI] Attempting connection to $fav_label "
2022-07-18 10:58:38 +02:00
connect " $qport_list "
2022-06-16 05:24:10 +02:00
one_shot_launch = 0
2022-06-15 12:32:10 +02:00
else
warn "No fav server configured"
fi
2022-06-04 22:29:30 +02:00
}
2022-07-18 10:58:38 +02:00
set_header( ) {
2022-07-24 15:20:06 +02:00
if [ [ $1 = = "delete" ] ] ; then
sel = $( cat $tmp | zenity $sd_res --list $cols --title= "DZGUI" --text= " DZGUI $version | Mode: $mode | Branch: $branch | Fav: $fav_label " \
--separator= " $separator " --print-column= 1,2 --ok-label= "Delete" 2>/dev/null)
elif [ [ $1 = = "populate" ] ] ; then
sel = $( cat $tmp | zenity $sd_res --list $cols --title= "DZGUI" --text= " DZGUI $version | Mode: $mode | Branch: $branch | Fav: $fav_label " \
2022-07-18 10:58:38 +02:00
--separator= " $separator " --print-column= 2,6 2>/dev/null)
elif [ [ $1 = = "main_menu" ] ] ; then
2022-07-24 15:20:06 +02:00
sel = $( zenity $sd_res --list --title= "DZGUI" --text= " ${ news } DZGUI $version | Mode: $mode | Branch: $branch | Fav: $fav_label " \
--cancel-label= "Exit" --ok-label= "Select" --column= "Select launch option" --hide-header " ${ items [@] } " 2>/dev/null)
2022-07-18 10:58:38 +02:00
fi
}
toggle_branch( ) {
mv $config_file ${ config_path } dztuirc.old
nr = $( awk '/branch=/ {print NR}' ${ config_path } dztuirc.old)
if [ [ $branch = = "stable" ] ] ; then
branch = "testing"
else
branch = "stable"
fi
flip_branch = " branch=\" $branch \" "
awk -v " var= $flip_branch " -v " nr= $nr " 'NR==nr {$0=var}{print}' ${ config_path } dztuirc.old > $config_file
printf " [DZGUI] Toggled branch to ' $branch '\n "
source $config_file
}
debug_menu( ) {
debug_list = (
"Toggle branch"
)
2022-07-24 15:20:06 +02:00
debug_sel = $( zenity --list --width= 1280 --height= 800 --column= "Options" --title= "DZGUI" --hide-header " ${ debug_list [@] } " 2>/dev/null)
2022-07-18 10:58:38 +02:00
if [ [ $debug_sel = = " ${ debug_list [0] } " ] ] ; then
2022-07-31 19:45:33 +02:00
enforce_dl = 1
2022-07-31 20:01:58 +02:00
toggle_branch &&
2022-07-18 10:58:38 +02:00
check_version
fi
}
2022-07-24 15:20:06 +02:00
query_and_connect( ) {
query_api
parse_json <<< " $response "
#TODO: create logger function
echo "[DZGUI] Checking response time of servers"
create_array | zenity --progress --pulsate --title= "DZGUI" --auto-close 2>/dev/null
rc = $?
if [ [ $rc -eq 1 ] ] ; then
:
else
populate
fi
}
2022-06-04 22:29:30 +02:00
main_menu( ) {
2022-07-24 15:20:06 +02:00
print_news
set_mode
2022-06-22 15:33:52 +02:00
if [ [ $debug -eq 1 ] ] ; then
items += ( "Debug options" )
fi
2022-06-15 12:32:10 +02:00
if [ [ -n $fav ] ] ; then
items[ 3] = "Change favorite server"
fi
2022-06-04 22:29:30 +02:00
while true; do
2022-07-18 10:58:38 +02:00
set_header ${ FUNCNAME [0] }
2022-06-04 22:29:30 +02:00
rc = $?
if [ [ $rc -eq 0 ] ] ; then
if [ [ -z $sel ] ] ; then
warn "No item was selected."
2022-06-15 12:32:10 +02:00
elif [ [ $sel = = " ${ items [0] } " ] ] ; then
2022-07-24 15:20:06 +02:00
query_and_connect
2022-06-15 12:32:10 +02:00
elif [ [ $sel = = " ${ items [1] } " ] ] ; then
2022-06-04 22:29:30 +02:00
connect_to_fav
2022-06-15 12:32:10 +02:00
elif [ [ $sel = = " ${ items [2] } " ] ] ; then
add_by_id
elif [ [ $sel = = " ${ items [3] } " ] ] ; then
add_by_fav
elif [ [ $sel = = " ${ items [4] } " ] ] ; then
2022-07-24 15:20:06 +02:00
delete = 1
query_and_connect
2022-06-15 12:32:10 +02:00
elif [ [ $sel = = " ${ items [5] } " ] ] ; then
2022-07-24 15:20:06 +02:00
list_mods
elif [ [ $sel = = " ${ items [6] } " ] ] ; then
2022-06-22 15:33:52 +02:00
toggle_debug
main_menu
return
2022-06-15 12:32:10 +02:00
elif [ [ $sel = = " ${ items [7] } " ] ] ; then
2022-07-24 15:20:06 +02:00
report_bug
2022-06-22 15:33:52 +02:00
elif [ [ $sel = = " ${ items [8] } " ] ] ; then
2022-07-24 15:20:06 +02:00
help_file
2022-07-18 10:58:38 +02:00
elif [ [ $sel = = " ${ items [9] } " ] ] ; then
2022-07-24 15:20:06 +02:00
changelog | zenity --text-info $sd_res --title= "DZGUI" 2>/dev/null
elif [ [ $sel = = " ${ items [10] } " ] ] ; then
2022-07-18 10:58:38 +02:00
debug_menu
2022-06-04 22:29:30 +02:00
else
warn "This feature is not yet implemented."
fi
2022-05-22 21:20:34 +02:00
else
2022-06-04 22:29:30 +02:00
return
2022-05-22 21:20:34 +02:00
fi
done
}
parse_json( ) {
2022-06-15 12:32:10 +02:00
list = $( jq -r '.data[] .attributes | "\(.name)\t\(.ip):\(.port)\t\(.players)/\(.maxPlayers)\t\(.details.time)\t\(.status)\t\(.id)"' )
2022-07-24 15:20:06 +02:00
fetch_query_ports
#qport_list=$(echo "$response" | jq -r '.data[] .attributes | "\(.ip):\(.port)%%\(.portQuery)"')
2022-05-22 21:20:34 +02:00
echo -e " $list " > $tmp
2022-05-09 13:42:24 +02:00
}
check_ping( ) {
2022-07-24 15:20:06 +02:00
ping_ip = $( echo " $1 " | awk -F'\t' '{print $2}' | awk -F: '{print $1}' )
ms = $( ping -c 1 -W 1 " $ping_ip " | awk -Ftime= '/time=/ {print $2}' )
if [ [ -z $ms ] ] ; then
echo "Timeout"
else
echo " $ms "
fi
2022-05-22 21:20:34 +02:00
}
2022-05-09 13:42:24 +02:00
create_array( ) {
list = $( cat $tmp )
#TODO: improve error handling for null values
2022-05-29 08:33:14 +02:00
lc = 1
2022-05-09 13:42:24 +02:00
while read line; do
name = $( echo " $line " | awk -F'\t' '{print $1}' )
2022-05-22 21:20:34 +02:00
#truncate names
2022-06-15 12:32:10 +02:00
if [ [ $( echo " $name " | wc -m) -gt 50 ] ] ; then
name = " $( echo $name | awk '{print substr($0,1,50) "..."}' ) "
2022-05-09 13:42:24 +02:00
else
:
fi
ip = $( echo " $line " | awk -F'\t' '{print $2}' )
players = $( echo " $line " | awk -F'\t' '{print $3}' )
2022-06-15 12:32:10 +02:00
time = $( echo " $line " | awk -F'\t' '{print $4}' )
stat = $( echo " $line " | awk -F'\t' '{print $5}' )
2022-06-04 22:29:30 +02:00
2022-05-22 21:20:34 +02:00
#yad only
#[[ $stat == "online" ]] && stat="<span color='#77ff33'>online</span>" || :
2022-06-04 22:29:30 +02:00
2022-05-09 13:42:24 +02:00
#TODO: probe offline return codes
2022-06-15 12:32:10 +02:00
id = $( echo " $line " | awk -F'\t' '{print $6}' )
2022-05-29 08:33:14 +02:00
tc = $( awk 'END{print NR}' $tmp )
2022-07-24 15:20:06 +02:00
if [ [ $delete -eq 1 ] ] ; then
declare -g -a rows = ( " ${ rows [@] } " " $name " " $id " )
else
echo " $lc / $tc "
echo " # Checking ping: $lc / $tc "
ping = $( check_ping " $line " )
declare -g -a rows = ( " ${ rows [@] } " " $name " " $ip " " $players " " $time " " $stat " " $id " " $ping " )
fi
2022-05-29 08:33:14 +02:00
let lc++
done <<< " $list "
for i in " ${ rows [@] } " ; do echo -e " $i " ; done > $tmp
2022-05-09 13:42:24 +02:00
}
2022-06-04 22:29:30 +02:00
set_fav( ) {
#TODO: test API key here and return errors
2022-06-12 06:44:30 +02:00
query_api
2022-06-04 22:29:30 +02:00
fav_label = $( curl -s " $api " -H "Authorization: Bearer " $api_key "" -G -d " filter[game]= $game " -d " filter[ids][whitelist]= $fav " \
| jq -r '.data[] .attributes .name' )
2022-06-15 12:32:10 +02:00
if [ [ -z $fav_label ] ] ; then
fav_label = null
2022-06-12 06:44:30 +02:00
fi
echo " [DZGUI] Setting favorite server to ' $fav_label ' "
2022-06-04 22:29:30 +02:00
}
2022-06-15 12:32:10 +02:00
check_unmerged( ) {
if [ [ -f ${ config_path } .unmerged ] ] ; then
printf "[DZGUI] Found new config format, merging changes\n"
merge_config
rm ${ config_path } .unmerged
fi
}
merge_config( ) {
source $config_file
mv $config_file ${ config_path } dztuirc.old
write_config > $config_file
printf "[DZGUI] Wrote new config file to %sdztuirc\n" $config_path
zenity --info --title= "DZGUI" --text= " Wrote new config format to \n ${ config_path } dztuirc\nIf errors occur, you can restore the file:\n ${ config_path } dztuirc.old " 2>/dev/null
}
2022-06-04 22:29:30 +02:00
download_new_version( ) {
2022-06-18 05:32:24 +02:00
source_script = $( realpath " $0 " )
source_dir = $( dirname " $source_script " )
mv $source_script $source_script .old
curl -Ls " $version_url " > $source_script
2022-06-04 22:29:30 +02:00
rc = $?
if [ [ $rc -eq 0 ] ] ; then
2022-06-18 05:32:24 +02:00
echo " [DZGUI] Wrote $upstream to $source_script "
chmod +x $source_script
2022-06-15 12:32:10 +02:00
touch ${ config_path } .unmerged
2022-06-12 06:44:30 +02:00
zenity --question --title= "DZGUI" --text " DZGUI $upstream successfully downloaded.\nTo view the changelog, select Changelog.\nTo use the new version, select Exit and restart. " --ok-label= "Changelog" --cancel-label= "Exit" 2>/dev/null
code = $?
if [ [ $code -eq 0 ] ] ; then
changelog | zenity --text-info $sd_res --title= "DZGUI" 2>/dev/null
exit
elif [ [ $code -eq 1 ] ] ; then
exit
fi
2022-06-04 22:29:30 +02:00
else
2022-06-18 05:32:24 +02:00
mv $source_script .old $source_script
2022-06-04 22:29:30 +02:00
zenity --info --title= "DZGUI" --text "Failed to download new version." 2>/dev/null
return
fi
}
2022-07-18 10:58:38 +02:00
check_branch( ) {
if [ [ $branch = = "stable" ] ] ; then
version_url = " $stable_url "
elif [ [ $branch = = "testing" ] ] ; then
version_url = " $testing_url "
fi
upstream = $( curl -Ls " $version_url " | awk -F= '/^version=/ {print $2}' )
}
2022-07-31 19:45:33 +02:00
enforce_dl( ) {
2022-07-31 20:01:58 +02:00
download_new_version
2022-07-31 19:45:33 +02:00
}
prompt_dl( ) {
zenity --question --title= "DZGUI" --text " Version conflict.\n\nYour branch:\t\t\t $branch \nYour version:\t\t\t $version \nUpstream version:\t\t $upstream \n\nVersion updates introduce important bug fixes and are encouraged.\n\nAttempt to download latest version? " --width= 500 --ok-label= "Yes" --cancel-label= "No" 2>/dev/null
rc = $?
if [ [ $rc -eq 1 ] ] ; then
return
else
download_new_version
fi
}
2022-06-04 22:29:30 +02:00
check_version( ) {
2022-07-18 10:58:38 +02:00
source $config_file
2022-07-31 19:52:59 +02:00
[ [ -z $branch ] ] && branch = "stable"
2022-07-18 10:58:38 +02:00
check_branch
2022-06-04 22:29:30 +02:00
if [ [ $version = = $upstream ] ] ; then
2022-06-15 12:32:10 +02:00
check_unmerged
2022-06-04 22:29:30 +02:00
else
2022-07-31 19:45:33 +02:00
echo " [DZGUI] Upstream ( $upstream ) != local ( $version ) "
if [ [ $enforce_dl -eq 1 ] ] ; then
enforce_dl
2022-06-04 22:29:30 +02:00
else
2022-07-31 19:45:33 +02:00
prompt_dl
2022-06-04 22:29:30 +02:00
fi
fi
}
2022-06-15 12:32:10 +02:00
check_architecture( ) {
2022-07-24 15:20:06 +02:00
cpu = $( cat /proc/cpuinfo | grep "AMD Custom APU 0405" )
if [ [ -n $cpu ] ] ; then
2022-06-15 12:32:10 +02:00
is_steam_deck = 1
echo "[DZGUI] Setting architecture to 'Steam Deck'"
else
is_steam_deck = 0
echo "[DZGUI] Setting architecture to 'desktop'"
fi
}
2022-06-04 22:29:30 +02:00
add_by_id( ) {
2022-07-17 18:06:41 +02:00
#TODO: prevent redundant creation of existent IDs (for neatness)
2022-06-15 12:32:10 +02:00
while true; do
id = $( zenity --entry --text= "Enter server ID" --title= "DZGUI" 2>/dev/null)
rc = $?
if [ [ $rc -eq 1 ] ] ; then
return
else
if [ [ ! $id = ~ ^[ 0-9] +$ ] ] ; then
zenity --warning --title= "DZGUI" --text= "Invalid ID" 2>/dev/null
else
new_whitelist = " whitelist=\" $whitelist , $id \" "
mv $config_file ${ config_path } dztuirc.old
nr = $( awk '/whitelist=/ {print NR}' ${ config_path } dztuirc.old)
awk -v " var= $new_whitelist " -v " nr= $nr " 'NR==nr {$0=var}{print}' ${ config_path } dztuirc.old > ${ config_path } dztuirc
echo " [DZGUI] Added $id to key 'whitelist' "
zenity --info --title= "DZGUI" --text= "Added " $id " to:\n ${ config_path } dztuirc\nIf errors occur, you can restore the file:\n ${ config_path } dztuirc.old " 2>/dev/null
source $config_file
return
fi
fi
done
}
2022-06-22 15:33:52 +02:00
toggle_debug( ) {
mv $config_file ${ config_path } dztuirc.old
nr = $( awk '/debug=/ {print NR}' ${ config_path } dztuirc.old)
if [ [ $debug -eq 1 ] ] ; then
debug = 0
items = ( )
init_items
else
debug = 1
fi
flip_debug = " debug=\" $debug \" "
awk -v " var= $flip_debug " -v " nr= $nr " 'NR==nr {$0=var}{print}' ${ config_path } dztuirc.old > $config_file
printf " [DZGUI] Toggled debug flag to ' $debug '\n "
source $config_file
}
setup( ) {
if [ [ -n $fav ] ] ; then
set_fav
items[ 3] = "Change favorite server"
fi
}
2022-07-24 15:20:06 +02:00
check_map_count( ) {
count = 1048576
if [ [ $( sysctl -q vm.max_map_count | awk -F"= " '{print $2}' ) -ne $count ] ] ; then
map_warning = $( zenity --question --title= "DZGUI" --text " System map count must be $count or higher to run DayZ with Wine. Increase map count and make this change permanent? (will prompt for sudo password) " 2>/dev/null)
if [ [ $? -eq 0 ] ] ; then
pass = $( zenity --password)
sudo -S <<< " $pass " sh -c "echo 'vm.max_map_count=1048576' > /etc/sysctl.d/dayz.conf"
echo ""
fi
fi
}
2022-06-15 12:32:10 +02:00
add_by_fav( ) {
2022-06-04 22:29:30 +02:00
while true; do
2022-06-15 12:32:10 +02:00
fav_id = $( zenity --entry --text= "Enter server ID" --title= "DZGUI" 2>/dev/null)
2022-06-04 22:29:30 +02:00
rc = $?
if [ [ $rc -eq 1 ] ] ; then
return
else
2022-06-15 12:32:10 +02:00
if [ [ ! $fav_id = ~ ^[ 0-9] +$ ] ] ; then
2022-06-04 22:29:30 +02:00
zenity --warning --title= "DZGUI" --text= "Invalid ID"
else
2022-06-15 12:32:10 +02:00
new_fav = " fav=\" $fav_id \" "
2022-06-04 22:29:30 +02:00
mv $config_file ${ config_path } dztuirc.old
2022-06-15 12:32:10 +02:00
nr = $( awk '/fav=/ {print NR}' ${ config_path } dztuirc.old)
awk -v " var= $new_fav " -v " nr= $nr " 'NR==nr {$0=var}{print}' ${ config_path } dztuirc.old > ${ config_path } dztuirc
echo " [DZGUI] Added $fav_id to key 'fav' "
zenity --info --title= "DZGUI" --text= "Added " $fav_id " to:\n ${ config_path } dztuirc\nIf errors occurred, you can restore the file:\n ${ config_path } dztuirc.old " 2>/dev/null
2022-06-04 22:29:30 +02:00
source $config_file
2022-06-15 12:32:10 +02:00
set_fav
items[ 3] = "Change favorite server"
2022-06-04 22:29:30 +02:00
return
fi
fi
done
}
2022-05-09 13:42:24 +02:00
main( ) {
2022-06-15 12:32:10 +02:00
run_depcheck
2022-06-04 22:29:30 +02:00
check_version
2022-06-15 12:32:10 +02:00
check_architecture
2022-07-24 15:20:06 +02:00
check_map_count
2022-05-22 21:20:34 +02:00
config
2022-06-15 12:32:10 +02:00
run_varcheck
2022-06-22 15:33:52 +02:00
init_items
setup
2022-05-22 21:20:34 +02:00
main_menu
2022-05-09 13:42:24 +02:00
}
main