command_mods_update.sh 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. #!/bin/bash
  2. # LGSM command_mods_update.sh function
  3. # Author: Daniel Gibbs
  4. # Contributor: UltimateByte
  5. # Website: https://gameservermanagers.com
  6. # Description: Updates installed mods along with mods_list.sh and mods_core.sh.
  7. local commandname="MODS"
  8. local commandaction="Mods Update"
  9. local function_selfname="$(basename $(readlink -f "${BASH_SOURCE[0]}"))"
  10. check.sh
  11. mods_core.sh
  12. fn_print_header
  13. echo "Update addons/mods"
  14. echo "================================="
  15. fn_mods_check_installed
  16. fn_print_information_nl "${installedmodscount} addons/mods will be updated"
  17. fn_script_log_info "${installedmodscount} mods or addons will be updated"
  18. fn_mods_installed_list
  19. echo ""
  20. echo "Installed addons/mods"
  21. echo "================================="
  22. # Go through all available commands, get details and display them to the user
  23. for ((ulindex=0; ulindex < ${#installedmodslist[@]}; ulindex++)); do
  24. # Current mod is the "ulindex" value of the array we're going through
  25. currentmod="${installedmodslist[ulindex]}"
  26. fn_mod_get_info
  27. # Display installed mods and the update policy
  28. if [ -z "${modkeepfiles}" ]; then
  29. # If modkeepfiles is not set for some reason, that's a problem
  30. fn_script_log_error "Couldn't find update policy for ${modprettyname}"
  31. fn_print_error_nl "Couldn't find update policy for ${modprettyname}"
  32. exitcode="1"
  33. core_exit.sh
  34. # If the mod won't get updated
  35. elif [ "${modkeepfiles}" == "NOUPDATE" ]; then
  36. echo -e " * \e[31m${modprettyname}${default} (won't be updated)"
  37. # If the mode is just overwritten
  38. elif [ "${modkeepfiles}" == "OVERWRITE" ]; then
  39. echo -e " * \e[1m${modprettyname}${default} (overwrite)"
  40. else
  41. echo -e " * ${yellow}${modprettyname}${default} (common custom files remain untouched)"
  42. fi
  43. done
  44. ## Update
  45. # List all installed mods and apply update
  46. # Reset line value
  47. installedmodsline="1"
  48. while [ ${installedmodsline} -le ${installedmodscount} ]; do
  49. currentmod="$(sed "${installedmodsline}q;d" "${modslockfilefullpath}")"
  50. if [ -n "${currentmod}" ]; then
  51. fn_mod_get_info
  52. # Don not update mod if the policy is set to "NOUPDATE"
  53. if [ "${modkeepfiles}" == "NOUPDATE" ]; then
  54. fn_print_info "${modprettyname} will not be updated to preserve custom files"
  55. fn_script_log_info "${modprettyname} will not be updated to preserve custom files"
  56. else
  57. echo ""
  58. fn_create_mods_dir
  59. fn_mods_clear_tmp_dir
  60. fn_mods_create_tmp_dir
  61. fn_mod_install_files
  62. fn_mod_lowercase
  63. fn_remove_cfg_files
  64. fn_mod_create_filelist
  65. fn_mod_copy_destination
  66. fn_mod_add_list
  67. fn_mod_tidy_files_list
  68. fn_mods_clear_tmp_dir
  69. fi
  70. ((installedmodsline++))
  71. else
  72. fn_print_fail "No mod was selected"
  73. fn_script_log_fail "No mod was selected"
  74. exitcode="1"
  75. core_exit.sh
  76. fi
  77. done
  78. echo ""
  79. fn_print_ok_nl "Mods update complete"
  80. fn_script_log "Mods update complete"
  81. # Prevents specific files being overwritten upon update (set by ${modkeepfiles})
  82. # For that matter, remove cfg files after extraction before copying them to destination
  83. fn_remove_cfg_files(){
  84. if [ "${modkeepfiles}" != "OVERWRITE" ]&&[ "${modkeepfiles}" != "NOUPDATE" ]; then
  85. fn_print_dots "Preventing overwriting of ${modprettyname} config files"
  86. fn_script_log "Preventing overwriting of ${modprettyname} config files"
  87. sleep 0.5
  88. # Count how many files there are to remove
  89. removefilesamount="$(echo "${modkeepfiles}" | awk -F ';' '{ print NF }')"
  90. # Test all subvalues of "modkeepfiles" using the ";" separator
  91. for ((removefilesindex=1; removefilesindex < ${removefilesamount}; removefilesindex++)); do
  92. # Put the current file we are looking for into a variable
  93. filetoremove="$( echo "${modkeepfiles}" | awk -F ';' -v x=${removefilesindex} '{ print $x }' )"
  94. # If it matches an existing file that have been extracted delete the file
  95. if [ -f "${extractdir}/${filetoremove}" ]||[ -d "${extractdir}/${filetoremove}" ]; then
  96. rm -r "${extractdir}/${filetoremove}"
  97. # Write the file path in a tmp file, to rebuild a full file list as it is rebuilt upon update
  98. if [ ! -f "${modsdir}/.removedfiles.tmp" ]; then
  99. touch "${modsdir}/.removedfiles.tmp"
  100. fi
  101. echo "${filetoremove}" >> "${modsdir}/.removedfiles.tmp"
  102. fi
  103. done
  104. fn_print_ok "Preventing overwriting of ${modprettyname} config files"
  105. sleep 0.5
  106. fi
  107. }
  108. core_exit.sh