forked from SignatureBeef/Open-Terraria-API
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHookCommandProcessing.Server.cs
More file actions
120 lines (103 loc) · 4.32 KB
/
HookCommandProcessing.Server.cs
File metadata and controls
120 lines (103 loc) · 4.32 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
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
/*
Copyright (C) 2020 DeathCradle
This file is part of Open Terraria API v3 (OTAPI)
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma warning disable CS8321 // Local function is declared but never used
#pragma warning disable CS0436 // Type conflicts with imported type
#if tModLoaderServer_V1_3 || tModLoader_V1_4
System.Console.WriteLine("Command processing not available in TML");
#else
using ModFramework;
using Mono.Cecil.Cil;
using MonoMod;
using MonoMod.Cil;
using System;
using System.Linq;
/// <summary>
/// @doc Creates Hooks.Main.CommandProcess. Allows plugins to intercept issued commands.
/// </summary>
[Modification(ModType.PreMerge, "Hooking command processing")]
[MonoMod.MonoModIgnore]
void HookCommandProcessing(MonoModder modder)
{
var startDedInputCallBack = modder.GetILCursor(() => Terraria.Main.startDedInputCallBack());
var vText = startDedInputCallBack.Body.Variables[0];
#if TerrariaServer_1450_OrAbove || Terraria__1450_OrAbove || tModLoader_1450_OrAbove
#else
var vTextLowered = startDedInputCallBack.Body.Variables[1];
#endif
if (vText.VariableType.FullName != modder.Module.TypeSystem.String.FullName)
throw new NotSupportedException("Expected the first variable to be string");
#if TerrariaServer_1450_OrAbove || Terraria__1450_OrAbove || tModLoader_1450_OrAbove
#else
if (vTextLowered.VariableType.FullName != modder.Module.TypeSystem.String.FullName)
throw new NotSupportedException("Expected the second variable to be string");
#endif
var exceptionHandler = startDedInputCallBack.Body.ExceptionHandlers.Single(
x => (
x.TryStart.Next.OpCode == OpCodes.Ldstr
&& x.TryStart.Next.Operand.Equals("CLI.Help_Command")
) || (
x.TryStart.OpCode == OpCodes.Ldstr
&& x.TryStart.Operand.Equals("CLI.Help_Command")
)
);
startDedInputCallBack.Goto(exceptionHandler.TryStart, MoveType.Before);
startDedInputCallBack.Emit(OpCodes.Ldloc, vText);
var newStart = startDedInputCallBack.Instrs[startDedInputCallBack.Index - 1];
exceptionHandler.TryStart.ReplaceTransfer(newStart, startDedInputCallBack.Method);
#if TerrariaServer_1450_OrAbove || Terraria__1450_OrAbove || tModLoader_1450_OrAbove
startDedInputCallBack.EmitDelegate<Func<string, bool>>(OTAPI.Hooks.Main.InvokeCommandProcess);
#else
startDedInputCallBack.Emit(OpCodes.Ldloc, vTextLowered)
.EmitDelegate<Func<string, string, bool>>(OTAPI.Hooks.Main.InvokeCommandProcess);
#endif
startDedInputCallBack.Emit(OpCodes.Brfalse, exceptionHandler.TryEnd.Previous);
}
namespace OTAPI
{
public static partial class Hooks
{
public static partial class Main
{
public class CommandProcessEventArgs : EventArgs
{
public HookResult? Result { get; set; }
public string Command { get; set; }
public string Lowered { get; set; }
}
public static event EventHandler<CommandProcessEventArgs> CommandProcess;
public static bool InvokeCommandProcess(string lowered, string raw)
{
var args = new CommandProcessEventArgs()
{
Lowered = lowered,
Command = raw,
};
CommandProcess?.Invoke(null, args);
return args.Result != HookResult.Cancel;
}
public static bool InvokeCommandProcess(string raw)
{
var args = new CommandProcessEventArgs()
{
Lowered = raw.ToLower(),
Command = raw,
};
CommandProcess?.Invoke(null, args);
return args.Result != HookResult.Cancel;
}
}
}
}
#endif