Skip to content

Commit

Permalink
Remove references to temporaries and this_state in properties
Browse files Browse the repository at this point in the history
  • Loading branch information
Assumeru committed Sep 6, 2024
1 parent 090c65c commit a32e006
Show file tree
Hide file tree
Showing 8 changed files with 62 additions and 56 deletions.
10 changes: 5 additions & 5 deletions apps/openmw/mwlua/factionbindings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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++)
{
Expand All @@ -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;
Expand All @@ -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");
Expand All @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion apps/openmw/mwlua/idcollectionbindings.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion apps/openmw/mwlua/localscripts.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
89 changes: 47 additions & 42 deletions apps/openmw/mwlua/magicbindings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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");
Expand All @@ -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<ESM::IndexedENAMstruct>("ESM3_EffectParams");
Expand Down Expand Up @@ -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);
});
Expand Down
2 changes: 1 addition & 1 deletion apps/openmw/mwlua/types/creature.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
6 changes: 3 additions & 3 deletions apps/openmw/mwlua/types/ingredient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand All @@ -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)
{
Expand Down
2 changes: 1 addition & 1 deletion apps/openmw/mwlua/types/npc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
5 changes: 3 additions & 2 deletions apps/openmw/mwlua/types/potion.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand All @@ -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)
Expand Down

0 comments on commit a32e006

Please sign in to comment.