Skip to content

Instantly share code, notes, and snippets.

@aki-hp
Last active May 12, 2024 08:35
Show Gist options
  • Save aki-hp/27dbcd38f55b68e21d2edd5b5a46ebc5 to your computer and use it in GitHub Desktop.
Save aki-hp/27dbcd38f55b68e21d2edd5b5a46ebc5 to your computer and use it in GitHub Desktop.
Edited MO2 Broker for rockerbacon's script suite so that even if the NXMHandler fails, the download will still proceed. Might be useful for Steam Deck users (or Flatpak Protontricks users in general).
#!/usr/bin/env bash
# SUPPLY YOUR PERSONAL NEXUS API KEY HERE (nexus_apikey="yourNexusAPIKeyInsideDoubleQuotes")
nexus_apikey="yourNexusAPIKeyInsideDoubleQuotes"
### PARSE POSITIONAL ARGS ###
nxm_link=$1; shift
if [ -z "$nxm_link" ]; then
zenity --ok-label=Exit --ellipsize --error --text \
"ERROR: please specify a NXM Link to download"
exit 1
fi
nexus_game_link=${nxm_link#nxm://}
IFS='/' read -ra ADDR <<< "$nexus_game_link"
nexus_game_id=${ADDR[0]}
nexus_mod_id=${ADDR[2]}
IFS='?' read -r nexus_file_id nexus_query_string <<< "${ADDR[4]}"
nexus_file_id=$((nexus_file_id))
IFS='&' read -r key_pair expires_pair user_id_pair <<< "$nexus_query_string"
IFS='=' read -r _ nexus_key <<< "$key_pair"
IFS='=' read -r _ nexus_expires <<< "$expires_pair"
IFS='=' read -r _ nexus_user_id <<< "$user_id_pair"
nexus_expires=$((nexus_expires))
### PARSE POSITIONAL ARGS ###
instance_link="$HOME/.config/modorganizer2/instances/${nexus_game_id:?}"
instance_dir=$(readlink -f "$instance_link")
if [ ! -d "$instance_dir" ]; then
[ -L "$instance_link"] && rm "$instance_link"
zenity --ok-label=Exit --ellipsize --error --text \
"Could not download file because there is no Mod Organizer 2 instance for '$nexus_game_id'"
exit 1
fi
instance_dir_windowspath="Z:$(sed 's/\//\\\\/g' <<<$instance_dir)"
pgrep -f "$instance_dir_windowspath\\\\modorganizer2\\\\ModOrganizer.exe"
process_search_status=$?
game_appid=$(cat "$instance_dir/appid.txt")
steam_compat = $HOME/.local/share/Steam/steamapps/compatdata/$game_appid
if [ "$process_search_status" == "0" ]; then
echo "INFO: sending download '$nxm_link' to running Mod Organizer 2 instance"
download_start_output=$(WINEESYNC=1 WINEFSYNC=1 protontricks-launch --appid "$game_appid" "$instance_dir/modorganizer2/nxmhandler.exe" "$nxm_link")
download_start_status=$?
if [ "$download_start_status" != "0" ]; then
api_url=$"https://api.nexusmods.com/v1/games/${nexus_game_id}/mods/${nexus_mod_id}/files/${nexus_file_id}/download_link.json?key=${nexus_key}&expires=${nexus_expires}"
nexus_response=$(curl -X 'GET' \
"$api_url"\
-H 'accept: application/json' \
-H "apikey: ${nexus_apikey}")
uri=$(echo "$nexus_response" | python3 -c "import sys, json; data=json.load(sys.stdin); key='URI'; print(data[0][key] if isinstance(data, list) and data and key in data[0] else 'Unexpected structure or key not found')")
filename=$(basename "$uri")
filename=$(echo "$filename" | sed 's/\?.*$//')
uri_link=$(echo "$uri" |sed -e 's/ /%20/g')
download_start_output=$(curl -C - -L "$uri_link" -o "$instance_dir/modorganizer2/downloads/$filename" 2>&1 | stdbuf -oL tr '\r' '\n' | \
stdbuf -oL awk '/%/ {gsub(/%/,""); print $1}' | \
while read -r progress; do
echo "$progress"
done
) |
zenity --progress \
--title="Downloading Mod" \
--text="Downloading $filename..." \
--percentage=0 \
--auto-close \
--auto-kill
download_start_status=$?
fi
else
echo "INFO: starting Mod Organizer 2 to download '$nxm_link'"
download_start_output=$(steam -applaunch "$game_appid" "$nxm_link")
download_start_status=$?
fi
if [ "$download_start_status" != "0" ]; then
zenity --ok-label=Exit --ellipsize --error --text \
"Failed to start download:\n\n$filename"
exit 1
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment