Skip to content

Commit

Permalink
Merge branch 'devel'
Browse files Browse the repository at this point in the history
  • Loading branch information
lewisdoesstuff committed Oct 5, 2017
2 parents 5aab184 + 1c5860a commit 2a10436
Show file tree
Hide file tree
Showing 25 changed files with 1,752 additions and 405 deletions.
22 changes: 9 additions & 13 deletions HookAttribute/HookAttribute.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

public class HookAttribute : Attribute
{
private string _fullName;
public HookAttribute(string fullName, bool end = false)
{
_fullName = fullName;
}
using System;


public class HookAttribute : Attribute
{
private string _fullName;
public HookAttribute(string fullName, bool end = false) {
_fullName = fullName;
}
}
1 change: 0 additions & 1 deletion HookAttribute/HookAttribute.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>HookAttribute</RootNamespace>
<AssemblyName>HookAttribute</AssemblyName>
<TargetFrameworkVersion>v4.5.2</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
Expand Down
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -1 +1 @@
6f051f3773f28dd737e06e6fa081d55ae6f23112
da346672aca068797a3a3ea91e844556111b0f4a
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,9 @@ F:/UnityHooks/HookAttribute/obj/Release/HookAttribute.pdb
/home/dbk/Documents/Git/UniversalUnityHooks/HookAttribute/obj/Release/HookAttribute.csproj.CoreCompileInputs.cache
/home/dbk/Documents/Git/UniversalUnityHooks/HookAttribute/obj/Release/HookAttribute.dll
/home/dbk/Documents/Git/UniversalUnityHooks/HookAttribute/obj/Release/HookAttribute.pdb
/home/lewis/Documents/Git/UniversalUnityHooks/Output/HookAttribute.dll
/home/lewis/Documents/Git/UniversalUnityHooks/Output/HookAttribute.pdb
/home/lewis/Documents/Git/UniversalUnityHooks/HookAttribute/obj/Release/HookAttribute.csprojResolveAssemblyReference.cache
/home/lewis/Documents/Git/UniversalUnityHooks/HookAttribute/obj/Release/HookAttribute.csproj.CoreCompileInputs.cache
/home/lewis/Documents/Git/UniversalUnityHooks/HookAttribute/obj/Release/HookAttribute.dll
/home/lewis/Documents/Git/UniversalUnityHooks/HookAttribute/obj/Release/HookAttribute.pdb
Binary file not shown.
Binary file modified HookAttribute/obj/Release/HookAttribute.dll
Binary file not shown.
Binary file modified HookAttribute/obj/Release/HookAttribute.pdb
Binary file not shown.
36 changes: 19 additions & 17 deletions HooksInjector/HooksInjector.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
<OutputType>Exe</OutputType>
<RootNamespace>HooksInjector</RootNamespace>
<AssemblyName>HooksInjector</AssemblyName>
<TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
<TargetFrameworkProfile />
Expand Down Expand Up @@ -51,33 +50,36 @@
<Reference Include="HookAttribute">
<HintPath>..\Output\HookAttribute.dll</HintPath>
</Reference>
<Reference Include="Mono.Cecil, Version=0.9.6.0, Culture=neutral, PublicKeyToken=0738eb9f132ed756, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\Mono.Cecil\Mono.Cecil.dll</HintPath>
</Reference>
<Reference Include="Mono.Cecil.Inject, Version=1.2.6.0, Culture=neutral, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\Mono.Cecil\Mono.Cecil.Inject.dll</HintPath>
</Reference>
<Reference Include="Mono.Cecil.Mdb, Version=0.9.6.0, Culture=neutral, PublicKeyToken=0738eb9f132ed756, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\Mono.Cecil\Mono.Cecil.Mdb.dll</HintPath>
</Reference>
<Reference Include="Mono.Cecil.Pdb, Version=0.9.6.0, Culture=neutral, PublicKeyToken=0738eb9f132ed756, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\Mono.Cecil\Mono.Cecil.Pdb.dll</HintPath>
</Reference>
<Reference Include="Mono.Cecil.Rocks, Version=0.9.6.0, Culture=neutral, PublicKeyToken=0738eb9f132ed756, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\Mono.Cecil\Mono.Cecil.Rocks.dll</HintPath>
</Reference>
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" />
<Reference Include="System.Xml" />
<Reference Include="System.Core" />
<Reference Include="System" />
<Reference Include="System.Drawing" />
<Reference Include="System.IO.Compression.FileSystem" />
<Reference Include="System.Numerics" />
<Reference Include="System.Runtime.Serialization" />
<Reference Include="CommandLine">
<HintPath>..\packages\CommandLineParser.1.9.71\lib\net40\CommandLine.dll</HintPath>
</Reference>
<Reference Include="Mono.Cecil">
<HintPath>..\packages\Mono.Cecil.0.9.6.4\lib\net40\Mono.Cecil.dll</HintPath>
</Reference>
<Reference Include="Mono.Cecil.Mdb">
<HintPath>..\packages\Mono.Cecil.0.9.6.4\lib\net40\Mono.Cecil.Mdb.dll</HintPath>
</Reference>
<Reference Include="Mono.Cecil.Pdb">
<HintPath>..\packages\Mono.Cecil.0.9.6.4\lib\net40\Mono.Cecil.Pdb.dll</HintPath>
</Reference>
<Reference Include="Mono.Cecil.Rocks">
<HintPath>..\packages\Mono.Cecil.0.9.6.4\lib\net40\Mono.Cecil.Rocks.dll</HintPath>
</Reference>
</ItemGroup>
<ItemGroup>
<Compile Include="Injector.cs" />
Expand Down
200 changes: 91 additions & 109 deletions HooksInjector/Injector.cs
Original file line number Diff line number Diff line change
@@ -1,109 +1,91 @@
using Mono.Cecil;
using Mono.Cecil.Inject;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;

namespace HooksInjector
{
class Injector
{
private string _pluginPath;
private AssemblyDefinition _gameAssembly;
private AssemblyDefinition _pluginAssembly;
public Injector(AssemblyDefinition gameAssembly, AssemblyDefinition pluginAssembly, string pluginPath)
{
_pluginPath = pluginPath;
_gameAssembly = gameAssembly;
_pluginAssembly = pluginAssembly;
}

public void InjectHook(ScriptsParser.ParsedHook hook)
{
var nameSplit = hook.fullName.Split('.');
var className = nameSplit[0];
var methodName = nameSplit[1];

var classType = _gameAssembly.MainModule.GetType(className);
if(classType == null)
{
Console.WriteLine(className + " class not found in game assembly!");
Console.ReadLine();
return;
}

var method = classType.GetMethod(methodName);

if (method == null)
{
Console.WriteLine(methodName + " method not found in " + className + "!");
Console.ReadLine();
return;
}

TypeDefinition pluginClassType = null;
foreach(var type in _pluginAssembly.MainModule.GetTypes())
{
if(type.Name.EndsWith("Plugin"))
{
pluginClassType = type;
}
}

if(pluginClassType == null)
{
Console.WriteLine("No plugin class ending with \"Plugin\" found in " + _pluginPath + "!");
Console.ReadLine();
return;
}

var rawMethodName = hook.fullName.Split('.').Last();
var hookMethod = pluginClassType.GetMethod(rawMethodName);

if (hookMethod == null)
{
Console.WriteLine(pluginClassType.Name + " doesn't contain method: " + rawMethodName);
Console.ReadLine();
return;
}

InjectionDefinition injector;

try
{
if (hook.canBlock)
{
if(method.Parameters.Count > 0)
injector = new InjectionDefinition(method, hookMethod, InjectFlags.PassInvokingInstance | InjectFlags.PassParametersRef | InjectFlags.ModifyReturn);
else
injector = new InjectionDefinition(method, hookMethod, InjectFlags.PassInvokingInstance | InjectFlags.ModifyReturn);
}
else
{
if (method.Parameters.Count > 0)
injector = new InjectionDefinition(method, hookMethod, InjectFlags.PassInvokingInstance | InjectFlags.PassParametersRef);
else
injector = new InjectionDefinition(method, hookMethod, InjectFlags.PassInvokingInstance);
}

if (hook.hookEnd)
{
injector.Inject(-1, null, InjectDirection.Before);
}
else
injector.Inject();

Console.WriteLine(rawMethodName + " hooked!");
}
catch(Exception e)
{
Console.WriteLine("Hook definition is wrong!");
Console.WriteLine(e.ToString());
Console.ReadLine();
return;
}
}
}
}
using Mono.Cecil;
using Mono.Cecil.Inject;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;

namespace HooksInjector
{
class Injector
{
string pluginPath;
AssemblyDefinition gameAssembly;
AssemblyDefinition pluginAssembly;
public Injector(AssemblyDefinition _gameAssembly, AssemblyDefinition _pluginAssembly, string _pluginPath) {
pluginPath = _pluginPath;
gameAssembly = _gameAssembly;
pluginAssembly = _pluginAssembly;

}
public void InjectHook(ScriptsParser.ParsedHook hook) {
var nameSplit = hook.fullName.Split('.');
var className = nameSplit[0];
var methodName = nameSplit[1];

var methodClassType = gameAssembly.MainModule.GetType(className);
if (methodClassType == null) {
Console.WriteLine("HooksInjector: ERROR: Class " + className + " Was not found in game assembly. Please check the spelling of the class.");
Console.ReadLine();
return;
}

var method = methodClassType.GetMethod(methodName);

if (method == null) {
Console.WriteLine("HooksInjector: ERROR: Method " + methodName + " could not be found in class: " + className + ". Please check the spelling of the method.");
Console.Read();
return;

}
TypeDefinition classType = null;
foreach (var type in pluginAssembly.MainModule.GetTypes()) {
if (type.Name.EndsWith("Plugin", StringComparison.CurrentCulture)) {
classType = type;
}
}
if (classType == null) {
Console.WriteLine("HooksInjector: ERROR: No class ending with \"Plugin\" found in " + pluginPath);
Console.Read();
return;
}
var rawmethodName = hook.fullName.Split('.').Last();
var hookMethod = classType.GetMethod(methodName);

if (hookMethod == null) {
Console.WriteLine("HooksInjector: ERROR: Method " + rawmethodName + " Not found in class " + className);
Console.ReadLine();
return;
}
InjectionDefinition injector;

try {
if (hook.canBlock) {
if (method.Parameters.Count > 0)
injector = new InjectionDefinition(method, hookMethod, InjectFlags.PassInvokingInstance | InjectFlags.PassParametersRef | InjectFlags.ModifyReturn);
else
injector = new InjectionDefinition(method, hookMethod, InjectFlags.PassInvokingInstance | InjectFlags.ModifyReturn);
}
else {
if (method.Parameters.Count > 0)
injector = new InjectionDefinition(method, hookMethod, InjectFlags.PassInvokingInstance | InjectFlags.PassParametersRef);
else
injector = new InjectionDefinition(method, hookMethod, InjectFlags.PassInvokingInstance);
}

if (hook.hookEnd) {
injector.Inject(-1, null, InjectDirection.Before);
}
else
injector.Inject();
Console.WriteLine("HooksInjector: Hooked " + rawmethodName + ".");
}
catch (Exception e) {
Console.WriteLine("HooksInjector: ERROR: " + e.ToString() + " Hook definition is probably wrong.");
Console.ReadLine();
return;
}
}
}
}
Loading

0 comments on commit 2a10436

Please sign in to comment.