|
1 | | -using Mono.Cecil; |
2 | | -using Mono.Cecil.Inject; |
3 | | -using System; |
4 | | -using System.Collections.Generic; |
5 | | -using System.IO; |
6 | | -using System.Linq; |
7 | | -using System.Text; |
8 | | - |
9 | | -namespace HooksInjector |
10 | | -{ |
11 | | - class Injector |
12 | | - { |
13 | | - private string _pluginPath; |
14 | | - private AssemblyDefinition _gameAssembly; |
15 | | - private AssemblyDefinition _pluginAssembly; |
16 | | - public Injector(AssemblyDefinition gameAssembly, AssemblyDefinition pluginAssembly, string pluginPath) |
17 | | - { |
18 | | - _pluginPath = pluginPath; |
19 | | - _gameAssembly = gameAssembly; |
20 | | - _pluginAssembly = pluginAssembly; |
21 | | - } |
22 | | - |
23 | | - public void InjectHook(ScriptsParser.ParsedHook hook) |
24 | | - { |
25 | | - var nameSplit = hook.fullName.Split('.'); |
26 | | - var className = nameSplit[0]; |
27 | | - var methodName = nameSplit[1]; |
28 | | - |
29 | | - var classType = _gameAssembly.MainModule.GetType(className); |
30 | | - if(classType == null) |
31 | | - { |
32 | | - Console.WriteLine(className + " class not found in game assembly!"); |
33 | | - Console.ReadLine(); |
34 | | - return; |
35 | | - } |
36 | | - |
37 | | - var method = classType.GetMethod(methodName); |
38 | | - |
39 | | - if (method == null) |
40 | | - { |
41 | | - Console.WriteLine(methodName + " method not found in " + className + "!"); |
42 | | - Console.ReadLine(); |
43 | | - return; |
44 | | - } |
45 | | - |
46 | | - TypeDefinition pluginClassType = null; |
47 | | - foreach(var type in _pluginAssembly.MainModule.GetTypes()) |
48 | | - { |
49 | | - if(type.Name.EndsWith("Plugin")) |
50 | | - { |
51 | | - pluginClassType = type; |
52 | | - } |
53 | | - } |
54 | | - |
55 | | - if(pluginClassType == null) |
56 | | - { |
57 | | - Console.WriteLine("No plugin class ending with \"Plugin\" found in " + _pluginPath + "!"); |
58 | | - Console.ReadLine(); |
59 | | - return; |
60 | | - } |
61 | | - |
62 | | - var rawMethodName = hook.fullName.Split('.').Last(); |
63 | | - var hookMethod = pluginClassType.GetMethod(rawMethodName); |
64 | | - |
65 | | - if (hookMethod == null) |
66 | | - { |
67 | | - Console.WriteLine(pluginClassType.Name + " doesn't contain method: " + rawMethodName); |
68 | | - Console.ReadLine(); |
69 | | - return; |
70 | | - } |
71 | | - |
72 | | - InjectionDefinition injector; |
73 | | - |
74 | | - try |
75 | | - { |
76 | | - if (hook.canBlock) |
77 | | - { |
78 | | - if(method.Parameters.Count > 0) |
79 | | - injector = new InjectionDefinition(method, hookMethod, InjectFlags.PassInvokingInstance | InjectFlags.PassParametersRef | InjectFlags.ModifyReturn); |
80 | | - else |
81 | | - injector = new InjectionDefinition(method, hookMethod, InjectFlags.PassInvokingInstance | InjectFlags.ModifyReturn); |
82 | | - } |
83 | | - else |
84 | | - { |
85 | | - if (method.Parameters.Count > 0) |
86 | | - injector = new InjectionDefinition(method, hookMethod, InjectFlags.PassInvokingInstance | InjectFlags.PassParametersRef); |
87 | | - else |
88 | | - injector = new InjectionDefinition(method, hookMethod, InjectFlags.PassInvokingInstance); |
89 | | - } |
90 | | - |
91 | | - if (hook.hookEnd) |
92 | | - { |
93 | | - injector.Inject(-1, null, InjectDirection.Before); |
94 | | - } |
95 | | - else |
96 | | - injector.Inject(); |
97 | | - |
98 | | - Console.WriteLine(rawMethodName + " hooked!"); |
99 | | - } |
100 | | - catch(Exception e) |
101 | | - { |
102 | | - Console.WriteLine("Hook definition is wrong!"); |
103 | | - Console.WriteLine(e.ToString()); |
104 | | - Console.ReadLine(); |
105 | | - return; |
106 | | - } |
107 | | - } |
108 | | - } |
109 | | -} |
| 1 | +using Mono.Cecil; |
| 2 | +using Mono.Cecil.Inject; |
| 3 | +using System; |
| 4 | +using System.Collections.Generic; |
| 5 | +using System.IO; |
| 6 | +using System.Linq; |
| 7 | +using System.Text; |
| 8 | + |
| 9 | +namespace HooksInjector |
| 10 | +{ |
| 11 | + class Injector |
| 12 | + { |
| 13 | + string pluginPath; |
| 14 | + AssemblyDefinition gameAssembly; |
| 15 | + AssemblyDefinition pluginAssembly; |
| 16 | + public Injector(AssemblyDefinition _gameAssembly, AssemblyDefinition _pluginAssembly, string _pluginPath) { |
| 17 | + pluginPath = _pluginPath; |
| 18 | + gameAssembly = _gameAssembly; |
| 19 | + pluginAssembly = _pluginAssembly; |
| 20 | + |
| 21 | + } |
| 22 | + public void InjectHook(ScriptsParser.ParsedHook hook) { |
| 23 | + var nameSplit = hook.fullName.Split('.'); |
| 24 | + var className = nameSplit[0]; |
| 25 | + var methodName = nameSplit[1]; |
| 26 | + |
| 27 | + var methodClassType = gameAssembly.MainModule.GetType(className); |
| 28 | + if (methodClassType == null) { |
| 29 | + Console.WriteLine("HooksInjector: ERROR: Class " + className + " Was not found in game assembly. Please check the spelling of the class."); |
| 30 | + Console.ReadLine(); |
| 31 | + return; |
| 32 | + } |
| 33 | + |
| 34 | + var method = methodClassType.GetMethod(methodName); |
| 35 | + |
| 36 | + if (method == null) { |
| 37 | + Console.WriteLine("HooksInjector: ERROR: Method " + methodName + " could not be found in class: " + className + ". Please check the spelling of the method."); |
| 38 | + Console.Read(); |
| 39 | + return; |
| 40 | + |
| 41 | + } |
| 42 | + TypeDefinition classType = null; |
| 43 | + foreach (var type in pluginAssembly.MainModule.GetTypes()) { |
| 44 | + if (type.Name.EndsWith("Plugin", StringComparison.CurrentCulture)) { |
| 45 | + classType = type; |
| 46 | + } |
| 47 | + } |
| 48 | + if (classType == null) { |
| 49 | + Console.WriteLine("HooksInjector: ERROR: No class ending with \"Plugin\" found in " + pluginPath); |
| 50 | + Console.Read(); |
| 51 | + return; |
| 52 | + } |
| 53 | + var rawmethodName = hook.fullName.Split('.').Last(); |
| 54 | + var hookMethod = classType.GetMethod(methodName); |
| 55 | + |
| 56 | + if (hookMethod == null) { |
| 57 | + Console.WriteLine("HooksInjector: ERROR: Method " + rawmethodName + " Not found in class " + className); |
| 58 | + Console.ReadLine(); |
| 59 | + return; |
| 60 | + } |
| 61 | + InjectionDefinition injector; |
| 62 | + |
| 63 | + try { |
| 64 | + if (hook.canBlock) { |
| 65 | + if (method.Parameters.Count > 0) |
| 66 | + injector = new InjectionDefinition(method, hookMethod, InjectFlags.PassInvokingInstance | InjectFlags.PassParametersRef | InjectFlags.ModifyReturn); |
| 67 | + else |
| 68 | + injector = new InjectionDefinition(method, hookMethod, InjectFlags.PassInvokingInstance | InjectFlags.ModifyReturn); |
| 69 | + } |
| 70 | + else { |
| 71 | + if (method.Parameters.Count > 0) |
| 72 | + injector = new InjectionDefinition(method, hookMethod, InjectFlags.PassInvokingInstance | InjectFlags.PassParametersRef); |
| 73 | + else |
| 74 | + injector = new InjectionDefinition(method, hookMethod, InjectFlags.PassInvokingInstance); |
| 75 | + } |
| 76 | + |
| 77 | + if (hook.hookEnd) { |
| 78 | + injector.Inject(-1, null, InjectDirection.Before); |
| 79 | + } |
| 80 | + else |
| 81 | + injector.Inject(); |
| 82 | + Console.WriteLine("HooksInjector: Hooked " + rawmethodName + "."); |
| 83 | + } |
| 84 | + catch (Exception e) { |
| 85 | + Console.WriteLine("HooksInjector: ERROR: " + e.ToString() + " Hook definition is probably wrong."); |
| 86 | + Console.ReadLine(); |
| 87 | + return; |
| 88 | + } |
| 89 | + } |
| 90 | + } |
| 91 | +} |
0 commit comments