Address feedback
This commit is contained in:
@@ -56,7 +56,7 @@ namespace MWLua
|
||||
~BoolScopeGuard() { mValue = false; }
|
||||
};
|
||||
|
||||
LocalScripts* asLocal(const LuaUtil::ScriptsContainerPtr& ptr)
|
||||
LocalScripts* asLocal(const LuaUtil::ScriptsContainerWeakPtr& ptr)
|
||||
{
|
||||
return static_cast<LocalScripts*>(*ptr);
|
||||
}
|
||||
@@ -227,20 +227,20 @@ namespace MWLua
|
||||
|
||||
mObjectLists.update();
|
||||
|
||||
for (const LuaUtil::ScriptsContainerPtr& ptr : mQueuedAutoStartedScripts)
|
||||
for (const LuaUtil::ScriptsContainerWeakPtr& ptr : mQueuedAutoStartedScripts)
|
||||
{
|
||||
if (LocalScripts* scripts = asLocal(ptr))
|
||||
scripts->addAutoStartedScripts();
|
||||
}
|
||||
mQueuedAutoStartedScripts.clear();
|
||||
|
||||
std::erase_if(mActiveLocalScripts, [](const LuaUtil::ScriptsContainerPtr& ptr) {
|
||||
std::erase_if(mActiveLocalScripts, [](const LuaUtil::ScriptsContainerWeakPtr& ptr) {
|
||||
LocalScripts* l = asLocal(ptr);
|
||||
return l == nullptr || l->getPtrOrEmpty().isEmpty() || l->getPtrOrEmpty().mRef->isDeleted();
|
||||
});
|
||||
|
||||
mGlobalScripts.statsNextFrame();
|
||||
for (const LuaUtil::ScriptsContainerPtr& ptr : mActiveLocalScripts)
|
||||
for (const LuaUtil::ScriptsContainerWeakPtr& ptr : mActiveLocalScripts)
|
||||
asLocal(ptr)->statsNextFrame();
|
||||
|
||||
mLuaEvents.finalizeEventBatch();
|
||||
@@ -250,7 +250,7 @@ namespace MWLua
|
||||
{
|
||||
mMenuScripts.processTimers(timeManager.getSimulationTime(), timeManager.getGameTime());
|
||||
mGlobalScripts.processTimers(timeManager.getSimulationTime(), timeManager.getGameTime());
|
||||
for (const LuaUtil::ScriptsContainerPtr& ptr : mActiveLocalScripts)
|
||||
for (const LuaUtil::ScriptsContainerWeakPtr& ptr : mActiveLocalScripts)
|
||||
asLocal(ptr)->processTimers(timeManager.getSimulationTime(), timeManager.getGameTime());
|
||||
}
|
||||
|
||||
@@ -268,7 +268,7 @@ namespace MWLua
|
||||
bool isPaused = timeManager.isPaused();
|
||||
|
||||
float frameDuration = MWBase::Environment::get().getFrameDuration();
|
||||
for (const LuaUtil::ScriptsContainerPtr& ptr : mActiveLocalScripts)
|
||||
for (const LuaUtil::ScriptsContainerWeakPtr& ptr : mActiveLocalScripts)
|
||||
asLocal(ptr)->update(isPaused ? 0 : frameDuration);
|
||||
mGlobalScripts.update(isPaused ? 0 : frameDuration);
|
||||
|
||||
@@ -844,7 +844,7 @@ namespace MWLua
|
||||
scripts->load(localData[id]);
|
||||
}
|
||||
|
||||
for (const LuaUtil::ScriptsContainerPtr& ptr : mActiveLocalScripts)
|
||||
for (const LuaUtil::ScriptsContainerWeakPtr& ptr : mActiveLocalScripts)
|
||||
{
|
||||
if (LocalScripts* scripts = asLocal(ptr))
|
||||
scripts->setActive(true);
|
||||
@@ -962,7 +962,7 @@ namespace MWLua
|
||||
|
||||
std::vector<Stats> activeStats;
|
||||
mGlobalScripts.collectStats(activeStats);
|
||||
for (const LuaUtil::ScriptsContainerPtr& ptr : mActiveLocalScripts)
|
||||
for (const LuaUtil::ScriptsContainerWeakPtr& ptr : mActiveLocalScripts)
|
||||
{
|
||||
if (LocalScripts* scripts = asLocal(ptr))
|
||||
scripts->collectStats(activeStats);
|
||||
|
||||
@@ -206,8 +206,8 @@ namespace MWLua
|
||||
LoadScripts mLoadScripts{ &mLua };
|
||||
MenuScripts mMenuScripts{ &mLua };
|
||||
GlobalScripts mGlobalScripts{ &mLua };
|
||||
std::set<LuaUtil::ScriptsContainerPtr, std::less<>> mActiveLocalScripts;
|
||||
std::vector<LuaUtil::ScriptsContainerPtr> mQueuedAutoStartedScripts;
|
||||
std::set<LuaUtil::ScriptsContainerWeakPtr, std::less<>> mActiveLocalScripts;
|
||||
std::vector<LuaUtil::ScriptsContainerWeakPtr> mQueuedAutoStartedScripts;
|
||||
ObjectLists mObjectLists;
|
||||
|
||||
MWWorld::Ptr mPlayer;
|
||||
|
||||
@@ -521,6 +521,10 @@ MWWorld::ContainerStoreIterator MWWorld::ContainerStore::addImp(const ConstPtr&
|
||||
|
||||
MWWorld::ContainerStoreIterator MWWorld::ContainerStore::addNewStack(const ConstPtr& ptr, int count)
|
||||
{
|
||||
// As with MWWorld::CellStore::insert, the caller is expected to deal with LiveCellRefBase's copy constructor
|
||||
// copying RefData and CellRef, thereby creating another instance with the same ESM::RefNum and MWLua::LocalScripts.
|
||||
// In practice this means the caller MUST ensure the object that gets copied into this inventory is removed from the
|
||||
// world.
|
||||
ContainerStoreIterator it = begin();
|
||||
|
||||
switch (getType(ptr))
|
||||
|
||||
@@ -831,9 +831,9 @@ namespace LuaUtil
|
||||
stats[id].mMemoryUsage += mem;
|
||||
}
|
||||
|
||||
ScriptsContainerPtr ScriptsContainer::getWeakPointer() const
|
||||
ScriptsContainerWeakPtr ScriptsContainer::getWeakPointer() const
|
||||
{
|
||||
return ScriptsContainerPtr(mThis);
|
||||
return ScriptsContainerWeakPtr(mThis);
|
||||
}
|
||||
|
||||
ScriptsContainer::Script::~Script()
|
||||
|
||||
@@ -16,17 +16,18 @@ namespace LuaUtil
|
||||
{
|
||||
class ScriptTracker;
|
||||
class ScriptsContainer;
|
||||
using ScriptsContainerWeakPtr = std::shared_ptr<ScriptsContainer*>;
|
||||
// Immutable ScriptsContainerWeakPtr
|
||||
class ScriptsContainerPtr
|
||||
using ScriptsContainerLifetime = std::shared_ptr<ScriptsContainer*>;
|
||||
|
||||
// Immutable ScriptsContainerLifetime
|
||||
class ScriptsContainerWeakPtr
|
||||
{
|
||||
ScriptsContainerWeakPtr mWeakPtr;
|
||||
ScriptsContainerLifetime mWeakPtr;
|
||||
|
||||
public:
|
||||
ScriptsContainerPtr(const ScriptsContainerPtr&) = default;
|
||||
ScriptsContainerPtr(ScriptsContainerPtr&&) = default;
|
||||
ScriptsContainerWeakPtr(const ScriptsContainerWeakPtr&) = default;
|
||||
ScriptsContainerWeakPtr(ScriptsContainerWeakPtr&&) = default;
|
||||
|
||||
explicit ScriptsContainerPtr(ScriptsContainerWeakPtr ptr)
|
||||
explicit ScriptsContainerWeakPtr(ScriptsContainerLifetime ptr)
|
||||
: mWeakPtr(std::move(ptr))
|
||||
{
|
||||
}
|
||||
@@ -34,12 +35,12 @@ namespace LuaUtil
|
||||
ScriptsContainer* operator*() const noexcept { return *mWeakPtr.get(); }
|
||||
};
|
||||
|
||||
inline auto operator<=>(const ScriptsContainerPtr& lhs, const ScriptsContainerPtr& rhs)
|
||||
inline auto operator<=>(const ScriptsContainerWeakPtr& lhs, const ScriptsContainerWeakPtr& rhs)
|
||||
{
|
||||
return *lhs <=> *rhs;
|
||||
}
|
||||
|
||||
inline auto operator<=>(const ScriptsContainerPtr& lhs, ScriptsContainer* rhs)
|
||||
inline auto operator<=>(const ScriptsContainerWeakPtr& lhs, ScriptsContainer* rhs)
|
||||
{
|
||||
return *lhs <=> rhs;
|
||||
}
|
||||
@@ -192,7 +193,7 @@ namespace LuaUtil
|
||||
|
||||
virtual bool isActive() const { return false; }
|
||||
|
||||
ScriptsContainerPtr getWeakPointer() const;
|
||||
ScriptsContainerWeakPtr getWeakPointer() const;
|
||||
|
||||
protected:
|
||||
// Call a function on an interface.
|
||||
@@ -348,7 +349,7 @@ namespace LuaUtil
|
||||
int64_t mTemporaryCallbackCounter = 0;
|
||||
|
||||
std::map<int, int64_t> mRemovedScriptsMemoryUsage;
|
||||
ScriptsContainerWeakPtr mThis; // used by LuaState to track ownership of memory allocations
|
||||
ScriptsContainerLifetime mThis; // used by LuaState to track ownership of memory allocations
|
||||
|
||||
ScriptTracker* mTracker;
|
||||
bool mRequiredLoading = false;
|
||||
|
||||
@@ -12,7 +12,7 @@ namespace LuaUtil
|
||||
class ScriptTracker
|
||||
{
|
||||
using Frame = unsigned int;
|
||||
using TrackedScriptContainer = std::pair<ScriptsContainerPtr, Frame>;
|
||||
using TrackedScriptContainer = std::pair<ScriptsContainerWeakPtr, Frame>;
|
||||
std::queue<TrackedScriptContainer> mLoadedScripts;
|
||||
Frame mFrame = 0;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user