Files
MewtocolNet/Examples/Program.cs
2023-06-27 20:44:11 +02:00

109 lines
2.8 KiB
C#

using MewtocolNet.RegisterBuilding;
using MewtocolNet;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;
using MewtocolNet.Logging;
using System.Text.RegularExpressions;
namespace Examples;
class Program {
static ExampleScenarios ExampleSzenarios = new ExampleScenarios();
static void Main(string[] args) {
AppDomain.CurrentDomain.UnhandledException += (s,e) => {
Console.WriteLine(e.ExceptionObject.ToString());
};
TaskScheduler.UnobservedTaskException += (s,e) => {
Console.WriteLine(e.Exception.ToString());
};
ExampleSzenarios.SetupLogger();
LoopInput();
}
private static void LoopInput () {
Console.WriteLine("All available scenarios\n");
var methods = ExampleSzenarios.GetType().GetMethods();
var invokeableMethods = new List<MethodInfo>();
for (int i = 0, j = 0; i < methods.Length; i++) {
MethodInfo method = methods[i];
var foundAtt = method.GetCustomAttribute(typeof(ScenarioAttribute));
if(foundAtt != null && foundAtt is ScenarioAttribute att) {
Console.WriteLine($"[{j + 1}] {method.Name}() - {att.Description}");
invokeableMethods.Add(method);
j++;
}
}
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine("\nEnter a number to excecute a example");
Console.ResetColor();
Console.WriteLine("\nOther possible commands: \n");
Console.WriteLine($"'logger <level>' - set loglevel to one of: {string.Join(", ", Enum.GetNames(typeof(LogLevel)).ToList())}");
Console.WriteLine("'exit' - to close this program");
Console.WriteLine("'clear' - to clear the output");
Console.Write("> ");
var line = Console.ReadLine();
var loggerMatch = Regex.Match(line, @"logger (?<level>[a-zA-Z]{0,})");
if (loggerMatch.Success && Enum.TryParse<LogLevel>(loggerMatch.Groups["level"].Value, out var loglevel)) {
Logger.LogLevel = loglevel;
Console.WriteLine($"Loglevel changed to: {loglevel}");
} else if (line == "exit") {
Environment.Exit(0);
} else if (line == "clear") {
Console.Clear();
} else if (int.TryParse(line, out var lineNum)) {
var index = Math.Clamp(lineNum - 1, 0, invokeableMethods.Count - 1);
var task = (Task)invokeableMethods.ElementAt(index).Invoke(ExampleSzenarios, null);
task.Wait();
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine("The program ran to completition");
Console.ResetColor();
} else {
Console.WriteLine("Wrong input");
}
LoopInput();
}
}