From a0de6c2918512fe420d3fc3621d14ffe52e039f1 Mon Sep 17 00:00:00 2001 From: lexonegit Date: Thu, 22 Aug 2024 15:33:12 +0300 Subject: [PATCH] Add joinChannelOnStart property --- .../Assets/Package/Runtime/IRC.cs | 49 ++++++++++++------- .../Runtime/TwitchConnection.ReadThread.cs | 7 ++- .../Package/Runtime/TwitchConnection.cs | 2 + 3 files changed, 38 insertions(+), 20 deletions(-) diff --git a/Unity-Twitch-Chat/Assets/Package/Runtime/IRC.cs b/Unity-Twitch-Chat/Assets/Package/Runtime/IRC.cs index f4fd5e5..7ad3560 100644 --- a/Unity-Twitch-Chat/Assets/Package/Runtime/IRC.cs +++ b/Unity-Twitch-Chat/Assets/Package/Runtime/IRC.cs @@ -29,11 +29,14 @@ public partial class IRC : MonoBehaviour [Header("General settings")] - [Tooltip("If true, duplicate instances will be destroyed. The first instance will be set to DontDestroyOnLoad.")] - [SerializeField] public bool singleton = true; - [Tooltip("If true, the client will connect to Twitch IRC on Start.")] - [SerializeField] private bool connectOnStart = true; + [SerializeField] private bool connectIRCOnStart = true; + + [Tooltip("If true, the client will automatically join the given channel upon a successful connection.")] + [SerializeField] public bool joinChannelOnStart = true; + + [Tooltip("If true, IRC will be set to DontDestroyOnLoad and any duplicate instances will be removed.")] + [SerializeField] public bool dontDestroyOnLoad = true; [Tooltip("If true, every IRC message sent and received will be logged to the console.")] [SerializeField] public bool showIRCDebug = true; @@ -86,7 +89,7 @@ private void Awake() { if (Instance) { - if (singleton) + if (dontDestroyOnLoad) { gameObject.SetActive(false); Destroy(gameObject); @@ -96,14 +99,14 @@ private void Awake() { Instance = this; - if (singleton) + if (dontDestroyOnLoad) DontDestroyOnLoad(gameObject); } } private void Start() { - if (connectOnStart) + if (connectIRCOnStart) Connect(); } @@ -114,7 +117,7 @@ private void Update() private void OnDestroy() { - if (singleton && Instance == this) + if (dontDestroyOnLoad && Instance == this) Instance = null; BlockingDisconnect(); @@ -212,11 +215,11 @@ public void Connect() oauth = oauth.Substring(6); } - if (channel.Length <= 0) - { - alertQueue.Enqueue(IRCReply.MISSING_LOGIN_INFO); - return; - } + // if (channel.Length <= 0) + // { + // alertQueue.Enqueue(IRCReply.MISSING_LOGIN_INFO); + // return; + // } StartCoroutine(StartConnection()); IEnumerator StartConnection() @@ -290,7 +293,7 @@ public void SendChatMessage(string message) { if (useAnonymousLogin) { - Debug.LogWarning("Chat messages cannot be sent with anonymous login"); + Debug.LogError("Chat messages cannot be sent with anonymous login"); return; } @@ -303,8 +306,13 @@ public void SendChatMessage(string message) /// The channel to join public void JoinChannel(string channel) { - if (channel != "") - connection.SendCommand("JOIN #" + channel.ToLower(), true); + if (channel == "") + { + Debug.LogError("Failed joining channel. Channel name is empty"); + return; + } + + connection.SendCommand("JOIN #" + channel.ToLower(), true); } /// @@ -313,8 +321,13 @@ public void JoinChannel(string channel) /// The channel to leave public void LeaveChannel(string channel) { - if (channel != "") - connection.SendCommand("PART #" + channel.ToLower(), true); + if (channel == "") + { + Debug.LogError("Failed leaving channel. Channel name is empty"); + return; + } + + connection.SendCommand("PART #" + channel.ToLower(), true); } } } \ No newline at end of file diff --git a/Unity-Twitch-Chat/Assets/Package/Runtime/TwitchConnection.ReadThread.cs b/Unity-Twitch-Chat/Assets/Package/Runtime/TwitchConnection.ReadThread.cs index b133c0b..468403c 100644 --- a/Unity-Twitch-Chat/Assets/Package/Runtime/TwitchConnection.ReadThread.cs +++ b/Unity-Twitch-Chat/Assets/Package/Runtime/TwitchConnection.ReadThread.cs @@ -44,7 +44,7 @@ private void ReadThreadLoop() // Decode the bytes to chars int charsDecoded = decoder.GetChars(readBuffer, 0, bytesReceived, chars, 0); - + for (int i = 0; i < charsDecoded; ++i) { // Character is a linebreak -> We have a complete line @@ -194,7 +194,10 @@ private void HandleRPL(string type) { case "001": alertQueue.Enqueue(IRCReply.CONNECTED_TO_SERVER); - SendCommand("JOIN #" + channel.ToLower(), true); + + if (joinChannelOnStart) + IRC.Instance.JoinChannel(channel); + break; case "353": alertQueue.Enqueue(IRCReply.JOINED_CHANNEL); diff --git a/Unity-Twitch-Chat/Assets/Package/Runtime/TwitchConnection.cs b/Unity-Twitch-Chat/Assets/Package/Runtime/TwitchConnection.cs index e241869..1f7ce34 100644 --- a/Unity-Twitch-Chat/Assets/Package/Runtime/TwitchConnection.cs +++ b/Unity-Twitch-Chat/Assets/Package/Runtime/TwitchConnection.cs @@ -32,6 +32,7 @@ public TwitchConnection(IRC irc) this.rateLimit = RateLimit.ChatRegular; + this.joinChannelOnStart = irc.joinChannelOnStart; this.showIRCDebug = irc.showIRCDebug; this.showThreadDebug = irc.showThreadDebug; @@ -62,6 +63,7 @@ private bool ThreadsRunning private readonly int readBufferSize; private readonly int readInterval; private readonly int writeInterval; + private readonly bool joinChannelOnStart; private readonly bool showIRCDebug; private readonly bool showThreadDebug; private readonly bool useRandomColorForUndefined;