diff --git a/h2mm b/h2mm index a57d503..3220bcc 100755 --- a/h2mm +++ b/h2mm @@ -1,5 +1,10 @@ #!/bin/bash +# TODO: +# unzip command +# export mods +# import mods + RED='\033[0;31m' GREEN='\033[0;32m' ORANGE='\033[0;33m' @@ -9,6 +14,10 @@ H2PATH="./h2path" MODS_DIR="" DB_FILE="" +function get_basename() { + base_name=$(echo "$1" | sed -E 's/\.+.*//') +} + function find_game_directory() { local search_dir="/" local target_dir="Steam/steamapps/common/Helldivers\ 2/data" @@ -65,9 +74,35 @@ function display_help() { echo " install -n \"\" Install a mod with a name and files" echo " list List all installed mods" echo " uninstall -n \"\" Uninstall a mod by name" - echo " uninstall -i Uninstall a mod by index" + echo " export \"\" Export installed mods to a zip file" + echo " import \"\" Import mods from a zip file" + echo " help Display this help message" + echo "For more information, use h2mm [command] --help" + echo "Basic Usage:" + echo " h2mm install -n \"Example mod\" a5f2c029522e6714.patch_0 a5f2c029522e6714.patch_0.stream" + echo " h2mm uninstall -n \"Example mod\"" +} + +function display_install_help() { + echo "Usage: h2mm install -n \"\" [options] " + echo "Options:" + echo " -n \"\" Name of the mod (MANDATORY), inside double quotes" + echo " -d Directory containing mod files" + echo " -z Zip file containing mod files (.zip, .rar)" + echo " List of mod files to install" echo "Usage:" echo " h2mm install -n \"Example mod\" a5f2c029522e6714.patch_0 a5f2c029522e6714.patch_0.stream" + echo " h2mm install -n \"Example mod\" a5f* (using a wildcard to include all files)" + echo " h2mm install -n \"Example mod\" -d /path/to/mod/files" + echo " h2mm install -n \"Example mod\" -z /path/to/mod.zip" +} + +function display_uninstall_help() { + echo "Usage: h2mm uninstall [options]" + echo "Options:" + echo " -n \"\" Name of the mod to uninstall" + echo " -i Index of the mod to uninstall" + echo "Usage:" echo " h2mm uninstall -n \"Example mod\"" echo " h2mm uninstall -i 1" } @@ -75,6 +110,7 @@ function display_help() { function install_mod() { local mod_name="" local mod_files=() + local mod_dir="" # parse arguments while [[ $# -gt 0 ]]; do @@ -83,6 +119,18 @@ function install_mod() { mod_name="$2" shift 2 ;; + -d) + mod_dir="$2" + shift 2 + ;; + -z) + mod_zip="$2" + shift 2 + ;; + --help) + display_install_help + exit 0 + ;; *) mod_files+=("$1") shift @@ -90,6 +138,24 @@ function install_mod() { esac done + # zip file containing mod files + if [[ -n "$mod_zip" ]]; then + if ! command -v unzip &> /dev/null; then + echo -e "${RED}Error${NC}: unzip is not installed, please install the package and try again" + exit 1 + fi + temp_dir=$(mktemp -d) + unzip "$mod_zip" -d "$temp_dir" + mod_files+=$(find "$temp_dir" -type f -name "*.patch_*") + rm -rf "$temp_dir" + fi + + # directory containing mod files + if [[ -n "$mod_dir" ]]; then + # mod_files+=$(find "$mod_dir" -type f -name "*.patch_*") + readarray -d '' mod_files < <(find "$mod_dir" -type f -name "*.patch_*" -print0) + fi + if [[ -z "$mod_name" || ${#mod_files[@]} -eq 0 ]]; then echo -e "${RED}Error${NC}: Mod name and files are required." exit 1 @@ -98,7 +164,7 @@ function install_mod() { # verify mod files exist for file in "${mod_files[@]}"; do if [[ ! -f "$file" ]]; then - echo -e "${RED}Error${NC}: File $file does not exist." + echo -e "${RED}Error${NC}: File $file does not exist" exit 1 fi done @@ -106,7 +172,7 @@ function install_mod() { 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 - base_name=$(echo "$file" | sed -E 's/\.+.*//') + 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 @@ -124,7 +190,8 @@ function install_mod() { done # add entry to database - echo "$(($(wc -l < "$DB_FILE") + 1)),$mod_name,${target_files[*]}" >> "$DB_FILE" + next_id=$(awk -F, 'END {print $1 + 1}' "$DB_FILE") + echo "$next_id,$mod_name,${target_files[*]}" >> "$DB_FILE" echo -e "Mod $mod_name ($base_name) ${GREEN}installed successfully${NC}" } @@ -134,7 +201,6 @@ function list_mods() { return fi - echo "Installed Mods:" awk -v color="$GREEN" -v nc="$NC" -F, '{ printf "%s. %s%s%s (%s)\n", $1, color, $2, nc, $3 }' "$DB_FILE" } @@ -153,6 +219,10 @@ function uninstall_mod() { mod_index="$2" shift 2 ;; + --help) + display_uninstall_help + exit 0 + ;; *) echo -e "${RED}Error${NC}: Invalid argument $1" exit 1 @@ -167,9 +237,11 @@ function uninstall_mod() { # find mod files if [[ -n "$mod_index" ]]; then - entry=$(sed -n "${mod_index}p" "$DB_FILE") + entry=$(grep "^${mod_index}," "$DB_FILE") + mod_name=$(echo "$entry" | awk -F, '{print $2}') elif [[ -n "$mod_name" ]]; then entry=$(grep -i ",$mod_name," "$DB_FILE") + mod_index=$(echo "$entry" | awk -F, '{print $1}') fi if [[ -z "$entry" ]]; then @@ -179,17 +251,41 @@ function uninstall_mod() { # delete mod files files=$(echo "$entry" | cut -d',' -f3- | tr ',' ' ') + declare -A downgrades for file in $files; do - echo -e "Removing ${RED}\$MODS_DIR/$file${NC}" + echo -e "Removing ${YELLOW}\$MODS_DIR/$file${NC}" rm -f "$MODS_DIR/$file" + + get_basename "$file" + current_version=$(echo $file | grep -oP '(?<=patch_)\d+') + downgrades["$base_name"]=current_version + done + + # 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" + same_patches=$(ls "$MODS_DIR/${base_name}.patch_"* 2>/dev/null | grep -Eo "$base_name.*") + + for patch in $same_patches; do + patch_version=$(echo $patch | grep -oP '(?<=patch_)\d+') + if [[ $patch_version -gt ${downgrades[$file]} ]]; then + new_version=$((patch_version - 1)) + extension=$(echo "$patch" | sed -E 's/.*patch_[0-9]+//') + + new_patch="${base_name}.patch_${new_version}${extension}" + mv "$MODS_DIR/$patch" "$MODS_DIR/$new_patch" + echo -e "Downgraded ${ORANGE}$patch${NC} to ${GREEN}\$MODS_DIR/$new_patch${NC}" + + # save changes in database as well + sed -i "s/$patch/$new_patch/" "$DB_FILE" + fi + done done # remove entry from database - if [[ -n "$mod_index" ]]; then - sed -i "/^$mod_index/d" "$DB_FILE" - elif [[ -n "$mod_name" ]]; then - sed -i "/,$mod_name,/d" "$DB_FILE" - fi + sed -i "/^$mod_index/d" "$DB_FILE" + echo -e "Mod ${GREEN}uninstalled successfully${NC}" }