Пример #1
0
        private void PushCLRObj(object obj, string name = "")
        {
            switch (obj)
            {
            case bool b:
                CApi.lua_pushboolean(lua_State, Convert.ToInt32(b));
                break;

            case double d:
                CApi.lua_pushnumber(lua_State, d);
                break;

            case int i:
                CApi.lua_pushinteger(lua_State, i);
                break;

            case long l:
                CApi.lua_pushinteger(lua_State, l);
                break;

            case string str:
            {
                byte[] s = Encoding.ASCII.GetBytes(str);
                CApi.lua_pushlstring(lua_State, s, (UIntPtr)str.Length);
                break;
            }

            case LuaTable tbl:
            {
                foreach (var kv in tbl)
                {
                    if (CApi.lua_type(lua_State, -1) != (int)LuaTypes.Table)
                    {
                        CApi.lua_getglobal(lua_State, name);

                        if (CApi.lua_type(lua_State, -1) != (int)LuaTypes.Table)
                        {
                            CApi.lua_settop(lua_State, -2);
                            CApi.lua_createtable(lua_State, 0, 0);
                        }
                    }
                    PushCLRObj(kv.Key);
                    PushCLRObj(kv.Value);

                    CApi.lua_settable(lua_State, -3);
                }
                break;
            }

            case null:
                CApi.lua_pushnil(lua_State);
                break;

            default:
                throw new LuaException("Unsupported type for push object!");
            }
        }
Пример #2
0
        private LuaTable ConvertToTable(int index)
        {
            var table     = new LuaTable();
            var tempIndex = index < 0 ? index - 1 : index;

            CApi.lua_pushnil(lua_State);

            while (CApi.lua_next(lua_State, tempIndex) != 0)
            {
                try
                {
                    table.Add(LuaObjToCLRObj(-2), LuaObjToCLRObj(-1));
                }
                catch (LuaInvalidArgumentException)
                { }

                CApi.lua_settop(lua_State, -2);
            }

            return(table);
        }