core_steamcmd.sh 10 KB

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