Skip to content

Commit 2a10436

Browse files
Merge branch 'devel'
2 parents 5aab184 + 1c5860a commit 2a10436

25 files changed

Lines changed: 1752 additions & 405 deletions

HookAttribute/HookAttribute.cs

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,10 @@
1-
using System;
2-
using System.Collections.Generic;
3-
using System.Linq;
4-
using System.Text;
5-
using System.Threading.Tasks;
6-
7-
public class HookAttribute : Attribute
8-
{
9-
private string _fullName;
10-
public HookAttribute(string fullName, bool end = false)
11-
{
12-
_fullName = fullName;
13-
}
1+
using System;
2+
3+
4+
public class HookAttribute : Attribute
5+
{
6+
private string _fullName;
7+
public HookAttribute(string fullName, bool end = false) {
8+
_fullName = fullName;
9+
}
1410
}

HookAttribute/HookAttribute.csproj

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
<AppDesignerFolder>Properties</AppDesignerFolder>
1010
<RootNamespace>HookAttribute</RootNamespace>
1111
<AssemblyName>HookAttribute</AssemblyName>
12-
<TargetFrameworkVersion>v4.5.2</TargetFrameworkVersion>
1312
<FileAlignment>512</FileAlignment>
1413
</PropertyGroup>
1514
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
Binary file not shown.
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
6f051f3773f28dd737e06e6fa081d55ae6f23112
1+
da346672aca068797a3a3ea91e844556111b0f4a

HookAttribute/obj/Release/HookAttribute.csproj.FileListAbsolute.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,9 @@ F:/UnityHooks/HookAttribute/obj/Release/HookAttribute.pdb
1515
/home/dbk/Documents/Git/UniversalUnityHooks/HookAttribute/obj/Release/HookAttribute.csproj.CoreCompileInputs.cache
1616
/home/dbk/Documents/Git/UniversalUnityHooks/HookAttribute/obj/Release/HookAttribute.dll
1717
/home/dbk/Documents/Git/UniversalUnityHooks/HookAttribute/obj/Release/HookAttribute.pdb
18+
/home/lewis/Documents/Git/UniversalUnityHooks/Output/HookAttribute.dll
19+
/home/lewis/Documents/Git/UniversalUnityHooks/Output/HookAttribute.pdb
20+
/home/lewis/Documents/Git/UniversalUnityHooks/HookAttribute/obj/Release/HookAttribute.csprojResolveAssemblyReference.cache
21+
/home/lewis/Documents/Git/UniversalUnityHooks/HookAttribute/obj/Release/HookAttribute.csproj.CoreCompileInputs.cache
22+
/home/lewis/Documents/Git/UniversalUnityHooks/HookAttribute/obj/Release/HookAttribute.dll
23+
/home/lewis/Documents/Git/UniversalUnityHooks/HookAttribute/obj/Release/HookAttribute.pdb
Binary file not shown.
0 Bytes
Binary file not shown.
-84 Bytes
Binary file not shown.

HooksInjector/HooksInjector.csproj

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
<OutputType>Exe</OutputType>
99
<RootNamespace>HooksInjector</RootNamespace>
1010
<AssemblyName>HooksInjector</AssemblyName>
11-
<TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
1211
<FileAlignment>512</FileAlignment>
1312
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
1413
<TargetFrameworkProfile />
@@ -51,33 +50,36 @@
5150
<Reference Include="HookAttribute">
5251
<HintPath>..\Output\HookAttribute.dll</HintPath>
5352
</Reference>
54-
<Reference Include="Mono.Cecil, Version=0.9.6.0, Culture=neutral, PublicKeyToken=0738eb9f132ed756, processorArchitecture=MSIL">
55-
<SpecificVersion>False</SpecificVersion>
56-
<HintPath>..\Mono.Cecil\Mono.Cecil.dll</HintPath>
57-
</Reference>
5853
<Reference Include="Mono.Cecil.Inject, Version=1.2.6.0, Culture=neutral, processorArchitecture=MSIL">
5954
<SpecificVersion>False</SpecificVersion>
6055
<HintPath>..\Mono.Cecil\Mono.Cecil.Inject.dll</HintPath>
6156
</Reference>
62-
<Reference Include="Mono.Cecil.Mdb, Version=0.9.6.0, Culture=neutral, PublicKeyToken=0738eb9f132ed756, processorArchitecture=MSIL">
63-
<SpecificVersion>False</SpecificVersion>
64-
<HintPath>..\Mono.Cecil\Mono.Cecil.Mdb.dll</HintPath>
65-
</Reference>
66-
<Reference Include="Mono.Cecil.Pdb, Version=0.9.6.0, Culture=neutral, PublicKeyToken=0738eb9f132ed756, processorArchitecture=MSIL">
67-
<SpecificVersion>False</SpecificVersion>
68-
<HintPath>..\Mono.Cecil\Mono.Cecil.Pdb.dll</HintPath>
69-
</Reference>
70-
<Reference Include="Mono.Cecil.Rocks, Version=0.9.6.0, Culture=neutral, PublicKeyToken=0738eb9f132ed756, processorArchitecture=MSIL">
71-
<SpecificVersion>False</SpecificVersion>
72-
<HintPath>..\Mono.Cecil\Mono.Cecil.Rocks.dll</HintPath>
73-
</Reference>
7457
<Reference Include="System.Xml.Linq" />
7558
<Reference Include="System.Data.DataSetExtensions" />
7659
<Reference Include="Microsoft.CSharp" />
7760
<Reference Include="System.Data" />
7861
<Reference Include="System.Xml" />
7962
<Reference Include="System.Core" />
8063
<Reference Include="System" />
64+
<Reference Include="System.Drawing" />
65+
<Reference Include="System.IO.Compression.FileSystem" />
66+
<Reference Include="System.Numerics" />
67+
<Reference Include="System.Runtime.Serialization" />
68+
<Reference Include="CommandLine">
69+
<HintPath>..\packages\CommandLineParser.1.9.71\lib\net40\CommandLine.dll</HintPath>
70+
</Reference>
71+
<Reference Include="Mono.Cecil">
72+
<HintPath>..\packages\Mono.Cecil.0.9.6.4\lib\net40\Mono.Cecil.dll</HintPath>
73+
</Reference>
74+
<Reference Include="Mono.Cecil.Mdb">
75+
<HintPath>..\packages\Mono.Cecil.0.9.6.4\lib\net40\Mono.Cecil.Mdb.dll</HintPath>
76+
</Reference>
77+
<Reference Include="Mono.Cecil.Pdb">
78+
<HintPath>..\packages\Mono.Cecil.0.9.6.4\lib\net40\Mono.Cecil.Pdb.dll</HintPath>
79+
</Reference>
80+
<Reference Include="Mono.Cecil.Rocks">
81+
<HintPath>..\packages\Mono.Cecil.0.9.6.4\lib\net40\Mono.Cecil.Rocks.dll</HintPath>
82+
</Reference>
8183
</ItemGroup>
8284
<ItemGroup>
8385
<Compile Include="Injector.cs" />

HooksInjector/Injector.cs

Lines changed: 91 additions & 109 deletions
Original file line numberDiff line numberDiff line change
@@ -1,109 +1,91 @@
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

Comments
 (0)