command_fastdl.sh 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  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. echo -en "\n"
  38. fn_scriptlog "Started FastDL creation"
  39. sleep 1
  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. 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_printdots "Stupid file structure fix"
  217. sleep 1
  218. cp -Rf "${fastdldir}/lua/"* "${fastdldir}"
  219. fn_printok "Stupid file structure fixed"
  220. echo -en "\n"
  221. sleep 2
  222. fi
  223. }
  224. # Generate lua file that will force download any file into the FastDL folder
  225. fn_lua_fastdl(){
  226. # Remove lua file if luaressource is turned off and file exists
  227. if [ "${luaressource}" == "off" ]; then
  228. if [ -f "${luafastdlfullpath}" ]; then
  229. fn_printdots "Removing download enforcer"
  230. sleep 1
  231. rm -R "${luafastdlfullpath}"
  232. fn_printok "Removed download enforcer"
  233. fn_scriptlog "Removed old download inforcer"
  234. echo -en "\n"
  235. sleep 2
  236. fi
  237. fi
  238. # Remove old lua file and generate a new one if user said yes
  239. if [ "${luaressource}" == "on" ]; then
  240. if [ -f "${luafastdlfullpath}" ]; then
  241. fn_printdots "Removing old download enforcer"
  242. sleep 1
  243. rm "${luafastdlfullpath}"
  244. fn_printok "Removed old download enforcer"
  245. fn_scriptlog "Removed old download enforcer"
  246. echo -en "\n"
  247. sleep 1
  248. fi
  249. fn_printdots "Generating new download enforcer"
  250. fn_scriptlog "Generating new download enforcer"
  251. sleep 1
  252. # Read all filenames and put them into a lua file at the right path
  253. find "${fastdldir}" \( -name "." ! -name "*.bz2" \) -printf '%P\n' | while read line; do
  254. echo "resource.AddFile("\""${line}"\"")" >> ${luafastdlfullpath}
  255. done
  256. fn_printok "Download enforcer generated"
  257. fn_scriptlog "Download enforcer generated"
  258. echo -en "\n"
  259. echo ""
  260. sleep 2
  261. fi
  262. }
  263. fn_fastdl_bzip2(){
  264. # Compressing using bzip2 if user said yes
  265. if [ ${bzip2enable} == "on" ]; then
  266. fn_printdots "Compressing files using bzip2..."
  267. fn_scriptlog "Compressing files using bzip2..."
  268. sleep 2
  269. # bzip2 all files that are not already compressed (keeping original files)
  270. find "${fastdldir}" -not -name \*.bz2 -exec bzip2 -qk \{\} \;
  271. fn_printinfo "bzip2 compression done"
  272. fn_scriptlog "bzip2 compression done"
  273. sleep 1
  274. fi
  275. }
  276. fn_fastdl_completed(){
  277. # Finished message
  278. fn_printok "Congratulations, it's done"
  279. fn_scriptlog "FastDL job done"
  280. echo -en "\n"
  281. echo "For more information, see https://github.com/dgibbs64/linuxgsm/wiki/Fastdl"
  282. if [ "$bzip2installed" == "0" ]; then
  283. echo "By the way, you'd better install bzip2 an re-run this command !"
  284. fi
  285. echo "Credits : UltimateByte"
  286. }
  287. # Game checking and functions running
  288. # Garry's Mod
  289. if [ "${gamename}" == "Garry's Mod" ]; then
  290. fn_check_bzip2
  291. fn_fastdl_init
  292. fn_fastdl_config
  293. fn_fastdl_gmod_config
  294. fn_clear_old_fastdl
  295. fn_gmod_fastdl
  296. fn_lua_fastdl
  297. fn_fastdl_bzip2
  298. fn_fastdl_completed
  299. exit
  300. fi