Ejemplo n.º 1
0
        static int _GetBaseMetatable(IntPtr L)
        {
            LuaState luaState = LuaState.Get(L);

            int refId;

            LuaLib.lua_pushstring(L, "__refId");                                // |mt|s
            LuaLib.lua_rawget(L, 1);                                            // |mt|v	// v = mt.s
            refId = (int)LuaLib.lua_tonumber(L, -1);
            LuaLib.lua_pop(L, 1);                                               // |mt

            LuaBindMetatable self     = luaState.Bindings.GetMetatableByRefId(refId);
            Type             baseType = self._type.BaseType;

            if (baseType == null)
            {
                // No base type
                LuaLib.lua_pushstring(L, "__base");                             // |mt|s
                LuaLib.lua_pushboolean(L, false);                               // |mt|s|b
                LuaLib.lua_rawset(L, -3);                                       // |mt		// mt.__base = false
            }
            else
            {
                LuaBindMetatable baseMetatable = luaState.Bindings.GetMetatable(baseType, self._metatableType);

                LuaLib.lua_pushstring(L, "__base");                             // |mt|s
                baseMetatable.Push();                                           // |mt|s|basemt
                LuaLib.lua_rawset(L, -3);                                       // |mt		// mt.__base = basemt
            }

            return(0);
        }
Ejemplo n.º 2
0
        public LuaBindMetatable(LuaState luaState, Type type, LuaBindMetatableType metatableType) : base(luaState)
        {
            _luaState      = luaState;
            _type          = type;
            _metatableType = metatableType;

            IntPtr L = luaState.L;

            if (luaState.Bindings.MetatableIndexMetamethod == null)
            {
                LuaLib.luaL_dostring(L, _INDEX_META_METHOD_CODE);                                                               // |f
                luaState.Bindings.MetatableIndexMetamethod = new LuaFunction(luaState, -1);
                LuaLib.lua_pop(L, 1);                                                                                           // |
                LuaLib.luaL_dostring(L, _NEWINDEX_META_METHOD_CODE);                                                            // |f
                luaState.Bindings.MetatableNewIndexMetamethod = new LuaFunction(luaState, -1);
                LuaLib.lua_pop(L, 1);                                                                                           // |
            }
            if (_gcMetamethod == null)
            {
                _gcMetamethod = new LuaCSFunction(_GC);
                _getBaseMetatableMetamethod = new LuaCSFunction(_GetBaseMetatable);
                _getMemberMetamethod        = new LuaCSFunction(_GetMember);
            }

            Push();                                                                                     // |mt

            LuaLib.lua_pushstring(L, "__refId");                                                        // |mt|s
            LuaLib.lua_pushinteger(L, _RefId);                                                          // |mt|s|v
            LuaLib.lua_rawset(L, -3);                                                                   // |mt		// mt.__refId = _RefId

            LuaLib.lua_pushstring(L, "__index");                                                        // |mt|s
            luaState.Bindings.MetatableIndexMetamethod.Push();                                          // |mt|s|v
            LuaLib.lua_rawset(L, -3);                                                                   // |mt		// mt.__index = f

            if (!type.IsEnum)
            {
                LuaLib.lua_pushstring(L, "__newindex");                                         // |mt|s
                luaState.Bindings.MetatableNewIndexMetamethod.Push();                           // |mt|s|v
                LuaLib.lua_rawset(L, -3);                                                       // |mt		// mt.__newindex = f
            }

            LuaLib.lua_pushstring(L, "__gc");                                           // |mt|s
            LuaLib.lua_pushcsfunction(L, _gcMetamethod);                                // |mt|s|csf
            LuaLib.lua_rawset(L, -3);                                                   // |mt		// mt.__gc = csf

            LuaLib.lua_pushstring(L, "__getbase");                                      // |mt|s
            LuaLib.lua_pushcsfunction(L, _getBaseMetatableMetamethod);                  // |mt|s|csf
            LuaLib.lua_rawset(L, -3);                                                   // |mt		// mt.__getbase = csf

            LuaLib.lua_pushstring(L, "__getmember");                                    // |mt|s
            LuaLib.lua_pushcsfunction(L, _getMemberMetamethod);                         // |mt|s|csf
            LuaLib.lua_rawset(L, -3);                                                   // |mt		// mt.__getmember = csf

            LuaLib.lua_pop(L, 1);                                                       // |

            luaState.Bindings.RegisterMetatableRefId(_RefId, this);
        }
Ejemplo n.º 3
0
        public void RawSet(string key, object value)
        {
            IntPtr L = _LuaState.L;

            Push();                                             // |t
            LuaLib.lua_pushstring(L, key);                      // |t|k
            _LuaState.PushObject(value);                        // |t|k|v
            LuaLib.lua_rawset(L, -3);                           // |t		// t.k = v

            LuaLib.lua_pop(L, 1);                               // |
        }
Ejemplo n.º 4
0
        public bool CheckType(int index, Type type)
        {
            if (type == typeof(object))
            {
                return(true);
            }

            LuaTypes luaType = LuaLib.lua_type(L, index);

            switch (luaType)
            {
            case LuaTypes.LUA_TBOOLEAN:
                return(type == typeof(bool));

            case LuaTypes.LUA_TNUMBER:
                return(type.IsPrimitive);

            case LuaTypes.LUA_TSTRING:
                return(type == typeof(string) || type == typeof(char[]) || type == typeof(byte[]));

            case LuaTypes.LUA_TTABLE:
            {
                if (type == typeof(Type))
                {
                    LuaLib.lua_pushstring(L, "__typeName");                                     // |k
                    LuaLib.lua_rawget(L, index);                                                // |v
                    bool ret = !LuaLib.lua_isnil(L, -1);
                    LuaLib.lua_pop(L, 1);                                                       // |

                    return(ret);
                }

                return(false);
            }

            case LuaTypes.LUA_TFUNCTION:
                return(false);

            case LuaTypes.LUA_TUSERDATA:
            {
                int    objectReference = LuaLib.wutlua_rawuserdata(L, index);
                object o;
                if (!_objects.TryGetValue(objectReference, out o))
                {
                    return(false);
                }
                return(type.IsAssignableFrom(o.GetType()));
            }
            }

            return(false);
        }
Ejemplo n.º 5
0
        public UnityEngine.Vector2 ToVector2(int index)
        {
            UnityEngine.Vector2 vector = new UnityEngine.Vector2();

            if (LuaLib.lua_type(L, index) == LuaTypes.LUA_TTABLE)
            {
                LuaLib.lua_rawgeti(L, index, 1);                                        // |x
                vector.x = (float)LuaLib.lua_tonumber(L, -1);                           // |x
                LuaLib.lua_rawgeti(L, index, 2);                                        // |x|y
                vector.y = (float)LuaLib.lua_tonumber(L, -1);                           // |x|y
                LuaLib.lua_pop(L, 2);                                                   // |
            }

            return(vector);
        }
Ejemplo n.º 6
0
        public UnityEngine.Vector4 ToVector4(int index)
        {
            UnityEngine.Vector4 vector = new UnityEngine.Vector4();

            if (LuaLib.lua_type(L, index) == LuaTypes.LUA_TTABLE)
            {
                LuaLib.lua_rawgeti(L, index, 1);                                        // |x
                vector.x = (float)LuaLib.lua_tonumber(L, -1);                           // |x
                LuaLib.lua_rawgeti(L, index, 2);                                        // |x|y
                vector.y = (float)LuaLib.lua_tonumber(L, -1);                           // |x|y
                LuaLib.lua_rawgeti(L, index, 3);                                        // |x|y|z
                vector.z = (float)LuaLib.lua_tonumber(L, -1);                           // |x|y|z
                LuaLib.lua_rawgeti(L, index, 4);                                        // |x|y|z|w
                vector.w = (float)LuaLib.lua_tonumber(L, -1);                           // |x|y|z|w
                LuaLib.lua_pop(L, 4);                                                   // |
            }

            return(vector);
        }
Ejemplo n.º 7
0
        public UnityEngine.Quaternion ToQuaternion(int index)
        {
            UnityEngine.Quaternion quaternion = new UnityEngine.Quaternion();

            if (LuaLib.lua_type(L, index) == LuaTypes.LUA_TTABLE)
            {
                LuaLib.lua_rawgeti(L, index, 1);                                        // |x
                quaternion.x = (float)LuaLib.lua_tonumber(L, -1);                       // |x
                LuaLib.lua_rawgeti(L, index, 2);                                        // |x|y
                quaternion.y = (float)LuaLib.lua_tonumber(L, -1);                       // |x|y
                LuaLib.lua_rawgeti(L, index, 3);                                        // |x|y|z
                quaternion.z = (float)LuaLib.lua_tonumber(L, -1);                       // |x|y|z
                LuaLib.lua_rawgeti(L, index, 4);                                        // |x|y|z|w
                quaternion.w = (float)LuaLib.lua_tonumber(L, -1);                       // |x|y|z|w
                LuaLib.lua_pop(L, 4);                                                   // |
            }

            return(quaternion);
        }
Ejemplo n.º 8
0
        public UnityEngine.Color ToColor(int index)
        {
            UnityEngine.Color color = new UnityEngine.Color(0f, 1f, 1f, 1f);

            if (LuaLib.lua_type(L, index) == LuaTypes.LUA_TTABLE)
            {
                LuaLib.lua_rawgeti(L, index, 1);                                        // |r
                color.r = (float)LuaLib.lua_tonumber(L, -1);                            // |r
                LuaLib.lua_rawgeti(L, index, 2);                                        // |r|g
                color.g = (float)LuaLib.lua_tonumber(L, -1);                            // |r|g
                LuaLib.lua_rawgeti(L, index, 3);                                        // |r|g|b
                color.b = (float)LuaLib.lua_tonumber(L, -1);                            // |r|g|b
                LuaLib.lua_rawgeti(L, index, 4);                                        // |r|g|b|a
                color.a = (float)LuaLib.lua_tonumber(L, -1);                            // |r|g|b|a
                LuaLib.lua_pop(L, 4);                                                   // |
            }

            return(color);
        }
Ejemplo n.º 9
0
        public void RegisterType(Type type)
        {
            UnityEngine.Debug.Assert(!_typeTables.ContainsKey(type));

            IntPtr L = _luaState.L;

            LuaTable typeTable = new LuaTable(_luaState);

            typeTable.Push();                                                   // |t
            LuaLib.lua_pushstring(L, "__typeName");                             // |t|sk
            LuaLib.lua_pushstring(L, type.ToString());                          // |t|sk|sv
            LuaLib.lua_rawset(L, -3);                                           // |t		// t.__typeName = typeName

            LuaBindMetatable metatable = GetMetatable(type, LuaBindMetatableType.Type);

            metatable.Push();                                                           // |t|mt
            LuaLib.lua_setmetatable(L, -2);                                             // |t		// t.metatable = mt

            LuaLib.lua_pop(L, 1);                                                       // |

            _typeNames.Add(type.ToString(), type);
            _typeTables[type] = typeTable;
        }
Ejemplo n.º 10
0
        // -------------------------------------------------------------------------------------------------------------
        // Public interfaces
        // -------------------------------------------------------------------------------------------------------------

        public object[] DoBuffer(byte[] chunk, string chunkName = "chunk")
        {
            int oldTop = LuaLib.lua_gettop(L);

            if (LuaLib.luaL_loadbuffer(L, chunk, chunk.Length, chunkName) != 0)                 // |f or |err
            {
                string error = LuaLib.lua_tostring(L, -1);                                      // |err
                LuaLib.lua_settop(L, oldTop);                                                   // |

                throw new LuaException(error);
            }
            if (LuaLib.lua_pcall(L, 0, LuaLib.LUA_MULTRET, 0) != 0)                                             // | or |ret1|ret2 or |err
            {
                string error = LuaLib.lua_tostring(L, -1);                                                      // |err
                LuaLib.lua_settop(L, oldTop);                                                                   // |

                throw new LuaException(error);
            }

            int top = LuaLib.lua_gettop(L);

            if (top == oldTop)
            {
                return(null);
            }

            var objects = new List <object>();

            for (int i = oldTop + 1; i <= top; ++i)
            {
                objects.Add(ToObject(i));
            }

            LuaLib.lua_pop(L, top - oldTop);

            return(objects.ToArray());
        }
Ejemplo n.º 11
0
        public object ToObject(int index)
        {
            LuaTypes luaType = LuaLib.lua_type(L, index);

            switch (luaType)
            {
            case LuaTypes.LUA_TNONE:
            case LuaTypes.LUA_TNIL:
            {
                return(null);
            }

            case LuaTypes.LUA_TBOOLEAN:
            {
                return(LuaLib.lua_toboolean(L, index));
            }

            case LuaTypes.LUA_TNUMBER:
            {
                return(LuaLib.lua_tonumber(L, index));
            }

            case LuaTypes.LUA_TSTRING:
            {
                return(LuaLib.lua_tostring(L, index));
            }

            case LuaTypes.LUA_TTABLE:
            {
                LuaValueType valueType = LuaValueType.None;

                LuaLib.lua_pushvalue(L, index);                                                                                 // |t
                LuaLib.lua_pushstring(L, "__valueType");                                                                        // |t|k
                LuaLib.lua_rawget(L, -2);                                                                                       // |t|vt
                if (!LuaLib.lua_isnil(L, -1))
                {
                    valueType = (LuaValueType)LuaLib.lua_tonumber(L, -1);                                       // |t|vt
                }
                LuaLib.lua_pop(L, 2);                                                                           // |

                switch (valueType)
                {
                case LuaValueType.None:
                {
                    return(ToTable(index));
                }

                case LuaValueType.Color:
                {
                    return(ToColor(index));
                }

                case LuaValueType.Quaternion:
                {
                    return(ToQuaternion(index));
                }

                case LuaValueType.Vector2:
                {
                    return(ToVector2(index));
                }

                case LuaValueType.Vector3:
                {
                    return(ToVector3(index));
                }

                case LuaValueType.Vector4:
                {
                    return(ToVector4(index));
                }

                default:
                {
                    return(null);
                }
                }
            }

            case LuaTypes.LUA_TFUNCTION:
            {
                return(ToFunction(index));
            }

            case LuaTypes.LUA_TUSERDATA:
            {
                return(ToCSObject(index));
            }

            default:
            {
                return(null);
            }
            }
        }