1
0
Fork 0
mirror of https://github.com/OpenMW/openmw.git synced 2025-01-31 05:45:34 +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.getRefData().setLuaScripts(nullptr);
MWBase::Environment::get().getWorldModel()->getPtrRegistry().insert(newPtr);
MWBase::Environment::get().getWorldModel()->registerPtr(newPtr);
return newPtr;
}
@ -229,7 +229,7 @@ namespace MWClass
newPtr = MWWorld::Ptr(cell.insert(ref), &cell);
}
ptr.getRefData().setLuaScripts(nullptr);
MWBase::Environment::get().getWorldModel()->getPtrRegistry().insert(newPtr);
MWBase::Environment::get().getWorldModel()->registerPtr(newPtr);
return newPtr;
}

View file

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

View file

@ -74,7 +74,7 @@ namespace MWLua
private:
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)
Log(Debug::Verbose) << "Can not find object" << id.toString() << " when calling engine hanglers";
return res;

View file

@ -40,7 +40,7 @@ namespace MWLua
mGlobalEventBatch.clear();
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();
if (scripts)
scripts->receiveEvent(e.mEventName, e.mEventData);

View file

@ -136,7 +136,7 @@ namespace MWLua
if (!mPlayer.isInCell() || !newPlayerPtr.isInCell() || mPlayer.getCell() != newPlayerPtr.getCell())
{
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();
@ -299,7 +299,7 @@ namespace MWLua
if (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) });
}
}
@ -401,7 +401,7 @@ namespace MWLua
return;
}
MWBase::Environment::get().getWorldModel()->getPtrRegistry().insert(ptr);
MWBase::Environment::get().getWorldModel()->registerPtr(ptr);
LocalScripts* scripts = createLocalScripts(ptr);
scripts->setSerializer(mLocalSerializer.get());
@ -409,7 +409,7 @@ namespace MWLua
scripts->load(data);
// 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()
@ -430,7 +430,7 @@ namespace MWLua
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
LocalScripts* scripts = ptr.getRefData().getLuaScripts();
if (scripts == nullptr)

View file

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

View file

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

View file

@ -58,7 +58,7 @@ namespace MWLua
void WorldView::objectAddedToScene(const MWWorld::Ptr& ptr)
{
MWBase::Environment::get().getWorldModel()->getPtrRegistry().insert(ptr);
MWBase::Environment::get().getWorldModel()->registerPtr(ptr);
ObjectGroup* group = chooseGroup(ptr);
if (group)
addToGroup(*group, ptr);
@ -81,13 +81,13 @@ namespace MWLua
void WorldView::load(ESM::ESMReader& esm)
{
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
{
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()

View file

@ -63,7 +63,7 @@ namespace
if (pair.second.empty())
return MWWorld::Ptr();
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);
}
};

View file

@ -272,7 +272,7 @@ namespace
iter->mData.enable();
MWBase::Environment::get().getWorld()->disable(ptr);
}
MWBase::Environment::get().getWorldModel()->getPtrRegistry().insert(ptr);
MWBase::Environment::get().getWorldModel()->registerPtr(ptr);
return;
}
@ -287,7 +287,7 @@ namespace
collection.mList.push_back(ref);
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
@ -440,8 +440,7 @@ namespace MWWorld
throw std::runtime_error(
"moveTo: can't move object from a non-loaded cell (how did you get this object anyway?)");
MWBase::Environment::get().getWorldModel()->getPtrRegistry().insert(
MWWorld::Ptr(object.getBase(), cellToMoveTo));
MWBase::Environment::get().getWorldModel()->registerPtr(MWWorld::Ptr(object.getBase(), cellToMoveTo));
MovedRefTracker::iterator found = mMovedHere.find(object.getBase());
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.
Ptr movedRef = MWBase::Environment::get().getWorldModel()->getPtrRegistry().getOrDefault(refnum);
Ptr movedRef = MWBase::Environment::get().getWorldModel()->getPtr(refnum);
if (movedRef.isEmpty())
{
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.getRefData().setCount(count);
newPtr.getRefData().setLuaScripts(nullptr);
MWBase::Environment::get().getWorldModel()->getPtrRegistry().insert(newPtr);
MWBase::Environment::get().getWorldModel()->registerPtr(newPtr);
if (hasInventoryStore(newPtr))
getInventoryStore(newPtr).setActor(newPtr);
return newPtr;
@ -385,7 +385,7 @@ namespace MWWorld
{
Ptr newPtr = copyToCellImpl(ptr, cell);
ptr.getRefData().setLuaScripts(nullptr);
MWBase::Environment::get().getWorldModel()->getPtrRegistry().insert(newPtr);
MWBase::Environment::get().getWorldModel()->registerPtr(newPtr);
if (hasInventoryStore(newPtr))
getInventoryStore(newPtr).setActor(newPtr);
return newPtr;

View file

@ -308,7 +308,7 @@ MWWorld::ContainerStoreIterator MWWorld::ContainerStore::add(
// The copy of the original item we just made
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
item.getRefData().setBaseNode(

View file

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

View file

@ -20,7 +20,7 @@ namespace MWWorld
auto end() const { return mIndex.cend(); }
Ptr getOrDefault(ESM::RefNum refNum) const
Ptr getOrEmpty(ESM::RefNum refNum) const
{
const auto it = mIndex.find(refNum);
if (it != mIndex.end())
@ -54,6 +54,22 @@ namespace MWWorld
std::unordered_map<ESM::RefNum, Ptr> mIndex;
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

View file

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

View file

@ -54,7 +54,19 @@ namespace MWWorld
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>
void forEachLoadedCellStore(Fn&& fn)