core_steamcmd.sh 11 KB

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