private void ListLoaded(CommandEventArgs e) { if (e.Arguments.Length != 1) { throw new CommandSyntaxException(); } Plugin[] loaded; if (e.HasFlag("h")) { loaded = Server.InternalPluginManager.ActuallyGetAllPlugins(); } else { loaded = Server.InternalPluginManager.GetAllPlugins(); } StringBuilder sb = new StringBuilder(); sb.Append("Found "); sb.Append(loaded.Length); sb.AppendLine(" plugins loaded."); for (int i = 0; i < loaded.Length; i++) { sb.Append((i + 1).ToString().PadRight(4)); sb.Append(loaded[i].Name.PadRight(24)); sb.AppendLine(loaded[i].Hidden ? "(Hidden)" : ""); } Logger.Info(sb.ToString()); }
private void Help(object sender, CommandEventArgs e) { if (e.Arguments.Length == 1) { Command command; try { command = Server.CommandEngine.FindCommand(e.Arguments[0]); } catch (Exception exception) { Logger.Error(exception.Message); return; } Logger.Info($"Command: \"{command.Name}\"\n\nUsage:\n{command.Usage}\n\nDescription:\n{command.Description}"); } else if (e.HasFlag("l")) { if (e.Flags["l"] != null) { IEnumerable <Command> commands; try { commands = Server.CommandEngine.GetCommands(e.Flags["l"]); } catch (KeyNotFoundException) { Logger.Error("No plugins found with the name '" + e.Flags["l"] + "'"); return; } Logger.Info(commands.Count() == 0 ? "This plugin has no commands." : ("Available commands: " + string.Join(", ", commands.Select(c => c.Name).ToArray()))); } else { IEnumerable <Command> commands = Server.CommandEngine.GetCommands(); Logger.Info(commands.Count() == 0 ? "This server has no commands." : ("Available commands: " + string.Join(", ", commands.Select(c => c.Name).ToArray()))); } } else { throw new CommandSyntaxException(); } }
private void DebugLogs(object sender, CommandEventArgs e) { Logger.Trace("This is a test message at Trace level.", new Exception("This is a test exception at Trace level.")); Logger.Info("This is a test message at Info level.", new Exception("This is a test exception at Info level.")); Logger.Warning("This is a test message at Warning level.", new Exception("This is a test exception at Warning level.")); Logger.Error("This is a test message at Error level.", new Exception("This is a test exception at Error level.")); Logger.Fatal("This is a test message at Fatal level.", new Exception("This is a test exception at Fatal level.")); if (e.HasFlag("l")) { #pragma warning disable CS0618 // Type or member is obsolete WriteEvent("This is a test message at Trace level.", LogType.Trace, new Exception("This is a test exception at Trace level.")); WriteEvent("This is a test message at Info level.", LogType.Info, new Exception("This is a test exception at Info level.")); WriteEvent("This is a test message at Warning level.", LogType.Warning, new Exception("This is a test exception at Warning level.")); WriteEvent("This is a test message at Error level.", LogType.Error, new Exception("This is a test exception at Error level.")); WriteEvent("This is a test message at Fatal level.", LogType.Fatal, new Exception("This is a test exception at Fatal level.")); #pragma warning restore CS0618 // Type or member is obsolete } }
private void ClientCommandHandler(object sender, CommandEventArgs e) { if (e.Arguments.Length < 1) { throw new CommandSyntaxException(); } if (e.Arguments[0].ToLower() == "add") { if (e.Arguments.Length != 1) { throw new CommandSyntaxException($"Expected 1 argument to client command but found {e.Arguments.Length}."); } IPAddress ip; if (e.Flags["ip"] != null) { if (!IPAddress.TryParse(e.Flags["ip"], out ip)) { throw new CommandSyntaxException("Could not parse the IP address of the client to create."); } } else { ip = IPAddress.Loopback; } ushort port; if (e.Flags["port"] != null) { if (!ushort.TryParse(e.Flags["port"], out port)) { throw new CommandSyntaxException("Could not parse the port of the client to create."); } } else if (e.Flags["p"] != null) { if (!ushort.TryParse(e.Flags["p"], out port)) { throw new CommandSyntaxException("Could not parse the port of the client to create."); } } else { port = 0; } bool outputData = e.HasFlag("h"); MockedNetworkServerConnection connection = new MockedNetworkServerConnection(this, ip, port, outputData); Server.InternalClientManager.HandleNewConnection(connection); connections.Add(connection.Client.ID, connection); } else if (e.Arguments[0].ToLower() == "remove") { if (e.Arguments.Length != 2) { throw new CommandSyntaxException($"Expected 2 arguments to client command but found {e.Arguments.Length}."); } if (!int.TryParse(e.Arguments[1], out int id)) { throw new CommandSyntaxException("Could not parse the ID of the client to disconnect."); } if (!connections.TryGetValue(id, out MockedNetworkServerConnection connection)) { throw new CommandSyntaxException("You can only disconnect clients previously created with the client command."); } Server.InternalClientManager.HandleDisconnection(connection.Client, true, SocketError.Success, null); } else { throw new CommandSyntaxException("Invalid argument '" + e.Arguments[0] + "'"); } }