mods_core.sh 31 KB

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