1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
|
// Copyright (c) 2017 Eric Mellino
// Copyright (c) 2019 Jean-Philippe Bruyère <jp_bruyere@hotmail.com>
//
// This code is licensed under the MIT license (MIT) (http://opensource.org/licenses/MIT)
using Mono.Cecil;
using Mono.Cecil.Cil;
using System;
using System.Linq;
using Mono.Collections.Generic;
using System.IO;
using System.Collections.Generic;
using System.Text;
namespace Vk.Rewrite
{
public class Program
{
const string mainVkNamespace = "Vulkan";
static TypeReference s_calliRewriteRef;
public static int Main(string[] args)
{
if ((args.Length < 1) == true || args[0] == null) {
Console.WriteLine ("Error: argument 1 must be the path of VK.dll.");
return -1;
}
string vkDllPath = args[0];
string outputPath = null;
bool rewriteInPlace = false;
if (outputPath == null)
{
outputPath = Path.GetTempFileName ();
rewriteInPlace = true;
}
Rewrite(vkDllPath, outputPath);
if (rewriteInPlace) {
File.Delete (vkDllPath);
File.Copy (outputPath, vkDllPath);
File.Delete (outputPath);
outputPath = vkDllPath;
}
Console.WriteLine (outputPath + " rewrited.");
return 0;
}
private static void Rewrite(string vkDllPath, string outputPath)
{
using (AssemblyDefinition vkDll = AssemblyDefinition.ReadAssembly (vkDllPath))
{
ModuleDefinition mainModule = vkDll.Modules[0];
s_calliRewriteRef = mainModule.GetType (mainVkNamespace+".Generator.CalliRewriteAttribute");
foreach (TypeDefinition type in mainModule.Types)
{
foreach (MethodDefinition method in type.Methods)
{
if (method.CustomAttributes.Any(ca => ca.AttributeType == s_calliRewriteRef))
RewriteMethod (method);
}
}
vkDll.Write(outputPath);
}
}
static void RewriteMethod (MethodDefinition method)
{
ILProcessor il = method.Body.GetILProcessor();
il.Body.Instructions.Clear();
List<VariableDefinition> stringParams = new List<VariableDefinition>();
for (int i = 0; i < method.Parameters.Count; i++)
{
EmitLoadArgument (il, i, method.Parameters);
TypeReference parameterType = method.Parameters[i].ParameterType;
if (parameterType.IsGenericInstance) {
if (parameterType.Name == "Nullable`1")
System.Diagnostics.Debugger.Break ();
}
if (parameterType.IsByReference)
{
VariableDefinition byRefVariable = new VariableDefinition(new PinnedType(parameterType));
method.Body.Variables.Add(byRefVariable);
il.Emit(OpCodes.Stloc, byRefVariable);
il.Emit(OpCodes.Ldloc, byRefVariable);
il.Emit(OpCodes.Conv_I);
}
}
FieldDefinition field = method.DeclaringType.Fields.SingleOrDefault(fd => fd.Name == method.Name + "_ptr");
if (field == null)
throw new InvalidOperationException("Can't find function pointer field for " + method.Name);
il.Emit(OpCodes.Ldsfld, field);
CallSite callSite = new CallSite(method.ReturnType) {CallingConvention = MethodCallingConvention.StdCall};
foreach (ParameterDefinition pd in method.Parameters)
{
TypeReference parameterType;
if (pd.ParameterType.IsByReference)
parameterType = new PointerType(pd.ParameterType.GetElementType());
else
parameterType = pd.ParameterType;
ParameterDefinition calliPD = new ParameterDefinition(pd.Name, pd.Attributes, parameterType);
callSite.Parameters.Add(calliPD);
}
il.Emit(OpCodes.Calli, callSite);
/*foreach (VariableDefinition stringVar in stringParams)
{
il.Emit(OpCodes.Ldloc, stringVar);
il.Emit(OpCodes.Call, s_freeHGlobalRef);
}*/
il.Emit(OpCodes.Ret);
if (method.Body.Variables.Count > 0)
method.Body.InitLocals = true;
}
static void EmitLoadArgument(ILProcessor il, int i, Collection<ParameterDefinition> parameters)
{
switch (i) {
case 0:
il.Emit(OpCodes.Ldarg_0);
break;
case 1:
il.Emit(OpCodes.Ldarg_1);
break;
case 2:
il.Emit(OpCodes.Ldarg_2);
break;
case 3:
il.Emit(OpCodes.Ldarg_3);
break;
default:
il.Emit(OpCodes.Ldarg, i);
break;
}
}
}
}
|