示例#1
0
        public SharpClass RegMethod(string name, MethodBase[] methodInfo, bool isAsync = false)
        {
            MethodReflection method = new MethodReflection(methodInfo);

#if !IL2CPP
            for (int i = 0; i < methodInfo.Length; i++)
            {
                var mi = methodInfo[i] as MethodInfo;
                if (mi != null)
                {
                    if (DelegateCache.GetMethodDelegate(classType, mi, "CallDel", out CallDel fn, out Delegate del))
                    {
                        method.luaFunc[i] = fn;
                        method.del[i]     = del;
                    }
                }
            }
#endif
            //Luna.Log("反射方式实现");
            LuaRef luaFun = LuaRef.CreateFunction(State, MethodReflection.Call, method);
            if (IsTagMethod(name, out var tag))
            {
                meta.RawSet(tag, luaFun);
            }
            else
            {
                meta.RawSet(name, luaFun);
            }

            if (classType.IsArray)
            {
                if (name == "Get")
                {
                    SetMemberFunction(LunaNative.___get_indexed, luaFun);
                }

                if (name == "Set")
                {
                    SetMemberFunction(LunaNative.___set_indexed, luaFun);
                }
            }

            if (isAsync)
            {
                LuaRef r = new LuaRef(State, "coroutine.__async");
                luaFun = r.Call <LuaRef>(luaFun);

                meta.RawSet("_async_" + name, luaFun);
            }

            return(this);
        }
示例#2
0
        public void OnRegClass()
        {
            var fields = classType.GetFields();

            foreach (var field in fields)
            {
                if (!field.IsPublic)
                {
                    continue;
                }

                if (field.IsLiteral)
                {
                    RegConstant(field);
                }
                else
                {
                    RegField(field);
                }
            }

            if (classInfo.TryGetValue("ctor", out var methodConfig))
            {
                meta.RawSet("__call", LuaRef.CreateFunction(State, methodConfig.func));
            }
            else
            {
                var ctors = classType.GetConstructors();
                List <MethodBase> memberInfo = new List <MethodBase>();
                foreach (var constructorInfo in ctors)
                {
                    if (!constructorInfo.IsPublic)
                    {
                        continue;
                    }

                    if (!constructorInfo.ShouldExport())
                    {
                        continue;
                    }

                    var paramInfos = constructorInfo.GetParameters();
                    foreach (var info in paramInfos)
                    {
                        if (!info.ParameterType.ShouldExport())
                        {
                            continue;
                        }
                    }

                    memberInfo.Add(constructorInfo);
                }

                if (memberInfo.Count > 0)
                {
                    RegMethod("__call", memberInfo.ToArray());
                }
            }

            var props = classType.GetProperties(BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static | BindingFlags.DeclaredOnly);

            foreach (var p in props)
            {
                if (!p.ShouldExport())
                {
                    continue;
                }

                RegProperty(p);
            }

            var methods = classType.GetMethods(BindingFlags.Public | BindingFlags.Static | BindingFlags.Instance | BindingFlags.DeclaredOnly);
            HashSet <string> registered = new HashSet <string>();

            foreach (var m in methods)
            {
                if (registered.Contains(m.Name))
                {
                    continue;
                }

                bool isAsync = m.GetCustomAttribute <LuaAsyncAttribute>() != null;

                if (classInfo.TryGetValue(m.Name, out methodConfig))
                {
                    if (methodConfig.func != null)
                    {
                        var fn = LuaRef.CreateFunction(State, methodConfig.func);
                        if (IsTagMethod(m.Name, out var tag))
                        {
                            meta.RawSet(tag, fn);
                        }
                        else
                        {
                            meta.RawSet(m.Name, fn);
                        }

                        if (isAsync)
                        {
                            LuaRef r = new LuaRef(State, "coroutine.__async");
                            fn = r.Call <LuaRef>(fn);

                            meta.RawSet("_async_" + m.Name, fn);
                        }

                        if (classType.IsArray)
                        {
                            if (m.Name == "Get")
                            {
                                SetMemberFunction(LunaNative.___get_indexed, fn);
                            }

                            if (m.Name == "Set")
                            {
                                SetMemberFunction(LunaNative.___set_indexed, fn);
                            }
                        }
                    }

                    registered.Add(m.Name);
                    continue;
                }

                if (!m.ShouldExport())
                {
                    continue;
                }

                var memberInfo = classType.GetMember(m.Name).Cast <MethodBase>().ToArray();
                registered.Add(m.Name);
                if (memberInfo.Length > 0)
                {
                    RegMethod(m.Name, memberInfo, isAsync);
                }
            }
        }