command_fastdl.sh 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  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. mkdir "${webdir}"
  56. fn_printok
  57. fn_scriptlog "FastDL created ${webdir}"
  58. sleep 1
  59. echo -en "\n"
  60. fi
  61. if [ ! -d "${fastdldir}" ]; then
  62. # No folder, won't ask for removing old ones
  63. newfastdl=1
  64. fn_printdots "Creating FastDL directory..."
  65. sleep 1
  66. mkdir "${fastdldir}"
  67. fn_scriptlog "FastDL created ${fastdldir}"
  68. fn_printok "Folders created"
  69. sleep 1
  70. echo -en "\n"
  71. else
  72. # Used to prompt for removing old files
  73. newfastdl=0
  74. fi
  75. }
  76. fn_fastdl_config(){
  77. # Global settings for FastDL creation
  78. fn_printinfo "Entering configuration"
  79. fn_scriptlog "Configuration"
  80. sleep 2
  81. echo -en "\n"
  82. # Prompt for clearing old files if folder was already here
  83. if [ ${newfastdl} == 0 ]; then
  84. fn_printdots
  85. while true; do
  86. read -p "Clear old FastDL files? [y/N]" yN
  87. case $yn in
  88. [Yy]* ) clearoldfastdl="on"; fn_scriptlog "clearoldfastdl enabled"; fn_printok "Clearing Enabled"; break;;
  89. [Nn]* ) clearoldfastdl="off"; fn_scriptlog "clearoldfastdl disabled"; fn_printok "Clearing Disabled"; break;;
  90. * ) echo "Please answer yes or no.";;
  91. esac
  92. done
  93. sleep 1
  94. echo -en "\n"
  95. fi
  96. # Prompt for using bzip2 if it's installed
  97. if [ ${bzip2installed} == 1 ]; then
  98. fn_printdots
  99. while true; do
  100. read -p "Enable file compression using bzip2? [Y/n]" Yn
  101. case $yn in
  102. [Yy]* ) bzip2enable="on"; fn_scriptlog "bzip2 enabled"; fn_printok "bzip2 Enabled"; break;;
  103. [Nn]* ) bzip2enable="off"; fn_scriptlog "bzip2 disabled"; fn_printok "bzip2 Disabled;" break;;
  104. * ) echo "Please answer yes or no.";;
  105. esac
  106. done
  107. sleep 1
  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 -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"; "DL enforcer Disabled"; fn_printok "Enforcer Disabled"; break;;
  119. * ) echo "Please answer yes or no.";;
  120. esac
  121. sleep 1
  122. echo -en "\n"
  123. done
  124. }
  125. fn_clear_old_fastdl(){
  126. # Clearing old FastDL if user answered yes
  127. if [ ${clearoldfastdl} == "on" ]; then
  128. fn_printinfo "Clearing existing FastDL folder"
  129. fn_scriptlog "Clearing existing FastDL folder"
  130. sleep 1
  131. rm -R "${fastdldir}"/*
  132. fn_printok "Old FastDL folder cleared"
  133. fn_scriptlog "Old FastDL folder cleared"
  134. sleep 1
  135. echo -en "\n"
  136. fi
  137. }
  138. fn_gmod_fastdl(){
  139. # Copy all needed files for fastDL
  140. fn_printdots "Gathering all needed files..."
  141. echo -en "\n"
  142. sleep 1
  143. # No choice to cd to the directory, as find can't then display relative folder
  144. cd "${systemdir}"
  145. # Map Files
  146. fn_printdots "Copying map files..."
  147. fn_scriptlog "Copying map files"
  148. sleep 1
  149. find . -name '*.bsp' | cpio --quiet -updm "${fastdldir}"
  150. fn_printok "Map files copied"
  151. sleep 1
  152. echo -en "\n"
  153. # Materials
  154. fn_printdots "Copying materials..."
  155. fn_scriptlog "Copying materials"
  156. sleep 1
  157. find . -name '*.vtf' | cpio --quiet -updm "${fastdldir}"
  158. find . -name '*.vmt' | cpio --quiet -updm "${fastdldir}"
  159. fn_printok "Materials copied"
  160. sleep 1
  161. echo -en "\n"
  162. # Models
  163. fn_printdots "Copying models..."
  164. fn_scriptlog "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_printok "Models copied"
  171. sleep 1
  172. echo -en "\n"
  173. # Particles
  174. fn_printdots "Copying particles..."
  175. fn_scriptlog "Copying particles"
  176. sleep 1
  177. find . -name '*.pcf' | cpio --quiet -updm "${fastdldir}"
  178. fn_printok "Particles copied"
  179. sleep 1
  180. echo -en "\n"
  181. # Sounds
  182. fn_printdots "Copying sounds..."
  183. fn_scriptlog "Copying sounds"
  184. sleep 1
  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_printok "Sounds copied"
  189. sleep 1
  190. echo -en "\n"
  191. # Resources (mostly fonts)
  192. fn_printdots "Copying fonts and png..."
  193. fn_scriptlog "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_printok "Fonts and png copied"
  199. sleep 1
  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_printinfo "Adjusting addons' file structure"
  206. fn_scriptlog "Adjusting addon's file structure"
  207. sleep 2
  208. cp -Rf "${fastdldir}"/addons/*/* "${fastdldir}"
  209. #Don't remove yet rm -R "${fastdldir}/addons"
  210. fn_printok "Adjusted addon's file structure"
  211. echo -en "\n"
  212. sleep 1
  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_printwarn "Typical DarkRP shit detected"
  217. sleep 2
  218. echo -en "\n"
  219. fn_printdots "Fixing DarkRP file structure..."
  220. sleep 2
  221. cp -Rf "${fastdldir}/lua/"* "${fastdldir}"
  222. fn_printok "Stupid file structure fixed"
  223. sleep 2
  224. echo -en "\n"
  225. fi
  226. }
  227. # Generate lua file that will force download any file into the FastDL folder
  228. fn_lua_fastdl(){
  229. # Remove lua file if luaressource is turned off and file exists
  230. if [ "${luaressource}" == "off" ]; then
  231. if [ -f "${luafastdlfullpath}" ]; then
  232. fn_printdots "Removing download enforcer"
  233. sleep 1
  234. rm -R "${luafastdlfullpath}"
  235. fn_printok "Removed download enforcer"
  236. fn_scriptlog "Removed old download inforcer"
  237. echo -en "\n"
  238. sleep 2
  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 enforcer"
  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. echo ""
  263. sleep 2
  264. fi
  265. }
  266. fn_fastdl_bzip2(){
  267. # Compressing using bzip2 if user said yes
  268. if [ ${bzip2enable} == "on" ]; then
  269. fn_printdots "Compressing files using bzip2..."
  270. fn_scriptlog "Compressing files using bzip2..."
  271. sleep 2
  272. # bzip2 all files that are not already compressed (keeping original files)
  273. find "${fastdldir}" -not -name \*.bz2 -exec bzip2 -qk \{\} \;
  274. fn_printinfo "bzip2 compression done"
  275. fn_scriptlog "bzip2 compression done"
  276. sleep 1
  277. fi
  278. }
  279. fn_fastdl_completed(){
  280. # Finished message
  281. fn_printok "Congratulations, it's done"
  282. fn_scriptlog "FastDL job done"
  283. echo -en "\n"
  284. echo "For more information, see https://github.com/dgibbs64/linuxgsm/wiki/Fastdl"
  285. if [ "$bzip2installed" == "0" ]; then
  286. echo "By the way, you'd better install bzip2 an re-run this command !"
  287. fi
  288. echo "Credits : UltimateByte"
  289. }
  290. # Game checking and functions running
  291. # Garry's Mod
  292. if [ "${gamename}" == "Garry's Mod" ]; then
  293. fn_check_bzip2
  294. fn_fastdl_init
  295. fn_fastdl_config
  296. fn_fastdl_gmod_config
  297. fn_clear_old_fastdl
  298. fn_gmod_fastdl
  299. fn_lua_fastdl
  300. fn_fastdl_bzip2
  301. fn_fastdl_completed
  302. exit
  303. fi