mods_core.sh 15 KB

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