Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- ####################################################
- # PART 1 — Extract Games using unrar and innoextract
- ####################################################
- INNOEXTRACT_DIR="$HOME/Downloads/innoextract-1.9-linux"
- INNOEXTRACT_BIN="$INNOEXTRACT_DIR/innoextract"
- SOURCE_DIR="$HOME/Downloads"
- DEST_DIR="$HOME/Games/Heroic"
- if [ ! -f "$INNOEXTRACT_BIN" ]; then
- echo "Downloading innoextract..."
- curl -Lo "$SOURCE_DIR/innoextract-1.9-linux.tar.xz" "https://constexpr.org/innoextract/files/innoextract-1.9-linux.tar.xz"
- tar -xf "$SOURCE_DIR/innoextract-1.9-linux.tar.xz" -C "$SOURCE_DIR"
- else
- echo "innoextract already downloaded."
- fi
- mkdir -p "$DEST_DIR"
- find "$SOURCE_DIR" -maxdepth 1 -type f -name "*.rar" | while read -r rarfile; do
- filename=$(basename "$rarfile")
- base_name="${filename%.rar}"
- if [[ "$filename" == game* ]]; then
- echo "Processing GOG game archive: $filename"
- temp_extract_dir="$SOURCE_DIR/$base_name"
- final_extract_dir="$DEST_DIR/$base_name"
- if [ -d "$final_extract_dir" ]; then
- echo "Skipping $filename — already extracted."
- echo
- continue
- fi
- mkdir -p "$temp_extract_dir"
- unrar x -o+ "$rarfile" "$temp_extract_dir/" >/dev/null 2>&1
- mkdir -p "$final_extract_dir"
- find "$temp_extract_dir" -type f -name "*.exe" | while read -r exe_file; do
- echo "Running innoextract on: $exe_file"
- "$INNOEXTRACT_BIN" -d "$final_extract_dir" "$exe_file"
- done
- rm -rf "$temp_extract_dir"
- echo "Extracted GOG game to: $final_extract_dir"
- echo
- else
- extract_dir="$DEST_DIR/$base_name"
- if [ -d "$extract_dir" ]; then
- echo "Skipping $filename — already extracted."
- echo
- continue
- fi
- echo "Processing non-GOG archive: $filename"
- mkdir -p "$extract_dir"
- unrar x -o+ "$rarfile" "$extract_dir/" >/dev/null 2>&1
- find "$extract_dir" -type d -name "_CommonRedist" -exec rm -rf {} + 2>/dev/null
- echo "Extracted to: $extract_dir"
- echo
- fi
- done
- #############################################
- # PART 2 — Heroic .json builder
- #############################################
- games_dir="$HOME/Games/Heroic"
- json_file="$HOME/.var/app/com.heroicgameslauncher.hgl/config/heroic/sideload_apps/library.json"
- temp_json="/tmp/library_temp.json"
- API_KEY="STEAMGRIDDB API KEY HERE"
- pictures_download_dir="$HOME/Downloads/heroicpictures"
- pictures_final_dir="$HOME/.var/app/com.heroicgameslauncher.hgl/config/heroicpictures"
- # CUSTOM COVER SECTION
- declare -A custom_covers=(
- ["dread dawn"]="url here"
- )
- #CUSTOM INDEX SECTION
- declare -A indexed_covers=(
- ["Stardew Valley"]="5"
- ["Rain World"]="4"
- ["Underrail"]="1"
- ["Portal"]="5"
- )
- #CUSTOM EXE PATH SECTION
- declare -A custom_exes=(
- ["Swat 4 Gold Edition"]="$HOME/Games/Heroic/swat4/Content/System/Swat4.exe"
- )
- normalize_key() {
- echo "$1" | tr '[:upper:]' '[:lower:]' | sed -E 's/[.\-]/ /g' | tr -s ' ' | sed 's/^[[:space:]]*//;s/[[:space:]]*$//'
- }
- apply_custom_cover() {
- local title_norm
- title_norm=$(normalize_key "$1")
- for key in "${!custom_covers[@]}"; do
- local key_norm
- key_norm=$(normalize_key "$key")
- if [[ "$title_norm" == *"$key_norm"* ]]; then
- echo "${custom_covers[$key]}"
- return
- fi
- done
- echo ""
- }
- apply_custom_exe() {
- local folder_norm
- folder_norm=$(normalize_key "$1")
- for key in "${!custom_exes[@]}"; do
- local key_norm
- key_norm=$(normalize_key "$key")
- if [[ "$folder_norm" == *"$key_norm"* ]]; then
- echo "${custom_exes[$key]}"
- return
- fi
- done
- echo ""
- }
- if [[ -f "$json_file" ]]; then
- echo "Removing existing library JSON at $json_file for fresh start."
- rm "$json_file"
- fi
- mkdir -p "$(dirname "$json_file")" "$pictures_download_dir"
- echo '{"games": []}' > "$json_file"
- existing_entries=$(jq -r '.games[].folder_name' "$json_file")
- cp "$json_file" "$temp_json"
- longest_common_substring_length() {
- local s1="$1"
- local s2="$2"
- local max=0
- local len1=${#s1}
- local len2=${#s2}
- for ((i=0; i<len1; i++)); do
- for ((j=0; j<len2; j++)); do
- local lcs=0
- while [[ $((i+lcs)) -lt $len1 && $((j+lcs)) -lt $len2 && "${s1:i+lcs:1}" == "${s2:j+lcs:1}" ]]; do
- ((lcs++))
- done
- (( lcs > max )) && max=$lcs
- done
- done
- echo $max
- }
- make_nice_title() {
- local original="$1"
- local cleaned="$original"
- cleaned=$(echo "$cleaned" | sed -E 's/steamrip//Ig')
- cleaned=$(echo "$cleaned" | sed -E 's/\.com//Ig')
- cleaned=$(echo "$cleaned" | sed -E 's/\bgame\b//Ig')
- cleaned=$(echo "$cleaned" | sed -E 's/\([0-9]+\)//g')
- cleaned="${cleaned//-/ }"
- cleaned=$(echo "$cleaned" | tr -s ' ')
- cleaned=$(echo "$cleaned" | sed 's/^[[:space:]]*//;s/[[:space:]]*$//')
- echo "$cleaned"
- }
- capitalize_words() {
- echo "$1" | awk '{
- for(i=1; i<=NF; i++) {
- $i=toupper(substr($i,1,1)) tolower(substr($i,2))
- }
- print
- }'
- }
- fetch_steamgriddb_image_at_index() {
- local game_name="$1"
- local index="${2:-0}"
- local encoded_game_name
- encoded_game_name=$(python3 -c "import urllib.parse; print(urllib.parse.quote('''$game_name'''))")
- local search_response
- search_response=$(curl -s -H "Authorization: Bearer $API_KEY" "https://www.steamgriddb.com/api/v2/search/autocomplete/$encoded_game_name")
- local game_id
- game_id=$(echo "$search_response" | jq -r '.data[0].id // empty')
- if [[ -z "$game_id" ]]; then
- echo ""
- return
- fi
- local grids_response
- grids_response=$(curl -s -H "Authorization: Bearer $API_KEY" "https://www.steamgriddb.com/api/v2/grids/game/$game_id")
- local urls
- mapfile -t urls < <(echo "$grids_response" | jq -r '
- .data
- | map(select((.style == "official") or (.style == "alternate")))
- | sort_by(-.score)
- | .[].url
- ')
- if [[ ${#urls[@]} -eq 0 ]]; then
- mapfile -t urls < <(echo "$grids_response" | jq -r '
- .data
- | sort_by(-.score)
- | .[].url
- ')
- fi
- if (( index < ${#urls[@]} )); then
- echo "${urls[index]}"
- else
- echo ""
- fi
- }
- for folder_path in "$games_dir"/*/; do
- folder_path="${folder_path%/}"
- folder_name_only=$(basename "$folder_path")
- if [[ "$(normalize_key "$folder_name_only")" == "prefixes" ]]; then
- echo "Skipping prefixes folder."
- continue
- fi
- if echo "$existing_entries" | grep -Fxq "$folder_path"; then
- echo "Skipping already entered: $folder_path"
- continue
- fi
- echo "Processing folder: $folder_path"
- exe_override=$(apply_custom_exe "$folder_name_only")
- if [[ -n "$exe_override" ]]; then
- best_exe="$exe_override"
- if [[ ! -f "$best_exe" ]]; then
- echo "Custom exe path does not exist: $best_exe, skipping."
- continue
- fi
- echo "Using custom exe override: $best_exe"
- else
- mapfile -t exe_files < <(find "$folder_path" -type f -iname '*.exe')
- if [[ ${#exe_files[@]} -eq 0 ]]; then
- echo "No .exe files in $folder_path, skipping."
- continue
- fi
- best_exe=""
- best_score=0
- normalized_folder=$(normalize_key "$folder_name_only")
- for exe_file in "${exe_files[@]}"; do
- exe_base=$(basename "$exe_file" .exe)
- exe_lower=$(echo "$exe_base" | tr '[:upper:]' '[:lower:]')
- score=$(longest_common_substring_length "$normalized_folder" "$exe_lower")
- if (( score > best_score )); then
- best_score=$score
- best_exe="$exe_file"
- fi
- done
- if [[ -z "$best_exe" ]]; then
- echo "Could not find suitable .exe, skipping."
- continue
- fi
- echo "Selected executable: $best_exe"
- fi
- app_name=$(uuidgen)
- nice_title=$(make_nice_title "$folder_name_only")
- nice_title="${nice_title//./ }"
- nice_title=$(capitalize_words "$nice_title")
- local_art_path="$pictures_final_dir/$nice_title/cover.jpg"
- if [[ -f "$local_art_path" ]]; then
- echo "Cover already exists for '$nice_title', skipping SteamGridDB fetch."
- cover_url=""
- else
- cover_url=$(apply_custom_cover "$nice_title")
- if [[ -z "$cover_url" ]]; then
- norm_title=$(normalize_key "$nice_title")
- selected_index=""
- for key in "${!indexed_covers[@]}"; do
- key_norm=$(normalize_key "$key")
- if [[ "$norm_title" == *"$key_norm"* ]]; then
- selected_index="${indexed_covers[$key]}"
- break
- fi
- done
- if [[ -n "$selected_index" ]]; then
- echo "Fetching SteamGridDB image for '$nice_title' at index $selected_index..."
- cover_url=$(fetch_steamgriddb_image_at_index "$nice_title" "$selected_index")
- else
- echo "Fetching SteamGridDB image for '$nice_title' at default index 0..."
- cover_url=$(fetch_steamgriddb_image_at_index "$nice_title" "0")
- fi
- else
- echo "Using custom cover for '$nice_title': $cover_url"
- fi
- fi
- if [[ -n "$cover_url" ]]; then
- pic_folder="$pictures_download_dir/$nice_title"
- mkdir -p "$pic_folder"
- pic_file="$pic_folder/cover.jpg"
- echo "Downloading cover art to $pic_file"
- curl -L -s "$cover_url" -o "$pic_file"
- else
- echo "No new cover art needed or found for '$nice_title'."
- fi
- done
- echo "Replacing final config pictures with downloaded pictures..."
- rm -rf "$pictures_final_dir"
- mkdir -p "$pictures_final_dir"
- cp -r "$pictures_download_dir/." "$pictures_final_dir/"
- echo '{"games": []}' > "$json_file"
- for folder_path in "$games_dir"/*/; do
- folder_path="${folder_path%/}"
- folder_name_only=$(basename "$folder_path")
- normalized_folder=$(normalize_key "$folder_name_only")
- if [[ "$normalized_folder" == "prefixes" ]]; then
- continue
- fi
- if echo "$existing_entries" | grep -Fxq "$folder_path"; then
- continue
- fi
- exe_override=$(apply_custom_exe "$folder_name_only")
- if [[ -n "$exe_override" ]]; then
- best_exe="$exe_override"
- else
- mapfile -t exe_files < <(find "$folder_path" -type f -iname '*.exe')
- best_exe=""
- best_score=0
- for exe_file in "${exe_files[@]}"; do
- exe_base=$(basename "$exe_file" .exe)
- exe_lower=$(echo "$exe_base" | tr '[:upper:]' '[:lower:]')
- score=$(longest_common_substring_length "$normalized_folder" "$exe_lower")
- if (( score > best_score )); then
- best_score=$score
- best_exe="$exe_file"
- fi
- done
- fi
- app_name=$(uuidgen)
- nice_title=$(make_nice_title "$folder_name_only")
- nice_title="${nice_title//./ }"
- nice_title=$(capitalize_words "$nice_title")
- local_art_path="$pictures_final_dir/$nice_title/cover.jpg"
- null_val=null
- new_game=$(jq -n \
- --arg runner "sideload" \
- --arg app_name "$app_name" \
- --arg title "$nice_title" \
- --arg exe "$best_exe" \
- --arg folder "$folder_path" \
- --argjson is_dlc false \
- --arg platform "Windows" \
- --argjson null_val "$null_val" \
- --arg art_url "$local_art_path" \
- '{
- runner: $runner,
- app_name: $app_name,
- title: $title,
- install: {
- executable: $exe,
- platform: $platform,
- is_dlc: $is_dlc
- },
- folder_name: $folder,
- art_cover: ($art_url | select(length > 0) // $null_val),
- art_square: ($art_url | select(length > 0) // $null_val),
- is_installed: true,
- canRunOffline: true,
- browserUrl: "",
- customUserAgent: "",
- launchFullScreen: false
- }'
- )
- jq ".games += [$new_game]" "$json_file" > "${json_file}.tmp" && mv "${json_file}.tmp" "$json_file"
- done
- echo "Library JSON updated using local cover images only. Covers backed up and synchronized."
Advertisement
Add Comment
Please, Sign In to add comment