Below is an example of using the intermediate language generator to call functions without having to declare or register a delegate. At the current moment it only supports x86, but that is all i need it for, modify it if you want x64 support.
Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using System.Reflection.Emit;
using System.Runtime.InteropServices;
namespace Phoenix
{
public static class Functions
{
public static retType CallFunction<retType>(IntPtr pFunction, CallingConvention CallingConvention, params Object[] args)
{
Type ReturnType = typeof(retType);
Type[] ParamTypes = (from arg in args
select arg.GetType()).ToArray();
DynamicMethod dm = new DynamicMethod("DynamicInvoke", ReturnType, ParamTypes, typeof(Object));
ILGenerator il = dm.GetILGenerator();
for (Int32 i = 0; i < args.Length; i++)
il.Emit(OpCodes.Ldarg, i);
il.Emit(OpCodes.Ldc_I4, pFunction.ToInt32());
il.EmitCalli(OpCodes.Calli, CallingConvention, ReturnType, ParamTypes);
il.Emit(OpCodes.Ret);
return (retType)dm.Invoke(null, args);
}
public static void CallFunction(IntPtr pFunction, CallingConvention CallingConvention, params Object[] args)
{
CallFunction<Int32>(pFunction, CallingConvention, args);
}
}
}
Example Usage:
Code:
CallFunction((IntPtr)Offsets.LuaDoString, "DoEmote('dance')", "LuaFilename", 0)