itchio-opengamepadui-plugin/plugin.gd
Jose Falanga 97c1519833 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
2026-08-19 16:41:31 -03:00

100 lines
3.5 KiB
GDScript

extends Plugin
## itch.io plugin entrypoint
##
## Boots the ItchClient (which spawns and speaks to butlerd, itch's official
## JSON-RPC launcher daemon) and registers the Library implementation so
## installed itch.io games show up alongside everything else in the user's
## OpenGamepadUI library.
const ItchClient := preload("res://plugins/itch/core/itch_client.gd")
var settings_manager := load("res://core/global/settings_manager.tres") as SettingsManager
var notification_manager := load("res://core/global/notification_manager.tres") as NotificationManager
var settings_menu := load("res://plugins/itch/core/itch_settings.tscn") as PackedScene
var icon := preload("res://plugins/itch/assets/itch.svg")
const SETTINGS_SECTION := "plugin.itch"
enum LOGIN_METHOD { API_KEY = 0, PASSWORD = 1 }
var itch: ItchClient
var api_key := settings_manager.get_value(SETTINGS_SECTION, "api_key", "") as String
var login_method := settings_manager.get_value(SETTINGS_SECTION, "login_method", 0) as int
var username := settings_manager.get_value(SETTINGS_SECTION, "username", "") as String
func _ready() -> void:
logger = Log.get_logger("Itch", Log.LEVEL.INFO)
# Load the itch client. This spawns butlerd in the background and
# manages the JSON-RPC connection to it.
itch = load("res://plugins/itch/core/itch_client.tscn").instantiate()
itch.bootstrap_finished.connect(_on_bootstrap_finished)
itch.client_ready.connect(_on_client_ready)
itch.logged_in.connect(_on_client_logged_in)
add_child(itch)
# Load the Library implementation
var library: Library = load("res://plugins/itch/core/library_itch.tscn").instantiate()
add_child(library)
# Triggers when butlerd has been spawned
func _on_bootstrap_finished() -> void:
if not itch.client_started:
var notify := Notification.new("Unable to start the itch.io (butlerd) client")
notify.icon = icon
logger.error(notify.text)
notification_manager.show(notify)
return
# Triggers when butlerd has completed its handshake and is ready for calls
func _on_client_ready() -> void:
# Always try butlerd's stored credentials first — this handles password
# logins from previous runs without any stored tokens on our side.
if await itch.try_saved_login():
return
if login_method == LOGIN_METHOD.API_KEY:
# API key login
if api_key == "":
var notify := Notification.new("itch.io API key required")
notify.icon = icon
logger.info(notify.text)
notification_manager.show(notify)
return
itch.login_with_api_key(api_key)
else:
# Username/password login
if username == "":
var notify := Notification.new("itch.io username required")
notify.icon = icon
logger.info(notify.text)
notification_manager.show(notify)
return
var notify := Notification.new("itch.io: open plugin settings to log in")
notify.icon = icon
logger.info(notify.text)
notification_manager.show(notify)
# Triggers when the itch client finishes a login attempt
func _on_client_logged_in(status: ItchClient.LOGIN_STATUS, profile: Dictionary) -> void:
var notify := Notification.new("")
notify.icon = icon
if status == ItchClient.LOGIN_STATUS.OK:
var uname: String = profile.get("user", {}).get("username", "")
notify.text = "Successfully logged in to itch.io as " + uname
logger.info(notify.text)
notification_manager.show(notify)
return
notify.text = "Failed to log in to itch.io. Check your credentials in plugin settings."
logger.warn(notify.text)
notification_manager.show(notify)
# Return the settings menu scene
func get_settings_menu() -> Control:
return settings_menu.instantiate()