Ejemplo n.º 1
0
        /// <summary>
        /// Imports the given type as a global, allowing Lua to access its static members.
        /// </summary>
        /// <param name="type">The type.</param>
        /// <exception cref="ArgumentNullException"><paramref name="type"/> is <c>null</c>.</exception>
        /// <exception cref="ObjectDisposedException">The <see cref="Lua"/> environment is disposed.</exception>
        public void ImportType(Type type)
        {
            if (type == null)
            {
                throw new ArgumentNullException(nameof(type));
            }
            ThrowIfDisposed();

            ObjectBinder.PushNetType(MainState, type);
            var cleanName = type.Name.Split('`')[0];

            LuaApi.SetGlobal(MainState, cleanName);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Gets or sets the global with the given name.
        /// </summary>
        /// <param name="name">The name.</param>
        /// <returns>The value of the global.</returns>
        /// <exception cref="ArgumentException">
        /// <paramref name="value"/> is a <see cref="LuaReference"/> which is tied to a different <see cref="Lua"/> environment.
        /// </exception>
        /// <exception cref="ArgumentNullException"><paramref name="name"/> is <c>null</c>.</exception>
        /// <exception cref="ObjectDisposedException">The <see cref="Lua"/> environment is disposed.</exception>
        public object this[string name] {
            get {
                if (name == null)
                {
                    throw new ArgumentNullException(nameof(name));
                }
                ThrowIfDisposed();

                var type   = LuaApi.GetGlobal(MainState, name);
                var result = ToObject(-1, type);
                LuaApi.Pop(MainState, 1);
                return(result);
            }
            set {
                if (name == null)
                {
                    throw new ArgumentNullException(nameof(name));
                }
                ThrowIfDisposed();

                PushObject(value);
                LuaApi.SetGlobal(MainState, name);
            }
        }