コード例 #1
0
        private static int indexInt(IntPtr l, object self, int index)
        {
            Type type = LuaVarObject.getType(self);

            if (self is IList)
            {
                LuaObject.pushValue(l, true);
                LuaObject.pushVar(l, (self as IList)[index]);
                return(2);
            }
            if (self is IDictionary)
            {
                IDictionary dictionary = (IDictionary)self;
                object      obj        = index;
                if (type.IsGenericType)
                {
                    Type type2 = type.GetGenericArguments()[0];
                    if (type2.IsEnum)
                    {
                        LuaObject.pushValue(l, true);
                        LuaObject.pushVar(l, dictionary[Enum.Parse(type2, obj.ToString())]);
                        return(2);
                    }
                    obj = LuaObject.changeType(obj, type2);
                }
                LuaObject.pushValue(l, true);
                LuaObject.pushVar(l, dictionary[obj]);
                return(2);
            }
            return(0);
        }
コード例 #2
0
        public static int luaNewIndex(IntPtr l)
        {
            int result;

            try
            {
                ObjectCache objectCache = ObjectCache.get(l);
                object      self        = objectCache.get(l, 1);
                LuaTypes    luaTypes    = LuaDLL.pua_type(l, 2);
                LuaTypes    luaTypes2   = luaTypes;
                if (luaTypes2 != LuaTypes.LUA_TNUMBER)
                {
                    if (luaTypes2 != LuaTypes.LUA_TSTRING)
                    {
                        result = LuaVarObject.newindexObject(l, self, LuaObject.checkVar(l, 2), LuaObject.checkVar(l, 3));
                    }
                    else
                    {
                        result = LuaVarObject.newindexString(l, self, LuaDLL.pua_tostring(l, 2));
                    }
                }
                else
                {
                    result = LuaVarObject.newindexInt(l, self, LuaDLL.pua_tointeger(l, 2));
                }
            }
            catch (Exception e)
            {
                result = LuaObject.error(l, e);
            }
            return(result);
        }
コード例 #3
0
        private static int newindexInt(IntPtr l, object self, int index)
        {
            Type type = LuaVarObject.getType(self);

            if (self is IList)
            {
                if (type.IsGenericType)
                {
                    Type t = type.GetGenericArguments()[0];
                    (self as IList)[index] = LuaObject.changeType(LuaObject.checkVar(l, 3), t);
                }
                else
                {
                    (self as IList)[index] = LuaObject.checkVar(l, 3);
                }
            }
            else if (self is IDictionary)
            {
                Type   t2  = type.GetGenericArguments()[0];
                object obj = index;
                obj = LuaObject.changeType(obj, t2);
                if (type.IsGenericType)
                {
                    Type t3 = type.GetGenericArguments()[1];
                    (self as IDictionary)[obj] = LuaObject.changeType(LuaObject.checkVar(l, 3), t3);
                }
                else
                {
                    (self as IDictionary)[obj] = LuaObject.checkVar(l, 3);
                }
            }
            LuaObject.pushValue(l, true);
            return(1);
        }
コード例 #4
0
        private static int indexString(IntPtr l, object self, string key)
        {
            Type type = LuaVarObject.getType(self);

            if (self is IDictionary)
            {
                if (!type.IsGenericType || type.GetGenericArguments()[0] == typeof(string))
                {
                    object obj = (self as IDictionary)[key];
                    if (obj != null)
                    {
                        LuaObject.pushValue(l, true);
                        LuaObject.pushVar(l, obj);
                        return(2);
                    }
                }
            }
            IList <MemberInfo> cacheMembers = LuaVarObject.GetCacheMembers(type, key);

            if (cacheMembers == null || cacheMembers.Count == 0)
            {
                return(LuaObject.error(l, "Can't find " + key));
            }
            LuaObject.pushValue(l, true);
            MemberInfo  memberInfo = cacheMembers[0];
            MemberTypes memberType = memberInfo.MemberType;

            switch (memberType)
            {
            case MemberTypes.Event:
                return(2);

            case MemberTypes.Constructor | MemberTypes.Event:
            {
IL_B3:
                if (memberType == MemberTypes.Method)
                {
                    LuaCSFunction o = new LuaCSFunction(new LuaVarObject.MethodWrapper(self, cacheMembers).invoke);
                    LuaObject.pushObject(l, o);
                    return(2);
                }
                if (memberType != MemberTypes.Property)
                {
                    return(1);
                }
                PropertyInfo propertyInfo = (PropertyInfo)memberInfo;
                MethodInfo   getMethod    = propertyInfo.GetGetMethod(true);
                LuaObject.pushVar(l, getMethod.Invoke(self, null));
                return(2);
            }

            case MemberTypes.Field:
            {
                FieldInfo fieldInfo = (FieldInfo)memberInfo;
                LuaObject.pushVar(l, fieldInfo.GetValue(self));
                return(2);
            }
            }
            goto IL_B3;
        }
コード例 #5
0
        private static int newindexString(IntPtr l, object self, string key)
        {
            if (self is IDictionary)
            {
                Type type = LuaVarObject.getType(self);
                Type t    = type.GetGenericArguments()[1];
                (self as IDictionary)[key] = LuaObject.checkVar(l, 3, t);
                return(LuaObject.ok(l));
            }
            Type type2 = LuaVarObject.getType(self);
            IList <MemberInfo> cacheMembers = LuaVarObject.GetCacheMembers(type2, key);

            if (cacheMembers == null || cacheMembers.Count == 0)
            {
                return(LuaObject.error(l, "Can't find " + key));
            }
            MemberInfo  memberInfo = cacheMembers[0];
            MemberTypes memberType = memberInfo.MemberType;

            switch (memberType)
            {
            case MemberTypes.Event:
                return(LuaObject.error(l, "Event can't set"));

            case MemberTypes.Constructor | MemberTypes.Event:
            {
IL_8F:
                if (memberType == MemberTypes.Method)
                {
                    return(LuaObject.error(l, "Method can't set"));
                }
                if (memberType != MemberTypes.Property)
                {
                    goto IL_121;
                }
                PropertyInfo propertyInfo = (PropertyInfo)memberInfo;
                MethodInfo   setMethod    = propertyInfo.GetSetMethod(true);
                object       obj          = LuaObject.checkVar(l, 3, propertyInfo.PropertyType);
                setMethod.Invoke(self, new object[]
                    {
                        obj
                    });
                goto IL_121;
            }

            case MemberTypes.Field:
            {
                FieldInfo fieldInfo = (FieldInfo)memberInfo;
                object    value     = LuaObject.checkVar(l, 3, fieldInfo.FieldType);
                fieldInfo.SetValue(self, value);
                goto IL_121;
            }
            }
            goto IL_8F;
IL_121:
            return(LuaObject.ok(l));
        }
コード例 #6
0
        private static IList <MemberInfo> GetCacheMembers(Type type, string key)
        {
            Dictionary <string, List <MemberInfo> > dictionary;

            if (!LuaVarObject.cachedMemberInfos.TryGetValue(type, out dictionary))
            {
                dictionary = (LuaVarObject.cachedMemberInfos[type] = new Dictionary <string, List <MemberInfo> >());
                LuaVarObject.CollectTypeMembers(type, ref dictionary);
            }
            return(dictionary[key]);
        }
コード例 #7
0
 private static int newindexObject(IntPtr l, object self, object k, object v)
 {
     if (self is IDictionary)
     {
         IDictionary dictionary = self as IDictionary;
         Type        type       = LuaVarObject.getType(self);
         Type        t          = type.GetGenericArguments()[1];
         object      value      = LuaObject.changeType(v, t);
         dictionary[k] = value;
     }
     return(LuaObject.ok(l));
 }
コード例 #8
0
 private static void CollectTypeMembers(Type type, ref Dictionary <string, List <MemberInfo> > membersMap)
 {
     MemberInfo[] members = type.GetMembers(BindingFlags.DeclaredOnly | BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic);
     for (int i = 0; i < members.Length; i++)
     {
         MemberInfo        memberInfo = members[i];
         List <MemberInfo> list;
         if (!membersMap.TryGetValue(memberInfo.Name, out list))
         {
             List <MemberInfo> list2 = new List <MemberInfo>();
             membersMap[memberInfo.Name] = list2;
             list = list2;
         }
         list.Add(memberInfo);
     }
     if (type.BaseType != null)
     {
         LuaVarObject.CollectTypeMembers(type.BaseType, ref membersMap);
     }
 }
コード例 #9
0
ファイル: LuaObject.cs プロジェクト: double-haipi/Unity_Lua
        public static void init(IntPtr l)
        {
            string newindexfun = @"

local getmetatable=getmetatable
local rawget=rawget
local error=error
local type=type
local function newindex(ud,k,v)
    local t=getmetatable(ud)
    repeat
        local h=rawget(t,k)
        if h then
			if h[2] then
				h[2](ud,v)
	            return
			else
				error('property '..k..' is read only')
			end
        end
        t=rawget(t,'__parent')
    until t==nil
    error('can not find '..k)
end

return newindex
";

            string   indexfun = @"
local type=type
local error=error
local rawget=rawget
local getmetatable=getmetatable
local function index(ud,k)
    local t=getmetatable(ud)
    repeat
        local fun=rawget(t,k)
        local tp=type(fun)	
        if tp=='function' then 
            return fun 
        elseif tp=='table' then
			local f=fun[1]
			if f then
				return f(ud)
			else
				error('property '..k..' is write only')
			end
        end
        t = rawget(t,'__parent')
    until t==nil
    error('Can not find '..k)
end

return index
";
            LuaState L        = LuaState.get(l);

            newindex_func = (LuaFunction)L.doString(newindexfun);
            index_func    = (LuaFunction)L.doString(indexfun);

            // object method
            LuaDLL.pua_createtable(l, 0, 4);
            addMember(l, ToString);
            addMember(l, GetHashCode);
            addMember(l, Equals);
            addMember(l, GetType);
            LuaDLL.pua_setfield(l, LuaIndexes.LUA_REGISTRYINDEX, "__luabaseobject");

            LuaArray.init(l);
            LuaVarObject.init(l);

            LuaDLL.pua_newtable(l);
            LuaDLL.pua_setglobal(l, DelgateTable);


            setupPushVar();
        }