Skip to content

Commit

Permalink
Clean up and additional error reporting
Browse files Browse the repository at this point in the history
  • Loading branch information
Hotrian committed Jun 26, 2016
1 parent 35b7c18 commit 9b521b3
Show file tree
Hide file tree
Showing 3 changed files with 118 additions and 34 deletions.
2 changes: 1 addition & 1 deletion Assets/HOTK/HOTK_TrackedDeviceManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,6 @@ void Log(string text)
/// <param name="text"></param>
void LogWarning(string text)
{
TwitchChatTester.Instance.AddSystemNotice(text, true);
TwitchChatTester.Instance.AddSystemNotice(text, TwitchIRC.NoticeColor.Yellow);
}
}
30 changes: 17 additions & 13 deletions Assets/HOTK/Twitch/TwitchChatTester.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,13 +67,18 @@ public void ToggleConnect()
{
if (ChannelBox != null && ChannelBox.text != "")
{
if (ChannelBox.text.Contains(" "))
{
AddSystemNotice("Channel name invalid!", TwitchIRC.NoticeColor.Red);
return;
}
UsernameBox.interactable = false;
OAuthBox.interactable = false;
ChannelBox.interactable = false;
ConnectButtonText.text = "Press to Disconnect";

_connected = true;
OnChatMsg(ToTwitchNotice(string.Format("Logging into #{0} as {1}!", ChannelBox.text, UsernameBox.text)));
OnChatMsg(TwitchIRC.ToTwitchNotice(string.Format("Logging into #{0} as {1}!", ChannelBox.text, UsernameBox.text)));
IRC.NickName = UsernameBox.text;
IRC.Oauth = OAuthBox.text;
IRC.ChannelName = ChannelBox.text.ToLower();
Expand All @@ -82,11 +87,11 @@ public void ToggleConnect()
IRC.MessageRecievedEvent.AddListener(OnChatMsg);
IRC.StartIRC();
}
else OnChatMsg(ToTwitchNotice("Unable to Connect: Enter a Valid Channel Name!", true));
else AddSystemNotice("Unable to Connect: Enter a Valid Channel Name!", TwitchIRC.NoticeColor.Red);
}
else OnChatMsg(ToTwitchNotice("Unable to Connect: Enter a Valid OAuth Key! http://www.twitchapps.com/tmi/", true));
else AddSystemNotice("Unable to Connect: Enter a Valid OAuth Key! http://www.twitchapps.com/tmi/", TwitchIRC.NoticeColor.Red);
}
else OnChatMsg(ToTwitchNotice("Unable to Connect: Enter a Valid Username!", true));
else AddSystemNotice("Unable to Connect: Enter a Valid Username!", TwitchIRC.NoticeColor.Red);
}
else
{
Expand All @@ -98,7 +103,7 @@ public void ToggleConnect()
_connected = false;
IRC.MessageRecievedEvent.RemoveListener(OnChatMsg);
IRC.enabled = false;
OnChatMsg(ToTwitchNotice("Disconnected!", true));
OnChatMsg(TwitchIRC.ToTwitchNotice("Disconnected!", TwitchIRC.NoticeColor.Red));
}
}

Expand All @@ -114,12 +119,16 @@ private void OnChatMsg(string msg)
switch (mode)
{
case "NOTICE":
// Compatability with real Twitch System messages
if (nickname == "tmi.twitch.tv")
{
nickname = "Twitch";
if (chat.StartsWith("Error"))
channel = "System-Red";
else if (chat == "Login unsuccessful")
channel = "System-Red";
}
// Convert Notice to Name Color
switch (channel)
{
case "System-Green":
Expand All @@ -129,7 +138,7 @@ private void OnChatMsg(string msg)
AddMsg(nickname, ColorToHex(new Color(1f, 0f, 0f)), chat);
break;
case "System-Blue":
AddMsg(nickname, ColorToHex(new Color(0.2f, 0.4f, 1f)), chat);
AddMsg(nickname, ColorToHex(new Color(0f, 0.4f, 1f)), chat);
break;
case "System-Yellow":
AddMsg(nickname, ColorToHex(new Color(1f, 1f, 0f)), chat);
Expand Down Expand Up @@ -159,14 +168,9 @@ private void OnChatMsg(string msg)
}
}

public void AddSystemNotice(string msgIn, bool warning = false)
{
OnChatMsg(string.Format(warning ? ":System NOTICE *System-Yellow :{0}" : ":System NOTICE *System-Blue :{0}", msgIn));
}

public static string ToTwitchNotice(string msgIn, bool error = false)
public void AddSystemNotice(string msgIn, TwitchIRC.NoticeColor colorEnum = TwitchIRC.NoticeColor.Blue)
{
return string.Format(error ? ":Twitch NOTICE *System-Red :{0}" : ":Twitch NOTICE *System-Green :{0}", msgIn);
OnChatMsg(TwitchIRC.ToNotice("System", msgIn, colorEnum));
}

private void AddMsg(string nickname, string color, string chat)
Expand Down
120 changes: 100 additions & 20 deletions Assets/HOTK/Twitch/TwitchIRC.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ public class MsgEvent : UnityEngine.Events.UnityEvent<string> { }
private readonly Queue<string> _commandQueue = new Queue<string>();
private readonly List<string> _recievedMsgs = new List<string>();
private System.Threading.Thread _inProc, _outProc;

private bool _connected;
private bool _loggedin;

public void StartIRC()
{
Expand All @@ -35,7 +38,9 @@ public void StartIRC()
var networkStream = sock.GetStream();
var input = new System.IO.StreamReader(networkStream);
var output = new System.IO.StreamWriter(networkStream);


_loggedin = false;
_connected = false;
//Send PASS & NICK.
output.WriteLine("PASS " + Oauth);
output.WriteLine("NICK " + NickName.ToLower());
Expand All @@ -47,7 +52,27 @@ public void StartIRC()
//input proc
_inProc = new System.Threading.Thread(() => IRCInputProcedure(input, networkStream));
_inProc.Start();

CancelInvoke("CheckConnection");
Invoke("CheckConnection", 5f);
}

private void CheckConnection()
{
if (_stopThreads) return;
lock (_recievedMsgs)
{
if (!_loggedin)
{
_recievedMsgs.Add(ToNotice("System", "Should be logged in by now.. are the username and oauth correct?", NoticeColor.Yellow));
}
else if (!_connected)
{
_recievedMsgs.Add(ToNotice("System", "Should be connected by now.. is the channel name correct?", NoticeColor.Yellow));
}
}
}

private void IRCInputProcedure(System.IO.TextReader input, System.Net.Sockets.NetworkStream networkStream)
{
while (!_stopThreads)
Expand All @@ -59,26 +84,38 @@ private void IRCInputProcedure(System.IO.TextReader input, System.Net.Sockets.Ne
}

_buffer = input.ReadLine();

if (_buffer == null) continue;
if (_buffer.Contains("PRIVMSG #") || _buffer.Split(' ')[1] == "NOTICE")
var tokens = _buffer.Split(' ');
switch (tokens[1])
{
lock (_recievedMsgs)
{
_recievedMsgs.Add(_buffer);
}
}
else if (_buffer.StartsWith("PING "))
{
SendCommand(_buffer.Replace("PING", "PONG"));
}
else if (_buffer.Split(' ')[1] == "001")
{
lock (_recievedMsgs)
{
_recievedMsgs.Add(TwitchChatTester.ToTwitchNotice("Connected!"));
}
SendCommand("JOIN #" + ChannelName);
case "PRIVMSG":
case "NOTICE":
lock (_recievedMsgs)
{
_recievedMsgs.Add(_buffer);
}
break;
case "001":
lock (_recievedMsgs)
{
_recievedMsgs.Add(ToTwitchNotice("Logged in! Connecting to chat.."));
_loggedin = true;
}
SendCommand("JOIN #" + ChannelName);
break;
case "JOIN":
lock (_recievedMsgs)
{
_recievedMsgs.Add(ToTwitchNotice(string.Format("Connected to {0}!", tokens[2])));
_connected = true;
}
break;
default:
if (_buffer.StartsWith("PING "))
{
SendCommand(_buffer.Replace("PING", "PONG"));
}
break;
}
}
}
Expand All @@ -96,7 +133,7 @@ private void IRCOutputProcedure(System.IO.TextWriter output)
continue;
}
// https://github.com/justintv/Twitch-API/blob/master/IRC.md#command--message-limit
//have enough time passed since we last sent a message/command?
//has enough time passed since we last sent a message/command?
if (stopWatch.ElapsedMilliseconds <= 1750)
{
Thread.Sleep(20);
Expand Down Expand Up @@ -135,10 +172,12 @@ public void OnEnable()
public void OnDisable()
{
_stopThreads = true;
CancelInvoke("CheckConnection");
}
public void OnDestroy()
{
_stopThreads = true;
CancelInvoke("CheckConnection");
}
public void Update()
{
Expand All @@ -152,4 +191,45 @@ public void Update()
_recievedMsgs.Clear();
}
}

public static string ToTwitchNotice(string msgIn, NoticeColor colorEnum = NoticeColor.Green)
{
return ToNotice("Twitch", msgIn, colorEnum);
}

public static string ToNotice(string nickname, string msgIn, NoticeColor colorEnum = NoticeColor.Green)
{
return string.Format(":{0} NOTICE {1} :{2}", nickname, NoticeColorToString(colorEnum), msgIn);
}

public static string NoticeColorToString(NoticeColor colorEnum)
{
switch (colorEnum)
{
case NoticeColor.Green:
return "*System-Green";
case NoticeColor.Red:
return "*System-Red";
case NoticeColor.Blue:
return "*System-Blue";
case NoticeColor.Yellow:
return "*System-Yellow";
case NoticeColor.Purple:
return "*System-Purple";
case NoticeColor.White:
return "*System-White";
default:
throw new ArgumentOutOfRangeException("colorEnum", colorEnum, null);
}
}

public enum NoticeColor
{
Green,
Red,
Blue,
Yellow,
Purple,
White
}
}

0 comments on commit 9b521b3

Please sign in to comment.