command_fastdl.sh 12 KB

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