readability: rename constants, variables, extract enums across plugins
- itch: UPPER_CASE constants, CACHE_NAMESPACE, butler_process/rpc_socket/connection_state, LOGIN_METHOD enum, SETTINGS_SECTION, signal handler renames - artprovider: UPPER_CASE constants, _enriched_meta, _find_game_dict, dead code removal, named poll intervals - session-switch: MAX_POLL_FRAMES, _poll_for_power_menu, BUTTON_LABEL const
This commit is contained in:
parent
7ee08ac571
commit
97c1519833
4 changed files with 61 additions and 56 deletions
|
|
@ -15,11 +15,11 @@ extends NodeThread
|
|||
## https://pkg.go.dev/github.com/itchio/butler/butlerd). If itch.io ships a
|
||||
## butler update that changes a field, diff against that spec.
|
||||
|
||||
const broth_base := "https://broth.itch.zone/butler"
|
||||
const BROTH_BASE := "https://broth.itch.zone/butler"
|
||||
## Persistent storage for the downloaded butler binary + butler.db. Kept OUT
|
||||
## of user://plugins/<id>/ because OGPU wipes that directory (and everything
|
||||
## inside it) whenever the plugin is updated or re-extracted.
|
||||
const butler_dir := "user://butler"
|
||||
const BUTLER_DIR := "user://butler"
|
||||
## Where installed games live. This is intentionally NOT user://plugins/itch:
|
||||
## OGPU moves the entire extracted plugin directory (plugins/<id>/) to the
|
||||
## trash on every plugin update, so any install kept under it silently loses
|
||||
|
|
@ -27,8 +27,8 @@ const butler_dir := "user://butler"
|
|||
## used to break both fresh installs ("That upload is already installed!")
|
||||
## and launches. Keeping games next to butler.db (user://butler) means they
|
||||
## survive plugin updates untouched.
|
||||
const games_dir := "user://butler/games"
|
||||
const CACHE_DIR := "itch"
|
||||
const GAMES_DIR := "user://butler/games"
|
||||
const CACHE_NAMESPACE := "itch"
|
||||
const CONNECTION_TIMEOUT_ITERATIONS := 50
|
||||
const CONNECTION_POLL_DELAY_MS := 20
|
||||
const PERCENTAGE_SCALE := 100
|
||||
|
|
@ -60,9 +60,9 @@ signal app_updated(cave_id: String, success: bool)
|
|||
signal app_uninstalled(cave_id: String, success: bool)
|
||||
signal launch_exited(cave_id: String)
|
||||
|
||||
var proc: InteractiveProcess
|
||||
var socket: StreamPeerTCP
|
||||
var state: STATE = STATE.BOOT
|
||||
var butler_process: InteractiveProcess
|
||||
var rpc_socket: StreamPeerTCP
|
||||
var connection_state: STATE = STATE.BOOT
|
||||
var client_started := false
|
||||
var is_logged_in := false
|
||||
var profile: Dictionary = {}
|
||||
|
|
@ -70,7 +70,7 @@ var profile: Dictionary = {}
|
|||
var _proc_buffer := ""
|
||||
var _recv_buffer := ""
|
||||
var _next_id := 1
|
||||
var _install_location_id := ""
|
||||
var _cached_install_location_id := ""
|
||||
|
||||
var logger := Log.get_logger("ItchClient", Log.LEVEL.INFO)
|
||||
|
||||
|
|
@ -84,7 +84,7 @@ func _ready() -> void:
|
|||
|
||||
## Bootstraps the butler binary if it isn't present, then spawns butlerd.
|
||||
func bootstrap() -> void:
|
||||
var butler_dir_global := ProjectSettings.globalize_path(butler_dir)
|
||||
var butler_dir_global := ProjectSettings.globalize_path(BUTLER_DIR)
|
||||
_migrate_butler(butler_dir_global)
|
||||
var butler_bin := "/".join([butler_dir_global, "butler"])
|
||||
if not FileAccess.file_exists(butler_bin):
|
||||
|
|
@ -108,12 +108,12 @@ func bootstrap() -> void:
|
|||
"--destiny-pid", str(OS.get_process_id()),
|
||||
]
|
||||
|
||||
proc = InteractiveProcess.new(butler_bin, args)
|
||||
if proc.start() != OK:
|
||||
butler_process = InteractiveProcess.new(butler_bin, args)
|
||||
if butler_process.start() != OK:
|
||||
logger.error("Unable to spawn butlerd")
|
||||
return
|
||||
client_started = true
|
||||
state = STATE.WAITING_HANDSHAKE
|
||||
connection_state = STATE.WAITING_HANDSHAKE
|
||||
bootstrap_finished.emit()
|
||||
|
||||
|
||||
|
|
@ -156,7 +156,7 @@ func _install_butler() -> bool:
|
|||
arch_name = "arm64"
|
||||
|
||||
var platform_slug := os_name + "-" + arch_name
|
||||
var latest_url := "/".join([broth_base, platform_slug, "LATEST"])
|
||||
var latest_url := "/".join([BROTH_BASE, platform_slug, "LATEST"])
|
||||
|
||||
var http := HTTPRequest.new()
|
||||
add_child.call_deferred(http)
|
||||
|
|
@ -177,7 +177,7 @@ func _install_butler() -> bool:
|
|||
return false
|
||||
var version := body.get_string_from_utf8().strip_edges()
|
||||
|
||||
var archive_url := "/".join([broth_base, platform_slug, version, "archive", "default"])
|
||||
var archive_url := "/".join([BROTH_BASE, platform_slug, version, "archive", "default"])
|
||||
if http.request(archive_url) != OK:
|
||||
logger.error("Error downloading butler: " + archive_url)
|
||||
remove_child(http)
|
||||
|
|
@ -193,7 +193,7 @@ func _install_butler() -> bool:
|
|||
logger.error("butler couldn't be downloaded: " + archive_url)
|
||||
return false
|
||||
|
||||
var globalized_dir := ProjectSettings.globalize_path(butler_dir)
|
||||
var globalized_dir := ProjectSettings.globalize_path(BUTLER_DIR)
|
||||
DirAccess.make_dir_recursive_absolute(globalized_dir)
|
||||
var zip_path := "/tmp/butler-" + version + ".zip"
|
||||
var file := FileAccess.open(zip_path, FileAccess.WRITE_READ)
|
||||
|
|
@ -223,8 +223,8 @@ func _get_user_agent() -> String:
|
|||
# ---------------------------------------------------------------------------
|
||||
|
||||
func _thread_process(_delta: float) -> void:
|
||||
if state == STATE.WAITING_HANDSHAKE and proc:
|
||||
_proc_buffer += proc.read()
|
||||
if connection_state == STATE.WAITING_HANDSHAKE and butler_process:
|
||||
_proc_buffer += butler_process.read()
|
||||
if not _proc_buffer.contains("\n"):
|
||||
return
|
||||
var lines := _proc_buffer.split("\n")
|
||||
|
|
@ -244,12 +244,12 @@ func _thread_process(_delta: float) -> void:
|
|||
_connect_and_authenticate.call_deferred(address, secret)
|
||||
return
|
||||
|
||||
if state == STATE.CONNECTED and socket:
|
||||
socket.poll()
|
||||
var available := socket.get_available_bytes()
|
||||
if connection_state == STATE.CONNECTED and rpc_socket:
|
||||
rpc_socket.poll()
|
||||
var available := rpc_socket.get_available_bytes()
|
||||
if available <= 0:
|
||||
return
|
||||
var chunk := socket.get_partial_data(available)
|
||||
var chunk := rpc_socket.get_partial_data(available)
|
||||
if chunk[0] != OK:
|
||||
return
|
||||
_recv_buffer += (chunk[1] as PackedByteArray).get_string_from_utf8()
|
||||
|
|
@ -274,22 +274,22 @@ func _connect_and_authenticate(address: String, secret: String) -> void:
|
|||
var host: String = parts[0]
|
||||
var port: int = int(parts[1])
|
||||
|
||||
socket = StreamPeerTCP.new()
|
||||
if socket.connect_to_host(host, port) != OK:
|
||||
rpc_socket = StreamPeerTCP.new()
|
||||
if rpc_socket.connect_to_host(host, port) != OK:
|
||||
logger.error("Unable to connect to butlerd at " + address)
|
||||
return
|
||||
|
||||
# Wait for the connection to establish
|
||||
var timeout := CONNECTION_TIMEOUT_ITERATIONS
|
||||
while socket.get_status() == StreamPeerTCP.STATUS_CONNECTING and timeout > 0:
|
||||
socket.poll()
|
||||
while rpc_socket.get_status() == StreamPeerTCP.STATUS_CONNECTING and timeout > 0:
|
||||
rpc_socket.poll()
|
||||
OS.delay_msec(CONNECTION_POLL_DELAY_MS)
|
||||
timeout -= 1
|
||||
if socket.get_status() != StreamPeerTCP.STATUS_CONNECTED:
|
||||
if rpc_socket.get_status() != StreamPeerTCP.STATUS_CONNECTED:
|
||||
logger.error("Timed out connecting to butlerd")
|
||||
return
|
||||
|
||||
state = STATE.CONNECTED
|
||||
connection_state = STATE.CONNECTED
|
||||
|
||||
var res := await _rpc_call("Meta.Authenticate", {"secret": secret})
|
||||
if "error" in res:
|
||||
|
|
@ -305,9 +305,9 @@ func _connect_and_authenticate(address: String, secret: String) -> void:
|
|||
# ---------------------------------------------------------------------------
|
||||
|
||||
func _send_line(text: String) -> void:
|
||||
if not socket:
|
||||
if not rpc_socket:
|
||||
return
|
||||
socket.put_data((text + "\n").to_utf8_buffer())
|
||||
rpc_socket.put_data((text + "\n").to_utf8_buffer())
|
||||
|
||||
|
||||
## Godot's JSON.parse decodes every JSON number as a float (so 7670 becomes
|
||||
|
|
@ -589,17 +589,17 @@ func _get_caves() -> Variant:
|
|||
## every plugin update; existing locations pointing there are left alone (their
|
||||
## orphaned caves are cleaned up by [method _resolve_cave]) but never reused.
|
||||
func _ensure_install_location() -> String:
|
||||
if _install_location_id != "":
|
||||
return _install_location_id
|
||||
if _cached_install_location_id != "":
|
||||
return _cached_install_location_id
|
||||
|
||||
var target_path := ProjectSettings.globalize_path(games_dir)
|
||||
var target_path := ProjectSettings.globalize_path(GAMES_DIR)
|
||||
var res := await _rpc_call("Install.Locations.List", {})
|
||||
var locations: Array = res.get("installLocations", [])
|
||||
for loc in locations:
|
||||
var location: Dictionary = loc
|
||||
if location.get("path", "") == target_path:
|
||||
_install_location_id = location.get("id", "")
|
||||
return _install_location_id
|
||||
_cached_install_location_id = location.get("id", "")
|
||||
return _cached_install_location_id
|
||||
|
||||
DirAccess.make_dir_recursive_absolute(target_path)
|
||||
# Let butlerd generate the id so we never collide with a stale location
|
||||
|
|
@ -608,8 +608,8 @@ func _ensure_install_location() -> String:
|
|||
if "error" in add_res:
|
||||
logger.error("Install.Locations.Add failed: " + str(add_res["error"]))
|
||||
return ""
|
||||
_install_location_id = add_res.get("installLocation", {}).get("id", "")
|
||||
return _install_location_id
|
||||
_cached_install_location_id = add_res.get("installLocation", {}).get("id", "")
|
||||
return _cached_install_location_id
|
||||
|
||||
|
||||
## Returns every configured install location, each as:
|
||||
|
|
@ -629,7 +629,7 @@ func _get_install_locations() -> Array:
|
|||
logger.warn("Install.Locations.List failed: " + str(res["error"]))
|
||||
return []
|
||||
var locations: Array = res.get("installLocations", [])
|
||||
var preferred_path := ProjectSettings.globalize_path(games_dir)
|
||||
var preferred_path := ProjectSettings.globalize_path(GAMES_DIR)
|
||||
var plugin_dir := ProjectSettings.globalize_path("user://plugins/itch")
|
||||
var preferred: Array = []
|
||||
var others: Array = []
|
||||
|
|
@ -998,8 +998,8 @@ func _logout() -> void:
|
|||
|
||||
|
||||
func _exit_tree() -> void:
|
||||
if socket:
|
||||
socket.disconnect_from_host()
|
||||
if not proc:
|
||||
if rpc_socket:
|
||||
rpc_socket.disconnect_from_host()
|
||||
if not butler_process:
|
||||
return
|
||||
proc.stop()
|
||||
butler_process.stop()
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue