-
Notifications
You must be signed in to change notification settings - Fork 6
/
Program.cs
151 lines (129 loc) · 5.07 KB
/
Program.cs
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
using System.CommandLine;
using System.Reflection;
using Decryptor.Utils;
namespace Decryptor
{
class Program
{
static int Main(string[] args)
{
// dotnet run 跳过项目路径参数
// if (args.Length > 0 && args[0].EndsWith(".cs", StringComparison.OrdinalIgnoreCase))
// {
// args = args.Skip(1).ToArray();
// }
// 创建根命令
var rootCommand = new RootCommand("MobaXterm Decryptor Application");
// 添加 --debug 选项作为全局选项
var debugOption = new Option<bool>("--debug", "Enable debug mode");
rootCommand.AddGlobalOption(debugOption);
// 自动模式命令
// var autoModeCommand = new Command("auto", "Run all tools in automatic mode");
// autoModeCommand.SetHandler(
// (bool debug) =>
// {
// Logger.Initialize(debug);
// Logger.Info("Automatic mode");
// CallAllToolsClasses();
// },
// debugOption
// );
// rootCommand.AddCommand(autoModeCommand);
// 动态加载子命令
LoadSubCommands(rootCommand, debugOption);
// 设置默认命令
rootCommand.SetHandler(
(bool debug) =>
{
Logger.Initialize(debug);
Logger.Info("Automatic mode");
CallAllSubCommandClasses();
},
debugOption
);
return rootCommand.InvokeAsync(args).Result;
}
private static void LoadSubCommands(Command rootCommand, Option<bool> debugOption)
{
// 获取当前执行程序集
Assembly assembly = Assembly.GetExecutingAssembly();
// 查找 Decryptor.Softwares 命名空间下的所有类型
var toolTypes = assembly
.GetTypes()
.Where(t => t.Namespace == "Decryptor.Softwares" && t.GetMethod("Run") != null)
.ToList();
foreach (var type in toolTypes)
{
var commandName = type.Name.ToLower();
var toolCommand = new Command(commandName, $"Execute {commandName} command");
// 添加位置参数
var argsOption = new Argument<string[]>("args", "Arguments for the command")
{
Arity = ArgumentArity.ZeroOrMore
};
toolCommand.AddArgument(argsOption);
toolCommand.SetHandler(
(string[] classArgs, bool debug) =>
{
Logger.Initialize(debug);
Logger.Info($"Execute {commandName} command");
CallSpecificClass(type, classArgs);
},
argsOption,
debugOption
);
rootCommand.AddCommand(toolCommand);
}
}
private static void CallSpecificClass(Type type, string[] classArgs)
{
try
{
object instance = Activator.CreateInstance(type);
MethodInfo methodInfo = type.GetMethod("Run");
if (methodInfo != null)
{
methodInfo.Invoke(instance, new object[] { classArgs });
}
else
{
Logger.Info($"Run method not found for class {type.Name}", label: "[-]");
}
}
catch (Exception e)
{
Logger.Info(
$"An error occurred while calling class {type.Name}: {e.Message}",
label: "[-]"
);
}
}
private static void CallAllSubCommandClasses()
{
// 获取当前执行程序集
Assembly assembly = Assembly.GetExecutingAssembly();
// 查找 Decryptor.Softwares 命名空间下的所有类型
var toolTypes = assembly
.GetTypes()
.Where(t => t.Namespace == "Decryptor.Softwares" && t.GetMethod("Run") != null)
.ToList();
foreach (var type in toolTypes)
{
try
{
object instance = Activator.CreateInstance(type);
MethodInfo methodInfo = type.GetMethod("Run");
methodInfo.Invoke(instance, new object[] { new string[] { } });
}
catch (Exception e)
{
Logger.Info(
$"An error occurred while calling class {type.Name}: {e.Message}",
label: "[-]"
);
continue;
}
}
}
}
}