feat: unzip and directory functionality

This commit is contained in:
v4n
2025-01-14 15:59:23 +02:00
parent 3835382077
commit ecd31b6eac
2 changed files with 35 additions and 37 deletions
+1
View File
@@ -1,2 +1,3 @@
h2path h2path
*.patch* *.patch*
*test*
+33 -36
View File
@@ -1,7 +1,6 @@
#!/bin/bash #!/bin/bash
# TODO: # TODO:
# unzip command
# export mods # export mods
# import mods # import mods
@@ -15,7 +14,7 @@ MODS_DIR=""
DB_FILE="" DB_FILE=""
function get_basename() { function get_basename() {
base_name=$(echo "$1" | sed -E 's/\.+.*//') base_name=$(echo "$1" | awk -F/ '{print $NF}' | sed -E 's/\.+.*//')
} }
function find_game_directory() { function find_game_directory() {
@@ -32,7 +31,7 @@ function find_game_directory() {
echo "$saved_dir" echo "$saved_dir"
return return
else else
echo "Saved game directory is invalid" echo "Saved game directory is invalid."
fi fi
fi fi
@@ -41,10 +40,10 @@ function find_game_directory() {
game_dir=$(find "$search_dir" -type d -path "*/$target_dir" 2>/dev/null | head -n 1) game_dir=$(find "$search_dir" -type d -path "*/$target_dir" 2>/dev/null | head -n 1)
if [[ -z "$game_dir" ]]; then if [[ -z "$game_dir" ]]; then
echo "Could not find the Helldivers 2 data directory automatically" >&2 echo "Could not find the Helldivers 2 data directory automatically." >&2
read -p "Please enter the path to the Helldivers 2 data directory: " game_dir read -p "Please enter the path to the Helldivers 2 data directory: " game_dir
if [[ ! -d "$game_dir" ]]; then if [[ ! -d "$game_dir" ]]; then
echo -e "${RED}Error${NC}: Provided path is not a valid directory" echo -e "${RED}Error${NC}: Provided path is not a valid directory."
exit 1 exit 1
fi fi
fi fi
@@ -71,12 +70,12 @@ function display_help() {
echo "Helldivers 2 Mod Manager" echo "Helldivers 2 Mod Manager"
echo "Usage: h2mm [command] [options]" echo "Usage: h2mm [command] [options]"
echo "Commands:" echo "Commands:"
echo " install -n \"<mod_name>\" <mod_files> Install a mod with a name and files" echo " install -n \"<mod_name>\" <mod_files> Install a mod with a name and files."
echo " list List all installed mods" echo " list List all installed mods."
echo " uninstall -n \"<mod_name>\" Uninstall a mod by name" echo " uninstall -n \"<mod_name>\" Uninstall a mod by name."
echo " export \"<zip_name>\" Export installed mods to a zip file" echo " export \"<zip_name>\" Export installed mods to a zip file."
echo " import \"<zip_name>\" Import mods from a zip file" echo " import \"<zip_name>\" Import mods from a zip file."
echo " help Display this help message" echo " help Display this help message."
echo "For more information, use h2mm [command] --help" echo "For more information, use h2mm [command] --help"
echo "Basic Usage:" echo "Basic Usage:"
echo " h2mm install -n \"Example mod\" a5f2c029522e6714.patch_0 a5f2c029522e6714.patch_0.stream" echo " h2mm install -n \"Example mod\" a5f2c029522e6714.patch_0 a5f2c029522e6714.patch_0.stream"
@@ -86,10 +85,10 @@ function display_help() {
function display_install_help() { function display_install_help() {
echo "Usage: h2mm install -n \"<mod_name>\" [options] <mod_files>" echo "Usage: h2mm install -n \"<mod_name>\" [options] <mod_files>"
echo "Options:" echo "Options:"
echo " -n \"<mod_name>\" Name of the mod (MANDATORY), inside double quotes" echo " -n \"<mod_name>\" Name of the mod (MANDATORY), inside double quotes."
echo " -d <mod_dir> Directory containing mod files" echo " -d <mod_dir> Directory containing mod files."
echo " -z <mod_zip> Zip file containing mod files (.zip, .rar)" echo " -z <mod_zip> Zip file containing mod files (.zip only)."
echo " <mod_files> List of mod files to install" echo " <mod_files> List of mod files to install."
echo "Usage:" echo "Usage:"
echo " h2mm install -n \"Example mod\" a5f2c029522e6714.patch_0 a5f2c029522e6714.patch_0.stream" 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\" a5f* (using a wildcard to include all files)"
@@ -100,8 +99,8 @@ function display_install_help() {
function display_uninstall_help() { function display_uninstall_help() {
echo "Usage: h2mm uninstall [options]" echo "Usage: h2mm uninstall [options]"
echo "Options:" echo "Options:"
echo " -n \"<mod_name>\" Name of the mod to uninstall" echo " -n \"<mod_name>\" Name of the mod to uninstall."
echo " -i <index> Index of the mod to uninstall" echo " -i <index> Index of the mod to uninstall."
echo "Usage:" echo "Usage:"
echo " h2mm uninstall -n \"Example mod\"" echo " h2mm uninstall -n \"Example mod\""
echo " h2mm uninstall -i 1" echo " h2mm uninstall -i 1"
@@ -141,21 +140,19 @@ function install_mod() {
# zip file containing mod files # zip file containing mod files
if [[ -n "$mod_zip" ]]; then if [[ -n "$mod_zip" ]]; then
if ! command -v unzip &> /dev/null; then if ! command -v unzip &> /dev/null; then
echo -e "${RED}Error${NC}: unzip is not installed, please install the package and try again" echo -e "${RED}Error${NC}: unzip is not installed, please install the package and try again."
exit 1 exit 1
fi fi
temp_dir=$(mktemp -d) mod_dir=$(mktemp -d)
unzip "$mod_zip" -d "$temp_dir" unzip -qq "$mod_zip" -d "$mod_dir"
mod_files+=$(find "$temp_dir" -type f -name "*.patch_*")
rm -rf "$temp_dir"
fi fi
# directory containing mod files # directory containing mod files
if [[ -n "$mod_dir" ]]; then 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) readarray -d '' mod_files < <(find "$mod_dir" -type f -name "*.patch_*" -print0)
fi fi
# verify minimum information required
if [[ -z "$mod_name" || ${#mod_files[@]} -eq 0 ]]; then if [[ -z "$mod_name" || ${#mod_files[@]} -eq 0 ]]; then
echo -e "${RED}Error${NC}: Mod name and files are required." echo -e "${RED}Error${NC}: Mod name and files are required."
exit 1 exit 1
@@ -164,7 +161,7 @@ function install_mod() {
# verify mod files exist # verify mod files exist
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" echo -e "${RED}Error${NC}: File $file does not exist."
exit 1 exit 1
fi fi
done done
@@ -186,18 +183,18 @@ function install_mod() {
target_files+=($target_file) target_files+=($target_file)
cp "$file" "$MODS_DIR/$target_file" cp "$file" "$MODS_DIR/$target_file"
echo -e "Mod file ${ORANGE}$file${NC} installed at ${GREEN}\$MODS_DIR/$target_file${NC}" echo -e "Mod file ${ORANGE}$file${NC} installed at ${GREEN}\$MODS_DIR/$target_file${NC}."
done done
# add entry to database # add entry to database
next_id=$(awk -F, 'END {print $1 + 1}' "$DB_FILE") next_id=$(awk -F, 'END {print $1 + 1}' "$DB_FILE")
echo "$next_id,$mod_name,${target_files[*]}" >> "$DB_FILE" echo "$next_id,$mod_name,${target_files[*]}" >> "$DB_FILE"
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 list_mods() {
if [[ ! -s "$DB_FILE" ]]; then if [[ ! -s "$DB_FILE" ]]; then
echo "No mods installed" echo "No mods installed."
return return
fi fi
@@ -224,14 +221,14 @@ function uninstall_mod() {
exit 0 exit 0
;; ;;
*) *)
echo -e "${RED}Error${NC}: Invalid argument $1" echo -e "${RED}Error${NC}: Invalid argument $1."
exit 1 exit 1
;; ;;
esac esac
done done
if [[ -z "$mod_name" && -z "$mod_index" ]]; then if [[ -z "$mod_name" && -z "$mod_index" ]]; then
echo -e "${RED}Error${NC}: Mod name or index is required to uninstall" echo -e "${RED}Error${NC}: Mod name or index is required to uninstall."
exit 1 exit 1
fi fi
@@ -245,7 +242,7 @@ function uninstall_mod() {
fi fi
if [[ -z "$entry" ]]; then if [[ -z "$entry" ]]; then
echo -e "${RED}Error${NC}: Mod not found" echo -e "${RED}Error${NC}: Mod not found."
exit 1 exit 1
fi fi
@@ -253,7 +250,7 @@ function uninstall_mod() {
files=$(echo "$entry" | cut -d',' -f3- | tr ',' ' ') files=$(echo "$entry" | cut -d',' -f3- | tr ',' ' ')
declare -A downgrades declare -A downgrades
for file in $files; do for file in $files; do
echo -e "Removing ${YELLOW}\$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" get_basename "$file"
@@ -275,7 +272,7 @@ function uninstall_mod() {
new_patch="${base_name}.patch_${new_version}${extension}" new_patch="${base_name}.patch_${new_version}${extension}"
mv "$MODS_DIR/$patch" "$MODS_DIR/$new_patch" mv "$MODS_DIR/$patch" "$MODS_DIR/$new_patch"
echo -e "Downgraded ${ORANGE}$patch${NC} to ${GREEN}\$MODS_DIR/$new_patch${NC}" echo -e "Downgraded ${ORANGE}$patch${NC} to ${GREEN}\$MODS_DIR/$new_patch${NC}."
# save changes in database as well # save changes in database as well
sed -i "s/$patch/$new_patch/" "$DB_FILE" sed -i "s/$patch/$new_patch/" "$DB_FILE"
@@ -286,7 +283,7 @@ function uninstall_mod() {
# remove entry from database # remove entry from database
sed -i "/^$mod_index/d" "$DB_FILE" sed -i "/^$mod_index/d" "$DB_FILE"
echo -e "Mod ${GREEN}uninstalled successfully${NC}" echo -e "Mod ${GREEN}uninstalled successfully${NC}."
} }
# main # main
@@ -303,15 +300,15 @@ initialize_directories
case "$command" in case "$command" in
install) install)
install_mod "$@" install_mod "$@"
echo "--- /// ---" echo "--- /// END /// ---"
;; ;;
list) list)
list_mods list_mods
echo "--- /// ---" echo "--- /// END /// ---"
;; ;;
uninstall) uninstall)
uninstall_mod "$@" uninstall_mod "$@"
echo "--- /// ---" echo "--- /// END /// ---"
;; ;;
*) *)
display_help display_help