command_fastdl.sh 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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. # Server lua autorun dir, used to autorun lua on client connect to the server
  15. luasvautorundir="${systemdir}/lua/audoturn/server"
  16. luafastdlfile="lgsm_cl_force_fastdl.lua"
  17. luafastdlfullpath="${luasvautorundir}/${luafastdlfile}"
  18. fn_fastdl_init(){
  19. # User confirmation for starting process
  20. echo "Generate a FastDL Folder ?"
  21. while true; do
  22. read -p "Continue? [y/N]" yn
  23. case $yn in
  24. [Yy]* ) break;;
  25. [Nn]* ) exit;;
  26. * ) echo "Please answer yes or no.";;
  27. esac
  28. done
  29. # Create FastDL folder if it doesn't exist
  30. if [ ! -d "${webdir}" ]; then
  31. echo "Creating www directory"
  32. mkdir -v "${webdir}"
  33. sleep 1
  34. fi
  35. if [ ! -d "${fastdldir}" ]; then
  36. echo "Creating FastDL directory"
  37. mkdir -v "${fastdldir}"
  38. sleep 1
  39. else
  40. echo "Updating FastDL..."
  41. fi
  42. # Ask for lua resource add file use
  43. echo "Do you wish to generate a lua file to force clients to download all FastDL content ?"
  44. echo "It is useful for many addons where devs didn't register their files to be downloaded through FastDL."
  45. while true; do
  46. read -p "Continue? [y/n]" yn
  47. case $yn in
  48. [Yy]* ) luaressource="on"; break;;
  49. [Nn]* ) luaressource="off"; return 0;;
  50. * ) echo "Please answer yes or no.";;
  51. esac
  52. done
  53. }
  54. fn_gmod_fastdl(){
  55. # Copy all needed files for fastDL
  56. echo "Gathering all needed FastDL files..."
  57. sleep 1
  58. cd "${systemdir}"
  59. # Map Files
  60. echo "Copying map files"
  61. sleep 1
  62. find . -name '*.bsp' | cpio -updm "${fastdldir}"
  63. echo "Done"
  64. sleep 1
  65. # Materials
  66. echo "Copying Materials"
  67. sleep 1
  68. find . -name '*.vtf' | cpio -updm "${fastdldir}"
  69. find . -name '*.vmt' | cpio -updm "${fastdldir}"
  70. echo "Done"
  71. sleep 1
  72. # Models
  73. echo "Copying Models"
  74. sleep 1
  75. find . -name '*.vtx' | cpio -updm "${fastdldir}"
  76. find . -name '*.vvd' | cpio -updm "${fastdldir}"
  77. find . -name '*.mdl' | cpio -updm "${fastdldir}"
  78. find . -name '*.phy' | cpio -updm "${fastdldir}"
  79. echo "Done"
  80. sleep 1
  81. # Particles
  82. echo "Copying Particles"
  83. sleep 1
  84. find . -name '*.pcf' | cpio -updm "${fastdldir}"
  85. echo "Done"
  86. sleep 1
  87. # Sounds
  88. echo "Copying Sounds"
  89. sleep 1
  90. find . -name '*.wav' | cpio -updm "${fastdldir}"
  91. find . -name '*.mp3' | cpio -updm "${fastdldir}"
  92. find . -name '*.ogg' | cpio -updm "${fastdldir}"
  93. echo "Done"
  94. sleep 1
  95. # Resources (mostly fonts)
  96. echo "Copying fonts and png"
  97. sleep 1
  98. find . -name '*.otf' | cpio -updm "${fastdldir}"
  99. find . -name '*.ttf' | cpio -updm "${fastdldir}"
  100. find . -name '*.png' | cpio -updm "${fastdldir}"
  101. echo "Done"
  102. sleep 1
  103. # Going back to scriptfolder to avoid mistakes
  104. cd "${rootdir}"
  105. # Correct addons folder structure
  106. if [ -d "${fastdldir}/addons" ]; then
  107. echo "Possible FastDL files found into addons"
  108. echo "Moving those files to their correct folder"
  109. sleep 2
  110. cp -Rf "${fastdldir}"/addons/*/* "${fastdldir}"
  111. rm -R "${fastdldir}/addons"
  112. echo "Done"
  113. sleep 1
  114. fi
  115. # Correct content that may be into a lua folder by mistake like some darkrpmodification addons
  116. if [ -d "${fastdldir}/lua" ]; then
  117. echo "Some FastDL files (often addons in darkrpmodifications) may be in the wrong folder"
  118. sleep 1
  119. echo "Copying those files to their hopefully correct locations"
  120. sleep 1
  121. cp -Rf "${fastdldir}/lua/"* "${fastdldir}"
  122. echo "Done"
  123. sleep 1
  124. fi
  125. }
  126. # bzip2 compression
  127. fn_check_bzip2(){
  128. # Returns true if not installed
  129. if [ -z "$(command -v bzip2)" ]; then
  130. bzip2installed="0"
  131. echo "WARNING bzip2 packed is not installed !"
  132. sleep 2
  133. echo "You can't compress your FastDL files !"
  134. sleep 2
  135. echo "Loading time won't be as good as possible for your players."
  136. sleep 2
  137. echo "It's advised that your install bzip2 and re-run the fastdl command."
  138. sleep 3
  139. else
  140. bzip2installed="1"
  141. fi
  142. }
  143. fn_fastdl_bzip2(){
  144. echo "Do you want to compress files using bzip2 for even faster client download ?"
  145. echo "It may take a while..."
  146. while true; do
  147. read -p "Continue? [y/N]" yn
  148. case $yn in
  149. [Yy]* ) break;;
  150. [Nn]* ) return 0;;
  151. * ) echo "Please answer yes or no.";;
  152. esac
  153. done
  154. echo "Compressing files using bzip2..."
  155. sleep 2
  156. # bzip2 all files that are not already compressed (keeping original files)
  157. find "${fastdldir}" -not -name \*.bz2 -exec bzip2 -k \{\} \;
  158. echo "bzip2 compression done"
  159. sleep 1
  160. }
  161. # Generate lua file that will force download any file into the FastDL folder
  162. fn_lua_fastdl(){
  163. # Remove lua file if luaressource is turned off
  164. if [ "${luaressource}" == "off" ]; then
  165. if [ -f "${luafastdlfullpath}" ]; then
  166. echo "Removing download enforcer"
  167. sleep 1
  168. rm -R "${luafastdlfullpath}"
  169. fi
  170. fi
  171. if [ "${luaressource}" == "on" ]; then
  172. if [ -f "${luafastdlfullpath}" ]; then
  173. echo "Removing old download enforcer"
  174. sleep 1
  175. rm "${luafastdlfullpath}"
  176. fi
  177. echo "Generating new download enforcer"
  178. sleep 1
  179. find "${fastdldir}" \( -name "." ! -name "*.bz2" \) -printf '%P\n' | while read line; do
  180. echo "resource.AddFile("\""${line}"\"")" >> "${luafastdlfullpath}"
  181. done
  182. echo "Download enforcer generated"
  183. sleep 1
  184. fi
  185. }
  186. fn_fastdl_completed(){
  187. echo "----------------------------------"
  188. echo "Congratulations, it's done"
  189. echo "Now you should configure your HTTP server to target the fastdl folder that was created in ${fastdldir}"
  190. echo "Or copy files to an external server"
  191. echo "Don't forget to change your sv_downloadurl accordingly in ${servercfgfullpath}"
  192. echo "You may want to use the www folder to host a loadingurl too,"
  193. echo "for that purpose, just make a loadingurl folder next to the fastdl folder and put your loadingurl in it"
  194. if [ "$bzip2installed" == "0" ]; then
  195. echo "By the way, you'd better install bzip2 an re-run this command"
  196. fi
  197. echo "----------------------------------"
  198. }
  199. # Game checking
  200. # Garry's Mod
  201. if [ "${gamename}" == "Garry's Mod" ]; then
  202. fn_fastdl_init
  203. fn_gmod_fastdl
  204. fn_check_bzip2
  205. if [ "${bzip2installed}" == "1" ]; then
  206. fn_fastdl_bzip2
  207. fi
  208. fn_lua_fastdl
  209. fn_fastdl_completed
  210. exit
  211. fi