Ejemplo n.º 1
0
    private static IEnumerator GenAllNameSpace()
    {
        yield return(null);

        List <string> noNameSpaceList = new List <string>();
        List <string> nameSpaceList   = new List <string>();
        List <Type>   typeList        = CommonUtility.GetAllTypes();

        for (int i = 0; i < typeList.Count; i++)
        {
            Type type = typeList[i];
            if (i % 100 == 0)
            {
                yield return(null);

                EditorUtility.DisplayProgressBar("Dealing", i + "/" + typeList.Count, i * 1f / typeList.Count);
            }
            if (string.IsNullOrEmpty(type.Namespace))
            {
                string typeName = type.FullName.Split('+')[0];
                if (!noNameSpaceList.Contains(typeName))
                {
                    noNameSpaceList.Add(typeName);
                }
            }
            else
            {
                try
                {
                    string typeName = type.FullName.Split('.')[0];
                    if (!nameSpaceList.Contains(typeName))
                    {
                        nameSpaceList.Add(typeName);
                    }
                }
                catch (System.Exception ex)
                { }
            }
        }
        EditorUtility.ClearProgressBar();
        nameSpaceList.Sort();
        noNameSpaceList.Sort();

        StringBuilder stringBuilder = new StringBuilder();

        stringBuilder.AppendLine("CS = {");
        foreach (string nameSpace in nameSpaceList)
        {
            stringBuilder.Append("\t");
            stringBuilder.Append(nameSpace);
            stringBuilder.Append(" = ");
            stringBuilder.Append(nameSpace);
            stringBuilder.AppendLine(";");
        }
        stringBuilder.AppendLine();
        foreach (string noNameSpace in noNameSpaceList)
        {
            if (noNameSpace.Contains("AnonType") || noNameSpace.Contains("PrivateImplementationDetails") || noNameSpace.Contains("AnonymousType"))
            {
                continue;
            }
            string curNoNameSpace = noNameSpace;
            if (curNoNameSpace.Contains("`"))
            {
                int i = curNoNameSpace.LastIndexOf("`");
                curNoNameSpace = curNoNameSpace.Substring(0, i);
            }
            stringBuilder.Append("\t");
            stringBuilder.Append(curNoNameSpace);
            stringBuilder.Append(" = ");
            stringBuilder.Append(curNoNameSpace);
            stringBuilder.AppendLine(";");
        }
        stringBuilder.Append("}");

        string luaScriptFileName = NameSpaceZipName + ".lua";

        byte[] luaScriptsBytes = Encoding.UTF8.GetBytes(stringBuilder.ToString());
        Dictionary <string, byte[]> zipFileDict = new Dictionary <string, byte[]>()
        {
            { luaScriptFileName, luaScriptsBytes }
        };
        string zipFileName = CommonLoadPath.CreateLuaGrammarTipPath + "/" + NameSpaceZipName + ".zip";

        ZipManager.CreateZip(zipFileName, zipFileDict);
        CSDebug.Log("成功创建 " + zipFileName);
    }
Ejemplo n.º 2
0
    private static void GenAllMethod()
    {
        if (!Directory.Exists(CommonLoadPath.CreateCSClientTipPath))
        {
            Directory.CreateDirectory(CommonLoadPath.CreateCSClientTipPath);
        }
        List <Type> typeList = CommonUtility.GetAllTypes();

        for (int i = 0; i < typeList.Count; i++)
        {
            Type type = typeList[i];
            if (IgnoreTypeList.Contains(type))
            {
                continue;
            }
            string fileName = CommonUtility.GetTypeFileName(type) + "_Wrap.lua";
            EditorUtility.DisplayProgressBar("生成中", "生成" + fileName, (float)(i + 1) / typeList.Count);
            List <string> NoTipsGenericList = new List <string>();
            if (IsContainsIgnoreSubClass(type))
            {
                if (NoTipsGenericList.Contains(type.Name) == false)
                {
                    NoTipsGenericList.Add(type.Name);
                }
                continue;
            }
            try
            {
                GenNameSpaceFile(type);
                using (StreamWriter writer = new StreamWriter(CommonLoadPath.CreateCSClientTipPath + fileName, false, System.Text.Encoding.UTF8))
                {
                    writer.Write("---@class ");
                    writer.Write(CommonUtility.GetTypeTagName(type));
                    MethodInfo methodInfo = null;
                    if (type.IsEnum)
                    {
                        writer.Write("\r\n");
                        foreach (var item in Enum.GetValues(type))
                        {
                            writer.Write("---@field ");
                            writer.Write(item);
                            writer.Write(" @");
                            writer.WriteLine((int)item);
                        }
                    }
                    else
                    {
                        //基类
                        var baseType = type.BaseType;
                        if (baseType != null && baseType != typeof(object))
                        {
                            writer.Write(" : ");
                            writer.WriteLine(CommonUtility.GetTypeTagName(baseType));
                        }
                        else
                        {
                            writer.WriteLine();
                        }
                        if (baseType != null)
                        {
                            if (baseType.Name == "Singleton`1")
                            {
                                writer.WriteLine("---@field public Instance " + type.FullName);
                            }
                            else if (baseType.Name.Contains("`"))
                            {
                                if (!NoTipsGenericList.Contains(baseType.Name))
                                {
                                    NoTipsGenericList.Add(baseType.Name);
                                }
                            }
                            else if (baseType.Name == "TableManager`4")
                            {
                                methodInfo = type.GetMethod("TryGetValue");//TryGetValue在基类,后面的WriteMethods没有处理它
                                writer.WriteLine("---@field public Instance " + type.FullName);
                            }
                        }
                        WriteFields(writer, type);
                        WriteProperties(writer, type);
                        writer.WriteLine("local m = {};");
                        WriteMethods(writer, type);
                        if (methodInfo != null)
                        {
                            WriteMethods(writer, methodInfo);
                        }
                    }
                    writer.WriteLine(type.GetTypeTagName() + "=m");
                    writer.Write("return m;");
                }
            }
            catch (System.Exception ex)
            {
                CSDebug.Log(fileName + " is not Gen");
            }
        }
        EditorUtility.DisplayProgressBar("正在生成zip", "", 1);

        DirectoryInfo wrapDir = new DirectoryInfo(CommonLoadPath.CreateCSClientTipPath);

        FileInfo[] files = wrapDir.GetFiles("*", SearchOption.AllDirectories);
        Dictionary <string, byte[]> fileDict = new Dictionary <string, byte[]>();

        foreach (var cur in files)
        {
            string fileName = CommonUtility_Filed.GetFileName(cur.FullName) + ".lua";
            if (fileDict.ContainsKey(fileName))
            {
                continue;
            }
            string content        = File.ReadAllText(cur.FullName, Encoding.UTF8);
            byte[] luaScriptBytes = Encoding.UTF8.GetBytes(content);
            fileDict.Add(fileName, luaScriptBytes);
        }
        ZipManager.CreateZip(CommonLoadPath.CreateLuaGrammarTipPath + "/CSClient.zip", fileDict);
        EditorUtility.ClearProgressBar();
    }