private static void GenerateMethod(ClassDescription cd, MethodDescription md, Lua4NetSerializer serializer) { serializer.NewLine(); string funcdef = string.Format("private static int {0}({1} Instance,IntPtr l)", md.NickName, cd.ClassName); GeneratorHelper.GenerateCSFunction(funcdef, serializer, s => { string classOrInstance = md.IsStatic ? cd.ClassName : "Instance"; serializer.NewLine("// get method arguments"); int argIndex = 3; foreach (MethodFieldDescription mdf in md.InputArgs) { switch (mdf.Type) { case MessageFieldType.NumberType: { switch (mdf.GetNumberType()) { case NumberType.Boolean: { serializer.NewLine(string.Format("{0} {1} = LuaApi.lua_tonumber(l,{2})!=0;", mdf.TypeName, mdf.Name, argIndex)); break; } case NumberType.Enum: case NumberType.Numeric: { serializer.NewLine(string.Format("{0} {1} = ({0})LuaApi.lua_tonumber(l,{2});", mdf.TypeName, mdf.Name, argIndex)); break; } } break; } case MessageFieldType.StringType: { serializer.NewLine(string.Format("string {0} = LuaApi.lua_tostring(l,{1});", mdf.Name, argIndex)); break; } case MessageFieldType.DelegateType: case MessageFieldType.ClientType: { if (mdf.RawType == typeof(LuaStackFunction)) { serializer.NewLine(string.Format("LuaStackFunction {0} = new LuaStackFunction({1});", mdf.Name, argIndex)); } else if (mdf.RawType == typeof(LuaRefFunction)) { serializer.NewLine(string.Format("LuaRefFunction {0} = new LuaRefFunction({1},l);", mdf.Name, argIndex)); } else { serializer.NewLine(string.Format("int {0}Id = (int)LuaApi.lua_tonumber(l,{1});", mdf.Name, argIndex)); serializer.NewLine(string.Format("{0} {1} = LuaManager.Instance.GetObjectT<{0}>({1}Id);", mdf.TypeName, mdf.Name)); } break; } } ++argIndex; } serializer.NewLine(); serializer.NewLine("// call method"); if (md.Output == null) { if (md.IsStatic) { serializer.NewLine(string.Format("{0}.{1}(", cd.ClassName, md.Name)); } else { serializer.NewLine(string.Format("Instance.{0}(", md.Name)); } } else { if (md.IsStatic) { serializer.NewLine(string.Format("{2} methodRetVar = {0}.{1}(", cd.ClassName, md.Name, md.Output.TypeName)); } else { serializer.NewLine(string.Format("{2} methodRetVar = Instance.{1}(", cd.ClassName, md.Name, md.Output.TypeName)); } } int nNextArgIndex = 0; while (nNextArgIndex < md.InputArgs.Count) { MethodFieldDescription mfd = md.InputArgs[nNextArgIndex]; serializer.Apppend(mfd.Name); ++nNextArgIndex; if (nNextArgIndex <= (md.InputArgs.Count - 1)) serializer.Apppend(","); } serializer.Apppend(");"); serializer.NewLine(); if (md.Output != null) { switch (md.Output.Type) { case MessageFieldType.NumberType: { serializer.NewLine(string.Format("LuaApi.lua_pushnumber(l,{0}methodRetVar);", string.Empty)); break; } case MessageFieldType.StringType: { serializer.NewLine("LuaApi.lua_pushstring(l,methodRetVar);"); break; } case MessageFieldType.DelegateType: case MessageFieldType.ClientType: { serializer.NewLine(string.Format("int nRetObjectId = LuaManager.Instance.PushStackObject(methodRetVar);")); serializer.NewLine("LuaApi.lua_pushnumber(l,nRetObjectId);"); break; } } serializer.NewLine("return 1;"); } else { serializer.NewLine("return 0;"); } }); }
private static void GetMethod(MethodDescription md,MethodBase mi, string NickName,string returnName) { md.Method = mi; md.InputArgs = new List<MethodFieldDescription>(); md.Output = null; md.RawType = mi.GetType(); md.TypeName = GeneratorHelper.GetTypeName(md.RawType); md.Name = mi.Name; md.NickName = string.IsNullOrEmpty(NickName) ? md.Name : NickName; md.IsStatic = mi.IsStatic; foreach (ParameterInfo pi in md.Method.GetParameters()) { MethodFieldDescription mfd = new MethodFieldDescription(); mfd.Name = pi.Name; mfd.RawType = pi.ParameterType; mfd.TypeName = GeneratorHelper.GetTypeName(mfd.RawType); mfd.IsStatic = false; mfd.Type = GetMessageFieldType(mfd.RawType); md.InputArgs.Add(mfd); } MethodInfo mii = md.Method as MethodInfo; if (null != mii) { if (!string.Equals(mii.ReturnType.FullName, "System.Void")) { MethodFieldDescription mfd = new MethodFieldDescription(); mfd.Name = returnName; mfd.RawType = mii.ReturnType; mfd.TypeName = GeneratorHelper.GetTypeName(mfd.RawType); mfd.IsStatic = false; mfd.Type = GetMessageFieldType(mfd.RawType); md.Output = mfd; } } }
private static void GetMethod(Assembly a, ClassDescription result, ScriptableClass sc) { result.Methods = new List<MethodDescription>(); foreach (ScriptableMethod sm in sc.Method) { MethodInfo mi = result.Class.GetMethod(sm.Name, sm.GetArgs(result)); if (null != mi) { MethodDescription md = new MethodDescription(); GetMethod(md,mi, sm.NickName,string.Empty); result.Methods.Add(md); } else { Console.WriteLine(string.Format("method {0} not found,In={1}", sm.Name, sm.In)); } } }
private static void GetMethod(Assembly a, ClassDescription result, Type type) { result.Methods = new List<MethodDescription>(); foreach (MethodInfo mi in type.GetMethods()) { ScriptableAttribute attr = GetAttribute(mi); if (null != attr) { MethodDescription md = new MethodDescription(); GetMethod(md, mi, attr.Name, string.Empty); result.Methods.Add(md); } } }
private static void GenerateMethod(ClassDescription csd, MethodDescription md, int methodId, Lua4NetSerializer serializer) { serializer.NewLine(); serializer.NewLine(string.Format("function {0}{1}{2}(", csd.GetNamespaceName(csd.ProxyName), md.IsStatic ? "." : ":", md.NickName)); int nNextArg = 0; while (nNextArg < md.InputArgs.Count) { serializer.Apppend(md.InputArgs[nNextArg].Name); nNextArg++; if (nNextArg <= (md.InputArgs.Count - 1)) serializer.Apppend(","); } serializer.Apppend(")"); serializer.BeginBlock(string.Empty); if (md.Output != null) { if (md.Output.Type == MessageFieldType.ClientType) { serializer.NewLine(string.Format("local id = {0}({1},{2}", csd.ServantCallName, md.IsConstructor || md.IsStatic ? "0" : "self.Id", methodId)); } else { serializer.NewLine(string.Format("return {0}({1},{2}", csd.ServantCallName, md.IsConstructor || md.IsStatic ? "0" : "self.Id", methodId)); } } else { if (md.IsConstructor) { serializer.NewLine(string.Format("local id = {0}({1},{2}", csd.ServantCallName, md.IsConstructor || md.IsStatic ? "0" : "self.Id", methodId)); } else { serializer.NewLine(string.Format("{0}({1},{2}", csd.ServantCallName, md.IsConstructor || md.IsStatic ? "0" : "self.Id", methodId)); } } if (md.InputArgs.Count > 0) serializer.Apppend(","); nNextArg = 0; while (nNextArg < md.InputArgs.Count) { MethodFieldDescription mfd = md.InputArgs[nNextArg]; if (mfd.Type == MessageFieldType.ClientType) { serializer.Apppend(md.InputArgs[nNextArg].Name+".Id"); } else { serializer.Apppend(md.InputArgs[nNextArg].Name); } nNextArg++; if (nNextArg <= (md.InputArgs.Count - 1)) serializer.Apppend(","); } serializer.Apppend(")"); if (md.Output != null) { if (md.Output.Type == MessageFieldType.ClientType) { serializer.NewLine(string.Format("return {0}.Get(id)", md.GetOutputProxyName())); } } else if (md.IsConstructor) { serializer.NewLine(string.Format("return {0}.Get(id)", csd.GetNamespaceName(csd.ProxyName))); } serializer.EndBlock("end"); }
private static void GenerateMetaTable(ClassDescription csd,List<MethodDescription> methods, Lua4NetSerializer serializer,MethodDescription ctor) { serializer.NewLine(); serializer.NewLine(csd.GetNamespaceName(csd.ProxyName) + ".__Method = { "); int nNextMeghod = 0; while (nNextMeghod < methods.Count) { serializer.NewLine(string.Format(" {0} = {1}.{0}", methods[nNextMeghod].NickName, csd.GetNamespaceName(csd.ProxyName))); nNextMeghod++; if (nNextMeghod <= (methods.Count - 1)) serializer.Apppend(","); } serializer.NewLine("}"); serializer.NewLine(); serializer.NewLine(csd.GetNamespaceName(csd.ProxyName)); serializer.Apppend(".MetaTable = { "); serializer.Apppend(string.Format("__index = {0}", csd.GetNamespaceName(csd.ProxyName) + ".__GetByIndex")); serializer.Apppend(string.Format(",__newindex = {0}", csd.GetNamespaceName(csd.ProxyName) + ".__SetByIndex")); if (ctor != null) { serializer.Apppend(string.Format(",__call = {0}", csd.GetNamespaceName(csd.ProxyName) + "." + ctor.NickName)); } serializer.Apppend(" }"); serializer.NewLine(string.Format("setmetatable({0},{0}.MetaTable)", csd.GetNamespaceName(csd.ProxyName))); }