1
0
Fork 0
mirror of https://github.com/OpenMW/openmw.git synced 2025-01-30 15:45:33 +00:00

Fixed issue with getSummonedCreature( that returned a reference to a non const static value

Fix compile, and apply review comment

Fixed greater vs more typo.

getCellname is back to a string view.

Because in most cases was used as a strong not a refId.
and there was a fundamental issue with region names used as a cellname
This commit is contained in:
florent.teppe 2022-12-01 22:09:30 +01:00
parent 1ef1de974d
commit dc21df97c8
17 changed files with 57 additions and 43 deletions

View file

@ -177,12 +177,12 @@ namespace MWBase
virtual char getGlobalVariableType(std::string_view name) const = 0; virtual char getGlobalVariableType(std::string_view name) const = 0;
///< Return ' ', if there is no global variable with this name. ///< Return ' ', if there is no global variable with this name.
virtual const ESM::RefId& getCellName(const MWWorld::CellStore* cell = nullptr) const = 0; virtual std::string_view getCellName(const MWWorld::CellStore* cell = nullptr) const = 0;
///< Return name of the cell. ///< Return name of the cell.
/// ///
/// \note If cell==0, the cell the player is currently in will be used instead to /// \note If cell==0, the cell the player is currently in will be used instead to
/// generate a name. /// generate a name.
virtual const ESM::RefId getCellName(const ESM::Cell* cell) const = 0; virtual std::string_view getCellName(const ESM::Cell* cell) const = 0;
virtual void removeRefScript(MWWorld::RefData* ref) = 0; virtual void removeRefScript(MWWorld::RefData* ref) = 0;
//< Remove the script attached to ref from mLocalScripts //< Remove the script attached to ref from mLocalScripts

View file

@ -299,7 +299,7 @@ namespace MWClass
std::string Door::getDestination(const MWWorld::LiveCellRef<ESM::Door>& door) std::string Door::getDestination(const MWWorld::LiveCellRef<ESM::Door>& door)
{ {
auto dest = door.mRef.getDestCell(); std::string_view dest = door.mRef.getDestCell().getRefIdString();
if (dest.empty()) if (dest.empty())
{ {
// door leads to exterior, use cell name (if any), otherwise translated region name // door leads to exterior, use cell name (if any), otherwise translated region name
@ -310,7 +310,7 @@ namespace MWClass
dest = world->getCellName(cell); dest = world->getCellName(cell);
} }
return "#{sCell=" + dest.getRefIdString() + "}"; return "#{sCell=" + std::string{ dest } + "}";
} }
MWWorld::Ptr Door::copyToCellImpl(const MWWorld::ConstPtr& ptr, MWWorld::CellStore& cell) const MWWorld::Ptr Door::copyToCellImpl(const MWWorld::ConstPtr& ptr, MWWorld::CellStore& cell) const

View file

@ -196,8 +196,7 @@ bool MWDialogue::Filter::testPlayer(const ESM::DialInfo& info) const
if (!info.mCell.empty()) if (!info.mCell.empty())
{ {
// supports partial matches, just like getPcCell // supports partial matches, just like getPcCell
std::string_view playerCell std::string_view playerCell = MWBase::Environment::get().getWorld()->getCellName(player.getCell());
= MWBase::Environment::get().getWorld()->getCellName(player.getCell()).getRefIdString();
if (!Misc::StringUtils::ciStartsWith(playerCell, info.mCell.getRefIdString())) if (!Misc::StringUtils::ciStartsWith(playerCell, info.mCell.getRefIdString()))
return false; return false;
} }
@ -554,8 +553,7 @@ bool MWDialogue::Filter::getSelectStructBoolean(const SelectWrapper& select) con
case SelectWrapper::Function_NotCell: case SelectWrapper::Function_NotCell:
{ {
std::string_view actorCell std::string_view actorCell = MWBase::Environment::get().getWorld()->getCellName(mActor.getCell());
= MWBase::Environment::get().getWorld()->getCellName(mActor.getCell()).getRefIdString();
return !Misc::StringUtils::ciStartsWith(actorCell, select.getName()); return !Misc::StringUtils::ciStartsWith(actorCell, select.getName());
} }
case SelectWrapper::Function_SameGender: case SelectWrapper::Function_SameGender:

View file

@ -330,7 +330,7 @@ namespace MWGui
continue; continue;
if (mGenderIndex != (bodypart.mData.mFlags & ESM::BodyPart::BPF_Female)) if (mGenderIndex != (bodypart.mData.mFlags & ESM::BodyPart::BPF_Female))
continue; continue;
bool firstPerson = idString.ends_with("1st"); bool firstPerson = Misc::StringUtils::ciEndsWith(idString, "1st");
if (firstPerson) if (firstPerson)
continue; continue;
if (bodypart.mRace == mCurrentRaceId) if (bodypart.mRace == mCurrentRaceId)

View file

@ -123,7 +123,7 @@ namespace MWGui
for (unsigned int i = 0; i < transport.size(); i++) for (unsigned int i = 0; i < transport.size(); i++)
{ {
ESM::RefId cellname = transport[i].mCellName; std::string_view cellname = transport[i].mCellName.getRefIdString();
bool interior = true; bool interior = true;
const osg::Vec2i cellIndex const osg::Vec2i cellIndex
= MWWorld::positionToCellIndex(transport[i].mPos.pos[0], transport[i].mPos.pos[1]); = MWWorld::positionToCellIndex(transport[i].mPos.pos[0], transport[i].mPos.pos[1]);
@ -134,7 +134,7 @@ namespace MWGui
cellname = MWBase::Environment::get().getWorld()->getCellName(cell); cellname = MWBase::Environment::get().getWorld()->getCellName(cell);
interior = false; interior = false;
} }
addDestination(cellname, transport[i].mPos, interior); addDestination(ESM::RefId::stringRefId(cellname), transport[i].mPos, interior);
} }
updateLabels(); updateLabels();

View file

@ -933,7 +933,7 @@ namespace MWGui
{ {
mMap->requestMapRender(cell); mMap->requestMapRender(cell);
std::string name = MWBase::Environment::get().getWorld()->getCellName(cell).getRefIdString(); std::string name{ MWBase::Environment::get().getWorld()->getCellName(cell) };
mMap->setCellName(name); mMap->setCellName(name);
mHud->setCellName(name); mHud->setCellName(name);

View file

@ -29,9 +29,14 @@ namespace MWMechanics
|| (effectId >= ESM::MagicEffect::SummonFabricant && effectId <= ESM::MagicEffect::SummonCreature05)); || (effectId >= ESM::MagicEffect::SummonFabricant && effectId <= ESM::MagicEffect::SummonCreature05));
} }
const ESM::RefId& getSummonedCreature(int effectId) static const std::map<int, ESM::RefId>& getSummonMap()
{ {
static const std::map<int, std::string_view> summonMap{ static std::map<int, ESM::RefId> summonMap;
if (summonMap.size() > 0)
return summonMap;
const std::map<int, std::string_view> summonMapToGameSetting{
{ ESM::MagicEffect::SummonAncestralGhost, "sMagicAncestralGhostID" }, { ESM::MagicEffect::SummonAncestralGhost, "sMagicAncestralGhostID" },
{ ESM::MagicEffect::SummonBonelord, "sMagicBonelordID" }, { ESM::MagicEffect::SummonBonelord, "sMagicBonelordID" },
{ ESM::MagicEffect::SummonBonewalker, "sMagicLeastBonewalkerID" }, { ESM::MagicEffect::SummonBonewalker, "sMagicLeastBonewalkerID" },
@ -56,16 +61,25 @@ namespace MWMechanics
{ ESM::MagicEffect::SummonCreature05, "sMagicCreature05ID" }, { ESM::MagicEffect::SummonCreature05, "sMagicCreature05ID" },
}; };
for (const auto& it : summonMapToGameSetting)
{
summonMap[it.first] = ESM::RefId::stringRefId(MWBase::Environment::get()
.getWorld()
->getStore()
.get<ESM::GameSetting>()
.find(it.second)
->mValue.getString());
}
return summonMap;
}
const ESM::RefId& getSummonedCreature(int effectId)
{
const auto& summonMap = getSummonMap();
auto it = summonMap.find(effectId); auto it = summonMap.find(effectId);
if (it != summonMap.end()) if (it != summonMap.end())
{ {
static ESM::RefId val = ESM::RefId::stringRefId(MWBase::Environment::get() return it->second;
.getWorld()
->getStore()
.get<ESM::GameSetting>()
.find(it->second)
->mValue.getString());
return val;
} }
return ESM::RefId::sEmpty; return ESM::RefId::sEmpty;
} }

View file

@ -165,7 +165,7 @@ namespace MWScript
} }
const MWWorld::CellStore* cell = MWMechanics::getPlayer().getCell(); const MWWorld::CellStore* cell = MWMechanics::getPlayer().getCell();
std::string_view current = MWBase::Environment::get().getWorld()->getCellName(cell).getRefIdString(); std::string_view current = MWBase::Environment::get().getWorld()->getCellName(cell);
bool match = Misc::StringUtils::ciCompareLen(name, current, name.length()) == 0; bool match = Misc::StringUtils::ciCompareLen(name, current, name.length()) == 0;
runtime.push(match ? 1 : 0); runtime.push(match ? 1 : 0);

View file

@ -414,7 +414,7 @@ namespace MWScript
std::string_view InterpreterContext::getCurrentCellName() const std::string_view InterpreterContext::getCurrentCellName() const
{ {
return MWBase::Environment::get().getWorld()->getCellName().getRefIdString(); return MWBase::Environment::get().getWorld()->getCellName();
} }
void InterpreterContext::executeActivation(const MWWorld::Ptr& ptr, const MWWorld::Ptr& actor) void InterpreterContext::executeActivation(const MWWorld::Ptr& ptr, const MWWorld::Ptr& actor)

View file

@ -210,7 +210,7 @@ namespace MWScript
return iter->second; return iter->second;
} }
throw std::logic_error("script " + std::string{ name.getRefIdString() } + " does not exist"); throw std::logic_error("script " + name.getRefIdString() + " does not exist");
} }
GlobalScripts& ScriptManager::getGlobalScripts() GlobalScripts& ScriptManager::getGlobalScripts()

View file

@ -222,7 +222,7 @@ void MWState::StateManager::saveGame(const std::string& description, const Slot*
else else
profile.mPlayerClassId = classId; profile.mPlayerClassId = classId;
profile.mPlayerCell = world.getCellName(); profile.mPlayerCell = ESM::RefId::stringRefId(world.getCellName());
profile.mInGameTime = world.getEpochTimeStamp(); profile.mInGameTime = world.getEpochTimeStamp();
profile.mTimePlayed = mTimePlayed; profile.mTimePlayed = mTimePlayed;
profile.mDescription = description; profile.mDescription = description;

View file

@ -957,7 +957,7 @@ namespace MWWorld
Store<ESM::Skill>::Store() {} Store<ESM::Skill>::Store() {}
// Magic effect // Game Settings
//========================================================================= //=========================================================================
const ESM::GameSetting* Store<ESM::GameSetting>::search(const ESM::RefId& id) const const ESM::GameSetting* Store<ESM::GameSetting>::search(const ESM::RefId& id) const
@ -973,9 +973,10 @@ namespace MWWorld
const ESM::GameSetting* Store<ESM::GameSetting>::search(std::string_view id) const const ESM::GameSetting* Store<ESM::GameSetting>::search(std::string_view id) const
{ {
return TypedDynamicStore::search(ESM::RefId::stringRefId(id)); return TypedDynamicStore::search(ESM::RefId::stringRefId(id));
;
} }
// Magic effect
//=========================================================================
Store<ESM::MagicEffect>::Store() {} Store<ESM::MagicEffect>::Store() {}
// Attribute // Attribute

View file

@ -572,7 +572,7 @@ namespace MWWorld
if (cell) if (cell)
return cell; return cell;
// treat "Wilderness" like an empty string // treat "Wilderness" like an empty string
const ESM::RefId defaultName static const ESM::RefId defaultName
= ESM::RefId::stringRefId(mStore.get<ESM::GameSetting>().find("sDefaultCellname")->mValue.getString()); = ESM::RefId::stringRefId(mStore.get<ESM::GameSetting>().find("sDefaultCellname")->mValue.getString());
if (cellName == defaultName) if (cellName == defaultName)
{ {
@ -584,7 +584,7 @@ namespace MWWorld
// didn't work -> now check for regions // didn't work -> now check for regions
for (const ESM::Region& region : mStore.get<ESM::Region>()) for (const ESM::Region& region : mStore.get<ESM::Region>())
{ {
if (Misc::StringUtils::ciEqual(cellName.getRefIdString(), region.mName)) if (cellName == ESM::RefId::stringRefId(region.mName))
{ {
return mStore.get<ESM::Cell>().searchExtByRegion(region.mId); return mStore.get<ESM::Cell>().searchExtByRegion(region.mId);
} }
@ -656,24 +656,25 @@ namespace MWWorld
return mCurrentDate->getMonthName(month); return mCurrentDate->getMonthName(month);
} }
const ESM::RefId& World::getCellName(const MWWorld::CellStore* cell) const std::string_view World::getCellName(const MWWorld::CellStore* cell) const
{ {
if (!cell) if (!cell)
cell = mWorldScene->getCurrentCell(); cell = mWorldScene->getCurrentCell();
return getCellName(cell->getCell()); return getCellName(cell->getCell());
} }
const ESM::RefId World::getCellName(const ESM::Cell* cell) const std::string_view World::getCellName(const ESM::Cell* cell) const
{ {
if (cell) if (cell)
{ {
if (!cell->isExterior() || !cell->mName.empty()) if (!cell->isExterior() || !cell->mName.empty())
return cell->mName; return cell->mName.getRefIdString();
if (const ESM::Region* region = mStore.get<ESM::Region>().search(cell->mRegion)) if (const ESM::Region* region = mStore.get<ESM::Region>().search(cell->mRegion))
return ESM::RefId::stringRefId(region->mName); return region->mName;
} }
return ESM::RefId::stringRefId(mStore.get<ESM::GameSetting>().find("sDefaultCellname")->mValue.getString());
return mStore.get<ESM::GameSetting>().find("sDefaultCellname")->mValue.getString();
} }
void World::removeRefScript(MWWorld::RefData* ref) void World::removeRefScript(MWWorld::RefData* ref)
@ -1422,7 +1423,7 @@ namespace MWWorld
esmPos.pos[0] = traced.x(); esmPos.pos[0] = traced.x();
esmPos.pos[1] = traced.y(); esmPos.pos[1] = traced.y();
esmPos.pos[2] = traced.z(); esmPos.pos[2] = traced.z();
const ESM::RefId* cell; const ESM::RefId* cell = &ESM::RefId::sEmpty;
if (!actor.getCell()->isExterior()) if (!actor.getCell()->isExterior())
cell = &actor.getCell()->getCell()->mName; cell = &actor.getCell()->getCell()->mName;
MWWorld::ActionTeleport(*cell, esmPos, false).execute(actor); MWWorld::ActionTeleport(*cell, esmPos, false).execute(actor);
@ -3430,7 +3431,7 @@ namespace MWWorld
return; return;
} }
const ESM::RefId* cellName; const ESM::RefId* cellName = &ESM::RefId::sEmpty;
if (!closestMarker.mCell->isExterior()) if (!closestMarker.mCell->isExterior())
cellName = &closestMarker.mCell->getCell()->mName; cellName = &closestMarker.mCell->getCell()->mName;

View file

@ -267,12 +267,12 @@ namespace MWWorld
char getGlobalVariableType(std::string_view name) const override; char getGlobalVariableType(std::string_view name) const override;
///< Return ' ', if there is no global variable with this name. ///< Return ' ', if there is no global variable with this name.
const ESM::RefId& getCellName(const MWWorld::CellStore* cell = nullptr) const override; std::string_view getCellName(const MWWorld::CellStore* cell = nullptr) const override;
///< Return name of the cell. ///< Return name of the cell.
/// ///
/// \note If cell==0, the cell the player is currently in will be used instead to /// \note If cell==0, the cell the player is currently in will be used instead to
/// generate a name. /// generate a name.
const ESM::RefId getCellName(const ESM::Cell* cell) const override; std::string_view getCellName(const ESM::Cell* cell) const override;
void removeRefScript(MWWorld::RefData* ref) override; void removeRefScript(MWWorld::RefData* ref) override;
//< Remove the script attached to ref from mLocalScripts //< Remove the script attached to ref from mLocalScripts

View file

@ -65,7 +65,7 @@ MWWorld::CellStore* MWWorld::WorldModel::getCellStore(const ESM::Cell* cell)
auto result = mInteriors.find(cell->mName); auto result = mInteriors.find(cell->mName);
if (result == mInteriors.end()) if (result == mInteriors.end())
result = mInteriors.emplace(std::move(cell->mName), CellStore(cell, mStore, mReaders)).first; result = mInteriors.emplace(cell->mName, CellStore(cell, mStore, mReaders)).first;
return &result->second; return &result->second;
} }

View file

@ -18,7 +18,7 @@ namespace ESM
bool RefId::operator>(const RefId& rhs) const bool RefId::operator>(const RefId& rhs) const
{ {
return Misc::StringUtils::ciMore(mId, rhs.mId); return Misc::StringUtils::ciGreater(mId, rhs.mId);
} }
std::ostream& operator<<(std::ostream& os, const RefId& refId) std::ostream& operator<<(std::ostream& os, const RefId& refId)

View file

@ -20,14 +20,14 @@ namespace Misc::StringUtils
return std::lexicographical_compare(x.begin(), x.end(), y.begin(), y.end(), CiCharLess()); return std::lexicographical_compare(x.begin(), x.end(), y.begin(), y.end(), CiCharLess());
} }
struct CiCharMore struct CiCharGreater
{ {
bool operator()(char x, char y) const { return toLower(x) > toLower(y); } bool operator()(char x, char y) const { return toLower(x) > toLower(y); }
}; };
inline bool ciMore(std::string_view x, std::string_view y) inline bool ciGreater(std::string_view x, std::string_view y)
{ {
return std::lexicographical_compare(x.begin(), x.end(), y.begin(), y.end(), CiCharMore()); return std::lexicographical_compare(x.begin(), x.end(), y.begin(), y.end(), CiCharGreater());
} }
inline bool ciEqual(std::string_view x, std::string_view y) inline bool ciEqual(std::string_view x, std::string_view y)