mods_core.sh 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757
  1. #!/bin/bash
  2. # LinuxGSM command_mods_install.sh module
  3. # Author: Daniel Gibbs
  4. # Contributors: https://linuxgsm.com/contrib
  5. # Website: https://linuxgsm.com
  6. # Description: Core modules for mods list/install/update/remove
  7. moduleselfname="$(basename "$(readlink -f "${BASH_SOURCE[0]}")")"
  8. # Files and Directories.
  9. modsdir="${lgsmdir}/mods"
  10. modstmpdir="${modsdir}/tmp"
  11. extractdest="${modstmpdir}/extract"
  12. modsinstalledlist="installed-mods.txt"
  13. modsinstalledlistfullpath="${modsdir}/${modsinstalledlist}"
  14. ## Installation.
  15. # Download management.
  16. fn_mod_install_files() {
  17. fn_fetch_file "${modurl}" "" "" "" "${modstmpdir}" "${modfilename}"
  18. # Check if variable is valid checking if file has been downloaded and exists.
  19. if [ ! -f "${modstmpdir}/${modfilename}" ]; then
  20. fn_print_failure "An issue occurred downloading ${modprettyname}"
  21. fn_script_log_fail "An issue occurred downloading ${modprettyname}"
  22. core_exit.sh
  23. fi
  24. if [ ! -d "${extractdest}" ]; then
  25. mkdir -p "${extractdest}"
  26. fi
  27. fn_dl_extract "${modstmpdir}" "${modfilename}" "${extractdest}"
  28. # If modsubdirs names a specific subfolder, use its contents as the install root.
  29. if [ "${modsubdirs}" != "0" ] && [ -d "${extractdest}/${modsubdirs}" ]; then
  30. local tmpsubdir
  31. tmpsubdir=$(mktemp -d)
  32. mv "${extractdest}/${modsubdirs}" "${tmpsubdir}/"
  33. rm -rf "${extractdest}"
  34. mv "${tmpsubdir}/${modsubdirs}" "${extractdest}"
  35. rm -rf "${tmpsubdir}"
  36. fi
  37. }
  38. # Convert mod files to lowercase if needed.
  39. fn_mod_lowercase() {
  40. # Checking lowercase settings from mods array definition
  41. if [ "${modlowercase}" == "LowercaseOn" ]; then
  42. echo -en "converting ${modprettyname} files to lowercase..."
  43. fn_sleep_time
  44. fn_script_log_info "Converting ${modprettyname} files to lowercase"
  45. # Total files and directories for the mod, to output to the user
  46. fileswc=$(find "${extractdest}" | wc -l)
  47. # Total uppercase files and directories for the mod, to output to the user
  48. filesupperwc=$(find "${extractdest}" -name '*[[:upper:]]*' | wc -l)
  49. fn_script_log_info "Found ${filesupperwc} uppercase files out of ${fileswc}, converting"
  50. echo -en "Found ${filesupperwc} uppercase files out of ${fileswc}, converting..."
  51. # Convert files and directories starting from the deepest to prevent issues (-depth argument)
  52. while read -r src; do
  53. # We have to convert only the last file from the path, otherwise we will fail to convert anything if a parent dir has any uppercase
  54. # therefore, we have to separate the end of the filename to only lowercase it rather than the whole line
  55. # Gather parent dir, filename lowercase filename, and set lowercase destination name
  56. latestparentdir=$(dirname "${src}")
  57. latestfilelc=$(basename "${src}" | tr '[:upper:]' '[:lower:]')
  58. dst="${latestparentdir}/${latestfilelc}"
  59. # Only convert if destination does not already exist for some reason
  60. if [ ! -e "${dst}" ]; then
  61. # Finally we can rename the file
  62. mv "${src}" "${dst}"
  63. # Exit if it fails for any reason
  64. exitcode=$?
  65. if [ "${exitcode}" -ne 0 ]; then
  66. fn_print_fail_eol_nl
  67. core_exit.sh
  68. fi
  69. fi
  70. done < <(find "${extractdest}" -depth -name '*[[:upper:]]*')
  71. fn_print_ok_eol_nl
  72. fi
  73. }
  74. # Create ${modcommand}-files.txt containing the full extracted file/directory list.
  75. fn_mod_create_filelist() {
  76. echo -en "building ${modcommand}-files.txt..."
  77. fn_sleep_time
  78. # ${modsdir}/${modcommand}-files.txt.
  79. find "${extractdest}" -mindepth 1 -printf '%P\n' > "${modsdir}/${modcommand}-files.txt"
  80. exitcode=$?
  81. if [ "${exitcode}" -ne 0 ]; then
  82. fn_print_fail_eol_nl
  83. fn_script_log_fail "Building ${modsdir}/${modcommand}-files.txt"
  84. core_exit.sh
  85. else
  86. fn_print_ok_eol_nl
  87. fn_script_log_pass "Building ${modsdir}/${modcommand}-files.txt"
  88. fi
  89. # Adding removed files if needed.
  90. if [ -f "${modsdir}/.removedfiles.tmp" ]; then
  91. cat "${modsdir}/.removedfiles.tmp" >> "${modsdir}/${modcommand}-files.txt"
  92. fi
  93. }
  94. # Copy the mod into serverfiles.
  95. fn_mod_copy_destination() {
  96. echo -en "copying ${modprettyname} to ${modinstalldir}..."
  97. fn_sleep_time
  98. cp -Rf "${extractdest}/." "${modinstalldir}/"
  99. exitcode=$?
  100. if [ "${exitcode}" -ne 0 ]; then
  101. fn_print_fail_eol_nl
  102. fn_script_log_fail "Copying ${modprettyname} to ${modinstalldir}"
  103. else
  104. fn_print_ok_eol_nl
  105. fn_script_log_pass "Copying ${modprettyname} to ${modinstalldir}"
  106. fi
  107. }
  108. # Add the mod to the installed-mods.txt.
  109. fn_mod_add_list() {
  110. if [ -z "$(sed -n "/^${modcommand}$/p" "${modsinstalledlistfullpath}")" ]; then
  111. echo -e "${modcommand}" >> "${modsinstalledlistfullpath}"
  112. fn_script_log_info "${modcommand} added to ${modsinstalledlist}"
  113. fi
  114. }
  115. # Prevent sensitive directories from being erased upon uninstall by removing them from: ${modcommand}-files.txt.
  116. fn_mod_tidy_files_list() {
  117. # Check file list validity.
  118. fn_check_mod_files_list
  119. # Output to the user
  120. echo -en "tidy up ${modcommand}-files.txt..."
  121. fn_sleep_time
  122. fn_script_log_info "Tidy up ${modcommand}-files.txt"
  123. # Lines/files to remove from file list (end with ";" separator).
  124. removefromlist="cfg;addons;RustDedicated_Data;RustDedicated_Data\/Managed;RustDedicated_Data\/Managed\/x86;RustDedicated_Data\/Managed\/x64;"
  125. # Loop through files to remove from file list,
  126. # generate elements to remove from list.
  127. removefromlistamount=$(echo -e "${removefromlist}" | awk -F ';' '{ print NF }')
  128. # Test all subvalue of "removefromlist" using the ";" separator.
  129. for ((filesindex = 1; filesindex < removefromlistamount; filesindex++)); do
  130. # Put current file into test variable.
  131. removefilevar=$(echo -e "${removefromlist}" | awk -F ';' -v x=${filesindex} '{ print $x }')
  132. # Delete line(s) matching exactly.
  133. sed -i "/^${removefilevar}$/d" "${modsdir}/${modcommand}-files.txt"
  134. # Exit on error.
  135. exitcode=$?
  136. if [ "${exitcode}" -ne 0 ]; then
  137. fn_print_fail_eol_nl
  138. fn_script_log_fail "Error while tidying line: ${removefilevar} from: ${modsdir}/${modcommand}-files.txt"
  139. core_exit.sh
  140. break
  141. fi
  142. done
  143. fn_print_ok_eol_nl
  144. # Sourcemod fix
  145. # Remove metamod from sourcemod fileslist.
  146. if [ "${modcommand}" == "sourcemod" ]; then
  147. # Remove addons/metamod & addons/metamod/sourcemod.vdf from ${modcommand}-files.txt.
  148. sed -i "/^addons\/metamod$/d" "${modsdir}/${modcommand}-files.txt"
  149. sed -i "/^addons\/metamod\/sourcemod.vdf$/d" "${modsdir}/${modcommand}-files.txt"
  150. fi
  151. # Remove common paths from deletion list (Add your sourcemod mod here)
  152. if [ "${modcommand}" == "gokz" ] || [ "${modcommand}" == "ttt" ] || [ "${modcommand}" == "steamworks" ] || [ "${modcommand}" == "get5" ]; then
  153. sed -i "/^addons\/sourcemod$/d" "${modsdir}/${modcommand}-files.txt"
  154. sed -i "/^addons\/sourcemod\/configs$/d" "${modsdir}/${modcommand}-files.txt"
  155. sed -i "/^addons\/sourcemod\/extensions$/d" "${modsdir}/${modcommand}-files.txt"
  156. sed -i "/^addons\/sourcemod\/logs$/d" "${modsdir}/${modcommand}-files.txt"
  157. sed -i "/^addons\/sourcemod\/plugins$/d" "${modsdir}/${modcommand}-files.txt"
  158. sed -i "/^addons\/sourcemod\/plugins\/disabled$/d" "${modsdir}/${modcommand}-files.txt"
  159. sed -i "/^addons\/sourcemod\/scripting$/d" "${modsdir}/${modcommand}-files.txt"
  160. sed -i "/^addons\/sourcemod\/scripting\/include$/d" "${modsdir}/${modcommand}-files.txt"
  161. sed -i "/^addons\/sourcemod\/translations$/d" "${modsdir}/${modcommand}-files.txt"
  162. # Don't delete directories of translations like 'fr', 'sv', 'de', etc
  163. sed -i "/^addons\/sourcemod\/translations\/[A-Za-z0-9_]*$/d" "${modsdir}/${modcommand}-files.txt"
  164. sed -i "/^cfg\/sourcemod$/d" "${modsdir}/${modcommand}-files.txt"
  165. sed -i "/^maps$/d" "${modsdir}/${modcommand}-files.txt"
  166. sed -i "/^materialss$/d" "${modsdir}/${modcommand}-files.txt"
  167. sed -i "/^materials\/models$/d" "${modsdir}/${modcommand}-files.txt"
  168. sed -i "/^materials\/models\/weapons$/d" "${modsdir}/${modcommand}-files.txt"
  169. sed -i "/^materials\/darkness$/d" "${modsdir}/${modcommand}-files.txt"
  170. sed -i "/^materials\/decals$/d" "${modsdir}/${modcommand}-files.txt"
  171. sed -i "/^materials\/overlays$/d" "${modsdir}/${modcommand}-files.txt"
  172. sed -i "/^models$/d" "${modsdir}/${modcommand}-files.txt"
  173. sed -i "/^models\/weapons$/d" "${modsdir}/${modcommand}-files.txt"
  174. sed -i "/^sound$/d" "${modsdir}/${modcommand}-files.txt"
  175. sed -i "/^sound\/weapons$/d" "${modsdir}/${modcommand}-files.txt"
  176. fi
  177. # Remove paths of specific mods from deletion list
  178. if [ "${modcommand}" == "gokz" ]; then
  179. sed -i "/^addons\/sourcemod\/scripting\/include\/smjansson.inc$/d" "${modsdir}/${modcommand}-files.txt"
  180. sed -i "/^addons\/sourcemod\/scripting\/include\/GlobalAPI-Core.inc$/d" "${modsdir}/${modcommand}-files.txt"
  181. sed -i "/^addons\/sourcemod\/scripting\/include\/sourcebanspp.inc$/d" "${modsdir}/${modcommand}-files.txt"
  182. sed -i "/^addons\/sourcemod\/scripting\/include\/autoexecconfig.inc$/d" "${modsdir}/${modcommand}-files.txt"
  183. sed -i "/^addons\/sourcemod\/scripting\/include\/colorvariables.inc$/d" "${modsdir}/${modcommand}-files.txt"
  184. sed -i "/^addons\/sourcemod\/scripting\/include\/movementapi.inc$/d" "${modsdir}/${modcommand}-files.txt"
  185. sed -i "/^addons\/sourcemod\/scripting\/include\/movement.inc$/d" "${modsdir}/${modcommand}-files.txt"
  186. sed -i "/^addons\/sourcemod\/scripting\/include\/dhooks.inc$/d" "${modsdir}/${modcommand}-files.txt"
  187. sed -i "/^addons\/sourcemod\/scripting\/include\/updater.inc$/d" "${modsdir}/${modcommand}-files.txt"
  188. fi
  189. }
  190. ## Information Gathering.
  191. # Get details of a mod any (relevant and unique, such as full mod name or install command) value.
  192. fn_mod_get_info() {
  193. # Variable to know when job is done.
  194. modinfocommand="0"
  195. # Find entry in global array.
  196. for ((index = 0; index < ${#mods_global_array[@]}; index++)); do
  197. # When entry is found.
  198. if [ "${mods_global_array[index]}" == "${currentmod}" ]; then
  199. # Go back to the previous "MOD" separator.
  200. for ((index_search = index; index_search >= 0; index_search--)); do
  201. # When "MOD" is found.
  202. if [ "${mods_global_array[index_search]}" == "MOD" ]; then
  203. # Get info.
  204. index=${index_search}
  205. fn_mods_define
  206. modinfocommand="1"
  207. break
  208. fi
  209. done
  210. fi
  211. # Exit the loop if job is done.
  212. if [ "${modinfocommand}" == "1" ]; then
  213. break
  214. fi
  215. done
  216. # What happens if mod is not found.
  217. if [ "${modinfocommand}" == "0" ]; then
  218. fn_script_log_error "Could not find information for ${currentmod}"
  219. fn_print_error_nl "Could not find information for ${currentmod}"
  220. core_exit.sh
  221. fi
  222. }
  223. # Define all variables for a mod at once when index is set to a separator.
  224. fn_mods_define() {
  225. if [ -z "$index" ]; then
  226. fn_script_log_fail "index variable not set. Please report an issue."
  227. fn_print_error "index variable not set. Please report an issue."
  228. echo -e "* https://github.com/GameServerManagers/LinuxGSM/issues"
  229. core_exit.sh
  230. fi
  231. modcommand="${mods_global_array[index + 1]}"
  232. modprettyname="${mods_global_array[index + 2]}"
  233. modurl="${mods_global_array[index + 3]}"
  234. modfilename="${mods_global_array[index + 4]}"
  235. modsubdirs="${mods_global_array[index + 5]}"
  236. modlowercase="${mods_global_array[index + 6]}"
  237. modinstalldir="${mods_global_array[index + 7]}"
  238. modkeepfiles="${mods_global_array[index + 8]}"
  239. modengines="${mods_global_array[index + 9]}"
  240. modgames="${mods_global_array[index + 10]}"
  241. modexcludegames="${mods_global_array[index + 11]}"
  242. modsite="${mods_global_array[index + 12]}"
  243. moddescription="${mods_global_array[index + 13]}"
  244. }
  245. # Builds list of installed mods.
  246. # using installed-mods.txt grabbing mod info from mods_list.sh.
  247. fn_mods_installed_list() {
  248. fn_mods_count_installed
  249. # Set/reset variables.
  250. installedmodsline="1"
  251. installedmodslist=()
  252. modprettynamemaxlength="0"
  253. modsitemaxlength="0"
  254. moddescriptionmaxlength="0"
  255. modcommandmaxlength="0"
  256. # Loop through every line of the installed mods list ${modsinstalledlistfullpath}.
  257. while [ "${installedmodsline}" -le "${installedmodscount}" ]; do
  258. currentmod=$(sed "${installedmodsline}q;d" "${modsinstalledlistfullpath}")
  259. # Get mod info to make sure mod exists.
  260. fn_mod_get_info
  261. # Add the mod to available commands.
  262. installedmodslist+=("${modcommand}")
  263. # Increment line check.
  264. ((installedmodsline++))
  265. done
  266. if [ "${installedmodscount}" ]; then
  267. fn_script_log_info "${installedmodscount} addons/mods are currently installed"
  268. fi
  269. }
  270. # Loops through mods_global_array to define available mods & provide available commands for mods installation.
  271. fn_mods_available() {
  272. # First, reset variables.
  273. compatiblemodslist=()
  274. availablemodscommands=()
  275. # Find compatible games.
  276. # Find separators through the global array.
  277. for ((index = "0"; index <= ${#mods_global_array[@]}; index++)); do
  278. # If current value is a separator; then.
  279. if [ "${mods_global_array[index]}" == "${modseparator}" ]; then
  280. # Set mod variables.
  281. fn_mods_define
  282. # Test if game is compatible.
  283. fn_mod_compatible_test
  284. # If game is compatible.
  285. if [ "${modcompatibility}" == "1" ]; then
  286. # Put it into an array to prepare user output.
  287. compatiblemodslist+=("${modprettyname}" "${modcommand}" "${modsite}" "${moddescription}")
  288. # Keep available commands in an array to make life easier.
  289. availablemodscommands+=("${modcommand}")
  290. fi
  291. fi
  292. done
  293. }
  294. ## Mod compatibility check.
  295. # Find out if a game is compatible with a mod from a modgames (list of games supported by a mod) variable.
  296. fn_compatible_mod_games() {
  297. # Reset test value.
  298. modcompatiblegame="0"
  299. # If value is set to GAMES (ignore).
  300. if [ "${modgames}" != "GAMES" ]; then
  301. # How many games we need to test.
  302. gamesamount=$(echo -e "${modgames}" | awk -F ';' '{ print NF }')
  303. # Test all subvalue of "modgames" using the ";" separator.
  304. for ((gamevarindex = 1; gamevarindex < gamesamount; gamevarindex++)); do
  305. # Put current game name into modtest variable.
  306. gamemodtest=$(echo -e "${modgames}" | awk -F ';' -v x=${gamevarindex} '{ print $x }')
  307. # If game name matches.
  308. if [ "${gamemodtest}" == "${gamename}" ]; then
  309. # Mod is compatible.
  310. modcompatiblegame="1"
  311. fi
  312. done
  313. fi
  314. }
  315. # Find out if an engine is compatible with a mod from a modengines (list of engines supported by a mod) variable.
  316. fn_compatible_mod_engines() {
  317. # Reset test value.
  318. modcompatibleengine="0"
  319. # If value is set to ENGINES (ignore).
  320. if [ "${modengines}" != "ENGINES" ]; then
  321. # How many engines we need to test.
  322. enginesamount=$(echo -e "${modengines}" | awk -F ';' '{ print NF }')
  323. # Test all subvalue of "modengines" using the ";" separator.
  324. for ((gamevarindex = 1; gamevarindex < enginesamount; gamevarindex++)); do
  325. # Put current engine name into modtest variable.
  326. enginemodtest=$(echo -e "${modengines}" | awk -F ';' -v x=${gamevarindex} '{ print $x }')
  327. # If engine name matches.
  328. if [ "${enginemodtest}" == "${engine}" ]; then
  329. # Mod is compatible.
  330. modcompatibleengine="1"
  331. fi
  332. done
  333. fi
  334. }
  335. # Find out if a game is not compatible with a mod from a modnotgames (list of games not supported by a mod) variable.
  336. fn_not_compatible_mod_games() {
  337. # Reset test value.
  338. modeincompatiblegame="0"
  339. # If value is set to NOTGAMES (ignore).
  340. if [ "${modexcludegames}" != "NOTGAMES" ]; then
  341. # How many engines we need to test.
  342. excludegamesamount=$(echo -e "${modexcludegames}" | awk -F ';' '{ print NF }')
  343. # Test all subvalue of "modexcludegames" using the ";" separator.
  344. for ((gamevarindex = 1; gamevarindex < excludegamesamount; gamevarindex++)); do
  345. # Put current engine name into modtest variable.
  346. excludegamemodtest=$(echo -e "${modexcludegames}" | awk -F ';' -v x=${gamevarindex} '{ print $x }')
  347. # If engine name matches.
  348. if [ "${excludegamemodtest}" == "${gamename}" ]; then
  349. # Mod is compatible.
  350. modeincompatiblegame="1"
  351. fi
  352. done
  353. fi
  354. }
  355. # Sums up if a mod is compatible or not with modcompatibility=0/1.
  356. fn_mod_compatible_test() {
  357. # Test game and engine compatibility.
  358. fn_compatible_mod_games
  359. fn_compatible_mod_engines
  360. fn_not_compatible_mod_games
  361. if [ "${modeincompatiblegame}" == "1" ]; then
  362. modcompatibility="0"
  363. elif [ "${modcompatibleengine}" == "1" ] || [ "${modcompatiblegame}" == "1" ]; then
  364. modcompatibility="1"
  365. else
  366. modcompatibility="0"
  367. fi
  368. }
  369. ## Directory management.
  370. # Create mods files and directories if it doesn't exist.
  371. fn_create_mods_dir() {
  372. # Create lgsm data modsdir.
  373. if [ ! -d "${modsdir}" ]; then
  374. echo -en "creating LinuxGSM mods data directory ${modsdir}..."
  375. mkdir -p "${modsdir}"
  376. exitcode=$?
  377. if [ "${exitcode}" -ne 0 ]; then
  378. fn_print_fail_eol_nl
  379. fn_script_log_fail "Creating mod download dir ${modsdir}"
  380. core_exit.sh
  381. else
  382. fn_print_ok_eol_nl
  383. fn_script_log_pass "Creating mod download dir ${modsdir}"
  384. fi
  385. fi
  386. # Create mod install directory.
  387. if [ ! -d "${modinstalldir}" ]; then
  388. echo -en "creating mods install directory ${modinstalldir}..."
  389. mkdir -p "${modinstalldir}"
  390. exitcode=$?
  391. if [ "${exitcode}" -ne 0 ]; then
  392. fn_print_fail_eol_nl
  393. fn_script_log_fail "Creating mod install directory ${modinstalldir}"
  394. core_exit.sh
  395. else
  396. fn_print_ok_eol_nl
  397. fn_script_log_pass "Creating mod install directory ${modinstalldir}"
  398. fi
  399. fi
  400. # Create ${datadir}/${modsinstalledlist}.
  401. if [ ! -f "${modsinstalledlistfullpath}" ]; then
  402. touch "${modsinstalledlistfullpath}"
  403. fn_script_log_info "Created ${modsinstalledlistfullpath}"
  404. fi
  405. }
  406. # Create tmp download mod directory.
  407. fn_mods_create_tmp_dir() {
  408. if [ ! -d "${modstmpdir}" ]; then
  409. mkdir -p "${modstmpdir}"
  410. exitcode=$?
  411. echo -en "creating mod download directory ${modstmpdir}..."
  412. if [ "${exitcode}" -ne 0 ]; then
  413. fn_print_fail_eol_nl
  414. fn_script_log_fail "Creating mod download directory ${modstmpdir}"
  415. core_exit.sh
  416. else
  417. fn_print_ok_eol_nl
  418. fn_script_log_pass "Creating mod download directory ${modstmpdir}"
  419. fi
  420. fi
  421. }
  422. # Remove the tmp mod download directory when finished.
  423. fn_mods_clear_tmp_dir() {
  424. if [ -d "${modstmpdir}" ]; then
  425. echo -en "clearing mod download directory ${modstmpdir}..."
  426. rm -rf "${modstmpdir:?}"
  427. exitcode=$?
  428. if [ "${exitcode}" -ne 0 ]; then
  429. fn_print_fail_eol_nl
  430. fn_script_log_fail "Clearing mod download directory ${modstmpdir}"
  431. core_exit.sh
  432. else
  433. fn_print_ok_eol_nl
  434. fn_script_log_pass "Clearing mod download directory ${modstmpdir}"
  435. fi
  436. fi
  437. # Clear temp file list as well.
  438. if [ -f "${modsdir}/.removedfiles.tmp" ]; then
  439. rm -f "${modsdir:?}/.removedfiles.tmp"
  440. fi
  441. }
  442. # Counts how many mods were installed.
  443. fn_mods_count_installed() {
  444. if [ -f "${modsinstalledlistfullpath}" ]; then
  445. installedmodscount=$(wc -l < "${modsinstalledlistfullpath}")
  446. else
  447. installedmodscount=0
  448. fi
  449. }
  450. # Exits if no mods were installed.
  451. fn_mods_check_installed() {
  452. # Count installed mods.
  453. fn_mods_count_installed
  454. # If no mods are found.
  455. if [ "${installedmodscount}" -eq 0 ]; then
  456. echo -e ""
  457. fn_print_failure_nl "No installed mods or addons were found"
  458. echo -e " * Install mods using LinuxGSM first with: ./${selfname} mods-install"
  459. fn_script_log_error "No installed mods or addons were found."
  460. core_exit.sh
  461. fi
  462. }
  463. # Checks that mod files list exists and isn't empty.
  464. fn_check_mod_files_list() {
  465. # File list must exist and be valid before any operation on it.
  466. if [ -f "${modsdir}/${modcommand}-files.txt" ]; then
  467. # How many lines is the file list.
  468. modsfilelistsize=$(wc -l < "${modsdir}/${modcommand}-files.txt")
  469. # If file list is empty.
  470. if [ "${modsfilelistsize}" -eq 0 ]; then
  471. fn_print_failure "${modcommand}-files.txt is empty"
  472. echo -e "* Unable to remove ${modprettyname}"
  473. fn_script_log_fail "${modcommand}-files.txt is empty: Unable to remove ${modprettyname}."
  474. core_exit.sh
  475. fi
  476. else
  477. fn_print_failure "${modsdir}/${modcommand}-files.txt does not exist"
  478. fn_script_log_fail "${modsdir}/${modcommand}-files.txt does not exist: Unable to remove ${modprettyname}."
  479. core_exit.sh
  480. fi
  481. }
  482. fn_mod_exist() {
  483. modreq=$1
  484. # requires one parameter, the mod
  485. if [ -f "${modsdir}/${modreq}-files.txt" ]; then
  486. # how many lines is the file list
  487. modsfilelistsize=$(wc -l < "${modsdir}/${modreq}-files.txt")
  488. # if file list is empty
  489. if [ "${modsfilelistsize}" -eq 0 ]; then
  490. fn_mod_required_fail_exist "${modreq}"
  491. fi
  492. else
  493. fn_mod_required_fail_exist "${modreq}"
  494. fi
  495. }
  496. fn_mod_required_fail_exist() {
  497. modreq=$1
  498. # requires one parameter, the mod
  499. fn_script_log_fail "${modreq}-files.txt is empty: unable to find ${modreq} installed"
  500. echo -en "* Unable to find '${modreq}' which is required prior to installing this mod..."
  501. fn_print_fail_eol_nl
  502. core_exit.sh
  503. }
  504. fn_mod_liblist_gam_filenames() {
  505. # clear variables just in case
  506. moddll=""
  507. modso=""
  508. moddylib=""
  509. # default libraries
  510. case ${gamename} in
  511. "Counter-Strike 1.6")
  512. moddll="mp.dll"
  513. modso="cs.so"
  514. moddylib="cs.dylib"
  515. ;;
  516. "Day of Defeat")
  517. moddll="dod.dll"
  518. modso="dod.so"
  519. moddylib="dod.dylib"
  520. ;;
  521. "Team Fortress Classic")
  522. moddll="tfc.dll"
  523. modso="tfc.so"
  524. moddylib="tfc.dylib"
  525. ;;
  526. "Natural Selection")
  527. moddll="ns.dll"
  528. modso="ns_i386.so"
  529. moddylib=""
  530. ;;
  531. "The Specialists")
  532. moddll="mp.dll"
  533. modso="ts_i386.so"
  534. moddylib=""
  535. ;;
  536. "Half-Life: Deathmatch")
  537. moddll="hl.dll"
  538. modso="hl.so"
  539. moddylib="hl.dylib"
  540. ;;
  541. esac
  542. }
  543. # modifiers for liblist.gam to add/remote metamod binaries
  544. fn_mod_install_liblist_gam_file() {
  545. fn_mod_liblist_gam_filenames
  546. if [ -f "${modinstalldir}/liblist.gam" ]; then
  547. # modify the liblist.gam file to initialize Metamod
  548. logentry="sed replace (dlls\\${moddll}) ${modinstalldir}/liblist.gam"
  549. echo -en "modifying gamedll in liblist.gam..."
  550. rpldll="s/dlls\\\\${moddll}/addons\/metamod\/dlls\/metamod.dll/g"
  551. sed -i $rpldll "${modinstalldir}/liblist.gam"
  552. grep -q "addons/metamod/dlls/metamod.dll" "${modinstalldir}/liblist.gam"
  553. exitcode=$?
  554. # if replacement back didn't happen, error out.
  555. if [ "${exitcode}" -ne 0 ]; then
  556. fn_script_log_fail "${logentry}"
  557. fn_print_fail_eol_nl
  558. else
  559. fn_script_log_pass "${logentry}"
  560. fn_print_ok_eol_nl
  561. fi
  562. # modify the liblist.gam file to initialize metamod
  563. logentry="sed replace (dlls\\${modso}) ${modinstalldir}/liblist.gam"
  564. echo -en "modifying gamedll_linux in liblist.gam..."
  565. rplso="s/dlls\/${modso}/addons\/metamod\/dlls\/metamod.so/g"
  566. sed -i $rplso "${modinstalldir}/liblist.gam"
  567. grep -q "addons/metamod/dlls/metamod.so" "${modinstalldir}/liblist.gam"
  568. exitcode=$?
  569. # if replacement back didn't happen, error out
  570. if [ "${exitcode}" -ne 0 ]; then
  571. fn_script_log_fail "${logentry}"
  572. fn_print_fail_eol_nl
  573. else
  574. fn_script_log_pass "${logentry}"
  575. fn_print_ok_eol_nl
  576. fi
  577. # mac os needs to be checked not all mods support mac os
  578. if [ -n "${moddylib}" ]; then
  579. # modify the liblist.gam file to initialize metamod
  580. logentry="sed replace (dlls\\${moddylib}) ${modinstalldir}/liblist.gam"
  581. echo -en "modifying gamedll_osx in liblist.gam..."
  582. rpldylib="s/dlls\/${moddylib}/addons\/metamod\/dlls\/metamod.dylib/g"
  583. sed -i $rpldylib "${modinstalldir}/liblist.gam"
  584. grep -q "addons/metamod/dlls/metamod.dylib" "${modinstalldir}/liblist.gam"
  585. exitcode=$?
  586. # if replacement back didn't happen, error out.
  587. if [ "${exitcode}" -ne 0 ]; then
  588. fn_script_log_fail "${logentry}"
  589. fn_print_fail_eol_nl
  590. else
  591. fn_script_log_pass "${logentry}"
  592. fn_print_ok_eol_nl
  593. fi
  594. fi
  595. fi
  596. }
  597. fn_mod_remove_liblist_gam_file() {
  598. fn_mod_liblist_gam_filenames
  599. if [ -f "${modinstalldir}/liblist.gam" ]; then
  600. # modify the liblist.gam file back to defaults
  601. logentry="sed replace (addons/metamod/dlls/metamod.dll) ${modinstalldir}/liblist.gam"
  602. echo -en "modifying gamedll in liblist.gam..."
  603. rpldll="s/addons\/metamod\/dlls\/metamod.dll/dlls\\\\${moddll}/g"
  604. sed -i $rpldll "${modinstalldir}/liblist.gam"
  605. grep -q "${moddll}" "${modinstalldir}/liblist.gam"
  606. exitcode=$?
  607. # if replacement back didn't happen, error out.
  608. if [ "${exitcode}" -ne 0 ]; then
  609. fn_script_log_fail "${logentry}"
  610. fn_print_fail_eol_nl
  611. else
  612. fn_script_log_pass "${logentry}"
  613. fn_print_ok_eol_nl
  614. fi
  615. # modify the liblist.gam file back to defaults
  616. logentry="sed replace (addons/metamod/dlls/metamod.so) ${modinstalldir}/liblist.gam"
  617. echo -en "modifying gamedll_linux in liblist.gam..."
  618. rplso="s/addons\/metamod\/dlls\/metamod.so/dlls\/${modso}/g"
  619. sed -i $rplso "${modinstalldir}/liblist.gam"
  620. grep -q "${modso}" "${modinstalldir}/liblist.gam"
  621. exitcode=$?
  622. # if replacement back didn't happen, error out
  623. if [ "${exitcode}" -ne 0 ]; then
  624. fn_script_log_fail "${logentry}"
  625. fn_print_fail_eol_nl
  626. else
  627. fn_script_log_pass "${logentry}"
  628. fn_print_ok_eol_nl
  629. fi
  630. # mac os needs to be checked not all mods support mac os
  631. if [ -n "${moddylib}" ]; then
  632. # modify the liblist.gam file back to defaults
  633. logentry="sed replace (addons/metamod/dlls/metamod.dylib) ${modinstalldir}/liblist.gam"
  634. echo -en "modifying gamedll_osx in liblist.gam..."
  635. rpldylib="s/addons\/metamod\/dlls\/metamod.dylib/dlls\/${moddylib}/g"
  636. sed -i $rpldylib "${modinstalldir}/liblist.gam"
  637. grep -q "${moddylib}" "${modinstalldir}/liblist.gam"
  638. # if replacement back didn't happen, error out.
  639. exitcode=$?
  640. if [ "${exitcode}" -ne 0 ]; then
  641. fn_script_log_fail "${logentry}"
  642. fn_print_fail_eol_nl
  643. else
  644. fn_script_log_pass "${logentry}"
  645. fn_print_ok_eol_nl
  646. fi
  647. fi
  648. fi
  649. }
  650. fn_mod_install_amxmodx_file() {
  651. # does plugins.ini exist?
  652. if [ -f "${modinstalldir}/addons/metamod/plugins.ini" ]; then
  653. # since it does exist, is the entry already in plugins.ini
  654. logentry="line (linux addons/amxmodx/dlls/amxmodx_mm_i386.so) inserted into ${modinstalldir}/addons/metamod/plugins.ini"
  655. echo -en "adding amxmodx_mm_i386.so in plugins.ini..."
  656. if ! grep -q "amxmodx_mm_i386.so" "${modinstalldir}/addons/metamod/plugins.ini"; then
  657. # file exists but the entry does not, let's add it
  658. if ! echo "linux addons/amxmodx/dlls/amxmodx_mm_i386.so" >> "${modinstalldir}/addons/metamod/plugins.ini"; then
  659. fn_script_log_fail "${logentry}"
  660. fn_print_fail_eol_nl
  661. else
  662. fn_script_log_pass "${logentry}"
  663. fn_print_ok_eol_nl
  664. fi
  665. fi
  666. else
  667. # create new file and add the mod to it
  668. if ! echo "linux addons/amxmodx/dlls/amxmodx_mm_i386.so" > "${modinstalldir}/addons/metamod/plugins.ini"; then
  669. fn_script_log_fail "${logentry}"
  670. fn_print_fail_eol_nl
  671. core_exit.sh
  672. else
  673. fn_script_log_pass "${logentry}"
  674. fn_print_ok_eol_nl
  675. fi
  676. fi
  677. }
  678. fn_mod_remove_amxmodx_file() {
  679. if [ -f "${modinstalldir}/addons/metamod/plugins.ini" ]; then
  680. # since it does exist, is the entry already in plugins.ini
  681. logentry="line (linux addons/amxmodx/dlls/amxmodx_mm_i386.so) removed from ${modinstalldir}/addons/metamod/plugins.ini"
  682. echo -en "removing amxmodx_mm_i386.so in plugins.ini..."
  683. # is it found? If so remove it and clean up
  684. if grep -q "linux addons/amxmodx/dlls/amxmodx_mm_i386.so" "${modinstalldir}/addons/metamod/plugins.ini"; then
  685. # delete the line we inserted
  686. sed -i '/linux addons\/amxmodx\/dlls\/amxmodx_mm_i386.so/d' "${modinstalldir}/addons/metamod/plugins.ini"
  687. # remove empty lines
  688. sed -i '/^$/d' "${modinstalldir}/addons/metamod/plugins.ini"
  689. exitcode=$?
  690. if [ "${exitcode}" -ne 0 ]; then
  691. fn_script_log_fail "${logentry}"
  692. fn_print_fail_eol_nl
  693. else
  694. fn_script_log_pass "${logentry}"
  695. fn_print_ok_eol_nl
  696. fi
  697. # if file is empty, remove it.
  698. if [ -f "${modinstalldir}/addons/metamod/plugins.ini" ]; then
  699. rm -f "${modinstalldir}/addons/metamod/plugins.ini"
  700. fn_script_log_pass "file removed ${modinstalldir}/addons/metamod/plugins.ini because it was empty"
  701. fi
  702. fi
  703. fi
  704. }
  705. ## Database initialisation.
  706. mods_list.sh
  707. fn_mods_available