feat: export, reset functionality

This commit is contained in:
v4n
2025-01-15 08:51:13 +02:00
parent 0f8ca3dd6b
commit 5b36ff282e
+102 -33
View File
@@ -3,7 +3,6 @@
# TODO:
# export mods
# import mods
# reset all
RED='\033[0;31m'
GREEN='\033[0;32m'
@@ -70,34 +69,32 @@ function display_help() {
echo "Helldivers 2 Mod Manager"
echo "Usage: h2mm [command] [options]"
echo "Commands:"
echo " install -n \"<mod_name>\" <mod_files> Install a mod with a name and files."
echo " uninstall -n \"<mod_name>\" Uninstall a mod by name."
echo " install Install a mod with files."
echo " uninstall Uninstall a mod by name."
echo " list List all installed mods."
echo " export \"<zip_name>\" Export installed mods to a zip file (not yet implemented)."
echo " import \"<zip_name>\" Import mods from a zip file (not yet implemented)."
echo " reset Reset all installed mods (not yet implemented)."
echo " export <zip_name> Export installed mods to a zip file (not yet implemented)."
echo " import <zip_name> Import mods from a zip file (not yet implemented)."
echo " reset Reset all installed mods."
echo " help Display this help message."
echo "For more information on usage, use h2mm [command] --help, available for install and uninstall."
echo "Basic Usage:"
echo " h2mm install -n \"Example mod\" a5f2c029522e6714.patch_0 a5f2c029522e6714.patch_0.stream"
echo " h2mm install -z /path/to/mod.zip"
echo " h2mm install -d /path/to/mod/files"
echo " h2mm uninstall -n \"Example mod\""
echo "Advanced Usage:"
echo " Database of mods is stored in Steam/steamapps/common/Helldivers\ 2/data/mods.csv"
echo " You can rename, delete, or edit this file to manage mods manually."
}
function display_install_help() {
echo "Usage: h2mm install -n \"<mod_name>\" [options] <mod_files>"
echo "Usage: h2mm install [options] <mod_files>"
echo "Options:"
echo " -n \"<mod_name>\" Name of the mod (MANDATORY), inside double quotes."
echo " -d <mod_dir> Directory containing mod files."
echo " -z <mod_zip> Zip file containing mod files (.zip only)."
echo " -n \"<mod_name>\" Name the mod yourself, inside double quotes."
echo " <mod_files> 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"
echo " h2mm install -z /path/to/mod.zip"
echo " h2mm install -d /path/to/mod/files"
echo " h2mm install -n \"Example mod\" mod.patch_0 mod.patch_0.stream (-n is mandatory when using files)"
echo " h2mm install -n \"Example mod\" mod* (using a wildcard to include all files)"
}
function display_uninstall_help() {
@@ -110,6 +107,35 @@ function display_uninstall_help() {
echo " h2mm uninstall -i 1"
}
function display_list_help() {
echo "Usage: h2mm list"
echo "List all installed mods."
echo " Database of mods is stored in Steam/steamapps/common/Helldivers\ 2/data/mods.csv"
echo " You can rename, delete, or edit this file to manage mods manually."
}
function display_reset_help() {
echo "Usage: h2mm reset"
echo "Reset all installed mods."
echo " Deletes all installed mods and the database file."
echo " Database of mods is stored in Steam/steamapps/common/Helldivers\ 2/data/mods.csv, along with the mods."
}
function reset() {
if [[ "$1" == "--help" || "$1" == "-h" ]]; then
display_reset_help
exit 0
fi
read -p "Are you sure you want to reset all installed mods? (y/n): " confirm
if [[ "$confirm" == "y" ]]; then
rm -f "$MODS_DIR"/*.patch_*
rm -f "$DB_FILE"
rm -f "$H2PATH"
echo "Database file deleted."
fi
}
function install_mod() {
local mod_name=""
local mod_files=()
@@ -135,7 +161,7 @@ function install_mod() {
mod_zip="$2"
shift 2
;;
--help)
--help|-h)
display_install_help
exit 0
;;
@@ -152,13 +178,27 @@ function install_mod() {
echo -e "${RED}Error${NC}: unzip is not installed, please install the package and try again."
exit 1
fi
if [[ ! -f "$mod_zip" ]]; then
echo -e "${RED}Error${NC}: Zip file $mod_zip does not exist."
exit 1
fi
if [[ -n "$mod_name" ]]; then
mod_name=$(basename "$mod_zip" | sed -E 's/\.zip//')
fi
mod_dir=$(mktemp -d)
unzip -qq "$mod_zip" -d "$mod_dir"
fi
# directory containing mod files
if [[ -n "$mod_dir" ]]; then
if [[ ! -d "$mod_dir" ]]; then
echo -e "${RED}Error${NC}: Directory $mod_dir does not exist."
exit 1
fi
readarray -d '' mod_files < <(find "$mod_dir" -type f -name "*.patch_*" -print0)
if [[ -n "$mod_name" ]]; then
mod_name=$($mod_dir | awk -F/ '{print $NF}')
fi
fi
# verify minimum information required
@@ -183,12 +223,18 @@ function install_mod() {
count=$(ls "${patch_prefix}"* 2>/dev/null | grep -E '([0-9]+$)' 2>/dev/null | wc -l) # count installed patches
# set patch count for file name
if [[ -z "${patch_count[$base_name]+unset}" ]]; then
patch_count["$base_name"]=$count
if [[ -z "${patch_count[$file]+unset}" ]]; then
patch_count["$file"]=$count
fi
patch_count["$base_name"]=$count
extension=$(echo "$file" | sed -E 's/.*patch_[0-9]+//')
target_file="${base_name}.patch_${patch_count[$base_name]}${extension}"
if [[ -n "$extension" ]]; then
target_file="${base_name}.patch_$((patch_count[$base_name] - 1))${extension}"
else
target_file="${base_name}.patch_${patch_count[$file]}"
fi
target_files+=($target_file)
cp "$file" "$MODS_DIR/$target_file"
@@ -202,12 +248,17 @@ function install_mod() {
}
function list_mods() {
if [[ "$1" == "--help" || "$1" == "-h" ]]; then
display_list_help
exit 0
fi
if [[ ! -s "$DB_FILE" ]]; then
echo "No mods installed."
return
fi
awk -v color="$GREEN" -v nc="$NC" -F, '{ printf "%s. %s%s%s (%s)\n", $1, color, $2, nc, $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() {
@@ -222,21 +273,17 @@ function uninstall_mod() {
# parse arguments
while [[ $# -gt 0 ]]; do
case "$1" in
-n)
mod_name="$2"
shift 2
;;
-i)
mod_index="$2"
shift 2
;;
--help)
--help|-h)
display_uninstall_help
exit 0
;;
*)
echo -e "${RED}Error${NC}: Invalid argument $1."
exit 1
mod_name="$1"
shift 1
;;
esac
done
@@ -300,6 +347,24 @@ function uninstall_mod() {
echo -e "Mod ${GREEN}uninstalled successfully${NC}."
}
function export_mods() {
if [[ "$1" == "--help" || "$1" == "-h" ]]; then
display_export_help
exit 0
fi
OUT_DIR=$(mktemp -d)
echo "$OUT_DIR"
cp "$DB_FILE" "$OUT_DIR"
ls "$MODS_DIR/" 2>/dev/null | grep -E 'patch_.*' | xargs -I {} cp "$MODS_DIR/{}" "$OUT_DIR"
if [[ $? -ne 0 ]]; then
echo -e "${RED}Error${NC}: Could not export mods. Possibly because no mods are present."
exit 1
fi
read -p "Enter the name of the zip file to export to: " zip_name
zip -r "$zip_name" "$OUT_DIR/*"
}
# main
if [[ $# -lt 1 ]]; then
display_help
@@ -314,24 +379,28 @@ initialize_directories
case "$command" in
install)
install_mod "$@"
echo "--- /// END /// ---"
;;
list)
list_mods
echo "--- /// END /// ---"
list_mods "$@"
;;
uninstall)
uninstall_mod "$@"
echo "--- /// END /// ---"
;;
export)
echo "Exporting mods to a zip file is not yet implemented."
export_mods "$@"
;;
import)
echo "Importing mods from a zip file is not yet implemented."
;;
reset)
reset "$@"
;;
help|--help|-h)
display_help
;;
*)
display_help
;;
esac
echo "--- /// END /// ---"