示例#1
0
        internal void setObject(string[] remainingPath, object val)
        {
            for (int i = 0; i < remainingPath.Length - 1; i++)
            {
                LuaAPI.lua_pushstring(L, remainingPath[i]);
                LuaAPI.lua_gettable(L, -2);
            }
            LuaAPI.lua_pushstring(L, remainingPath[remainingPath.Length - 1]);

            //可以释放先
            //if (val == null)
            //{
            //    LuaAPI.lua_gettable(L, -2);
            //    LuaTypes type = LuaAPI.lua_type(L, -1);
            //    if (type == LuaTypes.LUA_TUSERDATA)
            //    {
            //        int udata = LuaAPI.luanet_tonetobject(L, -1);
            //        if (udata != -1)
            //        {
            //            translator.collectObject(udata);
            //        }
            //    }
            //}
            translator.push(L, val);
            LuaAPI.lua_settable(L, -3);
        }
示例#2
0
        internal object getObject(int reference, object field)
        {
            int oldTop = LuaAPI.lua_gettop(L);

            LuaAPI.ulua_rawgeti(L, LuaAPI.LUA_REGISTRYINDEX, reference);
            translator.push(L, field);
            LuaAPI.lua_gettable(L, -2);
            object returnValue = translator.getObject(L, -1);

            LuaAPI.lua_settop(L, oldTop);
            return(returnValue);
        }
示例#3
0
        internal object getObject(string[] remainingPath)
        {
            object returnValue = null;

            for (int i = 0; i < remainingPath.Length; i++)
            {
                LuaAPI.lua_pushstring(L, remainingPath[i]);
                LuaAPI.lua_gettable(L, -2);
                returnValue = translator.getObject(L, -1);
                if (returnValue == null)
                {
                    break;
                }
            }
            return(returnValue);
        }
示例#4
0
        public static int unregisterTable(IntPtr luaState)
        {
            ObjectTranslator translator = ObjectTranslator.FromState(luaState);

            try
            {
                if (LuaAPI.lua_getmetatable(luaState, 1) != 0)
                {
                    LuaAPI.lua_pushstring(luaState, "__index");
                    LuaAPI.lua_gettable(luaState, -2);
                    object obj = translator.getRawNetObject(luaState, -1);
                    if (obj == null)
                    {
                        translator.throwError(luaState, "unregister_table: arg is not valid table");
                    }
                    FieldInfo luaTableField = obj.GetType().GetField("__luaInterface_luaTable");
                    if (luaTableField == null)
                    {
                        translator.throwError(luaState, "unregister_table: arg is not valid table");
                    }
                    luaTableField.SetValue(obj, null);
                    LuaAPI.lua_pushnil(luaState);
                    LuaAPI.lua_setmetatable(luaState, 1);
                    LuaAPI.lua_pushstring(luaState, "base");
                    LuaAPI.lua_pushnil(luaState);
                    LuaAPI.lua_settable(luaState, 1);
                }
                else
                {
                    translator.throwError(luaState, "unregister_table: arg is not valid table");
                }
            }
            catch (Exception e)
            {
                translator.throwError(luaState, e.Message);
            }
            return(0);
        }
示例#5
0
        internal ExtractValue checkType(IntPtr luaState, int stackPos, Type paramType)
        {
            LuaTypes luatype = LuaAPI.lua_type(luaState, stackPos);

            if (paramType.IsByRef)
            {
                paramType = paramType.GetElementType();
            }

            Type underlyingType = Nullable.GetUnderlyingType(paramType);

            if (underlyingType != null)
            {
                paramType = underlyingType;     // Silently convert nullable types to their non null requics
            }

            long runtimeHandleValue = paramType.TypeHandle.Value.ToInt64();

            if (paramType.Equals(typeof(object)))
            {
                return(extractValues[runtimeHandleValue]);
            }

            //CP: Added support for generic parameters
            if (paramType.IsGenericParameter)
            {
                if (luatype == LuaTypes.LUA_TBOOLEAN)
                {
                    return(extractValues[typeof(bool).TypeHandle.Value.ToInt64()]);
                }
                else if (luatype == LuaTypes.LUA_TSTRING)
                {
                    return(extractValues[typeof(string).TypeHandle.Value.ToInt64()]);
                }
                else if (luatype == LuaTypes.LUA_TTABLE)
                {
                    return(extractValues[typeof(LuaTable).TypeHandle.Value.ToInt64()]);
                }
                else if (luatype == LuaTypes.LUA_TUSERDATA)
                {
                    return(extractValues[typeof(object).TypeHandle.Value.ToInt64()]);
                }
                else if (luatype == LuaTypes.LUA_TFUNCTION)
                {
                    return(extractValues[typeof(LuaFunction).TypeHandle.Value.ToInt64()]);
                }
                else if (luatype == LuaTypes.LUA_TNUMBER)
                {
                    return(extractValues[typeof(double).TypeHandle.Value.ToInt64()]);
                }
            }

            if (paramType.IsValueType && luatype == LuaTypes.LUA_TTABLE)
            {
                int          oldTop = LuaAPI.lua_gettop(luaState);
                ExtractValue ret    = null;
                LuaAPI.lua_pushvalue(luaState, stackPos);
                LuaAPI.lua_pushstring(luaState, "class");
                LuaAPI.lua_gettable(luaState, -2);

                if (!LuaAPI.lua_isnil(luaState, -1))
                {
                    string cls = LuaAPI.lua_tostring(luaState, -1);

                    if (cls == "Vector3" && paramType == typeof(Vector3))
                    {
                        ret = extractValues[typeof(Vector3).TypeHandle.Value.ToInt64()];
                    }
                    else if (cls == "Vector2" && paramType == typeof(Vector2))
                    {
                        ret = extractValues[typeof(Vector2).TypeHandle.Value.ToInt64()];
                    }
                    else if (cls == "Quaternion" && paramType == typeof(Quaternion))
                    {
                        ret = extractValues[typeof(Quaternion).TypeHandle.Value.ToInt64()];
                    }
                    else if (cls == "Color" && paramType == typeof(Color))
                    {
                        ret = extractValues[typeof(Color).TypeHandle.Value.ToInt64()];
                    }
                    else if (cls == "Vector4" && paramType == typeof(Vector4))
                    {
                        ret = extractValues[typeof(Vector4).TypeHandle.Value.ToInt64()];
                    }
                    else if (cls == "Ray" && paramType == typeof(Ray))
                    {
                        ret = extractValues[typeof(Ray).TypeHandle.Value.ToInt64()];
                    }
                    else
                    {
                        ret = null;
                    }
                }

                LuaAPI.lua_settop(luaState, oldTop);

                if (ret != null)
                {
                    return(ret);
                }
            }

            if (LuaAPI.lua_isnumber(luaState, stackPos))
            {
                return(extractValues[runtimeHandleValue]);
            }

            if (paramType == typeof(bool))
            {
                if (LuaAPI.lua_isboolean(luaState, stackPos))
                {
                    return(extractValues[runtimeHandleValue]);
                }
            }
            else if (paramType == typeof(string))
            {
                if (LuaAPI.lua_isstring(luaState, stackPos))
                {
                    return(extractValues[runtimeHandleValue]);
                }
                else if (luatype == LuaTypes.LUA_TNIL)
                {
                    return(extractNetObject); // kevinh - silently convert nil to a null string pointer
                }
            }
            else if (paramType == typeof(LuaTable))
            {
                if (luatype == LuaTypes.LUA_TTABLE)
                {
                    return(extractValues[runtimeHandleValue]);
                }
            }
            else if (paramType == typeof(LuaFunction))
            {
                if (luatype == LuaTypes.LUA_TFUNCTION)
                {
                    return(extractValues[runtimeHandleValue]);
                }
            }
            else if (typeof(Delegate).IsAssignableFrom(paramType) && luatype == LuaTypes.LUA_TFUNCTION)
            {
                translator.throwError(luaState, "Delegates not implemnented");
            }
            else if (paramType.IsInterface && luatype == LuaTypes.LUA_TTABLE)
            {
                translator.throwError(luaState, "Interfaces not implemnented");
            }
            else if ((paramType.IsInterface || paramType.IsClass) && luatype == LuaTypes.LUA_TNIL)
            {
                // kevinh - allow nil to be silently converted to null - extractNetObject will return null when the item ain't found
                return(extractNetObject);
            }
            else if (LuaAPI.lua_type(luaState, stackPos) == LuaTypes.LUA_TTABLE)
            {
                if (LuaTypes.LUA_TNIL != LuaAPI.luaL_getmetafield(luaState, stackPos, "__index"))
                {
                    object obj = translator.getNetObject(luaState, -1);
                    LuaAPI.lua_settop(luaState, -2);
                    if (obj != null && paramType.IsAssignableFrom(obj.GetType()))
                    {
                        return(extractNetObject);
                    }
                }
            }
            else
            {
                //object obj = translator.getNetObject(luaState, stackPos);  //topameng 修改这里使支持注册到c#的lua类
                object obj = translator.getRawNetObject(luaState, stackPos);
                if (obj != null && paramType.IsAssignableFrom(obj.GetType()))
                {
                    return(extractNetObject);
                }
            }

            return(null);
        }