Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
alexbevi
GitHub Repository: alexbevi/BizHawk
Path: blob/master/BizHawk.Emulation.Cores/ExternalCores/Snippets.txt
2 views
1
////////////////////////////
2
//we can't use these because we need more clever control over the delegate type (marshalling attributes, for one thing)
3
4
Delegate MyMakeDelegate(string methodName)
5
{
6
MethodInfo mi = GetType().GetMethod(methodName,BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance);
7
var parameters = mi.GetParameters()
8
.Select(p => p.ParameterType)
9
.ToArray();
10
11
Type t = DelegateCreator.MakeNewCustomDelegate(mi.ReturnType, parameters);
12
return Delegate.CreateDelegate(t, this, mi);
13
}
14
15
16
static class DelegateCreator
17
{
18
public static Type MakeNewCustomDelegate(Type ret_type, Type[] argtypes)
19
{
20
var _DelegateCtorSignature = new Type[] { typeof(object), typeof(IntPtr) };
21
Type returnType = ret_type;
22
Type[] parameterTypes = argtypes;
23
TypeBuilder builder = DefineDelegateType("Delegate" + argtypes.Length);
24
builder.DefineConstructor(MethodAttributes.RTSpecialName | MethodAttributes.HideBySig | MethodAttributes.Public, CallingConventions.Standard, _DelegateCtorSignature).SetImplementationFlags(MethodImplAttributes.CodeTypeMask);
25
builder.DefineMethod("Invoke", MethodAttributes.VtableLayoutMask | MethodAttributes.HideBySig | MethodAttributes.Virtual | MethodAttributes.Public, returnType, parameterTypes).SetImplementationFlags(MethodImplAttributes.CodeTypeMask);
26
27
//[UnmanagedFunctionPointer(CallingConvention.ThisCall)]
28
//builder.SetCustomAttribute(new CustomAttributeBuilder(new ConstructorInfo(
29
ConstructorInfo ci = typeof(UnmanagedFunctionPointerAttribute).GetConstructor(new[] { typeof(CallingConvention) });
30
CustomAttributeBuilder cab = new CustomAttributeBuilder(ci, new object[] { CallingConvention.ThisCall });
31
builder.SetCustomAttribute(cab);
32
33
return builder.CreateType();
34
}
35
36
internal static TypeBuilder DefineDelegateType(string name)
37
{
38
return DefineType(name, typeof(MulticastDelegate), TypeAttributes.AutoClass | TypeAttributes.Sealed | TypeAttributes.Public);
39
}
40
41
static int _index;
42
private static TypeBuilder DefineType(string name, Type parent, TypeAttributes attr)
43
{
44
StringBuilder builder = new StringBuilder(name);
45
int num = Interlocked.Increment(ref _index);
46
builder.Append("$");
47
builder.Append(num);
48
builder.Replace('+', '_').Replace('[', '_').Replace(']', '_').Replace('*', '_').Replace('&', '_').Replace(',', '_').Replace('\\', '_');
49
name = builder.ToString();
50
return _myModule.DefineType(name, attr, parent);
51
}
52
53
static AssemblyBuilder _myAssembly;
54
static ModuleBuilder _myModule;
55
static void InitializeAssemblyGen()
56
{
57
AssemblyName name = new AssemblyName("Snippets");
58
CustomAttributeBuilder[] assemblyAttributes = new CustomAttributeBuilder[] { new CustomAttributeBuilder(typeof(SecurityTransparentAttribute).GetConstructor(Type.EmptyTypes), new object[0]) };
59
_myAssembly = AppDomain.CurrentDomain.DefineDynamicAssembly(name, AssemblyBuilderAccess.Run, assemblyAttributes);
60
_myModule = _myAssembly.DefineDynamicModule(name.Name, false);
61
_myAssembly.DefineVersionInfoResource();
62
}
63
static DelegateCreator()
64
{
65
InitializeAssemblyGen();
66
}
67
}
68
69
70
////////////////
71
these are members of external core. theyre deprecated.
72
73
74
public IntPtr Signal(string param, IntPtr value)
75
{
76
return mAccessor.Signal(null, IntPtr.Zero, param, value);
77
}
78
79
public IntPtr Signal(string param)
80
{
81
return mAccessor.Signal(null, IntPtr.Zero, param, IntPtr.Zero);
82
}
83
84
public IntPtr Signal(string type, IntPtr obj, string param, Delegate value)
85
{
86
liveDelegates[value.Target ?? ostatic][param] = value;
87
return mAccessor.Signal(type, obj, param, Marshal.GetFunctionPointerForDelegate(value));
88
}
89
90
public IntPtr Signal(string param, Delegate value)
91
{
92
return Signal(null, IntPtr.Zero, param, value);
93
}
94
95
public IntPtr Signal(string param, int value)
96
{
97
return mAccessor.Signal(null, IntPtr.Zero, param, new IntPtr(value));
98
}
99
100
public IntPtr Signal(string type, IntPtr obj, string param, IntPtr value)
101
{
102
return mAccessor.Signal(type, obj, param, value);
103
}
104