command_fastdl.sh 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  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. # Map Files
  139. fn_printdots "Copying map files..."
  140. fn_scriptlog "Copying map files"
  141. sleep 1
  142. find "${addonsdir}" -name '*.bsp' | cpio --quiet -updm "${fastdldir}"
  143. fn_printok "Map files copied"
  144. echo -en "\n"
  145. sleep 1
  146. # Materials
  147. fn_printdots "Copying materials..."
  148. fn_scriptlog "Copying materials"
  149. sleep 1
  150. find "${addonsdir}" -name '*.vtf' | cpio --quiet -updm "${fastdldir}"
  151. find "${addonsdir}" -name '*.vmt' | cpio --quiet -updm "${fastdldir}"
  152. fn_printok "Materials copied"
  153. echo -en "\n"
  154. sleep 1
  155. # Models
  156. fn_printdots "Copying models..."
  157. fn_scriptlog "Copying models"
  158. sleep 1
  159. find "${addonsdir}" -name '*.vtx' | cpio --quiet -updm "${fastdldir}"
  160. find "${addonsdir}" -name '*.vvd' | cpio --quiet -updm "${fastdldir}"
  161. find "${addonsdir}" -name '*.mdl' | cpio --quiet -updm "${fastdldir}"
  162. find "${addonsdir}" -name '*.phy' | cpio --quiet -updm "${fastdldir}"
  163. fn_printok "Models copied"
  164. echo -en "\n"
  165. sleep 1
  166. # Particles
  167. fn_printdots "Copying particles..."
  168. fn_scriptlog "Copying particles"
  169. sleep 1
  170. find "${addonsdir}" -name '*.pcf' | cpio --quiet -updm "${fastdldir}"
  171. fn_printok "Particles copied"
  172. echo -en "\n"
  173. sleep 1
  174. # Sounds
  175. fn_printdots "Copying sounds..."
  176. fn_scriptlog "Copying sounds"
  177. sleep 1
  178. find "${addonsdir}" -name '*.wav' | cpio --quiet -updm "${fastdldir}"
  179. find "${addonsdir}" -name '*.mp3' | cpio --quiet -updm "${fastdldir}"
  180. find "${addonsdir}" -name '*.ogg' | cpio --quiet -updm "${fastdldir}"
  181. fn_printok "Sounds copied"
  182. echo -en "\n"
  183. sleep 1
  184. # Resources (mostly fonts)
  185. fn_printdots "Copying fonts and png..."
  186. fn_scriptlog "Copying fonts and png"
  187. sleep 1
  188. find "${addonsdir}" -name '*.otf' | cpio --quiet -updm "${fastdldir}"
  189. find "${addonsdir}" -name '*.ttf' | cpio --quiet -updm "${fastdldir}"
  190. find "${addonsdir}" -name '*.png' | cpio --quiet -updm "${fastdldir}"
  191. fn_printok "Fonts and png copied"
  192. echo -en "\n"
  193. sleep 1
  194. # Correct addons folder structure
  195. if [ -d "${fastdldir}/addons" ]; then
  196. fn_printinfo "Correcting file structure"
  197. fn_scriptlog "Correcting file structure"
  198. sleep 2
  199. echo "Copying those files to their correct folder"
  200. sleep 2
  201. cp -Rf "${fastdldir}"/addons/*/* "${fastdldir}"
  202. # As we're not sure about the correct file structure, duplicate instead of remove
  203. # rm -R "${fastdldir}/addons"
  204. fn_printok "Corrected file structure"
  205. echo -en "\n"
  206. sleep 1
  207. fi
  208. # Correct content that may be into a lua folder by mistake like some darkrpmodification addons
  209. if [ -d "${fastdldir}/lua" ]; then
  210. fn_printdots "Stupid file structure fix"
  211. sleep 1
  212. cp -Rf "${fastdldir}/lua/"* "${fastdldir}"
  213. fn_printok "Stupid file structure fixed"
  214. echo -en "\n"
  215. sleep 1
  216. fi
  217. }
  218. fn_fastdl_bzip2(){
  219. # Compressing using bzip2 if user said yes
  220. if [ ${bzip2enable} == "on" ]; then
  221. fn_printinfo "Compressing files using bzip2..."
  222. fn_scriptlog "Compressing files using bzip2..."
  223. sleep 2
  224. # bzip2 all files that are not already compressed (keeping original files)
  225. find "${fastdldir}" -not -name \*.bz2 -exec bzip2 -qk \{\} \;
  226. fn_printinfo "bzip2 compression done"
  227. fn_scriptlog "bzip2 compression done"
  228. sleep 1
  229. fi
  230. }
  231. # Generate lua file that will force download any file into the FastDL folder
  232. fn_lua_fastdl(){
  233. # Remove lua file if luaressource is turned off and file exists
  234. if [ "${luaressource}" == "off" ]; then
  235. if [ -f "${luafastdlfullpath}" ]; then
  236. echo "Removing download enforcer"
  237. sleep 1
  238. rm -R "${luafastdlfullpath}"
  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_printdots "Removing old download enforcer"
  245. sleep 1
  246. rm "${luafastdlfullpath}"
  247. fn_printok "Removed old download enforcer"
  248. fn_scriptlog "Removed old download inforcer"
  249. echo -en "\n"
  250. sleep 1
  251. fi
  252. fn_printdots "Generating new download enforcer"
  253. fn_scriptlog "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}" \( -name "." ! -name "*.bz2" \) -printf '%P\n' | while read line; do
  257. echo "resource.AddFile("\""${line}"\"")" >> "${luafastdlfullpath}"
  258. done
  259. fn_printok "Download enforcer generated"
  260. fn_scriptlog "Download enforcer generated"
  261. echo -en "\n"
  262. sleep 1
  263. fi
  264. }
  265. fn_fastdl_completed(){
  266. # Finished message
  267. fn_printok "Congratulations, it's done"
  268. fn_scriptlog "FastDL job done"
  269. echo "For more information, see https://github.com/dgibbs64/linuxgsm/wiki/Fastdl"
  270. echo -en "\n"
  271. if [ "$bzip2installed" == "0" ]; then
  272. echo "By the way, you'd better install bzip2 an re-run this command"
  273. fi
  274. }
  275. # Game checking and functions running
  276. # Garry's Mod
  277. if [ "${gamename}" == "Garry's Mod" ]; then
  278. fn_check_bzip2
  279. fn_fastdl_init
  280. fn_fastdl_config
  281. fn_fastdl_gmod_config
  282. fn_clear_old_fastdl
  283. fn_gmod_fastdl
  284. fn_lua_fastdl
  285. fn_fastdl_bzip2
  286. fn_fastdl_completed
  287. exit
  288. fi