command_fastdl.sh 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  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 Generator"
  9. local function_selfname="$(basename $(readlink -f "${BASH_SOURCE[0]}"))"
  10. check.sh
  11. # Only Source supports FastDL
  12. if [ "${engine}" != "source" ]; then
  13. fn_print_error "${gamename} does not support FastDL"
  14. fn_script_log_error "${gamename} does not support FastDL"
  15. core_exit.sh
  16. fi
  17. # Directories
  18. webdir="${rootdir}/public_html"
  19. fastdldir="${webdir}/fastdl"
  20. addonsdir="${systemdir}/addons"
  21. # Server lua autorun dir, used to autorun lua on client connect to the server
  22. luasvautorundir="${systemdir}/lua/autorun/server"
  23. luafastdlfile="lgsm_cl_force_fastdl.lua"
  24. luafastdlfullpath="${luasvautorundir}/${luafastdlfile}"
  25. fn_check_bzip2(){
  26. # Returns true if not installed
  27. if [ -z "$(command -v bzip2)" ]; then
  28. bzip2installed="0"
  29. fn_print_info "bzip2 is not installed! Install it to enable file compression"
  30. fn_script_log_info "bzip2 is not installed. Install it to enable file compression."
  31. fn_script_log_info "https://github.com/GameServerManagers/LinuxGSM/wiki/FastDL#bzip2-compression"
  32. echo -en "\n"
  33. sleep 1
  34. echo " * https://github.com/GameServerManagers/LinuxGSM/wiki/FastDL#bzip2-compression"
  35. sleep 2
  36. echo ""
  37. else
  38. bzip2installed="1"
  39. fi
  40. }
  41. fn_fastdl_init(){
  42. fn_print_header
  43. fn_script_log "Started FastDL Generator"
  44. sleep 1
  45. echo -en "\n"
  46. fn_check_bzip2
  47. # User confirmation
  48. if ! fn_prompt_yn "Build FastDL directory?" Y; then
  49. exit
  50. fi
  51. fn_script_log "Initiating FastDL creation"
  52. # Check and create directories
  53. if [ ! -d "${webdir}" ]; then
  54. echo ""
  55. fn_print_info "Creating FastDL directories"
  56. echo -en "\n"
  57. sleep 1
  58. fn_print_dots "Creating ${webdir} directory"
  59. sleep 0.5
  60. mkdir "${webdir}"
  61. fn_print_ok "Created ${webdir} directory"
  62. fn_script_log "FastDL created ${webdir} directory"
  63. sleep 1
  64. echo -en "\n"
  65. fi
  66. if [ ! -d "${fastdldir}" ]; then
  67. # No directory, won't ask for removing old ones
  68. newfastdl=1
  69. fn_print_dots "Creating fastdl directory"
  70. sleep 0.5
  71. mkdir "${fastdldir}"
  72. fn_print_ok "Created fastdl directory"
  73. fn_script_log "FastDL created fastdl directory"
  74. sleep 1
  75. echo -en "\n"
  76. clearoldfastdl="off" # Nothing to clear
  77. elif [ "$(ls -A "${fastdldir}")" ]; then
  78. newfastdl=0
  79. fi
  80. }
  81. fn_fastdl_config(){
  82. # Global settings for FastDL creation
  83. fn_print_info "Entering configuration"
  84. fn_script_log "Configuration"
  85. sleep 2
  86. echo -en "\n"
  87. # Prompt for clearing old files if directory was already here
  88. if [ -n "${newfastdl}" ] && [ "${newfastdl}" == "0" ]; then
  89. fn_print_dots
  90. if fn_prompt_yn "Clear old FastDL files?" Y; then
  91. clearoldfastdl="on"; fn_script_log "clearoldfastdl enabled"; fn_print_ok "Clearing Enabled"
  92. else
  93. clearoldfastdl="off"; fn_script_log "clearoldfastdl disabled"; fn_print_ok "Clearing Disabled"
  94. fi
  95. echo -en "\n"
  96. fi
  97. # Prompt for using bzip2 if it's installed
  98. if [ ${bzip2installed} == 1 ]; then
  99. fn_print_dots
  100. if fn_prompt_yn "Enable file compression using bzip2?" Y; then
  101. bzip2enable="on"; fn_script_log "bzip2 enabled"; fn_print_ok "bzip2 Enabled"
  102. else
  103. bzip2enable="off"; fn_script_log "bzip2 disabled"; fn_print_ok "bzip2 Disabled"
  104. fi
  105. echo -en "\n"
  106. fi
  107. }
  108. fn_fastdl_gmod_luaenforcer(){
  109. # Prompt for download enforcer, that is using a .lua addfile resource generator
  110. fn_print_dots
  111. if fn_prompt_yn "Use client download enforcer?" Y; then
  112. luaressource="on"; fn_script_log "DL enforcer Enabled"; fn_print_ok "Enforcer Enabled"
  113. else
  114. luaressource="off"; fn_script_log "DL enforcer Disabled"; fn_print_ok "Enforcer Disabled"
  115. fi
  116. echo -en "\n"
  117. }
  118. fn_clear_old_fastdl(){
  119. # Clearing old FastDL if user answered yes
  120. if [ "${clearoldfastdl}" == "on" ]; then
  121. fn_print_info "Clearing existing FastDL directory"
  122. fn_script_log "Clearing existing FastDL directory"
  123. sleep 0.5
  124. rm -R "${fastdldir:?}"/*
  125. fn_print_ok "Old FastDL directory cleared"
  126. fn_script_log "Old FastDL directory cleared"
  127. sleep 1
  128. echo -en "\n"
  129. fi
  130. }
  131. fn_fastdl_gmod(){
  132. # Copy all needed files for FastDL
  133. echo ""
  134. fn_print_dots "Starting gathering all needed files"
  135. fn_script_log "Starting gathering all needed files"
  136. sleep 1
  137. echo -en "\n"
  138. # No choice to cd to the directory, as find can't then display relative directory
  139. cd "${systemdir}"
  140. # Map Files
  141. fn_print_dots "Copying map files..."
  142. fn_script_log "Copying map files"
  143. sleep 0.5
  144. find . -name '*.bsp' | cpio --quiet -updm "${fastdldir}"
  145. find . -name '*.ain' | cpio --quiet -updm "${fastdldir}"
  146. fn_print_ok "Map files copied"
  147. sleep 0.5
  148. echo -en "\n"
  149. # Materials
  150. fn_print_dots "Copying materials..."
  151. fn_script_log "Copying materials"
  152. sleep 0.5
  153. find . -name '*.vtf' | cpio --quiet -updm "${fastdldir}"
  154. find . -name '*.vmt' | cpio --quiet -updm "${fastdldir}"
  155. fn_print_ok "Materials copied"
  156. sleep 0.5
  157. echo -en "\n"
  158. # Models
  159. fn_print_dots "Copying models..."
  160. fn_script_log "Copying models"
  161. sleep 1
  162. find . -name '*.vtx' | cpio --quiet -updm "${fastdldir}"
  163. find . -name '*.vvd' | cpio --quiet -updm "${fastdldir}"
  164. find . -name '*.mdl' | cpio --quiet -updm "${fastdldir}"
  165. find . -name '*.phy' | cpio --quiet -updm "${fastdldir}"
  166. fn_print_ok "Models copied"
  167. sleep 0.5
  168. echo -en "\n"
  169. # Particles
  170. fn_print_dots "Copying particles..."
  171. fn_script_log "Copying particles"
  172. sleep 0.5
  173. find . -name '*.pcf' | cpio --quiet -updm "${fastdldir}"
  174. fn_print_ok "Particles copied"
  175. sleep 0.5
  176. echo -en "\n"
  177. # Sounds
  178. fn_print_dots "Copying sounds..."
  179. fn_script_log "Copying sounds"
  180. sleep 0.5
  181. find . -name '*.wav' | cpio --quiet -updm "${fastdldir}"
  182. find . -name '*.mp3' | cpio --quiet -updm "${fastdldir}"
  183. find . -name '*.ogg' | cpio --quiet -updm "${fastdldir}"
  184. fn_print_ok "Sounds copied"
  185. sleep 0.5
  186. echo -en "\n"
  187. # Resources (mostly fonts)
  188. fn_print_dots "Copying fonts and png..."
  189. fn_script_log "Copying fonts and png"
  190. sleep 1
  191. find . -name '*.otf' | cpio --quiet -updm "${fastdldir}"
  192. find . -name '*.ttf' | cpio --quiet -updm "${fastdldir}"
  193. find . -name '*.png' | cpio --quiet -updm "${fastdldir}"
  194. fn_print_ok "Fonts and png copied"
  195. sleep 0.5
  196. echo -en "\n"
  197. # Going back to rootdir in order to prevent mistakes
  198. cd "${rootdir}"
  199. # Correct addons directory structure for FastDL
  200. if [ -d "${fastdldir}/addons" ]; then
  201. fn_print_info "Adjusting addons' file structure"
  202. fn_script_log "Adjusting addon's file structure"
  203. sleep 1
  204. cp -Rf "${fastdldir}"/addons/*/* "${fastdldir}"
  205. # Don't remove yet rm -R "${fastdldir:?}/addons"
  206. fn_print_ok "Adjusted addon's file structure"
  207. sleep 1
  208. echo -en "\n"
  209. fi
  210. # Correct content that may be into a lua directory by mistake like some darkrpmodification addons
  211. if [ -d "${fastdldir}/lua" ]; then
  212. fn_print_dots "Typical DarkRP files detected, fixing"
  213. sleep 2
  214. cp -Rf "${fastdldir}/lua/"* "${fastdldir}"
  215. fn_print_ok "Stupid DarkRP file structure fixed"
  216. sleep 2
  217. echo -en "\n"
  218. fi
  219. }
  220. fn_fastdl_source(){
  221. # Copy all needed files for FastDL
  222. echo ""
  223. fn_print_dots "Starting gathering all needed files"
  224. fn_script_log "Starting gathering all needed files"
  225. sleep 1
  226. echo -en "\n"
  227. # Map Files
  228. fn_print_dots "Copying map files..."
  229. fn_script_log "Copying map files"
  230. sleep 0.5
  231. mkdir "${fastdldir}/maps"
  232. find "${systemdir}/maps" -name '*.bsp' -exec cp {} "${fastdldir}/maps" \;
  233. find "${systemdir}/maps" -name '*.ain' -exec cp {} "${fastdldir}/maps" \;
  234. find "${systemdir}/maps" -name '*.nav' -exec cp {} "${fastdldir}/maps" \;
  235. find "${systemdir}/maps" -name '*.jpg' -exec cp {} "${fastdldir}/maps" \;
  236. find "${systemdir}/maps" -name '*.txt' -exec cp {} "${fastdldir}/maps" \;
  237. fn_print_ok "Map files copied"
  238. sleep 0.5
  239. echo -en "\n"
  240. # Materials
  241. fn_print_dots "Copying materials..."
  242. fn_script_log "Copying materials"
  243. sleep 0.5
  244. mkdir "${fastdldir}/materials"
  245. find "${systemdir}/materials" -name '*.vtf' -exec cp {} "${fastdldir}/materials" \;
  246. find "${systemdir}/materials" -name '*.vmt' -exec cp {} "${fastdldir}/materials" \;
  247. find "${systemdir}/materials" -name '*.vbf' -exec cp {} "${fastdldir}/materials" \;
  248. fn_print_ok "Materials copied"
  249. sleep 0.5
  250. echo -en "\n"
  251. # Models
  252. fn_print_dots "Copying models..."
  253. fn_script_log "Copying models"
  254. sleep 1
  255. mkdir "${fastdldir}/models"
  256. find "${systemdir}/models" -name '*.vtx' -exec cp {} "${fastdldir}/models" \;
  257. find "${systemdir}/models" -name '*.vvd' -exec cp {} "${fastdldir}/models" \;
  258. find "${systemdir}/models" -name '*.mdl' -exec cp {} "${fastdldir}/models" \;
  259. find "${systemdir}/models" -name '*.phy' -exec cp {} "${fastdldir}/models" \;
  260. find "${systemdir}/models" -name '*.jpg' -exec cp {} "${fastdldir}/models" \;
  261. find "${systemdir}/models" -name '*.png' -exec cp {} "${fastdldir}/models" \;
  262. fn_print_ok "Models copied"
  263. sleep 0.5
  264. echo -en "\n"
  265. # Particles
  266. fn_print_dots "Copying particles..."
  267. fn_script_log "Copying particles"
  268. sleep 0.5
  269. mkdir "${fastdldir}/particles"
  270. find "${systemdir}" -name '*.pcf' -exec cp {} "${fastdldir}/particles" \;
  271. fn_print_ok "Particles copied"
  272. sleep 0.5
  273. echo -en "\n"
  274. # Sounds
  275. fn_print_dots "Copying sounds..."
  276. fn_script_log "Copying sounds"
  277. sleep 0.5
  278. mkdir "${fastdldir}/sound"
  279. find "${systemdir}" -name '*.wav' -exec cp {} "${fastdldir}/sound" \;
  280. find "${systemdir}" -name '*.mp3' -exec cp {} "${fastdldir}/sound" \;
  281. find "${systemdir}" -name '*.ogg' -exec cp {} "${fastdldir}/sound" \;
  282. fn_print_ok "Sounds copied"
  283. sleep 0.5
  284. echo -en "\n"
  285. }
  286. # Generate lua file that will force download any file into the FastDL directory
  287. fn_fastdl_gmod_lua(){
  288. # Remove lua file if luaressource is turned off and file exists
  289. echo ""
  290. if [ "${luaressource}" == "off" ]; then
  291. if [ -f "${luafastdlfullpath}" ]; then
  292. fn_print_dots "Removing download enforcer"
  293. sleep 1
  294. rm -R "${luafastdlfullpath:?}"
  295. fn_print_ok "Removed download enforcer"
  296. fn_script_log "Removed old download inforcer"
  297. echo -en "\n"
  298. sleep 2
  299. fi
  300. fi
  301. # Remove old lua file and generate a new one if user said yes
  302. if [ "${luaressource}" == "on" ]; then
  303. if [ -f "${luafastdlfullpath}" ]; then
  304. fn_print_dots "Removing old download enforcer"
  305. sleep 1
  306. rm "${luafastdlfullpath}"
  307. fn_print_ok "Removed old download enforcer"
  308. fn_script_log "Removed old download enforcer"
  309. echo -en "\n"
  310. sleep 1
  311. fi
  312. fn_print_dots "Generating new download enforcer"
  313. fn_script_log "Generating new download enforcer"
  314. sleep 1
  315. # Read all filenames and put them into a lua file at the right path
  316. find "${fastdldir}" \( -type f ! -name "*.bz2" \) -printf '%P\n' | while read line; do
  317. echo "resource.AddFile( "\""${line}"\"" )" >> ${luafastdlfullpath}
  318. done
  319. fn_print_ok "Download enforcer generated"
  320. fn_script_log "Download enforcer generated"
  321. echo -en "\n"
  322. echo ""
  323. sleep 2
  324. fi
  325. }
  326. fn_fastdl_bzip2(){
  327. # Compressing using bzip2 if user said yes
  328. echo ""
  329. if [ ${bzip2enable} == "on" ]; then
  330. fn_print_info "Have a break, this step could take a while..."
  331. echo -en "\n"
  332. echo ""
  333. fn_print_dots "Compressing files using bzip2..."
  334. fn_script_log "Compressing files using bzip2..."
  335. # bzip2 all files that are not already compressed (keeping original files)
  336. find "${fastdldir}" \( -type f ! -name "*.bz2" \) -exec bzip2 -qk \{\} \;
  337. fn_print_ok "bzip2 compression done"
  338. fn_script_log "bzip2 compression done"
  339. sleep 1
  340. echo -en "\n"
  341. # Prompt for clearing uncompressed files, can save some space but might cause issues for gmod
  342. fn_print_dots
  343. if [ "${gamename}" == "Garry's Mod" ]; then
  344. if fn_prompt_yn "Keep original/uncompressed FastDL files?" N; then
  345. fn_print_info "Keeping uncompressed FastDL files"; fn_script_log fn_print_info "Keeping uncompressed FastDL files"
  346. else
  347. find "${fastdldir}" \( -type f ! -name "*.bz2" \) -exec rm {} \; fn_print_dots "Clearing uncompressed FastDL files..."; sleep 1; fn_print_ok "Cleared uncompressed FastDL files"; fn_script_log "Cleared uncompressed FastDL files."
  348. fi
  349. echo -en "\n"
  350. else
  351. fn_print_dots "Clearing uncompressed FastDL files..."
  352. sleep 1
  353. find "${fastdldir}" \( -type f ! -name "*.bz2" \) -exec rm {} \
  354. fn_print_ok "Cleared uncompressed FastDL files."
  355. fn_script_log "Cleared uncompressed FastDL files."
  356. fi
  357. fi
  358. }
  359. fn_fastdl_completed(){
  360. # Finished message
  361. echo ""
  362. fn_print_ok "FastDL created!"
  363. fn_script_log "FastDL job done"
  364. sleep 2
  365. echo -en "\n"
  366. echo ""
  367. fn_print_info_nl "Need more documentation?
  368. echo " * https://github.com/GameServerManagers/LinuxGSM/wiki/FastDL"
  369. echo -en "\n"
  370. if [ "$bzip2installed" == "0" ]; then
  371. echo "By the way, you'd better install bzip2 and re-run this command!"
  372. fi
  373. echo "Credits: UltimateByte"
  374. }
  375. # Run functions
  376. fn_check_bzip2
  377. fn_fastdl_init
  378. fn_fastdl_config
  379. fn_clear_old_fastdl
  380. if [ "${gamename}" == "Garry's Mod" ]; then
  381. fn_fastdl_gmod_luaenforcer
  382. fn_fastdl_gmod
  383. fn_fastdl_gmod_lua
  384. else
  385. fn_fastdl_source
  386. fi
  387. fn_fastdl_bzip2
  388. fn_fastdl_completed
  389. core_exit.sh