command_fastdl.sh 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. #!/bin/bash
  2. # LGSM command_fastdl function
  3. # Author: Daniel Gibbs
  4. # Contributor: UltimateByte
  5. # Website: http://gameservermanagers.com
  6. lgsm_version="190216"
  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/audoturn/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. echo -en "\n"
  38. fn_scriptlog "Started FastDL creation"
  39. sleep 1
  40. while true; do
  41. read -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. fn_printdots "Creating www directory..."
  52. sleep 0.5
  53. mkdir "${webdir}"
  54. sleep 1
  55. fn_scriptlog "FastDL created ${webdir}"
  56. fi
  57. if [ ! -d "${fastdldir}" ]; then
  58. # No folder, won't ask for removing old ones
  59. newfastdl=1
  60. fn_printdots "Creating FastDL directory..."
  61. sleep 0.5
  62. mkdir "${fastdldir}"
  63. sleep 1
  64. fn_scriptlog "FastDL created ${fastdldir}"
  65. fn_printok "Folders created"
  66. echo -en "\n"
  67. else
  68. # Used to prompt for removing old files
  69. newfastdl=0
  70. fi
  71. }
  72. fn_fastdl_config(){
  73. # Global settings for FastDL creation
  74. fn_printinfo "Entering configuration"
  75. fn_scriptlog "Configuration"
  76. echo -en "\n"
  77. sleep 2
  78. # Prompt for clearing old files if folder was already here
  79. if [ ${newfastdl} == 0 ]; then
  80. fn_printdots
  81. while true; do
  82. read -p "Clear old FastDL files? [y/N]" yN
  83. case $yn in
  84. [Yy]* ) clearoldfastdl="on"; fn_scriptlog "clearoldfastdl enabled"; fn_printok "Clearing Enabled"; break;;
  85. [Nn]* ) clearoldfastdl="off"; fn_scriptlog "clearoldfastdl disabled"; fn_printok "Clearing Disabled"; break;;
  86. * ) echo "Please answer yes or no.";;
  87. esac
  88. done
  89. sleep 1
  90. echo -en "\n"
  91. fi
  92. # Prompt for using bzip2 if it's installed
  93. if [ ${bzip2installed} == 1 ]; then
  94. fn_printdots
  95. while true; do
  96. read -p "Enable file compression using bzip2? [Y/n]" Yn
  97. case $yn in
  98. [Yy]* ) bzip2enable="on"; fn_scriptlog "bzip2 enabled"; fn_printok "bzip2 Enabled"; break;;
  99. [Nn]* ) bzip2enable="off"; fn_scriptlog "bzip2 disabled"; fn_printok "bzip2 Disabled;" break;;
  100. * ) echo "Please answer yes or no.";;
  101. esac
  102. done
  103. sleep 1
  104. echo -en "\n"
  105. fi
  106. }
  107. fn_fastdl_gmod_config(){
  108. # Prompt for download enforcer, that is using a .lua addfile resource generator
  109. fn_printdots
  110. while true; do
  111. read -p "Use client download enforcer? [Y/n]" Yn
  112. case $yn in
  113. [Yy]* ) luaressource="on"; fn_scriptlog "DL enforcer Enabled"; fn_printok "Enforcer Enabled"; break;;
  114. [Nn]* ) luaressource="off"; "DL enforcer Disabled"; fn_printok "Enforcer Disabled"; break;;
  115. * ) echo "Please answer yes or no.";;
  116. esac
  117. sleep1
  118. echo -en "\n"
  119. done
  120. }
  121. fn_clear_old_fastdl(){
  122. # Clearing old FastDL if user answered yes
  123. if [ ${clearoldfastdl} == "on" ]; then
  124. fn_printinfo "Clearing existing FastDL folder"
  125. fn_scriptlog "Clearing existing FastDL folder"
  126. sleep 1
  127. rm -R "${fastdldir}"/*
  128. fn_printok "Old FastDL folder cleared"
  129. fn_scriptlog "Old FastDL folder cleared"
  130. echo -en "\n"
  131. fi
  132. }
  133. fn_gmod_fastdl(){
  134. # Copy all needed files for fastDL
  135. fn_printdots "Gathering all needed files..."
  136. echo -en "\n"
  137. sleep 1
  138. # No choice to cd to the directory, as find can't then display relative folder
  139. cd "${addonsdir}"
  140. # Map Files
  141. fn_printdots "Copying map files..."
  142. fn_scriptlog "Copying map files"
  143. sleep 1
  144. find . -name '*.bsp' | cpio --quiet -updm "${fastdldir}"
  145. fn_printok "Map files copied"
  146. echo -en "\n"
  147. sleep 1
  148. # Materials
  149. fn_printdots "Copying materials..."
  150. fn_scriptlog "Copying materials"
  151. sleep 1
  152. find . -name '*.vtf' | cpio --quiet -updm "${fastdldir}"
  153. find . -name '*.vmt' | cpio --quiet -updm "${fastdldir}"
  154. fn_printok "Materials copied"
  155. echo -en "\n"
  156. sleep 1
  157. # Models
  158. fn_printdots "Copying models..."
  159. fn_scriptlog "Copying models"
  160. sleep 1
  161. find . -name '*.vtx' | cpio --quiet -updm "${fastdldir}"
  162. find . -name '*.vvd' | cpio --quiet -updm "${fastdldir}"
  163. find . -name '*.mdl' | cpio --quiet -updm "${fastdldir}"
  164. find . -name '*.phy' | cpio --quiet -updm "${fastdldir}"
  165. fn_printok "Models copied"
  166. echo -en "\n"
  167. sleep 1
  168. # Particles
  169. fn_printdots "Copying particles..."
  170. fn_scriptlog "Copying particles"
  171. sleep 1
  172. find . -name '*.pcf' | cpio --quiet -updm "${fastdldir}"
  173. fn_printok "Particles copied"
  174. echo -en "\n"
  175. sleep 1
  176. # Sounds
  177. fn_printdots "Copying sounds..."
  178. fn_scriptlog "Copying sounds"
  179. sleep 1
  180. find . -name '*.wav' | cpio --quiet -updm "${fastdldir}"
  181. find . -name '*.mp3' | cpio --quiet -updm "${fastdldir}"
  182. find . -name '*.ogg' | cpio --quiet -updm "${fastdldir}"
  183. fn_printok "Sounds copied"
  184. echo -en "\n"
  185. sleep 1
  186. # Resources (mostly fonts)
  187. fn_printdots "Copying fonts and png..."
  188. fn_scriptlog "Copying fonts and png"
  189. sleep 1
  190. find . -name '*.otf' | cpio --quiet -updm "${fastdldir}"
  191. find . -name '*.ttf' | cpio --quiet -updm "${fastdldir}"
  192. find . -name '*.png' | cpio --quiet -updm "${fastdldir}"
  193. fn_printok "Fonts and png copied"
  194. echo -en "\n"
  195. sleep 1
  196. # Going back to rootdir in order to prevent crap from happening
  197. cd "${rootdir}"
  198. # Correct addons folder structure
  199. if [ -d "${fastdldir}/addons" ]; then
  200. fn_printinfo "Correcting file structure"
  201. fn_scriptlog "Correcting file structure"
  202. sleep 2
  203. echo "Copying those files to their correct folder"
  204. sleep 2
  205. cp -Rf "${fastdldir}"/addons/*/* "${fastdldir}"
  206. # As we're not sure about the correct file structure, duplicate instead of remove
  207. # rm -R "${fastdldir}/addons"
  208. fn_printok "Corrected file structure"
  209. echo -en "\n"
  210. sleep 1
  211. fi
  212. # Correct content that may be into a lua folder by mistake like some darkrpmodification addons
  213. if [ -d "${fastdldir}/lua" ]; then
  214. fn_printdots "Stupid file structure fix"
  215. sleep 1
  216. cp -Rf "${fastdldir}/lua/"* "${fastdldir}"
  217. fn_printok "Stupid file structure fixed"
  218. echo -en "\n"
  219. sleep 2
  220. fi
  221. }
  222. # Generate lua file that will force download any file into the FastDL folder
  223. fn_lua_fastdl(){
  224. # Remove lua file if luaressource is turned off and file exists
  225. if [ "${luaressource}" == "off" ]; then
  226. if [ -f "${luafastdlfullpath}" ]; then
  227. fn_printdots "Removing download enforcer"
  228. sleep 1
  229. rm -R "${luafastdlfullpath}"
  230. fn_printok "Removed download enforcer"
  231. fn_scriptlog "Removed old download inforcer"
  232. echo -en "\n"
  233. sleep 2
  234. fi
  235. fi
  236. # Remove old lua file and generate a new one if user said yes
  237. if [ "${luaressource}" == "on" ]; then
  238. if [ -f "${luafastdlfullpath}" ]; then
  239. fn_printdots "Removing old download enforcer"
  240. sleep 1
  241. rm "${luafastdlfullpath}"
  242. fn_printok "Removed old download enforcer"
  243. fn_scriptlog "Removed old download enforcer"
  244. echo -en "\n"
  245. sleep 1
  246. fi
  247. fn_printdots "Generating new download enforcer"
  248. fn_scriptlog "Generating new download enforcer"
  249. sleep 1
  250. # Read all filenames and put them into a lua file at the right path
  251. find "${fastdldir}" \( -name "." ! -name "*.bz2" \) -printf '%P\n' | while read line; do
  252. echo "resource.AddFile("\""${line}"\"")" >> ${luafastdlfullpath}
  253. done
  254. fn_printok "Download enforcer generated"
  255. fn_scriptlog "Download enforcer generated"
  256. echo -en "\n"
  257. echo ""
  258. sleep 2
  259. fi
  260. }
  261. fn_fastdl_bzip2(){
  262. # Compressing using bzip2 if user said yes
  263. if [ ${bzip2enable} == "on" ]; then
  264. fn_printdots "Compressing files using bzip2..."
  265. fn_scriptlog "Compressing files using bzip2..."
  266. sleep 2
  267. # bzip2 all files that are not already compressed (keeping original files)
  268. find "${fastdldir}" -not -name \*.bz2 -exec bzip2 -qk \{\} \;
  269. fn_printinfo "bzip2 compression done"
  270. fn_scriptlog "bzip2 compression done"
  271. sleep 1
  272. fi
  273. }
  274. fn_fastdl_completed(){
  275. # Finished message
  276. fn_printok "Congratulations, it's done"
  277. fn_scriptlog "FastDL job done"
  278. echo "For more information, see https://github.com/dgibbs64/linuxgsm/wiki/Fastdl"
  279. echo -en "\n"
  280. if [ "$bzip2installed" == "0" ]; then
  281. echo "By the way, you'd better install bzip2 an re-run this command"
  282. fi
  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