command_fastdl.sh 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  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 "Keep original uncompressed FastDL files?" N; then
  113. clearnonbzip2="off"; fn_script_log "Original uncompressed fastDL files will be kept."; fn_print_ok "Original uncompressed fastDL files will be kept"
  114. else
  115. clearnonbzip2="on"; fn_script_log "Original uncompressed fastDL files won't be kept."; fn_print_ok "Original uncompressed fastDL files won't be kept"
  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 for download enforcer, which is using a .lua addfile resource generator
  127. fn_print_dots
  128. if fn_prompt_yn "Use client download enforcer?" Y; then
  129. luaressource="on"; fn_script_log "DL enforcer Enabled"; fn_print_ok "Enforcer Enabled"
  130. else
  131. luaressource="off"; fn_script_log "DL enforcer Disabled"; fn_print_ok "Enforcer Disabled"
  132. fi
  133. echo -en "\n"
  134. fi
  135. }
  136. fn_clear_old_fastdl(){
  137. # Clearing old FastDL if user answered yes
  138. if [ "${clearoldfastdl}" == "on" ]; then
  139. fn_print_info "Clearing existing FastDL directory"
  140. fn_script_log "Clearing existing FastDL directory"
  141. sleep 0.5
  142. rm -R "${fastdldir:?}"/*
  143. fn_print_ok "Old FastDL directory cleared"
  144. fn_script_log "Old FastDL directory cleared"
  145. sleep 1
  146. echo -en "\n"
  147. fi
  148. }
  149. fn_fastdl_gmod(){
  150. # Copy all needed files for FastDL
  151. echo ""
  152. fn_print_dots "Starting gathering all needed files"
  153. fn_script_log "Starting gathering all needed files"
  154. sleep 1
  155. echo -en "\n"
  156. # No choice to cd to the directory, as find can't then display relative directory
  157. cd "${systemdir}" || exit
  158. # Map Files
  159. fn_print_dots "Copying map files..."
  160. fn_script_log "Copying map files"
  161. sleep 0.5
  162. find . -name '*.bsp' | cpio --quiet -updm "${fastdldir}"
  163. find . -name '*.ain' | cpio --quiet -updm "${fastdldir}"
  164. fn_print_ok "Map files copied"
  165. sleep 0.5
  166. echo -en "\n"
  167. # Materials
  168. fn_print_dots "Copying materials..."
  169. fn_script_log "Copying materials"
  170. sleep 0.5
  171. find . -name '*.vtf' | cpio --quiet -updm "${fastdldir}"
  172. find . -name '*.vmt' | cpio --quiet -updm "${fastdldir}"
  173. fn_print_ok "Materials copied"
  174. sleep 0.5
  175. echo -en "\n"
  176. # Models
  177. fn_print_dots "Copying models..."
  178. fn_script_log "Copying models"
  179. sleep 1
  180. find . -name '*.vtx' | cpio --quiet -updm "${fastdldir}"
  181. find . -name '*.vvd' | cpio --quiet -updm "${fastdldir}"
  182. find . -name '*.mdl' | cpio --quiet -updm "${fastdldir}"
  183. find . -name '*.phy' | cpio --quiet -updm "${fastdldir}"
  184. fn_print_ok "Models copied"
  185. sleep 0.5
  186. echo -en "\n"
  187. # Particles
  188. fn_print_dots "Copying particles..."
  189. fn_script_log "Copying particles"
  190. sleep 0.5
  191. find . -name '*.pcf' | cpio --quiet -updm "${fastdldir}"
  192. fn_print_ok "Particles copied"
  193. sleep 0.5
  194. echo -en "\n"
  195. # Sounds
  196. fn_print_dots "Copying sounds..."
  197. fn_script_log "Copying sounds"
  198. sleep 0.5
  199. find . -name '*.wav' | cpio --quiet -updm "${fastdldir}"
  200. find . -name '*.mp3' | cpio --quiet -updm "${fastdldir}"
  201. find . -name '*.ogg' | cpio --quiet -updm "${fastdldir}"
  202. fn_print_ok "Sounds copied"
  203. sleep 0.5
  204. echo -en "\n"
  205. # Resources (mostly fonts)
  206. fn_print_dots "Copying fonts and png..."
  207. fn_script_log "Copying fonts and png"
  208. sleep 1
  209. find . -name '*.otf' | cpio --quiet -updm "${fastdldir}"
  210. find . -name '*.ttf' | cpio --quiet -updm "${fastdldir}"
  211. find . -name '*.png' | cpio --quiet -updm "${fastdldir}"
  212. fn_print_ok "Fonts and png copied"
  213. sleep 0.5
  214. echo -en "\n"
  215. # Going back to rootdir in order to prevent mistakes
  216. cd "${rootdir}"
  217. # Correct addons directory structure for FastDL
  218. if [ -d "${fastdldir}/addons" ]; then
  219. fn_print_info "Adjusting addons' file structure"
  220. fn_script_log "Adjusting addons' file structure"
  221. sleep 1
  222. cp -Rf "${fastdldir}"/addons/*/* "${fastdldir}"
  223. # Don't remove yet rm -R "${fastdldir:?}/addons"
  224. fn_print_ok "Adjusted addons' file structure"
  225. sleep 1
  226. echo -en "\n"
  227. fi
  228. # Correct content that may be into a lua directory by mistake like some darkrpmodification addons
  229. if [ -d "${fastdldir}/lua" ]; then
  230. fn_print_dots "Typical DarkRP files detected, fixing"
  231. sleep 2
  232. cp -Rf "${fastdldir}/lua/"* "${fastdldir}"
  233. fn_print_ok "Stupid DarkRP file structure fixed"
  234. sleep 2
  235. echo -en "\n"
  236. fi
  237. }
  238. fn_fastdl_source(){
  239. # Copy all needed files for FastDL
  240. echo ""
  241. fn_print_dots "Starting gathering all needed files"
  242. fn_script_log "Starting gathering all needed files"
  243. sleep 1
  244. echo -en "\n"
  245. # Map Files
  246. fn_print_dots "Copying map files..."
  247. fn_script_log "Copying map files"
  248. sleep 0.5
  249. mkdir "${fastdldir}/maps"
  250. find "${systemdir}/maps" -name '*.bsp' -exec cp {} "${fastdldir}/maps" \;
  251. find "${systemdir}/maps" -name '*.ain' -exec cp {} "${fastdldir}/maps" \;
  252. find "${systemdir}/maps" -name '*.nav' -exec cp {} "${fastdldir}/maps" \;
  253. find "${systemdir}/maps" -name '*.jpg' -exec cp {} "${fastdldir}/maps" \;
  254. find "${systemdir}/maps" -name '*.txt' -exec cp {} "${fastdldir}/maps" \;
  255. fn_print_ok "Map files copied"
  256. sleep 0.5
  257. echo -en "\n"
  258. # Materials
  259. fn_print_dots "Copying materials..."
  260. fn_script_log "Copying materials"
  261. sleep 0.5
  262. mkdir "${fastdldir}/materials"
  263. find "${systemdir}/materials" -name '*.vtf' -exec cp {} "${fastdldir}/materials" \;
  264. find "${systemdir}/materials" -name '*.vmt' -exec cp {} "${fastdldir}/materials" \;
  265. find "${systemdir}/materials" -name '*.vbf' -exec cp {} "${fastdldir}/materials" \;
  266. fn_print_ok "Materials copied"
  267. sleep 0.5
  268. echo -en "\n"
  269. # Models
  270. fn_print_dots "Copying models..."
  271. fn_script_log "Copying models"
  272. sleep 1
  273. mkdir "${fastdldir}/models"
  274. find "${systemdir}/models" -name '*.vtx' -exec cp {} "${fastdldir}/models" \;
  275. find "${systemdir}/models" -name '*.vvd' -exec cp {} "${fastdldir}/models" \;
  276. find "${systemdir}/models" -name '*.mdl' -exec cp {} "${fastdldir}/models" \;
  277. find "${systemdir}/models" -name '*.phy' -exec cp {} "${fastdldir}/models" \;
  278. find "${systemdir}/models" -name '*.jpg' -exec cp {} "${fastdldir}/models" \;
  279. find "${systemdir}/models" -name '*.png' -exec cp {} "${fastdldir}/models" \;
  280. fn_print_ok "Models copied"
  281. sleep 0.5
  282. echo -en "\n"
  283. # Particles
  284. fn_print_dots "Copying particles..."
  285. fn_script_log "Copying particles"
  286. sleep 0.5
  287. mkdir "${fastdldir}/particles"
  288. find "${systemdir}" -name '*.pcf' -exec cp {} "${fastdldir}/particles" \;
  289. fn_print_ok "Particles copied"
  290. sleep 0.5
  291. echo -en "\n"
  292. # Sounds
  293. fn_print_dots "Copying sounds..."
  294. fn_script_log "Copying sounds"
  295. sleep 0.5
  296. mkdir "${fastdldir}/sound"
  297. find "${systemdir}" -name '*.wav' -exec cp {} "${fastdldir}/sound" \;
  298. find "${systemdir}" -name '*.mp3' -exec cp {} "${fastdldir}/sound" \;
  299. find "${systemdir}" -name '*.ogg' -exec cp {} "${fastdldir}/sound" \;
  300. fn_print_ok "Sounds copied"
  301. sleep 0.5
  302. echo -en "\n"
  303. }
  304. # Generate lua file that will force download any file into the FastDL directory
  305. fn_fastdl_gmod_lua_enforcer(){
  306. # Remove lua file if luaressource is turned off and file exists
  307. echo ""
  308. if [ "${luaressource}" == "off" ]; then
  309. if [ -f "${luafastdlfullpath}" ]; then
  310. fn_print_dots "Removing download enforcer"
  311. sleep 1
  312. rm -R "${luafastdlfullpath:?}"
  313. fn_print_ok "Removed download enforcer"
  314. fn_script_log "Removed old download inforcer"
  315. echo -en "\n"
  316. sleep 2
  317. fi
  318. fi
  319. # Remove old lua file and generate a new one if user said yes
  320. if [ "${luaressource}" == "on" ]; then
  321. if [ -f "${luafastdlfullpath}" ]; then
  322. fn_print_dots "Removing old download enforcer"
  323. sleep 1
  324. rm "${luafastdlfullpath}"
  325. fn_print_ok "Removed old download enforcer"
  326. fn_script_log "Removed old download enforcer"
  327. echo -en "\n"
  328. sleep 1
  329. fi
  330. fn_print_dots "Generating new download enforcer"
  331. fn_script_log "Generating new download enforcer"
  332. sleep 1
  333. # Read all filenames and put them into a lua file at the right path
  334. find "${fastdldir:?}" \( -type f ! -name "*.bz2" \) -printf '%P\n' | while read line; do
  335. echo "resource.AddFile( "\""${line}"\"" )" >> "${luafastdlfullpath}"
  336. done
  337. fn_print_ok "Download enforcer generated"
  338. fn_script_log "Download enforcer generated"
  339. echo -en "\n"
  340. echo ""
  341. sleep 2
  342. fi
  343. }
  344. fn_fastdl_bzip2(){
  345. # Compressing using bzip2 if user said yes
  346. echo ""
  347. if [ ${bzip2enable} == "on" ]; then
  348. fn_print_info "Have a break, this step could take a while..."
  349. echo -en "\n"
  350. echo ""
  351. fn_print_dots "Compressing files using bzip2..."
  352. fn_script_log "Compressing files using bzip2..."
  353. # bzip2 all files that are not already compressed (keeping original files)
  354. find "${fastdldir:?}" \( -type f ! -name "*.bz2" \) -exec bzip2 -qk \{\} \;
  355. fn_print_ok "bzip2 compression done"
  356. fn_script_log "bzip2 compression done"
  357. sleep 1
  358. echo -en "\n"
  359. # Clear non compressed FastDL files
  360. if [ "${clearnonbzip2}" == "on" ]; then
  361. fn_print_dots "Clearing original uncompressed FastDL files..."
  362. sleep 1
  363. find "${fastdldir:?}" \( -type f ! -name "*.bz2" \) -exec rm {} \;
  364. fn_print_ok "Cleared uncompressed FastDL files"
  365. fn_script_log "Cleared uncompressed FastDL files."
  366. fi
  367. fi
  368. }
  369. fn_fastdl_completed(){
  370. # Finished message
  371. echo ""
  372. fn_print_ok "FastDL created!"
  373. fn_script_log "FastDL job done"
  374. sleep 2
  375. echo -en "\n"
  376. echo ""
  377. fn_print_info_nl "Need more documentation?"
  378. echo " * https://github.com/GameServerManagers/LinuxGSM/wiki/FastDL"
  379. echo -en "\n"
  380. if [ "$bzip2installed" == "0" ]; then
  381. echo "By the way, you'd better install bzip2 and re-run this command!"
  382. fi
  383. echo "Credits: UltimateByte"
  384. }
  385. # Run functions
  386. fn_check_bzip2
  387. fn_fastdl_init
  388. fn_fastdl_config
  389. fn_clear_old_fastdl
  390. if [ "${gamename}" == "Garry's Mod" ]; then
  391. fn_fastdl_gmod
  392. fn_fastdl_gmod_lua_enforcer
  393. else
  394. fn_fastdl_source
  395. fi
  396. fn_fastdl_bzip2
  397. fn_fastdl_completed
  398. core_exit.sh