command_fastdl.sh 8.1 KB

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