Navigation Menu

Skip to content

Commit

Permalink
Fixed referenced library preload
Browse files Browse the repository at this point in the history
  • Loading branch information
Bert-Proesmans committed Jun 6, 2017
1 parent b3f243c commit d348a1a
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 31 deletions.
1 change: 1 addition & 0 deletions HookRegistry/HookRegistry.csproj
Expand Up @@ -64,6 +64,7 @@
<Compile Include="src\packetdumper\DumpServer.cs" />
<Compile Include="src\packetdumper\Serializer.cs" />
<Compile Include="src\SSLDisable.cs" />
<Compile Include="src\ReferenceLoader.cs" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>
5 changes: 4 additions & 1 deletion HookRegistry/src/HookRegistry.cs
Expand Up @@ -52,9 +52,12 @@ public static HookRegistry Get()
// Initialise the assembly store.
_instance.Init();

// Setup all hook information
// Setup all hook information.
_instance.LoadRuntimeHooks();

// Pre-load necessary library files.
ReferenceLoader.Load();

try
{
// Test if this code is running within the Unity Engine
Expand Down
15 changes: 0 additions & 15 deletions HookRegistry/src/IncomingPackets.cs
Expand Up @@ -5,8 +5,6 @@
using HackstoneAnalyzer.PayloadFormat;
using Hooks.PacketDumper;
using System;
using static Google.Protobuf.WireFormat;
using System.Runtime.CompilerServices;

namespace Hooks
{
Expand All @@ -27,19 +25,6 @@ public IncomingPackets()

// Perform hooking administration.
HookRegistry.Register(OnCall);

ForceLoadReferences();
}

// Use types from dependancy, since it's not possible to detect referenced libraries
// without code needing them.
[MethodImpl(MethodImplOptions.NoOptimization)]
private void ForceLoadReferences()
{
#pragma warning disable CS0219 // Variable is assigned but its value is never used
WireType _nil = WireType.Fixed32;
ulong _magic = Util.MAGIC_V;
#pragma warning restore CS0219 // Variable is assigned but its value is never used
}

// Returns a list of methods (full names) that this hook expects.
Expand Down
15 changes: 0 additions & 15 deletions HookRegistry/src/OutgoingPackets.cs
Expand Up @@ -6,8 +6,6 @@
using HackstoneAnalyzer.PayloadFormat;
using Hooks.PacketDumper;
using System;
using static Google.Protobuf.WireFormat;
using System.Runtime.CompilerServices;

namespace Hooks
{
Expand All @@ -29,19 +27,6 @@ public OutgoingPackets()
// Perform hooking administration.
HookRegistry.Register(OnCall);
RegisterGenericDeclaringTypes();

ForceLoadReferences();
}

// Use types from dependancy, since it's not possible to detect referenced libraries
// without code needing them.
[MethodImpl(MethodImplOptions.NoOptimization)]
private void ForceLoadReferences()
{
#pragma warning disable CS0219 // Variable is assigned but its value is never used
WireType _nil = WireType.Fixed32;
ulong _magic = Util.MAGIC_V;
#pragma warning restore CS0219 // Variable is assigned but its value is never used
}

private void RegisterGenericDeclaringTypes()
Expand Down
44 changes: 44 additions & 0 deletions HookRegistry/src/ReferenceLoader.cs
@@ -0,0 +1,44 @@
using Google.Protobuf.Reflection;
using HackstoneAnalyzer.PayloadFormat;
using System;
using System.Collections.Generic;

namespace Hooks
{
static class ReferenceLoader
{
/*
* Referenced libraries are loaded by the .NET runtime when they are used.
*
* UnityHook loads this library file (HookRegistry.dll) and starts the initialisation procedure
* to validate it's contents.
* While doing that, all loaded libraries that were referenced during initialisation were recorded
* and they will be copied WITH HookRegistry.dll to the game library folder.
*
* The result is that the injected code won't throw an exception because of missing library files, since
* all code will eventually run from the library folder of the game!
*
* The `Load()` method will be called after initialisation of HookRegistry is complete.
*/

// Holds the types created by Load().
// These types are explicitly stored in a list to prevent the compiler optimize away our load calls.
static List<Type> _references;

public static void Load()
{
// Put here a type statement for each referenced library, which MUST BE COPIED together
// with HookRegistry.dll to the game's library folder.
// e.g. typeof(String);

_references = new List<Type>()
{
// Reference to PayloadFormat.dll
typeof(Handshake),

// Reference to Google.Protobuf.dll
typeof(MessageDescriptor),
};
}
}
}

0 comments on commit d348a1a

Please sign in to comment.