Skip to content

Commit

Permalink
Bug fix release 1.0.4
Browse files Browse the repository at this point in the history
  • Loading branch information
pardeike committed Jan 17, 2017
1 parent b875090 commit e2de947
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 33 deletions.
7 changes: 4 additions & 3 deletions Harmony/Harmony.csproj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
Expand All @@ -20,7 +20,7 @@
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<DefineConstants>TRACE;DEBUG</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
Expand Down Expand Up @@ -64,7 +64,8 @@
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<PropertyGroup>
<PostBuildEvent>if exist "$(TargetDir)$(TargetName).pdb" "C:\Program Files (x86)\Mono\bin\mono.exe" "C:\Program Files (x86)\Mono\lib\mono\4.5\pdb2mdb.exe" "$(TargetPath)"</PostBuildEvent>
<PostBuildEvent>
</PostBuildEvent>
</PropertyGroup>
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
Expand Down
5 changes: 5 additions & 0 deletions Harmony/HookInjector.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Text;

Expand All @@ -25,6 +26,10 @@ int prefixBytes

public static HookInjector Create(MethodBase sourceMethod, MethodBase targetMethod = null)
{
RuntimeHelpers.PrepareMethod(sourceMethod.MethodHandle);
if (targetMethod != null)
RuntimeHelpers.PrepareMethod(targetMethod.MethodHandle);

var injector = new HookInjector();
injector.sourcePtr = sourceMethod.MethodHandle.GetFunctionPointer();
injector.targetPtr = targetMethod == null ? IntPtr.Zero : targetMethod.MethodHandle.GetFunctionPointer();
Expand Down
27 changes: 21 additions & 6 deletions Harmony/PatchFunctions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,22 @@
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Reflection.Emit;

namespace Harmony
{
public static class PatchFunctions
{
// this holds all our methods alive so they don't get garbage-collected
static PatchStorage[] allPatchReferences = new PatchStorage[0];
class PatchStorage
{
public DynamicMethod copy;
public MethodInfo copyDelegate;
public DynamicMethod wrapper;
public MethodInfo wrapperDelegate;
}

public static PatchInfo GetPatchInfo(MethodInfo original)
{
var bytes = HookInjector.Create(original).GetPayload();
Expand Down Expand Up @@ -63,14 +74,18 @@ public static void UpdateWrapper(MethodInfo original, PatchInfo patchInfo, bool
var sortedPrefixes = GetSortedPatchMethods(patchInfo.prefixes);
var sortedPostfixes = GetSortedPatchMethods(patchInfo.postfixes);

var copy = PatchTools.CreateMethodCopy(original);
if (copy == null) throw new MissingMethodException("Cannot create copy of " + original);
var copyDelegate = PatchTools.PrepareDynamicMethod(original, copy);
var patch = new PatchStorage();

patch.copy = PatchTools.CreateMethodCopy(original);
if (patch.copy == null) throw new MissingMethodException("Cannot create copy of " + original);
patch.copyDelegate = PatchTools.PrepareDynamicMethod(original, patch.copy);

patch.wrapper = PatchTools.CreatePatchWrapper(original, patch.copyDelegate, sortedPrefixes, sortedPostfixes);
patch.wrapperDelegate = PatchTools.PrepareDynamicMethod(original, patch.wrapper);

var wrapper = PatchTools.CreatePatchWrapper(original, copyDelegate, sortedPrefixes, sortedPostfixes);
var wrapperDelegate = PatchTools.PrepareDynamicMethod(original, wrapper);
allPatchReferences.Add(patch); // keep things alive and referenced

var injector = HookInjector.Create(original, wrapperDelegate);
var injector = HookInjector.Create(original, patch.wrapperDelegate);
injector.Detour(patchInfo.Serialize(), isNew);
}
}
Expand Down
16 changes: 12 additions & 4 deletions Harmony/Platform.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using System;
using System.Reflection;
using System.Reflection.Emit;
using System.Runtime.InteropServices;
using System.Runtime.CompilerServices;

namespace Harmony
{
Expand Down Expand Up @@ -100,6 +100,9 @@ public static unsafe long WriteLong(long memory, long value)
return memory + sizeof(long);
}

// this holds all our methods alive so they don't get garbage-collected
static MethodInfo[] allMethodReferences = new MethodInfo[0];

// purpose of this method is to "do some work" so the JIT compiler
// does not optimize our code away. since it is never called with 0
// it actually does nothing
Expand Down Expand Up @@ -130,9 +133,14 @@ public static long GetMemory(int size)
}
il.Emit(OpCodes.Ret);
var type = typeBuilder.CreateType();
var m = type.GetMethod(methodName);
m.Invoke(null, new object[] { }); // make sure it is JIT-compiled
return m.MethodHandle.GetFunctionPointer().ToInt64();
var method = type.GetMethod(methodName);
allMethodReferences.Add(method);

// make sure it is JIT-compiled
RuntimeHelpers.PrepareMethod(method.MethodHandle);
method.Invoke(null, new object[] { });

return method.MethodHandle.GetFunctionPointer().ToInt64();
}
}
}
4 changes: 2 additions & 2 deletions Harmony/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,5 @@
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.3.0")]
[assembly: AssemblyFileVersion("1.0.3.0")]
[assembly: AssemblyVersion("1.0.4.0")]
[assembly: AssemblyFileVersion("1.0.4.0")]
30 changes: 14 additions & 16 deletions HarmonyNUnitTests/HarmonyNUnitTests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>HarmonyTests</RootNamespace>
<AssemblyName>HarmonyNUnitTests</AssemblyName>
<TargetFrameworkVersion>v2.0</TargetFrameworkVersion>
<TargetFrameworkVersion>v3.5</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<TargetFrameworkProfile />
<NuGetPackageImportStamp>
Expand Down Expand Up @@ -47,28 +47,24 @@
</ItemGroup>
<ItemGroup>
<Reference Include="Microsoft.VisualStudio.QualityTools.UnitTestFramework, Version=10.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL" />
<Reference Include="nunit.core, Version=2.6.4.14350, Culture=neutral, PublicKeyToken=96d09a1eb7f44a77, processorArchitecture=MSIL">
<HintPath>..\packages\NUnitTestAdapter.2.0.0\lib\nunit.core.dll</HintPath>
<Reference Include="nunit.core, Version=2.6.3.13283, Culture=neutral, PublicKeyToken=96d09a1eb7f44a77, processorArchitecture=MSIL">
<HintPath>..\packages\NUnitTestAdapter.1.2\lib\nunit.core.dll</HintPath>
<Private>False</Private>
</Reference>
<Reference Include="nunit.core.interfaces, Version=2.6.4.14350, Culture=neutral, PublicKeyToken=96d09a1eb7f44a77, processorArchitecture=MSIL">
<HintPath>..\packages\NUnitTestAdapter.2.0.0\lib\nunit.core.interfaces.dll</HintPath>
<Reference Include="nunit.core.interfaces, Version=2.6.3.13283, Culture=neutral, PublicKeyToken=96d09a1eb7f44a77, processorArchitecture=MSIL">
<HintPath>..\packages\NUnitTestAdapter.1.2\lib\nunit.core.interfaces.dll</HintPath>
<Private>False</Private>
</Reference>
<Reference Include="nunit.framework, Version=3.6.0.0, Culture=neutral, PublicKeyToken=2638cd05610744eb, processorArchitecture=MSIL">
<HintPath>..\packages\NUnit.3.6.0\lib\net20\nunit.framework.dll</HintPath>
<Reference Include="nunit.framework, Version=3.5.0.0, Culture=neutral, PublicKeyToken=2638cd05610744eb, processorArchitecture=MSIL">
<HintPath>..\packages\NUnit.3.5.0\lib\net35\nunit.framework.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="NUnit.System.Linq, Version=0.6.0.0, Culture=neutral, PublicKeyToken=2638cd05610744eb, processorArchitecture=MSIL">
<HintPath>..\packages\NUnit.3.6.0\lib\net20\NUnit.System.Linq.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="nunit.util, Version=2.6.4.14350, Culture=neutral, PublicKeyToken=96d09a1eb7f44a77, processorArchitecture=MSIL">
<HintPath>..\packages\NUnitTestAdapter.2.0.0\lib\nunit.util.dll</HintPath>
<Reference Include="nunit.util, Version=2.6.3.13283, Culture=neutral, PublicKeyToken=96d09a1eb7f44a77, processorArchitecture=MSIL">
<HintPath>..\packages\NUnitTestAdapter.1.2\lib\nunit.util.dll</HintPath>
<Private>False</Private>
</Reference>
<Reference Include="NUnit.VisualStudio.TestAdapter, Version=2.0.0.0, Culture=neutral, PublicKeyToken=4cb40d35494691ac, processorArchitecture=MSIL">
<HintPath>..\packages\NUnitTestAdapter.2.0.0\lib\NUnit.VisualStudio.TestAdapter.dll</HintPath>
<Reference Include="NUnit.VisualStudio.TestAdapter, Version=1.2.0.0, Culture=neutral, PublicKeyToken=4cb40d35494691ac, processorArchitecture=MSIL">
<HintPath>..\packages\NUnitTestAdapter.1.2\lib\NUnit.VisualStudio.TestAdapter.dll</HintPath>
<Private>False</Private>
</Reference>
<Reference Include="System" />
Expand All @@ -88,7 +84,9 @@
</ProjectReference>
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
<None Include="packages.config">
<SubType>Designer</SubType>
</None>
</ItemGroup>
<ItemGroup>
<Service Include="{82A7F48D-3B50-4B1E-B82E-3ADA8210C358}" />
Expand Down
4 changes: 2 additions & 2 deletions HarmonyNUnitTests/packages.config
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="NUnit" version="3.6.0" targetFramework="net20" />
<package id="NUnitTestAdapter" version="2.0.0" targetFramework="net20" />
<package id="NUnit" version="3.5.0" targetFramework="net35" />
<package id="NUnitTestAdapter" version="1.2" targetFramework="net35" />
</packages>

0 comments on commit e2de947

Please sign in to comment.