-
Notifications
You must be signed in to change notification settings - Fork 0
/
ConsoleCommand.cs
157 lines (139 loc) · 5.65 KB
/
ConsoleCommand.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
152
153
154
155
156
157
using System;
using System.Text.RegularExpressions;
using System.Collections.Generic;
using HarmonyLib;
namespace K8Lib.Commands
{
public class ConsoleCommand
{
public string CommandName { get; private set; }
public Action<int> OnCommandSentInt { get; private set; }
public Action<string> OnCommandSentString { get; private set; }
public ConsoleCommand(string commandName, Action<int> onCommandSent)
{
CommandName = commandName;
OnCommandSentInt = onCommandSent;
Patch.AddCommandInt(this);
}
public ConsoleCommand(string commandName, Action<string> onCommandSent)
{
CommandName = commandName;
OnCommandSentString = onCommandSent;
Patch.AddCommandString(this);
}
[HarmonyPatch(typeof(ac_DevConsole), "Submit")]
public class Patch
{
private static List<ConsoleCommand> commandsInt = new List<ConsoleCommand>();
private static List<ConsoleCommand> commandsString = new List<ConsoleCommand>();
private static List<ConsoleCommand> postfixCommandsInt = new List<ConsoleCommand>();
private static List<ConsoleCommand> postfixCommandsString = new List<ConsoleCommand>();
public static void AddCommandInt(ConsoleCommand command)
{
if (commandsInt.Count == 0 && commandsString.Count == 0)
{
Harmony harmony = new Harmony(PluginInfo.PLUGIN_GUID);
harmony.PatchAll();
}
commandsInt.Add(command);
}
public static void AddCommandString(ConsoleCommand command)
{
if (commandsInt.Count == 0 && commandsString.Count == 0)
{
Harmony harmony = new Harmony(PluginInfo.PLUGIN_GUID);
harmony.PatchAll();
}
commandsString.Add(command);
}
public static bool Prefix(ac_DevConsole __instance, string ConsoleSubmission)
{
foreach (var command in commandsInt)
{
if (ConsoleSubmission.ToLower().Contains(command.CommandName))
{
string[] digits = Regex.Split(ConsoleSubmission, "\\D+");
for (int i = 0; i < digits.Length; i++)
{
int value;
if (int.TryParse(digits[i], out value))
{
command.OnCommandSentInt?.Invoke(value);
}
}
return true;
}
}
foreach (var command in commandsString)
{
if (ConsoleSubmission.ToLower().Contains(command.CommandName))
{
command.OnCommandSentString?.Invoke(ConsoleSubmission);
return true;
}
}
return true;
}
}
}
public class PostfixConsoleCommand
{
public string CommandName { get; private set; }
public Action<int> OnCommandSentInt { get; private set; }
public Action<string> OnCommandSentString { get; private set; }
public PostfixConsoleCommand(string commandName, Action<int> onCommandSent)
{
CommandName = commandName;
OnCommandSentInt = onCommandSent;
Patch.AddPostfixCommandInt(this);
}
public PostfixConsoleCommand(string commandName, Action<string> onCommandSent)
{
CommandName = commandName;
OnCommandSentString = onCommandSent;
Patch.AddPostfixCommandString(this);
}
[HarmonyPatch(typeof(ac_DevConsole), "Submit")]
public class Patch
{
private static List<PostfixConsoleCommand> postfixCommandsInt = new List<PostfixConsoleCommand>();
private static List<PostfixConsoleCommand> postfixCommandsString = new List<PostfixConsoleCommand>();
public static void AddPostfixCommandInt(PostfixConsoleCommand command)
{
postfixCommandsInt.Add(command);
}
public static void AddPostfixCommandString(PostfixConsoleCommand command)
{
postfixCommandsString.Add(command);
}
public static void Postfix(ac_DevConsole __instance, string ConsoleSubmission)
{
foreach (var command in postfixCommandsInt)
{
if (ConsoleSubmission.ToLower().Contains(command.CommandName))
{
string[] digits = Regex.Split(ConsoleSubmission, "\\D+");
for (int i = 0; i < digits.Length; i++)
{
int value;
if (int.TryParse(digits[i], out value))
{
command.OnCommandSentInt?.Invoke(value);
}
}
return;
}
}
foreach (var command in postfixCommandsString)
{
if (ConsoleSubmission.ToLower().Contains(command.CommandName))
{
command.OnCommandSentString?.Invoke(ConsoleSubmission);
return;
}
}
return;
}
}
}
}