Ejemplo n.º 1
0
        public LuaState()
        {
            L = LuaLib.luaL_newstate();
            if (L == IntPtr.Zero)
            {
                UnityEngine.Debug.LogError("Failed to create Lua state!");
                return;
            }

            _luaStates[L] = this;

            LuaLib.luaL_openlibs(L);

            LuaCSFunction panicCallback = new LuaCSFunction(_PanicCallback);

            LuaLib.lua_atpanic(L, panicCallback);

            Set("print", (LuaCSFunction)_Print);

            LuaLib.lua_newtable(L);                                     // |t
            LuaLib.lua_pushcsfunction(L, _ImportType);                  // |t|csf
            LuaLib.lua_setfield(L, -2, "ImportType");                   // |t
            LuaLib.lua_setglobal(L, "wutLua");                          // |		// _G["wutLua"] = t

            Bindings = new LuaBindings(this);
            Bindings.Initialize();
        }
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 PushObject(object o)
        {
            if (o == null)
            {
                LuaLib.lua_pushnil(L);
                return;
            }

            Type type = o.GetType();

            if (type.IsPrimitive)
            {
                switch (Type.GetTypeCode(type))
                {
                case TypeCode.Boolean:
                {
                    PushObject((bool)o);
                    break;
                }

                default:
                {
                    PushObject(Convert.ToDouble(o));
                    break;
                }
                }
            }
            else if (type.IsValueType)
            {
                if (type == typeof(UnityEngine.Color))
                {
                    PushObject((UnityEngine.Color)o);
                }
                else if (type == typeof(UnityEngine.Quaternion))
                {
                    PushObject((UnityEngine.Quaternion)o);
                }
                else if (type == typeof(UnityEngine.Vector2))
                {
                    PushObject((UnityEngine.Vector2)o);
                }
                else if (type == typeof(UnityEngine.Vector3))
                {
                    PushObject((UnityEngine.Vector3)o);
                }
                else if (type == typeof(UnityEngine.Vector4))
                {
                    PushObject((UnityEngine.Vector4)o);
                }
                else
                {
                    // TODO
                }
            }
            else
            {
                if (type == typeof(string))
                {
                    LuaLib.lua_pushstring(L, (string)o);
                }
                else if (type == typeof(LuaCSFunction))
                {
                    LuaLib.lua_pushcsfunction(L, (LuaCSFunction)o);
                }
                else if (type.IsSubclassOf(typeof(LuaObjectBase)))
                {
                    ((LuaObjectBase)o).Push();
                }
                else
                {
                    PushCSObject(o);
                }
            }
        }