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")
|
|
|
|
|
|
|
|
|
|
var itch: ItchClient
|
|
|
|
|
var api_key := settings_manager.get_value("plugin.itch", "api_key", "") as String
|
2026-08-17 02:00:39 -03:00
|
|
|
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
|
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_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:
|
2026-08-17 02:19:22 -03:00
|
|
|
# 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
|
2026-08-17 02:00:39 -03:00
|
|
|
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:
|
2026-08-17 02:19:22 -03:00
|
|
|
# Username/password login
|
2026-08-17 02:00:39 -03:00
|
|
|
if username == "":
|
|
|
|
|
var notify := Notification.new("itch.io username required")
|
|
|
|
|
notify.icon = icon
|
|
|
|
|
logger.info(notify.text)
|
|
|
|
|
notification_manager.show(notify)
|
|
|
|
|
return
|
2026-08-17 02:19:22 -03:00
|
|
|
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:
|
2026-08-17 02:04:00 -03:00
|
|
|
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
|
|
|
|
|
|
2026-08-17 02:04:00 -03:00
|
|
|
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()
|