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());
 }
Exemple #2
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 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 GenerateCSFunction(string definition,Lua4NetSerializer serializer,Action<Lua4NetSerializer> OnGetBody)
 {
     serializer.NewLine(definition);
     serializer.BeginBlock("{");
         OnGetBody(serializer);
     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(");");
        }
        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");
            });
        }
Exemple #7
0
        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;");
            });
        }
 public static string Generate(string path,ScriptableXml sxml)
 {
     List<string> allNamespaces = new List<string>();
     sxml.Assembly.ForEach(s =>
     {
         if (!string.IsNullOrEmpty(s.Namespace))
             allNamespaces.Add(s.Namespace);
     });
     Lua4NetSerializer serializer = new Lua4NetSerializer(0);
     allNamespaces.ForEach(n =>
     {
         serializer.NewLine(string.Format("{0} = { }",n));
     });
     return Path.Combine(path, "namespace.lua");
 }
        public static void Generate(EventInfo ei, Lua4NetSerializer serializer)
        {
            serializer.NewLine();
            serializer.NewLine(string.Format("class {0}Delegate:LuaRefFunction", ei.Name));
            serializer.BeginBlock("{");
            // 构造函数
            serializer.NewLine(string.Format("public {0}Delegate(int ref_,IntPtr l)", ei.Name));
            serializer.NewLine("    :base(ref_,l)");
            serializer.BeginBlock("{");
            serializer.EndBlock("}");

            // 调用函数
            serializer.NewLine();
            GeneratorHelper.GenerateMethod("public void Call", ei.EventHandlerType, serializer, (args) =>
            {
                GeneratorHelper.GenerateMethodCall("LuaManager.Instance.Call", new string[] { "this" }, args, serializer);
            });
            serializer.EndBlock("}");
        }
        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 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 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 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 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
        }
        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 #17
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");
        }
Exemple #18
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 #19
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)));
        }
        public void GenerateProxy(string path,ScriptableXml sxml)
        {
            List<string> allfile = new List<string>();
            GenerateProxyImpl(csd =>
            {
                if (csd.IsEnum)
                {
                    GenerateEnum(csd, path);
                    allfile.Add(csd.LuaFileName);
                }
                else
                {
                    Lua4NetSerializer serializer = new Lua4NetSerializer(0);
                    ProxyGenerator.Generate(csd, serializer);

                    string content = serializer.ToString();
                    Console.WriteLine(content);

                    string filename = csd.LuaFileName;
                    string fullpath = Path.Combine(path, filename);
                    allfile.Add(filename);

                    SaveAsFile(fullpath, content);
                }
            });

            Lua4NetSerializer rs = new Lua4NetSerializer(0);
            rs.NewLine();

            List<string> allNamespaces = new List<string>();
            sxml.Assembly.ForEach(s =>
            {
                if (!string.IsNullOrEmpty(s.Namespace))
                {
                    GetNamespace(s.Namespace).ForEach(ns=>
                    {
                        if (!allNamespaces.Contains(ns))
                        {
                            allNamespaces.Add(ns);
                        }
                    });
                }
            });

            if (allNamespaces.Count > 0)
            {
                allNamespaces.ForEach(n =>
                {
                    rs.NewLine(n + " = { }");
                });
            }

            allfile.ForEach(s =>
            {
                char ch1 = '\\';
                char ch2 = '/';
                s = s.Replace(ch1, ch2);
                rs.NewLine(string.Format("dofile('{0}')", s));
            });
            rs.NewLine("print('load Lua4NetRoot done!')");
            string rp = Path.Combine(path, "Lua4NetRoot.lua");
            SaveAsFile(rp, rs.ToString());
        }
Exemple #21
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 #22
0
 private static void GenerateGet(ClassDescription csd, Lua4NetSerializer serializer)
 {
     serializer.NewLine();
     serializer.NewLine(string.Format("function {0}.Get(id)", csd.GetNamespaceName(csd.ProxyName)));
     serializer.BeginBlock(string.Empty);
         serializer.NewLine("local t = { Id = id }");
         serializer.NewLine(string.Format("setmetatable(t,{0}.MetaTable)", csd.GetNamespaceName(csd.ProxyName)));
         serializer.NewLine("return t");
         serializer.NewLine();
     serializer.EndBlock("end");
 }
Exemple #23
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("}");
 }
        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 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 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 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;
        }
Exemple #28
0
 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");
 }