command_fastdl.sh 8.8 KB

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