fix: directories stop counting as files
This commit is contained in:
@@ -1,5 +1,7 @@
|
|||||||
#!/bin/bash
|
#!/bin/bash
|
||||||
|
|
||||||
|
# --- Globals ---
|
||||||
|
|
||||||
RED='\033[0;31m'
|
RED='\033[0;31m'
|
||||||
GREEN='\033[0;32m'
|
GREEN='\033[0;32m'
|
||||||
ORANGE='\033[0;33m'
|
ORANGE='\033[0;33m'
|
||||||
@@ -9,8 +11,14 @@ H2PATH="./h2path"
|
|||||||
MODS_DIR=""
|
MODS_DIR=""
|
||||||
DB_FILE=""
|
DB_FILE=""
|
||||||
|
|
||||||
|
# --- Utility Functions ---
|
||||||
|
|
||||||
|
function get_filename_without_path() {
|
||||||
|
echo $(echo "$1" | awk -F/ '{print $NF}')
|
||||||
|
}
|
||||||
|
|
||||||
function get_basename() {
|
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() {
|
function find_game_directory() {
|
||||||
@@ -61,6 +69,8 @@ function initialize_directories() {
|
|||||||
echo "--- /// MAIN /// ---" >&2
|
echo "--- /// MAIN /// ---" >&2
|
||||||
}
|
}
|
||||||
|
|
||||||
|
# --- Help Functions ---
|
||||||
|
|
||||||
function display_help() {
|
function display_help() {
|
||||||
echo "Helldivers 2 Mod Manager"
|
echo "Helldivers 2 Mod Manager"
|
||||||
echo "Usage: h2mm [command] [options]"
|
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)."
|
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
|
if [[ "$1" == "--help" || "$1" == "-h" ]]; then
|
||||||
display_reset_help
|
display_reset_help
|
||||||
exit 0
|
exit 0
|
||||||
@@ -143,7 +155,7 @@ function reset() {
|
|||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
function install_mod() {
|
function mod_install() {
|
||||||
local mod_name=""
|
local mod_name=""
|
||||||
local mod_files=()
|
local mod_files=()
|
||||||
local mod_dir=""
|
local mod_dir=""
|
||||||
@@ -214,18 +226,22 @@ function install_mod() {
|
|||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# verify mod files exist
|
# verify mod files exist and is not directory
|
||||||
for file in "${mod_files[@]}"; do
|
for file in "${mod_files[@]}"; do
|
||||||
if [[ ! -f "$file" ]]; then
|
if [[ ! -f "$file" ]]; then
|
||||||
echo -e "${RED}Error${NC}: File $file does not exist."
|
if [[ ! -d "$file" ]]; then
|
||||||
exit 1
|
echo -e "${RED}Error${NC}: File $file does not exist."
|
||||||
|
exit 1
|
||||||
|
else
|
||||||
|
mod_files=(${mod_files[@]/$file})
|
||||||
|
fi
|
||||||
fi
|
fi
|
||||||
done
|
done
|
||||||
|
|
||||||
declare -A patch_count # hash table - in case multiple named files are needed for 1 mod install, store the patch count
|
declare -A patch_count # hash table - in case multiple named files are needed for 1 mod install, store the patch count
|
||||||
target_files=()
|
target_files=()
|
||||||
for file in "${mod_files[@]}"; do
|
for file in "${mod_files[@]}"; do
|
||||||
get_basename "$file"
|
base_name=$(get_basename "$file")
|
||||||
patch_prefix="$MODS_DIR/${base_name}.patch_"
|
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
|
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}."
|
echo -e "Mod $mod_name ($base_name) ${GREEN}installed successfully${NC}."
|
||||||
}
|
}
|
||||||
|
|
||||||
function list_mods() {
|
function mod_list() {
|
||||||
if [[ "$1" == "--help" || "$1" == "-h" ]]; then
|
if [[ "$1" == "--help" || "$1" == "-h" ]]; then
|
||||||
display_list_help
|
display_list_help
|
||||||
exit 0
|
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"
|
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_name=""
|
||||||
local mod_index=""
|
local mod_index=""
|
||||||
|
|
||||||
@@ -321,7 +337,7 @@ function uninstall_mod() {
|
|||||||
echo -e "Removing ${ORANGE}\$MODS_DIR/$file${NC}."
|
echo -e "Removing ${ORANGE}\$MODS_DIR/$file${NC}."
|
||||||
rm -f "$MODS_DIR/$file"
|
rm -f "$MODS_DIR/$file"
|
||||||
|
|
||||||
get_basename "$file"
|
base_name=$(get_basename "$file")
|
||||||
current_version=$(echo $file | grep -oP '(?<=patch_)\d+')
|
current_version=$(echo $file | grep -oP '(?<=patch_)\d+')
|
||||||
downgrades["$base_name"]=current_version
|
downgrades["$base_name"]=current_version
|
||||||
done
|
done
|
||||||
@@ -329,7 +345,7 @@ function uninstall_mod() {
|
|||||||
# downgrade any necessary mods
|
# downgrade any necessary mods
|
||||||
for file in "${!downgrades[@]}"; do
|
for file in "${!downgrades[@]}"; do
|
||||||
# find all files that have the same base name, and are greater than the current version, and downgrade them
|
# 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.*")
|
same_patches=$(ls "$MODS_DIR/${base_name}.patch_"* 2>/dev/null | grep -Eo "$base_name.*")
|
||||||
|
|
||||||
for patch in $same_patches; do
|
for patch in $same_patches; do
|
||||||
@@ -354,7 +370,7 @@ function uninstall_mod() {
|
|||||||
echo -e "Mod ${GREEN}uninstalled successfully${NC}."
|
echo -e "Mod ${GREEN}uninstalled successfully${NC}."
|
||||||
}
|
}
|
||||||
|
|
||||||
function export_mods() {
|
function mod_export() {
|
||||||
if [[ "$1" == "--help" || "$1" == "-h" ]]; then
|
if [[ "$1" == "--help" || "$1" == "-h" ]]; then
|
||||||
display_export_help
|
display_export_help
|
||||||
exit 0
|
exit 0
|
||||||
@@ -383,7 +399,7 @@ function export_mods() {
|
|||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
function import_mods() {
|
function mod_import() {
|
||||||
if [[ "$1" == "--help" || "$1" == "-h" ]]; then
|
if [[ "$1" == "--help" || "$1" == "-h" ]]; then
|
||||||
display_import_help
|
display_import_help
|
||||||
exit 0
|
exit 0
|
||||||
@@ -418,42 +434,46 @@ function import_mods() {
|
|||||||
echo -e "Mods imported ${GREEN}successfully${NC}."
|
echo -e "Mods imported ${GREEN}successfully${NC}."
|
||||||
}
|
}
|
||||||
|
|
||||||
# main
|
# --- Main ---
|
||||||
if [[ $# -lt 1 ]]; then
|
|
||||||
display_help
|
|
||||||
exit 1
|
|
||||||
fi
|
|
||||||
|
|
||||||
command="$1"
|
function main() {
|
||||||
shift
|
if [[ $# -lt 1 ]]; then
|
||||||
|
|
||||||
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)
|
|
||||||
display_help
|
display_help
|
||||||
;;
|
exit 1
|
||||||
*)
|
fi
|
||||||
display_help
|
|
||||||
;;
|
|
||||||
esac
|
|
||||||
|
|
||||||
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 "$@"
|
||||||
Reference in New Issue
Block a user