#!/usr/bin/env bash # --------------------------------------------------------------------------- # Agrega el gateway gw.nynusoft.com al prefijo de Wine de StarCraft. # # Uso: ./add-gateway.sh (busca tu prefijo solo) | ./add-gateway.sh ~/.wine-mio (le dices cual usar) | WINEPREFIX=~/.wine ./add-gateway.sh # # Igual que el add-gateway.cmd de Windows: agrega la entrada y CONSERVA los servidores que ya tengas, con copia de seguridad de user.reg. Corre esto con el juego cerrado. # --------------------------------------------------------------------------- set -u ADDRESS='gw.nynusoft.com' TIMEZONE='0' TITLE='gw.nynusoft.com' VALUE='Battle.net Gateways' KEY_MAIN='Software\Battle.net\Configuration' KEY_BLIZZARD='Software\Blizzard Entertainment\Battle.net\Configuration' REG_MAIN='Software\\Battle.net\\Configuration' REG_BLIZZARD='Software\\Blizzard Entertainment\\Battle.net\\Configuration' die() { printf '%s\n' "$1" >&2 exit 1 } # --- Wine prefix ----------------------------------------------------------- command -v wine >/dev/null 2>&1 || die "No encuentro 'wine' en el PATH." PREFIX='' for candidate in "${1:-}" "${WINEPREFIX:-}" "$HOME/.wine-starcraft" "$HOME/.wine"; do if [ -n "$candidate" ] && [ -f "$candidate/user.reg" ]; then PREFIX="$candidate" break fi done [ -n "$PREFIX" ] || die "No encontre un prefijo de Wine (hace falta user.reg). Pasame la ruta: ./add-gateway.sh /ruta/del/prefijo" export WINEPREFIX="$PREFIX" printf '%s %s\n' "Prefijo:" "$PREFIX" # --- back the registry up -------------------------------------------------- BACKUP="$PREFIX/user.reg.bak-$(date +%Y%m%d-%H%M%S)" cp -p "$PREFIX/user.reg" "$BACKUP" || die "No pude copiar user.reg; no toco nada." printf '%s %s\n' "Copia de seguridad:" "$BACKUP" # --- read the current list ------------------------------------------------- # # With ``BNETGW_ONLY_OURS=1`` the list is rewritten from scratch, with only # this server: the player's own list is safe in the backup taken above. The # installer uses it so nobody ends up with servers they do not run (dead # Blizzard gateways, or somebody else's abandoned project). ONLY_OURS="${BNETGW_ONLY_OURS:-0}" # ``wine reg query`` prints a REG_MULTI_SZ with \0 separators, so the list can # be split and put back together as is. read_list() { wine reg query "HKCU\\$1" /v "$VALUE" 2>/dev/null | tr -d '\r' | awk ' /REG_MULTI_SZ/ { sub(/^.*REG_MULTI_SZ[[:space:]]*/, ""); print; exit }' } split_list() { printf '%s' "$1" | sed 's/\\0/\n/g' } list='' if [ "$ONLY_OURS" != "1" ]; then list=$(read_list "$KEY_MAIN") [ -n "$list" ] || list=$(read_list "$KEY_BLIZZARD") fi TOKENS=() if [ -n "$list" ]; then # ``|| [ -n "$token" ]``: read returns 1 on a last line without a newline. while IFS= read -r token || [ -n "$token" ]; do TOKENS+=("$token") done < <(split_list "$list") fi # A valid list is ``1001``, a two digit count and exactly that many # address/timezone/name triples. Anything else (a value a broken ``wine reg # import`` left behind) is discarded: keeping garbage would leave the player # with a broken gateway selector. valid=0 if [ "${#TOKENS[@]}" -ge 2 ] && [ "${TOKENS[0]}" = "1001" ]; then case "${TOKENS[1]}" in [0-9][0-9]) [ "${#TOKENS[@]}" -eq $((2 + 10#${TOKENS[1]} * 3)) ] && valid=1 ;; esac fi [ "$valid" -eq 1 ] || TOKENS=() # --- new list: what you already had (minus this gateway) and ours at the end NEW=() if [ "${#TOKENS[@]}" -gt 2 ]; then index=2 while [ "$index" -lt "${#TOKENS[@]}" ]; do address="${TOKENS[$index]}" lower=$(printf '%s' "$address" | tr '[:upper:]' '[:lower:]') mine=$(printf '%s' "$ADDRESS" | tr '[:upper:]' '[:lower:]') if [ "$lower" != "$mine" ]; then NEW+=("$address" "${TOKENS[$((index + 1))]:-0}" "${TOKENS[$((index + 2))]:-$address}") fi index=$((index + 3)) done else if [ "$ONLY_OURS" = "1" ]; then printf '%s\n' "(lista reiniciada: queda solo el nuestro; la tuya está en la copia de seguridad)" else printf '%s\n' "(no había lista previa: se escribió solo el nuestro)" fi NEW=() fi NEW+=("$ADDRESS" "$TIMEZONE" "$TITLE") # ``1001`` plus the two digit number of the gateway the client pre-selects; # ours goes last, so that index is also the number of gateways. COUNT=$(printf '%02d' "$(( ${#NEW[@]} / 3 ))") # --- write where the game looks for it ------------------------------------- # Wine's own writers are unreliable for REG_MULTI_SZ on old builds: ``reg add`` # stores the literal backslash-zero text and ``reg import`` corrupts a hex(7) # payload (both verified on openSUSE). What the game actually reads is the # plain-text ``user.reg`` next to the prefix, so we stop Wine, rewrite that one # value there and let the next launch pick the new list up. to_utf16le_hex() { # $1 = string -> comma separated hex of its UTF-16LE bytes (ASCII safe). printf '%s' "$1" | od -An -tx1 -v | tr -d ' \n' | sed 's/\(..\)/\1,00,/g' | sed 's/,$//' } hex_multisz() { # $@ = strings -> the whole REG_MULTI_SZ payload as comma separated hex. local s out="" for s in "$@"; do out="${out}$(to_utf16le_hex "$s"),00,00," done out="${out}00,00" printf '%s' "$out" } set_reg_value() { # $1 = section (double backslashes), $2 = hex(7) payload. Replaces that one # value inside user.reg and leaves the rest of the file untouched. local reg="$PREFIX/user.reg" section="$1" hexdata="$2" [ -f "$reg" ] || return 1 SECTION="[$section]" SECTION_NEW="[$section] $(date +%s)" \ VALUE_LINE="\"$VALUE\"=hex(7):$hexdata" \ awk ' BEGIN { sec = ENVIRON["SECTION"]; secnew = ENVIRON["SECTION_NEW"] val = ENVIRON["VALUE_LINE"] } { if (substr($0, 1, 1) == "[") { if (in_sec && !replaced) print val in_sec = (index($0, sec) == 1) if (in_sec) found = 1 replaced = 0 print next } if (in_sec && !replaced && index($0, "\"Battle.net Gateways\"=") == 1) { print val replaced = 1 next } print } END { if (in_sec && !replaced) print val if (!found) { print secnew; print val } } ' "$reg" > "$reg.tmp" || return 1 mv "$reg.tmp" "$reg" || return 1 } DATA_HEX=$(hex_multisz "1001" "$COUNT" "${NEW[@]}") # Stop Wine first: a process still holding the old registry would write it back # over our edit when it exits (run this with StarCraft closed). WINEPREFIX="$PREFIX" wineserver -k 2>/dev/null || true set_reg_value "$REG_MAIN" "$DATA_HEX" || die "No pude escribir user.reg; revisa permisos." set_reg_value "$REG_BLIZZARD" "$DATA_HEX" || true # --- read it back to prove it stuck ---------------------------------------- check=$(read_list "$KEY_MAIN") case "$check" in *"$ADDRESS"*) ;; *) die "El registro no quedo como esperaba; revisalo en user.reg o con regedit." ;; esac printf '\n%s\n' "Gateways despues del cambio:" FINAL=() while IFS= read -r token || [ -n "$token" ]; do FINAL+=("$token") done < <(split_list "$check") index=2 while [ "$index" -lt "${#FINAL[@]}" ]; do printf ' - %-30s %s\n' "${FINAL[$index]}" "${FINAL[$((index + 2))]:-}" index=$((index + 3)) done printf '\n%s\n' "Listo." printf '%s\n' "Abre StarCraft y elige gw.nynusoft.com en la lista de gateways."