mods_core.sh 17 KB

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