Advertisement
Guest User

Dayz Linux Script to download Mods and Launch game

a guest
Apr 21st, 2022
995
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 11.26 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. #####################################################################################################################################################################
  4. #
  5. # Script helps installing mods for connecting to a modded Dayz server (as dayz launcher does not yet work in proton).
  6. #
  7. # I used dayz-linux-cli-launcher and dayz-dztui as examples to combine and change what I understand and need as
  8. # both were not running completely for me. To understand what's wrong I need to do it for myself from scratch.
  9. # So, credits to both the authors for the great work!
  10. #
  11. # Script reads what mods the given server needs and downloads them via steamcmd.
  12. # Mods are downloaded into the Steam-workshop-dayz (221100) directory. After that symlinks will be created
  13. # from these workshop mods into the Dayz directory (starting with @ followed by the mod-ID).
  14. # From all the mod-IDs a string is concatenated to be given to the launch command (-mod="@...;@...;").
  15. # Then the script tries to connect to the dayz server using these mods (Steam application must be running for this).
  16. #
  17. # I normally get the server ip:port from web pages listing servers. If the steam query port is not 27016 and not mentioned,
  18. # then I do not know how to get it. This port is needed for the API to retrieve the server information.
  19. # In that case I normally try another server until I get a positive response.
  20. #
  21. # Start the script with e.g.:
  22. # ./dayz-mod-launcher.sh \
  23. #  --server <ip>:<gameport> \
  24. #  --port 27016 \
  25. #  --mods \
  26. #  --launch \
  27. #  --name jahan
  28. #
  29. # Or in a shorter command:
  30. # ./dayz-mod-launcher.sh -s <ip>:<gameport> -m -l -n <profilename>
  31. #
  32. # I put this in an additional script, like dayz_servername.sh to be able to connect to different servers.
  33. # So for every server you create a own file with the right ip:port.
  34. #
  35. # If all mods are downloaded for server, I edit this script and delete the line for updating the mods (--mods).
  36. # That way it does not take so long to connect to the server.
  37. # Also 2 scripts can be created, one for launching and one for updating the mods.
  38. # 1) ./dayz-mod-launcher.sh --server <ip>:<port> --port 27016 --mods
  39. # 2) ./dayz-mod-launcher.sh --server <ip>:<port> --port 27016 --launch --name jahan
  40. # (add -d for more output=
  41. #
  42. # The script does not check everything possible and error out. I just wanted to create a short, intuitive solution
  43. # which does the job automatically for me as simple as possible.
  44. #
  45. # In Steam add proton_experimental in the compability tab of the Dayz Properties.
  46. # I have the following launch options set on the General tab (DXVK_FRAME_RATE to cap my fps):
  47. # DXVK_FRAME_RATE=60 %command% -skipintro -nolauncher -nosplash
  48. #
  49. # Issue:
  50. # The very first time all mods where downloaded for a server and I tried to connect, it worked but I had only 1-2fps and the game crashed soon.
  51. # So go to Steam and start Dayz (seöect Run Dayz Client). In the options (Video) menu deactivate vertical sync.
  52. # After that I was able to play on the modded server and everything worked fine.
  53. #
  54. # I do not know why but sometimes steamcmd stays here for long before it starts to validate the mods:
  55. #   Connecting anonymously to Steam Public...OK
  56. #   Waiting for client config...OK
  57. #   Waiting for user info...OK
  58. # I think it's related to Steam. At other times of the day (in the morning) it works very much faster.
  59. #
  60. # It takes a bit to download the mods. If there is a Timeout in steamcmd I copy the last mod-ID and use this page (add the mod-id at the end of the URL)...
  61. #    https://steamcommunity.com/sharedfiles/filedetails/?id=
  62. # ...to get the name of the mod to download it directly in the Steam workshop of Dayz (Steam-UI).
  63. # This happens often for the larger mods (>2-3+ GB). You can also try to run the script again....sometimes it finishes downloading.
  64. #
  65. #####################################################################################################################################################################
  66. # Edit/change config options below to your needs.
  67.  
  68. #----------------------------------------------------------------------------------
  69.  
  70. #BEGIN CONFIG===============
  71. # Steam Dayz application ID (no need to change)
  72. DAYZ_ID=221100
  73. # path to the Steam directory
  74. STEAM_PATH="/home/jahan/.local/share/Steam/"
  75. # path from the Steam directory to the Dayz game directory
  76. GAME_DIR="$STEAM_PATH/steamapps/common/DayZ"
  77. # path from the Steam directory to the workshop directory
  78. WORKSHOP_DIR="$STEAM_PATH/steamapps/workshop/content"
  79. # Dayz ingame name/profile
  80. INGAMENAME="jahan"
  81. #END CONFIG=================
  82.  
  83. #STEAMCMD CONFIG============
  84. # user which runs the steamcmd application
  85. STEAMCMD_USER="jahan"
  86. # username for steamcmd, anonymous can be used or your real steam user
  87. STEAMCMD_USERNAME="anonymous"
  88. #END STEAMCMD CONFIG========
  89.  
  90. #----------------------------------------------------------------------------------
  91. # Only change anything below if something is not working for you or you want to adapt something
  92.  
  93. set -eo pipefail
  94. SELF=$(basename "$(readlink -f "${0}")")
  95.  
  96. # program dependencies
  97. deps=("jq" "steamcmd" "curl" "sed" "steam")
  98.  
  99. # DZSA Launcher API (to query server data)
  100. API_URL="https://dayzsalauncher.com/api/v1/query/@ADDRESS@/@PORT@"
  101. API_PARAMS=(
  102.   -sSL
  103.   -m 10
  104.   -H "User-Agent: dayz-linux-cli-launcher"
  105. )
  106.  
  107. DEBUG=0
  108. LAUNCH=0
  109. SERVER=""
  110. PORT="27016"
  111. DLMODS=0
  112. NAME=""
  113.  
  114. print_help() {
  115.   cat <<EOF
  116. Usage: ${SELF} [OPTION]... [MODID]... [-- [GAME-PARAM]...]
  117.  
  118. For a given Dayz server:port the script automatically set up mods,
  119. launch the game and connect to a server.
  120.  
  121. Command line options:
  122.  
  123.   -h
  124.   --help
  125.     Print this help text.
  126.  
  127.   -d
  128.   --debug
  129.     Print debug messages to output (more text).
  130.  
  131.   -s <address[:port]>
  132.   --server <address[:port]>
  133.     Server ip and game port (e.g. from game server listings)
  134.     Needs to be provided...otherwise the script cannot do anything.
  135.     This will query the servers mod list and prints out information.
  136.     Can be used (without --mods / --launch) just to get these information.
  137.     If --mods is set, it will automatically download mods.
  138.     If --launch is set, Dayz will automatically connect to the server/port.
  139.  
  140.   -m
  141.   --mods
  142.     Download or updates mods automatically via steamcmd.
  143.     Also creates sysmlinks of the workshop mods into the Dayz game directory.
  144.     If mods are up-to-date, this can be left out (to start the game via --launch faster).
  145.     If there are pbo issues connecting to the game, include it again to validate the mods.
  146.  
  147.   -l
  148.   --launch
  149.     Launch DayZ and connect to given server.
  150.     Steam needs to be open.
  151.  
  152.   -n <name>
  153.   --name <name>
  154.     Set the profile name when launching the game via --launch.
  155.     Some community servers require a profile name when trying to connect.
  156.  
  157.   -p <port>
  158.   --port <port>
  159.     The server's query port, not to be confused with the server's game port.
  160.     If option is not used, the default value will be used: 27016
  161.     Needed to query mod information from the server.
  162.  
  163. EOF
  164. }
  165.  
  166. while (( "$#" )); do
  167.   case "${1}" in
  168.     -h|--help)
  169.       print_help
  170.       exit
  171.       ;;
  172.     -d|--debug)
  173.       DEBUG=1
  174.       ;;
  175.     -m|--mods)
  176.       DLMODS=1
  177.      ;;
  178.     -l|--launch)
  179.       LAUNCH=1
  180.       ;;
  181.     -s|--server)
  182.       SERVER="${2}"
  183.       shift
  184.       ;;
  185.     -p|--port)
  186.       PORT="${2}"
  187.       shift
  188.       ;;
  189.     -n|--name)
  190.       INGAMENAME="${2}"
  191.       shift
  192.       ;;
  193.   esac
  194.   shift
  195. done
  196.  
  197.  
  198. depcheck(){
  199.   for dep in "${deps[@]}"; do
  200.     command -v $dep 2>&1>/dev/null || (printf "[ERROR] Program $dep is missing but required. Please install.\n" ; exit 1)
  201.   done
  202. }
  203.  
  204. msg() {
  205.   echo -e "[${SELF}][info] ${@}"
  206. }
  207.  
  208. err() {
  209.   echo -e >&2 "[${SELF}][error] ${@}"
  210.   exit 1
  211. }
  212.  
  213. debug() {
  214.   if [[ ${DEBUG} == 1 ]]; then
  215.     echo -e "[${SELF}][debug] ${@}"
  216.   fi
  217. }
  218.  
  219. check_dir() {
  220.   debug "Checking directory: ${1} - ${2}"
  221.   [[ -d "${1}" ]] || err "Invalid/missing directory: ${1}"
  222. }
  223.  
  224. query_server_api() {
  225.   [[ -z "${SERVER}" ]] && return
  226.   local query
  227.   local response
  228.   msg "Querying API for server: ${SERVER%:*}:${PORT}"
  229.   query="$(sed -e "s/@ADDRESS@/${SERVER%:*}/" -e "s/@PORT@/${PORT}/" <<< "${API_URL}")"
  230.   debug "Querying ${query}"
  231.   response="$(curl "${API_PARAMS[@]}" "${query}")"
  232.   debug $response"\n"
  233.   msg "Parsing API response...\n"
  234.   jq -e ".result.mods[]" >/dev/null 2>&1 <<< "${response}" || err "Missing mods data from API response"
  235.  
  236.   SERVERNAME=$(jq -r ".result.name" <<< "${response}")
  237.   msg "Servername:\n"$SERVERNAME"\n"
  238.  
  239.   MODNAMES=$(jq ".result.mods[].name" <<< "${response}")
  240.   MODIDS=$(jq ".result.mods[].steamWorkshopId" <<< "${response}")
  241.   msg "Anzahl Mods: "$(echo $MODIDS | wc -w)"\n"
  242.   debug "Modnames:\n"$MODNAMES"\n"
  243.   debug "ModIds:\n"$MODIDS"\n"
  244. }
  245.  
  246. # create a mod list which can be given to steamcmd to download each of them
  247. steamcmd_modlist(){
  248.   for i in $MODIDS; do
  249.     printf "+workshop_download_item %s %s " $DAYZ_ID $i
  250.   done
  251. }
  252.  
  253. # download mods to the Steam workshop dir
  254. auto_download_mods() {
  255.   msg "Downloading mods via steamcmd...\n"
  256.   sudo su -c "steamcmd +force_install_dir $STEAM_PATH +login $STEAMCMD_USERNAME $(steamcmd_modlist) validate +quit" $STEAMCMD_USER
  257.   printf "\n"
  258. }
  259.  
  260. # create symlinks of the Workshop mods into the Steam Dayz directory (mod IDs as names)
  261. symlinks(){
  262.   printf "\n"
  263.   msg "Creating symlinks for new mods...\n"
  264.   for i in $MODIDS; do
  265.   # create symlink from mods in Steam/steamapps/workshop/DAYZ-ID folder in the Steam/steamapps/common/DAYZ folder (starting with @)
  266.   ln -fs "$WORKSHOP_DIR/$DAYZ_ID/$i" "$GAME_DIR/@$i"
  267. #  MODS=$MODS"@"$i";"
  268.   done
  269. }
  270.  
  271. buildmodstring(){
  272.   # build mod string to be given to the launch command -mod= parameter
  273.   for i in $MODIDS; do
  274.   MODS=$MODS"@"$i";"
  275.   done
  276.   debug "Mod String to launch game (-mod= parameter): "
  277.   debug $MODS"\n"
  278. }
  279.  
  280. main () {
  281.  
  282.   clear
  283.  
  284.   msg "Checking a few program dependencies..."
  285.   depcheck
  286.   msg "Ok."
  287.  
  288.   if [[ -z "$SERVER" ]]; then
  289.     msg "Nothing to do..."
  290.     msg "No --server address set. This is a mandatory option to continue-"
  291.     msg "See --help for all the available options."
  292.     exit
  293.   fi
  294.  
  295.   msg "Start of Dayz-Mod-Launcher-Script...\n"
  296.  
  297.   debug "Checking directories..."
  298.   check_dir "${STEAM_PATH}"  "STEAM_PATH"
  299.   check_dir "${GAME_DIR}" "GAME_DIR"
  300.   check_dir "${WORKSHOP_DIR}" "WORKSHOP_DIR"
  301.   debug "Directories ok.\n"
  302.  
  303.   # query mod-information from server
  304.   query_server_api
  305.   buildmodstring
  306.  
  307.   if [[ "${DLMODS}" == 1 ]]; then
  308.     # download or update mods
  309.     # comment out if all mods are downloaded already
  310.     auto_download_mods
  311.  
  312.     # create symlinks in the game dir for each mod
  313.     # comment out if all mods are downloaded and links are already installed
  314.     symlinks
  315.   fi
  316.  
  317.   if [[ "${LAUNCH}" == 1 ]]; then
  318.     msg "Launching DayZ:"
  319.     msg "Trying to connect to server...\n"
  320.     # the next command is needed, otherwise the game crashes or causes a black screen (see protondb dayz comments)
  321.     sudo sysctl -w vm.max_map_count=16777216
  322.     debug "steam -applaunch $DAYZ_ID -connect=$SERVER -nolauncher -skipintro -nosplash -world=empty -name=$INGAMENAME -mod="\"$MODS\""\n"
  323.     # connect to the server via steam applaunch
  324.     steam -applaunch $DAYZ_ID -connect=$SERVER -nolauncher -skipintro -nosplash -world=empty -name=$INGAMENAME -mod="$MODS"
  325.     msg "Good luck out there in the fields...\n"
  326.   fi
  327.  
  328. }
  329.  
  330. main
  331.  
  332.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement