mods_core.sh 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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="Core functions for mods"
  9. local function_selfname="$(basename $(readlink -f "${BASH_SOURCE[0]}"))"
  10. ## Useful variables
  11. # Files and Directories
  12. modstmpdir="${tmpdir}/mods"
  13. modsdatadir="${lgsmdir}/data/mods"
  14. modslockfile="installed-mods-listing"
  15. modslockfilefullpath="${modsdatadir}/${modslockfile}"
  16. # Create mods directory if it doesn't exist
  17. # Assuming the game is already installed as mods_list.sh checked for it.
  18. fn_mods_dir(){
  19. if [ ! -d "${modinstalldir}" ]; then
  20. fn_script_log_info "Creating mods directory: ${modinstalldir}"
  21. fn_print_dots "Creating mods directory"
  22. sleep 1
  23. mkdir -p "${modinstalldir}"
  24. fn_print_ok_nl "Created mods directory"
  25. fi
  26. }
  27. # Clear mod download directory so that there is only one file in it since we don't the file name and extention
  28. fn_clear_tmp_mods(){
  29. if [ -d "${modstmpdir}" ]; then
  30. rm -r "${modstmpdir}"
  31. fn_script_log "Clearing temp mod download directory: ${modstmpdir}"
  32. fi
  33. }
  34. # Create tmp download mod directory
  35. fn_mods_tmpdir(){
  36. if [ ! -d "${modstmpdir}" ]; then
  37. mkdir -p "${modstmpdir}"
  38. fn_script_log "Creating temp mod download directory: ${modstmpdir}"
  39. fi
  40. }
  41. fn_mod_dl(){
  42. # fn_fetch_file "${fileurl}" "${filedir}" "${filename}" "${executecmd}" "${run}" "${force}" "${md5}"
  43. fileurl="${modurl}"
  44. filedir="${modstmpdir}"
  45. filename="${modfilename}"
  46. fn_script_log "Downloading mods to ${modstmpdir}"
  47. fn_fetch_file "${fileurl}" "${filedir}" "${filename}"
  48. # Check if variable is valid checking if file has been downloaded and exists
  49. if [ ! -f "${modstmpdir}/${modfilename}" ]; then
  50. fn_print_fail "An issue occurred upon downloading ${modprettyname}"
  51. core_exit.sh
  52. fi
  53. }
  54. fn_mod_extract(){
  55. # fn_dl_extract "${filedir}" "${filename}" "${extractdir}"
  56. filename="${modfilename}"
  57. extractdir="${modstmpdir}/extracted"
  58. if [ ! -d "${extractdir}" ]; then
  59. mkdir -p "${extractdir}"
  60. fi
  61. fn_script_log "Extracting ${modprettyname} to ${extractdir}"
  62. fn_dl_extract "${filedir}" "${filename}" "${extractdir}"
  63. }
  64. fn_mod_fileslist(){
  65. # ${modsdatadir}/${modcommand}-files.list
  66. find "${extractdir}" -mindepth 1 -printf '%P\n' >> ${modsdatadir}/${modcommand}-files.list
  67. fn_script_log "Writing file list: ${modsdatadir}/${modcommand}-files.list}"
  68. }
  69. fn_mod_copy_destination(){
  70. # Destination directory: ${modinstalldir}
  71. fn_script_log "Copying ${modprettyname} to ${modinstalldir}"
  72. cp -Rf "${extractdir}/." "${modinstalldir}/"
  73. }
  74. # Check if the mod is already installed and warn the user
  75. fn_mod_already_installed(){
  76. if [ -f "${modslockfilefullpath}" ]; then
  77. if [ -n "$(cat "${modslockfilefullpath}" | grep "${modcommand}")" ]; then
  78. echo ""
  79. fn_print_warning_nl "${modprettyname} has already been installed."
  80. echo " * Mod files will be overwritten."
  81. sleep 4
  82. fi
  83. fi
  84. }
  85. # Add the mod to the installed mods list
  86. fn_mod_add_list(){
  87. # Create lgsm/data directory
  88. if [ ! -d "${modsdatadir}" ]; then
  89. mkdir -p "${modsdatadir}"
  90. fn_script_log "Created ${modsdatadir}"
  91. fi
  92. # Create lgsm/data/${modslockfile}
  93. if [ ! -f "${modslockfilefullpath}" ]; then
  94. touch "${modslockfilefullpath}"
  95. fn_script_log "Created ${modslockfilefullpath}"
  96. fi
  97. # Input mod name to lockfile
  98. if [ ! -n "$(cat "${modslockfilefullpath}" | grep "${modcommand}")" ]; then
  99. echo "${modcommand}" >> "${modslockfilefullpath}"
  100. fn_script_log "${modcommand} added to ${modslockfile}"
  101. fi
  102. }