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; }); = sol::readonly_property([](const ESM::Faction& rec) -> std::string_view { return rec.mName; });
factionT["hidden"] factionT["hidden"]
= sol::readonly_property([](const ESM::Faction& rec) -> bool { return rec.mData.mIsHidden; }); = 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); sol::table res(lua, sol::create);
for (size_t i = 0; i < rec.mRanks.size() && i < rec.mData.mRankData.size(); i++) for (size_t i = 0; i < rec.mRanks.size() && i < rec.mData.mRankData.size(); i++)
{ {
@ -70,7 +70,7 @@ namespace MWLua
return res; 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); sol::table res(lua, sol::create);
for (const auto& [factionId, reaction] : rec.mReactions) for (const auto& [factionId, reaction] : rec.mReactions)
res[factionId.serializeText()] = reaction; res[factionId.serializeText()] = reaction;
@ -86,10 +86,10 @@ namespace MWLua
return res; 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); 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); return createReadOnlyRefIdTable(lua, rec.mData.mSkills, ESM::Skill::indexToRefId);
}); });
auto rankT = lua.new_usertype<FactionRank>("ESM3_FactionRank"); 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["primarySkillValue"] = sol::readonly_property([](const FactionRank& rec) { return rec.mPrimarySkill; });
rankT["favouredSkillValue"] = sol::readonly_property([](const FactionRank& rec) { return rec.mFavouredSkill; }); rankT["favouredSkillValue"] = sol::readonly_property([](const FactionRank& rec) { return rec.mFavouredSkill; });
rankT["factionReaction"] = sol::readonly_property([](const FactionRank& rec) { return rec.mFactReaction; }); 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); sol::table res(lua, sol::create);
res.add(rec.mAttribute1); res.add(rec.mAttribute1);
res.add(rec.mAttribute2); res.add(rec.mAttribute2);

@ -9,7 +9,7 @@
namespace MWLua namespace MWLua
{ {
template <class C, class P = std::identity> 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); sol::table res(lua, sol::create);
for (const auto& element : container) for (const auto& element : container)

@ -115,7 +115,7 @@ namespace MWLua
aiPackage["distance"] = sol::readonly_property([](const AiPackage& p) { return p.getDistance(); }); aiPackage["distance"] = sol::readonly_property([](const AiPackage& p) { return p.getDistance(); });
aiPackage["duration"] = sol::readonly_property([](const AiPackage& p) { return p.getDuration(); }); aiPackage["duration"] = sol::readonly_property([](const AiPackage& p) { return p.getDuration(); });
aiPackage["idle"] 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) if (p.getTypeId() == MWMechanics::AiPackageTypeId::Wander)
{ {
sol::table idles(lua, sol::create); 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); sol::table res(lua, sol::create);
for (size_t i = 0; i < effects.size(); ++i) 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); }); [](const ESM::Spell& rec) -> bool { return !!(rec.mData.mFlags & ESM::Spell::F_PCStart); });
spellT["autocalcFlag"] = sol::readonly_property( spellT["autocalcFlag"] = sol::readonly_property(
[](const ESM::Spell& rec) -> bool { return !!(rec.mData.mFlags & ESM::Spell::F_Autocalc); }); [](const ESM::Spell& rec) -> bool { return !!(rec.mData.mFlags & ESM::Spell::F_Autocalc); });
spellT["effects"] = sol::readonly_property( spellT["effects"] = sol::readonly_property([lua = lua.lua_state()](const ESM::Spell& rec) -> sol::table {
[&lua](const ESM::Spell& rec) -> sol::table { return effectParamsListToTable(lua, rec.mEffects.mList); }); return effectParamsListToTable(lua, rec.mEffects.mList);
});
// Enchantment record // Enchantment record
auto enchantT = lua.new_usertype<ESM::Enchantment>("ESM3_Enchantment"); auto enchantT = lua.new_usertype<ESM::Enchantment>("ESM3_Enchantment");
@ -328,9 +329,10 @@ namespace MWLua
enchantT["cost"] = sol::readonly_property([](const ESM::Enchantment& rec) -> int { return rec.mData.mCost; }); enchantT["cost"] = sol::readonly_property([](const ESM::Enchantment& rec) -> int { return rec.mData.mCost; });
enchantT["charge"] enchantT["charge"]
= sol::readonly_property([](const ESM::Enchantment& rec) -> int { return rec.mData.mCharge; }); = 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"]
return effectParamsListToTable(lua, rec.mEffects.mList); = sol::readonly_property([lua = lua.lua_state()](const ESM::Enchantment& rec) -> sol::table {
}); return effectParamsListToTable(lua, rec.mEffects.mList);
});
// Effect params // Effect params
auto effectParamsT = lua.new_usertype<ESM::IndexedENAMstruct>("ESM3_EffectParams"); auto effectParamsT = lua.new_usertype<ESM::IndexedENAMstruct>("ESM3_EffectParams");
@ -522,42 +524,45 @@ namespace MWLua
activeSpellT["id"] = sol::readonly_property([](const ActiveSpell& activeSpell) -> std::string { activeSpellT["id"] = sol::readonly_property([](const ActiveSpell& activeSpell) -> std::string {
return activeSpell.mParams.getSourceSpellId().serializeText(); return activeSpell.mParams.getSourceSpellId().serializeText();
}); });
activeSpellT["item"] = sol::readonly_property([&lua](const ActiveSpell& activeSpell) -> sol::object { activeSpellT["item"]
auto item = activeSpell.mParams.getItem(); = sol::readonly_property([lua = lua.lua_state()](const ActiveSpell& activeSpell) -> sol::object {
if (!item.isSet()) auto item = activeSpell.mParams.getItem();
return sol::nil; if (!item.isSet())
auto itemPtr = MWBase::Environment::get().getWorldModel()->getPtr(item); return sol::nil;
if (itemPtr.isEmpty()) auto itemPtr = MWBase::Environment::get().getWorldModel()->getPtr(item);
return sol::nil; if (itemPtr.isEmpty())
if (activeSpell.mActor.isGObject()) return sol::nil;
return sol::make_object(lua, GObject(itemPtr)); if (activeSpell.mActor.isGObject())
else return sol::make_object(lua, GObject(itemPtr));
return sol::make_object(lua, LObject(itemPtr)); else
}); return sol::make_object(lua, LObject(itemPtr));
activeSpellT["caster"] = sol::readonly_property([&lua](const ActiveSpell& activeSpell) -> sol::object { });
auto caster activeSpellT["caster"]
= MWBase::Environment::get().getWorld()->searchPtrViaActorId(activeSpell.mParams.getCasterActorId()); = sol::readonly_property([lua = lua.lua_state()](const ActiveSpell& activeSpell) -> sol::object {
if (caster.isEmpty()) auto caster = MWBase::Environment::get().getWorld()->searchPtrViaActorId(
return sol::nil; activeSpell.mParams.getCasterActorId());
else if (caster.isEmpty())
{ return sol::nil;
if (activeSpell.mActor.isGObject()) else
return sol::make_object(lua, GObject(getId(caster))); {
else if (activeSpell.mActor.isGObject())
return sol::make_object(lua, LObject(getId(caster))); 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; activeSpellT["effects"]
for (const ESM::ActiveEffect& effect : activeSpell.mParams.getEffects()) = sol::readonly_property([lua = lua.lua_state()](const ActiveSpell& activeSpell) -> sol::table {
{ sol::table res(lua, sol::create);
if (!(effect.mFlags & ESM::ActiveEffect::Flag_Applied)) size_t tableIndex = 0;
continue; for (const ESM::ActiveEffect& effect : activeSpell.mParams.getEffects())
res[++tableIndex] = effect; // ESM::ActiveEffect (effect params) {
} if (!(effect.mFlags & ESM::ActiveEffect::Flag_Applied))
return res; continue;
}); res[++tableIndex] = effect; // ESM::ActiveEffect (effect params)
}
return res;
});
activeSpellT["fromEquipment"] = sol::readonly_property([](const ActiveSpell& activeSpell) -> bool { activeSpellT["fromEquipment"] = sol::readonly_property([](const ActiveSpell& activeSpell) -> bool {
return activeSpell.mParams.hasFlag(ESM::ActiveSpells::Flag_Equipment); return activeSpell.mParams.hasFlag(ESM::ActiveSpells::Flag_Equipment);
}); });

@ -52,7 +52,7 @@ namespace MWLua
record["magicSkill"] = sol::readonly_property([](const ESM::Creature& rec) -> int { return rec.mData.mMagic; }); record["magicSkill"] = sol::readonly_property([](const ESM::Creature& rec) -> int { return rec.mData.mMagic; });
record["stealthSkill"] record["stealthSkill"]
= sol::readonly_property([](const ESM::Creature& rec) -> int { return rec.mData.mStealth; }); = 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); sol::table res(lua, sol::create);
int index = 1; int index = 1;
for (auto attack : rec.mData.mAttack) for (auto attack : rec.mData.mAttack)

@ -24,8 +24,8 @@ namespace MWLua
auto vfs = MWBase::Environment::get().getResourceSystem()->getVFS(); auto vfs = MWBase::Environment::get().getResourceSystem()->getVFS();
addRecordFunctionBinding<ESM::Ingredient>(ingredient, context); addRecordFunctionBinding<ESM::Ingredient>(ingredient, context);
sol::state_view lua = context.sol();
sol::usertype<ESM::Ingredient> record = context.sol().new_usertype<ESM::Ingredient>(("ESM3_Ingredient")); sol::usertype<ESM::Ingredient> record = lua.new_usertype<ESM::Ingredient>(("ESM3_Ingredient"));
record[sol::meta_function::to_string] record[sol::meta_function::to_string]
= [](const ESM::Ingredient& rec) { return "ESM3_Ingredient[" + rec.mId.toDebugString() + "]"; }; = [](const ESM::Ingredient& rec) { return "ESM3_Ingredient[" + rec.mId.toDebugString() + "]"; };
record["id"] record["id"]
@ -43,7 +43,7 @@ namespace MWLua
record["weight"] record["weight"]
= sol::readonly_property([](const ESM::Ingredient& rec) -> float { return rec.mData.mWeight; }); = 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["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); sol::table res(lua, sol::create);
for (size_t i = 0; i < 4; ++i) for (size_t i = 0; i < 4; ++i)
{ {

@ -333,7 +333,7 @@ namespace MWLua
ESM::RefId factionId = parseFactionId(faction); ESM::RefId factionId = parseFactionId(faction);
return ptr.getClass().getNpcStats(ptr).getExpelled(factionId); 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(); const MWWorld::Ptr ptr = actor.ptr();
MWMechanics::NpcStats& npcStats = ptr.getClass().getNpcStats(ptr); MWMechanics::NpcStats& npcStats = ptr.getClass().getNpcStats(ptr);
sol::table res(lua, sol::create); sol::table res(lua, sol::create);

@ -69,7 +69,8 @@ namespace MWLua
potion["createRecordDraft"] = tableToPotion; potion["createRecordDraft"] = tableToPotion;
auto vfs = MWBase::Environment::get().getResourceSystem()->getVFS(); 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] record[sol::meta_function::to_string]
= [](const ESM::Potion& rec) { return "ESM3_Potion[" + rec.mId.toDebugString() + "]"; }; = [](const ESM::Potion& rec) { return "ESM3_Potion[" + rec.mId.toDebugString() + "]"; };
record["id"] record["id"]
@ -84,7 +85,7 @@ namespace MWLua
[](const ESM::Potion& rec) -> sol::optional<std::string> { return LuaUtil::serializeRefId(rec.mScript); }); [](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["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["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); sol::table res(lua, sol::create);
for (size_t i = 0; i < rec.mEffects.mList.size(); ++i) for (size_t i = 0; i < rec.mEffects.mList.size(); ++i)
res[LuaUtil::toLuaIndex(i)] = rec.mEffects.mList[i]; // ESM::IndexedENAMstruct (effect params) res[LuaUtil::toLuaIndex(i)] = rec.mEffects.mList[i]; // ESM::IndexedENAMstruct (effect params)

Loading…
Cancel
Save