diff --git a/h2mm b/h2mm index 3eb5d2d..ac6a07d 100755 --- a/h2mm +++ b/h2mm @@ -1,5 +1,7 @@ #!/bin/bash +# --- Globals --- + RED='\033[0;31m' GREEN='\033[0;32m' ORANGE='\033[0;33m' @@ -9,8 +11,14 @@ H2PATH="./h2path" MODS_DIR="" DB_FILE="" +# --- Utility Functions --- + +function get_filename_without_path() { + echo $(echo "$1" | awk -F/ '{print $NF}') +} + function get_basename() { - base_name=$(echo "$1" | awk -F/ '{print $NF}' | sed -E 's/\.+.*//') + echo $(get_filename_without_path "$1" | sed -E 's/\.+.*//') } function find_game_directory() { @@ -61,6 +69,8 @@ function initialize_directories() { echo "--- /// MAIN /// ---" >&2 } +# --- Help Functions --- + function display_help() { echo "Helldivers 2 Mod Manager" echo "Usage: h2mm [command] [options]" @@ -128,7 +138,9 @@ function display_import_help() { echo "Import mods and database from a zip file (coming from h2mm)." } -function reset() { +# --- Main Functions --- + +function mod_reset() { if [[ "$1" == "--help" || "$1" == "-h" ]]; then display_reset_help exit 0 @@ -143,7 +155,7 @@ function reset() { fi } -function install_mod() { +function mod_install() { local mod_name="" local mod_files=() local mod_dir="" @@ -214,18 +226,22 @@ function install_mod() { exit 1 fi - # verify mod files exist + # verify mod files exist and is not directory for file in "${mod_files[@]}"; do if [[ ! -f "$file" ]]; then - echo -e "${RED}Error${NC}: File $file does not exist." - exit 1 + if [[ ! -d "$file" ]]; then + echo -e "${RED}Error${NC}: File $file does not exist." + exit 1 + else + mod_files=(${mod_files[@]/$file}) + fi fi done declare -A patch_count # hash table - in case multiple named files are needed for 1 mod install, store the patch count target_files=() for file in "${mod_files[@]}"; do - get_basename "$file" + base_name=$(get_basename "$file") patch_prefix="$MODS_DIR/${base_name}.patch_" count=$(ls "${patch_prefix}"* 2>/dev/null | grep -E '([0-9]+$)' 2>/dev/null | wc -l) # count installed patches @@ -254,7 +270,7 @@ function install_mod() { echo -e "Mod $mod_name ($base_name) ${GREEN}installed successfully${NC}." } -function list_mods() { +function mod_list() { if [[ "$1" == "--help" || "$1" == "-h" ]]; then display_list_help exit 0 @@ -268,7 +284,7 @@ function list_mods() { awk -F, '{ if (length($3) > 150) $3 = substr($3, 1, 147) "..."; printf "%2s. %s (%s)\n", $1, $2, $3 }' "$DB_FILE" } -function uninstall_mod() { +function mod_uninstall() { local mod_name="" local mod_index="" @@ -321,7 +337,7 @@ function uninstall_mod() { echo -e "Removing ${ORANGE}\$MODS_DIR/$file${NC}." rm -f "$MODS_DIR/$file" - get_basename "$file" + base_name=$(get_basename "$file") current_version=$(echo $file | grep -oP '(?<=patch_)\d+') downgrades["$base_name"]=current_version done @@ -329,7 +345,7 @@ function uninstall_mod() { # downgrade any necessary mods for file in "${!downgrades[@]}"; do # find all files that have the same base name, and are greater than the current version, and downgrade them - get_basename "$file" + base_name=$(get_basename "$file") same_patches=$(ls "$MODS_DIR/${base_name}.patch_"* 2>/dev/null | grep -Eo "$base_name.*") for patch in $same_patches; do @@ -354,7 +370,7 @@ function uninstall_mod() { echo -e "Mod ${GREEN}uninstalled successfully${NC}." } -function export_mods() { +function mod_export() { if [[ "$1" == "--help" || "$1" == "-h" ]]; then display_export_help exit 0 @@ -383,7 +399,7 @@ function export_mods() { fi } -function import_mods() { +function mod_import() { if [[ "$1" == "--help" || "$1" == "-h" ]]; then display_import_help exit 0 @@ -418,42 +434,46 @@ function import_mods() { echo -e "Mods imported ${GREEN}successfully${NC}." } -# main -if [[ $# -lt 1 ]]; then - display_help - exit 1 -fi +# --- Main --- -command="$1" -shift - -initialize_directories - -case "$command" in - install|i) - install_mod "$@" - ;; - list|l) - list_mods "$@" - ;; - uninstall|u) - uninstall_mod "$@" - ;; - export|ex) - export_mods "$@" - ;; - import|im) - import_mods "$@" - ;; - reset|rr) - reset "$@" - ;; - help|--help|-h|h) +function main() { + if [[ $# -lt 1 ]]; then display_help - ;; - *) - display_help - ;; -esac + exit 1 + fi -echo "--- /// END /// ---" + command="$1" + shift + initialize_directories + + case "$command" in + install|i) + mod_install "$@" + ;; + list|l) + mod_list "$@" + ;; + uninstall|u) + mod_uninstall "$@" + ;; + export|ex) + mod_export "$@" + ;; + import|im) + mod_import "$@" + ;; + reset|rr) + mod_reset "$@" + ;; + help|--help|-h|h) + display_help + ;; + *) + display_help + ;; + esac + + echo "--- /// END /// ---" +} + +main "$@" \ No newline at end of file