EmmyLua Tolua代碼提示

EmmyLua 配置教程參考博文:https://blog.csdn.net/sinat_24229853/article/details/79226608

博文中代碼部分修改如下:
1.在包含Tolua的項目中,在ToluaMenu.cs 中添加下列代碼

[MenuItem("Lua/Gen EmmyLuaApi", false, 103)]
    static void GenEmmyLuaApi()
    {
        string path = "./EmmyLuaApi/";
        if (Directory.Exists(path))
        {
            Directory.Delete(path, true);
        }
        Directory.CreateDirectory(path);
        GenCustom(path);
    }

    static void GenCustom(string path)
    {
        BindType[] typeList = CustomSettings.customTypeList;

        BindType[] list = GenBindTypes(typeList);
        List<Type> tlist = new List<Type>(baseType);
        foreach (var item in list)
        {
            GenType(item.type, true, path, item);
        }

        foreach (var item in tlist)
        {
            GenType(item, true, path);
        }
    }
    static void GenType(Type t, bool custom, string path, ToLuaMenu.BindType bindType = null)
    {
        if (!CheckType(t, custom))
            return;
        //TODO System.MulticastDelegate
        var sb = new StringBuilder();
        string className = bindType != null ? bindType.libName : t.Name;
        if (!CheckType(t.BaseType, custom))
            sb.AppendFormat("---@class {0}\n", t.Name);
        else
            sb.AppendFormat("---@class {0} : {1}\n", t.Name, t.BaseType.Name);
        GenTypeField(t, sb);
        sb.AppendFormat("local {0}={{ }}\n", t.Name);

        GenTypeMehod(t, sb);

        sb.AppendFormat("{0}.{1} = {2}", t.Namespace, t.Name, t.Name);

        if (className != t.Name)
        {
            sb.AppendFormat("\n{0} = {1}", className, t.Name);
        }

        File.WriteAllText(path + t.Name + ".lua", sb.ToString(), Encoding.UTF8);
    }

    static bool CheckType(Type t, bool custom)
    {
        if (t == null)
        {
            return false;
        }
        return !BindType.IsObsolete(t);
    }

    static void GenTypeField(Type t, StringBuilder sb)
    {
        FieldInfo[] fields = t.GetFields(BindingFlags.Public | BindingFlags.Static | BindingFlags.Instance | BindingFlags.DeclaredOnly);
        foreach (var field in fields)
        {
            if (field.IsDefined(typeof(NoToLuaAttribute), false))
                continue;
            sb.AppendFormat("---@field public {0} {1}\n", field.Name, GetLuaType(field.FieldType));
        }
        PropertyInfo[] properties = t.GetProperties(BindingFlags.Public | BindingFlags.Static | BindingFlags.Instance | BindingFlags.DeclaredOnly);
        foreach (var pro in properties)
        {
            if (pro.IsDefined(typeof(NoToLuaAttribute), false))
                continue;
            sb.AppendFormat("---@field public {0} {1}\n", pro.Name, GetLuaType(pro.PropertyType));
        }
    }

    static void GenTypeMehod(Type t, StringBuilder sb)
    {
        MethodInfo[] methods = t.GetMethods(BindingFlags.Public | BindingFlags.Static | BindingFlags.Instance | BindingFlags.DeclaredOnly);
        foreach (var method in methods)
        {
            if (method.IsGenericMethod)
                continue;
            if (method.IsDefined(typeof(NoToLuaAttribute), false))
                continue;
            if (method.Name.StartsWith("get_") || method.Name.StartsWith("set_"))
                continue;
            sb.AppendLine("---@public");
            var paramstr = new StringBuilder();
            foreach (var param in method.GetParameters())
            {
                sb.AppendFormat("---@param {0} {1}\n", param.Name, GetLuaType(param.ParameterType));
                if (paramstr.Length != 0)
                {
                    paramstr.Append(", ");
                }
                paramstr.Append(param.Name);
            }
            sb.AppendFormat("---@return {0}\n", method.ReturnType == null ? "void" : GetLuaType(method.ReturnType));
            if (method.IsStatic)
            {
                sb.AppendFormat("function {0}.{1}({2}) end\n", t.Name, method.Name, paramstr);
            }
            else
            {
                sb.AppendFormat("function {0}:{1}({2}) end\n", t.Name, method.Name, paramstr);
            }
        }
    }

    static string GetLuaType(Type t)
    {
        if (t.IsEnum
            //|| t == typeof(ulong)
            //|| t == typeof(long)
            //|| t == typeof(int)
            //|| t == typeof(uint)
            //|| t == typeof(float)
            || t == typeof(double)
            //|| t == typeof(byte)
            //|| t == typeof(ushort)
            //|| t == typeof(short)
            )
            return "number";
        if (t == typeof(bool))
            return "bool";
        if (t == typeof(string))
            return "string";
        if (t == typeof(void))
            return "void";

        return t.Name;
    }

2.點擊Unity菜單欄中Lua/Gen EmmyLuaApi
3.即可生成EmmyLuaApi文件夾

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章