mods_core.sh 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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. # Clear temp file list as well
  34. if [ -f "${modsdatadir}/.removedfiles.tmp" ]; then
  35. rm "${modsdatadir}/.removedfiles.tmp"
  36. fi
  37. }
  38. # Create tmp download mod directory
  39. fn_mods_tmpdir(){
  40. if [ ! -d "${modstmpdir}" ]; then
  41. mkdir -p "${modstmpdir}"
  42. fn_script_log "Creating temp mod download directory: ${modstmpdir}"
  43. fi
  44. }
  45. fn_mod_dl(){
  46. # fn_fetch_file "${fileurl}" "${filedir}" "${filename}" "${executecmd}" "${run}" "${force}" "${md5}"
  47. fileurl="${modurl}"
  48. filedir="${modstmpdir}"
  49. filename="${modfilename}"
  50. fn_script_log "Downloading mods to ${modstmpdir}"
  51. fn_fetch_file "${fileurl}" "${filedir}" "${filename}"
  52. # Check if variable is valid checking if file has been downloaded and exists
  53. if [ ! -f "${modstmpdir}/${modfilename}" ]; then
  54. fn_print_fail "An issue occurred upon downloading ${modprettyname}"
  55. core_exit.sh
  56. fi
  57. }
  58. fn_mod_extract(){
  59. # fn_dl_extract "${filedir}" "${filename}" "${extractdir}"
  60. filename="${modfilename}"
  61. extractdir="${modstmpdir}/extracted"
  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. removefilesamount="$(echo "${modkeepfiles}" | awk -F ';' '{ print NF }')"
  84. # Test all subvalue of "modgames" using the ";" separator
  85. for ((removefilesindex=1; removefilesindex < ${removefilesamount}; removefilesindex++)); do
  86. # Put current game name into modtest variable
  87. removefiletest="$( echo "${modkeepfiles}" | awk -F ';' -v x=${removefilesindex} '{ print $x }' )"
  88. # If it matches
  89. if [ -f "${extractdir}/${removefiletest}" ]||[ -d "${extractdir}/${removefiletest}" ]; then
  90. # Then delete the file!
  91. rm -R "${extractdir}/${removefiletest}"
  92. # Write this file path in a tmp file, to rebuild a full file list
  93. if [ ! -f "${modsdatadir}/.removedfiles.tmp" ]; then
  94. touch "${modsdatadir}/.removedfiles.tmp"
  95. fi
  96. echo "${removefiletest}" > ${modsdatadir}/.removedfiles.tmp"
  97. fi
  98. done
  99. fi
  100. }
  101. fn_mod_fileslist(){
  102. # Create lgsm/data/mods directory
  103. if [ ! -d "${modsdatadir}" ]; then
  104. mkdir -p "${modsdatadir}"
  105. fn_script_log "Created ${modsdatadir}"
  106. fi
  107. # ${modsdatadir}/${modcommand}-files.list
  108. find "${extractdir}" -mindepth 1 -printf '%P\n' > ${modsdatadir}/${modcommand}-files.list
  109. fn_script_log "Writing file list: ${modsdatadir}/${modcommand}-files.list}"
  110. # Adding removed files if needed
  111. if [ -f "${modsdatadir}/.removedfiles.tmp" ]; then
  112. cat "${modsdatadir}/.removedfiles.tmp" >> ${modsdatadir}/${modcommand}-files.list
  113. fi
  114. }
  115. fn_mod_copy_destination(){
  116. # Destination directory: ${modinstalldir}
  117. fn_script_log "Copying ${modprettyname} to ${modinstalldir}"
  118. cp -Rf "${extractdir}/." "${modinstalldir}/"
  119. }
  120. # Check if the mod is already installed and warn the user
  121. fn_mod_already_installed(){
  122. if [ -f "${modslockfilefullpath}" ]; then
  123. if [ -n "$(cat "${modslockfilefullpath}" | grep "${modcommand}")" ]; then
  124. echo ""
  125. fn_print_warning_nl "${modprettyname} has already been installed."
  126. echo " * Config files might be overwritten."
  127. echo " * Press ctrl + c to abort."
  128. sleep 4
  129. fi
  130. fi
  131. }
  132. # Add the mod to the installed mods list
  133. fn_mod_add_list(){
  134. # Create lgsm/data/mods directory
  135. if [ ! -d "${modsdatadir}" ]; then
  136. mkdir -p "${modsdatadir}"
  137. fn_script_log "Created ${modsdatadir}"
  138. fi
  139. # Create lgsm/data/${modslockfile}
  140. if [ ! -f "${modslockfilefullpath}" ]; then
  141. touch "${modslockfilefullpath}"
  142. fn_script_log "Created ${modslockfilefullpath}"
  143. fi
  144. # Input mod name to lockfile
  145. if [ ! -n "$(cat "${modslockfilefullpath}" | grep "${modcommand}")" ]; then
  146. echo "${modcommand}" >> "${modslockfilefullpath}"
  147. fn_script_log "${modcommand} added to ${modslockfile}"
  148. fi
  149. }
  150. fn_check_files_list(){
  151. # File list must exist and be valid before any operation on it
  152. if [ -f "${modsdatadir}/${modcommand}-files.list" ]; then
  153. # How many lines is the file list
  154. modsfilelistsize="$(cat "${modsdatadir}/${modcommand}-files.list" | wc -l)"
  155. # If file list is empty
  156. if [ $modsfilelistsize -eq 0 ]; then
  157. fn_print_error_nl "${modcommand}-files.list is empty"
  158. echo "Exiting."
  159. fn_scrip_log_fatal "${modcommand}-files.list is empty"
  160. exitcode="2"
  161. core_exit.sh
  162. fi
  163. else
  164. fn_print_error_nl "${modsdatadir}/${modcommand}-files.list don't exist"
  165. echo "Exiting."
  166. fn_scrip_log_fatal "${modsdatadir}/${modcommand}-files.list don't exist"
  167. exitcode="2"
  168. core_exit.sh
  169. fi
  170. }
  171. fn_postinstall_tasks(){
  172. # Sourcemod, but any other game as well should never delete "cfg" or "addons" folder
  173. # Prevent addons folder from being removed by clearing them in: ${modsdatadir}/${modcommand}-files.list
  174. fn_check_files_list
  175. # Output to the user
  176. fn_print_information_nl "Rearranging ${modcommand}-files.list"
  177. fn_script_log_info "Rearranging ${modcommand}-files.list"
  178. smremovefromlist="cfg;addons"
  179. # Loop through every single line to find any of the files to remove from the list
  180. # that way these files won't get removed upon update or uninstall
  181. fileslistline=1
  182. while [ $fileslistline -le $modsfilelistsize ]; do
  183. testline="$(sed "${fileslistline}q;d" "${modsdatadir}/${modcommand}-files.list")"
  184. # How many elements to remove from list
  185. smremoveamount="$(echo "${smremovefromlist}" | awk -F ';' '{ print NF }')"
  186. # Test all subvalue of "modkeepfiles" using the ";" separator
  187. for ((filesindex=1; filesindex < ${smremoveamount}; filesindex++)); do
  188. # Put current file into test variable
  189. smremovetestvar="$( echo "${smremovefromlist}" | awk -F ';' -v x=${filesindex} '{ print $x }' )"
  190. # If it matches
  191. if [ "${testline}" == "${smremovetestvar}" ]; then
  192. # Then delete the line!
  193. sed -i "${testline}d" "${modsdatadir}/${modcommand}-files.list"
  194. fi
  195. done
  196. let fileslistline=fileslistline+1
  197. done
  198. }