command_fastdl.sh 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507
  1. #!/bin/bash
  2. # LinuxGSM command_fastdl.sh function
  3. # Author: Daniel Gibbs
  4. # Contributor: UltimateByte
  5. # Website: https://gameservermanagers.com
  6. # Description: Creates a FastDL directory.
  7. local commandname="FASTDL"
  8. local commandaction="FastDL"
  9. local function_selfname="$(basename $(readlink -f "${BASH_SOURCE[0]}"))"
  10. check.sh
  11. # Directories
  12. if [ -z "${webdir}" ]; then
  13. webdir="${rootdir}/public_html"
  14. fi
  15. fastdldir="${webdir}/fastdl"
  16. addonsdir="${systemdir}/addons"
  17. # Server lua autorun dir, used to autorun lua on client connect to the server
  18. luasvautorundir="${systemdir}/lua/autorun/server"
  19. luafastdlfile="lgsm_cl_force_fastdl.lua"
  20. luafastdlfullpath="${luasvautorundir}/${luafastdlfile}"
  21. fn_check_bzip2(){
  22. # Returns true if not installed
  23. if [ -z "$(command -v bzip2)" ]; then
  24. fn_print_fail "bzip2 is not installed"
  25. fn_script_log_fatal "bzip2 is not installed"
  26. core_exit.sh
  27. fi
  28. }
  29. # Prompts user for FastDL creation settings
  30. fn_fastdl_config(){
  31. echo "FastDL setup"
  32. echo "================================="
  33. # Prompt for clearing old files if directory was already here
  34. if [ -d "${fastdldir}" ]; then
  35. fn_print_warning_nl "FastDL directory already exists."
  36. echo "${fastdldir}"
  37. echo ""
  38. if fn_prompt_yn "Overwrite existing directory?" Y; then
  39. fn_script_log_info "Overwrite existing directory: YES"
  40. else
  41. core_exit.sh
  42. fi
  43. fi
  44. # Garry's Mod Specific
  45. if [ "${gamename}" == "Garry's Mod" ]; then
  46. # Prompt to clear addons dir from fastdl, can use unnecessary space or not be required depending on addon's file structures
  47. if fn_prompt_yn "Clear addons directory from FastDL?" Y; then
  48. cleargmodaddons="on";
  49. fn_script_log_info "Clear addons directory from FastDL: YES"
  50. else
  51. cleargmodaddons="off";
  52. fn_script_log_info "Clear addons directory from FastDL: NO"
  53. fi
  54. # Prompt for download enforcer, which is using a .lua addfile resource generator
  55. if fn_prompt_yn "Use client download enforcer?" Y; then
  56. luaressource="on"
  57. fn_script_log_info "Use client download enforcer: YES"
  58. else
  59. luaressource="off"
  60. fn_script_log_info "Use client download enforcer: NO"
  61. fi
  62. fi
  63. }
  64. fn_fastdl_dirs(){
  65. # Check and create directories
  66. if [ ! -d "${modsdir}" ];then
  67. echo -en "creating web directory ${webdir}..."
  68. mkdir -p "${webdir}"
  69. exitcode=$?
  70. if [ ${exitcode} -ne 0 ]; then
  71. fn_print_fail_eol_nl
  72. fn_script_log_fatal "Creating web directory ${webdir}"
  73. core_exit.sh
  74. else
  75. fn_print_ok_eol_nl
  76. fn_script_log_pass "Creating web directory ${webdir}"
  77. fi
  78. sleep 0.5
  79. fi
  80. if [ ! -d "${fastdldir}" ];then
  81. echo -en "creating fastdl directory ${fastdldir}..."
  82. mkdir -p "${fastdldir}"
  83. exitcode=$?
  84. if [ ${exitcode} -ne 0 ]; then
  85. fn_print_fail_eol_nl
  86. fn_script_log_fatal "Creating fastdl directory ${fastdldir}"
  87. core_exit.sh
  88. else
  89. fn_print_ok_eol_nl
  90. fn_script_log_pass "Creating fastdl directory ${fastdldir}"
  91. fi
  92. sleep 0.5
  93. fi
  94. }
  95. fn_clear_old_fastdl(){
  96. # Clearing old FastDL if user answered yes
  97. if [ ! -d "${modsdir}" ];then
  98. echo -en "clearing existing FastDL directory ${fastdldir}..."
  99. rm -R "${fastdldir:?}"/*
  100. exitcode=$?
  101. if [ ${exitcode} -ne 0 ]; then
  102. fn_print_fail_eol_nl
  103. fn_script_log_fatal "Clearing existing FastDL directory ${fastdldir}"
  104. core_exit.sh
  105. else
  106. fn_print_ok_eol_nl
  107. fn_script_log_pass "Clearing existing FastDL directory ${fastdldir}"
  108. fi
  109. sleep 0.5
  110. fi
  111. }
  112. fn_fastdl_gmod(){
  113. # Copy all needed files for FastDL
  114. if [ -n "${copyflag}" ]; then
  115. # Removes all existing FastDL files.
  116. if [ -d "${fastdldir}" ]; then
  117. echo -e "removing existing FastDL files"
  118. sleep 0.1
  119. fileswc=1
  120. totalfileswc=$(find "${fastdldir}" | wc -l)
  121. tput sc
  122. while read -r filetoremove; do
  123. tput rc; tput el
  124. printf "removing ${fileswc} / ${totalfileswc} : ${filetoremove}..."
  125. ((fileswc++))
  126. rm -rf "${filetoremove}"
  127. ((exitcode=$?))
  128. if [ ${exitcode} -ne 0 ]; then
  129. fn_script_log_fatal "Removing ${filetoremove}"
  130. break
  131. else
  132. fn_script_log_pass "Removing ${filetoremove}"
  133. fi
  134. sleep 0.01
  135. done < <(find "${fastdldir}")
  136. if [ ${exitcode} -ne 0 ]; then
  137. fn_print_fail_eol_nl
  138. core_exit.sh
  139. else
  140. fn_print_ok_eol_nl
  141. fi
  142. fi
  143. fn_fastdl_dirs
  144. echo -e "copying files to ${fastdldir}"
  145. fn_script_log_info "Copying files to ${fastdldir}"
  146. else
  147. if [ -f "${tmpdir}/fastdl_files_to_compress.txt" ]; then
  148. rm -f "${tmpdir}/fastdl_files_to_compress.txt"
  149. fi
  150. echo -e "analysing required files"
  151. fn_script_log_info "Analysing required files"
  152. fi
  153. cd "${systemdir}"
  154. local allowed_extentions_array=( "*.ain" "*.bsp" "*.mdl" "*.mp3" "*.ogg" "*.otf" "*.pcf" "*.phy" "*.png" "*.vtf" "*.vmt" "*.vtx" "*.vvd" "*.ttf" "*.wav" )
  155. for allowed_extention in "${allowed_extentions_array[@]}"
  156. do
  157. fileswc=0
  158. tput sc
  159. if [ -z "${copyflag}" ]; then
  160. tput rc; tput el
  161. printf "copying ${directory} ${allowed_extention} : ${fileswc}..."
  162. fi
  163. while read -r ext; do
  164. ((fileswc++))
  165. if [ -n "${copyflag}" ]; then
  166. tput rc; tput el
  167. printf "copying ${directory} ${allowed_extention} : ${fileswc}..."
  168. sleep 0.01
  169. cp --parents "${ext}" "${fastdldir}"
  170. exitcode=$?
  171. if [ ${exitcode} -ne 0 ]; then
  172. fn_print_fail_eol_nl
  173. fn_script_log_fatal "Copying ${ext} > ${fastdldir}"
  174. core_exit.sh
  175. else
  176. fn_script_log_pass "Copying ${ext} > ${fastdldir}"
  177. fi
  178. else
  179. tput rc; tput el
  180. printf "gathering ${directory} ${allowed_extention} : ${fileswc}..."
  181. sleep 0.01
  182. echo "${ext}" >> "${tmpdir}/fastdl_files_to_compress.txt"
  183. fi
  184. done < <(find . -type f -iname ${allowed_extention})
  185. if [ -z "${copyflag}" ]; then
  186. tput rc; tput el
  187. printf "gathering ${allowed_extention} : ${fileswc}..."
  188. fi
  189. if [ ${fileswc} != 0 ]&&[ -n "${copyflag}" ]||[ -z "${copyflag}" ]; then
  190. fn_print_ok_eol_nl
  191. fi
  192. done
  193. # Correct addons directory structure for FastDL
  194. if [ -d "${fastdldir}/addons" ]; then
  195. echo -en "updating addons file structure..."
  196. cp -Rf "${fastdldir}"/addons/*/* "${fastdldir}"
  197. exitcode=$?
  198. if [ ${exitcode} -ne 0 ]; then
  199. fn_print_fail_eol_nl
  200. fn_script_log_fatal "updating addons file structure"
  201. core_exit.sh
  202. else
  203. fn_print_ok_eol_nl
  204. fn_script_log_pass "updating addons file structure"
  205. fi
  206. # Clear addons directory in fastdl
  207. if [ "${cleargmodaddons}" == "on" ]; then
  208. echo -en "clearing addons dir from fastdl dir..."
  209. sleep 1
  210. rm -R "${fastdldir:?}/addons"
  211. exitcode=$?
  212. if [ ${exitcode} -ne 0 ]; then
  213. fn_print_fail_eol_nl
  214. fn_script_log_fatal "clearing addons dir from fastdl dir"
  215. core_exit.sh
  216. else
  217. fn_print_ok_eol_nl
  218. fn_script_log_pass "clearing addons dir from fastdl dir"
  219. fi
  220. fi
  221. fi
  222. # Correct content that may be into a lua directory by mistake like some darkrpmodification addons
  223. if [ -d "${fastdldir}/lua" ]; then
  224. echo -en "correcting DarkRP files..."
  225. sleep 2
  226. cp -Rf "${fastdldir}/lua/"* "${fastdldir}"
  227. exitcode=$?
  228. if [ ${exitcode} -ne 0 ]; then
  229. fn_print_fail_eol_nl
  230. fn_script_log_fatal "correcting DarkRP files"
  231. core_exit.sh
  232. else
  233. fn_print_ok_eol_nl
  234. fn_script_log_pass "correcting DarkRP files"
  235. fi
  236. fi
  237. if [ -f "${tmpdir}/fastdl_files_to_compress.txt" ]; then
  238. totalfiles=$(wc -l < "${tmpdir}/fastdl_files_to_compress.txt")
  239. # Calculates total file size
  240. while read dufile; do
  241. filesize=$(du -b "${dufile}"| awk '{ print $1 }')
  242. filesizetotal=$(( ${filesizetotal} + ${filesize} ))
  243. done <"${tmpdir}/fastdl_files_to_compress.txt"
  244. fi
  245. if [ -z "${copyflag}" ]; then
  246. echo "about to compress ${totalfiles} files, total size $(fn_human_readable_file_size ${filesizetotal} 0)"
  247. fn_script_log_info "${totalfiles} files, total size $(fn_human_readable_file_size ${filesizetotal} 0)"
  248. rm "${tmpdir}/fastdl_files_to_compress.txt"
  249. if fn_prompt_yn "Continue?" Y; then
  250. copyflag=1
  251. fn_fastdl_gmod
  252. else
  253. core_exit.sh
  254. fi
  255. else
  256. if [ "${gamename}" == "Garry's Mod" ]; then
  257. fn_fastdl_gmod_lua_enforcer
  258. fi
  259. fn_fastdl_bzip2
  260. fi
  261. }
  262. # Using this gist https://gist.github.com/agunnerson-ibm/efca449565a3e7356906
  263. fn_human_readable_file_size() {
  264. local abbrevs=(
  265. $((1 << 60)):ZB
  266. $((1 << 50)):EB
  267. $((1 << 40)):TB
  268. $((1 << 30)):GB
  269. $((1 << 20)):MB
  270. $((1 << 10)):KB
  271. $((1)):bytes
  272. )
  273. local bytes="${1}"
  274. local precision="${2}"
  275. if [[ "${bytes}" == "1" ]]; then
  276. echo "1 byte"
  277. else
  278. for item in "${abbrevs[@]}"; do
  279. local factor="${item%:*}"
  280. local abbrev="${item#*:}"
  281. if [[ "${bytes}" -ge "${factor}" ]]; then
  282. local size="$(bc -l <<< "${bytes} / ${factor}")"
  283. printf "%.*f %s\n" "${precision}" "${size}" "${abbrev}"
  284. break
  285. fi
  286. done
  287. fi
  288. }
  289. fn_fastdl_source(){
  290. # Copy all needed files for FastDL
  291. if [ -n "${copyflag}" ]; then
  292. # Removes all existing FastDL files.
  293. if [ -d "${fastdldir}" ]; then
  294. echo -e "removing existing FastDL files"
  295. sleep 0.1
  296. fileswc=1
  297. totalfileswc=$(find "${fastdldir}" | wc -l)
  298. tput sc
  299. while read -r filetoremove; do
  300. tput rc; tput el
  301. printf "removing ${fileswc} / ${totalfileswc} : ${filetoremove}..."
  302. ((fileswc++))
  303. rm -rf "${filetoremove}"
  304. ((exitcode=$?))
  305. if [ ${exitcode} -ne 0 ]; then
  306. fn_script_log_fatal "Removing ${filetoremove}"
  307. break
  308. else
  309. fn_script_log_pass "Removing ${filetoremove}"
  310. fi
  311. sleep 0.01
  312. done < <(find "${fastdldir}")
  313. if [ ${exitcode} -ne 0 ]; then
  314. fn_print_fail_eol_nl
  315. core_exit.sh
  316. else
  317. fn_print_ok_eol_nl
  318. fi
  319. fi
  320. fn_fastdl_dirs
  321. echo -e "copying files to ${fastdldir}"
  322. fn_script_log_info "Copying files to ${fastdldir}"
  323. else
  324. if [ -f "${tmpdir}/fastdl_files_to_compress.txt" ]; then
  325. rm -f "${tmpdir}/fastdl_files_to_compress.txt"
  326. fi
  327. echo -e "analysing required files"
  328. fn_script_log_info "Analysing required files"
  329. fi
  330. local directorys_array=( "maps" "materials" "models" "particles" "sounds" "resources" )
  331. for directory in "${directorys_array[@]}"
  332. do
  333. if [ -d "${systemdir}/${directory}" ]; then
  334. if [ "${directory}" == "maps" ]; then
  335. local allowed_extentions_array=( "*.bsp" "*.ain" "*.nav" "*.jpg" "*.txt" )
  336. elif [ "${directory}" == "materials" ]; then
  337. local allowed_extentions_array=( "*.vtf" "*.vmt" "*.vbf" )
  338. elif [ "${directory}" == "particles" ]; then
  339. local allowed_extentions_array=( "*.pcf" )
  340. elif [ "${directory}" == "sounds" ]; then
  341. local allowed_extentions_array=( "*.vtx" "*.vvd" "*.mdl" "*.mdl" "*.phy" "*.jpg" "*.png" )
  342. fi
  343. for allowed_extention in "${allowed_extentions_array[@]}"
  344. do
  345. fileswc=0
  346. tput sc
  347. if [ -z "${copyflag}" ]; then
  348. tput rc; tput el
  349. printf "copying ${directory} ${allowed_extention} : ${fileswc}..."
  350. fi
  351. while read -r mapfile; do
  352. ((fileswc++))
  353. if [ -n "${copyflag}" ]; then
  354. tput rc; tput el
  355. printf "copying ${directory} ${allowed_extention} : ${fileswc}..."
  356. sleep 0.01
  357. if [ ! -d "${fastdldir}/${directory}" ]; then
  358. mkdir "${fastdldir}/${directory}"
  359. fi
  360. cp "${mapfile}" "${fastdldir}/${directory}"
  361. exitcode=$?
  362. if [ ${exitcode} -ne 0 ]; then
  363. fn_print_fail_eol_nl
  364. fn_script_log_fatal "Copying ${mapfile} > ${fastdldir}/${directory}"
  365. core_exit.sh
  366. else
  367. fn_script_log_pass "Copying ${mapfile} > ${fastdldir}/${directory}"
  368. fi
  369. else
  370. tput rc; tput el
  371. printf "gathering ${directory} ${allowed_extention} : ${fileswc}..."
  372. sleep 0.01
  373. echo "${mapfile}" >> "${tmpdir}/fastdl_files_to_compress.txt"
  374. fi
  375. done < <(find "${systemdir}/${directory}" -type f -iname ${allowed_extention})
  376. if [ -z "${copyflag}" ]; then
  377. tput rc; tput el
  378. printf "gathering ${directory} ${allowed_extention} : ${fileswc}..."
  379. fi
  380. if [ ${fileswc} != 0 ]&&[ -n "${copyflag}" ]||[ -z "${copyflag}" ]; then
  381. fn_print_ok_eol_nl
  382. fi
  383. done
  384. fi
  385. done
  386. if [ -f "${tmpdir}/fastdl_files_to_compress.txt" ]; then
  387. totalfiles=$(wc -l < "${tmpdir}/fastdl_files_to_compress.txt")
  388. # Calculates total file size
  389. while read dufile; do
  390. filesize=$(du -b "${dufile}"| awk '{ print $1 }')
  391. filesizetotal=$(( ${filesizetotal} + ${filesize} ))
  392. done <"${tmpdir}/fastdl_files_to_compress.txt"
  393. fi
  394. if [ -z "${copyflag}" ]; then
  395. echo "about to compress ${totalfiles} files, total size $(fn_human_readable_file_size ${filesizetotal} 0)"
  396. fn_script_log_info "${totalfiles} files, total size $(fn_human_readable_file_size ${filesizetotal} 0)"
  397. rm "${tmpdir}/fastdl_files_to_compress.txt"
  398. if fn_prompt_yn "Continue?" Y; then
  399. copyflag=1
  400. fn_fastdl_source
  401. else
  402. core_exit.sh
  403. fi
  404. else
  405. if [ "${gamename}" == "Garry's Mod" ]; then
  406. fn_fastdl_gmod_lua_enforcer
  407. fi
  408. fn_fastdl_bzip2
  409. fi
  410. }
  411. # Generate lua file that will force download any file into the FastDL directory
  412. fn_fastdl_gmod_lua_enforcer(){
  413. # Clear old lua file
  414. if [ -f "${luafastdlfullpath}" ]; then
  415. echo -en "removing existing download enforcer: ${luafastdlfile}..."
  416. rm "${luafastdlfullpath:?}"
  417. exitcode=$?
  418. if [ ${exitcode} -ne 0 ]; then
  419. fn_print_fail_eol_nl
  420. fn_script_log_fatal "removing existing download enforcer ${luafastdlfullpath}"
  421. core_exit.sh
  422. else
  423. fn_print_ok_eol_nl
  424. fn_script_log_pass "removing existing download enforcer ${luafastdlfullpath}"
  425. fi
  426. fi
  427. # Generate new one if user said yes
  428. if [ "${luaressource}" == "on" ]; then
  429. echo -en "creating new download enforcer: ${luafastdlfile}..."
  430. touch "${luafastdlfullpath}"
  431. # Read all filenames and put them into a lua file at the right path
  432. find "${fastdldir:?}" \( -type f ! -name "*.bz2" \) -printf '%P\n' | while read line; do
  433. echo "resource.AddFile( "\""${line}"\"" )" >> "${luafastdlfullpath}"
  434. done
  435. exitcode=$?
  436. if [ ${exitcode} -ne 0 ]; then
  437. fn_print_fail_eol_nl
  438. fn_script_log_fatal "creating new download enforcer ${luafastdlfullpath}"
  439. core_exit.sh
  440. else
  441. fn_print_ok_eol_nl
  442. fn_script_log_pass "creating new download enforcer ${luafastdlfullpath}"
  443. fi
  444. fi
  445. }
  446. fn_fastdl_bzip2(){
  447. while read -r filetocompress; do
  448. echo -en "compressing ${filetocompress}..."
  449. bzip2 -f "${filetocompress}"
  450. exitcode=$?
  451. if [ ${exitcode} -ne 0 ]; then
  452. fn_print_fail_eol_nl
  453. fn_script_log_fatal "compressing ${filetocompress}"
  454. core_exit.sh
  455. else
  456. fn_print_ok_eol_nl
  457. fn_script_log_pass "compressing ${filetocompress}"
  458. fi
  459. done < <(find "${fastdldir:?}" \( -type f ! -name "*.bz2" \))
  460. }
  461. fn_fastdl_completed(){
  462. # Finished message
  463. echo "FastDL files are located in:"
  464. echo "${webdir}"
  465. echo "FastDL completed"
  466. fn_script_log_info "FastDL completed"
  467. }
  468. # Run functions
  469. fn_print_header
  470. echo "More info: https://git.io/vyk9a"
  471. echo ""
  472. fn_fastdl_config
  473. if [ "${gamename}" == "Garry's Mod" ]; then
  474. fn_fastdl_gmod
  475. else
  476. fn_fastdl_source
  477. fi
  478. fn_fastdl_completed
  479. core_exit.sh