Remove references to temporaries and this_state in properties

pull/3236/head
Evil Eye 4 months ago
parent 090c65c017
commit a32e006e1c

@ -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<FactionRank>("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);

@ -9,7 +9,7 @@
namespace MWLua
{
template <class C, class P = std::identity>
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)

@ -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::table> {
= sol::readonly_property([lua = lua.lua_state()](const AiPackage& p) -> sol::optional<sol::table> {
if (p.getTypeId() == MWMechanics::AiPackageTypeId::Wander)
{
sol::table idles(lua, sol::create);

@ -204,7 +204,7 @@ namespace MWLua
}
}
static sol::table effectParamsListToTable(sol::state_view& lua, const std::vector<ESM::IndexedENAMstruct>& effects)
static sol::table effectParamsListToTable(lua_State* lua, const std::vector<ESM::IndexedENAMstruct>& 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<ESM::Enchantment>("ESM3_Enchantment");
@ -328,7 +329,8 @@ 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 {
enchantT["effects"]
= sol::readonly_property([lua = lua.lua_state()](const ESM::Enchantment& rec) -> sol::table {
return effectParamsListToTable(lua, rec.mEffects.mList);
});
@ -522,7 +524,8 @@ 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 {
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;
@ -534,9 +537,10 @@ namespace MWLua
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());
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
@ -547,7 +551,8 @@ namespace MWLua
return sol::make_object(lua, LObject(getId(caster)));
}
});
activeSpellT["effects"] = sol::readonly_property([&lua](const ActiveSpell& activeSpell) -> sol::table {
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())

@ -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)

@ -24,8 +24,8 @@ namespace MWLua
auto vfs = MWBase::Environment::get().getResourceSystem()->getVFS();
addRecordFunctionBinding<ESM::Ingredient>(ingredient, context);
sol::usertype<ESM::Ingredient> record = context.sol().new_usertype<ESM::Ingredient>(("ESM3_Ingredient"));
sol::state_view lua = context.sol();
sol::usertype<ESM::Ingredient> record = lua.new_usertype<ESM::Ingredient>(("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)
{

@ -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);

@ -69,7 +69,8 @@ namespace MWLua
potion["createRecordDraft"] = tableToPotion;
auto vfs = MWBase::Environment::get().getResourceSystem()->getVFS();
sol::usertype<ESM::Potion> record = context.sol().new_usertype<ESM::Potion>("ESM3_Potion");
sol::state_view lua = context.sol();
sol::usertype<ESM::Potion> record = lua.new_usertype<ESM::Potion>("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<std::string> { 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)

Loading…
Cancel
Save