mirror of
https://github.com/Ryujinx/Opentk.git
synced 2025-01-26 22:00:58 +00:00
Implemented dll rewriter using Mono.Cecil
This commit is contained in:
parent
74159da199
commit
1868b5e3db
BIN
Source/OpenTK.Rewrite/Dependencies/IKVM.Reflection.dll
Normal file
BIN
Source/OpenTK.Rewrite/Dependencies/IKVM.Reflection.dll
Normal file
Binary file not shown.
BIN
Source/OpenTK.Rewrite/Dependencies/Mono.Cecil.Mdb.dll
Normal file
BIN
Source/OpenTK.Rewrite/Dependencies/Mono.Cecil.Mdb.dll
Normal file
Binary file not shown.
BIN
Source/OpenTK.Rewrite/Dependencies/Mono.Cecil.Pdb.dll
Normal file
BIN
Source/OpenTK.Rewrite/Dependencies/Mono.Cecil.Pdb.dll
Normal file
Binary file not shown.
BIN
Source/OpenTK.Rewrite/Dependencies/Mono.Cecil.dll
Normal file
BIN
Source/OpenTK.Rewrite/Dependencies/Mono.Cecil.dll
Normal file
Binary file not shown.
82
Source/OpenTK.Rewrite/OpenTK.Rewrite.csproj
Normal file
82
Source/OpenTK.Rewrite/OpenTK.Rewrite.csproj
Normal file
|
@ -0,0 +1,82 @@
|
||||||
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
|
<Project ToolsVersion="4.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>
|
||||||
|
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
|
||||||
|
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
|
||||||
|
<ProjectGuid>{C426C9D1-8857-4E52-BAC7-4C05EE6070AB}</ProjectGuid>
|
||||||
|
<OutputType>Exe</OutputType>
|
||||||
|
<AppDesignerFolder>Properties</AppDesignerFolder>
|
||||||
|
<RootNamespace>OpenTK.Rewrite</RootNamespace>
|
||||||
|
<AssemblyName>OpenTK.Rewrite</AssemblyName>
|
||||||
|
<TargetFrameworkVersion>v3.5</TargetFrameworkVersion>
|
||||||
|
<FileAlignment>512</FileAlignment>
|
||||||
|
</PropertyGroup>
|
||||||
|
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
|
||||||
|
<PlatformTarget>AnyCPU</PlatformTarget>
|
||||||
|
<DebugSymbols>true</DebugSymbols>
|
||||||
|
<DebugType>full</DebugType>
|
||||||
|
<Optimize>false</Optimize>
|
||||||
|
<OutputPath>..\..\Binaries\OpenTK\Debug\</OutputPath>
|
||||||
|
<DefineConstants>DEBUG;TRACE</DefineConstants>
|
||||||
|
<ErrorReport>prompt</ErrorReport>
|
||||||
|
<WarningLevel>4</WarningLevel>
|
||||||
|
</PropertyGroup>
|
||||||
|
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
|
||||||
|
<PlatformTarget>AnyCPU</PlatformTarget>
|
||||||
|
<DebugType>pdbonly</DebugType>
|
||||||
|
<Optimize>true</Optimize>
|
||||||
|
<OutputPath>..\..\Binaries\OpenTK\Release\</OutputPath>
|
||||||
|
<DefineConstants>TRACE</DefineConstants>
|
||||||
|
<ErrorReport>prompt</ErrorReport>
|
||||||
|
<WarningLevel>4</WarningLevel>
|
||||||
|
</PropertyGroup>
|
||||||
|
<PropertyGroup>
|
||||||
|
<SignAssembly>true</SignAssembly>
|
||||||
|
</PropertyGroup>
|
||||||
|
<PropertyGroup>
|
||||||
|
<AssemblyOriginatorKeyFile>..\..\OpenTK.snk</AssemblyOriginatorKeyFile>
|
||||||
|
</PropertyGroup>
|
||||||
|
<ItemGroup>
|
||||||
|
<Reference Include="Mono.Cecil, Version=0.9.5.0, Culture=neutral, PublicKeyToken=0738eb9f132ed756, processorArchitecture=MSIL">
|
||||||
|
<SpecificVersion>False</SpecificVersion>
|
||||||
|
<HintPath>Dependencies\Mono.Cecil.dll</HintPath>
|
||||||
|
</Reference>
|
||||||
|
<Reference Include="Mono.Cecil.Mdb, Version=0.9.5.0, Culture=neutral, PublicKeyToken=0738eb9f132ed756, processorArchitecture=MSIL">
|
||||||
|
<SpecificVersion>False</SpecificVersion>
|
||||||
|
<HintPath>Dependencies\Mono.Cecil.Mdb.dll</HintPath>
|
||||||
|
</Reference>
|
||||||
|
<Reference Include="Mono.Cecil.Pdb, Version=0.9.5.0, Culture=neutral, PublicKeyToken=0738eb9f132ed756, processorArchitecture=MSIL">
|
||||||
|
<SpecificVersion>False</SpecificVersion>
|
||||||
|
<HintPath>Dependencies\Mono.Cecil.Pdb.dll</HintPath>
|
||||||
|
</Reference>
|
||||||
|
<Reference Include="System" />
|
||||||
|
<Reference Include="System.Core" />
|
||||||
|
</ItemGroup>
|
||||||
|
<ItemGroup>
|
||||||
|
<Compile Include="Program.cs" />
|
||||||
|
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||||
|
</ItemGroup>
|
||||||
|
<ItemGroup>
|
||||||
|
<Content Include="Dependencies\Mono.Cecil.dll" />
|
||||||
|
<Content Include="Dependencies\Mono.Cecil.Mdb.dll" />
|
||||||
|
<Content Include="Dependencies\Mono.Cecil.Mdb.pdb" />
|
||||||
|
<Content Include="Dependencies\Mono.Cecil.pdb" />
|
||||||
|
<Content Include="Dependencies\Mono.Cecil.Pdb.dll" />
|
||||||
|
<Content Include="Dependencies\Mono.Cecil.Pdb.pdb" />
|
||||||
|
<Content Include="Dependencies\IKVM.Reflection.dll" />
|
||||||
|
</ItemGroup>
|
||||||
|
<ItemGroup>
|
||||||
|
<None Include="..\..\OpenTK.snk">
|
||||||
|
<Link>OpenTK.snk</Link>
|
||||||
|
</None>
|
||||||
|
</ItemGroup>
|
||||||
|
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
|
||||||
|
<!-- 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.
|
||||||
|
<Target Name="BeforeBuild">
|
||||||
|
</Target>
|
||||||
|
<Target Name="AfterBuild">
|
||||||
|
</Target>
|
||||||
|
-->
|
||||||
|
</Project>
|
150
Source/OpenTK.Rewrite/Program.cs
Normal file
150
Source/OpenTK.Rewrite/Program.cs
Normal file
|
@ -0,0 +1,150 @@
|
||||||
|
// OpenTK.Rewrite: IL rewriter for OpenTK.dll
|
||||||
|
// Copyright (C) 2013 Stefanos Apostolopoulos
|
||||||
|
//
|
||||||
|
// This program is free software: you can redistribute it and/or modify
|
||||||
|
// it under the terms of the GNU General Public License as published by
|
||||||
|
// the Free Software Foundation, either version 3 of the License, or
|
||||||
|
// (at your option) any later version.
|
||||||
|
//
|
||||||
|
// This program is distributed in the hope that it will be useful,
|
||||||
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
// GNU General Public License for more details.
|
||||||
|
//
|
||||||
|
// You should have received a copy of the GNU General Public License
|
||||||
|
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.IO;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
|
||||||
|
using Mono.Cecil;
|
||||||
|
using Mono.Cecil.Cil;
|
||||||
|
|
||||||
|
namespace OpenTK.Rewrite
|
||||||
|
{
|
||||||
|
// Replaces OpenTK.InteropHelper method instances
|
||||||
|
// with the s IL instructions.
|
||||||
|
class Program
|
||||||
|
{
|
||||||
|
static void Main(string[] args)
|
||||||
|
{
|
||||||
|
if (args.Length == 0)
|
||||||
|
{
|
||||||
|
Console.WriteLine("Usage: rewrite [file1] [file2] ...");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
var program = new Program();
|
||||||
|
foreach (var file in args)
|
||||||
|
{
|
||||||
|
program.Rewrite(file);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void Rewrite(string file)
|
||||||
|
{
|
||||||
|
var assembly = AssemblyDefinition.ReadAssembly(file);
|
||||||
|
foreach (var module in assembly.Modules)
|
||||||
|
{
|
||||||
|
foreach (var reference in module.AssemblyReferences)
|
||||||
|
{
|
||||||
|
module.AssemblyResolver.Resolve(reference);
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach (var type in module.Types)
|
||||||
|
{
|
||||||
|
Rewrite(type);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
var fs = new FileStream("../../../OpenTK.snk", FileMode.Open);
|
||||||
|
var keypair = new System.Reflection.StrongNameKeyPair(fs);
|
||||||
|
fs.Close();
|
||||||
|
assembly.Write(
|
||||||
|
file,
|
||||||
|
new WriterParameters
|
||||||
|
{
|
||||||
|
StrongNameKeyPair = keypair
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
void Rewrite(TypeDefinition type)
|
||||||
|
{
|
||||||
|
foreach (var method in type.Methods)
|
||||||
|
{
|
||||||
|
if (method.HasBody)
|
||||||
|
{
|
||||||
|
ProcessMethodBody(method.Body);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Search the instruction stream for calls
|
||||||
|
// to methods we need to rewrite.
|
||||||
|
static void ProcessMethodBody(MethodBody body)
|
||||||
|
{
|
||||||
|
var instructions = body.Instructions;
|
||||||
|
var il = body.GetILProcessor();
|
||||||
|
|
||||||
|
Instruction inst1 = instructions[0];
|
||||||
|
Instruction inst2 = instructions[0];
|
||||||
|
|
||||||
|
for (int i = 1; i < instructions.Count; i++)
|
||||||
|
{
|
||||||
|
var inst = instructions[i];
|
||||||
|
if ((inst.OpCode == OpCodes.Call || inst.OpCode == OpCodes.Callvirt) &&
|
||||||
|
inst.Operand is MethodReference)
|
||||||
|
{
|
||||||
|
var reference = inst.Operand as MethodReference;
|
||||||
|
|
||||||
|
// Make sure we are rewriting OpenTK.InteropHelper methods
|
||||||
|
// and not random methods that happen to have similar names.
|
||||||
|
if (reference.DeclaringType.Name == "InteropHelper")
|
||||||
|
{
|
||||||
|
switch (reference.Name)
|
||||||
|
{
|
||||||
|
case "Call":
|
||||||
|
case "CallReturn":
|
||||||
|
RewriteCall(il, inst, reference);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static void RewriteCall(ILProcessor il, Instruction inst, MethodReference reference)
|
||||||
|
{
|
||||||
|
var signature = new CallSite(reference.ReturnType)
|
||||||
|
{
|
||||||
|
CallingConvention = MethodCallingConvention.StdCall,
|
||||||
|
};
|
||||||
|
if (reference is GenericInstanceMethod)
|
||||||
|
{
|
||||||
|
var greference = reference as GenericInstanceMethod;
|
||||||
|
foreach (var ptype in greference.GenericArguments)
|
||||||
|
{
|
||||||
|
var p = new ParameterDefinition(ptype);
|
||||||
|
signature.Parameters.Add(p);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
// The last parameter is the function address of this entry point.
|
||||||
|
// It is placed at the top of the stack (first parameter of calli)
|
||||||
|
// but is not actually part of the unmanaged signature, so we must
|
||||||
|
// not add it to the signature parameters.
|
||||||
|
foreach (var p in reference.Parameters.Take(reference.Parameters.Count - 1))
|
||||||
|
{
|
||||||
|
signature.Parameters.Add(p);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
var call = il.Create(OpCodes.Calli, signature);
|
||||||
|
il.Replace(inst, call);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
36
Source/OpenTK.Rewrite/Properties/AssemblyInfo.cs
Normal file
36
Source/OpenTK.Rewrite/Properties/AssemblyInfo.cs
Normal file
|
@ -0,0 +1,36 @@
|
||||||
|
using System.Reflection;
|
||||||
|
using System.Runtime.CompilerServices;
|
||||||
|
using System.Runtime.InteropServices;
|
||||||
|
|
||||||
|
// General Information about an assembly is controlled through the following
|
||||||
|
// set of attributes. Change these attribute values to modify the information
|
||||||
|
// associated with an assembly.
|
||||||
|
[assembly: AssemblyTitle("OpenTK.Rewrite")]
|
||||||
|
[assembly: AssemblyDescription("")]
|
||||||
|
[assembly: AssemblyConfiguration("")]
|
||||||
|
[assembly: AssemblyCompany("")]
|
||||||
|
[assembly: AssemblyProduct("OpenTK.Rewrite")]
|
||||||
|
[assembly: AssemblyCopyright("Copyright © 2013")]
|
||||||
|
[assembly: AssemblyTrademark("")]
|
||||||
|
[assembly: AssemblyCulture("")]
|
||||||
|
|
||||||
|
// Setting ComVisible to false makes the types in this assembly not visible
|
||||||
|
// to COM components. If you need to access a type in this assembly from
|
||||||
|
// COM, set the ComVisible attribute to true on that type.
|
||||||
|
[assembly: ComVisible(false)]
|
||||||
|
|
||||||
|
// The following GUID is for the ID of the typelib if this project is exposed to COM
|
||||||
|
[assembly: Guid("01f167a5-6aff-41be-a70a-828c124c5164")]
|
||||||
|
|
||||||
|
// Version information for an assembly consists of the following four values:
|
||||||
|
//
|
||||||
|
// Major Version
|
||||||
|
// Minor Version
|
||||||
|
// Build Number
|
||||||
|
// Revision
|
||||||
|
//
|
||||||
|
// 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.0.0")]
|
||||||
|
[assembly: AssemblyFileVersion("1.0.0.0")]
|
Loading…
Reference in a new issue