This repository was archived by the owner on Feb 21, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathShellRegist.cs
More file actions
71 lines (58 loc) · 2.27 KB
/
Copy pathShellRegist.cs
File metadata and controls
71 lines (58 loc) · 2.27 KB
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
59
60
61
62
63
64
65
66
67
68
69
70
71
using cwiz.ext;
using System;
using System.Collections.Generic;
public static class ShellRegist {
private static readonly CommandRegistry _cmdreg =
typeof(Shell).RefGetField<CommandRegistry>("commands");
private static Lazy<Dictionary<string, Action>> Commands => new(() =>
_cmdreg.RefGetField<Dictionary<string, Action>>("commands"));
private static Lazy<Dictionary<string, Action<string>>> CommandsStr => new(() =>
_cmdreg.RefGetField<Dictionary<string, Action<string>>>("commandsStr"));
private static Lazy<Dictionary<string, string>> Description => new(() =>
_cmdreg.RefGetField<Dictionary<string, string>>("description"));
public static Lazy<string> HelpColor => new(() =>
_cmdreg.RefGetField<string>("helpColor"));
public class CommandBase(string key, string desc) {
public string Key { get; } = key ?? throw new ArgumentException("command key can't be null");
public string Desc { get; } = desc;
}
public class Command(
string key,
string desc,
Action act
) : CommandBase(key, desc) {
public Action Act { get; } = act;
}
public class SCommand(
string key,
string desc,
Action<string> act
) : CommandBase(key, desc) {
public Action<string> Act { get; } = act;
}
public static void Reg(this CommandBase cmd) {
switch (cmd) {
case Command c:
Shell.RegisterCommand(c.Key, c.Act, c.Desc);
break;
case SCommand c:
Shell.RegisterCommand(c.Key, c.Act, c.Desc);
break;
}
}
public static bool Destroy(this CommandBase cmd) {
Description.Value.Remove(cmd.Key);
return cmd switch {
Command => Commands.Value.Remove(cmd.Key),
SCommand => CommandsStr.Value.Remove(cmd.Key),
_ => throw new NotImplementedException("Unimplemented Command type")
};
}
public static void Help(this CommandBase cmd) =>
_cmdreg.OnHelp(cmd.Key);
public static string Help(string key, bool doformat = false) =>
!Description.Value.TryGetValue(key, out var help) ? null :
(doformat ?
HelpColor.Value + help + "</color>" :
help);
}