command_fastdl.sh 8.8 KB

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