From a32e006e1c62f512ed4d5f3b8236ab6a3405a194 Mon Sep 17 00:00:00 2001 From: Evil Eye Date: Fri, 6 Sep 2024 16:57:36 +0200 Subject: [PATCH] Remove references to temporaries and this_state in properties --- apps/openmw/mwlua/factionbindings.cpp | 10 +-- apps/openmw/mwlua/idcollectionbindings.hpp | 2 +- apps/openmw/mwlua/localscripts.cpp | 2 +- apps/openmw/mwlua/magicbindings.cpp | 89 ++++++++++++---------- apps/openmw/mwlua/types/creature.cpp | 2 +- apps/openmw/mwlua/types/ingredient.cpp | 6 +- apps/openmw/mwlua/types/npc.cpp | 2 +- apps/openmw/mwlua/types/potion.cpp | 5 +- 8 files changed, 62 insertions(+), 56 deletions(-) diff --git a/apps/openmw/mwlua/factionbindings.cpp b/apps/openmw/mwlua/factionbindings.cpp index 2116752647..45234827de 100644 --- a/apps/openmw/mwlua/factionbindings.cpp +++ b/apps/openmw/mwlua/factionbindings.cpp @@ -58,7 +58,7 @@ namespace MWLua = sol::readonly_property([](const ESM::Faction& rec) -> std::string_view { return rec.mName; }); factionT["hidden"] = sol::readonly_property([](const ESM::Faction& rec) -> bool { return rec.mData.mIsHidden; }); - factionT["ranks"] = sol::readonly_property([&lua](const ESM::Faction& rec) { + factionT["ranks"] = sol::readonly_property([lua = lua.lua_state()](const ESM::Faction& rec) { sol::table res(lua, sol::create); for (size_t i = 0; i < rec.mRanks.size() && i < rec.mData.mRankData.size(); i++) { @@ -70,7 +70,7 @@ namespace MWLua return res; }); - factionT["reactions"] = sol::readonly_property([&lua](const ESM::Faction& rec) { + factionT["reactions"] = sol::readonly_property([lua = lua.lua_state()](const ESM::Faction& rec) { sol::table res(lua, sol::create); for (const auto& [factionId, reaction] : rec.mReactions) res[factionId.serializeText()] = reaction; @@ -86,10 +86,10 @@ namespace MWLua return res; }); - factionT["attributes"] = sol::readonly_property([&lua](const ESM::Faction& rec) { + factionT["attributes"] = sol::readonly_property([lua = lua.lua_state()](const ESM::Faction& rec) { return createReadOnlyRefIdTable(lua, rec.mData.mAttribute, ESM::Attribute::indexToRefId); }); - factionT["skills"] = sol::readonly_property([&lua](const ESM::Faction& rec) { + factionT["skills"] = sol::readonly_property([lua = lua.lua_state()](const ESM::Faction& rec) { return createReadOnlyRefIdTable(lua, rec.mData.mSkills, ESM::Skill::indexToRefId); }); auto rankT = lua.new_usertype("ESM3_FactionRank"); @@ -102,7 +102,7 @@ namespace MWLua rankT["primarySkillValue"] = sol::readonly_property([](const FactionRank& rec) { return rec.mPrimarySkill; }); rankT["favouredSkillValue"] = sol::readonly_property([](const FactionRank& rec) { return rec.mFavouredSkill; }); rankT["factionReaction"] = sol::readonly_property([](const FactionRank& rec) { return rec.mFactReaction; }); - rankT["attributeValues"] = sol::readonly_property([&lua](const FactionRank& rec) { + rankT["attributeValues"] = sol::readonly_property([lua = lua.lua_state()](const FactionRank& rec) { sol::table res(lua, sol::create); res.add(rec.mAttribute1); res.add(rec.mAttribute2); diff --git a/apps/openmw/mwlua/idcollectionbindings.hpp b/apps/openmw/mwlua/idcollectionbindings.hpp index 15e2b14fb9..329486aba1 100644 --- a/apps/openmw/mwlua/idcollectionbindings.hpp +++ b/apps/openmw/mwlua/idcollectionbindings.hpp @@ -9,7 +9,7 @@ namespace MWLua { template - sol::table createReadOnlyRefIdTable(const sol::state_view& lua, const C& container, P projection = {}) + sol::table createReadOnlyRefIdTable(lua_State* lua, const C& container, P projection = {}) { sol::table res(lua, sol::create); for (const auto& element : container) diff --git a/apps/openmw/mwlua/localscripts.cpp b/apps/openmw/mwlua/localscripts.cpp index d57ac8da9b..988bd73051 100644 --- a/apps/openmw/mwlua/localscripts.cpp +++ b/apps/openmw/mwlua/localscripts.cpp @@ -115,7 +115,7 @@ namespace MWLua aiPackage["distance"] = sol::readonly_property([](const AiPackage& p) { return p.getDistance(); }); aiPackage["duration"] = sol::readonly_property([](const AiPackage& p) { return p.getDuration(); }); aiPackage["idle"] - = sol::readonly_property([](sol::this_state lua, const AiPackage& p) -> sol::optional { + = sol::readonly_property([lua = lua.lua_state()](const AiPackage& p) -> sol::optional { if (p.getTypeId() == MWMechanics::AiPackageTypeId::Wander) { sol::table idles(lua, sol::create); diff --git a/apps/openmw/mwlua/magicbindings.cpp b/apps/openmw/mwlua/magicbindings.cpp index d77ea90806..4fac1533e8 100644 --- a/apps/openmw/mwlua/magicbindings.cpp +++ b/apps/openmw/mwlua/magicbindings.cpp @@ -204,7 +204,7 @@ namespace MWLua } } - static sol::table effectParamsListToTable(sol::state_view& lua, const std::vector& effects) + static sol::table effectParamsListToTable(lua_State* lua, const std::vector& effects) { sol::table res(lua, sol::create); for (size_t i = 0; i < effects.size(); ++i) @@ -313,8 +313,9 @@ namespace MWLua [](const ESM::Spell& rec) -> bool { return !!(rec.mData.mFlags & ESM::Spell::F_PCStart); }); spellT["autocalcFlag"] = sol::readonly_property( [](const ESM::Spell& rec) -> bool { return !!(rec.mData.mFlags & ESM::Spell::F_Autocalc); }); - spellT["effects"] = sol::readonly_property( - [&lua](const ESM::Spell& rec) -> sol::table { return effectParamsListToTable(lua, rec.mEffects.mList); }); + spellT["effects"] = sol::readonly_property([lua = lua.lua_state()](const ESM::Spell& rec) -> sol::table { + return effectParamsListToTable(lua, rec.mEffects.mList); + }); // Enchantment record auto enchantT = lua.new_usertype("ESM3_Enchantment"); @@ -328,9 +329,10 @@ namespace MWLua enchantT["cost"] = sol::readonly_property([](const ESM::Enchantment& rec) -> int { return rec.mData.mCost; }); enchantT["charge"] = sol::readonly_property([](const ESM::Enchantment& rec) -> int { return rec.mData.mCharge; }); - enchantT["effects"] = sol::readonly_property([&lua](const ESM::Enchantment& rec) -> sol::table { - return effectParamsListToTable(lua, rec.mEffects.mList); - }); + enchantT["effects"] + = sol::readonly_property([lua = lua.lua_state()](const ESM::Enchantment& rec) -> sol::table { + return effectParamsListToTable(lua, rec.mEffects.mList); + }); // Effect params auto effectParamsT = lua.new_usertype("ESM3_EffectParams"); @@ -522,42 +524,45 @@ namespace MWLua activeSpellT["id"] = sol::readonly_property([](const ActiveSpell& activeSpell) -> std::string { return activeSpell.mParams.getSourceSpellId().serializeText(); }); - activeSpellT["item"] = sol::readonly_property([&lua](const ActiveSpell& activeSpell) -> sol::object { - auto item = activeSpell.mParams.getItem(); - if (!item.isSet()) - return sol::nil; - auto itemPtr = MWBase::Environment::get().getWorldModel()->getPtr(item); - if (itemPtr.isEmpty()) - return sol::nil; - if (activeSpell.mActor.isGObject()) - return sol::make_object(lua, GObject(itemPtr)); - else - return sol::make_object(lua, LObject(itemPtr)); - }); - activeSpellT["caster"] = sol::readonly_property([&lua](const ActiveSpell& activeSpell) -> sol::object { - auto caster - = MWBase::Environment::get().getWorld()->searchPtrViaActorId(activeSpell.mParams.getCasterActorId()); - if (caster.isEmpty()) - return sol::nil; - else - { - if (activeSpell.mActor.isGObject()) - return sol::make_object(lua, GObject(getId(caster))); - else - return sol::make_object(lua, LObject(getId(caster))); - } - }); - activeSpellT["effects"] = sol::readonly_property([&lua](const ActiveSpell& activeSpell) -> sol::table { - sol::table res(lua, sol::create); - size_t tableIndex = 0; - for (const ESM::ActiveEffect& effect : activeSpell.mParams.getEffects()) - { - if (!(effect.mFlags & ESM::ActiveEffect::Flag_Applied)) - continue; - res[++tableIndex] = effect; // ESM::ActiveEffect (effect params) - } - return res; - }); + activeSpellT["item"] + = sol::readonly_property([lua = lua.lua_state()](const ActiveSpell& activeSpell) -> sol::object { + auto item = activeSpell.mParams.getItem(); + if (!item.isSet()) + return sol::nil; + auto itemPtr = MWBase::Environment::get().getWorldModel()->getPtr(item); + if (itemPtr.isEmpty()) + return sol::nil; + if (activeSpell.mActor.isGObject()) + return sol::make_object(lua, GObject(itemPtr)); + else + return sol::make_object(lua, LObject(itemPtr)); + }); + activeSpellT["caster"] + = sol::readonly_property([lua = lua.lua_state()](const ActiveSpell& activeSpell) -> sol::object { + auto caster = MWBase::Environment::get().getWorld()->searchPtrViaActorId( + activeSpell.mParams.getCasterActorId()); + if (caster.isEmpty()) + return sol::nil; + else + { + if (activeSpell.mActor.isGObject()) + return sol::make_object(lua, GObject(getId(caster))); + else + return sol::make_object(lua, LObject(getId(caster))); + } + }); + activeSpellT["effects"] + = sol::readonly_property([lua = lua.lua_state()](const ActiveSpell& activeSpell) -> sol::table { + sol::table res(lua, sol::create); + size_t tableIndex = 0; + for (const ESM::ActiveEffect& effect : activeSpell.mParams.getEffects()) + { + if (!(effect.mFlags & ESM::ActiveEffect::Flag_Applied)) + continue; + res[++tableIndex] = effect; // ESM::ActiveEffect (effect params) + } + return res; + }); activeSpellT["fromEquipment"] = sol::readonly_property([](const ActiveSpell& activeSpell) -> bool { return activeSpell.mParams.hasFlag(ESM::ActiveSpells::Flag_Equipment); }); diff --git a/apps/openmw/mwlua/types/creature.cpp b/apps/openmw/mwlua/types/creature.cpp index f9ef65a707..a171f147e6 100644 --- a/apps/openmw/mwlua/types/creature.cpp +++ b/apps/openmw/mwlua/types/creature.cpp @@ -52,7 +52,7 @@ namespace MWLua record["magicSkill"] = sol::readonly_property([](const ESM::Creature& rec) -> int { return rec.mData.mMagic; }); record["stealthSkill"] = sol::readonly_property([](const ESM::Creature& rec) -> int { return rec.mData.mStealth; }); - record["attack"] = sol::readonly_property([](sol::this_state lua, const ESM::Creature& rec) -> sol::table { + record["attack"] = sol::readonly_property([lua = lua.lua_state()](const ESM::Creature& rec) -> sol::table { sol::table res(lua, sol::create); int index = 1; for (auto attack : rec.mData.mAttack) diff --git a/apps/openmw/mwlua/types/ingredient.cpp b/apps/openmw/mwlua/types/ingredient.cpp index 9855b44265..e05dc2bdb8 100644 --- a/apps/openmw/mwlua/types/ingredient.cpp +++ b/apps/openmw/mwlua/types/ingredient.cpp @@ -24,8 +24,8 @@ namespace MWLua auto vfs = MWBase::Environment::get().getResourceSystem()->getVFS(); addRecordFunctionBinding(ingredient, context); - - sol::usertype record = context.sol().new_usertype(("ESM3_Ingredient")); + sol::state_view lua = context.sol(); + sol::usertype record = lua.new_usertype(("ESM3_Ingredient")); record[sol::meta_function::to_string] = [](const ESM::Ingredient& rec) { return "ESM3_Ingredient[" + rec.mId.toDebugString() + "]"; }; record["id"] @@ -43,7 +43,7 @@ namespace MWLua record["weight"] = sol::readonly_property([](const ESM::Ingredient& rec) -> float { return rec.mData.mWeight; }); record["value"] = sol::readonly_property([](const ESM::Ingredient& rec) -> int { return rec.mData.mValue; }); - record["effects"] = sol::readonly_property([](sol::this_state lua, const ESM::Ingredient& rec) -> sol::table { + record["effects"] = sol::readonly_property([lua = lua.lua_state()](const ESM::Ingredient& rec) -> sol::table { sol::table res(lua, sol::create); for (size_t i = 0; i < 4; ++i) { diff --git a/apps/openmw/mwlua/types/npc.cpp b/apps/openmw/mwlua/types/npc.cpp index 1a2876dc98..c0cfbc90db 100644 --- a/apps/openmw/mwlua/types/npc.cpp +++ b/apps/openmw/mwlua/types/npc.cpp @@ -333,7 +333,7 @@ namespace MWLua ESM::RefId factionId = parseFactionId(faction); return ptr.getClass().getNpcStats(ptr).getExpelled(factionId); }; - npc["getFactions"] = [&lua](const Object& actor) { + npc["getFactions"] = [](sol::this_state lua, const Object& actor) { const MWWorld::Ptr ptr = actor.ptr(); MWMechanics::NpcStats& npcStats = ptr.getClass().getNpcStats(ptr); sol::table res(lua, sol::create); diff --git a/apps/openmw/mwlua/types/potion.cpp b/apps/openmw/mwlua/types/potion.cpp index 34d41d4588..4ba3351cc4 100644 --- a/apps/openmw/mwlua/types/potion.cpp +++ b/apps/openmw/mwlua/types/potion.cpp @@ -69,7 +69,8 @@ namespace MWLua potion["createRecordDraft"] = tableToPotion; auto vfs = MWBase::Environment::get().getResourceSystem()->getVFS(); - sol::usertype record = context.sol().new_usertype("ESM3_Potion"); + sol::state_view lua = context.sol(); + sol::usertype record = lua.new_usertype("ESM3_Potion"); record[sol::meta_function::to_string] = [](const ESM::Potion& rec) { return "ESM3_Potion[" + rec.mId.toDebugString() + "]"; }; record["id"] @@ -84,7 +85,7 @@ namespace MWLua [](const ESM::Potion& rec) -> sol::optional { return LuaUtil::serializeRefId(rec.mScript); }); record["weight"] = sol::readonly_property([](const ESM::Potion& rec) -> float { return rec.mData.mWeight; }); record["value"] = sol::readonly_property([](const ESM::Potion& rec) -> int { return rec.mData.mValue; }); - record["effects"] = sol::readonly_property([](sol::this_state lua, const ESM::Potion& rec) -> sol::table { + record["effects"] = sol::readonly_property([lua = lua.lua_state()](const ESM::Potion& rec) -> sol::table { sol::table res(lua, sol::create); for (size_t i = 0; i < rec.mEffects.mList.size(); ++i) res[LuaUtil::toLuaIndex(i)] = rec.mEffects.mList[i]; // ESM::IndexedENAMstruct (effect params)