command_fastdl.sh 9.3 KB

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