private bool TryGetObject(ScriptState luaState, object o, out int index) { bool found = _mappings.TryGetValue(o, out index); if (found) { LuaCore.GetMetatable(luaState, DotNetObjectsMetatable.GlobalName); LuaCore.RawGetIndex(luaState, -1, index); var type = LuaCore.GetType(luaState, -1); if (type != ScriptTypes.Nil) { LuaCore.RemoveValue(luaState, -2); return(true); } LuaCore.RemoveValue(luaState, -1); LuaCore.RemoveValue(luaState, -1); Remove(luaState, index); } return(false); }
public int PushObjectEx(ScriptState luaState, object o, string metatableName) { int index; if (TryGetObject(luaState, o, out index)) { return(index); } var uindex = AddObject(o); LuaCore.GetMetatable(luaState, metatableName); LuaCore.GetMetatable(luaState, DotNetObjectsMetatable.GlobalName); LuaCore.NewUserData(luaState, uindex); LuaCore.PushValue(luaState, -3); LuaCore.RemoveValue(luaState, -4); LuaCore.SetMetatable(luaState, -2); LuaCore.PushValue(luaState, -1); LuaCore.RawSetIndex(luaState, -3, uindex); LuaCore.RemoveValue(luaState, -2); return(uindex); }