aboutsummaryrefslogtreecommitdiffstatsci
path: root/Samples/CrowDebugger/Program.cs
blob: b15e3b17f4142b49417931f94bdd893d92dc3b8c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
using System.Threading;
using System;
using System.IO;
using System.Reflection;
using System.Runtime.Loader;

namespace CrowDebugger
{
	class Program
	{
		const int BUFF_MAX = 1024;
		static void Main(string[] args)
		{
			AssemblyLoadContext crowLoadCtx = new AssemblyLoadContext("CrowDebuggerLoadContext");
			Console.WriteLine("Crow Debugger started.");
			char[] buffer = new char[1024];
			int buffPtr = 0;
			while (true)
			{				
				if (Console.KeyAvailable)
				{
					ConsoleKeyInfo key = Console.ReadKey(true);
					if (key.Key == ConsoleKey.Enter) {
						Span<char> cmd = buffer.AsSpan(0, buffPtr);
						Console.WriteLine($"=> {cmd.ToString()}");
						if (cmd[0] == 'q' || cmd.SequenceEqual ("quit"))
							break;
						
						if (cmd.StartsWith("load")) {
							using(crowLoadCtx.EnterContextualReflection()) {
								string str = cmd.Slice(5).ToString();
								Console.WriteLine($"[msg]:Trying to load crow dll from: '{str}'");
								if (!File.Exists (str)) {
									Console.WriteLine ($"[error]:File not found: {str}");
								} else {
									Assembly crowAssembly = crowLoadCtx.LoadFromAssemblyPath (str);
									if (crowAssembly == null) {
										Console.WriteLine($"[error]:Failed to load crow dll from: {str}");
									} else {
										Console.WriteLine($"[ok]:Crow Assembly loaded:{crowAssembly.FullName}");
										
									}
								}
							}
						} else
							Console.WriteLine($"=> {cmd.ToString()}");
						buffPtr = 0;
						continue;
					}
						
					buffer[buffPtr++] = key.KeyChar;
				}
			}

			Console.WriteLine("exited");
		}
	}
}