mods_core.sh 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  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. echo " * Press ctrl + c to abort."
  163. sleep 3
  164. fi
  165. fn_script_log "${modprettyname} is already installed, overwriting any file."
  166. fi
  167. }
  168. # Add the mod to the installed mods list
  169. fn_mod_add_list(){
  170. # Append modname to lockfile if it's not already in it
  171. if [ ! -n "$(sed -n "/^${modcommand}$/p" "${modslockfilefullpath}")" ]; then
  172. echo "${modcommand}" >> "${modslockfilefullpath}"
  173. fn_script_log "${modcommand} added to ${modslockfile}"
  174. fi
  175. }
  176. fn_check_files_list(){
  177. # File list must exist and be valid before any operation on it
  178. if [ -f "${modsdatadir}/${modcommand}-files.list" ]; then
  179. # How many lines is the file list
  180. modsfilelistsize="$(cat "${modsdatadir}/${modcommand}-files.list" | wc -l)"
  181. # If file list is empty
  182. if [ $modsfilelistsize -eq 0 ]; then
  183. fn_print_error_nl "${modcommand}-files.list is empty"
  184. echo "Exiting."
  185. fn_scrip_log_fatal "${modcommand}-files.list is empty"
  186. exitcode="2"
  187. core_exit.sh
  188. fi
  189. else
  190. fn_print_error_nl "${modsdatadir}/${modcommand}-files.list don't exist"
  191. echo "Exiting."
  192. fn_scrip_log_fatal "${modsdatadir}/${modcommand}-files.list don't exist"
  193. exitcode="2"
  194. core_exit.sh
  195. fi
  196. }
  197. # Apply some post-install fixes to make sure everything will be fine
  198. fn_postinstall_tasks(){
  199. # Prevent sensitive directories from being erased upon uninstall by removing them them from: ${modsdatadir}/${modcommand}-files.list
  200. # Check file validity
  201. fn_check_files_list
  202. # Output to the user
  203. fn_print_dots "Rearranging ${modcommand}-files.list"
  204. sleep 0.5
  205. fn_script_log_info "Rearranging ${modcommand}-files.list"
  206. # What lines/files to remove from file list (end var with a ";" separator)
  207. removefromlist="cfg;addons;"
  208. # Loop through files to remove from file list,
  209. # that way these files won't get removed upon uninstall
  210. # How many elements to remove from list
  211. removefromlistamount="$(echo "${removefromlist}" | awk -F ';' '{ print NF }')"
  212. # Test all subvalue of "removefromlist" using the ";" separator
  213. for ((filesindex=1; filesindex < ${removefromlistamount}; filesindex++)); do
  214. # Put current file into test variable
  215. removefilevar="$( echo "${removefromlist}" | awk -F ';' -v x=${filesindex} '{ print $x }' )"
  216. # Then delete matching line(s)!
  217. sed -i "/^${removefilevar}$/d" "${modsdatadir}/${modcommand}-files.list"
  218. done
  219. # Sourcemod fix
  220. # Remove metamod from sourcemod fileslist
  221. if [ "${modcommand}" == "sourcemod" ]; then
  222. # Remove addons/metamod & addons/metamod/sourcemod.vdf from ${modcommand}-files.list
  223. sed -i "/^addons\/metamod$/d" "${modsdatadir}/${modcommand}-files.list"
  224. sed -i "/^addons\/metamod\/sourcemod.vdf$/d" "${modsdatadir}/${modcommand}-files.list"
  225. fi
  226. fn_print_ok "Rearranging ${modcommand}-files.list"
  227. sleep 0.5
  228. }
  229. # Apply some post-uninstall fixes to make sure everything will be fine
  230. fn_postuninstall_tasks(){
  231. # Oxide fix
  232. # Oxide replaces server files, so a validate is required after uninstall
  233. if [ "${engine}" == "unity3d" ]&&[[ "${modprettyname}" == *"Oxide"* ]]; then
  234. fn_print_information_nl "Validating to restore original ${gamename} files replaced by Oxide"
  235. fn_script_log "Validating to restore original ${gamename} files replaced by Oxide"
  236. exitbypass="1"
  237. command_validate.sh
  238. unset exitbypass
  239. fi
  240. }
  241. #########################
  242. ## mods_list.sh arrays ##
  243. #########################
  244. ## Define info for a mod
  245. # Define all variables from a mod at once when index is set to a separator
  246. fn_mod_info(){
  247. # If for some reason no index is set, none of this can work
  248. if [ -z "$index" ]; then
  249. fn_print_error "index variable not set. Please report an issue to LGSM Team."
  250. echo "* https://github.com/GameServerManagers/LinuxGSM/issues"
  251. exitcode="1"
  252. core_exit.sh
  253. fi
  254. modcommand="${mods_global_array[index+1]}"
  255. modprettyname="${mods_global_array[index+2]}"
  256. modurl="${mods_global_array[index+3]}"
  257. modfilename="${mods_global_array[index+4]}"
  258. modsubdirs="${mods_global_array[index+5]}"
  259. modlowercase="${mods_global_array[index+6]}"
  260. modinstalldir="${mods_global_array[index+7]}"
  261. modkeepfiles="${mods_global_array[index+8]}"
  262. modengines="${mods_global_array[index+9]}"
  263. modgames="${mods_global_array[index+10]}"
  264. modexcludegames="${mods_global_array[index+11]}"
  265. modsite="${mods_global_array[index+12]}"
  266. moddescription="${mods_global_array[index+13]}"
  267. }
  268. ## Mod compatibility check
  269. # Find out if a game is compatible with a mod from a modgames (list of games supported by a mod) variable
  270. fn_compatible_mod_games(){
  271. # Reset test value
  272. modcompatiblegame="0"
  273. # If value is set to GAMES (ignore)
  274. if [ "${modgames}" != "GAMES" ]; then
  275. # How many games we need to test
  276. gamesamount="$(echo "${modgames}" | awk -F ';' '{ print NF }')"
  277. # Test all subvalue of "modgames" using the ";" separator
  278. for ((gamevarindex=1; gamevarindex < ${gamesamount}; gamevarindex++)); do
  279. # Put current game name into modtest variable
  280. gamemodtest="$( echo "${modgames}" | awk -F ';' -v x=${gamevarindex} '{ print $x }' )"
  281. # If game name matches
  282. if [ "${gamemodtest}" == "${gamename}" ]; then
  283. # Mod is compatible !
  284. modcompatiblegame="1"
  285. fi
  286. done
  287. fi
  288. }
  289. # Find out if an engine is compatible with a mod from a modengines (list of engines supported by a mod) variable
  290. fn_compatible_mod_engines(){
  291. # Reset test value
  292. modcompatibleengine="0"
  293. # If value is set to ENGINES (ignore)
  294. if [ "${modengines}" != "ENGINES" ]; then
  295. # How many engines we need to test
  296. enginesamount="$(echo "${modengines}" | awk -F ';' '{ print NF }')"
  297. # Test all subvalue of "modengines" using the ";" separator
  298. for ((gamevarindex=1; gamevarindex < ${enginesamount}; gamevarindex++)); do
  299. # Put current engine name into modtest variable
  300. enginemodtest="$( echo "${modengines}" | awk -F ';' -v x=${gamevarindex} '{ print $x }' )"
  301. # If engine name matches
  302. if [ "${enginemodtest}" == "${engine}" ]; then
  303. # Mod is compatible !
  304. modcompatibleengine="1"
  305. fi
  306. done
  307. fi
  308. }
  309. # Find out if a game is not compatible with a mod from a modnotgames (list of games not supported by a mod) variable
  310. fn_not_compatible_mod_games(){
  311. # Reset test value
  312. modeincompatiblegame="0"
  313. # If value is set to NOTGAMES (ignore)
  314. if [ "${modexcludegames}" != "NOTGAMES" ]; then
  315. # How many engines we need to test
  316. excludegamesamount="$(echo "${modexcludegames}" | awk -F ';' '{ print NF }')"
  317. # Test all subvalue of "modexcludegames" using the ";" separator
  318. for ((gamevarindex=1; gamevarindex < ${excludegamesamount}; gamevarindex++)); do
  319. # Put current engine name into modtest variable
  320. excludegamemodtest="$( echo "${modexcludegames}" | awk -F ';' -v x=${gamevarindex} '{ print $x }' )"
  321. # If engine name matches
  322. if [ "${excludegamemodtest}" == "${gamename}" ]; then
  323. # Mod is compatible !
  324. modeincompatiblegame="1"
  325. fi
  326. done
  327. fi
  328. }
  329. # Sums up if a mod is compatible or not with modcompatibility=0/1
  330. fn_mod_compatible_test(){
  331. # Test game and engine compatibility
  332. fn_compatible_mod_games
  333. fn_compatible_mod_engines
  334. fn_not_compatible_mod_games
  335. if [ "${modeincompatiblegame}" == "1" ]; then
  336. modcompatibility="0"
  337. elif [ "${modcompatibleengine}" == "1" ]||[ "${modcompatiblegame}" == "1" ]; then
  338. modcompatibility="1"
  339. else
  340. modcompatibility="0"
  341. fi
  342. }
  343. # Checks if a mod is compatibile for installation
  344. # Provides available mods for installation
  345. # Provides commands for mods installation
  346. fn_mods_available(){
  347. # First, reset variables
  348. compatiblemodslist=()
  349. availablemodscommands=()
  350. modprettynamemaxlength="0"
  351. modsitemaxlength="0"
  352. moddescriptionmaxlength="0"
  353. modcommandmaxlength="0"
  354. # Find compatible games
  355. # Find separators through the global array
  356. for ((index="0"; index <= ${#mods_global_array[@]}; index++)); do
  357. # If current value is a separator; then
  358. if [ "${mods_global_array[index]}" == "${modseparator}" ]; then
  359. # Set mod variables
  360. fn_mod_info
  361. # Test if game is compatible
  362. fn_mod_compatible_test
  363. # If game is compatible
  364. if [ "${modcompatibility}" == "1" ]; then
  365. # Put it into an array to prepare user output
  366. compatiblemodslist+=( "${modprettyname}" "${modcommand}" "${modsite}" "${moddescription}" )
  367. # Keep available commands in an array to make life easier
  368. availablemodscommands+=( "${modcommand}" )
  369. fi
  370. fi
  371. done
  372. }
  373. # Output available mods in a nice way to the user
  374. fn_mods_show_available(){
  375. # Set and reset vars
  376. compatiblemodslistindex=0
  377. spaces=" "
  378. # As long as we're within index values
  379. while [ "${compatiblemodslistindex}" -lt "${#compatiblemodslist[@]}" ]; do
  380. # Set values for convenience
  381. displayedmodname="${compatiblemodslist[compatiblemodslistindex]}"
  382. displayedmodcommand="${compatiblemodslist[compatiblemodslistindex+1]}"
  383. displayedmodsite="${compatiblemodslist[compatiblemodslistindex+2]}"
  384. displayedmoddescription="${compatiblemodslist[compatiblemodslistindex+3]}"
  385. # Output mods to the user
  386. echo -e "\e[1m${displayedmodname}\e[0m - ${displayedmoddescription} - ${displayedmodsite}"
  387. echo -e " * \e[36m${displayedmodcommand}\e[0m"
  388. # Increment index from the amount of values we just displayed
  389. let "compatiblemodslistindex+=4"
  390. done
  391. # If no mods are found
  392. if [ -z "${compatiblemodslist}" ]; then
  393. fn_print_fail "No mods are currently available for ${gamename}."
  394. core_exit.sh
  395. fi
  396. }
  397. # Get details of a mod any (relevant and unique, such as full mod name or install command) value
  398. fn_mod_get_info_from_command(){
  399. # Variable to know when job is done
  400. modinfocommand="0"
  401. # Find entry in global array
  402. for ((index=0; index <= ${#mods_global_array[@]}; index++)); do
  403. # When entry is found
  404. if [ "${mods_global_array[index]}" == "${currentmod}" ]; then
  405. # Go back to the previous "MOD" separator
  406. for ((index=index; index <= ${#mods_global_array[@]}; index--)); do
  407. # When "MOD" is found
  408. if [ "${mods_global_array[index]}" == "MOD" ]; then
  409. # Get info
  410. fn_mod_info
  411. modinfocommand="1"
  412. break
  413. fi
  414. done
  415. fi
  416. # Exit the loop if job is done
  417. if [ "${modinfocommand}" == "1" ]; then
  418. break
  419. fi
  420. done
  421. }
  422. fn_gsm_requirements
  423. fn_mods_scrape_urls
  424. fn_mods_info
  425. fn_mods_available