public static void WriteCommand(string path, string contents) { try { File.WriteAllText(path, contents); } catch (Exception e) { if (e is DirectoryNotFoundException) { AdvancedConsole.Warn("Directory Not Found!"); } else if (e is PathTooLongException) { AdvancedConsole.Warn("Path Too Long!"); } else if (e is UnauthorizedAccessException) { AdvancedConsole.Warn("Unauthorized Access!"); } else { AdvancedConsole.Warn(AdvancedConsole.WarningType.WrongArguments); } return; } AdvancedConsole.WriteLine("Text successfully written!"); }
public static void ReadCommand(string path) { try { AdvancedConsole.WriteLine(File.ReadAllText(path)); } catch (Exception e) { if (e is DirectoryNotFoundException) { AdvancedConsole.Warn("Directory Not Found!"); } else if (e is FileNotFoundException) { AdvancedConsole.Warn("File Not Found!"); } else if (e is PathTooLongException) { AdvancedConsole.Warn("Path Too Long!"); } else if (e is UnauthorizedAccessException) { AdvancedConsole.Warn("Unauthorized Access!"); } else { AdvancedConsole.Warn(AdvancedConsole.WarningType.WrongArguments); } } }
public static void ComputeCommand(string expression) { try { using DataTable table = new DataTable(); object result = table.Compute(expression, string.Empty); AdvancedConsole.WriteLine($"Result: {result}"); } catch { AdvancedConsole.Warn("Wrong expression!"); } }
public static void RandomCommand(int minValue, int maxValue) { try { Random random = new Random(); AdvancedConsole.WriteLine($"Result: {random.Next(minValue, maxValue)}"); } catch (Exception e) { if (e is ArgumentOutOfRangeException) { AdvancedConsole.Warn("Wrong arguments! minValue cannot be greater than maxValue!"); } else { AdvancedConsole.Warn(AdvancedConsole.WarningType.WrongArguments); } } }
public static void HelpCommand(string prefix = null) { List <Command> commands; ListScanner ignoreInHelpFilter = new ListScanner("Help Ignore", ListScanner.TargetOfScanner.Tags, ListScanner.ScannerCondition.NotContains); if (prefix != null && !Command.Prefixes.Contains(prefix)) { if (Command.Prefixes.Count <= 1) { AdvancedConsole.Warn(AdvancedConsole.WarningType.WrongArguments); } else { AdvancedConsole.Warn("Invalid prefix!"); } return; } if (prefix == null) { #region Colored text AdvancedConsole.ColoredWriteLine(new CStr("Command to enter:\n"), new CStr(ConsoleColor.DarkCyan, "prefix "), new CStr(ConsoleColor.Cyan, "name "), new CStr(ConsoleColor.Magenta, "argument1 argument2 argumentN\n")); AdvancedConsole.ColoredWriteLine(new CStr("Command in help:\n"), new CStr(ConsoleColor.DarkCyan, "prefix "), new CStr(ConsoleColor.Cyan, "name "), new CStr(ConsoleColor.DarkYellow, "parameter-type1 "), new CStr(ConsoleColor.Yellow, "parameter-name1"), new CStr(", "), new CStr(ConsoleColor.DarkYellow, "parameter-type2 "), new CStr(ConsoleColor.Yellow, "parameter-name2 "), new ColoredString("= "), new CStr(ConsoleColor.Magenta, "default-value "), new CStr(ConsoleColor.DarkGray, "| Description.\n")); #endregion } if (Command.Prefixes.Count == 1) { prefix = Command.Prefixes[0]; } if (prefix == null) { commands = Command.Find(ignoreInHelpFilter, new ListScanner("Help")); } else { ListScanner ignoreHelpCommandsFilter = new ListScanner("Help", ListScanner.TargetOfScanner.Tags, ListScanner.ScannerCondition.NotContains); SimpleCommandScanner prefixFilter = new SimpleCommandScanner(prefix, SimpleCommandScanner.TargetOfScanner.Prefix); commands = Command.Find(ignoreInHelpFilter, ignoreHelpCommandsFilter, prefixFilter); } AdvancedConsole.WriteLine(commands.Count > 0 ? "Commands:" : "Sorry, commands not found :("); foreach (Command command in commands) { AdvancedConsole.ColoredWrite(ConsoleColor.DarkCyan, command.Prefix); AdvancedConsole.ColoredWrite(ConsoleColor.Cyan, command.Name); foreach (ParameterInfo parameter in command.Parameters) { AdvancedConsole.ColoredWrite(ConsoleColor.DarkYellow, " " + parameter.ParameterType.Name + " "); AdvancedConsole.ColoredWrite(ConsoleColor.Yellow, parameter.Name); if (parameter.HasDefaultValue) { AdvancedConsole.Write(" = "); AdvancedConsole.ColoredWrite(ConsoleColor.Magenta, $"{parameter.DefaultValue ?? "null"}"); } if (parameter != command.Parameters.Last()) { AdvancedConsole.Write(","); } } AdvancedConsole.ColoredWriteLine(ConsoleColor.DarkGray, $" | {command.Description}"); } }