command_fastdl.sh 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. #!/bin/bash
  2. # LGSM 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. bzip2installed="0"
  23. fn_print_info "bzip2 is not installed !"
  24. fn_script_log_info "bzip2 is not installed"
  25. echo -en "\n"
  26. sleep 1
  27. echo "We advise using it"
  28. echo "For more information, see https://github.com/GameServerManagers/LinuxGSM/wiki/FastDL#bzip2-compression"
  29. sleep 2
  30. else
  31. bzip2installed="1"
  32. fi
  33. }
  34. fn_fastdl_init(){
  35. # User confirmation
  36. fn_print_ok "Welcome to LGSM's FastDL generator"
  37. sleep 1
  38. echo -en "\n"
  39. fn_script_log "Started FastDL creation"
  40. while true; do
  41. read -e -i "y" -p "Continue? [Y/n]" yn
  42. case $yn in
  43. [Yy]* ) break;;
  44. [Nn]* ) exit;;
  45. * ) echo "Please answer yes or no.";;
  46. esac
  47. done
  48. fn_script_log "Initiating FastDL creation"
  49. # Check and create directories
  50. if [ ! -d "${webdir}" ]; then
  51. echo ""
  52. fn_print_info "Creating FastDL directories"
  53. echo -en "\n"
  54. sleep 1
  55. fn_print_dots "Creating ${webdir} directory"
  56. sleep 0.5
  57. mkdir "${webdir}"
  58. fn_print_ok "Created ${webdir} directory"
  59. fn_script_log "FastDL created ${webdir} directory"
  60. sleep 1
  61. echo -en "\n"
  62. fi
  63. if [ ! -d "${fastdldir}" ]; then
  64. # No directory, won't ask for removing old ones
  65. newfastdl=1
  66. fn_print_dots "Creating fastdl directory"
  67. sleep 0.5
  68. mkdir "${fastdldir}"
  69. fn_print_ok "Created fastdl directory"
  70. fn_script_log "FastDL created fastdl directory"
  71. sleep 1
  72. echo -en "\n"
  73. clearoldfastdl="off" # Nothing to clear
  74. elif [ "$(ls -A "${fastdldir}")" ]; then
  75. newfastdl=0
  76. fi
  77. }
  78. fn_fastdl_config(){
  79. # Global settings for FastDL creation
  80. fn_print_info "Entering configuration"
  81. fn_script_log "Configuration"
  82. sleep 2
  83. echo -en "\n"
  84. # Prompt for clearing old files if directory was already here
  85. if [ -n "${newfastdl}" ] && [ "${newfastdl}" == "0" ]; then
  86. fn_print_dots
  87. while true; do
  88. read -e -i "y" -p "Clear old FastDL files? [Y/n]" yn
  89. case $yn in
  90. [Yy]* ) clearoldfastdl="on"; fn_script_log "clearoldfastdl enabled"; fn_print_ok "Clearing Enabled"; break;;
  91. [Nn]* ) clearoldfastdl="off"; fn_script_log "clearoldfastdl disabled"; fn_print_ok "Clearing Disabled"; break;;
  92. * ) echo "Please answer yes or no.";;
  93. esac
  94. done
  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. while true; do
  101. read -e -i "y" -p "Enable file compression using bzip2? [Y/n]" yn
  102. case $yn in
  103. [Yy]* ) bzip2enable="on"; fn_script_log "bzip2 enabled"; fn_print_ok "bzip2 Enabled"; break;;
  104. [Nn]* ) bzip2enable="off"; fn_script_log "bzip2 disabled"; fn_print_ok "bzip2 Disabled"; break;;
  105. * ) echo "Please answer yes or no.";;
  106. esac
  107. done
  108. echo -en "\n"
  109. fi
  110. }
  111. fn_fastdl_gmod_config(){
  112. # Prompt for download enforcer, that is using a .lua addfile resource generator
  113. fn_print_dots
  114. while true; do
  115. read -e -i "y" -p "Use client download enforcer? [Y/n]" yn
  116. case $yn in
  117. [Yy]* ) luaressource="on"; fn_script_log "DL enforcer Enabled"; fn_print_ok "Enforcer Enabled"; break;;
  118. [Nn]* ) luaressource="off"; fn_script_log "DL enforcer Disabled"; fn_print_ok "Enforcer Disabled"; break;;
  119. * ) echo "Please answer yes or no.";;
  120. esac
  121. done
  122. echo -en "\n"
  123. }
  124. fn_clear_old_fastdl(){
  125. # Clearing old FastDL if user answered yes
  126. if [ "${clearoldfastdl}" == "on" ]; then
  127. fn_print_info "Clearing existing FastDL directory"
  128. fn_script_log "Clearing existing FastDL directory"
  129. sleep 0.5
  130. rm -R "${fastdldir:?}"/*
  131. fn_print_ok "Old FastDL directory cleared"
  132. fn_script_log "Old FastDL directory cleared"
  133. sleep 1
  134. echo -en "\n"
  135. fi
  136. }
  137. fn_gmod_fastdl(){
  138. # Copy all needed files for FastDL
  139. echo ""
  140. fn_print_dots "Starting gathering all needed files"
  141. fn_script_log "Starting gathering all needed files"
  142. sleep 1
  143. echo -en "\n"
  144. # No choice to cd to the directory, as find can't then display relative directory
  145. cd "${systemdir}"
  146. # Map Files
  147. fn_print_dots "Copying map files..."
  148. fn_script_log "Copying map files"
  149. sleep 0.5
  150. find . -name '*.bsp' | cpio --quiet -updm "${fastdldir}"
  151. find . -name '*.ain' | cpio --quiet -updm "${fastdldir}"
  152. fn_print_ok "Map files copied"
  153. sleep 0.5
  154. echo -en "\n"
  155. # Materials
  156. fn_print_dots "Copying materials..."
  157. fn_script_log "Copying materials"
  158. sleep 0.5
  159. find . -name '*.vtf' | cpio --quiet -updm "${fastdldir}"
  160. find . -name '*.vmt' | cpio --quiet -updm "${fastdldir}"
  161. fn_print_ok "Materials copied"
  162. sleep 0.5
  163. echo -en "\n"
  164. # Models
  165. fn_print_dots "Copying models..."
  166. fn_script_log "Copying models"
  167. sleep 1
  168. find . -name '*.vtx' | cpio --quiet -updm "${fastdldir}"
  169. find . -name '*.vvd' | cpio --quiet -updm "${fastdldir}"
  170. find . -name '*.mdl' | cpio --quiet -updm "${fastdldir}"
  171. find . -name '*.phy' | cpio --quiet -updm "${fastdldir}"
  172. fn_print_ok "Models copied"
  173. sleep 0.5
  174. echo -en "\n"
  175. # Particles
  176. fn_print_dots "Copying particles..."
  177. fn_script_log "Copying particles"
  178. sleep 0.5
  179. find . -name '*.pcf' | cpio --quiet -updm "${fastdldir}"
  180. fn_print_ok "Particles copied"
  181. sleep 0.5
  182. echo -en "\n"
  183. # Sounds
  184. fn_print_dots "Copying sounds..."
  185. fn_script_log "Copying sounds"
  186. sleep 0.5
  187. find . -name '*.wav' | cpio --quiet -updm "${fastdldir}"
  188. find . -name '*.mp3' | cpio --quiet -updm "${fastdldir}"
  189. find . -name '*.ogg' | cpio --quiet -updm "${fastdldir}"
  190. fn_print_ok "Sounds copied"
  191. sleep 0.5
  192. echo -en "\n"
  193. # Resources (mostly fonts)
  194. fn_print_dots "Copying fonts and png..."
  195. fn_script_log "Copying fonts and png"
  196. sleep 1
  197. find . -name '*.otf' | cpio --quiet -updm "${fastdldir}"
  198. find . -name '*.ttf' | cpio --quiet -updm "${fastdldir}"
  199. find . -name '*.png' | cpio --quiet -updm "${fastdldir}"
  200. fn_print_ok "Fonts and png copied"
  201. sleep 0.5
  202. echo -en "\n"
  203. # Going back to rootdir in order to prevent mistakes
  204. cd "${rootdir}"
  205. # Correct addons directory structure for FastDL
  206. if [ -d "${fastdldir}/addons" ]; then
  207. fn_print_info "Adjusting addons' file structure"
  208. fn_script_log "Adjusting addon's file structure"
  209. sleep 1
  210. cp -Rf "${fastdldir}"/addons/*/* "${fastdldir}"
  211. #Don't remove yet rm -R "${fastdldir:?}/addons"
  212. fn_print_ok "Adjusted addon's file structure"
  213. sleep 1
  214. echo -en "\n"
  215. fi
  216. # Correct content that may be into a lua directory by mistake like some darkrpmodification addons
  217. if [ -d "${fastdldir}/lua" ]; then
  218. fn_print_dots "Typical DarkRP shit detected, fixing"
  219. sleep 2
  220. cp -Rf "${fastdldir}/lua/"* "${fastdldir}"
  221. fn_print_ok "Stupid DarkRP file structure fixed"
  222. sleep 2
  223. echo -en "\n"
  224. fi
  225. }
  226. # Generate lua file that will force download any file into the FastDL directory
  227. fn_lua_fastdl(){
  228. # Remove lua file if luaressource is turned off and file exists
  229. echo ""
  230. if [ "${luaressource}" == "off" ]; then
  231. if [ -f "${luafastdlfullpath}" ]; then
  232. fn_print_dots "Removing download enforcer"
  233. sleep 1
  234. rm -R "${luafastdlfullpath:?}"
  235. fn_print_ok "Removed download enforcer"
  236. fn_script_log "Removed old download inforcer"
  237. echo -en "\n"
  238. sleep 2
  239. fi
  240. fi
  241. # Remove old lua file and generate a new one if user said yes
  242. if [ "${luaressource}" == "on" ]; then
  243. if [ -f "${luafastdlfullpath}" ]; then
  244. fn_print_dots "Removing old download enforcer"
  245. sleep 1
  246. rm "${luafastdlfullpath}"
  247. fn_print_ok "Removed old download enforcer"
  248. fn_script_log "Removed old download enforcer"
  249. echo -en "\n"
  250. sleep 1
  251. fi
  252. fn_print_dots "Generating new download enforcer"
  253. fn_script_log "Generating new download enforcer"
  254. sleep 1
  255. # Read all filenames and put them into a lua file at the right path
  256. find "${fastdldir}" \( -type f ! -name "*.bz2" \) -printf '%P\n' | while read line; do
  257. echo "resource.AddFile( "\""${line}"\"" )" >> ${luafastdlfullpath}
  258. done
  259. fn_print_ok "Download enforcer generated"
  260. fn_script_log "Download enforcer generated"
  261. echo -en "\n"
  262. echo ""
  263. sleep 2
  264. fi
  265. }
  266. fn_fastdl_bzip2(){
  267. # Compressing using bzip2 if user said yes
  268. echo ""
  269. if [ ${bzip2enable} == "on" ]; then
  270. fn_print_info "Have a break, this step could take a while..."
  271. echo -en "\n"
  272. echo ""
  273. fn_print_dots "Compressing files using bzip2..."
  274. fn_script_log "Compressing files using bzip2..."
  275. # bzip2 all files that are not already compressed (keeping original files)
  276. find "${fastdldir}" \( -type f ! -name "*.bz2" \) -exec bzip2 -qk \{\} \;
  277. fn_print_ok "bzip2 compression done"
  278. fn_script_log "bzip2 compression done"
  279. sleep 1
  280. echo -en "\n"
  281. fi
  282. }
  283. fn_fastdl_completed(){
  284. # Finished message
  285. echo ""
  286. fn_print_ok "Congratulations, it's done!"
  287. fn_script_log "FastDL job done"
  288. sleep 2
  289. echo -en "\n"
  290. echo ""
  291. fn_print_info "Need more documentation? See https://github.com/GameServerManagers/LinuxGSM/wiki/FastDL"
  292. echo -en "\n"
  293. if [ "$bzip2installed" == "0" ]; then
  294. echo "By the way, you'd better install bzip2 and re-run this command!"
  295. fi
  296. echo "Credits : UltimateByte"
  297. }
  298. # Game checking and functions running
  299. # Garry's Mod
  300. if [ "${gamename}" == "Garry's Mod" ]; then
  301. fn_check_bzip2
  302. fn_fastdl_init
  303. fn_fastdl_config
  304. fn_fastdl_gmod_config
  305. fn_clear_old_fastdl
  306. fn_gmod_fastdl
  307. fn_lua_fastdl
  308. fn_fastdl_bzip2
  309. fn_fastdl_completed
  310. exit
  311. fi