NETPLAY: outsourced flags, added placeholder logics for pretendo, pcsx2, rpcs3 and dolphin

This commit is contained in:
XargonWan 2024-06-18 15:17:48 +09:00
parent 3adb696922
commit 1115fcab3e

View file

@ -1,279 +1,59 @@
#!/bin/bash
# Mapping of country codes to flag emojis
declare -A country_flags=(
["af"]="🇦🇫"
["ax"]="🇦🇽"
["al"]="🇦🇱"
["dz"]="🇩🇿"
["as"]="🇦🇸"
["ad"]="🇦🇩"
["ao"]="🇦🇴"
["ai"]="🇦🇮"
["aq"]="🇦🇶"
["ag"]="🇦🇬"
["ar"]="🇦🇷"
["am"]="🇦🇲"
["aw"]="🇦🇼"
["au"]="🇦🇺"
["at"]="🇦🇹"
["az"]="🇦🇿"
["bs"]="🇧🇸"
["bh"]="🇧🇭"
["bd"]="🇧🇩"
["bb"]="🇧🇧"
["by"]="🇧🇾"
["be"]="🇧🇪"
["bz"]="🇧🇿"
["bj"]="🇧🇯"
["bm"]="🇧🇲"
["bt"]="🇧🇹"
["bo"]="🇧🇴"
["bq"]="🇧🇶"
["ba"]="🇧🇦"
["bw"]="🇧🇼"
["bv"]="🇧🇻"
["br"]="🇧🇷"
["io"]="🇮🇴"
["bn"]="🇧🇳"
["bg"]="🇧🇬"
["bf"]="🇧🇫"
["bi"]="🇧🇮"
["kh"]="🇰🇭"
["cm"]="🇨🇲"
["ca"]="🇨🇦"
["cv"]="🇨🇻"
["ky"]="🇰🇾"
["cf"]="🇨🇫"
["td"]="🇹🇩"
["cl"]="🇨🇱"
["cn"]="🇨🇳"
["cx"]="🇨🇽"
["cc"]="🇨🇨"
["co"]="🇨🇴"
["km"]="🇰🇲"
["cd"]="🇨🇩"
["cg"]="🇨🇬"
["ck"]="🇨🇰"
["cr"]="🇨🇷"
["ci"]="🇨🇮"
["hr"]="🇭🇷"
["cu"]="🇨🇺"
["cw"]="🇨🇼"
["cy"]="🇨🇾"
["cz"]="🇨🇿"
["dk"]="🇩🇰"
["dj"]="🇩🇯"
["dm"]="🇩🇲"
["do"]="🇩🇴"
["ec"]="🇪🇨"
["eg"]="🇪🇬"
["sv"]="🇸🇻"
["gq"]="🇬🇶"
["er"]="🇪🇷"
["ee"]="🇪🇪"
["et"]="🇪🇹"
["fk"]="🇫🇰"
["fo"]="🇫🇴"
["fj"]="🇫🇯"
["fi"]="🇫🇮"
["fr"]="🇫🇷"
["gf"]="🇬🇫"
["pf"]="🇵🇫"
["tf"]="🇹🇫"
["ga"]="🇬🇦"
["gm"]="🇬🇲"
["ge"]="🇬🇪"
["de"]="🇩🇪"
["gh"]="🇬🇭"
["gi"]="🇬🇮"
["gr"]="🇬🇷"
["gl"]="🇬🇱"
["gd"]="🇬🇩"
["gp"]="🇬🇵"
["gu"]="🇬🇺"
["gt"]="🇬🇹"
["gg"]="🇬🇬"
["gn"]="🇬🇳"
["gw"]="🇬🇼"
["gy"]="🇬🇾"
["ht"]="🇭🇹"
["hm"]="🇭🇲"
["va"]="🇻🇦"
["hn"]="🇭🇳"
["hk"]="🇭🇰"
["hu"]="🇭🇺"
["is"]="🇮🇸"
["in"]="🇮🇳"
["id"]="🇮🇩"
["ir"]="🇮🇷"
["iq"]="🇮🇶"
["ie"]="🇮🇪"
["im"]="🇮🇲"
["il"]="🇮🇱"
["it"]="🇮🇹"
["jm"]="🇯🇲"
["jp"]="🇯🇵"
["je"]="🇯🇪"
["jo"]="🇯🇴"
["kz"]="🇰🇿"
["ke"]="🇰🇪"
["ki"]="🇰🇮"
["kp"]="🇰🇵"
["kr"]="🇰🇷"
["kw"]="🇰🇼"
["kg"]="🇰🇬"
["la"]="🇱🇦"
["lv"]="🇱🇻"
["lb"]="🇱🇧"
["ls"]="🇱🇸"
["lr"]="🇱🇷"
["ly"]="🇱🇾"
["li"]="🇱🇮"
["lt"]="🇱🇹"
["lu"]="🇱🇺"
["mo"]="🇲🇴"
["mg"]="🇲🇬"
["mw"]="🇲🇼"
["my"]="🇲🇾"
["mv"]="🇲🇻"
["ml"]="🇲🇱"
["mt"]="🇲🇹"
["mh"]="🇲🇭"
["mq"]="🇲🇶"
["mr"]="🇲🇷"
["mu"]="🇲🇺"
["yt"]="🇾🇹"
["mx"]="🇲🇽"
["fm"]="🇫🇲"
["md"]="🇲🇩"
["mc"]="🇲🇨"
["mn"]="🇲🇳"
["me"]="🇲🇪"
["ms"]="🇲🇸"
["ma"]="🇲🇦"
["mz"]="🇲🇿"
["mm"]="🇲🇲"
["na"]="🇳🇦"
["nr"]="🇳🇷"
["np"]="🇳🇵"
["nl"]="🇳🇱"
["nc"]="🇳🇨"
["nz"]="🇳🇿"
["ni"]="🇳🇮"
["ne"]="🇳🇪"
["ng"]="🇳🇬"
["nu"]="🇳🇺"
["nf"]="🇳🇫"
["mk"]="🇲🇰"
["mp"]="🇲🇵"
["no"]="🇳🇴"
["om"]="🇴🇲"
["pk"]="🇵🇰"
["pw"]="🇵🇼"
["ps"]="🇵🇸"
["pa"]="🇵🇦"
["pg"]="🇵🇬"
["py"]="🇵🇾"
["pe"]="🇵🇪"
["ph"]="🇵🇭"
["pn"]="🇵🇳"
["pl"]="🇵🇱"
["pt"]="🇵🇹"
["pr"]="🇵🇷"
["qa"]="🇶🇦"
["re"]="🇷🇪"
["ro"]="🇷🇴"
["ru"]="🇷🇺"
["rw"]="🇷🇼"
["bl"]="🇧🇱"
["sh"]="🇸🇭"
["kn"]="🇰🇳"
["lc"]="🇱🇨"
["mf"]="🇲🇫"
["pm"]="🇵🇲"
["vc"]="🇻🇨"
["ws"]="🇼🇸"
["sm"]="🇸🇲"
["st"]="🇸🇹"
["sa"]="🇸🇦"
["sn"]="🇸🇳"
["rs"]="🇷🇸"
["sc"]="🇸🇨"
["sl"]="🇸🇱"
["sg"]="🇸🇬"
["sx"]="🇸🇽"
["sk"]="🇸🇰"
["si"]="🇸🇮"
["sb"]="🇸🇧"
["so"]="🇸🇴"
["za"]="🇿🇦"
["gs"]="🇬🇸"
["ss"]="🇸🇸"
["es"]="🇪🇸"
["lk"]="🇱🇰"
["sd"]="🇸🇩"
["sr"]="🇸🇷"
["sj"]="🇸🇯"
["se"]="🇸🇪"
["ch"]="🇨🇭"
["sy"]="🇸🇾"
["tw"]="🇹🇼"
["tj"]="🇹🇯"
["tz"]="🇹🇿"
["th"]="🇹🇭"
["tl"]="🇹🇱"
["tg"]="🇹🇬"
["tk"]="🇹🇰"
["to"]="🇹🇴"
["tt"]="🇹🇹"
["tn"]="🇹🇳"
["tr"]="🇹🇷"
["tm"]="🇹🇲"
["tc"]="🇹🇨"
["tv"]="🇹🇻"
["ug"]="🇺🇬"
["ua"]="🇺🇦"
["ae"]="🇦🇪"
["gb"]="🇬🇧"
["um"]="🇺🇲"
["us"]="🇺🇸"
["uy"]="🇺🇾"
["uz"]="🇺🇿"
["vu"]="🇻🇺"
["ve"]="🇻🇪"
["vn"]="🇻🇳"
["vg"]="🇻🇬"
["vi"]="🇻🇮"
["wf"]="🇼🇫"
["eh"]="🇪🇭"
["ye"]="🇾🇪"
["zm"]="🇿🇲"
["zw"]="🇿🇼"
)
source functions/flags.sh # TODO: set it in the real folder for flatpak enviro
populate_table() {
# URL of the RetroArch lobby API
url="http://lobby.libretro.com/list"
retroarch_url="http://lobby.libretro.com/list"
# Fetch the list of netplay rooms in JSON format
response=$(curl -s "$url")
# Check if the response is empty or if there are errors
if [ -z "$response" ]; then
zenity --error --text="Error connecting to the RetroArch Netplay server."
exit 1
retroarch_response=$(curl -s "$retroarch_url")
# Fetch Pretendo netplay rooms (Example URL)
pretendo_url="https://pretendo.network/api/rooms"
pretendo_response=$(curl -s "$pretendo_url")
# Fetch PCSX2 netplay rooms (Example URL)
pcsx2_url="https://pcsx2.netplay.api/rooms"
pcsx2_response=$(curl -s "$pcsx2_url")
# Fetch RPCS3 netplay rooms (Example URL)
rpcs3_url="https://rpcn.rpc3.net/v1/rooms"
rpcs3_response=$(curl -s "$rpcs3_url")
# Fetch Dolphin netplay rooms (Example URL)
dolphin_url="https://dolphin-emu.org/netplay/rooms"
dolphin_response=$(curl -s "$dolphin_url")
# Check if the responses are empty or if there are errors
if [ -z "$retroarch_response" ]; then
log e "Error connecting to the RetroArch Netplay server."
fi
# Parse the JSON response using jq
rooms=$(echo "$response" | jq -r '.[] | .fields | [.country, .username, .game_name, .core_name, .has_password, .retroarch_version, .created, .game_crc, .ip, .port] | @tsv')
if [ -z "$pretendo_response" ]; then
log e "Error connecting to the Pretendo Netplay server."
fi
if [ -z "$pcsx2_response" ]; then
log e "Error connecting to the PCSX2 Netplay server."
fi
if [ -z "$rpcs3_response" ]; then
log e "Error connecting to the RPCS3 Netplay server."
fi
if [ -z "$dolphin_response" ]; then
log e "Error connecting to the Dolphin Netplay server."
fi
# Parse the JSON responses using jq
retroarch_rooms=$(echo "$retroarch_response" | jq -r '.[] | .fields | [.country, .username, .game_name, .core_name, .has_password, .retroarch_version, .created, .game_crc, .ip, .port] | @tsv')
pretendo_rooms=$(echo "$pretendo_response" | jq -r '.rooms[] | [.game, .host, .status] | @tsv')
pcsx2_rooms=$(echo "$pcsx2_response" | jq -r '.rooms[] | [.game, .host, .status] | @tsv')
rpcs3_rooms=$(echo "$rpcs3_response" | jq -r '.rooms[] | [.game, .host, .status] | @tsv')
dolphin_rooms=$(echo "$dolphin_response" | jq -r '.rooms[] | [.game, .host, .status] | @tsv')
# Initialize the results for the Zenity table
results=()
room_details=()
# Process each room
# Process each room from RetroArch
while IFS=$'\t' read -r country username game_name core_name has_password retroarch_version created game_crc ip port; do
# Convert boolean to human-readable format
if [ "$has_password" = "true" ]; then
@ -281,21 +61,45 @@ populate_table() {
else
has_password="No"
fi
# Get the flag emoji for the country
flag="${country_flags[$country]}"
# Add the extracted data to the results array
results+=("$flag" "$username" "$game_name" "$core_name" "$has_password" "$retroarch_version" "$created")
room_details+=("$country,$username,$game_name,$core_name,$has_password,$retroarch_version,$created,$game_crc,$ip,$port")
done <<< "$rooms"
done <<< "$retroarch_rooms"
# Process each room from Pretendo
while IFS=$'\t' read -r game host status; do
results+=("Pretendo" "$host" "$game" "Pretendo" "$status" "N/A" "N/A")
room_details+=("Pretendo,$host,$game,Pretendo,$status,N/A,N/A,N/A,N/A,N/A")
done <<< "$pretendo_rooms"
# Process each room from PCSX2
while IFS=$'\t' read -r game host status; do
results+=("PCSX2" "$host" "$game" "PCSX2" "$status" "N/A" "N/A")
room_details+=("PCSX2,$host,$game,PCSX2,$status,N/A,N/A,N/A,N/A,N/A")
done <<< "$pcsx2_rooms"
# Process each room from RPCS3
while IFS=$'\t' read -r game host status; do
results+=("RPCS3" "$host" "$game" "RPCS3" "$status" "N/A" "N/A")
room_details+=("RPCS3,$host,$game,RPCS3,$status,N/A,N/A,N/A,N/A,N/A")
done <<< "$rpcs3_rooms"
# Process each room from Dolphin
while IFS=$'\t' read -r game host status; do
results+=("Dolphin" "$host" "$game" "Dolphin" "$status" "N/A" "N/A")
room_details+=("Dolphin,$host,$game,Dolphin,$status,N/A,N/A,N/A,N/A,N/A")
done <<< "$dolphin_rooms"
# Check if results array is populated
if [ ${#results[@]} -eq 0 ]; then
zenity --info --title="Netplay Results" --text="No valid rooms found."
exit 0
fi
# Display the results using Zenity in a table and get the selected row
selected=$(zenity --list --width="1280" --height="800" \
--title="Available Netplay Rooms" \
@ -308,47 +112,40 @@ populate_table() {
--column="Created" \
"${results[@]}" \
--print-column=ALL)
echo "$selected"
}
# Call the function to populate the table
selected_room=$(populate_table)
# Parse the selected room details
IFS=',' read -r selected_country selected_username selected_game_name selected_core_name selected_has_password selected_version selected_created selected_game_crc selected_ip selected_port <<< "$selected_room"
# Implement the logic for launching the selected emulator with the selected game and netplay room details
start_game() {
selected="$1"
room_details=("${@:2}")
# Check if the user selected a row
if [ -z "$selected" ]; then
exit 0
fi
# Extract the details of the selected room
selected_flag=$(echo "$selected" | awk -F'|' '{print $1}')
selected_username=$(echo "$selected" | awk -F'|' '{print $2}')
selected_game_name=$(echo "$selected" | awk -F'|' '{print $3}')
selected_core_name=$(echo "$selected" | awk -F'|' '{print $4}')
selected_has_password=$(echo "$selected" | awk -F'|' '{print $5}')
selected_version=$(echo "$selected" | awk -F'|' '{print $6}')
raw_dates=$(echo "$selected" | awk -F'|' '{print $7}')
# Convert ISO 8601 format to human-readable format
selected_created=$(date -d "$raw_dates" +"%Y-%m-%d %H:%M:%S")
# Find the matching room details
for room in "${room_details[@]}"; do
IFS=',' read -r country username game_name core_name has_password retroarch_version created game_crc ip port <<< "$room"
flag="${country_flags[$country]}"
if [ "$flag" = "$selected_flag" ] && [ "$username" = "$selected_username" ] && [ "$game_name" = "$selected_game_name" ] && [ "$core_name" = "$selected_core_name" ] && [ "$has_password" = "$selected_has_password" ] && [ "$retroarch_version" = "$selected_version" ] && [ "$created" = "$selected_created" ]; then
# Get the details of the selected room
room_details=("$@")
selected_game_crc=""
selected_ip=""
selected_port=""
for detail in "${room_details[@]}"; do
IFS=',' read -r country username game_name core_name has_password version created game_crc ip port <<< "$detail"
if [ "$country" = "$selected_country" ] && [ "$username" = "$selected_username" ] && [ "$game_name" = "$selected_game_name" ] && [ "$core_name" = "$selected_core_name" ] && [ "$has_password" = "$selected_has_password" ] && [ "$version" = "$selected_version" ] && [ "$created" = "$selected_created" ]; then
selected_game_crc="$game_crc"
selected_ip="$ip"
selected_port="$port"
break
fi
done
# Find the game ROM by name and then verify CRC
found_rom=""
candidates=($(find "$roms_folder" -type f -iname "*$(echo "$selected_game_name" | sed 's/[^a-zA-Z0-9]//g')*"))
for rom in "${candidates[@]}"; do
# Check the CRC of the ROM
rom_crc=$(crc32 "$rom")
@ -357,35 +154,54 @@ start_game() {
break
fi
done
# Check if the ROM was found
if [ -z "$found_rom" ]; then
zenity --error --text="Game ROM not found or CRC mismatch."
exit 1
fi
# Warn the user if the CRC is different
if [ "$rom_crc" != "$selected_game_crc" ]; then
zenity --warning --text="CRC mismatch! The game may not work correctly."
fi
# If the room has a password, ask for it
if [ "$selected_has_password" = "Yes" ]; then
password=$(zenity --entry --title="Password Required" --text="Enter the password for the netplay room:")
if [ -z "$password" ]; then
zenity --error --text="Password required to join the room."
# Launch the appropriate emulator with the selected game and netplay room details
case "$selected_core_name" in
"RetroArch")
if [ "$selected_has_password" = "Yes" ]; then
password=$(zenity --entry --title="Password Required" --text="Enter the password for the netplay room:")
if [ -z "$password" ]; then
zenity --error --text="Password required to join the room."
exit 1
fi
retroarch -L "/app/share/libretro/cores/${selected_core_name}.so" "$found_rom" --connect "$selected_ip:$selected_port" --password "$password"
else
retroarch -L "/app/share/libretro/cores/${selected_core_name}.so" "$found_rom" --connect "$selected_ip:$selected_port"
fi
;;
"Pretendo")
# Pretendo-specific launch command (example, replace with actual)
pretendoclient --connect "$selected_ip:$selected_port" --game "$found_rom"
;;
"PCSX2")
# PCSX2-specific launch command (example, replace with actual)
pcsx2 --netplay --host "$selected_ip" --port "$selected_port" "$found_rom"
;;
"RPCS3")
# RPCS3-specific launch command (example, replace with actual)
rpcs3 --netplay --host "$selected_ip" --port "$selected_port" "$found_rom"
;;
"Dolphin")
# Dolphin-specific launch command (example, replace with actual)
dolphin-emu --netplay --host "$selected_ip" --port "$selected_port" "$found_rom"
;;
*)
zenity --error --text="Unsupported core: $selected_core_name"
exit 1
fi
# Launch RetroArch with the selected game and netplay room details, including password
retroarch -L "/app/share/libretro/cores/${selected_core_name}.so" "$found_rom" --connect "$selected_ip:$selected_port" --password "$password"
else
# Launch RetroArch without password
retroarch -L "/app/share/libretro/cores/${selected_core_name}.so" "$found_rom" --connect "$selected_ip:$selected_port"
fi
;;
esac
}
# Call the function to populate the table
selected_room=$(populate_table)
# Call the function to start the game
start_game "$selected_room" "${room_details[@]}"