mods_core.sh 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  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. # Database initialization
  18. mods_list.sh
  19. # Sets some gsm requirements
  20. fn_gsm_requirements(){
  21. # If tmpdir variable doesn't exist, LGSM is too old
  22. if [ -z "${tmpdir}" ]||[ -z "${lgsmdir}" ]; then
  23. fn_print_fail "Your LGSM version is too old."
  24. echo " * Please do a full update, including ${selfname} script."
  25. core_exit.sh
  26. fi
  27. }
  28. # Create mods directory if it doesn't exist
  29. # Assuming the game is already installed as mods_list.sh checked for it.
  30. fn_mods_dir(){
  31. if [ ! -d "${modinstalldir}" ]; then
  32. fn_script_log_info "Creating mods directory: ${modinstalldir}"
  33. fn_print_dots "Creating mods directory"
  34. sleep 0.5
  35. mkdir -p "${modinstalldir}"
  36. fn_print_ok "Created mods directory"
  37. sleep 0.5
  38. fi
  39. }
  40. # Clear mod download directory so that there is only one file in it since we don't the file name and extention
  41. fn_clear_tmp_mods(){
  42. if [ -d "${modstmpdir}" ]; then
  43. rm -r "${modstmpdir}"
  44. fn_script_log "Clearing temp mod download directory: ${modstmpdir}"
  45. fi
  46. # Clear temp file list as well
  47. if [ -f "${modsdatadir}/.removedfiles.tmp" ]; then
  48. rm "${modsdatadir}/.removedfiles.tmp"
  49. fi
  50. }
  51. # Create tmp download mod directory
  52. fn_mods_tmpdir(){
  53. if [ ! -d "${modstmpdir}" ]; then
  54. mkdir -p "${modstmpdir}"
  55. fn_script_log "Creating temp mod download directory: ${modstmpdir}"
  56. fi
  57. }
  58. fn_mod_dl(){
  59. # fn_fetch_file "${fileurl}" "${filedir}" "${filename}" "${executecmd}" "${run}" "${force}" "${md5}"
  60. fileurl="${modurl}"
  61. filedir="${modstmpdir}"
  62. filename="${modfilename}"
  63. fn_script_log "Downloading mods to ${modstmpdir}"
  64. fn_fetch_file "${fileurl}" "${filedir}" "${filename}"
  65. # Check if variable is valid checking if file has been downloaded and exists
  66. if [ ! -f "${modstmpdir}/${modfilename}" ]; then
  67. fn_print_fail "An issue occurred upon downloading ${modprettyname}"
  68. core_exit.sh
  69. fi
  70. }
  71. fn_mod_extract(){
  72. # fn_dl_extract "${filedir}" "${filename}" "${extractdir}"
  73. filename="${modfilename}"
  74. if [ ! -d "${extractdir}" ]; then
  75. mkdir -p "${extractdir}"
  76. fi
  77. fn_script_log "Extracting ${modprettyname} to ${extractdir}"
  78. fn_dl_extract "${filedir}" "${filename}" "${extractdir}"
  79. }
  80. fn_mod_lowercase(){
  81. # Converting files to lowercase
  82. if [ "${modlowercase}" == "LowercaseOn" ]; then
  83. fn_print_dots "Converting ${modprettyname} files to lowercase"
  84. sleep 0.5
  85. fn_script_log "Converting ${modprettyname} files to lowercase"
  86. find "${extractdir}" -depth -exec rename 's/(.*)\/([^\/]*)/$1\/\L$2/' {} \;
  87. fn_print_ok "Converting ${modprettyname} files to lowercase"
  88. sleep 0.5
  89. fi
  90. }
  91. fn_remove_cfg_files(){
  92. # Remove config file after extraction for updates set by ${modkeepfiles}
  93. if [ "${modkeepfiles}" != "OVERWRITE" ]&&[ "${modkeepfiles}" != "NOUPDATE" ]; then
  94. # Upon mods updates, config files should not be overwritten
  95. # We will just remove these files before copying the mod to the destination
  96. # Let's count how many files there are to remove
  97. fn_print_dots "Allow for preserving ${modprettyname} config files"
  98. sleep 0.5
  99. removefilesamount="$(echo "${modkeepfiles}" | awk -F ';' '{ print NF }')"
  100. # Test all subvalue of "modgames" using the ";" separator
  101. for ((removefilesindex=1; removefilesindex < ${removefilesamount}; removefilesindex++)); do
  102. # Put current file we're looking for into a variable
  103. filetoremove="$( echo "${modkeepfiles}" | awk -F ';' -v x=${removefilesindex} '{ print $x }' )"
  104. # If it matches an existing file that have been extracted
  105. if [ -f "${extractdir}/${filetoremove}" ]||[ -d "${extractdir}/${filetoremove}" ]; then
  106. # Then delete the file!
  107. rm -R "${extractdir}/${filetoremove}"
  108. # Write this file path in a tmp file, to rebuild a full file list
  109. if [ ! -f "${modsdatadir}/.removedfiles.tmp" ]; then
  110. touch "${modsdatadir}/.removedfiles.tmp"
  111. fi
  112. echo "${filetoremove}" >> "${modsdatadir}/.removedfiles.tmp"
  113. fi
  114. done
  115. fn_print_ok "Allow for preserving ${modprettyname} config files"
  116. sleep 0.5
  117. fi
  118. }
  119. fn_mod_fileslist(){
  120. # Create lgsm/data/mods directory
  121. if [ ! -d "${modsdatadir}" ]; then
  122. mkdir -p "${modsdatadir}"
  123. fn_script_log "Created ${modsdatadir}"
  124. fi
  125. fn_print_dots "Building ${modcommand}-files.list"
  126. fn_script_log "Building ${modcommand}-files.list"
  127. sleep 0.5
  128. # ${modsdatadir}/${modcommand}-files.list
  129. find "${extractdir}" -mindepth 1 -printf '%P\n' > ${modsdatadir}/${modcommand}-files.list
  130. fn_script_log "Writing file list: ${modsdatadir}/${modcommand}-files.list}"
  131. # Adding removed files if needed
  132. if [ -f "${modsdatadir}/.removedfiles.tmp" ]; then
  133. cat "${modsdatadir}/.removedfiles.tmp" >> ${modsdatadir}/${modcommand}-files.list
  134. fi
  135. fn_print_ok "Building ${modcommand}-files.list"
  136. sleep 0.5
  137. }
  138. fn_mod_copy_destination(){
  139. # Destination directory: ${modinstalldir}
  140. fn_print_dots "Copying ${modprettyname} to ${modinstalldir}"
  141. fn_script_log "Copying ${modprettyname} to ${modinstalldir}"
  142. sleep 0.5
  143. cp -Rf "${extractdir}/." "${modinstalldir}/"
  144. fn_print_ok "Copying ${modprettyname} to ${modinstalldir}"
  145. sleep 0.5
  146. }
  147. # Check if the mod is already installed and warn the user
  148. fn_mod_already_installed(){
  149. if [ -f "${modslockfilefullpath}" ]; then
  150. if [ -n "$(sed -n "/^${modcommand}$/p" "${modslockfilefullpath}")" ]; then
  151. fn_print_warning_nl "${modprettyname} has already been installed"
  152. sleep 1
  153. echo " * Config files, if any, might be overwritten."
  154. echo " * Press ctrl + c to abort."
  155. sleep 3
  156. fi
  157. fn_script_log "${modprettyname} is already installed, overwriting any file."
  158. fi
  159. }
  160. # Add the mod to the installed mods list
  161. fn_mod_add_list(){
  162. # Create lgsm/data/mods directory
  163. if [ ! -d "${modsdatadir}" ]; then
  164. mkdir -p "${modsdatadir}"
  165. fn_script_log "Created ${modsdatadir}"
  166. fi
  167. # Create lgsm/data/${modslockfile}
  168. if [ ! -f "${modslockfilefullpath}" ]; then
  169. touch "${modslockfilefullpath}"
  170. fn_script_log "Created ${modslockfilefullpath}"
  171. fi
  172. # Input mod name to lockfile
  173. if [ ! -n "$(sed -n "/^${modcommand}$/p" "${modslockfilefullpath}")" ]; then
  174. echo "${modcommand}" >> "${modslockfilefullpath}"
  175. fn_script_log "${modcommand} added to ${modslockfile}"
  176. fi
  177. }
  178. fn_check_files_list(){
  179. # File list must exist and be valid before any operation on it
  180. if [ -f "${modsdatadir}/${modcommand}-files.list" ]; then
  181. # How many lines is the file list
  182. modsfilelistsize="$(cat "${modsdatadir}/${modcommand}-files.list" | wc -l)"
  183. # If file list is empty
  184. if [ $modsfilelistsize -eq 0 ]; then
  185. fn_print_error_nl "${modcommand}-files.list is empty"
  186. echo "Exiting."
  187. fn_scrip_log_fatal "${modcommand}-files.list is empty"
  188. exitcode="2"
  189. core_exit.sh
  190. fi
  191. else
  192. fn_print_error_nl "${modsdatadir}/${modcommand}-files.list don't exist"
  193. echo "Exiting."
  194. fn_scrip_log_fatal "${modsdatadir}/${modcommand}-files.list don't exist"
  195. exitcode="2"
  196. core_exit.sh
  197. fi
  198. }
  199. fn_postinstall_tasks(){
  200. # Prevent addons folder from being removed by clearing them in: ${modsdatadir}/${modcommand}-files.list
  201. # Check file validity
  202. fn_check_files_list
  203. # Output to the user
  204. fn_print_dots "Rearranging ${modcommand}-files.list"
  205. sleep 0.5
  206. fn_script_log_info "Rearranging ${modcommand}-files.list"
  207. # What lines/files to remove from file list
  208. removefromlist="cfg;addons;"
  209. # Loop through files to remove from file list,
  210. # that way these files won't get removed upon uninstall
  211. # How many elements to remove from list
  212. removefromlistamount="$(echo "${removefromlist}" | awk -F ';' '{ print NF }')"
  213. # Test all subvalue of "removefromlist" using the ";" separator
  214. for ((filesindex=1; filesindex < ${removefromlistamount}; filesindex++)); do
  215. # Put current file into test variable
  216. removefilevar="$( echo "${removefromlist}" | awk -F ';' -v x=${filesindex} '{ print $x }' )"
  217. # Then delete matching line(s)!
  218. sed -i "/^${removefilevar}$/d" "${modsdatadir}/${modcommand}-files.list"
  219. done
  220. # Sourcemod fix
  221. # Remove metamod from sourcemod fileslist
  222. if [ "${modcommand}" == "sourcemod" ]; then
  223. # Remove addons/metamod & addons/metamod/sourcemod.vdf from ${modcommand}-files.list
  224. sed -i "/^addons\/metamod$/d" "${modsdatadir}/${modcommand}-files.list"
  225. sed -i "/^addons\/metamod\/sourcemod.vdf$/d" "${modsdatadir}/${modcommand}-files.list"
  226. fi
  227. fn_print_ok "Rearranging ${modcommand}-files.list"
  228. sleep 0.5
  229. }
  230. ## mods_list.sh arrays
  231. # Define all variables from a mod at once when index is set to a separator
  232. fn_mod_info(){
  233. # If for some reason no index is set, none of this can work
  234. if [ -z "$index" ]; then
  235. fn_print_error "index variable not set. Please report an issue to LGSM Team."
  236. echo "* https://github.com/GameServerManagers/LinuxGSM/issues"
  237. core_exit.sh
  238. fi
  239. modcommand="${mods_global_array[index+1]}"
  240. modprettyname="${mods_global_array[index+2]}"
  241. modurl="${mods_global_array[index+3]}"
  242. modfilename="${mods_global_array[index+4]}"
  243. modsubdirs="${mods_global_array[index+5]}"
  244. modlowercase="${mods_global_array[index+6]}"
  245. modinstalldir="${mods_global_array[index+7]}"
  246. modkeepfiles="${mods_global_array[index+8]}"
  247. modengines="${mods_global_array[index+9]}"
  248. modgames="${mods_global_array[index+10]}"
  249. modexcludegames="${mods_global_array[index+11]}"
  250. modsite="${mods_global_array[index+12]}"
  251. moddescription="${mods_global_array[index+13]}"
  252. }
  253. # Find out if a game is compatible with a mod from a modgames (list of games supported by a mod) variable
  254. fn_compatible_mod_games(){
  255. # Reset test value
  256. modcompatiblegame="0"
  257. # If value is set to GAMES (ignore)
  258. if [ "${modgames}" != "GAMES" ]; then
  259. # How many games we need to test
  260. gamesamount="$(echo "${modgames}" | awk -F ';' '{ print NF }')"
  261. # Test all subvalue of "modgames" using the ";" separator
  262. for ((gamevarindex=1; gamevarindex < ${gamesamount}; gamevarindex++)); do
  263. # Put current game name into modtest variable
  264. gamemodtest="$( echo "${modgames}" | awk -F ';' -v x=${gamevarindex} '{ print $x }' )"
  265. # If game name matches
  266. if [ "${gamemodtest}" == "${gamename}" ]; then
  267. # Mod is compatible !
  268. modcompatiblegame="1"
  269. fi
  270. done
  271. fi
  272. }
  273. # Find out if an engine is compatible with a mod from a modengines (list of engines supported by a mod) variable
  274. fn_compatible_mod_engines(){
  275. # Reset test value
  276. modcompatibleengine="0"
  277. # If value is set to ENGINES (ignore)
  278. if [ "${modengines}" != "ENGINES" ]; then
  279. # How many engines we need to test
  280. enginesamount="$(echo "${modengines}" | awk -F ';' '{ print NF }')"
  281. # Test all subvalue of "modengines" using the ";" separator
  282. for ((gamevarindex=1; gamevarindex < ${enginesamount}; gamevarindex++)); do
  283. # Put current engine name into modtest variable
  284. enginemodtest="$( echo "${modengines}" | awk -F ';' -v x=${gamevarindex} '{ print $x }' )"
  285. # If engine name matches
  286. if [ "${enginemodtest}" == "${engine}" ]; then
  287. # Mod is compatible !
  288. modcompatibleengine="1"
  289. fi
  290. done
  291. fi
  292. }
  293. # Find out if a game is not compatible with a mod from a modnotgames (list of games not supported by a mod) variable
  294. fn_not_compatible_mod_games(){
  295. # Reset test value
  296. modeincompatiblegame="0"
  297. # If value is set to NOTGAMES (ignore)
  298. if [ "${modexcludegames}" != "NOTGAMES" ]; then
  299. # How many engines we need to test
  300. excludegamesamount="$(echo "${modexcludegames}" | awk -F ';' '{ print NF }')"
  301. # Test all subvalue of "modexcludegames" using the ";" separator
  302. for ((gamevarindex=1; gamevarindex < ${excludegamesamount}; gamevarindex++)); do
  303. # Put current engine name into modtest variable
  304. excludegamemodtest="$( echo "${modexcludegames}" | awk -F ';' -v x=${gamevarindex} '{ print $x }' )"
  305. # If engine name matches
  306. if [ "${excludegamemodtest}" == "${gamename}" ]; then
  307. # Mod is compatible !
  308. modeincompatiblegame="1"
  309. fi
  310. done
  311. fi
  312. }
  313. # Sums up if a mod is compatible or not with modcompatibility=0/1
  314. fn_mod_compatible_test(){
  315. # Test game and engine compatibility
  316. fn_compatible_mod_games
  317. fn_compatible_mod_engines
  318. fn_not_compatible_mod_games
  319. if [ "${modeincompatiblegame}" == "1" ]; then
  320. modcompatibility="0"
  321. elif [ "${modcompatibleengine}" == "1" ]||[ "${modcompatiblegame}" == "1" ]; then
  322. modcompatibility="1"
  323. else
  324. modcompatibility="0"
  325. fi
  326. }
  327. # Checks if a mod is compatibile for installation
  328. # Provides available mods for installation
  329. # Provides commands for mods installation
  330. fn_mods_available(){
  331. # First, reset variables
  332. compatiblemodslist=()
  333. availablemodscommands=()
  334. modprettynamemaxlength="0"
  335. modsitemaxlength="0"
  336. moddescriptionmaxlength="0"
  337. modcommandmaxlength="0"
  338. # Find compatible games
  339. # Find separators through the global array
  340. for ((index="0"; index <= ${#mods_global_array[@]}; index++)); do
  341. # If current value is a separator; then
  342. if [ "${mods_global_array[index]}" == "${modseparator}" ]; then
  343. # Set mod variables
  344. fn_mod_info
  345. # Test if game is compatible
  346. fn_mod_compatible_test
  347. # If game is compatible
  348. if [ "${modcompatibility}" == "1" ]; then
  349. # Put it into an array to prepare user output
  350. compatiblemodslist+=( "${modprettyname}" "${modcommand}" "${modsite}" "${moddescription}" )
  351. # Keep available commands in an array to make life easier
  352. availablemodscommands+=( "${modcommand}" )
  353. fi
  354. fi
  355. done
  356. }
  357. # Output available mods in a nice way to the user
  358. fn_mods_show_available(){
  359. # Set and reset vars
  360. compatiblemodslistindex=0
  361. spaces=" "
  362. # As long as we're within index values
  363. while [ "${compatiblemodslistindex}" -lt "${#compatiblemodslist[@]}" ]; do
  364. # Set values for convenience
  365. displayedmodname="${compatiblemodslist[compatiblemodslistindex]}"
  366. displayedmodcommand="${compatiblemodslist[compatiblemodslistindex+1]}"
  367. displayedmodsite="${compatiblemodslist[compatiblemodslistindex+2]}"
  368. displayedmoddescription="${compatiblemodslist[compatiblemodslistindex+3]}"
  369. # Output mods to the user
  370. echo -e "\e[1m${displayedmodname}\e[0m - ${displayedmoddescription} - ${displayedmodsite}"
  371. echo -e " * \e[36m${displayedmodcommand}\e[0m"
  372. # Increment index from the amount of values we just displayed
  373. let "compatiblemodslistindex+=4"
  374. done
  375. # If no mods are found
  376. if [ -z "${compatiblemodslist}" ]; then
  377. fn_print_fail "No mods are currently available for ${gamename}."
  378. core_exit.sh
  379. fi
  380. }
  381. # Get details of a mod any (relevant and unique, such as full mod name or install command) value
  382. fn_mod_get_info_from_command(){
  383. # Variable to know when job is done
  384. modinfocommand="0"
  385. # Find entry in global array
  386. for ((index=0; index <= ${#mods_global_array[@]}; index++)); do
  387. # When entry is found
  388. if [ "${mods_global_array[index]}" == "${currentmod}" ]; then
  389. # Go back to the previous "MOD" separator
  390. for ((index=index; index <= ${#mods_global_array[@]}; index--)); do
  391. # When "MOD" is found
  392. if [ "${mods_global_array[index]}" == "MOD" ]; then
  393. # Get info
  394. fn_mod_info
  395. modinfocommand="1"
  396. break
  397. fi
  398. done
  399. fi
  400. # Exit the loop if job is done
  401. if [ "${modinfocommand}" == "1" ]; then
  402. break
  403. fi
  404. done
  405. }
  406. fn_gsm_requirements
  407. fn_mods_scrape_urls
  408. fn_mods_info
  409. fn_mods_available