public static string GetScriptName(string absPath) { var methods = GetMethodInfo.WithAttr <ScriptNameAttribute>().ToArray(); if (methods.Length > 0) { var m = methods[0]; var getScriptName = (Func <string, string>)Delegate.CreateDelegate(typeof(Func <string, string>), m); return(getScriptName(absPath)); } return(null); }
public static Dictionary <string, string> GetAllLuaScripts() { var methods = GetMethodInfo.WithAttr <AllLuaScriptsAttribute>().ToArray(); if (methods.Length > 0) { var m = methods[0]; var all = (AllLuaScripts)Delegate.CreateDelegate(typeof(AllLuaScripts), m); return(all()); } return(new Dictionary <string, string>()); }
public static GetPathDelegate GetDelegate() { if (delegateCached) { return(cachedDelegate); } delegateCached = true; var methods = GetMethodInfo.WithAttr <LuaEditorGetPathDelegateAttribute>().ToArray(); if (methods.Length > 0) { var m = methods[0]; cachedDelegate = (GetPathDelegate)Delegate.CreateDelegate(typeof(GetPathDelegate), m); } return(cachedDelegate); }
public static TypeLoader GetLoader() { if (typeLoaderCached) { return(cachedTypeLoader); } typeLoaderCached = true; var methods = GetMethodInfo.WithAttr <LuaTypeLoaderAttribute>().ToArray(); if (methods.Length > 0) { var m = methods[0]; cachedTypeLoader = (TypeLoader)Delegate.CreateDelegate(typeof(TypeLoader), m); } return(cachedTypeLoader); }
void AddRpc(TypeDefinition newType, MethodDefinition irpc) { var tag = irpc.CustomAttributes.FirstOrDefault(x => x.AttributeType.Name == "TAG"); if (tag is null) { return; } var cmdx = tag.ConstructorArguments.First().Value; var cmd = 0; switch (cmdx) { case Mono.Cecil.CustomAttributeArgument args: cmd = (int)args.Value; break; default: cmd = (int)cmdx; break; } var method = new MethodDefinition(irpc.Name, MethodAttributes.Public | MethodAttributes.HideBySig | MethodAttributes.NewSlot | MethodAttributes.Virtual | MethodAttributes.Final, irpc.ReturnType); var il = method.Body.GetILProcessor(); var parameters = irpc.Parameters; var paramTypes = ParamTypes(parameters.ToArray(), false); for (int i = 0; i < paramTypes.Length; i++) { method.Parameters.Add(new ParameterDefinition(parameters[i].Name, parameters[i].Attributes, paramTypes[i])); } if (irpc.ContainsGenericParameter) { throw new Exception($"not have generic parameter{irpc.FullName}"); } var returntype = irpc.ReturnType; if (returntype.IsGenericInstance) { var genreturntype = returntype as GenericInstanceType; if (genreturntype.Name != "Task`1") { throw new Exception($"return type error:{genreturntype.FullName}"); } if (genreturntype.GenericArguments[0].Name == "IResult") { ParametersArray args = new ParametersArray(this, il, paramTypes); il.Emit(OpCodes.Nop); il.Emit(OpCodes.Ldarg_0); il.Emit(OpCodes.Ldfld, obj); il.Emit(OpCodes.Ldc_I4, cmd); GenericArray <object> argsArr = new GenericArray <object>(this, il, ParamTypes(parameters.ToArray(), true).Length); for (int i = 0; i < parameters.Count; i++) { // args[i] = argi; if (!parameters[i].IsOut) { argsArr.BeginSet(i); args.Get(i); argsArr.EndSet(parameters[i].ParameterType); } } argsArr.Load(); var asyncAction = obj.FieldType.Resolve().Methods.First(p => p.Name == "AsyncFunc"); il.Emit(OpCodes.Callvirt, ModuleDefinition.ImportReference(asyncAction)); il.Emit(OpCodes.Ret); newType.Methods.Add(method); } else { ParametersArray args = new ParametersArray(this, il, paramTypes); il.Emit(OpCodes.Nop); il.Emit(OpCodes.Ldarg_0); il.Emit(OpCodes.Ldfld, obj); il.Emit(OpCodes.Ldc_I4, cmd); GenericArray <object> argsArr = new GenericArray <object>(this, il, ParamTypes(parameters.ToArray(), true).Length); for (int i = 0; i < parameters.Count; i++) { // args[i] = argi; if (!parameters[i].IsOut) { argsArr.BeginSet(i); args.Get(i); argsArr.EndSet(parameters[i].ParameterType); } } argsArr.Load(); var genericAsyncFunc = obj.FieldType.Resolve().Methods.First(p => p.Name == "AsyncFunc" && p.CallingConvention == MethodCallingConvention.Generic); il.Emit(OpCodes.Callvirt, ModuleDefinition.ImportReference(MakeGenericInstanceMethod(genericAsyncFunc, genreturntype.GenericArguments.ToArray()))); il.Emit(OpCodes.Ret); newType.Methods.Add(method); } } else if (returntype.FullName == "System.Void") { ParametersArray args = new ParametersArray(this, il, paramTypes); il.Emit(OpCodes.Nop); il.Emit(OpCodes.Ldarg_0); il.Emit(OpCodes.Ldfld, obj); il.Emit(OpCodes.Ldc_I4, cmd); GenericArray <object> argsArr = new GenericArray <object>(this, il, ParamTypes(parameters.ToArray(), true).Length); for (int i = 0; i < parameters.Count; i++) { // args[i] = argi; if (!parameters[i].IsOut) { argsArr.BeginSet(i); args.Get(i); argsArr.EndSet(parameters[i].ParameterType); } } argsArr.Load(); var Action = obj.FieldType.Resolve().Methods.First(p => p.Name == "Action"); il.Emit(OpCodes.Callvirt, ModuleDefinition.ImportReference(Action)); il.Emit(OpCodes.Nop); il.Emit(OpCodes.Ret); newType.Methods.Add(method); } else if (returntype.FullName == "System.Threading.Tasks.Task") { ParametersArray args = new ParametersArray(this, il, paramTypes); il.Emit(OpCodes.Nop); il.Emit(OpCodes.Ldarg_0); il.Emit(OpCodes.Ldfld, obj); il.Emit(OpCodes.Ldc_I4, cmd); GenericArray <object> argsArr = new GenericArray <object>(this, il, ParamTypes(parameters.ToArray(), true).Length); for (int i = 0; i < parameters.Count; i++) { // args[i] = argi; if (!parameters[i].IsOut) { argsArr.BeginSet(i); args.Get(i); argsArr.EndSet(parameters[i].ParameterType); } } argsArr.Load(); var asyncAction = obj.FieldType.Resolve().Methods.First(p => p.Name == "AsyncAction"); il.Emit(OpCodes.Callvirt, ModuleDefinition.ImportReference(asyncAction)); il.Emit(OpCodes.Ret); newType.Methods.Add(method); } else { ParametersArray args = new ParametersArray(this, il, paramTypes); il.Emit(OpCodes.Nop); il.Emit(OpCodes.Ldarg_0); il.Emit(OpCodes.Ldfld, obj); il.Emit(OpCodes.Ldc_I4, cmd); il.Emit(OpCodes.Ldtoken, irpc.ReturnType); var ptype = ModuleDefinition.ImportReference(GetMethodInfo.GetTypeofHandler()); il.Emit(OpCodes.Call, ptype); GenericArray <object> argsArr = new GenericArray <object>(this, il, ParamTypes(parameters.ToArray(), true).Length); for (int i = 0; i < parameters.Count; i++) { // args[i] = argi; if (!parameters[i].IsOut) { argsArr.BeginSet(i); args.Get(i); argsArr.EndSet(parameters[i].ParameterType); } } argsArr.Load(); var func = obj.FieldType.Resolve().Methods.First(p => p.Name == "Func"); il.Emit(OpCodes.Callvirt, ModuleDefinition.ImportReference(func)); var res = new VariableDefinition(irpc.ReturnType); method.Body.Variables.Add(res); Convert(il, ModuleDefinition.ImportReference(typeof(object)), irpc.ReturnType, false); il.Emit(OpCodes.Stloc, res); il.Emit(OpCodes.Ldloc, res); il.Emit(OpCodes.Ret); newType.Methods.Add(method); } }
void AddRpc(TypeDefinition newType, MethodDefinition irpc) { var tag = irpc.CustomAttributes.FirstOrDefault(x => x.AttributeType.Name == "TAG"); if (tag is null) { return; } var cmd = (int)tag.ConstructorArguments.First().Value; var method = new MethodDefinition(irpc.Name, MethodAttributes.Public | MethodAttributes.HideBySig | MethodAttributes.NewSlot | MethodAttributes.Virtual | MethodAttributes.Final, irpc.ReturnType); var il = method.Body.GetILProcessor(); var parameters = irpc.Parameters; var paramTypes = ParamTypes(parameters.ToArray(), false); foreach (var param in paramTypes) { method.Parameters.Add(new ParameterDefinition(param)); } if (irpc.ContainsGenericParameter) { throw new Exception($"not have generic parameter{irpc.FullName}"); //var ts = irpc.GenericParameters; //for (int i = 0; i < ts.Count; i++) //{ // method.GenericParameters.Add(ts[i]); //} } ParametersArray args = new ParametersArray(this, il, paramTypes); il.Emit(OpCodes.Nop); il.Emit(OpCodes.Ldarg_0); il.Emit(OpCodes.Ldc_I4, cmd); il.Emit(OpCodes.Ldtoken, irpc.ReturnType); var ptype = ModuleDefinition.ImportReference(GetMethodInfo.GetTypeofHandler()); il.Emit(OpCodes.Call, ModuleDefinition.ImportReference(ptype)); GenericArray <System.Object> argsArr = new GenericArray <System.Object>(this, il, ParamTypes(parameters.ToArray(), true).Length); for (int i = 0; i < parameters.Count; i++) { // args[i] = argi; if (!parameters[i].IsOut) { argsArr.BeginSet(i); args.Get(i); argsArr.EndSet(parameters[i].ParameterType); } } argsArr.Load(); il.Emit(OpCodes.Call, _methodcall); if (irpc.ReturnType.Name == "Void") { il.Emit(OpCodes.Pop); } else { var res = new VariableDefinition(irpc.ReturnType); method.Body.Variables.Add(res); Convert(il, ModuleDefinition.ImportReference(typeof(System.Object)), irpc.ReturnType, false); il.Emit(OpCodes.Stloc, res); il.Emit(OpCodes.Ldloc, res); } il.Emit(OpCodes.Ret); newType.Methods.Add(method); }