command_fastdl.sh 9.0 KB

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