1
0
Fork 0
mirror of https://github.com/OpenMW/openmw.git synced 2025-01-31 06:15:44 +00:00

Merge branch 'hide_ptr_registry' into 'master'

Make WorldModel a front-end for PtrRegistry

See merge request OpenMW/openmw!3087
This commit is contained in:
psi29a 2023-06-01 09:32:57 +00:00
commit 8e3e351015
16 changed files with 64 additions and 40 deletions

View file

@ -208,7 +208,7 @@ namespace MWClass
} }
newPtr.getCellRef().unsetRefNum(); newPtr.getCellRef().unsetRefNum();
newPtr.getRefData().setLuaScripts(nullptr); newPtr.getRefData().setLuaScripts(nullptr);
MWBase::Environment::get().getWorldModel()->getPtrRegistry().insert(newPtr); MWBase::Environment::get().getWorldModel()->registerPtr(newPtr);
return newPtr; return newPtr;
} }
@ -229,7 +229,7 @@ namespace MWClass
newPtr = MWWorld::Ptr(cell.insert(ref), &cell); newPtr = MWWorld::Ptr(cell.insert(ref), &cell);
} }
ptr.getRefData().setLuaScripts(nullptr); ptr.getRefData().setLuaScripts(nullptr);
MWBase::Environment::get().getWorldModel()->getPtrRegistry().insert(newPtr); MWBase::Environment::get().getWorldModel()->registerPtr(newPtr);
return newPtr; return newPtr;
} }

View file

@ -83,7 +83,7 @@ namespace MWLua
auto visitor = [&](const MWWorld::Ptr& ptr) { auto visitor = [&](const MWWorld::Ptr& ptr) {
if (ptr.getRefData().isDeleted()) if (ptr.getRefData().isDeleted())
return true; return true;
MWBase::Environment::get().getWorldModel()->getPtrRegistry().insert(ptr); MWBase::Environment::get().getWorldModel()->registerPtr(ptr);
if (getLiveCellRefType(ptr.mRef) == ptr.getType()) if (getLiveCellRefType(ptr.mRef) == ptr.getType())
res->push_back(getId(ptr)); res->push_back(getId(ptr));
return true; return true;

View file

@ -74,7 +74,7 @@ namespace MWLua
private: private:
MWWorld::Ptr getPtr(const ESM::RefNum& id) const MWWorld::Ptr getPtr(const ESM::RefNum& id) const
{ {
MWWorld::Ptr res = mWorldModel->getPtrRegistry().getOrDefault(id); MWWorld::Ptr res = mWorldModel->getPtr(id);
if (res.isEmpty() && Settings::lua().mLuaDebug) if (res.isEmpty() && Settings::lua().mLuaDebug)
Log(Debug::Verbose) << "Can not find object" << id.toString() << " when calling engine hanglers"; Log(Debug::Verbose) << "Can not find object" << id.toString() << " when calling engine hanglers";
return res; return res;

View file

@ -40,7 +40,7 @@ namespace MWLua
mGlobalEventBatch.clear(); mGlobalEventBatch.clear();
for (const Local& e : mLocalEventBatch) for (const Local& e : mLocalEventBatch)
{ {
MWWorld::Ptr ptr = MWBase::Environment::get().getWorldModel()->getPtrRegistry().getOrDefault(e.mDest); MWWorld::Ptr ptr = MWBase::Environment::get().getWorldModel()->getPtr(e.mDest);
LocalScripts* scripts = ptr.isEmpty() ? nullptr : ptr.getRefData().getLuaScripts(); LocalScripts* scripts = ptr.isEmpty() ? nullptr : ptr.getRefData().getLuaScripts();
if (scripts) if (scripts)
scripts->receiveEvent(e.mEventName, e.mEventData); scripts->receiveEvent(e.mEventName, e.mEventData);

View file

@ -136,7 +136,7 @@ namespace MWLua
if (!mPlayer.isInCell() || !newPlayerPtr.isInCell() || mPlayer.getCell() != newPlayerPtr.getCell()) if (!mPlayer.isInCell() || !newPlayerPtr.isInCell() || mPlayer.getCell() != newPlayerPtr.getCell())
{ {
mPlayer = newPlayerPtr; // player was moved to another cell, update ptr in registry mPlayer = newPlayerPtr; // player was moved to another cell, update ptr in registry
MWBase::Environment::get().getWorldModel()->getPtrRegistry().insert(mPlayer); MWBase::Environment::get().getWorldModel()->registerPtr(mPlayer);
} }
mWorldView.update(); mWorldView.update();
@ -299,7 +299,7 @@ namespace MWLua
if (localScripts) if (localScripts)
{ {
mActiveLocalScripts.erase(localScripts); mActiveLocalScripts.erase(localScripts);
if (!MWBase::Environment::get().getWorldModel()->getPtrRegistry().getOrDefault(getId(ptr)).isEmpty()) if (!MWBase::Environment::get().getWorldModel()->getPtr(getId(ptr)).isEmpty())
mEngineEvents.addToQueue(EngineEvents::OnInactive{ getId(ptr) }); mEngineEvents.addToQueue(EngineEvents::OnInactive{ getId(ptr) });
} }
} }
@ -401,7 +401,7 @@ namespace MWLua
return; return;
} }
MWBase::Environment::get().getWorldModel()->getPtrRegistry().insert(ptr); MWBase::Environment::get().getWorldModel()->registerPtr(ptr);
LocalScripts* scripts = createLocalScripts(ptr); LocalScripts* scripts = createLocalScripts(ptr);
scripts->setSerializer(mLocalSerializer.get()); scripts->setSerializer(mLocalSerializer.get());
@ -409,7 +409,7 @@ namespace MWLua
scripts->load(data); scripts->load(data);
// LiveCellRef is usually copied after loading, so this Ptr will become invalid and should be deregistered. // LiveCellRef is usually copied after loading, so this Ptr will become invalid and should be deregistered.
MWBase::Environment::get().getWorldModel()->getPtrRegistry().remove(ptr); MWBase::Environment::get().getWorldModel()->deregisterPtr(ptr);
} }
void LuaManager::reloadAllScripts() void LuaManager::reloadAllScripts()
@ -430,7 +430,7 @@ namespace MWLua
mGlobalScripts.load(data); mGlobalScripts.load(data);
} }
for (const auto& [id, ptr] : MWBase::Environment::get().getWorldModel()->getPtrRegistry()) for (const auto& [id, ptr] : MWBase::Environment::get().getWorldModel()->getPtrRegistryView())
{ // Reload local scripts { // Reload local scripts
LocalScripts* scripts = ptr.getRefData().getLuaScripts(); LocalScripts* scripts = ptr.getRefData().getLuaScripts();
if (scripts == nullptr) if (scripts == nullptr)

View file

@ -497,7 +497,7 @@ namespace MWLua
while (it.getType() != -1) while (it.getType() != -1)
{ {
const MWWorld::Ptr& item = *(it++); const MWWorld::Ptr& item = *(it++);
MWBase::Environment::get().getWorldModel()->getPtrRegistry().insert(item); MWBase::Environment::get().getWorldModel()->registerPtr(item);
list->push_back(getId(item)); list->push_back(getId(item));
} }
return ObjectList<ObjectT>{ list }; return ObjectList<ObjectT>{ list };
@ -516,7 +516,7 @@ namespace MWLua
{ {
if (item.getCellRef().getRefId() == itemId) if (item.getCellRef().getRefId() == itemId)
{ {
MWBase::Environment::get().getWorldModel()->getPtrRegistry().insert(item); MWBase::Environment::get().getWorldModel()->registerPtr(item);
return ObjectT(getId(item)); return ObjectT(getId(item));
} }
} }
@ -531,7 +531,7 @@ namespace MWLua
{ {
if (item.getCellRef().getRefId() == itemId) if (item.getCellRef().getRefId() == itemId)
{ {
MWBase::Environment::get().getWorldModel()->getPtrRegistry().insert(item); MWBase::Environment::get().getWorldModel()->registerPtr(item);
list->push_back(getId(item)); list->push_back(getId(item));
} }
} }

View file

@ -32,8 +32,7 @@ namespace MWLua
MWWorld::Ptr itemPtr; MWWorld::Ptr itemPtr;
if (std::holds_alternative<ObjectId>(item)) if (std::holds_alternative<ObjectId>(item))
{ {
itemPtr = MWBase::Environment::get().getWorldModel()->getPtrRegistry().getOrDefault( itemPtr = MWBase::Environment::get().getWorldModel()->getPtr(std::get<ObjectId>(item));
std::get<ObjectId>(item));
if (old_it != store.end() && *old_it == itemPtr) if (old_it != store.end() && *old_it == itemPtr)
return true; // already equipped return true; // already equipped
if (itemPtr.isEmpty() || itemPtr.getRefData().getCount() == 0 if (itemPtr.isEmpty() || itemPtr.getRefData().getCount() == 0
@ -218,7 +217,7 @@ namespace MWLua
auto it = store.getSlot(slot); auto it = store.getSlot(slot);
if (it == store.end()) if (it == store.end())
continue; continue;
MWBase::Environment::get().getWorldModel()->getPtrRegistry().insert(*it); MWBase::Environment::get().getWorldModel()->registerPtr(*it);
if (dynamic_cast<const GObject*>(&o)) if (dynamic_cast<const GObject*>(&o))
equipment[slot] = sol::make_object(lua, GObject(*it)); equipment[slot] = sol::make_object(lua, GObject(*it));
else else
@ -234,7 +233,7 @@ namespace MWLua
auto it = store.getSlot(slot); auto it = store.getSlot(slot);
if (it == store.end()) if (it == store.end())
return sol::nil; return sol::nil;
MWBase::Environment::get().getWorldModel()->getPtrRegistry().insert(*it); MWBase::Environment::get().getWorldModel()->registerPtr(*it);
if (dynamic_cast<const GObject*>(&o)) if (dynamic_cast<const GObject*>(&o))
return sol::make_object(lua, GObject(*it)); return sol::make_object(lua, GObject(*it));
else else

View file

@ -58,7 +58,7 @@ namespace MWLua
void WorldView::objectAddedToScene(const MWWorld::Ptr& ptr) void WorldView::objectAddedToScene(const MWWorld::Ptr& ptr)
{ {
MWBase::Environment::get().getWorldModel()->getPtrRegistry().insert(ptr); MWBase::Environment::get().getWorldModel()->registerPtr(ptr);
ObjectGroup* group = chooseGroup(ptr); ObjectGroup* group = chooseGroup(ptr);
if (group) if (group)
addToGroup(*group, ptr); addToGroup(*group, ptr);
@ -81,13 +81,13 @@ namespace MWLua
void WorldView::load(ESM::ESMReader& esm) void WorldView::load(ESM::ESMReader& esm)
{ {
esm.getHNT(mSimulationTime, "LUAW"); esm.getHNT(mSimulationTime, "LUAW");
MWBase::Environment::get().getWorldModel()->getPtrRegistry().setLastGenerated(esm.getFormId(true)); MWBase::Environment::get().getWorldModel()->setLastGeneratedRefNum(esm.getFormId(true));
} }
void WorldView::save(ESM::ESMWriter& esm) const void WorldView::save(ESM::ESMWriter& esm) const
{ {
esm.writeHNT("LUAW", mSimulationTime); esm.writeHNT("LUAW", mSimulationTime);
esm.writeFormId(MWBase::Environment::get().getWorldModel()->getPtrRegistry().getLastGenerated(), true); esm.writeFormId(MWBase::Environment::get().getWorldModel()->getLastGeneratedRefNum(), true);
} }
void WorldView::ObjectGroup::updateList() void WorldView::ObjectGroup::updateList()

View file

@ -63,7 +63,7 @@ namespace
if (pair.second.empty()) if (pair.second.empty())
return MWWorld::Ptr(); return MWWorld::Ptr();
else if (pair.first.hasContentFile()) else if (pair.first.hasContentFile())
return MWBase::Environment::get().getWorldModel()->getPtrRegistry().getOrDefault(pair.first); return MWBase::Environment::get().getWorldModel()->getPtr(pair.first);
return MWBase::Environment::get().getWorld()->searchPtr(pair.second, false); return MWBase::Environment::get().getWorld()->searchPtr(pair.second, false);
} }
}; };

View file

@ -272,7 +272,7 @@ namespace
iter->mData.enable(); iter->mData.enable();
MWBase::Environment::get().getWorld()->disable(ptr); MWBase::Environment::get().getWorld()->disable(ptr);
} }
MWBase::Environment::get().getWorldModel()->getPtrRegistry().insert(ptr); MWBase::Environment::get().getWorldModel()->registerPtr(ptr);
return; return;
} }
@ -287,7 +287,7 @@ namespace
collection.mList.push_back(ref); collection.mList.push_back(ref);
MWWorld::LiveCellRefBase* base = &collection.mList.back(); MWWorld::LiveCellRefBase* base = &collection.mList.back();
MWBase::Environment::get().getWorldModel()->getPtrRegistry().insert(MWWorld::Ptr(base, cellstore)); MWBase::Environment::get().getWorldModel()->registerPtr(MWWorld::Ptr(base, cellstore));
} }
// this function allows us to link a CellRefList<T> to the associated recNameInt, and apply a function // this function allows us to link a CellRefList<T> to the associated recNameInt, and apply a function
@ -440,8 +440,7 @@ namespace MWWorld
throw std::runtime_error( throw std::runtime_error(
"moveTo: can't move object from a non-loaded cell (how did you get this object anyway?)"); "moveTo: can't move object from a non-loaded cell (how did you get this object anyway?)");
MWBase::Environment::get().getWorldModel()->getPtrRegistry().insert( MWBase::Environment::get().getWorldModel()->registerPtr(MWWorld::Ptr(object.getBase(), cellToMoveTo));
MWWorld::Ptr(object.getBase(), cellToMoveTo));
MovedRefTracker::iterator found = mMovedHere.find(object.getBase()); MovedRefTracker::iterator found = mMovedHere.find(object.getBase());
if (found != mMovedHere.end()) if (found != mMovedHere.end())
@ -1036,7 +1035,7 @@ namespace MWWorld
} }
// Search for the reference. It might no longer exist if its content file was removed. // Search for the reference. It might no longer exist if its content file was removed.
Ptr movedRef = MWBase::Environment::get().getWorldModel()->getPtrRegistry().getOrDefault(refnum); Ptr movedRef = MWBase::Environment::get().getWorldModel()->getPtr(refnum);
if (movedRef.isEmpty()) if (movedRef.isEmpty())
{ {
Log(Debug::Warning) << "Warning: Dropping moved ref tag for " << refnum.mIndex Log(Debug::Warning) << "Warning: Dropping moved ref tag for " << refnum.mIndex

View file

@ -375,7 +375,7 @@ namespace MWWorld
newPtr.getCellRef().unsetRefNum(); // This RefNum is only valid within the original cell of the reference newPtr.getCellRef().unsetRefNum(); // This RefNum is only valid within the original cell of the reference
newPtr.getRefData().setCount(count); newPtr.getRefData().setCount(count);
newPtr.getRefData().setLuaScripts(nullptr); newPtr.getRefData().setLuaScripts(nullptr);
MWBase::Environment::get().getWorldModel()->getPtrRegistry().insert(newPtr); MWBase::Environment::get().getWorldModel()->registerPtr(newPtr);
if (hasInventoryStore(newPtr)) if (hasInventoryStore(newPtr))
getInventoryStore(newPtr).setActor(newPtr); getInventoryStore(newPtr).setActor(newPtr);
return newPtr; return newPtr;
@ -385,7 +385,7 @@ namespace MWWorld
{ {
Ptr newPtr = copyToCellImpl(ptr, cell); Ptr newPtr = copyToCellImpl(ptr, cell);
ptr.getRefData().setLuaScripts(nullptr); ptr.getRefData().setLuaScripts(nullptr);
MWBase::Environment::get().getWorldModel()->getPtrRegistry().insert(newPtr); MWBase::Environment::get().getWorldModel()->registerPtr(newPtr);
if (hasInventoryStore(newPtr)) if (hasInventoryStore(newPtr))
getInventoryStore(newPtr).setActor(newPtr); getInventoryStore(newPtr).setActor(newPtr);
return newPtr; return newPtr;

View file

@ -308,7 +308,7 @@ MWWorld::ContainerStoreIterator MWWorld::ContainerStore::add(
// The copy of the original item we just made // The copy of the original item we just made
MWWorld::Ptr item = *it; MWWorld::Ptr item = *it;
MWBase::Environment::get().getWorldModel()->getPtrRegistry().insert(item); MWBase::Environment::get().getWorldModel()->registerPtr(item);
// we may have copied an item from the world, so reset a few things first // we may have copied an item from the world, so reset a few things first
item.getRefData().setBaseNode( item.getRefData().setBaseNode(

View file

@ -24,7 +24,7 @@ namespace MWWorld
SafePtr::SafePtr(const Ptr& ptr) SafePtr::SafePtr(const Ptr& ptr)
: mId(ptr.getCellRef().getRefNum()) : mId(ptr.getCellRef().getRefNum())
, mPtr(ptr) , mPtr(ptr)
, mLastUpdate(MWBase::Environment::get().getWorldModel()->getPtrRegistry().getRevision()) , mLastUpdate(MWBase::Environment::get().getWorldModel()->getPtrRegistryRevision())
{ {
} }
@ -39,11 +39,11 @@ namespace MWWorld
void SafePtr::update() const void SafePtr::update() const
{ {
const PtrRegistry& registry = MWBase::Environment::get().getWorldModel()->getPtrRegistry(); const WorldModel& worldModel = *MWBase::Environment::get().getWorldModel();
if (mLastUpdate != registry.getRevision()) if (mLastUpdate != worldModel.getPtrRegistryRevision())
{ {
mPtr = registry.getOrDefault(mId); mPtr = worldModel.getPtr(mId);
mLastUpdate = registry.getRevision(); mLastUpdate = worldModel.getPtrRegistryRevision();
} }
} }
} }

View file

@ -20,7 +20,7 @@ namespace MWWorld
auto end() const { return mIndex.cend(); } auto end() const { return mIndex.cend(); }
Ptr getOrDefault(ESM::RefNum refNum) const Ptr getOrEmpty(ESM::RefNum refNum) const
{ {
const auto it = mIndex.find(refNum); const auto it = mIndex.find(refNum);
if (it != mIndex.end()) if (it != mIndex.end())
@ -54,6 +54,22 @@ namespace MWWorld
std::unordered_map<ESM::RefNum, Ptr> mIndex; std::unordered_map<ESM::RefNum, Ptr> mIndex;
ESM::RefNum mLastGenerated; ESM::RefNum mLastGenerated;
}; };
class PtrRegistryView
{
public:
explicit PtrRegistryView(const PtrRegistry& ref)
: mPtr(&ref)
{
}
auto begin() const { return mPtr->begin(); }
auto end() const { return mPtr->end(); }
private:
const PtrRegistry* mPtr;
};
} }
#endif #endif

View file

@ -1853,8 +1853,7 @@ namespace MWWorld
facedObject = rayToObject.mHitObject; facedObject = rayToObject.mHitObject;
if (facedObject.isEmpty() && rayToObject.mHitRefnum.isSet()) if (facedObject.isEmpty() && rayToObject.mHitRefnum.isSet())
facedObject facedObject = MWBase::Environment::get().getWorldModel()->getPtr(rayToObject.mHitRefnum);
= MWBase::Environment::get().getWorldModel()->getPtrRegistry().getOrDefault(rayToObject.mHitRefnum);
if (rayToObject.mHit) if (rayToObject.mHit)
mDistanceToFacedObject = (rayToObject.mRatio * maxDistance) - camDist; mDistanceToFacedObject = (rayToObject.mRatio * maxDistance) - camDist;
else else
@ -1871,8 +1870,7 @@ namespace MWWorld
res.mHitNormal = rayRes.mHitNormalWorld; res.mHitNormal = rayRes.mHitNormalWorld;
res.mHitObject = rayRes.mHitObject; res.mHitObject = rayRes.mHitObject;
if (res.mHitObject.isEmpty() && rayRes.mHitRefnum.isSet()) if (res.mHitObject.isEmpty() && rayRes.mHitRefnum.isSet())
res.mHitObject res.mHitObject = MWBase::Environment::get().getWorldModel()->getPtr(rayRes.mHitRefnum);
= MWBase::Environment::get().getWorldModel()->getPtrRegistry().getOrDefault(rayRes.mHitRefnum);
return res.mHit; return res.mHit;
} }

View file

@ -54,7 +54,19 @@ namespace MWWorld
Ptr getPtr(const ESM::RefId& name); Ptr getPtr(const ESM::RefId& name);
PtrRegistry& getPtrRegistry() { return mPtrRegistry; } Ptr getPtr(ESM::RefNum refNum) const { return mPtrRegistry.getOrEmpty(refNum); }
PtrRegistryView getPtrRegistryView() const { return PtrRegistryView(mPtrRegistry); }
ESM::RefNum getLastGeneratedRefNum() const { return mPtrRegistry.getLastGenerated(); }
void setLastGeneratedRefNum(ESM::RefNum v) { mPtrRegistry.setLastGenerated(v); }
std::size_t getPtrRegistryRevision() const { return mPtrRegistry.getRevision(); }
void registerPtr(const Ptr& ptr) { mPtrRegistry.insert(ptr); }
void deregisterPtr(const Ptr& ptr) { mPtrRegistry.remove(ptr); }
template <typename Fn> template <typename Fn>
void forEachLoadedCellStore(Fn&& fn) void forEachLoadedCellStore(Fn&& fn)