From 24960038693225b9884cec2c60c0dc27074ecc99 2022-06-18 13:55:05 From: Branko Majic <branko@majic.rs> Date: 2022-06-18 13:55:05 Subject: [PATCH] [factorio_manager.sh] Drop non-jq variant for listing information, improve output: - No need to complicate the script for non-jq variant. - Extract as much mod inforamtion as possible from the mod's info.json file. - Use mod title for showing a friendlier (non-internal) name to user. --- diff --git a/games/factorio_manager.sh b/games/factorio_manager.sh index 4a34e4cef04934c9c8b8b12a8ed08b33744a451c..415ec7fcf764d50a98768a37838107866caf5ff7 100755 --- a/games/factorio_manager.sh +++ b/games/factorio_manager.sh @@ -1921,60 +1921,7 @@ elif [[ $command == info ]]; then if [[ ${#mod_files[@]} == 0 ]]; then echo "Available mods: none" - - # Listing using just regular tools. - elif ! hash jq 2>/dev/null; then - echo "Available mods:" - echo - - # Determine if mod is enabled by default or not when added to - # mods directory (and is not yet listed in the mod list file). - if grep -i -q "^[[:blank:]]*enable-new-mods=false" "$game_config"; then - enable_new_mods="false" - else - enable_new_mods="true" - fi - - # Process every mod found. - for mod_file in "${mod_files[@]}"; do - - if [[ -f $mod_file ]]; then - # Extract basic information about mod from the filename. - mod_basename=$(basename "$mod_file" .zip) - mod_name="${mod_basename%_*}" - mod_version="${mod_basename##*_}" - - # Determine if mod is enabled or not. - - if grep -A1 "\"name\": \"$mod_name\"" "$mod_list" | tail -n1 | grep -q '"enabled": false'; then - color="$_text_yellow" - elif grep -A1 "\"name\": \"$mod_name\"" "$mod_list" | tail -n1 | grep -q '"enabled": true'; then - color="" - elif [[ $enable_new_mods == "false" ]]; then - color="$_text_yellow" - else - color="" - fi - - # Show colored-information for the mod. - printf "$color - %-48s %8s $_text_reset\n" "$mod_name" "$mod_version" - if (( verbose == 1 )); then - mod_info=$(unzip -Z1 "$mod_file" | grep "info\.json" | head -n1) - mod_description=$(unzip -p "$mod_file" "$mod_info" | grep '"description":' | sed -e 's/\r//g;s/[^"]*"description": //;s/^"//;s/"$//') - mod_description=$(echo -e "$mod_description" | fold -s -w 50 | sed -e 's/^/ /') - mod_url="https://mods.factorio.com/mod/${mod_info}" - echo - echo " ($mod_url)" - echo - echo "$mod_description" - echo - fi - fi - - done - - # Listing using the jq utility. - elif hash jq 2>/dev/null; then + else echo "Available mods (enabled/${_text_yellow}disabled${_text_reset}):" echo @@ -1999,10 +1946,22 @@ elif [[ $command == info ]]; then if [[ -f $mod_file ]]; then - # Extract basic information about mod from the filename. + # Extract internal name of the mod (used for URL paths + # etc). Differs from data stored in info.json. mod_basename=$(basename "$mod_file" .zip) - mod_name="${mod_basename%_*}" - mod_version="${mod_basename##*_}" + mod_internal_name="${mod_basename%_*}" + + # Extract mod information. + mod_info_file=$(unzip -Z1 "$mod_file" | grep "info\.json" | head -n1) + mod_info=$(unzip -p "$mod_file" "$mod_info_file") + mod_name=$(jq -r -e '.name' <<< "$mod_info") + mod_title=$(jq -r -e '.title' <<< "$mod_info") + mod_description=$(jq -r -e '.description' <<< "$mod_info") + mod_version=$(jq -r -e '.version' <<< "$mod_info") + mod_url="https://mods.factorio.com/mod/${mod_name}" + + # Pretty-print description. + mod_description=$(fold -s -w 50 <<< "$mod_description" | sed -e 's/^/ /') # Determine if mod is enabled or not. if jq -e \ @@ -2015,11 +1974,8 @@ elif [[ $command == info ]]; then fi # Show colored-information for the mod. - printf "$color - %-48s %8s $_text_reset\n" "$mod_name" "$mod_version" + printf "$color - %-48s %8s $_text_reset\n" "$mod_title" "$mod_version" if (( verbose == 1 )); then - mod_info=$(unzip -Z1 "$mod_file" | grep "info\.json" | head -n1) - mod_description=$(unzip -p "$mod_file" "$mod_info" | jq -r -e '.description' | fold -s -w 50 | sed -e 's/^/ /') - mod_url="https://mods.factorio.com/mod/${mod_info}" echo echo " ($mod_url)" echo