command_fastdl.sh 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  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. echo ""
  141. fn_printinfo "Starting..."
  142. fn_scriptlog "Starting FastFL job"
  143. sleep 2
  144. echo -en "\n"
  145. fn_printdots "Gathering all needed files..."
  146. sleep 1
  147. echo -en "\n"
  148. # No choice to cd to the directory, as find can't then display relative folder
  149. cd "${systemdir}"
  150. # Map Files
  151. fn_printdots "Copying map files..."
  152. fn_scriptlog "Copying map files"
  153. sleep 1
  154. find . -name '*.bsp' | cpio --quiet -updm "${fastdldir}"
  155. fn_printok "Map files copied"
  156. sleep 1
  157. echo -en "\n"
  158. # Materials
  159. fn_printdots "Copying materials..."
  160. fn_scriptlog "Copying materials"
  161. sleep 1
  162. find . -name '*.vtf' | cpio --quiet -updm "${fastdldir}"
  163. find . -name '*.vmt' | cpio --quiet -updm "${fastdldir}"
  164. fn_printok "Materials copied"
  165. sleep 1
  166. echo -en "\n"
  167. # Models
  168. fn_printdots "Copying models..."
  169. fn_scriptlog "Copying models"
  170. sleep 1
  171. find . -name '*.vtx' | cpio --quiet -updm "${fastdldir}"
  172. find . -name '*.vvd' | cpio --quiet -updm "${fastdldir}"
  173. find . -name '*.mdl' | cpio --quiet -updm "${fastdldir}"
  174. find . -name '*.phy' | cpio --quiet -updm "${fastdldir}"
  175. fn_printok "Models copied"
  176. sleep 1
  177. echo -en "\n"
  178. # Particles
  179. fn_printdots "Copying particles..."
  180. fn_scriptlog "Copying particles"
  181. sleep 1
  182. find . -name '*.pcf' | cpio --quiet -updm "${fastdldir}"
  183. fn_printok "Particles copied"
  184. sleep 1
  185. echo -en "\n"
  186. # Sounds
  187. fn_printdots "Copying sounds..."
  188. fn_scriptlog "Copying sounds"
  189. sleep 1
  190. find . -name '*.wav' | cpio --quiet -updm "${fastdldir}"
  191. find . -name '*.mp3' | cpio --quiet -updm "${fastdldir}"
  192. find . -name '*.ogg' | cpio --quiet -updm "${fastdldir}"
  193. fn_printok "Sounds copied"
  194. sleep 1
  195. echo -en "\n"
  196. # Resources (mostly fonts)
  197. fn_printdots "Copying fonts and png..."
  198. fn_scriptlog "Copying fonts and png"
  199. sleep 1
  200. find . -name '*.otf' | cpio --quiet -updm "${fastdldir}"
  201. find . -name '*.ttf' | cpio --quiet -updm "${fastdldir}"
  202. find . -name '*.png' | cpio --quiet -updm "${fastdldir}"
  203. fn_printok "Fonts and png copied"
  204. sleep 1
  205. echo -en "\n"
  206. # Going back to rootdir in order to prevent mistakes
  207. cd "${rootdir}"
  208. # Correct addons folder structure for FastDL
  209. if [ -d "${fastdldir}/addons" ]; then
  210. fn_printinfo "Adjusting addons' file structure"
  211. fn_scriptlog "Adjusting addon's file structure"
  212. sleep 2
  213. cp -Rf "${fastdldir}"/addons/*/* "${fastdldir}"
  214. #Don't remove yet rm -R "${fastdldir}/addons"
  215. fn_printok "Adjusted addon's file structure"
  216. echo -en "\n"
  217. sleep 1
  218. fi
  219. # Correct content that may be into a lua folder by mistake like some darkrpmodification addons
  220. if [ -d "${fastdldir}/lua" ]; then
  221. fn_printwarn "Typical DarkRP shit detected"
  222. sleep 2
  223. echo -en "\n"
  224. fn_printdots "Fixing DarkRP file structure..."
  225. sleep 2
  226. cp -Rf "${fastdldir}/lua/"* "${fastdldir}"
  227. fn_printok "Stupid file structure fixed"
  228. sleep 2
  229. echo -en "\n"
  230. fi
  231. }
  232. # Generate lua file that will force download any file into the FastDL folder
  233. fn_lua_fastdl(){
  234. # Remove lua file if luaressource is turned off and file exists
  235. if [ "${luaressource}" == "off" ]; then
  236. if [ -f "${luafastdlfullpath}" ]; then
  237. fn_printdots "Removing download enforcer"
  238. sleep 1
  239. rm -R "${luafastdlfullpath}"
  240. fn_printok "Removed download enforcer"
  241. fn_scriptlog "Removed old download inforcer"
  242. echo -en "\n"
  243. sleep 2
  244. fi
  245. fi
  246. # Remove old lua file and generate a new one if user said yes
  247. if [ "${luaressource}" == "on" ]; then
  248. if [ -f "${luafastdlfullpath}" ]; then
  249. fn_printdots "Removing old download enforcer"
  250. sleep 1
  251. rm "${luafastdlfullpath}"
  252. fn_printok "Removed old download enforcer"
  253. fn_scriptlog "Removed old download enforcer"
  254. echo -en "\n"
  255. sleep 1
  256. fi
  257. fn_printdots "Generating new download enforcer"
  258. fn_scriptlog "Generating new download enforcer"
  259. sleep 1
  260. # Read all filenames and put them into a lua file at the right path
  261. find "${fastdldir}" \( -name "." ! -name "*.bz2" \) -printf '%P\n' | while read line; do
  262. echo "resource.AddFile("\""${line}"\"")" >> ${luafastdlfullpath}
  263. done
  264. fn_printok "Download enforcer generated"
  265. fn_scriptlog "Download enforcer generated"
  266. echo -en "\n"
  267. echo ""
  268. sleep 2
  269. fi
  270. }
  271. fn_fastdl_bzip2(){
  272. # Compressing using bzip2 if user said yes
  273. if [ ${bzip2enable} == "on" ]; then
  274. fn_printdots "Compressing files using bzip2..."
  275. fn_scriptlog "Compressing files using bzip2..."
  276. sleep 2
  277. # bzip2 all files that are not already compressed (keeping original files)
  278. find "${fastdldir}" -not -name \*.bz2 -exec bzip2 -qk \{\} \;
  279. fn_printinfo "bzip2 compression done"
  280. fn_scriptlog "bzip2 compression done"
  281. sleep 1
  282. fi
  283. }
  284. fn_fastdl_completed(){
  285. # Finished message
  286. fn_printok "Congratulations, it's done"
  287. fn_scriptlog "FastDL job done"
  288. echo -en "\n"
  289. echo "For more information, see https://github.com/dgibbs64/linuxgsm/wiki/Fastdl"
  290. if [ "$bzip2installed" == "0" ]; then
  291. echo "By the way, you'd better install bzip2 an re-run this command !"
  292. fi
  293. echo "Credits : UltimateByte"
  294. }
  295. # Game checking and functions running
  296. # Garry's Mod
  297. if [ "${gamename}" == "Garry's Mod" ]; then
  298. fn_check_bzip2
  299. fn_fastdl_init
  300. fn_fastdl_config
  301. fn_fastdl_gmod_config
  302. fn_clear_old_fastdl
  303. fn_gmod_fastdl
  304. fn_lua_fastdl
  305. fn_fastdl_bzip2
  306. fn_fastdl_completed
  307. exit
  308. fi