Exemple #1
0
 static Type getType(RealStatePtr L, ObjectTranslator translator, int idx)
 {
     if (LuaAPI.lua_type(L, idx) == LuaTypes.LUA_TTABLE)
     {
         LuaTable tbl;
         translator.Get(L, idx, out tbl);
         return(tbl.Get <Type>("UnderlyingSystemType"));
     }
     else if (LuaAPI.lua_type(L, idx) == LuaTypes.LUA_TSTRING)
     {
         string className = LuaAPI.lua_tostring(L, idx);
         return(translator.FindType(className));
     }
     else
     {
         return(null);
     }
 }
        public static int ImportGenericType(RealStatePtr L)
        {
            try
            {
                int top = LuaAPI.lua_gettop(L);
                if (top < 2)
                {
                    return(LuaAPI.luaL_error(L, "import generic type need at lease 2 arguments"));
                }
                ObjectTranslator translator = ObjectTranslatorPool.Instance.Find(L);
                string           className  = LuaAPI.lua_tostring(L, 1);
                if (className.EndsWith("<>"))
                {
                    className = className.Substring(0, className.Length - 2);
                }
                Type genericDef = translator.FindType(className + "`" + (top - 1));
                if (genericDef == null || !genericDef.IsGenericTypeDefinition())
                {
                    LuaAPI.lua_pushnil(L);
                }
                else
                {
                    Type[] typeArguments = new Type[top - 1];
                    for (int i = 2; i <= top; i++)
                    {
                        typeArguments[i - 2] = getType(L, translator, i);
                        if (typeArguments[i - 2] == null)
                        {
                            return(LuaAPI.luaL_error(L, "param need a type"));
                        }
                    }
                    Type genericInc = genericDef.MakeGenericType(typeArguments);
                    translator.GetTypeId(L, genericInc);
                    translator.PushAny(L, genericInc);
                }

                return(1);
            }
            catch (System.Exception e)
            {
                return(LuaAPI.luaL_error(L, "c# exception in xlua.import_type:" + e));
            }
        }
Exemple #3
0
        public static int ImportType(RealStatePtr L)
        {
            ObjectTranslator translator = ObjectTranslatorPool.Instance.Find(L);
            string           className  = LuaAPI.lua_tostring(L, 1);
            Type             type       = translator.FindType(className);

            if (type != null)
            {
                if (translator.TryDelayWrapLoader(L, type))
                {
                    LuaAPI.lua_pushboolean(L, true);
                }
                else
                {
                    return(LuaAPI.luaL_error(L, "can not load type " + type));
                }
            }
            else
            {
                LuaAPI.lua_pushnil(L);
            }
            return(1);
        }
        public static int Ctype(RealStatePtr luaState)
        {
            ObjectTranslator translator = ObjectTranslatorPool.Instance.Find(luaState);
            Type             t          = translator.GetTypeOf(luaState, 1);

            if (t == null)
            {
                string typeName = XLua.LuaDLL.Lua.lua_tostring(luaState, 1);
                if (!string.IsNullOrEmpty(typeName))
                {
                    t = translator.FindType(typeName);
                }
            }

            if (t == null)
            {
                return(XLua.LuaDLL.Lua.luaL_error(luaState, "not a CLR class"));
            }

            translator.Push(luaState, t);

            return(1);
        }
        public static int XLuaAccess(RealStatePtr L)
        {
            try
            {
                ObjectTranslator translator = ObjectTranslatorPool.Instance.Find(L);
                Type             type       = null;
                object           obj        = null;
                if (LuaAPI.lua_type(L, 1) == LuaTypes.LUA_TTABLE)
                {
                    LuaTable tbl;
                    translator.Get(L, 1, out tbl);
                    type = tbl.Get <Type>("UnderlyingSystemType");
                }
                else if (LuaAPI.lua_type(L, 1) == LuaTypes.LUA_TSTRING)
                {
                    string className = LuaAPI.lua_tostring(L, 1);
                    type = translator.FindType(className);
                }
                else if (LuaAPI.lua_type(L, 1) == LuaTypes.LUA_TUSERDATA)
                {
                    obj = translator.SafeGetCSObj(L, 1);
                    if (obj == null)
                    {
                        return(LuaAPI.luaL_error(L, "xlua.access, #1 parameter must a type/c# object/string"));
                    }
                    type = obj.GetType();
                }

                if (type == null)
                {
                    return(LuaAPI.luaL_error(L, "xlua.access, can not find c# type"));
                }

                string fieldName = LuaAPI.lua_tostring(L, 2);

                BindingFlags bindingFlags = BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static;

                if (LuaAPI.lua_gettop(L) > 2) // set
                {
                    var field = type.GetField(fieldName, bindingFlags);
                    if (field != null)
                    {
                        field.SetValue(obj, translator.GetObject(L, 3, field.FieldType));
                        return(0);
                    }
                    var prop = type.GetProperty(fieldName, bindingFlags);
                    if (prop != null)
                    {
                        prop.SetValue(obj, translator.GetObject(L, 3, prop.PropertyType), null);
                        return(0);
                    }
                }
                else
                {
                    var field = type.GetField(fieldName, bindingFlags);
                    if (field != null)
                    {
                        translator.PushAny(L, field.GetValue(obj));
                        return(1);
                    }
                    var prop = type.GetProperty(fieldName, bindingFlags);
                    if (prop != null)
                    {
                        translator.PushAny(L, prop.GetValue(obj, null));
                        return(1);
                    }
                }
                return(LuaAPI.luaL_error(L, "xlua.access, no field " + fieldName));
            }
            catch (Exception e)
            {
                return(LuaAPI.luaL_error(L, "c# exception in xlua.access: " + e));
            }
        }