readability: extract magic constants, fix Go jargon

- goos/goarch -> os_name/arch_name (Go ecosystem jargon)
- BUTLER_USER_AGENT now reads version from plugin.json at runtime
- Added named constants: CONNECTION_TIMEOUT_ITERATIONS,
  CONNECTION_POLL_DELAY_MS, PERCENTAGE_SCALE, LAUNCH_WATCH_INTERVAL_SEC,
  UPDATE_CHECK_INTERVAL_SEC, BUTLERD_BOOT_MAX_ATTEMPTS,
  BUTLERD_BOOT_RETRY_DELAY_SEC, LAUNCH_WATCH_TIMEOUT_MS,
  EXECUTE_PERMISSION_BIT
- Removed BYTES_PER_MB (inline 1024 * 1024 is clearer)
This commit is contained in:
Jose Falanga 2026-08-19 16:20:20 -03:00
parent 18f674c00b
commit 7ee08ac571
2 changed files with 38 additions and 18 deletions

View file

@ -29,6 +29,9 @@ const butler_dir := "user://butler"
## survive plugin updates untouched.
const games_dir := "user://butler/games"
const CACHE_DIR := "itch"
const CONNECTION_TIMEOUT_ITERATIONS := 50
const CONNECTION_POLL_DELAY_MS := 20
const PERCENTAGE_SCALE := 100
enum STATE {
BOOT,
@ -101,7 +104,7 @@ func bootstrap() -> void:
"--keep-alive",
"--dbpath", dbpath,
"--address", "https://itch.io",
"--user-agent", "OpenGamepadUI-itch/0.1.0",
"--user-agent", _get_user_agent(),
"--destiny-pid", str(OS.get_process_id()),
]
@ -138,21 +141,21 @@ func _migrate_butler(dest_dir: String) -> void:
## Downloads a butler binary for this platform from itch's broth distribution
## channel (the same one the official itch.io app uses to self-update).
func _install_butler() -> bool:
var goos := "linux"
var goarch := "amd64"
var os_name := "linux"
var arch_name := "amd64"
if OS.get_name() == "Windows":
goos = "windows"
os_name = "windows"
if OS.get_name() == "macOS":
goos = "darwin"
os_name = "darwin"
# Engine.get_architecture_name() is only available on newer Godot 4.x
# builds; guard it so this still works if OpenGamepadUI is running on an
# older engine version.
if Engine.has_method("get_architecture_name"):
var arch: String = Engine.get_architecture_name()
if "arm64" in arch or "aarch64" in arch:
goarch = "arm64"
arch_name = "arm64"
var platform_slug := goos + "-" + goarch
var platform_slug := os_name + "-" + arch_name
var latest_url := "/".join([broth_base, platform_slug, "LATEST"])
var http := HTTPRequest.new()
@ -204,6 +207,17 @@ func _install_butler() -> bool:
return true
func _get_user_agent() -> String:
var version := "unknown"
var file := FileAccess.open("res://plugin.json", FileAccess.READ)
if file:
var json: Variant = JSON.parse_string(file.get_as_text())
file.close()
if json is Dictionary:
version = json.get("plugin.version", "unknown")
return "OpenGamepadUI-itch/" + version
# ---------------------------------------------------------------------------
# Wire protocol: spawn -> read handshake off stdout -> connect TCP -> auth
# ---------------------------------------------------------------------------
@ -266,10 +280,10 @@ func _connect_and_authenticate(address: String, secret: String) -> void:
return
# Wait for the connection to establish
var timeout := 50
var timeout := CONNECTION_TIMEOUT_ITERATIONS
while socket.get_status() == StreamPeerTCP.STATUS_CONNECTING and timeout > 0:
socket.poll()
OS.delay_msec(20)
OS.delay_msec(CONNECTION_POLL_DELAY_MS)
timeout -= 1
if socket.get_status() != StreamPeerTCP.STATUS_CONNECTED:
logger.error("Timed out connecting to butlerd")
@ -790,8 +804,8 @@ func _install(game: Dictionary, cave_id: String, options: Dictionary) -> bool:
var progress: float = p.get("progress", 0.0)
install_progressed.emit.call_deferred(
cave_id if cave_id != "" else str(game_id),
int(progress * 100),
100
int(progress * PERCENTAGE_SCALE),
PERCENTAGE_SCALE
)
rpc_notification.connect(on_notification)