public static void GenerateMethod(string methodName, Type type, Lua4NetSerializer serializer, Action<List<Type>> OnGetBody)
 {
     serializer.NewLine(methodName + "(");
     List<Type> args = new List<Type>();
     if (type.IsGenericType)
         args = type.GetGenericArguments().ToList();
     else
     {
         (type.GetMembers()[0] as MethodBase).GetParameters().ToList().ForEach(pi =>
         {
             args.Add(pi.ParameterType);
         }); ;
     }
     int nNextArgIndex = 0;
     while (nNextArgIndex < args.Count)
     {
         Type arg = args[nNextArgIndex];
         serializer.Apppend(GeneratorHelper.GetTypeName(arg) + " arg" + nNextArgIndex.ToString());
         ++nNextArgIndex;
         if (nNextArgIndex <= (args.Count - 1))
             serializer.Apppend(",");
     }
     serializer.Apppend(")");
     serializer.BeginBlock("{");
     OnGetBody(args);
     serializer.EndBlock("}");
 }
        public static void GenerateMethodCall(string methodName, string[] definedArgs, List<Type> args, Lua4NetSerializer serializer)
        {
            serializer.NewLine(string.Format("{0}(", methodName));

            if (null != definedArgs && definedArgs.Length > 0)
            {
                int definedArgIndex = 0;
                while (definedArgIndex < definedArgs.Length)
                {
                    serializer.Apppend(definedArgs[definedArgIndex]);
                    ++definedArgIndex;
                    while (definedArgIndex <= (definedArgs.Length - 1))
                        serializer.Apppend(",");
                }
            }

            if (null != args && args.Count > 0)
            {
                if (null != definedArgs && definedArgs.Length > 0)
                    serializer.Apppend(",");

                int nNextArgIndex = 0;
                while (nNextArgIndex < args.Count)
                {
                    serializer.Apppend("arg" + nNextArgIndex.ToString());
                    ++nNextArgIndex;
                    if (nNextArgIndex <= (args.Count - 1))
                        serializer.Apppend(",");
                }
            }
            serializer.Apppend(");");
        }
 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 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;");
                }
            });
        }
Exemple #5
0
        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 GenerateServantRoot(string path)
        {
            Lua4NetSerializer serializer = new Lua4NetSerializer(0);
            serializer.NewLine("using System;");
            serializer.NewLine("namespace Lua4Net");
            serializer.BeginBlock("{");

            serializer.NewLine();
            serializer.NewLine("public class Lua4NetRoot : LuaRegister");
            serializer.BeginBlock("{");

            serializer.NewLine("private LuaRegister[] allRegister = ");
            serializer.BeginBlock("{");
            for (int i = 0; i < allServants.Count; ++i)
            {
                serializer.NewLine(string.Format("new {0}()", allServants[i]));
                if (i != (allServants.Count - 1))
                    serializer.Apppend(",");
            }
            serializer.EndBlock("};");

            serializer.NewLine();
            serializer.NewLine("public override void Register(IntPtr l)");
            serializer.BeginBlock("{");
            serializer.NewLine("foreach(LuaRegister r in allRegister)");
            serializer.BeginBlock("{");
            serializer.NewLine("r.Register(l);");
            serializer.EndBlock("}");
            serializer.EndBlock("}");

            serializer.EndBlock("}");

            serializer.EndBlock("}");

            string fullpath = Path.Combine(path, "Lua4NetRoot.cs");
            string content = serializer.ToString();
            SaveAsFile(fullpath, content);
        }
Exemple #7
0
 private static void GenerateSetPropertys(ClassDescription csd, Lua4NetSerializer serializer)
 {
     serializer.NewLine();
     serializer.NewLine(csd.GetNamespaceName(csd.ProxyName) + ".__SetProperty = ");
     serializer.BeginBlock("{");
     int nNextProp = 0;
     while (nNextProp < csd.Propertys.Count)
     {
         PropertyFieldDescription pfd = csd.Propertys[nNextProp];
         if (pfd.SetMethod != null)
         {
             serializer.NewLine(string.Format("{0} = {1}.{2}", pfd.Name, csd.GetNamespaceName(csd.ProxyName), pfd.SetMethod.Name));
         }
         ++nNextProp;
         if (nNextProp <= (csd.Propertys.Count - 1) && pfd.SetMethod != null)
         {
             serializer.Apppend(",");
         }
     }
     serializer.EndBlock("}");
 }
Exemple #8
0
        private static void GenerateNewIndex(ClassDescription csd, Lua4NetSerializer serializer)
        {
            serializer.NewLine();
            serializer.NewLine(string.Format("function {0}.__SetByIndex(t,k,v)", csd.GetNamespaceName(csd.ProxyName)));
            serializer.BeginBlock("");
            serializer.NewLine(string.Format("local pf = {0}.__SetProperty[k]", csd.GetNamespaceName(csd.ProxyName)));
            serializer.NewLine("if pf ~= nil then");
            serializer.BeginBlock("");
            serializer.Apppend("        pf(t,v)");
            serializer.NewLine("return");
            serializer.EndBlock("end");

            if (csd.HasBaseClassDescription)
            {
                serializer.NewLine(string.Format("{0}.__SetByIndex(t,k,v)", csd.GetBaseClassProxyName()));
                serializer.NewLine("return");
            }
            else
            {
                serializer.NewLine("print(k .. ' is not a property')");
            }
            serializer.EndBlock("end");
        }
Exemple #9
0
        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");
        }
Exemple #10
0
        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)));
        }
Exemple #11
0
        private static void GenerateIndex(ClassDescription csd, Lua4NetSerializer serializer)
        {
            serializer.NewLine();
            serializer.NewLine(string.Format("function {0}.__GetByIndex(t,k)", csd.GetNamespaceName(csd.ProxyName)));
            serializer.BeginBlock("");
                serializer.NewLine(string.Format("local pf = {0}.__GetProperty[k]", csd.GetNamespaceName(csd.ProxyName)));
                serializer.NewLine("if pf ~= nil then");
                serializer.BeginBlock("");
                    serializer.Apppend("    return pf(t)");
                serializer.EndBlock("end");

                serializer.NewLine(string.Format("local f = {0}.__Method[k]", csd.GetNamespaceName(csd.ProxyName)));
                serializer.NewLine("if f ~= nil then");
                serializer.BeginBlock("");
                serializer.Apppend("    return f");
                serializer.EndBlock("end");
                if (csd.HasBaseClassDescription)
                {
                    serializer.NewLine(string.Format("return {0}.__GetByIndex(t,k)", csd.GetBaseClassProxyName()));
                }
                else
                {
                    serializer.NewLine("return nil");
                }
            serializer.EndBlock("end");
        }