command_mods_install.sh 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. #!/bin/bash
  2. # LGSM command_mods_install.sh function
  3. # Author: Daniel Gibbs
  4. # Contributor: UltimateByte
  5. # Website: https://gameservermanagers.com
  6. # Description: List and installs available mods along with mods_list.sh.
  7. local commandname="MODS"
  8. local commandaction="Mod Installation"
  9. local function_selfname="$(basename $(readlink -f "${BASH_SOURCE[0]}"))"
  10. check.sh
  11. mods_list.sh
  12. fn_mods_install_init(){
  13. fn_script_log "Entering mods & addons installation"
  14. echo "================================="
  15. echo "${gamename} mods & addons installation"
  16. echo ""
  17. # Display available mods from mods_list.sh
  18. fn_mods_show_available
  19. echo ""
  20. # Keep prompting as long as the user input doesn't correspond to an available mod
  21. while [[ ! " ${availablemodscommands[@]} " =~ " ${usermodselect} " ]]; do
  22. echo -en "Enter a \e[36mmod\e[0m to install (or exit to abort): "
  23. read -r usermodselect
  24. # Exit if user says exit or abort
  25. if [ "${usermodselect}" == "exit" ]||[ "${usermodselect}" == "abort" ]; then
  26. fn_script_log "User aborted."
  27. echo "Aborted."
  28. core_exit.sh
  29. # Supplementary output upon invalid user input
  30. elif [[ ! " ${availablemodscommands[@]} " =~ " ${usermodselect} " ]]; then
  31. fn_print_error2_nl "${usermodselect} is not a valid mod."
  32. echo " * Enter a valid mod or input exit to abort."
  33. fi
  34. done
  35. # Gives a pretty name to the user and get all mod info
  36. currentmod="${usermodselect}"
  37. fn_mod_get_info_from_command
  38. fn_print_dots "Installing ${modprettyname}"
  39. sleep 1
  40. fn_script_log "Installing ${modprettyname}."
  41. }
  42. # Create mods directory if it doesn't exist
  43. # Assuming the game is already installed as mods_list.sh checked for it.
  44. fn_mods_dir(){
  45. if [ ! -d "${modinstalldir}" ]; then
  46. fn_script_log_info "Creating mods directory: ${modinstalldir}"
  47. fn_print_dots "Creating mods directory"
  48. sleep 1
  49. mkdir -p "${modinstalldir}"
  50. fn_print_ok_nl "Created mods directory"
  51. fi
  52. }
  53. # Clear mod download directory so that there is only one file in it since we don't the file name and extention
  54. fn_clear_tmp_mods(){
  55. if [ -d "${modstmpdir}" ]; then
  56. rm -r "${modstmpdir}"
  57. fn_script_log "Clearing temp mod download directory: ${modstmpdir}"
  58. fi
  59. }
  60. # Create tmp download mod directory
  61. fn_mods_tmpdir(){
  62. if [ ! -d "${modstmpdir}" ]; then
  63. mkdir -p "${modstmpdir}"
  64. fn_script_log "Creating temp mod download directory: ${modstmpdir}"
  65. fi
  66. }
  67. fn_mod_dl(){
  68. # fn_fetch_file "${fileurl}" "${filedir}" "${filename}" "${executecmd}" "${run}" "${force}" "${md5}"
  69. fileurl="${modurl}"
  70. filedir="${modstmpdir}"
  71. filename="${modfilename}"
  72. fn_script_log "Downloading mods to ${modstmpdir}"
  73. fn_fetch_file "${fileurl}" "${filedir}" "${filename}"
  74. # Check if variable is valid checking if file has been downloaded and exists
  75. if [ ! -f "${modstmpdir}/${modfilename}" ]; then
  76. fn_print_fail "An issue occurred upon downloading ${modprettyname}"
  77. core_exit.sh
  78. fi
  79. }
  80. fn_mod_extract(){
  81. # fn_dl_extract "${filedir}" "${filename}" "${extractdir}"
  82. filename="${modfilename}"
  83. extractdir="${modstmpdir}/extracted"
  84. if [ ! -d "${extractdir}" ]; then
  85. mkdir -p "${extractdir}"
  86. fi
  87. fn_script_log "Extracting ${modprettyname} to ${extractdir}"
  88. fn_dl_extract "${filedir}" "${filename}" "${extractdir}"
  89. }
  90. fn_mod_fileslist(){
  91. # ${modsdatadir}/${modcommand}-files.list
  92. true;
  93. }
  94. fn_mod_copy_destination(){
  95. # Destination directory: ${modinstalldir}
  96. fn_script_log "Copying ${modprettyname} to ${modinstalldir}"
  97. cp -Rf "${extractdir}/." "${modinstalldir}/"
  98. }
  99. # Check if the mod is already installed and warn the user
  100. fn_mod_already_installed(){
  101. if [ -f "${modslockfilefullpath}" ]; then
  102. if [ -n "$(cat "${modslockfilefullpath}" | grep "${modcommand}")" ]; then
  103. echo ""
  104. fn_print_warning_nl "${modprettyname} has already been installed."
  105. echo " * Mod files will be overwritten."
  106. sleep 4
  107. fi
  108. fi
  109. }
  110. # Add the mod to the installed mods list
  111. fn_mod_add_list(){
  112. # Create lgsm/data directory
  113. if [ ! -d "${modsdatadir}" ]; then
  114. mkdir -p "${modsdatadir}"
  115. fn_script_log "Created ${modsdatadir}"
  116. fi
  117. # Create lgsm/data/${modslockfile}
  118. if [ ! -f "${modslockfilefullpath}" ]; then
  119. touch "${modslockfilefullpath}"
  120. fn_script_log "Created ${modslockfilefullpath}"
  121. fi
  122. # Input mod name to lockfile
  123. if [ ! -n "$(cat "${modslockfilefullpath}" | grep "${modcommand}")" ]; then
  124. echo "${modcommand}" >> "${modslockfilefullpath}"
  125. fn_script_log "${modcommand} added to ${modslockfile}"
  126. fi
  127. }
  128. # Run all required operation
  129. fn_mod_installation(){
  130. # If a mod was selected
  131. if [ -n "${currentmod}" ]; then
  132. # Get mod info
  133. fn_mod_get_info_from_command
  134. # Check if mod is already installed
  135. fn_mod_already_installed
  136. # Check and create required directories
  137. fn_mods_dir
  138. # Clear lgsm/tmp/mods dir if exists then recreate it
  139. fn_clear_tmp_mods
  140. fn_mods_tmpdir
  141. # Download mod
  142. fn_mod_dl
  143. # Extract the mod
  144. fn_mod_extract
  145. # Build a file list
  146. fn_mod_fileslist
  147. # Copying to destination
  148. fn_mod_copy_destination
  149. # Ending with installation routines
  150. fn_mod_add_list
  151. fn_clear_tmp_mods
  152. fn_print_ok_nl "${modprettyname} installed."
  153. fn_script_log "${modprettyname} installed."
  154. else
  155. fn_print_fail "No mod was selected."
  156. core_exit.sh
  157. fi
  158. }
  159. fn_mods_install_checks
  160. fn_mods_install_init
  161. fn_mod_installation