Exemplo n.º 1
0
        public LuaClass GetDynamicRegisterData(Type type)
        {
            LuaClass luaClass = null;

            if (dynamicRegisterDic.TryGetValue(type, out luaClass))
            {
                return(luaClass);
            }
            return(null);
        }
Exemplo n.º 2
0
        public static int DoCreate(IntPtr L)
        {
            LuaState lua        = LuaInstance.Instance.lua;
            int      classIndex = lua.ToInteger(lua.UpvalueIndex(1));
            LuaClass luaClass   = LuaInstance.Instance.RegisterData.GetDynamicRegisterData(classIndex);
            Type     classType  = luaClass.registerType;
            object   obj        = classType.Assembly.CreateInstance(classType.FullName);

            lua.NewClassUserData(obj);
            return(1);
        }
Exemplo n.º 3
0
        static int DoCallBack(LuaState lua, object obj, int indexInStack)
        {
            int id = lua.ToInteger(lua.UpvalueIndex(1));

            int classIndex  = 0;
            int methodIndex = 0;

            SpritInt(id, ref methodIndex, ref classIndex);

            LuaClass luaClass = LuaInstance.Instance.RegisterData.GetDynamicRegisterData(classIndex);

            ParameterInfo[] ps         = luaClass.paramters[methodIndex];
            MethodInfo      methodInfo = luaClass.methods[methodIndex];

            object[] p = LuaInstance.methodParamCached[luaClass.paramters[methodIndex].Length];

            if (ps.Length > 0)
            {
                int top = lua.GetTop();
                for (int i = 0; i < ps.Length; i++)
                {
                    if (top < indexInStack + i)
                    {
                        if (ps[i].IsOptional)
                        {
                            p[i] = ps[i].DefaultValue;
                        }
                        else
                        {
                            p[i] = ps[i].ParameterType.IsValueType ? Activator.CreateInstance(ps[i].ParameterType) : null;
                        }
                    }
                    else
                    {
                        p[i] = lua.ToSystemObject(indexInStack + i, ps[i].ParameterType);
                    }
                }
            }
            try
            {
                object result = methodInfo.Invoke(obj, p);
                if (methodInfo.ReturnType == typeof(void))
                {
                    return(0);
                }
                lua.PushSystemObject(result, methodInfo.ReturnType);
            }catch (Exception e)
            {
                UnityEngine.Debug.LogError(e.Message);
            }

            return(1);
        }
Exemplo n.º 4
0
        static public int SetField(IntPtr l)
        {
            LuaState lua = LuaInstance.Instance.lua;

            int id         = lua.ToInteger(lua.UpvalueIndex(1));
            int classIndex = 0;
            int fieldIndex = 0;

            SpritInt(id, ref fieldIndex, ref classIndex);

            LuaClass  luaClass  = LuaInstance.Instance.RegisterData.GetDynamicRegisterData(classIndex);
            FieldInfo fieldInfo = luaClass.fields[fieldIndex];

            int top = lua.GetTop();

            if (fieldInfo.IsStatic)
            {
                if (top > 0)
                {
                    object paramValue = lua.ToSystemObject(1, fieldInfo.FieldType);
                    fieldInfo.SetValue(null, paramValue);
                    return(0);
                }
                else
                {
                    object returnValue = fieldInfo.GetValue(null);
                    lua.PushSystemObject(returnValue, fieldInfo.FieldType);
                    return(1);
                }
            }
            else
            {
                object obj = lua.ToUserDataObject(1);
                if (top > 1)
                {
                    object paramValue = lua.ToSystemObject(2, fieldInfo.FieldType);
                    fieldInfo.SetValue(obj, paramValue);
                    return(0);
                }
                else
                {
                    object returnValue = fieldInfo.GetValue(obj);
                    lua.PushSystemObject(returnValue, fieldInfo.FieldType);
                    return(1);
                }
            }
        }
Exemplo n.º 5
0
        public int GetRegisterRef(Type type, bool isAutoRegister = true)
        {
            LuaStaticRegisterData staticRegData = null;

            if (!IsIngoreStaticRegister(type) && staticRegisterDic.TryGetValue(type, out staticRegData))
            {
                return(staticRegData.luaRef);
            }
            LuaClass dynamicRegData = null;

            if (dynamicRegisterDic.TryGetValue(type, out dynamicRegData))
            {
                return(dynamicRegData.typeRef);
            }

            if (isAutoRegister)
            {
                return(RegisterToLua(type));
            }

            return(LuaAPI.LUA_REFNIL);
        }
Exemplo n.º 6
0
        public int RegisterToLua(Type type)
        {
            LuaStaticRegisterData staticRegData = null;

            if (!IsIngoreStaticRegister(type) && staticRegisterDic.TryGetValue(type, out staticRegData))
            {
                if (staticRegData.luaRef == LuaAPI.LUA_REFNIL)
                {
                    staticRegData.luaRef = staticRegData.registerFun(luaState);

                    return(staticRegData.luaRef);
                }
                else
                {
                    return(staticRegData.luaRef);
                }
            }
            else
            {
                LuaClass dynamicRegData = null;
                if (!dynamicRegisterDic.TryGetValue(type, out dynamicRegData))
                {
                    dynamicRegData = new LuaClass(luaState, type, registerIndex++);
                    dynamicRegisterDic.Add(type, dynamicRegData);
                    dynamicRegisterList.Add(dynamicRegData);

                    dynamicRegData.RegisterClass();

                    return(dynamicRegData.typeRef);
                }
                else
                {
                    return(dynamicRegData.typeRef);
                }
            }
        }