command_fastdl.sh 8.9 KB

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