From b8e65edcefe3e737de7636ccca6748fd35c7edc0 Mon Sep 17 00:00:00 2001 From: Eroax Date: Mon, 15 Jan 2024 05:35:16 +0000 Subject: [PATCH] Implement a temporary system for saving Credentials. (#38) This handles adding the functionality for saving the Token and Channel for Twitch, as well as the Port and Password for OBS when handling connections. Co-authored-by: Eroax Reviewed-on: https://codeberg.org/Eroax/StreamGraph/pulls/38 --- addons/no_twitch/demo/token_saver.gd | 4 --- addons/no_twitch/twitch_connection.gd | 1 + classes/connections/connections.gd | 35 +++++++++++++++++++ classes/connections/creds_saver.gd | 4 +++ .../obs_websocket_setup_dialog.gd | 12 +++++++ graph_node_renderer/twitch_setup_dialog.gd | 27 ++++++++++++-- graph_node_renderer/twitch_setup_dialog.tscn | 17 +++++++-- 7 files changed, 90 insertions(+), 10 deletions(-) delete mode 100644 addons/no_twitch/demo/token_saver.gd create mode 100644 classes/connections/creds_saver.gd diff --git a/addons/no_twitch/demo/token_saver.gd b/addons/no_twitch/demo/token_saver.gd deleted file mode 100644 index b7e1d43..0000000 --- a/addons/no_twitch/demo/token_saver.gd +++ /dev/null @@ -1,4 +0,0 @@ -extends Resource -class_name TokenSaver - -@export var token : String diff --git a/addons/no_twitch/twitch_connection.gd b/addons/no_twitch/twitch_connection.gd index 1749705..437dfb0 100644 --- a/addons/no_twitch/twitch_connection.gd +++ b/addons/no_twitch/twitch_connection.gd @@ -46,6 +46,7 @@ func setup_chat_connection(default_chat : String = "", token : String = token, r if !default_chat.is_empty(): + Connections.save_credentials({"token" : token, "channel" : default_chat}, "twitch") chat_socket.join_chat(default_chat) diff --git a/classes/connections/connections.gd b/classes/connections/connections.gd index ddf7b18..e1d2606 100644 --- a/classes/connections/connections.gd +++ b/classes/connections/connections.gd @@ -10,3 +10,38 @@ static func _twitch_chat_received(msg_dict : Dictionary): DeckHolder.send_event(&"twitch_chat", msg_dict) + +## Temporary Function for loading generic Connection credentials from user:// as a [CredSaver] Resource +static func load_credentials(service : String): + + if !FileAccess.file_exists("user://" + service + "_creds.res"): + + DeckHolder.logger.log_system("No Credentials exist for " + service + " Service", Logger.LogType.WARN) + return null + + + var creds = ResourceLoader.load("user://" + service + "_creds.res") + + if !creds is CredSaver: + + DeckHolder.logger.log_system("Error loading Credentials for " + service + " Service", Logger.LogType.ERROR) + return null + + + return creds + + +## Temporary function for saving generic Connection credentials to user:// as a CredSaver +static func save_credentials(data : Dictionary, service: String): + + var creds := CredSaver.new() + + creds.data = data + + var err = ResourceSaver.save(creds, "user://" + service + "_creds.res") + + if err != OK: + + DeckHolder.logger.log_system("Error saving Credentials for " + service + " Service", Logger.LogType.ERROR) + + diff --git a/classes/connections/creds_saver.gd b/classes/connections/creds_saver.gd new file mode 100644 index 0000000..7bba6f8 --- /dev/null +++ b/classes/connections/creds_saver.gd @@ -0,0 +1,4 @@ +extends Resource +class_name CredSaver + +@export var data : Dictionary diff --git a/graph_node_renderer/obs_websocket_setup_dialog.gd b/graph_node_renderer/obs_websocket_setup_dialog.gd index 1121872..13ac783 100644 --- a/graph_node_renderer/obs_websocket_setup_dialog.gd +++ b/graph_node_renderer/obs_websocket_setup_dialog.gd @@ -21,6 +21,7 @@ var state: ConnectionState @onready var _old_password: String = password_line_edit.text + func get_port() -> int: return int(port_spin_box.value) @@ -39,13 +40,24 @@ func set_button_state(p_state: ConnectionState) -> void: ConnectionState.CONNECTING: connect_button.text = "Connecting..." ConnectionState.CONNECTED: + Connections.save_credentials({"port" : get_port(), "password" : get_password()}, "OBS") connect_button.text = "Disconnect" func _ready() -> void: + + var loaded_creds = Connections.load_credentials("OBS") + + if loaded_creds != null: + + password_line_edit.text = loaded_creds.data.password + port_spin_box.value = loaded_creds.data.port + connect_button.pressed.connect( func(): + connect_button_pressed.emit(state) + ) canceled.connect( diff --git a/graph_node_renderer/twitch_setup_dialog.gd b/graph_node_renderer/twitch_setup_dialog.gd index f10ef95..7091404 100644 --- a/graph_node_renderer/twitch_setup_dialog.gd +++ b/graph_node_renderer/twitch_setup_dialog.gd @@ -8,11 +8,29 @@ func _ready(): %Authenticate.pressed.connect(authenticate_twitch) %CopyURLButton.pressed.connect(copy_auth_link) - confirmed.connect(connect_to_chat) - + #confirmed.connect(connect_to_chat) + %Join_Chat.pressed.connect(connect_to_chat) func authenticate_twitch(): + # Temporary setup for loading credentials + var loaded_creds = Connections.load_credentials("twitch") + + if loaded_creds != null: + + Connections.twitch.token = loaded_creds.data.token + if loaded_creds.data.has("channel"): + %Default_Chat.text = loaded_creds.data.channel + connect_to_chat() + return + + + # Temporary setup for saving tokens. + Connections.twitch.token_received.connect(func(token): + + Connections.save_credentials({"token" : token}, "twitch") + ) + var url = Connections.twitch.authenticate_with_twitch(%Client_ID.text) OS.shell_open(url) @@ -24,5 +42,8 @@ func copy_auth_link(): func connect_to_chat(): - Connections.twitch.setup_chat_connection(%Default_Chat.text) + if Connections.twitch.chat_socket.get_ready_state() != WebSocketPeer.STATE_OPEN: + Connections.twitch.setup_chat_connection(%Default_Chat.text) + + Connections.twitch.join_channel(%Default_Chat.text) diff --git a/graph_node_renderer/twitch_setup_dialog.tscn b/graph_node_renderer/twitch_setup_dialog.tscn index 29945a3..eab28a9 100644 --- a/graph_node_renderer/twitch_setup_dialog.tscn +++ b/graph_node_renderer/twitch_setup_dialog.tscn @@ -35,7 +35,7 @@ unique_name_in_owner = true layout_mode = 2 size_flags_horizontal = 3 size_flags_stretch_ratio = 0.25 -text = "Authenticate" +text = "Connect" [node name="CopyURLButton" type="Button" parent="VBoxContainer/Authentication"] unique_name_in_owner = true @@ -50,7 +50,18 @@ disabled = true button_pressed = true text = "Extra Chat Info" -[node name="Default_Chat" type="LineEdit" parent="VBoxContainer"] +[node name="HBoxContainer" type="HBoxContainer" parent="VBoxContainer"] +layout_mode = 2 + +[node name="Default_Chat" type="LineEdit" parent="VBoxContainer/HBoxContainer"] unique_name_in_owner = true layout_mode = 2 -placeholder_text = "Default Chat Channel" +size_flags_horizontal = 3 +placeholder_text = "Default Channel/New Channel" + +[node name="Join_Chat" type="Button" parent="VBoxContainer/HBoxContainer"] +unique_name_in_owner = true +layout_mode = 2 +size_flags_horizontal = 3 +size_flags_stretch_ratio = 0.24 +text = "Join"