command_fastdl.sh 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. #!/bin/bash
  2. # LGSM command_fastdl function
  3. # Author: Daniel Gibbs
  4. # Contributor: UltimateByte
  5. # Website: https://gameservermanagers.com
  6. lgsm_version="210216"
  7. # Description: Creates a FastDL folder
  8. local modulename="FastDL"
  9. function_selfname="$(basename $(readlink -f "${BASH_SOURCE[0]}"))"
  10. check.sh
  11. # Directories
  12. webdir="${rootdir}/www"
  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_printinfo "bzip2 is not installed !"
  24. fn_scriptlog "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/dgibbs64/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_printok "Welcome to LGSM's FastDL generator"
  37. sleep 1
  38. echo -en "\n"
  39. fn_scriptlog "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_scriptlog "Initiating FastDL creation"
  49. # Check and create folders
  50. if [ ! -d "${webdir}" ]; then
  51. echo ""
  52. fn_printinfo "Creating FastDL directories"
  53. echo -en "\n"
  54. sleep 1
  55. fn_printdots "Creating www directory"
  56. sleep 0.5
  57. mkdir "${webdir}"
  58. fn_printok "Created www directory"
  59. fn_scriptlog "FastDL created www directory"
  60. sleep 1
  61. echo -en "\n"
  62. fi
  63. if [ ! -d "${fastdldir}" ]; then
  64. # No folder, won't ask for removing old ones
  65. newfastdl=1
  66. fn_printdots "Creating fastdl directory"
  67. sleep 0.5
  68. mkdir "${fastdldir}"
  69. fn_printok "Created fastdl directory"
  70. fn_scriptlog "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_printinfo "Entering configuration"
  81. fn_scriptlog "Configuration"
  82. sleep 2
  83. echo -en "\n"
  84. # Prompt for clearing old files if folder was already here
  85. if [ -n "${newfastdl}" ] && [ "${newfastdl}" == "0" ]; then
  86. fn_printdots
  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_scriptlog "clearoldfastdl enabled"; fn_printok "Clearing Enabled"; break;;
  91. [Nn]* ) clearoldfastdl="off"; fn_scriptlog "clearoldfastdl disabled"; fn_printok "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_printdots
  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_scriptlog "bzip2 enabled"; fn_printok "bzip2 Enabled"; break;;
  104. [Nn]* ) bzip2enable="off"; fn_scriptlog "bzip2 disabled"; fn_printok "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_printdots
  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_scriptlog "DL enforcer Enabled"; fn_printok "Enforcer Enabled"; break;;
  118. [Nn]* ) luaressource="off"; fn_scriptlog "DL enforcer Disabled"; fn_printok "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_printinfo "Clearing existing FastDL folder"
  128. fn_scriptlog "Clearing existing FastDL folder"
  129. sleep 0.5
  130. rm -R "${fastdldir:?}"/*
  131. fn_printok "Old FastDL folder cleared"
  132. fn_scriptlog "Old FastDL folder 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_printdots "Starting gathering all needed files"
  141. fn_scriptlog "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 folder
  145. cd "${systemdir}"
  146. # Map Files
  147. fn_printdots "Copying map files..."
  148. fn_scriptlog "Copying map files"
  149. sleep 0.5
  150. find . -name '*.bsp' | cpio --quiet -updm "${fastdldir}"
  151. fn_printok "Map files copied"
  152. sleep 0.5
  153. echo -en "\n"
  154. # Materials
  155. fn_printdots "Copying materials..."
  156. fn_scriptlog "Copying materials"
  157. sleep 0.5
  158. find . -name '*.vtf' | cpio --quiet -updm "${fastdldir}"
  159. find . -name '*.vmt' | cpio --quiet -updm "${fastdldir}"
  160. fn_printok "Materials copied"
  161. sleep 0.5
  162. echo -en "\n"
  163. # Models
  164. fn_printdots "Copying models..."
  165. fn_scriptlog "Copying models"
  166. sleep 1
  167. find . -name '*.vtx' | cpio --quiet -updm "${fastdldir}"
  168. find . -name '*.vvd' | cpio --quiet -updm "${fastdldir}"
  169. find . -name '*.mdl' | cpio --quiet -updm "${fastdldir}"
  170. find . -name '*.phy' | cpio --quiet -updm "${fastdldir}"
  171. fn_printok "Models copied"
  172. sleep 0.5
  173. echo -en "\n"
  174. # Particles
  175. fn_printdots "Copying particles..."
  176. fn_scriptlog "Copying particles"
  177. sleep 0.5
  178. find . -name '*.pcf' | cpio --quiet -updm "${fastdldir}"
  179. fn_printok "Particles copied"
  180. sleep 0.5
  181. echo -en "\n"
  182. # Sounds
  183. fn_printdots "Copying sounds..."
  184. fn_scriptlog "Copying sounds"
  185. sleep 0.5
  186. find . -name '*.wav' | cpio --quiet -updm "${fastdldir}"
  187. find . -name '*.mp3' | cpio --quiet -updm "${fastdldir}"
  188. find . -name '*.ogg' | cpio --quiet -updm "${fastdldir}"
  189. fn_printok "Sounds copied"
  190. sleep 0.5
  191. echo -en "\n"
  192. # Resources (mostly fonts)
  193. fn_printdots "Copying fonts and png..."
  194. fn_scriptlog "Copying fonts and png"
  195. sleep 1
  196. find . -name '*.otf' | cpio --quiet -updm "${fastdldir}"
  197. find . -name '*.ttf' | cpio --quiet -updm "${fastdldir}"
  198. find . -name '*.png' | cpio --quiet -updm "${fastdldir}"
  199. fn_printok "Fonts and png copied"
  200. sleep 0.5
  201. echo -en "\n"
  202. # Going back to rootdir in order to prevent mistakes
  203. cd "${rootdir}"
  204. # Correct addons folder structure for FastDL
  205. if [ -d "${fastdldir}/addons" ]; then
  206. fn_printinfo "Adjusting addons' file structure"
  207. fn_scriptlog "Adjusting addon's file structure"
  208. sleep 1
  209. cp -Rf "${fastdldir}"/addons/*/* "${fastdldir}"
  210. #Don't remove yet rm -R "${fastdldir:?}/addons"
  211. fn_printok "Adjusted addon's file structure"
  212. sleep 1
  213. echo -en "\n"
  214. fi
  215. # Correct content that may be into a lua folder by mistake like some darkrpmodification addons
  216. if [ -d "${fastdldir}/lua" ]; then
  217. fn_printdots "Typical DarkRP shit detected, fixing"
  218. sleep 2
  219. cp -Rf "${fastdldir}/lua/"* "${fastdldir}"
  220. fn_printok "Stupid DarkRP file structure fixed"
  221. sleep 2
  222. echo -en "\n"
  223. fi
  224. }
  225. # Generate lua file that will force download any file into the FastDL folder
  226. fn_lua_fastdl(){
  227. # Remove lua file if luaressource is turned off and file exists
  228. echo ""
  229. if [ "${luaressource}" == "off" ]; then
  230. if [ -f "${luafastdlfullpath}" ]; then
  231. fn_printdots "Removing download enforcer"
  232. sleep 1
  233. rm -R "${luafastdlfullpath:?}"
  234. fn_printok "Removed download enforcer"
  235. fn_scriptlog "Removed old download inforcer"
  236. echo -en "\n"
  237. sleep 2
  238. fi
  239. fi
  240. # Remove old lua file and generate a new one if user said yes
  241. if [ "${luaressource}" == "on" ]; then
  242. if [ -f "${luafastdlfullpath}" ]; then
  243. fn_printdots "Removing old download enforcer"
  244. sleep 1
  245. rm "${luafastdlfullpath}"
  246. fn_printok "Removed old download enforcer"
  247. fn_scriptlog "Removed old download enforcer"
  248. echo -en "\n"
  249. sleep 1
  250. fi
  251. fn_printdots "Generating new download enforcer"
  252. fn_scriptlog "Generating new download enforcer"
  253. sleep 1
  254. # Read all filenames and put them into a lua file at the right path
  255. find "${fastdldir}" \( -type f ! -name "*.bz2" \) -printf '%P\n' | while read line; do
  256. echo "resource.AddFile( "\""${line}"\"" )" >> ${luafastdlfullpath}
  257. done
  258. fn_printok "Download enforcer generated"
  259. fn_scriptlog "Download enforcer generated"
  260. echo -en "\n"
  261. echo ""
  262. sleep 2
  263. fi
  264. }
  265. fn_fastdl_bzip2(){
  266. # Compressing using bzip2 if user said yes
  267. echo ""
  268. if [ ${bzip2enable} == "on" ]; then
  269. fn_printinfo "Have a break, this step could take a while..."
  270. echo -en "\n"
  271. echo ""
  272. fn_printdots "Compressing files using bzip2..."
  273. fn_scriptlog "Compressing files using bzip2..."
  274. # bzip2 all files that are not already compressed (keeping original files)
  275. find "${fastdldir}" \( -type f ! -name "*.bz2" \) -exec bzip2 -qk \{\} \;
  276. fn_printok "bzip2 compression done"
  277. fn_scriptlog "bzip2 compression done"
  278. sleep 1
  279. echo -en "\n"
  280. fi
  281. }
  282. fn_fastdl_completed(){
  283. # Finished message
  284. echo ""
  285. fn_printok "Congratulations, it's done !"
  286. fn_scriptlog "FastDL job done"
  287. sleep 2
  288. echo -en "\n"
  289. echo ""
  290. fn_printinfo "Need more doc ? See https://github.com/dgibbs64/linuxgsm/wiki/FastDL"
  291. echo -en "\n"
  292. if [ "$bzip2installed" == "0" ]; then
  293. echo "By the way, you'd better install bzip2 an re-run this command !"
  294. fi
  295. echo "Credits : UltimateByte"
  296. }
  297. # Game checking and functions running
  298. # Garry's Mod
  299. if [ "${gamename}" == "Garry's Mod" ]; then
  300. fn_check_bzip2
  301. fn_fastdl_init
  302. fn_fastdl_config
  303. fn_fastdl_gmod_config
  304. fn_clear_old_fastdl
  305. fn_gmod_fastdl
  306. fn_lua_fastdl
  307. fn_fastdl_bzip2
  308. fn_fastdl_completed
  309. exit
  310. fi