public object Get(string path) { int oldTop = LuaLib.lua_gettop(L); LuaLib.lua_pushvalue(L, LuaIndices.LUA_GLOBALSINDEX); // |_G int startPos = 0; int pos; do { pos = path.IndexOf('.', startPos); if (pos < 0) { pos = path.Length; } string key = path.Substring(startPos, pos - startPos); LuaLib.lua_pushstring(L, key); // |_G|v1|...|vn-1|kn LuaLib.lua_rawget(L, -2); // |_G|v1|...|vn-1|vn startPos = pos + 1; } while(startPos < path.Length && !LuaLib.lua_isnil(L, -1)); object o = ToObject(-1); LuaLib.lua_settop(L, oldTop); // | return(o); }
public LuaTable(LuaState luaState, int index) : base(luaState) { if (!LuaLib.lua_isnil(luaState.L, index)) { LuaLib.lua_pushvalue(luaState.L, index); // |...|t|...|t _RefId = LuaLib.luaL_ref(luaState.L, LuaIndices.LUA_REGISTRYINDEX); // |...|t|...| // Registry[reference] = t } }
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); }
public void Set(string path, object value) { int oldTop = LuaLib.lua_gettop(L); LuaLib.lua_pushvalue(L, LuaIndices.LUA_GLOBALSINDEX); // |_G int startPos = 0; int pos = path.IndexOf('.'); while (pos >= 0) { string key = path.Substring(startPos, pos - startPos); LuaLib.lua_pushstring(L, key); // |_G|k1 or |_G|v1|k2 or |_G|v1|v2|k3 or ... LuaLib.lua_rawget(L, -2); // |_G|v1 or |_G|v1|v2 or |_G|v1|v2|v3 or ... if (LuaLib.lua_isnil(L, -1)) { startPos = path.Length; break; } startPos = pos + 1; pos = path.IndexOf('.', startPos); } if (startPos < path.Length) { string key = path.Substring(startPos, path.Length - startPos); PushObject(value); // |_G|v1|v2|...|vn-1|o LuaLib.lua_setfield(L, -2, key); // |_G|v1|v2|...|vn-1| // vn-1.kn = o } LuaLib.lua_settop(L, oldTop); // | }
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); } } }