core_steamcmd.sh 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. #!/bin/bash
  2. # LinuxGSM core_steamcmd.sh function
  3. # Author: Daniel Gibbs
  4. # Website: https://linuxgsm.com
  5. # Description: Core functions for SteamCMD
  6. functionselfname="$(basename "$(readlink -f "${BASH_SOURCE[0]}")")"
  7. fn_install_steamcmd(){
  8. if [ "${shortname}" == "ark" ]&&[ "${installsteamcmd}" == "1" ]; then
  9. steamcmddir="${serverfiles}/Engine/Binaries/ThirdParty/SteamCMD/Linux"
  10. fi
  11. if [ ! -d "${steamcmddir}" ]; then
  12. mkdir -p "${steamcmddir}"
  13. fi
  14. remote_fileurl="${1}"
  15. remote_fileurl_backup="${2}"
  16. remote_fileurl_name="${3}"
  17. remote_fileurl_backup_name="${4}"
  18. local_filedir="${5}"
  19. local_filename="${6}"
  20. chmodx="${7:-0}"
  21. run="${8:-0}"
  22. forcedl="${9:-0}"
  23. md5="${10:-0}"
  24. fn_fetch_file "http://media.steampowered.com/client/steamcmd_linux.tar.gz" "" "" "" "${tmpdir}" "steamcmd_linux.tar.gz" "" "norun" "noforce" "nomd5"
  25. fn_dl_extract "${tmpdir}" "steamcmd_linux.tar.gz" "${steamcmddir}"
  26. chmod +x "${steamcmddir}/steamcmd.sh"
  27. }
  28. fn_check_steamcmd_user(){
  29. # Checks if steamuser is setup.
  30. if [ "${steamuser}" == "username" ]; then
  31. fn_print_fail_nl "Steam login not set. Update steamuser in ${configdirserver}"
  32. echo -e " * Change steamuser=\"username\" to a valid steam login."
  33. if [ -d "${lgsmlogdir}" ]; then
  34. fn_script_log_fatal "Steam login not set. Update steamuser in ${configdirserver}"
  35. fi
  36. core_exit.sh
  37. fi
  38. # Anonymous user is set if steamuser is missing.
  39. if [ -z "${steamuser}" ]; then
  40. if [ -d "${lgsmlogdir}" ]; then
  41. fn_script_log_info "Using anonymous Steam login"
  42. fi
  43. steamuser="anonymous"
  44. steampass=''
  45. fi
  46. }
  47. fn_check_steamcmd(){
  48. # Checks if SteamCMD exists when starting or updating a server.
  49. # Only install if steamcmd package is missing or steamcmd dir is missing.
  50. if [ ! -f "${steamcmddir}/steamcmd.sh" ]&&[ -z "$(command -v steamcmd 2>/dev/null)" ]; then
  51. if [ "${commandname}" == "INSTALL" ]; then
  52. fn_install_steamcmd
  53. else
  54. fn_print_warn_nl "SteamCMD is missing"
  55. fn_script_log_warn "SteamCMD is missing"
  56. fn_install_steamcmd
  57. fi
  58. elif [ "${commandname}" == "INSTALL" ]; then
  59. fn_print_information "SteamCMD is already installed..."
  60. fn_print_ok_eol_nl
  61. fi
  62. }
  63. fn_check_steamcmd_dir(){
  64. # Worksround that pre-installs the correct steam directories to ensure all packages use the correct Standard.
  65. # https://github.com/ValveSoftware/steam-for-linux/issues/6976#issuecomment-610446347
  66. # Create Steam installation directory.
  67. if [ ! -d "${XDG_DATA_HOME:="${HOME}/.local/share"}/Steam" ]; then
  68. mkdir -p "${XDG_DATA_HOME:="${HOME}/.local/share"}/Steam"
  69. fi
  70. # Create common Steam directory.
  71. if [ ! -d "${HOME}/.steam" ]; then
  72. mkdir -p "${HOME}/.steam"
  73. fi
  74. # Symbolic links to Steam installation directory.
  75. if [ ! -L "${HOME}/.steam/root" ]; then
  76. if [ -d "${HOME}/.steam/root" ]; then
  77. rm "${HOME}/.steam/root"
  78. fi
  79. ln -s "${XDG_DATA_HOME:="${HOME}/.local/share"}/Steam" "${HOME}/.steam/root"
  80. fi
  81. if [ ! -L "${HOME}/.steam/steam" ]; then
  82. if [ -d "${HOME}/.steam/steam" ]; then
  83. rm -rf "${HOME}/.steam/steam"
  84. fi
  85. ln -s "${XDG_DATA_HOME:="${HOME}/.local/share"}/Steam" "${HOME}/.steam/steam"
  86. fi
  87. }
  88. fn_check_steamcmd_dir_legacy(){
  89. # Remove old Steam installation directories ~/Steam and ${rootdir}/steamcmd
  90. if [ -d "${rootdir}/steamcmd" ]&&[ "${steamcmddir}" == "${XDG_DATA_HOME:="${HOME}/.local/share"}/Steam" ]; then
  91. rm -rf "${rootdir:?}/steamcmd"
  92. fi
  93. if [ -d "${HOME}/Steam" ]&&[ "${steamcmddir}" == "${XDG_DATA_HOME:="${HOME}/.local/share"}/Steam" ]; then
  94. rm -rf "${HOME}/Steam"
  95. fi
  96. }
  97. fn_check_steamcmd_ark(){
  98. # Checks if SteamCMD exists in
  99. # Engine/Binaries/ThirdParty/SteamCMD/Linux
  100. # to allow ark mods to work
  101. if [ ! -f "${serverfiles}/Engine/Binaries/ThirdParty/SteamCMD/Linux/steamcmd.sh" ]; then
  102. installsteamcmd=1
  103. if [ "${commandname}" == "INSTALL" ]; then
  104. fn_install_steamcmd
  105. else
  106. fn_print_warn_nl "ARK mods SteamCMD is missing"
  107. fn_script_log_warn "ARK mods SteamCMD is missing"
  108. fn_install_steamcmd
  109. fi
  110. elif [ "${commandname}" == "INSTALL" ]; then
  111. fn_print_information "ARK mods SteamCMD is already installed..."
  112. fn_print_ok_eol_nl
  113. fi
  114. }
  115. fn_check_steamcmd_clear(){
  116. # Will remove steamcmd dir if steamcmd package is installed.
  117. if [ "$(command -v steamcmd 2>/dev/null)" ]&&[ -d "${rootdir}/steamcmd" ]; then
  118. rm -rf "${steamcmddir:?}"
  119. exitcode=$?
  120. if [ "${exitcode}" != 0 ]; then
  121. fn_script_log_fatal "Removing ${rootdir}/steamcmd"
  122. else
  123. fn_script_log_pass "Removing ${rootdir}/steamcmd"
  124. fi
  125. fi
  126. }
  127. fn_check_steamcmd_exec(){
  128. if [ "$(command -v steamcmd 2>/dev/null)" ]; then
  129. steamcmdcommand="steamcmd"
  130. else
  131. steamcmdcommand="./steamcmd.sh"
  132. fi
  133. }
  134. fn_update_steamcmd_localbuild(){
  135. # Gets local build info.
  136. fn_print_dots "Checking local build: ${remotelocation}"
  137. fn_appmanifest_check
  138. # Uses appmanifest to find local build.
  139. localbuild=$(grep buildid "${appmanifestfile}" | tr '[:blank:]"' ' ' | tr -s ' ' | cut -d\ -f3)
  140. # Set branch to public if no custom branch.
  141. if [ -z "${branch}" ]; then
  142. branch="public"
  143. fi
  144. # Checks if localbuild variable has been set.
  145. if [ -z "${localbuild}" ]||[ "${localbuild}" == "null" ]; then
  146. fn_print_fail "Checking local build: ${remotelocation}"
  147. fn_script_log_fatal "Checking local build"
  148. core_exit.sh
  149. else
  150. fn_print_ok "Checking local build: ${remotelocation}"
  151. fn_script_log_pass "Checking local build"
  152. fi
  153. }
  154. fn_update_steamcmd_remotebuild(){
  155. # Gets remote build info.
  156. if [ -d "${steamcmddir}" ]; then
  157. cd "${steamcmddir}" || exit
  158. fi
  159. # Removes appinfo.vdf as a fix for not always getting up to date version info from SteamCMD.
  160. if [ "$(find "${HOME}" -type f -name "appinfo.vdf" | wc -l)" -ne "0" ]; then
  161. find "${HOME}" -type f -name "appinfo.vdf" -exec rm -f {} \;
  162. fi
  163. # password for branch not needed to check the buildid
  164. remotebuild=$(${steamcmdcommand} +login "${steamuser}" "${steampass}" +app_info_update 1 +app_info_print "${appid}" +quit | sed -e '/"branches"/,/^}/!d' | sed -n "/\"${branch}\"/,/}/p" | grep -m 1 buildid | tr -cd '[:digit:]')
  165. if [ "${firstcommandname}" != "INSTALL" ]; then
  166. fn_print_dots "Checking remote build: ${remotelocation}"
  167. # Checks if remotebuild variable has been set.
  168. if [ -z "${remotebuild}" ]||[ "${remotebuild}" == "null" ]; then
  169. fn_print_fail "Checking remote build: ${remotelocation}"
  170. fn_script_log_fatal "Checking remote build"
  171. core_exit.sh
  172. else
  173. fn_print_ok "Checking remote build: ${remotelocation}"
  174. fn_script_log_pass "Checking remote build"
  175. fi
  176. else
  177. # Checks if remotebuild variable has been set.
  178. if [ -z "${remotebuild}" ]||[ "${remotebuild}" == "null" ]; then
  179. fn_print_failure "Unable to get remote build"
  180. fn_script_log_fatal "Unable to get remote build"
  181. core_exit.sh
  182. fi
  183. fi
  184. }
  185. fn_update_steamcmd_compare(){
  186. fn_print_dots "Checking for update: ${remotelocation}"
  187. if [ "${localbuild}" != "${remotebuild}" ]; then
  188. fn_print_ok_nl "Checking for update: ${remotelocation}"
  189. echo -en "\n"
  190. echo -e "Update available"
  191. echo -e "* Local build: ${red}${localbuild}${default}"
  192. echo -e "* Remote build: ${green}${remotebuild}${default}"
  193. if [ -n "${branch}" ]; then
  194. echo -e "* Branch: ${branch}"
  195. fi
  196. if [ -n "${betapassword}" ]; then
  197. echo -e "* Branch password: ${betapassword}"
  198. fi
  199. echo -e "https://steamdb.info/app/${appid}/"
  200. echo -en "\n"
  201. fn_script_log_info "Update available"
  202. fn_script_log_info "Local build: ${localbuild}"
  203. fn_script_log_info "Remote build: ${remotebuild}"
  204. if [ -n "${branch}" ]; then
  205. fn_script_log_info "Branch: ${branch}"
  206. fi
  207. if [ -n "${betapassword}" ]; then
  208. fn_script_log_info "Branch password: ${betapassword}"
  209. fi
  210. fn_script_log_info "${localbuild} > ${remotebuild}"
  211. if [ "${commandname}" == "UPDATE" ]; then
  212. unset updateonstart
  213. check_status.sh
  214. # If server stopped.
  215. if [ "${status}" == "0" ]; then
  216. fn_dl_steamcmd
  217. # If server started.
  218. else
  219. fn_print_restart_warning
  220. exitbypass=1
  221. command_stop.sh
  222. fn_firstcommand_reset
  223. exitbypass=1
  224. fn_dl_steamcmd
  225. exitbypass=1
  226. command_start.sh
  227. fn_firstcommand_reset
  228. fi
  229. unset exitbypass
  230. date +%s > "${lockdir}/lastupdate.lock"
  231. alert="update"
  232. elif [ "${commandname}" == "CHECK-UPDATE" ]; then
  233. alert="check-update"
  234. fi
  235. alert.sh
  236. else
  237. fn_print_ok_nl "Checking for update: ${remotelocation}"
  238. echo -en "\n"
  239. echo -e "No update available"
  240. echo -e "* Local build: ${green}${localbuild}${default}"
  241. echo -e "* Remote build: ${green}${remotebuild}${default}"
  242. if [ -n "${branch}" ]; then
  243. echo -e "* Branch: ${branch}"
  244. fi
  245. if [ -n "${betapassword}" ]; then
  246. echo -e "* Branch password: ${betapassword}"
  247. fi
  248. echo -e "https://steamdb.info/app/${appid}/"
  249. echo -en "\n"
  250. fn_script_log_info "No update available"
  251. fn_script_log_info "Local build: ${localbuild}"
  252. fn_script_log_info "Remote build: ${remotebuild}"
  253. if [ -n "${branch}" ]; then
  254. fn_script_log_info "Branch: ${branch}"
  255. fi
  256. if [ -n "${betapassword}" ]; then
  257. fn_script_log_info "Branch password: ${betapassword}"
  258. fi
  259. fi
  260. }
  261. fn_appmanifest_info(){
  262. appmanifestfile=$(find "${serverfiles}" -type f -name "appmanifest_${appid}.acf")
  263. appmanifestfilewc=$(find "${serverfiles}" -type f -name "appmanifest_${appid}.acf" | wc -l)
  264. }
  265. fn_appmanifest_check(){
  266. fn_appmanifest_info
  267. # Multiple or no matching appmanifest files may sometimes be present.
  268. # This error is corrected if required.
  269. if [ "${appmanifestfilewc}" -ge "2" ]; then
  270. fn_print_error "Multiple appmanifest_${appid}.acf files found"
  271. fn_script_log_error "Multiple appmanifest_${appid}.acf files found"
  272. fn_print_dots "Removing x${appmanifestfilewc} appmanifest_${appid}.acf files"
  273. for appfile in ${appmanifestfile}; do
  274. rm -f "${appfile:?}"
  275. done
  276. appmanifestfilewc1="${appmanifestfilewc}"
  277. fn_appmanifest_info
  278. # if error can not be resolved.
  279. if [ "${appmanifestfilewc}" -ge "2" ]; then
  280. fn_print_fail "Unable to remove x${appmanifestfilewc} appmanifest_${appid}.acf files"
  281. fn_script_log_fatal "Unable to remove x${appmanifestfilewc} appmanifest_${appid}.acf files"
  282. echo -e "* Check user permissions"
  283. for appfile in ${appmanifestfile}; do
  284. echo -e " ${appfile}"
  285. done
  286. core_exit.sh
  287. else
  288. fn_print_ok "Removed x${appmanifestfilewc1} appmanifest_${appid}.acf files"
  289. fn_script_log_pass "Removed x${appmanifestfilewc1} appmanifest_${appid}.acf files"
  290. fn_print_info_nl "Forcing update to correct issue"
  291. fn_script_log_info "Forcing update to correct issue"
  292. fn_dl_steamcmd
  293. fi
  294. elif [ "${appmanifestfilewc}" -eq "0" ]; then
  295. fn_print_error_nl "No appmanifest_${appid}.acf found"
  296. fn_script_log_error "No appmanifest_${appid}.acf found"
  297. fn_print_info_nl "Forcing update to correct issue"
  298. fn_script_log_info "Forcing update to correct issue"
  299. fn_dl_steamcmd
  300. fn_appmanifest_info
  301. if [ "${appmanifestfilewc}" -eq "0" ]; then
  302. fn_print_fail_nl "Still no appmanifest_${appid}.acf found"
  303. fn_script_log_fatal "Still no appmanifest_${appid}.acf found"
  304. core_exit.sh
  305. fi
  306. fi
  307. }