command_fastdl.sh 8.7 KB

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