예제 #1
0
    public static void GenerateClassWraps()
    {
        if (!beAutoGen && EditorApplication.isCompiling)
        {
            EditorUtility.DisplayDialog("警告", "请等待编辑器完成编译再执行此功能", "确定");
            return;
        }
        LuaIdeClassDoc.checkDoc();
        LuaIdeInfo.luaInfos = new List <LuaIdeInfo>();
        if (!File.Exists(CustomSettings.saveDir))
        {
            Directory.CreateDirectory(CustomSettings.saveDir);
        }
        List <ToLuaMenu.BindType> gtlist = new List <ToLuaMenu.BindType>()
        {
            CustomSettings._GT(typeof(Vector3)),
            CustomSettings._GT(typeof(Vector2)),
            CustomSettings._GT(typeof(Vector4)),
            CustomSettings._GT(typeof(Color)),
            CustomSettings._GT(typeof(Quaternion)),
            CustomSettings._GT(typeof(Ray)),
            CustomSettings._GT(typeof(Bounds)),
            CustomSettings._GT(typeof(Touch)),
            CustomSettings._GT(typeof(RaycastHit)),
            CustomSettings._GT(typeof(LayerMask)),
        };

        for (int i = 0; i < CustomSettings.customTypeList.Length; i++)
        {
            gtlist.Add(CustomSettings.customTypeList[i]);
        }


        allTypes.Clear();
        ToLuaMenu.BindType[] typeList = gtlist.ToArray();

        ToLuaMenu.BindType[] list = GenBindTypes(typeList);
        LuaIdeExport.allTypes.AddRange(baseType);

        for (int i = 0; i < list.Length; i++)
        {
            LuaIdeExport.allTypes.Add(list[i].type);
        }
        StringBuilder luasb = new StringBuilder();

        for (int i = 0; i < list.Length; i++)
        {
            LuaIdeExport.Clear();
            LuaIdeExport.className     = list[i].name;
            LuaIdeExport.type          = list[i].type;
            LuaIdeExport.isStaticClass = list[i].IsStatic;
            LuaIdeExport.baseType      = list[i].baseType;
            LuaIdeExport.wrapClassName = list[i].wrapName;
            LuaIdeExport.libClassName  = list[i].libName;
            LuaIdeExport.extendList    = list[i].extendList;
            if (LuaIdeExport.className == "Input")
            {
                string ss = "1";
            }
            LuaIdeExport.Generate(CustomSettings.saveDir);
            if (LuaIdeInfo.luaInfo.tableName == null)
            {
                string ss = "1";
            }
            LuaIdeInfo.luaInfos.Add(LuaIdeInfo.luaInfo);
        }
        foreach (LuaIdeInfo luainfo in LuaIdeInfo.luaInfos)
        {
            if (luainfo.tableName == "UnityEngine.CameraClearFlags")
            {
                string ss = "1";
            }
            if (luainfo.tableName != null)
            {
                luasb.AppendLine(luainfo.toStr());
            }
        }

        DirectoryInfo dirinfo = new DirectoryInfo(Application.dataPath);
        string        dir     = dirinfo.FullName + "/LuaIde/";

        if (!Directory.Exists(dir))
        {
            Directory.CreateDirectory(dir);
        }
        string filename = dir + "uluaApi.lua";

        using (StreamWriter textWriter = new StreamWriter(filename, false, Encoding.UTF8))
        {
            textWriter.Write(luasb.ToString());
            textWriter.Flush();
            textWriter.Close();
        }
        Debug.Log("Api 创建成功," + filename);
        LuaIdeExport.allTypes.Clear();
        allTypes.Clear();
        AssetDatabase.Refresh();
    }
예제 #2
0
    public string toStr()
    {
        StringBuilder sb = new StringBuilder();

        if (IsEnum)
        {
            sb.AppendLine("--" + tableName + "  Enum");
        }
        else
        {
            if (this.baseName != null)
            {
                sb.AppendLine("--@SuperType [luaIde#" + this.baseName + "]");
            }
        }
        sb.AppendLine(tableName + " = {}");
        //字段

        foreach (LuaIdeVarInfo info in varInfos)
        {
            string varName   = "";
            string commenStr = "--[[\n";
            Type   type_     = null;
            if (info.fieldInfo != null)
            {
                type_   = info.fieldInfo.FieldType;
                varName = info.fieldInfo.Name;
            }
            else if (info.propertyInfo != null)
            {
                type_   = info.propertyInfo.PropertyType;
                varName = info.propertyInfo.Name;
            }
            else if (info.eventInfo != null)
            {
                type_   = info.eventInfo.EventHandlerType;
                varName = info.eventInfo.Name;
            }
            bool   isadd   = false;
            string type__  = getTypeStr(type_, out isadd);
            string typestr = "";
            if (isadd && !type_.IsEnum)
            {
                typestr = "\t@RefType [luaIde#" + type__ + "]";
            }
            else
            {
                if (!IsEnum)
                {
                    typestr = "\t" + type__;
                }
            }



            commenStr += typestr + "\n";
            if (info.isget)
            {
                commenStr += "\t Get ";
            }
            if (info.isset)
            {
                commenStr += "\t Set ";
            }
            string str = tableName + "." + varName;
            string doc = LuaIdeClassDoc.getDoc(str);
            if (info.value != "")
            {
                varName += " = " + info.value;
            }
            else
            {
                varName += " = nil";
            }
            sb.AppendLine(commenStr);

            if (doc != "")
            {
                sb.AppendLine(doc);
            }
            sb.AppendLine("--]]");
            sb.AppendLine(tableName + "." + varName);
        }
        if (this.isDefConstructorInfo)
        {
            string commenStr = "--[[\n";
            commenStr += "\t@return [luaIde#" + tableName + "]\n";
            commenStr += "]]";
            sb.AppendLine("function " + tableName + ":New () end");
        }
        else
        {
            if (this.ctorList != null)
            {
                foreach (ConstructorInfo info in this.ctorList)
                {
                    string commenStr = "--[[\n";

                    ParameterInfo[] parameterInfos = info.GetParameters();
                    string          parstr         = "";
                    for (int i = 0; i < parameterInfos.Length; i++)
                    {
                        ParameterInfo param    = parameterInfos[i];
                        string        paramStr = "";

                        if (param.Name == "end")
                        {
                            paramStr = param.Name + "_";
                        }
                        else
                        {
                            paramStr = param.Name;
                        }
                        parstr += paramStr;
                        bool isadd = false;
                        paramStr   = "\t@" + paramStr + " " + getTypeStr(param.ParameterType, out isadd);
                        paramStr  += "\n";
                        commenStr += paramStr;
                        if (i < parameterInfos.Length - 1)
                        {
                            parstr += ",";
                        }
                    }
                    commenStr += "\t@return [luaIde#" + tableName + "]\n";
                    commenStr += "]]";
                    sb.AppendLine(commenStr);
                    sb.AppendLine("function " + tableName + ":New(" + parstr + ") end");
                }
            }
        }
        //方法
        foreach (MethodInfo m in methods)
        {
            string commenStr = "--[[\n";
            string fname     = m.Name;
            if (fname == "get_Item" && IsThisArray(m, 1))
            {
                fname = "geti";
            }
            else if (fname == "set_Item" && IsThisArray(m, 2))
            {
                fname = "seti";
            }
            LuaIdeClassDocInfo docInfo = LuaIdeClassDoc.getLuaIdeClassDocInfo(tableName + "." + m.Name);
            if (docInfo != null && docInfo.doc != "")
            {
                commenStr += docInfo.doc + "\n";
            }
            ParameterInfo[] parameterInfos = m.GetParameters();
            string          parstr         = "";
            for (int i = 0; i < parameterInfos.Length; i++)
            {
                ParameterInfo param    = parameterInfos[i];
                string        paramStr = "";

                if (param.Name == "end")
                {
                    paramStr = param.Name + "_";
                }
                else
                {
                    paramStr = param.Name;
                }
                parstr += paramStr;
                bool isadd = false;
                paramStr = "\t@" + paramStr + " " + getTypeStr(param.ParameterType, out isadd);
                if (docInfo != null)
                {
                    if (docInfo.paraminfo.ContainsKey(param.Name))
                    {
                        paramStr += docInfo.paraminfo[param.Name] + "\n";
                    }
                    else
                    {
                        paramStr += "\n";
                    }
                }
                else
                {
                    paramStr += "\n";
                }


                commenStr += paramStr;
                if (i < parameterInfos.Length - 1)
                {
                    parstr += ",";
                }
            }

            if (m.ReturnType != null && m.ReturnType.FullName != "System.Void")
            {
                bool   isadd  = false;
                string type__ = this.getTypeStr(m.ReturnType, out isadd);
                if (isadd && !m.ReturnType.IsEnum)
                {
                    commenStr += "\t@return [luaIde#" + type__ + "]\n";
                }
                else
                {
                    commenStr += "\treturn " + type__ + "\n";
                }
            }
            commenStr += "--]]";
            if (parameterInfos.Length > 0 || (docInfo != null && docInfo.doc != "") || m.ReturnType == null && m.ReturnType.FullName == "System.Void")
            {
                sb.AppendLine(commenStr);
            }
            sb.AppendLine("function " + tableName + ":" + fname + "(" + parstr + ") end");
        }
        return(sb.ToString());
    }