private static void GetBaseDescription(Assembly a, ClassDescription result, Type type) { Type thisType = type; string className = string.Empty; if (thisType.IsNested) { className = thisType.Name; className = className.Remove(0, thisType.Namespace.Length + 1); } else { className = GeneratorHelper.GetTypeName(thisType); } result.Class = thisType; result.BaseClass = thisType.BaseType; result.HasDefaultConstructor = false; result.IsValueType = thisType.IsValueType; result.IsEnum = thisType.IsEnum; result.ClassName = className; result.ServantCallName = result.Class.Name + "Servant4LuaCall"; result.ServantName = result.Class.Name + "LuaServant"; result.ProxyName = result.Class.Name; string assemblyName = thisType.Assembly.GetName().Name; result.CSFileName = Path.Combine(assemblyName, thisType.Name + ".cs"); result.LuaFileName = Path.Combine(assemblyName, thisType.Name + ".lua"); }
public static void GenerateEnum(ClassDescription cd, Lua4NetSerializer serializer) { Type tp = cd.Class; List<string> names = new List<string>(); List<int> values = new List<int>(); foreach (string s in Enum.GetNames(tp)) { names.Add(s); } foreach (int i in Enum.GetValues(tp)) { values.Add(i); } serializer.NewLine(string.Format("{0} = ", cd.GetNamespaceName(tp.Name))); serializer.NewLine(" { "); for (int i = 0; i < names.Count; ++i) { serializer.NewLine(string.Format(" {0} = {1}", names[i], values[i])); if (i != (names.Count - 1)) serializer.Apppend(","); } serializer.NewLine(" }"); }
public void GenerateEnum(ClassDescription cd, string path) { Lua4NetSerializer serializer = new Lua4NetSerializer(0); ProxyGenerator.GenerateEnum(cd, serializer); string fullpath = Path.Combine(path, cd.LuaFileName); SaveAsFile(fullpath, serializer.ToString()); }
public static ClassDescription Generate(Assembly a, Type type, List<Assembly> allAssembly) { ClassDescription result = new ClassDescription(); GetBaseDescription(a, result, type); GetConstructor(a, result,type); GetMethod(a, result, type); GetProperty(a, result, type); GetEvent(a, result, type); return result; }
public static ClassDescription Generate(Assembly a, ScriptableClass sc, List<Assembly> allAssembly) { ClassDescription result = new ClassDescription(); result.allAssembly = allAssembly; GetBaseDescription(a, result, sc); GetConstructor(a, result, sc); GetMethod(a, result, sc); GetProperty(a, result, sc); GetEvent(a, result, sc); return result; }
public static void Generate(ClassDescription cd, Lua4NetSerializer serializer) { string classdef = string.Format("public class {0}: LuaRegister", cd.ServantName); GeneratorHelper.GenerateClass(classdef, serializer, s => { serializer.NewLine("#region constructor"); if (cd.HasDefaultConstructor) GenerateDefaultConstructor(cd, serializer); cd.Constructors.ForEach(c=>GenerateConstructorMethod(cd,c,serializer)); serializer.NewLine(); serializer.NewLine("#endregion"); serializer.NewLine(); serializer.NewLine("#region method"); cd.Methods.ForEach(md => GenerateMethod(cd, md, serializer)); serializer.NewLine(); serializer.NewLine("#endregion"); serializer.NewLine(); serializer.NewLine("#region property"); cd.Propertys.ForEach(pf => GenerateGetSetMethod(cd, pf, serializer)); serializer.NewLine(); serializer.NewLine("#endregion"); serializer.NewLine(); serializer.NewLine("#region event"); cd.Events.ForEach(e => GenerateEvent(cd, e, serializer)); serializer.NewLine(); serializer.NewLine("#endregion"); serializer.NewLine(); serializer.NewLine("#region delegate"); cd.Propertys.ForEach(pf => DelegateGenerator.Generate(pf.Pi, serializer)); cd.Events.ForEach(ed => DelegateGenerator.Generate(ed.Ei, serializer)); serializer.NewLine(); serializer.NewLine("#endregion"); serializer.NewLine(); serializer.NewLine("#region register"); serializer.NewLine(); GenerateRegisterFunction(cd, serializer); serializer.NewLine(); GenerateStaticFields(cd,serializer); serializer.NewLine(); GenerateRootMethod(cd, serializer); serializer.NewLine(); serializer.NewLine("#endregion"); }); }
public static void Generate(ClassDescription csd, Lua4NetSerializer serializer) { serializer.NewLine(); serializer.NewLine(csd.GetNamespaceName(csd.ProxyName) + " = { " + string.Format("__Name = {0}",csd.ProxyName) +" }"); List<MethodDescription> allMethods = new List<MethodDescription>(); List<MethodDescription> allFunctions = new List<MethodDescription>(); allMethods.AddRange(csd.Constructors); allFunctions.AddRange(csd.Constructors); allMethods.AddRange(csd.Methods); allFunctions.AddRange(csd.Methods); csd.Propertys.ForEach(p=> { if (p.GetMethod != null) { allMethods.Add(p.GetMethod); if(p.IsItemProperty) allFunctions.Add(p.GetMethod); } if (p.SetMethod != null) { allMethods.Add(p.SetMethod); if (p.IsItemProperty) allFunctions.Add(p.SetMethod); } }); csd.Events.ForEach(e => { if (e.AddMethod != null) { allMethods.Add(e.AddMethod); allFunctions.Add(e.AddMethod); } if (e.RemoveMethod != null) { allMethods.Add(e.RemoveMethod); allFunctions.Add(e.RemoveMethod); } }); GenerateGet(csd, serializer); GenerateConvertFrom(csd, serializer); int id = 0; allMethods.ForEach(smd => GenerateMethod(csd, smd, id++, serializer)); GenerateGetPropertys(csd, serializer); GenerateSetPropertys(csd, serializer); GenerateIndex(csd, serializer); GenerateNewIndex(csd, serializer); GenerateMetaTable(csd, allFunctions, serializer,csd.GetDefaultOrFirstConstructor()); }
private static void GenerateConstructorMethod(ClassDescription cd, ConstructorDescription c, Lua4NetSerializer serializer) { serializer.NewLine(); string ctordef = string.Format("private static int {0}({1} Instance,IntPtr l)",c.NickName,cd.ClassName); GeneratorHelper.GenerateCSFunction(ctordef, serializer, s => { int nParameterIndex = 3; foreach (MethodFieldDescription mfd in c.InputArgs) { switch (mfd.Type) { case MessageFieldType.NumberType: { switch (mfd.GetNumberType()) { case NumberType.Boolean: { serializer.NewLine(string.Format("{0} {1} = LuaApi.lua_tonumber(l,{2})!=0;", mfd.TypeName, mfd.Name, nParameterIndex)); break; } case NumberType.Enum: case NumberType.Numeric: { serializer.NewLine(string.Format("{0} {1} = ({0})LuaApi.lua_tonumber(l,{2});", mfd.TypeName, mfd.Name, nParameterIndex)); break; } } break; } case MessageFieldType.StringType: { serializer.NewLine(string.Format("string {0} = LuaApi.lua_tostring(l,{1});", mfd.Name, nParameterIndex)); break; } default: { serializer.NewLine(string.Format("int {0}id = (int)LuaApi.lua_tonumber(l,{1});", mfd.Name, nParameterIndex)); serializer.NewLine(string.Format("{0} {1} = LuaManager.Instance.GetObjectT<{0}>({1}id);", mfd.TypeName, mfd.Name)); break; } } ++nParameterIndex; } serializer.NewLine(string.Format("Instance = new {0}({1});", cd.ClassName, GeneratorHelper.GenerateParameter(c.Ci.GetParameters().ToList()))); serializer.NewLine("int id = LuaManager.Instance.PushStackObject(Instance);"); serializer.NewLine("LuaApi.lua_pushnumber(l,id);"); serializer.NewLine("return 1;"); }); }
private static void GenerateEvent(ClassDescription cd, EventDescription ed, Lua4NetSerializer serializer) { #region add { serializer.NewLine(); string methodName = ed.Ei.GetAddMethod().Name; string funcdef = string.Format("private static int {0}({1} Instance,IntPtr l)", methodName, cd.ClassName); GeneratorHelper.GenerateCSFunction(funcdef, serializer, s => { string classOrInstance = ed.IsStatic ? cd.ClassName : "Instance"; serializer.NewLine(string.Format("{0}Delegate d = new {0}Delegate(3,l);", ed.Ei.Name)); serializer.NewLine(string.Format("{0}.{1} += d.Call;",classOrInstance,ed.Ei.Name)); serializer.NewLine("int id = LuaManager.Instance.PushStackObject(d);"); serializer.NewLine("LuaApi.lua_pushnumber(l,id);"); serializer.NewLine("return 1;"); }); } #endregion #region remove { serializer.NewLine(); string methodName = ed.Ei.GetRemoveMethod().Name; string funcdef = string.Format("private static int {0}({1} Instance,IntPtr l)", methodName, cd.ClassName); GeneratorHelper.GenerateCSFunction(funcdef, serializer, s => { serializer.NewLine("int id = (int)LuaApi.lua_tonumber(l,3);"); serializer.NewLine(string.Format("{0}Delegate d = LuaManager.Instance.GetObjectT<{0}Delegate>(id);", ed.Ei.Name)); serializer.NewLine("if(null != d)"); serializer.BeginBlock("{"); serializer.NewLine(string.Format("Instance.{0} -= d.Call;", ed.Ei.Name)); serializer.EndBlock("}"); serializer.NewLine("return 0;"); }); } #endregion }
private static void GenerateDefaultConstructor(ClassDescription cd, Lua4NetSerializer serializer) { serializer.NewLine(); string funcdef = string.Format("private static int DefaultConstructor({0} Instance,IntPtr l)", cd.ClassName); GeneratorHelper.GenerateCSFunction(funcdef, serializer, s => { serializer.NewLine(string.Format("Instance = new {0}();", cd.ClassName)); serializer.NewLine("int id = LuaManager.Instance.PushStackObject(Instance);"); serializer.NewLine("LuaApi.lua_pushnumber(l,id);"); serializer.NewLine("return 1;"); }); }
private static void GenerateConvertFrom(ClassDescription csd, Lua4NetSerializer serializer) { serializer.NewLine(); serializer.NewLine(string.Format("function {0}.ConvertFrom(t)", csd.GetNamespaceName(csd.ProxyName))); serializer.BeginBlock(string.Empty); serializer.NewLine(string.Format("return {0}.Get(t.Id)", csd.GetNamespaceName(csd.ProxyName))); serializer.EndBlock("end"); }
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); } } }
public void GenerateServant(ClassDescription cd, string path) { if (!allCSD.ContainsKey(cd.Class)) { if (cd.Class.IsEnum) { allCSD[cd.Class] = cd; csds.Enqueue(cd); } else { Generate(path, cd, GenerateServant); } } }
private static void GenerateRegisterFunction(ClassDescription cd, Lua4NetSerializer serializer) { serializer.NewLine(string.Format("public override void Register(IntPtr l)")); serializer.BeginBlock("{"); serializer.NewLine(string.Format("LuaApi.lua_pushstring(l,\"{0}\");", cd.ServantCallName)); serializer.NewLine(string.Format("LuaApi.lua_pushcfunction(l,{0});", cd.ServantCallName)); serializer.NewLine(string.Format("LuaApi.lua_settable(l,(int)LuaIndex.LUA_GLOBALSINDEX);")); serializer.EndBlock("}"); }
private static void GenerateStaticFields(ClassDescription cd,Lua4NetSerializer serializer) { serializer.NewLine(string.Format("delegate int Lua4NetFunc({0} Instance,IntPtr l);", cd.ClassName)); serializer.NewLine("private static Lua4NetFunc[] _Methods="); serializer.BeginBlock("{"); int methodid = 0; foreach (ConstructorDescription ctor in cd.Constructors) { serializer.NewLine(ctor.NickName+","); serializer.Apppend(string.Format(" // methodid = {0}", methodid++)); } foreach (MethodDescription md in cd.Methods) { serializer.NewLine(md.NickName + ","); serializer.Apppend(string.Format(" // methodid = {0}", methodid++)); } foreach (PropertyFieldDescription pfd in cd.Propertys) { if (pfd.GetMethod != null) { serializer.NewLine(pfd.GetMethod.NickName + ","); serializer.Apppend(string.Format(" // methodid = {0}", methodid++)); } if (pfd.SetMethod != null) { serializer.NewLine(pfd.SetMethod.NickName + ","); serializer.Apppend(string.Format(" // methodid = {0}", methodid++)); } } foreach (EventDescription ed in cd.Events) { if (ed.AddMethod != null) { serializer.NewLine(ed.AddMethod.Name + ","); serializer.Apppend(string.Format(" // methodid = {0}", methodid++)); } if (ed.RemoveMethod != null) { serializer.NewLine(ed.RemoveMethod.Name + ","); serializer.Apppend(string.Format(" // methodid = {0}", methodid++)); } } serializer.NewLine("null"); serializer.EndBlock("};"); }
private static void GetConstructor(Assembly a, ClassDescription result, Type type) { result.Constructors = new List<ConstructorDescription>(); foreach (ConstructorInfo ci in type.GetConstructors()) { ScriptableAttribute attr = GetAttribute(ci); if (null != attr) { ConstructorDescription ctordesc = new ConstructorDescription(); ctordesc.Ci = ci; GetMethod(ctordesc, ci, attr.Name, string.Empty); result.Constructors.Add(ctordesc); } } }
private void Generate(string path, ClassDescription cd, Func<ClassDescription, Lua4NetSerializer, string> GetContent) { Lua4NetSerializer serializer = new Lua4NetSerializer(0); List<string> namespaces = new List<string>() { "System", "Lua4Net" }; GenerateNamespace(cd, ns => { foreach (string n in ns) { if (!namespaces.Contains(n)) namespaces.Add(n); } }); namespaces.ForEach(n => { serializer.NewLine(string.Format("using {0};", n)); }); serializer.NewLine(); serializer.NewLine(string.Format("namespace {0}", "Lua4Net")); serializer.BeginBlock("{"); string file = GetContent(cd, serializer); serializer.EndBlock("}"); string fullpath = Path.Combine(path, file); string content = serializer.ToString(); Console.WriteLine(content); SaveAsFile(fullpath, content); }
private static void GetProperty(Assembly a, ClassDescription result, ScriptableClass sc) { result.Propertys = new List<PropertyFieldDescription>(); foreach (ScriptableProperty sp in sc.Property) { // PropertyInfo pi = result.Class.GetProperty(sp.Name,sp.GetArgs(result)); if (null != pi) { PropertyFieldDescription pfd = new PropertyFieldDescription(); pfd.Pi = pi; pfd.IsStatic = false; pfd.RawType = pi.PropertyType; pfd.TypeName = GeneratorHelper.GetTypeName(pfd.RawType); pfd.Name = pi.Name; pfd.Type = GetMessageFieldType(pi.PropertyType); pfd.IsItemProperty = string.Equals(sp.Name, "Item"); result.Propertys.Add(pfd); if (pi.GetGetMethod() != null && pi.GetGetMethod().IsStatic) { pfd.IsStatic = true; } if (pi.GetSetMethod() != null && pi.GetSetMethod().IsStatic) { pfd.IsStatic = true; } if (pi.GetGetMethod() != null) { pfd.GetMethod = new MethodDescription(); GetMethod(pfd.GetMethod,pi.GetGetMethod(), sp.Get,pi.Name); } if (pi.GetSetMethod() != null) { pfd.SetMethod = new MethodDescription(); GetMethod(pfd.SetMethod, pi.GetSetMethod(), sp.Set, pi.Name); } } else { Console.WriteLine(string.Format("property {0} not found", sp.Name)); } } }
private static void GetConstructor(Assembly a, ClassDescription result, ScriptableClass sc) { result.Constructors = new List<ConstructorDescription>(); foreach (ScriptableMethod ctor in sc.Constructor) { ConstructorInfo info = result.Class.GetConstructor(ctor.GetArgs(result)); if (null != info) { ConstructorDescription ctordesc = new ConstructorDescription(); ctordesc.Ci = info; ctordesc.Default = ctor.DefaultConstructor; GetMethod(ctordesc, info, string.IsNullOrEmpty(ctor.NickName)?ctor.Name:ctor.NickName,string.Empty); result.Constructors.Add(ctordesc); } else { Console.WriteLine(string.Format("constructor {0} not found,In = {1}", ctor.Name, ctor.In)); } } }
private static void GetEvent(Assembly a, ClassDescription result, Type type) { result.Events = new List<EventDescription>(); foreach (EventInfo ei in type.GetEvents()) { ScriptableAttribute attr = GetAttribute(ei); if (null != attr) { EventDescription ed = new EventDescription(); ed.Ei = ei; ed.RawType = ei.GetType(); ed.TypeName = GeneratorHelper.GetTypeName(ei.GetType()); ed.IsStatic = true; if (ei.GetAddMethod() != null) { ed.IsStatic = false; ed.AddMethod = new MethodDescription(); GetMethod(ed.AddMethod, ei.GetAddMethod(), string.Empty, ei.Name); } if (ei.GetRemoveMethod() != null) { ed.IsStatic = false; ed.RemoveMethod = new MethodDescription(); GetMethod(ed.RemoveMethod, ei.GetRemoveMethod(), string.Empty, ei.Name); } result.Events.Add(ed); } } }
private static void GetProperty(Assembly a, ClassDescription result, Type type) { result.Propertys = new List<PropertyFieldDescription>(); foreach (PropertyInfo pi in type.GetProperties()) { ScriptableAttribute attr = GetAttribute(pi); if (null != attr) { PropertyFieldDescription pfd = new PropertyFieldDescription(); pfd.Pi = pi; pfd.IsStatic = false; pfd.RawType = pi.PropertyType; pfd.TypeName = GeneratorHelper.GetTypeName(pfd.RawType); pfd.Name = pi.Name; pfd.Type = GetMessageFieldType(pi.PropertyType); pfd.IsItemProperty = string.Equals(attr.Name, "Item"); result.Propertys.Add(pfd); if (pi.GetGetMethod() != null && pi.GetGetMethod().IsStatic) { pfd.IsStatic = true; } if (pi.GetSetMethod() != null && pi.GetSetMethod().IsStatic) { pfd.IsStatic = true; } if (pi.GetGetMethod() != null) { pfd.GetMethod = new MethodDescription(); string getName = string.IsNullOrEmpty(attr.Name) ? "get_" + pi.Name : "get_" + attr.Name; GetMethod(pfd.GetMethod, pi.GetGetMethod(), getName, pi.Name); } if (pi.GetSetMethod() != null) { pfd.SetMethod = new MethodDescription(); string setName = string.IsNullOrEmpty(attr.Name) ? "set_" + pi.Name : "set_" + attr.Name; GetMethod(pfd.SetMethod, pi.GetSetMethod(), setName, pi.Name); } } } }
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 GenerateGetSetMethod(ClassDescription cd, PropertyFieldDescription pfd, Lua4NetSerializer serializer) { #region get if (pfd.GetMethod != null) { serializer.NewLine(); string funcdef = string.Format("private static int {0}({1} Instance,IntPtr l)", pfd.GetMethod.NickName, cd.ClassName); GeneratorHelper.GenerateCSFunction(funcdef, serializer, s => { string classOrInstance = pfd.IsStatic ? cd.ClassName : "Instance"; MethodFieldDescription mfd = pfd.GetMethod.Output; if (!pfd.IsItemProperty) { switch (pfd.GetMethod.Output.Type) { case MessageFieldType.NumberType: { switch (mfd.GetNumberType()) { case NumberType.Boolean: { serializer.NewLine(string.Format("LuaApi.lua_pushnumber(l,{1}.{0}?1:0);", pfd.Name, classOrInstance)); break; } case NumberType.Enum: { serializer.NewLine(string.Format("LuaApi.lua_pushnumber(l,(int){1}.{0});", pfd.Name, classOrInstance)); break; } case NumberType.Numeric: { serializer.NewLine(string.Format("LuaApi.lua_pushnumber(l,{1}.{0});", pfd.Name, classOrInstance)); break; } } break; } case MessageFieldType.StringType: { serializer.NewLine(string.Format("LuaApi.lua_pushstring(l,{1}.{0});", pfd.Name, classOrInstance)); break; } case MessageFieldType.DelegateType: case MessageFieldType.ClientType: { serializer.NewLine(string.Format("int id = LuaManager.Instance.PushStackObject({1}.{0});", pfd.Name, classOrInstance)); serializer.NewLine("LuaApi.lua_pushnumber(l,id);"); break; } } } else { MethodFieldDescription input = pfd.GetMethod.InputArgs[0]; switch (input.Type) { case MessageFieldType.NumberType: { serializer.NewLine("int index = (int)LuaApi.lua_tonumber(l,3);"); break; } case MessageFieldType.StringType: { serializer.NewLine("string index = LuaApi.lua_tostring(l,3);"); break; } case MessageFieldType.DelegateType: case MessageFieldType.ClientType: { serializer.NewLine("not supported...."); break; } } switch (pfd.GetMethod.Output.Type) { case MessageFieldType.NumberType: { switch (mfd.GetNumberType()) { case NumberType.Boolean: { serializer.NewLine(string.Format("LuaApi.lua_pushnumber(l,{0}[index]?1:0);", classOrInstance)); break; } case NumberType.Enum: { serializer.NewLine(string.Format("LuaApi.lua_pushnumber(l,(int){0}[index]);", classOrInstance)); break; } case NumberType.Numeric: { serializer.NewLine(string.Format("LuaApi.lua_pushnumber(l,{0}[index]);",classOrInstance)); break; } } break; } case MessageFieldType.StringType: { serializer.NewLine(string.Format("LuaApi.lua_pushstring(l,{0}[index]);", classOrInstance)); break; } case MessageFieldType.DelegateType: case MessageFieldType.ClientType: { serializer.NewLine(string.Format("int id = LuaManager.Instance.PushStackObject({0}[index]);", classOrInstance)); serializer.NewLine("LuaApi.lua_pushnumber(l,id);"); break; } } } serializer.NewLine("return 1;"); }); } #endregion #region set if (pfd.SetMethod != null) { string funcdef = string.Format("private static int {0}({1} Instance,IntPtr l)", pfd.SetMethod.NickName, cd.ClassName); GeneratorHelper.GenerateCSFunction(funcdef, serializer, s => { string classOrInstance = pfd.IsStatic ? cd.ClassName : "Instance"; foreach (MethodFieldDescription mfd in pfd.SetMethod.InputArgs) { switch (mfd.Type) { case MessageFieldType.NumberType: { switch (mfd.GetNumberType()) { case NumberType.Boolean: { serializer.NewLine(string.Format("{0}.{1} = LuaApi.lua_tonumber(l,3)!=0;", classOrInstance, pfd.Name)); break; } case NumberType.Enum: case NumberType.Numeric: { serializer.NewLine(string.Format("{0}.{1} = ({2})LuaApi.lua_tonumber(l,3);", classOrInstance, pfd.Name, pfd.TypeName)); break; } } break; } case MessageFieldType.StringType: { serializer.NewLine(string.Format("{0}.{1} = LuaApi.lua_tostring(l,3);", classOrInstance, pfd.Name)); break; } case MessageFieldType.DelegateType: case MessageFieldType.ClientType: { serializer.NewLine("int id = (int)LuaApi.lua_tonumber(l,3);"); serializer.NewLine(string.Format("{0}.{1} = LuaManager.Instance.GetObjectT<{2}>(id);", classOrInstance, pfd.Name, pfd.TypeName)); break; } } } serializer.NewLine("return 0;"); }); } #endregion }
private void GenerateNamespace(ClassDescription cd, Action<List<string>> OnAddNamespace) { OnAddNamespace(NamespaceGenerator.Generate(cd.Class, true)); cd.Constructors.ForEach(c => { OnAddNamespace(NamespaceGenerator.Generate(c.Ci)); }); cd.Propertys.ForEach(p => { OnAddNamespace(NamespaceGenerator.Generate(p.Pi.PropertyType, false)); MethodInfo mi = p.Pi.GetGetMethod(); if (null != mi) OnAddNamespace(NamespaceGenerator.Generate(mi)); mi = p.Pi.GetSetMethod(); if (null != mi) OnAddNamespace(NamespaceGenerator.Generate(mi)); }); cd.Methods.ForEach(m => { OnAddNamespace(NamespaceGenerator.Generate(m.Method)); }); cd.Events.ForEach(e => { MethodInfo mi = e.Ei.GetAddMethod(); if (null != mi) OnAddNamespace(NamespaceGenerator.Generate(mi)); mi = e.Ei.GetRemoveMethod(); if (null != mi) OnAddNamespace(NamespaceGenerator.Generate(mi)); }); }
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;"); } }); }
public Type[] GetArgs(ClassDescription cd) { List<Type> tps = new List<Type>(); if (!string.IsNullOrEmpty(In)) { foreach (string s in In.Split(',').ToList()) { Type tp = cd.GetType(s); if (null == tp) { tps.Add(tp); } } } return tps.ToArray(); }
private static void GenerateRootMethod(ClassDescription cd, Lua4NetSerializer serializer) { if (cd.aot) { serializer.NewLine("[AOT.MonoPInvokeCallback(typeof(LuaCSFunction))]"); } serializer.NewLine(string.Format("private static int {0}(IntPtr l)", cd.ServantCallName)); serializer.BeginBlock("{"); serializer.NewLine("try"); serializer.BeginBlock("{"); serializer.NewLine("// get object and methodid"); serializer.NewLine(string.Format("int nObjectId = (int)LuaApi.lua_tonumber(l,1);", cd.ClassName)); serializer.NewLine("int nMethodId = (int)LuaApi.lua_tonumber(l,2);"); serializer.NewLine(string.Format("{0} obj = LuaManager.Instance.GetObjectT<{0}>(nObjectId);", cd.ClassName)); serializer.NewLine(); serializer.NewLine("// call member function"); serializer.NewLine(string.Format("return _Methods[nMethodId](obj,l);", cd.ClassName)); serializer.EndBlock("}"); serializer.NewLine("catch(System.Exception ex)"); serializer.BeginBlock("{"); serializer.NewLine("LuaManager.Instance.Log(ex.Message);"); serializer.NewLine("return 0;"); serializer.EndBlock("}"); serializer.EndBlock("}"); }
private string GenerateServant(ClassDescription cd, Lua4NetSerializer serializer) { ServantGenerator.Generate(cd, serializer); csds.Enqueue(cd); allCSD.Add(cd.Class, cd); allServants.Add(cd.ServantName); return cd.CSFileName; }
private static void GetBaseDescription(Assembly a,ClassDescription result, ScriptableClass sc) { bool isNestedClass = false; string fullname = sc.Name; Queue<string> nestedClass = new Queue<string>(); while (fullname.Length > 0) { int index = fullname.IndexOf('+'); if (index == -1) { nestedClass.Enqueue(fullname); break; } else { string parentClassName = fullname.Substring(0, index); string nestedClassName = fullname.Substring(index + 1); nestedClass.Enqueue(parentClassName); fullname = nestedClassName; isNestedClass = true; } } Type parentType = null; Type thisType = null; string className = string.Empty; if (isNestedClass) { while (nestedClass.Count > 0) { string typeName = nestedClass.Dequeue(); if (parentType == null) { parentType = a.GetType(typeName); } else { thisType = parentType.GetNestedType(typeName); } } className = sc.Name.Replace('+', '.'); className = className.Remove(0, thisType.Namespace.Length + 1); } else { string typeName = nestedClass.Dequeue(); thisType = a.GetType(typeName); className = GeneratorHelper.GetTypeName(thisType); } result.Class = thisType; result.BaseClass = thisType.BaseType; result.HasDefaultConstructor = sc.HasDefaultConstructor; result.IsValueType = thisType.IsValueType; result.IsEnum = thisType.IsEnum; result.ClassName = className; result.ServantCallName = result.Class.Name + "Servant4LuaCall"; result.ServantName = result.Class.Name + "LuaServant"; result.ProxyName = result.Class.Name; string assemblyName = thisType.Assembly.GetName().Name; result.CSFileName = Path.Combine(assemblyName,thisType.Name+".cs"); result.LuaFileName = Path.Combine(assemblyName, thisType.Name + ".lua"); }
private static void GetEvent(Assembly a, ClassDescription result, ScriptableClass sc) { result.Events = new List<EventDescription>(); foreach (ScriptableEvent se in sc.Event) { EventInfo ei = result.Class.GetEvent(se.Name); if (null != ei) { EventDescription ed = new EventDescription(); ed.Ei = ei; ed.RawType = ei.GetType(); ed.TypeName = GeneratorHelper.GetTypeName(ei.GetType()); ed.IsStatic = true; if (ei.GetAddMethod() != null) { ed.IsStatic = false; ed.AddMethod = new MethodDescription(); GetMethod(ed.AddMethod,ei.GetAddMethod(), string.Empty,ei.Name); } if (ei.GetRemoveMethod() != null) { ed.IsStatic = false; ed.RemoveMethod = new MethodDescription(); GetMethod(ed.RemoveMethod, ei.GetRemoveMethod(), string.Empty, ei.Name); } result.Events.Add(ed); } else { Console.WriteLine(string.Format("event {0} not found", se.Name)); } } }