private static void Client_PacketReceived(Client sender, Client.PacketReceivedEventArgs e) { PacketType packetType = (PacketType)e.Reader.ReadInt16(); switch (packetType) { case PacketType.Disconnect: { string msg = e.Reader.ReadString(); Console.WriteLine("Disconnected:" + msg); } break; case PacketType.Message: { ConsoleColor oldColor = Console.ForegroundColor; ConsoleColor consoleColor = (ConsoleColor)e.Reader.ReadByte(); string msg = e.Reader.ReadString(); Console.ForegroundColor = consoleColor; if (msg.EndsWith(": ")) { Console.Write(msg); } else { Console.WriteLine(msg); } Console.ForegroundColor = oldColor; } break; case PacketType.MessageBuffer: { short length = e.Reader.ReadInt16(); ConsoleColor oldColor = Console.ForegroundColor; for (int i = 0; i < length; i++) { Console.ForegroundColor = (ConsoleColor)e.Reader.ReadByte(); string msg = e.Reader.ReadString(); if (msg.EndsWith(": ")) { Console.Write(msg); } else { Console.WriteLine(msg); } } Console.ForegroundColor = oldColor; } break; } }
private void RemoteClient_PacketReceived(Client sender, Client.PacketReceivedEventArgs e) { PacketType packetType = (PacketType)e.Reader.ReadInt16(); //Disconnect the user if he attempts to do anything else before authenticating. if (packetType != PacketType.Authenticate && !Authenticated) { Disconnect("Your attempt at sending packets before authenticating has been ignored!"); return; } switch (packetType) { case PacketType.Authenticate: InterfaceType = (InterfaceType)e.Reader.ReadByte(); int major = e.Reader.ReadInt32(); int minor = e.Reader.ReadInt32(); if (Rtc.buildVersion.Major != major || Rtc.buildVersion.Minor != minor) { Disconnect($"Your version ({major}.{minor}) is incompatible with the server's version ({Rtc.buildVersion.Major}.{Rtc.buildVersion.Minor})."); return; } string Username = e.Reader.ReadString(); string Password = e.Reader.ReadString(); TSUser = TShock.Users.GetUserByName(Username); if (TSUser == null || !TSUser.VerifyPassword(Password)) { Disconnect("Invalid username/password or insufficient privileges."); return; } Group g = TShock.Groups.GetGroupByName(TSUser.Group); if (!g.HasPermission("*")) { Disconnect("Invalid username/password or insufficient privileges."); return; } Authenticated = true; Packet pck = new Packet((short)PacketType.MessageBuffer, (short)Rtc.MessagesBuffer.Length); for (int i = 0; i < Rtc.MessagesBuffer.Length; i++) { if (!string.IsNullOrEmpty(Rtc.MessagesBuffer[i])) { pck.Write(Rtc.ColorBuffer[i], Rtc.MessagesBuffer[i]); } } sender.Send(pck); break; case PacketType.Input: string text = e.Reader.ReadString(); Rtc.ConsoleInput.SendText(text); break; } }