core_steamcmd.sh 11 KB

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