itchio-opengamepadui-plugin/plugin.gd

101 lines
3.5 KiB
GDScript3
Raw Normal View History

2026-08-05 17:27:43 -03:00
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 }
2026-08-05 17:27:43 -03:00
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
2026-08-05 17:27:43 -03:00
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)
2026-08-05 17:27:43 -03:00
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:
2026-08-05 17:27:43 -03:00
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)
2026-08-05 17:27:43 -03:00
# 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
2026-08-05 17:27:43 -03:00
logger.info(notify.text)
notification_manager.show(notify)
return
notify.text = "Failed to log in to itch.io. Check your credentials in plugin settings."
2026-08-05 17:27:43 -03:00
logger.warn(notify.text)
notification_manager.show(notify)
# Return the settings menu scene
func get_settings_menu() -> Control:
return settings_menu.instantiate()