command_fastdl.sh 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  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_printdots "Creating www directory..."
  52. sleep 0.5
  53. mkdir "${webdir}"
  54. sleep 1
  55. fn_scriptlog "FastDL created ${webdir}"
  56. fi
  57. if [ ! -d "${fastdldir}" ]; then
  58. newfastdl=1
  59. fn_printdots "Creating FastDL directory..."
  60. sleep 0.5
  61. mkdir "${fastdldir}"
  62. sleep 1
  63. fn_scriptlog "FastDL created ${fastdldir}"
  64. fn_printok "Folders created"
  65. echo -en "\n"
  66. else
  67. newfastdl=0
  68. fi
  69. }
  70. fn_fastdl_config(){
  71. fn_printinfo "Entering configuration"
  72. fn_scriptlog "Configuration"
  73. echo -en "\n"
  74. sleep 2
  75. if [ ${newfastdl} == 0 ]; then
  76. fn_printdots
  77. while true; do
  78. read -p "Clear old FastDL files? [y/n]" yn
  79. case $yn in
  80. [Yy]* ) clearoldfastdl="on"; fn_scriptlog "clearoldfastdl enabled"; fn_printok "Clearing Enabled"; break;;
  81. [Nn]* ) clearoldfastdl="off"; fn_scriptlog "clearoldfastdl disabled"; fn_printok "Clearing Disabled"; break;;
  82. * ) echo "Please answer yes or no.";;
  83. esac
  84. done
  85. sleep 1
  86. echo -en "\n"
  87. fi
  88. if [ ${bzip2installed} == 1 ]; then
  89. fn_printdots
  90. sleep 1
  91. while true; do
  92. read -p "Enable file compression using bzip2?? [Y/n]" Yn
  93. case $yn in
  94. [Yy]* ) bzip2enable="on"; fn_scriptlog "bzip2 enabled"; fn_printok "bzip2 Enabled"; break;;
  95. [Nn]* ) bzip2enable="off"; fn_scriptlog "bzip2 disabled"; fn_printok "bzip2 Disabled;" break;;
  96. * ) echo "Please answer yes or no.";;
  97. esac
  98. done
  99. sleep 1
  100. echo -en "\n"
  101. fi
  102. }
  103. fn_fastdl_gmod_config(){
  104. # Prompt for download enforcer, that is using a .lua addfile resource generator
  105. fn_printdots
  106. while true; do
  107. read -p "Use client download enforcer? [y/n]" yn
  108. case $yn in
  109. [Yy]* ) luaressource="on"; fn_scriptlog "DL enforcer Enabled"; fn_printok "Enforcer Enabled"; break;;
  110. [Nn]* ) luaressource="off"; "DL enforcer Disabled"; fn_printok "Enforcer Disabled"; break;;
  111. * ) echo "Please answer yes or no.";;
  112. esac
  113. sleep1
  114. echo -en "\n"
  115. done
  116. }
  117. fn_clear_old_fastdl(){
  118. if [ clearoldfastdl == "on" ]; then
  119. fn_printinfo "Clearing existing FastDL folder"
  120. fn_scriptlog "Clearing existing FastDL folder"
  121. sleep 1
  122. rm -R "${fastdldir}"/*
  123. fn_printok "Old FastDL folder cleared"
  124. fn_scriptlog "Old FastDL folder cleared"
  125. echo -en "\n"
  126. fi
  127. }
  128. fn_gmod_fastdl(){
  129. # Copy all needed files for fastDL
  130. fn_printdots "Gathering all needed FastDL files..."
  131. echo -en "\n"
  132. sleep 1
  133. # Map Files
  134. fn_printdots "Copying map files..."
  135. fn_scriptlog "Copying map files"
  136. sleep 1
  137. find "${addonsdir}" -name '*.bsp' | cpio -updm "${fastdldir}"
  138. fn_printok
  139. echo -en "\n"
  140. sleep 1
  141. # Materials
  142. fn_printdots "Copying materials"
  143. fn_scriptlog "Copying materials"
  144. sleep 1
  145. find "${addonsdir}" -name '*.vtf' | cpio -updm "${fastdldir}"
  146. find "${addonsdir}" -name '*.vmt' | cpio -updm "${fastdldir}"
  147. fn_printok
  148. echo -en "\n"
  149. sleep 1
  150. # Models
  151. fn_printdots "Copying models"
  152. fn_scriptlog "Copying models"
  153. sleep 1
  154. find "${addonsdir}" -name '*.vtx' | cpio -updm "${fastdldir}"
  155. find "${addonsdir}" -name '*.vvd' | cpio -updm "${fastdldir}"
  156. find "${addonsdir}" -name '*.mdl' | cpio -updm "${fastdldir}"
  157. find "${addonsdir}" -name '*.phy' | cpio -updm "${fastdldir}"
  158. fn_printok
  159. echo -en "\n"
  160. sleep 1
  161. # Particles
  162. fn_printdots "Copying particles"
  163. fn_scriptlog "Copying particles"
  164. sleep 1
  165. find "${addonsdir}" -name '*.pcf' | cpio -updm "${fastdldir}"
  166. fn_printok
  167. echo -en "\n"
  168. sleep 1
  169. # Sounds
  170. fn_printdots "Copying sounds"
  171. fn_scriptlog "Copying sounds"
  172. sleep 1
  173. find "${addonsdir}" -name '*.wav' | cpio -updm "${fastdldir}"
  174. find "${addonsdir}" -name '*.mp3' | cpio -updm "${fastdldir}"
  175. find "${addonsdir}" -name '*.ogg' | cpio -updm "${fastdldir}"
  176. fn_printok
  177. echo -en "\n"
  178. sleep 1
  179. # Resources (mostly fonts)
  180. fn_printdots "Copying fonts and png"
  181. fn_scriptlog "Copying fonts and png"
  182. sleep 1
  183. find "${addonsdir}" -name '*.otf' | cpio -updm "${fastdldir}"
  184. find "${addonsdir}" -name '*.ttf' | cpio -updm "${fastdldir}"
  185. find "${addonsdir}" -name '*.png' | cpio -updm "${fastdldir}"
  186. fn_printok
  187. echo -en "\n"
  188. sleep 1
  189. # Correct addons folder structure
  190. if [ -d "${fastdldir}/addons" ]; then
  191. echo "Possible FastDL files found into addons"
  192. echo "Copying those files to their correct folder"
  193. sleep 2
  194. cp -Rf "${fastdldir}"/addons/*/* "${fastdldir}"
  195. # As we're not sure about the correct file structure, duplicate instead of remove
  196. # rm -R "${fastdldir}/addons"
  197. fn_printok
  198. echo -en "\n"
  199. sleep 1
  200. fi
  201. # Correct content that may be into a lua folder by mistake like some darkrpmodification addons
  202. if [ -d "${fastdldir}/lua" ]; then
  203. fn_printdots "Stupid filestructure fix"
  204. sleep 1
  205. cp -Rf "${fastdldir}/lua/"* "${fastdldir}"
  206. fn_printok
  207. echo -en "\n"
  208. sleep 1
  209. fi
  210. }
  211. fn_fastdl_bzip2(){
  212. if [ ${bzip2enable} == "on" ]; then
  213. fn_printinfo "Compressing files using bzip2..."
  214. fn_scriptlog "Compressing files using bzip2..."
  215. sleep 2
  216. # bzip2 all files that are not already compressed (keeping original files)
  217. find "${fastdldir}" -not -name \*.bz2 -exec bzip2 -k \{\} \;
  218. fn_printinfo "bzip2 compression done"
  219. fn_scriptlog "bzip2 compression done"
  220. sleep 1
  221. fi
  222. }
  223. # Generate lua file that will force download any file into the FastDL folder
  224. fn_lua_fastdl(){
  225. # Remove lua file if luaressource is turned off
  226. if [ "${luaressource}" == "off" ]; then
  227. if [ -f "${luafastdlfullpath}" ]; then
  228. echo "Removing download enforcer"
  229. sleep 1
  230. rm -R "${luafastdlfullpath}"
  231. fi
  232. fi
  233. if [ "${luaressource}" == "on" ]; then
  234. if [ -f "${luafastdlfullpath}" ]; then
  235. echo "Removing old download enforcer"
  236. sleep 1
  237. rm "${luafastdlfullpath}"
  238. fi
  239. echo "Generating new download enforcer"
  240. sleep 1
  241. find "${fastdldir}" \( -name "." ! -name "*.bz2" \) -printf '%P\n' | while read line; do
  242. echo "resource.AddFile("\""${line}"\"")" >> "${luafastdlfullpath}"
  243. done
  244. echo "Download enforcer generated"
  245. sleep 1
  246. fi
  247. }
  248. fn_fastdl_completed(){
  249. echo "----------------------------------"
  250. echo "Congratulations, it's done"
  251. echo "Now you should configure your HTTP server to target the fastdl folder that was created in ${fastdldir}"
  252. echo "Or copy files to an external server"
  253. echo "Don't forget to change your sv_downloadurl accordingly in ${servercfgfullpath}"
  254. echo "You may want to use the www folder to host a loadingurl too,"
  255. echo "for that purpose, just make a loadingurl folder next to the fastdl folder and put your loadingurl in it"
  256. if [ "$bzip2installed" == "0" ]; then
  257. echo "By the way, you'd better install bzip2 an re-run this command"
  258. fi
  259. echo "----------------------------------"
  260. echo "For more information, see https://github.com/dgibbs64/linuxgsm/wiki/Fastdl"
  261. }
  262. # Game checking
  263. # Garry's Mod
  264. if [ "${gamename}" == "Garry's Mod" ]; then
  265. fn_check_bzip2
  266. fn_fastdl_init
  267. fn_fastdl_config
  268. fn_fastdl_gmod_config
  269. fn_clear_old_fastdl
  270. fn_gmod_fastdl
  271. if [ "${bzip2installed}" == "1" ]; then
  272. fn_fastdl_bzip2
  273. fi
  274. fn_lua_fastdl
  275. fn_fastdl_completed
  276. exit
  277. fi