command_fastdl.sh 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445
  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. # Initiates FastDL
  42. fn_fastdl_init(){
  43. fn_print_header
  44. fn_script_log "Started FastDL Generator"
  45. sleep 1
  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="true"
  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 "Created fastdl directory"
  74. sleep 1
  75. echo -en "\n"
  76. clearoldfastdl="off" # Nothing to clear
  77. elif [ "$(ls -A "${fastdldir}")" ]; then
  78. newfastdl="false"
  79. else
  80. newfastdl="true"
  81. fi
  82. }
  83. # Prompts user for FastDL creation settings
  84. fn_fastdl_config(){
  85. fn_print_info "Entering configuration"
  86. fn_script_log "Configuration"
  87. sleep 2
  88. echo -en "\n"
  89. # Prompt for clearing old files if directory was already here
  90. if [ "${newfastdl}" == "false" ]; then
  91. fn_print_dots
  92. if fn_prompt_yn "Clear old FastDL files?" Y; then
  93. clearoldfastdl="on"; fn_script_log "clearoldfastdl enabled"; fn_print_ok "Clearing Enabled"
  94. else
  95. clearoldfastdl="off"; fn_script_log "clearoldfastdl disabled"; fn_print_ok "Clearing Disabled"
  96. fi
  97. echo -en "\n"
  98. fi
  99. # Settings for bzip2 users
  100. if [ ${bzip2installed} == 1 ]; then
  101. # Prompt for using bzip2 if it's installed
  102. fn_print_dots
  103. if fn_prompt_yn "Enable bzip2 file compression?" Y; then
  104. bzip2enable="on"; fn_script_log "bzip2 enabled"; fn_print_ok "bzip2 Enabled"
  105. else
  106. bzip2enable="off"; fn_script_log "bzip2 disabled"; fn_print_ok "bzip2 Disabled"
  107. fi
  108. echo -en "\n"
  109. if [ "${gamename}" == "Garry's Mod" ]&&[ "${bzip2enable}" == "on" ]; then
  110. # Prompt for clearing uncompressed files, can save some space but might cause issues for gmod
  111. fn_print_dots
  112. if fn_prompt_yn "Clear non-bzip2 FastDL files?" Y; then
  113. clearnonbzip2="on"; fn_script_log "Clearing non-bzip2 files Enabled."; fn_print_ok "Clearing non-bzip2 files Enabled"
  114. else
  115. clearnonbzip2="off"; fn_script_log "Clearing non-bzip2 files Disabled."; fn_print_ok "Clearing non-bzip2 files Disabled"
  116. fi
  117. echo -en "\n"
  118. else
  119. # Other games default remove non bzip2 files
  120. clearnonbzip2="on"
  121. fn_script_log "Original uncompressed fastDL files won't be kept."
  122. fi
  123. fi
  124. # Garry's Mod Specific
  125. if [ "${gamename}" == "Garry's Mod" ]; then
  126. # Prompt to clear addons dir from fastdl, can use unnecessary space or be required depending on addon's file structures
  127. fn_print_dots
  128. if fn_prompt_yn "Clear addons dir from fastdl dir?" Y; then
  129. cleargmodaddons="on"; fn_script_log "Addons clearing Enabled."; fn_print_ok "Addons clearing Enabled"
  130. else
  131. cleargmodaddons="off"; fn_script_log "Addons clearing Disabled."; fn_print_ok "Addons clearing Disabled"
  132. fi
  133. echo -en "\n"
  134. # Prompt for download enforcer, which is using a .lua addfile resource generator
  135. fn_print_dots
  136. if fn_prompt_yn "Use client download enforcer?" Y; then
  137. luaressource="on"; fn_script_log "DL enforcer Enabled."; fn_print_ok "Enforcer Enabled"
  138. else
  139. luaressource="off"; fn_script_log "DL enforcer Disabled."; fn_print_ok "Enforcer Disabled"
  140. fi
  141. echo -en "\n"
  142. fi
  143. }
  144. fn_clear_old_fastdl(){
  145. # Clearing old FastDL if user answered yes
  146. if [ "${clearoldfastdl}" == "on" ]; then
  147. fn_print_info "Clearing existing FastDL directory"
  148. fn_script_log "Clearing existing FastDL directory"
  149. sleep 0.5
  150. rm -R "${fastdldir:?}"/*
  151. fn_print_ok "Old FastDL directory cleared"
  152. fn_script_log "Old FastDL directory cleared"
  153. sleep 1
  154. echo -en "\n"
  155. fi
  156. }
  157. fn_fastdl_gmod(){
  158. # Copy all needed files for FastDL
  159. echo ""
  160. fn_print_dots "Starting gathering all needed files"
  161. fn_script_log "Starting gathering all needed files"
  162. sleep 1
  163. echo -en "\n"
  164. # No choice to cd to the directory, as find can't then display relative directory
  165. cd "${systemdir}" || exit
  166. # Map Files
  167. fn_print_dots "Copying map files..."
  168. fn_script_log "Copying map files"
  169. sleep 0.5
  170. find . -name '*.bsp' | cpio --quiet -updm "${fastdldir}"
  171. find . -name '*.ain' | cpio --quiet -updm "${fastdldir}"
  172. fn_print_ok "Map files copied"
  173. sleep 0.5
  174. echo -en "\n"
  175. # Materials
  176. fn_print_dots "Copying materials..."
  177. fn_script_log "Copying materials"
  178. sleep 0.5
  179. find . -name '*.vtf' | cpio --quiet -updm "${fastdldir}"
  180. find . -name '*.vmt' | cpio --quiet -updm "${fastdldir}"
  181. fn_print_ok "Materials copied"
  182. sleep 0.5
  183. echo -en "\n"
  184. # Models
  185. fn_print_dots "Copying models..."
  186. fn_script_log "Copying models"
  187. sleep 1
  188. find . -name '*.vtx' | cpio --quiet -updm "${fastdldir}"
  189. find . -name '*.vvd' | cpio --quiet -updm "${fastdldir}"
  190. find . -name '*.mdl' | cpio --quiet -updm "${fastdldir}"
  191. find . -name '*.phy' | cpio --quiet -updm "${fastdldir}"
  192. fn_print_ok "Models copied"
  193. sleep 0.5
  194. echo -en "\n"
  195. # Particles
  196. fn_print_dots "Copying particles..."
  197. fn_script_log "Copying particles"
  198. sleep 0.5
  199. find . -name '*.pcf' | cpio --quiet -updm "${fastdldir}"
  200. fn_print_ok "Particles copied"
  201. sleep 0.5
  202. echo -en "\n"
  203. # Sounds
  204. fn_print_dots "Copying sounds..."
  205. fn_script_log "Copying sounds"
  206. sleep 0.5
  207. find . -name '*.wav' | cpio --quiet -updm "${fastdldir}"
  208. find . -name '*.mp3' | cpio --quiet -updm "${fastdldir}"
  209. find . -name '*.ogg' | cpio --quiet -updm "${fastdldir}"
  210. fn_print_ok "Sounds copied"
  211. sleep 0.5
  212. echo -en "\n"
  213. # Resources (mostly fonts)
  214. fn_print_dots "Copying fonts and png..."
  215. fn_script_log "Copying fonts and png"
  216. sleep 1
  217. find . -name '*.otf' | cpio --quiet -updm "${fastdldir}"
  218. find . -name '*.ttf' | cpio --quiet -updm "${fastdldir}"
  219. find . -name '*.png' | cpio --quiet -updm "${fastdldir}"
  220. fn_print_ok "Fonts and png copied"
  221. sleep 0.5
  222. echo -en "\n"
  223. # Going back to rootdir in order to prevent mistakes
  224. cd "${rootdir}"
  225. # Correct addons directory structure for FastDL
  226. if [ -d "${fastdldir}/addons" ]; then
  227. fn_print_info "Adjusting addons' file structure"
  228. fn_script_log "Adjusting addons' file structure"
  229. sleep 1
  230. cp -Rf "${fastdldir}"/addons/*/* "${fastdldir}"
  231. fn_print_ok "Adjusted addons' file structure"
  232. sleep 1
  233. echo -en "\n"
  234. # Clear addons directory in fastdl
  235. if [ "${cleargmodaddons}" == "on" ]; then
  236. fn_print_info "Clearing addons dir from fastdl dir"
  237. fn_script_log "Clearing addons dir from fastdl dir"
  238. sleep 1
  239. rm -R "${fastdldir:?}/addons"
  240. fn_print_ok "Cleared addons dir from fastdl dir"
  241. sleep 1
  242. echo -en "\n"
  243. fi
  244. fi
  245. # Correct content that may be into a lua directory by mistake like some darkrpmodification addons
  246. if [ -d "${fastdldir}/lua" ]; then
  247. fn_print_dots "Typical DarkRP files detected, fixing"
  248. sleep 2
  249. cp -Rf "${fastdldir}/lua/"* "${fastdldir}"
  250. fn_print_ok "Stupid DarkRP file structure fixed"
  251. sleep 2
  252. echo -en "\n"
  253. fi
  254. }
  255. fn_fastdl_source(){
  256. # Copy all needed files for FastDL
  257. echo ""
  258. fn_print_dots "Starting gathering all needed files"
  259. fn_script_log "Starting gathering all needed files"
  260. sleep 1
  261. echo -en "\n"
  262. # Map Files
  263. fn_print_dots "Copying map files..."
  264. fn_script_log "Copying map files"
  265. sleep 0.5
  266. mkdir "${fastdldir}/maps"
  267. find "${systemdir}/maps" -name '*.bsp' -exec cp {} "${fastdldir}/maps" \;
  268. find "${systemdir}/maps" -name '*.ain' -exec cp {} "${fastdldir}/maps" \;
  269. find "${systemdir}/maps" -name '*.nav' -exec cp {} "${fastdldir}/maps" \;
  270. find "${systemdir}/maps" -name '*.jpg' -exec cp {} "${fastdldir}/maps" \;
  271. find "${systemdir}/maps" -name '*.txt' -exec cp {} "${fastdldir}/maps" \;
  272. fn_print_ok "Map files copied"
  273. sleep 0.5
  274. echo -en "\n"
  275. # Materials
  276. fn_print_dots "Copying materials..."
  277. fn_script_log "Copying materials"
  278. sleep 0.5
  279. mkdir "${fastdldir}/materials"
  280. find "${systemdir}/materials" -name '*.vtf' -exec cp {} "${fastdldir}/materials" \;
  281. find "${systemdir}/materials" -name '*.vmt' -exec cp {} "${fastdldir}/materials" \;
  282. find "${systemdir}/materials" -name '*.vbf' -exec cp {} "${fastdldir}/materials" \;
  283. fn_print_ok "Materials copied"
  284. sleep 0.5
  285. echo -en "\n"
  286. # Models
  287. fn_print_dots "Copying models..."
  288. fn_script_log "Copying models"
  289. sleep 1
  290. mkdir "${fastdldir}/models"
  291. find "${systemdir}/models" -name '*.vtx' -exec cp {} "${fastdldir}/models" \;
  292. find "${systemdir}/models" -name '*.vvd' -exec cp {} "${fastdldir}/models" \;
  293. find "${systemdir}/models" -name '*.mdl' -exec cp {} "${fastdldir}/models" \;
  294. find "${systemdir}/models" -name '*.phy' -exec cp {} "${fastdldir}/models" \;
  295. find "${systemdir}/models" -name '*.jpg' -exec cp {} "${fastdldir}/models" \;
  296. find "${systemdir}/models" -name '*.png' -exec cp {} "${fastdldir}/models" \;
  297. fn_print_ok "Models copied"
  298. sleep 0.5
  299. echo -en "\n"
  300. # Particles
  301. fn_print_dots "Copying particles..."
  302. fn_script_log "Copying particles"
  303. sleep 0.5
  304. mkdir "${fastdldir}/particles"
  305. find "${systemdir}" -name '*.pcf' -exec cp {} "${fastdldir}/particles" \;
  306. fn_print_ok "Particles copied"
  307. sleep 0.5
  308. echo -en "\n"
  309. # Sounds
  310. fn_print_dots "Copying sounds..."
  311. fn_script_log "Copying sounds"
  312. sleep 0.5
  313. mkdir "${fastdldir}/sound"
  314. find "${systemdir}" -name '*.wav' -exec cp {} "${fastdldir}/sound" \;
  315. find "${systemdir}" -name '*.mp3' -exec cp {} "${fastdldir}/sound" \;
  316. find "${systemdir}" -name '*.ogg' -exec cp {} "${fastdldir}/sound" \;
  317. fn_print_ok "Sounds copied"
  318. sleep 0.5
  319. echo -en "\n"
  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. echo ""
  325. if [ "${luaressource}" == "off" ]; then
  326. if [ -f "${luafastdlfullpath}" ]; then
  327. fn_print_dots "Removing download enforcer"
  328. sleep 1
  329. rm -R "${luafastdlfullpath:?}"
  330. fn_print_ok "Removed download enforcer"
  331. fn_script_log "Removed old download inforcer"
  332. echo -en "\n"
  333. sleep 2
  334. fi
  335. fi
  336. # Remove old lua file and generate a new one if user said yes
  337. if [ "${luaressource}" == "on" ]; then
  338. if [ -f "${luafastdlfullpath}" ]; then
  339. fn_print_dots "Removing old download enforcer"
  340. sleep 1
  341. rm "${luafastdlfullpath}"
  342. fn_print_ok "Removed old download enforcer"
  343. fn_script_log "Removed old download enforcer"
  344. echo -en "\n"
  345. sleep 1
  346. fi
  347. fn_print_dots "Generating new download enforcer"
  348. fn_script_log "Generating new download enforcer"
  349. sleep 1
  350. # Read all filenames and put them into a lua file at the right path
  351. find "${fastdldir:?}" \( -type f ! -name "*.bz2" \) -printf '%P\n' | while read line; do
  352. echo "resource.AddFile( "\""${line}"\"" )" >> "${luafastdlfullpath}"
  353. done
  354. fn_print_ok "Download enforcer generated"
  355. fn_script_log "Download enforcer generated"
  356. echo -en "\n"
  357. echo ""
  358. sleep 2
  359. fi
  360. }
  361. fn_fastdl_bzip2(){
  362. # Compressing using bzip2 if user said yes
  363. echo ""
  364. if [ ${bzip2enable} == "on" ]; then
  365. fn_print_info "Have a break, this step could take a while..."
  366. echo -en "\n"
  367. echo ""
  368. fn_print_dots "Compressing files using bzip2..."
  369. fn_script_log "Compressing files using bzip2..."
  370. # bzip2 all files that are not already compressed (keeping original files)
  371. find "${fastdldir:?}" \( -type f ! -name "*.bz2" \) -exec bzip2 -qk \{\} \;
  372. fn_print_ok "bzip2 compression done"
  373. fn_script_log "bzip2 compression done"
  374. sleep 1
  375. echo -en "\n"
  376. # Clear non compressed FastDL files
  377. if [ "${clearnonbzip2}" == "on" ]; then
  378. fn_print_dots "Clearing original uncompressed FastDL files..."
  379. sleep 1
  380. find "${fastdldir:?}" \( -type f ! -name "*.bz2" \) -exec rm {} \;
  381. fn_print_ok "Cleared uncompressed FastDL files"
  382. fn_script_log "Cleared uncompressed FastDL files."
  383. fi
  384. fi
  385. }
  386. fn_fastdl_completed(){
  387. # Finished message
  388. echo ""
  389. fn_print_ok "FastDL created!"
  390. fn_script_log "FastDL job done"
  391. sleep 2
  392. echo -en "\n"
  393. echo ""
  394. fn_print_info_nl "Need more documentation?"
  395. echo " * https://github.com/GameServerManagers/LinuxGSM/wiki/FastDL"
  396. echo -en "\n"
  397. if [ "$bzip2installed" == "0" ]; then
  398. echo "By the way, you'd better install bzip2 and re-run this command!"
  399. fi
  400. echo "Credits: UltimateByte"
  401. }
  402. # Run functions
  403. fn_check_bzip2
  404. fn_fastdl_init
  405. fn_fastdl_config
  406. fn_clear_old_fastdl
  407. if [ "${gamename}" == "Garry's Mod" ]; then
  408. fn_fastdl_gmod
  409. fn_fastdl_gmod_lua_enforcer
  410. else
  411. fn_fastdl_source
  412. fi
  413. fn_fastdl_bzip2
  414. fn_fastdl_completed
  415. core_exit.sh