mods_core.sh 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. #!/bin/bash
  2. # LGSM command_mods_install.sh function
  3. # Author: Daniel Gibbs
  4. # Contributor: UltimateByte
  5. # Website: https://gameservermanagers.com
  6. # Description: Core functions for mods list/install/update/remove
  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. extractdir="${modstmpdir}/extracted"
  14. modsdatadir="${lgsmdir}/data/mods"
  15. modslockfile="installed-mods-listing"
  16. modslockfilefullpath="${modsdatadir}/${modslockfile}"
  17. # Create mods directory if it doesn't exist
  18. # Assuming the game is already installed as mods_list.sh checked for it.
  19. fn_mods_dir(){
  20. if [ ! -d "${modinstalldir}" ]; then
  21. fn_script_log_info "Creating mods directory: ${modinstalldir}"
  22. fn_print_dots "Creating mods directory"
  23. sleep 1
  24. mkdir -p "${modinstalldir}"
  25. fn_print_ok_nl "Created mods directory"
  26. fi
  27. }
  28. # Clear mod download directory so that there is only one file in it since we don't the file name and extention
  29. fn_clear_tmp_mods(){
  30. if [ -d "${modstmpdir}" ]; then
  31. rm -r "${modstmpdir}"
  32. fn_script_log "Clearing temp mod download directory: ${modstmpdir}"
  33. fi
  34. # Clear temp file list as well
  35. if [ -f "${modsdatadir}/.removedfiles.tmp" ]; then
  36. rm "${modsdatadir}/.removedfiles.tmp"
  37. fi
  38. }
  39. # Create tmp download mod directory
  40. fn_mods_tmpdir(){
  41. if [ ! -d "${modstmpdir}" ]; then
  42. mkdir -p "${modstmpdir}"
  43. fn_script_log "Creating temp mod download directory: ${modstmpdir}"
  44. fi
  45. }
  46. fn_mod_dl(){
  47. # fn_fetch_file "${fileurl}" "${filedir}" "${filename}" "${executecmd}" "${run}" "${force}" "${md5}"
  48. fileurl="${modurl}"
  49. filedir="${modstmpdir}"
  50. filename="${modfilename}"
  51. fn_script_log "Downloading mods to ${modstmpdir}"
  52. fn_fetch_file "${fileurl}" "${filedir}" "${filename}"
  53. # Check if variable is valid checking if file has been downloaded and exists
  54. if [ ! -f "${modstmpdir}/${modfilename}" ]; then
  55. fn_print_fail "An issue occurred upon downloading ${modprettyname}"
  56. core_exit.sh
  57. fi
  58. }
  59. fn_mod_extract(){
  60. # fn_dl_extract "${filedir}" "${filename}" "${extractdir}"
  61. filename="${modfilename}"
  62. if [ ! -d "${extractdir}" ]; then
  63. mkdir -p "${extractdir}"
  64. fi
  65. fn_script_log "Extracting ${modprettyname} to ${extractdir}"
  66. fn_dl_extract "${filedir}" "${filename}" "${extractdir}"
  67. }
  68. fn_mod_lowercase(){
  69. # Converting files to lowercase
  70. if [ "${modlowercase}" == "LowercaseOn" ]; then
  71. fn_print_dots "Converting ${modprettyname} files to lowercase"
  72. fn_script_log "Converting ${modprettyname} files to lowercase"
  73. find "${extractdir}" -depth -exec rename 's/(.*)\/([^\/]*)/$1\/\L$2/' {} \;
  74. fn_print_ok "Converting ${modprettyname} files to lowercase"
  75. sleep 1
  76. fi
  77. }
  78. fn_remove_cfg_files(){
  79. # Remove config file after extraction for updates set by ${modkeepfiles}
  80. if [ "${modkeepfiles}" != "OVERWRITE" ]&&[ "${modkeepfiles}" != "NOUPDATE" ]; then
  81. # Upon mods updates, config files should not be overwritten
  82. # We will just remove these files before copying the mod to the destination
  83. # Let's count how many files there are to remove
  84. removefilesamount="$(echo "${modkeepfiles}" | awk -F ';' '{ print NF }')"
  85. # Test all subvalue of "modgames" using the ";" separator
  86. for ((removefilesindex=1; removefilesindex < ${removefilesamount}; removefilesindex++)); do
  87. # Put current file we're looking for into a variable
  88. filetoremove="$( echo "${modkeepfiles}" | awk -F ';' -v x=${removefilesindex} '{ print $x }' )"
  89. # If it matches an existing file that have been extracted
  90. if [ -f "${extractdir}/${filetoremove}" ]||[ -d "${extractdir}/${filetoremove}" ]; then
  91. # Then delete the file!
  92. rm -R "${extractdir}/${filetoremove}"
  93. # Write this file path in a tmp file, to rebuild a full file list
  94. if [ ! -f "${modsdatadir}/.removedfiles.tmp" ]; then
  95. touch "${modsdatadir}/.removedfiles.tmp"
  96. fi
  97. echo "${filetoremove}" > "${modsdatadir}/.removedfiles.tmp"
  98. fi
  99. done
  100. fi
  101. }
  102. fn_mod_fileslist(){
  103. # Create lgsm/data/mods directory
  104. if [ ! -d "${modsdatadir}" ]; then
  105. mkdir -p "${modsdatadir}"
  106. fn_script_log "Created ${modsdatadir}"
  107. fi
  108. # ${modsdatadir}/${modcommand}-files.list
  109. find "${extractdir}" -mindepth 1 -printf '%P\n' > ${modsdatadir}/${modcommand}-files.list
  110. fn_script_log "Writing file list: ${modsdatadir}/${modcommand}-files.list}"
  111. # Adding removed files if needed
  112. if [ -f "${modsdatadir}/.removedfiles.tmp" ]; then
  113. cat "${modsdatadir}/.removedfiles.tmp" >> ${modsdatadir}/${modcommand}-files.list
  114. fi
  115. }
  116. fn_mod_copy_destination(){
  117. # Destination directory: ${modinstalldir}
  118. fn_script_log "Copying ${modprettyname} to ${modinstalldir}"
  119. cp -Rf "${extractdir}/." "${modinstalldir}/"
  120. }
  121. # Check if the mod is already installed and warn the user
  122. fn_mod_already_installed(){
  123. if [ -f "${modslockfilefullpath}" ]; then
  124. if [ -n "$(cat "${modslockfilefullpath}" | grep "${modcommand}")" ]; then
  125. echo ""
  126. fn_print_warning_nl "${modprettyname} has already been installed."
  127. echo " * Config files might be overwritten."
  128. echo " * Press ctrl + c to abort."
  129. sleep 4
  130. fi
  131. fi
  132. }
  133. # Add the mod to the installed mods list
  134. fn_mod_add_list(){
  135. # Create lgsm/data/mods directory
  136. if [ ! -d "${modsdatadir}" ]; then
  137. mkdir -p "${modsdatadir}"
  138. fn_script_log "Created ${modsdatadir}"
  139. fi
  140. # Create lgsm/data/${modslockfile}
  141. if [ ! -f "${modslockfilefullpath}" ]; then
  142. touch "${modslockfilefullpath}"
  143. fn_script_log "Created ${modslockfilefullpath}"
  144. fi
  145. # Input mod name to lockfile
  146. if [ ! -n "$(cat "${modslockfilefullpath}" | grep "${modcommand}")" ]; then
  147. echo "${modcommand}" >> "${modslockfilefullpath}"
  148. fn_script_log "${modcommand} added to ${modslockfile}"
  149. fi
  150. }
  151. fn_check_files_list(){
  152. # File list must exist and be valid before any operation on it
  153. if [ -f "${modsdatadir}/${modcommand}-files.list" ]; then
  154. # How many lines is the file list
  155. modsfilelistsize="$(cat "${modsdatadir}/${modcommand}-files.list" | wc -l)"
  156. # If file list is empty
  157. if [ $modsfilelistsize -eq 0 ]; then
  158. fn_print_error_nl "${modcommand}-files.list is empty"
  159. echo "Exiting."
  160. fn_scrip_log_fatal "${modcommand}-files.list is empty"
  161. exitcode="2"
  162. core_exit.sh
  163. fi
  164. else
  165. fn_print_error_nl "${modsdatadir}/${modcommand}-files.list don't exist"
  166. echo "Exiting."
  167. fn_scrip_log_fatal "${modsdatadir}/${modcommand}-files.list don't exist"
  168. exitcode="2"
  169. core_exit.sh
  170. fi
  171. }
  172. fn_postinstall_tasks(){
  173. # Prevent addons folder from being removed by clearing them in: ${modsdatadir}/${modcommand}-files.list
  174. # Check file validity
  175. fn_check_files_list
  176. # Output to the user
  177. fn_print_dots "Rearranging ${modcommand}-files.list"
  178. fn_script_log_info "Rearranging ${modcommand}-files.list"
  179. # What lines/files to remove from file list
  180. removefromlist="cfg;addons;"
  181. # Loop through files to remove from file list,
  182. # that way these files won't get removed upon uninstall
  183. # How many elements to remove from list
  184. removefromlistamount="$(echo "${removefromlist}" | awk -F ';' '{ print NF }')"
  185. # Test all subvalue of "removefromlist" using the ";" separator
  186. for ((filesindex=1; filesindex < ${removefromlistamount}; filesindex++)); do
  187. # Put current file into test variable
  188. removefilevar="$( echo "${removefromlist}" | awk -F ';' -v x=${filesindex} '{ print $x }' )"
  189. # Then delete matching line(s)!
  190. sed -i "/^${removefilevar}$/d" "${modsdatadir}/${modcommand}-files.list"
  191. done
  192. fn_print_ok "Rearranging ${modcommand}-files.list"
  193. }