itchio-opengamepadui-plugin/plugin.gd
Jose Falanga d09bd2ae79
Some checks failed
Build plugin / build (push) Has been cancelled
Replace cookie HTTP verification with butlerd's Profile.List + UseSavedLogin
Butlerd stores credentials in its SQLite DB after the first password
login. On restart, Profile.List finds saved profiles and
Profile.UseSavedLogin refreshes them — no cookie persistence needed on
our side.
2026-08-17 02:19:22 -03:00

97 lines
3.4 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")
var itch: ItchClient
var api_key := settings_manager.get_value("plugin.itch", "api_key", "") as String
var login_method := settings_manager.get_value("plugin.itch", "login_method", 0) as int
var username := settings_manager.get_value("plugin.itch", "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_client_start)
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_client_start() -> 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 == 0:
# 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()