1
0
mirror of https://github.com/moparisthebest/minetest synced 2024-12-22 23:58:48 -05:00

Fix object reference pushing functions when called from coroutines

This commit is contained in:
ShadowNinja 2014-10-02 15:58:13 -04:00
parent 28438bba27
commit 741df993ff
10 changed files with 42 additions and 45 deletions

View File

@ -238,22 +238,18 @@ void ScriptApiBase::removeObjectReference(ServerActiveObject *cobj)
}
// Creates a new anonymous reference if cobj=NULL or id=0
void ScriptApiBase::objectrefGetOrCreate(
void ScriptApiBase::objectrefGetOrCreate(lua_State *L,
ServerActiveObject *cobj)
{
lua_State *L = getStack();
if(cobj == NULL || cobj->getId() == 0){
ObjectRef::create(L, cobj);
} else {
objectrefGet(cobj->getId());
objectrefGet(L, cobj->getId());
}
}
void ScriptApiBase::objectrefGet(u16 id)
void ScriptApiBase::objectrefGet(lua_State *L, u16 id)
{
lua_State *L = getStack();
// Get core.object_refs[i]
lua_getglobal(L, "core");
lua_getfield(L, -1, "object_refs");
@ -263,3 +259,4 @@ void ScriptApiBase::objectrefGet(u16 id)
lua_remove(L, -2); // object_refs
lua_remove(L, -2); // core
}

View File

@ -78,8 +78,8 @@ protected:
GUIEngine* getGuiEngine() { return m_guiengine; }
void setGuiEngine(GUIEngine* guiengine) { m_guiengine = guiengine; }
void objectrefGetOrCreate(ServerActiveObject *cobj);
void objectrefGet(u16 id);
void objectrefGetOrCreate(lua_State *L, ServerActiveObject *cobj);
void objectrefGet(lua_State *L, u16 id);
JMutex m_luastackmutex;
// Stack index of Lua error handler

View File

@ -56,7 +56,7 @@ bool ScriptApiEntity::luaentity_Add(u16 id, const char *name)
// Add object reference
// This should be userdata with metatable ObjectRef
objectrefGet(id);
objectrefGet(L, id);
luaL_checktype(L, -1, LUA_TUSERDATA);
if (!luaL_checkudata(L, -1, "ObjectRef"))
luaL_typerror(L, -1, "ObjectRef");
@ -236,8 +236,8 @@ void ScriptApiEntity::luaentity_Punch(u16 id,
return;
}
luaL_checktype(L, -1, LUA_TFUNCTION);
lua_pushvalue(L, object); // self
objectrefGetOrCreate(puncher); // Clicker reference
lua_pushvalue(L, object); // self
objectrefGetOrCreate(L, puncher); // Clicker reference
lua_pushnumber(L, time_from_last_punch);
push_tool_capabilities(L, *toolcap);
push_v3f(L, dir);
@ -267,7 +267,7 @@ void ScriptApiEntity::luaentity_Rightclick(u16 id,
}
luaL_checktype(L, -1, LUA_TFUNCTION);
lua_pushvalue(L, object); // self
objectrefGetOrCreate(clicker); // Clicker reference
objectrefGetOrCreate(L, clicker); // Clicker reference
// Call with 2 arguments, 0 results
if (lua_pcall(L, 2, 0, m_errorhandler))
scriptError();

View File

@ -70,7 +70,7 @@ void ScriptApiEnv::player_event(ServerActiveObject* player, std::string type)
lua_getfield(L, -1, "registered_playerevents");
// Call callbacks
objectrefGetOrCreate(player); // player
objectrefGetOrCreate(L, player); // player
lua_pushstring(L,type.c_str()); // event type
try {
script_run_callbacks(L, 2, RUN_CALLBACKS_MODE_FIRST);

View File

@ -47,7 +47,7 @@ int ScriptApiDetached::detached_inventory_AllowMove(
lua_pushstring(L, to_list.c_str()); // to_list
lua_pushinteger(L, to_index + 1); // to_index
lua_pushinteger(L, count); // count
objectrefGetOrCreate(player); // player
objectrefGetOrCreate(L, player); // player
if (lua_pcall(L, 7, 1, m_errorhandler))
scriptError();
if(!lua_isnumber(L, -1))
@ -76,7 +76,7 @@ int ScriptApiDetached::detached_inventory_AllowPut(
lua_pushstring(L, listname.c_str()); // listname
lua_pushinteger(L, index + 1); // index
LuaItemStack::create(L, stack); // stack
objectrefGetOrCreate(player); // player
objectrefGetOrCreate(L, player); // player
if (lua_pcall(L, 5, 1, m_errorhandler))
scriptError();
if (!lua_isnumber(L, -1))
@ -105,7 +105,7 @@ int ScriptApiDetached::detached_inventory_AllowTake(
lua_pushstring(L, listname.c_str()); // listname
lua_pushinteger(L, index + 1); // index
LuaItemStack::create(L, stack); // stack
objectrefGetOrCreate(player); // player
objectrefGetOrCreate(L, player); // player
if (lua_pcall(L, 5, 1, m_errorhandler))
scriptError();
if (!lua_isnumber(L, -1))
@ -138,7 +138,7 @@ void ScriptApiDetached::detached_inventory_OnMove(
lua_pushstring(L, to_list.c_str()); // to_list
lua_pushinteger(L, to_index + 1); // to_index
lua_pushinteger(L, count); // count
objectrefGetOrCreate(player); // player
objectrefGetOrCreate(L, player); // player
if (lua_pcall(L, 7, 0, m_errorhandler))
scriptError();
}
@ -163,7 +163,7 @@ void ScriptApiDetached::detached_inventory_OnPut(
lua_pushstring(L, listname.c_str()); // listname
lua_pushinteger(L, index + 1); // index
LuaItemStack::create(L, stack); // stack
objectrefGetOrCreate(player); // player
objectrefGetOrCreate(L, player); // player
if (lua_pcall(L, 5, 0, m_errorhandler))
scriptError();
}
@ -188,7 +188,7 @@ void ScriptApiDetached::detached_inventory_OnTake(
lua_pushstring(L, listname.c_str()); // listname
lua_pushinteger(L, index + 1); // index
LuaItemStack::create(L, stack); // stack
objectrefGetOrCreate(player); // player
objectrefGetOrCreate(L, player); // player
if (lua_pcall(L, 5, 0, m_errorhandler))
scriptError();
}

View File

@ -40,7 +40,7 @@ bool ScriptApiItem::item_OnDrop(ItemStack &item,
// Call function
LuaItemStack::create(L, item);
objectrefGetOrCreate(dropper);
objectrefGetOrCreate(L, dropper);
pushFloatPos(L, pos);
if (lua_pcall(L, 3, 1, m_errorhandler))
scriptError();
@ -66,7 +66,7 @@ bool ScriptApiItem::item_OnPlace(ItemStack &item,
// Call function
LuaItemStack::create(L, item);
objectrefGetOrCreate(placer);
objectrefGetOrCreate(L, placer);
pushPointedThing(pointed);
if (lua_pcall(L, 3, 1, m_errorhandler))
scriptError();
@ -92,7 +92,7 @@ bool ScriptApiItem::item_OnUse(ItemStack &item,
// Call function
LuaItemStack::create(L, item);
objectrefGetOrCreate(user);
objectrefGetOrCreate(L, user);
pushPointedThing(pointed);
if (lua_pcall(L, 3, 1, m_errorhandler))
scriptError();
@ -115,7 +115,7 @@ bool ScriptApiItem::item_OnCraft(ItemStack &item, ServerActiveObject *user,
lua_getglobal(L, "core");
lua_getfield(L, -1, "on_craft");
LuaItemStack::create(L, item);
objectrefGetOrCreate(user);
objectrefGetOrCreate(L, user);
// Push inventory list
std::vector<ItemStack> items;
@ -146,7 +146,7 @@ bool ScriptApiItem::item_CraftPredict(ItemStack &item, ServerActiveObject *user,
lua_getglobal(L, "core");
lua_getfield(L, -1, "craft_predict");
LuaItemStack::create(L, item);
objectrefGetOrCreate(user);
objectrefGetOrCreate(L, user);
//Push inventory list
std::vector<ItemStack> items;
@ -229,7 +229,7 @@ void ScriptApiItem::pushPointedThing(const PointedThing& pointed)
{
lua_pushstring(L, "object");
lua_setfield(L, -2, "type");
objectrefGet(pointed.object_id);
objectrefGet(L, pointed.object_id);
lua_setfield(L, -2, "ref");
}
else

View File

@ -103,7 +103,7 @@ bool ScriptApiNode::node_on_punch(v3s16 p, MapNode node,
// Call function
push_v3s16(L, p);
pushnode(L, node, ndef);
objectrefGetOrCreate(puncher);
objectrefGetOrCreate(L, puncher);
pushPointedThing(pointed);
if (lua_pcall(L, 4, 0, m_errorhandler))
scriptError();
@ -124,7 +124,7 @@ bool ScriptApiNode::node_on_dig(v3s16 p, MapNode node,
// Call function
push_v3s16(L, p);
pushnode(L, node, ndef);
objectrefGetOrCreate(digger);
objectrefGetOrCreate(L, digger);
if (lua_pcall(L, 3, 0, m_errorhandler))
scriptError();
return true;
@ -227,7 +227,7 @@ void ScriptApiNode::node_on_receive_fields(v3s16 p,
lua_pushlstring(L, value.c_str(), value.size());
lua_settable(L, -3);
}
objectrefGetOrCreate(sender); // player
objectrefGetOrCreate(L, sender); // player
if (lua_pcall(L, 4, 0, m_errorhandler))
scriptError();
}

View File

@ -53,7 +53,7 @@ int ScriptApiNodemeta::nodemeta_inventory_AllowMove(v3s16 p,
lua_pushstring(L, to_list.c_str()); // to_list
lua_pushinteger(L, to_index + 1); // to_index
lua_pushinteger(L, count); // count
objectrefGetOrCreate(player); // player
objectrefGetOrCreate(L, player); // player
if (lua_pcall(L, 7, 1, m_errorhandler))
scriptError();
if (!lua_isnumber(L, -1))
@ -88,7 +88,7 @@ int ScriptApiNodemeta::nodemeta_inventory_AllowPut(v3s16 p,
lua_pushstring(L, listname.c_str()); // listname
lua_pushinteger(L, index + 1); // index
LuaItemStack::create(L, stack); // stack
objectrefGetOrCreate(player); // player
objectrefGetOrCreate(L, player); // player
if (lua_pcall(L, 5, 1, m_errorhandler))
scriptError();
if(!lua_isnumber(L, -1))
@ -123,7 +123,7 @@ int ScriptApiNodemeta::nodemeta_inventory_AllowTake(v3s16 p,
lua_pushstring(L, listname.c_str()); // listname
lua_pushinteger(L, index + 1); // index
LuaItemStack::create(L, stack); // stack
objectrefGetOrCreate(player); // player
objectrefGetOrCreate(L, player); // player
if (lua_pcall(L, 5, 1, m_errorhandler))
scriptError();
if (!lua_isnumber(L, -1))
@ -161,7 +161,7 @@ void ScriptApiNodemeta::nodemeta_inventory_OnMove(v3s16 p,
lua_pushstring(L, to_list.c_str()); // to_list
lua_pushinteger(L, to_index + 1); // to_index
lua_pushinteger(L, count); // count
objectrefGetOrCreate(player); // player
objectrefGetOrCreate(L, player); // player
if (lua_pcall(L, 7, 0, m_errorhandler))
scriptError();
}
@ -190,7 +190,7 @@ void ScriptApiNodemeta::nodemeta_inventory_OnPut(v3s16 p,
lua_pushstring(L, listname.c_str()); // listname
lua_pushinteger(L, index + 1); // index
LuaItemStack::create(L, stack); // stack
objectrefGetOrCreate(player); // player
objectrefGetOrCreate(L, player); // player
if (lua_pcall(L, 5, 0, m_errorhandler))
scriptError();
}
@ -219,7 +219,7 @@ void ScriptApiNodemeta::nodemeta_inventory_OnTake(v3s16 p,
lua_pushstring(L, listname.c_str()); // listname
lua_pushinteger(L, index + 1); // index
LuaItemStack::create(L, stack); // stack
objectrefGetOrCreate(player); // player
objectrefGetOrCreate(L, player); // player
if (lua_pcall(L, 5, 0, m_errorhandler))
scriptError();
}

View File

@ -29,7 +29,7 @@ void ScriptApiPlayer::on_newplayer(ServerActiveObject *player)
lua_getglobal(L, "core");
lua_getfield(L, -1, "registered_on_newplayers");
// Call callbacks
objectrefGetOrCreate(player);
objectrefGetOrCreate(L, player);
script_run_callbacks(L, 1, RUN_CALLBACKS_MODE_FIRST);
}
@ -41,7 +41,7 @@ void ScriptApiPlayer::on_dieplayer(ServerActiveObject *player)
lua_getglobal(L, "core");
lua_getfield(L, -1, "registered_on_dieplayers");
// Call callbacks
objectrefGetOrCreate(player);
objectrefGetOrCreate(L, player);
script_run_callbacks(L, 1, RUN_CALLBACKS_MODE_FIRST);
}
@ -53,7 +53,7 @@ bool ScriptApiPlayer::on_respawnplayer(ServerActiveObject *player)
lua_getglobal(L, "core");
lua_getfield(L, -1, "registered_on_respawnplayers");
// Call callbacks
objectrefGetOrCreate(player);
objectrefGetOrCreate(L, player);
script_run_callbacks(L, 1, RUN_CALLBACKS_MODE_OR);
bool positioning_handled_by_some = lua_toboolean(L, -1);
return positioning_handled_by_some;
@ -84,7 +84,7 @@ void ScriptApiPlayer::on_joinplayer(ServerActiveObject *player)
lua_getglobal(L, "core");
lua_getfield(L, -1, "registered_on_joinplayers");
// Call callbacks
objectrefGetOrCreate(player);
objectrefGetOrCreate(L, player);
script_run_callbacks(L, 1, RUN_CALLBACKS_MODE_FIRST);
}
@ -96,7 +96,7 @@ void ScriptApiPlayer::on_leaveplayer(ServerActiveObject *player)
lua_getglobal(L, "core");
lua_getfield(L, -1, "registered_on_leaveplayers");
// Call callbacks
objectrefGetOrCreate(player);
objectrefGetOrCreate(L, player);
script_run_callbacks(L, 1, RUN_CALLBACKS_MODE_FIRST);
}
@ -109,7 +109,7 @@ void ScriptApiPlayer::on_cheat(ServerActiveObject *player,
lua_getglobal(L, "core");
lua_getfield(L, -1, "registered_on_cheats");
// Call callbacks
objectrefGetOrCreate(player);
objectrefGetOrCreate(L, player);
lua_newtable(L);
lua_pushlstring(L, cheat_type.c_str(), cheat_type.size());
lua_setfield(L, -2, "type");
@ -127,7 +127,7 @@ void ScriptApiPlayer::on_playerReceiveFields(ServerActiveObject *player,
lua_getfield(L, -1, "registered_on_player_receive_fields");
// Call callbacks
// param 1
objectrefGetOrCreate(player);
objectrefGetOrCreate(L, player);
// param 2
lua_pushstring(L, formname.c_str());
// param 3

View File

@ -375,7 +375,7 @@ int ModApiEnvMod::l_add_entity(lua_State *L)
if(objectid == 0)
return 0;
// Return ObjectRef
getScriptApiBase(L)->objectrefGetOrCreate(obj);
getScriptApiBase(L)->objectrefGetOrCreate(L, obj);
return 1;
}
@ -440,7 +440,7 @@ int ModApiEnvMod::l_get_player_by_name(lua_State *L)
return 1;
}
// Put player on stack
getScriptApiBase(L)->objectrefGetOrCreate(sao);
getScriptApiBase(L)->objectrefGetOrCreate(L, sao);
return 1;
}
@ -459,7 +459,7 @@ int ModApiEnvMod::l_get_objects_inside_radius(lua_State *L)
for(u32 i = 0; iter != ids.end(); iter++) {
ServerActiveObject *obj = env->getActiveObject(*iter);
// Insert object reference into table
script->objectrefGetOrCreate(obj);
script->objectrefGetOrCreate(L, obj);
lua_rawseti(L, -2, ++i);
}
return 1;