Make ownership explicit in Environment

C++20
ζeh Matt 3 years ago
parent 251327fb9c
commit a383d9dfdf
No known key found for this signature in database
GPG Key ID: 18CE582C71A225B0

@ -694,18 +694,18 @@ void OMW::Engine::setWindowIcon()
void OMW::Engine::prepareEngine (Settings::Manager & settings)
{
mEnvironment.setStateManager (
new MWState::StateManager (mCfgMgr.getUserDataPath() / "saves", mContentFiles));
std::make_unique<MWState::StateManager> (mCfgMgr.getUserDataPath() / "saves", mContentFiles));
createWindow(settings);
osg::ref_ptr<osg::Group> rootNode (new osg::Group);
mViewer->setSceneData(rootNode);
mVFS.reset(new VFS::Manager(mFSStrict));
mVFS = std::make_unique<VFS::Manager>(mFSStrict);
VFS::registerArchives(mVFS.get(), mFileCollections, mArchives, true);
mResourceSystem.reset(new Resource::ResourceSystem(mVFS.get()));
mResourceSystem = std::make_unique<Resource::ResourceSystem>(mVFS.get());
mResourceSystem->getSceneManager()->setUnRefImageDataAfterApply(false); // keep to Off for now to allow better state sharing
mResourceSystem->getSceneManager()->setFilterSettings(
Settings::Manager::getString("texture mag filter", "General"),
@ -734,8 +734,9 @@ void OMW::Engine::prepareEngine (Settings::Manager & settings)
mViewer->addEventHandler(mScreenCaptureHandler);
mLuaManager = new MWLua::LuaManager(mVFS.get(), (mResDir / "lua_libs").string());
mEnvironment.setLuaManager(mLuaManager);
auto luaMgr = std::make_unique<MWLua::LuaManager>(mVFS.get(), (mResDir / "lua_libs").string());
mLuaManager = luaMgr.get();
mEnvironment.setLuaManager(std::move(luaMgr));
// Create input and UI first to set up a bootstrapping environment for
// showing a loading screen and keeping the window responsive while doing so
@ -806,33 +807,35 @@ void OMW::Engine::prepareEngine (Settings::Manager & settings)
guiRoot->setName("GUI Root");
guiRoot->setNodeMask(MWRender::Mask_GUI);
rootNode->addChild(guiRoot);
MWGui::WindowManager* window = new MWGui::WindowManager(mWindow, mViewer, guiRoot, mResourceSystem.get(), mWorkQueue.get(),
auto windowMgr = std::make_unique<MWGui::WindowManager>(mWindow, mViewer, guiRoot, mResourceSystem.get(), mWorkQueue.get(),
mCfgMgr.getLogPath().string() + std::string("/"), myguiResources,
mScriptConsoleMode, mTranslationDataStorage, mEncoding, mExportFonts,
Version::getOpenmwVersionDescription(mResDir.string()), mCfgMgr.getUserConfigPath().string(), shadersSupported);
mEnvironment.setWindowManager (window);
auto* windowMgrInternal = windowMgr.get();
mEnvironment.setWindowManager (std::move(windowMgr));
MWInput::InputManager* input = new MWInput::InputManager (mWindow, mViewer, mScreenCaptureHandler, mScreenCaptureOperation, keybinderUser, keybinderUserExists, userGameControllerdb, gameControllerdb, mGrab);
mEnvironment.setInputManager (input);
auto inputMgr = std::make_unique<MWInput::InputManager>(mWindow, mViewer, mScreenCaptureHandler, mScreenCaptureOperation, keybinderUser, keybinderUserExists, userGameControllerdb, gameControllerdb, mGrab);
mEnvironment.setInputManager (std::move(inputMgr));
// Create sound system
mEnvironment.setSoundManager (new MWSound::SoundManager(mVFS.get(), mUseSound));
mEnvironment.setSoundManager (std::make_unique<MWSound::SoundManager>(mVFS.get(), mUseSound));
if (!mSkipMenu)
{
const std::string& logo = Fallback::Map::getString("Movies_Company_Logo");
if (!logo.empty())
window->playVideo(logo, true);
mEnvironment.getWindowManager()->playVideo(logo, true);
}
// Create the world
mEnvironment.setWorld( new MWWorld::World (mViewer, rootNode, mResourceSystem.get(), mWorkQueue.get(),
mEnvironment.setWorld(std::make_unique<MWWorld::World>(mViewer, rootNode, mResourceSystem.get(), mWorkQueue.get(),
mFileCollections, mContentFiles, mGroundcoverFiles, mEncoder, mActivationDistanceOverride, mCellName,
mStartupScript, mResDir.string(), mCfgMgr.getUserDataPath().string()));
mEnvironment.getWorld()->setupPlayer();
window->setStore(mEnvironment.getWorld()->getStore());
window->initUI();
windowMgrInternal->setStore(mEnvironment.getWorld()->getStore());
windowMgrInternal->initUI();
//Load translation data
mTranslationDataStorage.setEncoder(mEncoder);
@ -845,16 +848,15 @@ void OMW::Engine::prepareEngine (Settings::Manager & settings)
mScriptContext = new MWScript::CompilerContext (MWScript::CompilerContext::Type_Full);
mScriptContext->setExtensions (&mExtensions);
mEnvironment.setScriptManager (new MWScript::ScriptManager (mEnvironment.getWorld()->getStore(), *mScriptContext, mWarningsMode,
mEnvironment.setScriptManager (std::make_unique<MWScript::ScriptManager>(mEnvironment.getWorld()->getStore(), *mScriptContext, mWarningsMode,
mScriptBlacklistUse ? mScriptBlacklist : std::vector<std::string>()));
// Create game mechanics system
MWMechanics::MechanicsManager* mechanics = new MWMechanics::MechanicsManager;
mEnvironment.setMechanicsManager (mechanics);
mEnvironment.setMechanicsManager (std::make_unique<MWMechanics::MechanicsManager>());
// Create dialog system
mEnvironment.setJournal (new MWDialogue::Journal);
mEnvironment.setDialogueManager (new MWDialogue::DialogueManager (mExtensions, mTranslationDataStorage));
mEnvironment.setJournal (std::make_unique<MWDialogue::Journal>());
mEnvironment.setDialogueManager (std::make_unique<MWDialogue::DialogueManager>(mExtensions, mTranslationDataStorage));
mEnvironment.setResourceSystem(mResourceSystem.get());
// scripts

@ -18,68 +18,64 @@
MWBase::Environment *MWBase::Environment::sThis = nullptr;
MWBase::Environment::Environment()
: mWorld (nullptr), mSoundManager (nullptr), mScriptManager (nullptr), mWindowManager (nullptr),
mMechanicsManager (nullptr), mDialogueManager (nullptr), mJournal (nullptr), mInputManager (nullptr),
mStateManager (nullptr), mLuaManager (nullptr), mResourceSystem (nullptr), mFrameDuration (0), mFrameRateLimit(0.f)
{
assert (!sThis);
assert(!sThis);
sThis = this;
}
MWBase::Environment::~Environment()
{
cleanup();
sThis = nullptr;
}
void MWBase::Environment::setWorld (World *world)
void MWBase::Environment::setWorld (std::unique_ptr<World>&& world)
{
mWorld = world;
mWorld = std::move(world);
}
void MWBase::Environment::setSoundManager (SoundManager *soundManager)
void MWBase::Environment::setSoundManager (std::unique_ptr<SoundManager>&& soundManager)
{
mSoundManager = soundManager;
mSoundManager = std::move(soundManager);
}
void MWBase::Environment::setScriptManager (ScriptManager *scriptManager)
void MWBase::Environment::setScriptManager (std::unique_ptr<ScriptManager>&& scriptManager)
{
mScriptManager = scriptManager;
mScriptManager = std::move(scriptManager);
}
void MWBase::Environment::setWindowManager (WindowManager *windowManager)
void MWBase::Environment::setWindowManager (std::unique_ptr<WindowManager>&& windowManager)
{
mWindowManager = windowManager;
mWindowManager = std::move(windowManager);
}
void MWBase::Environment::setMechanicsManager (MechanicsManager *mechanicsManager)
void MWBase::Environment::setMechanicsManager (std::unique_ptr<MechanicsManager>&& mechanicsManager)
{
mMechanicsManager = mechanicsManager;
mMechanicsManager = std::move(mechanicsManager);
}
void MWBase::Environment::setDialogueManager (DialogueManager *dialogueManager)
void MWBase::Environment::setDialogueManager (std::unique_ptr<DialogueManager>&& dialogueManager)
{
mDialogueManager = dialogueManager;
mDialogueManager = std::move(dialogueManager);
}
void MWBase::Environment::setJournal (Journal *journal)
void MWBase::Environment::setJournal (std::unique_ptr<Journal>&& journal)
{
mJournal = journal;
mJournal = std::move(journal);
}
void MWBase::Environment::setInputManager (InputManager *inputManager)
void MWBase::Environment::setInputManager (std::unique_ptr<InputManager>&& inputManager)
{
mInputManager = inputManager;
mInputManager = std::move(inputManager);
}
void MWBase::Environment::setStateManager (StateManager *stateManager)
void MWBase::Environment::setStateManager (std::unique_ptr<StateManager>&& stateManager)
{
mStateManager = stateManager;
mStateManager = std::move(stateManager);
}
void MWBase::Environment::setLuaManager (LuaManager *luaManager)
void MWBase::Environment::setLuaManager (std::unique_ptr<LuaManager>&& luaManager)
{
mLuaManager = luaManager;
mLuaManager = std::move(luaManager);
}
void MWBase::Environment::setResourceSystem (Resource::ResourceSystem *resourceSystem)
@ -105,61 +101,61 @@ float MWBase::Environment::getFrameRateLimit() const
MWBase::World *MWBase::Environment::getWorld() const
{
assert (mWorld);
return mWorld;
return mWorld.get();
}
MWBase::SoundManager *MWBase::Environment::getSoundManager() const
{
assert (mSoundManager);
return mSoundManager;
return mSoundManager.get();
}
MWBase::ScriptManager *MWBase::Environment::getScriptManager() const
{
assert (mScriptManager);
return mScriptManager;
return mScriptManager.get();
}
MWBase::WindowManager *MWBase::Environment::getWindowManager() const
{
assert (mWindowManager);
return mWindowManager;
return mWindowManager.get();
}
MWBase::MechanicsManager *MWBase::Environment::getMechanicsManager() const
{
assert (mMechanicsManager);
return mMechanicsManager;
return mMechanicsManager.get();
}
MWBase::DialogueManager *MWBase::Environment::getDialogueManager() const
{
assert (mDialogueManager);
return mDialogueManager;
return mDialogueManager.get();
}
MWBase::Journal *MWBase::Environment::getJournal() const
{
assert (mJournal);
return mJournal;
return mJournal.get();
}
MWBase::InputManager *MWBase::Environment::getInputManager() const
{
assert (mInputManager);
return mInputManager;
return mInputManager.get();
}
MWBase::StateManager *MWBase::Environment::getStateManager() const
{
assert (mStateManager);
return mStateManager;
return mStateManager.get();
}
MWBase::LuaManager *MWBase::Environment::getLuaManager() const
{
assert (mLuaManager);
return mLuaManager;
return mLuaManager.get();
}
Resource::ResourceSystem *MWBase::Environment::getResourceSystem() const
@ -174,35 +170,17 @@ float MWBase::Environment::getFrameDuration() const
void MWBase::Environment::cleanup()
{
delete mMechanicsManager;
mMechanicsManager = nullptr;
delete mDialogueManager;
mDialogueManager = nullptr;
delete mJournal;
mJournal = nullptr;
delete mScriptManager;
mScriptManager = nullptr;
delete mWindowManager;
mWindowManager = nullptr;
delete mWorld;
mWorld = nullptr;
delete mSoundManager;
mSoundManager = nullptr;
delete mInputManager;
mInputManager = nullptr;
delete mStateManager;
mStateManager = nullptr;
delete mLuaManager;
mLuaManager = nullptr;
mMechanicsManager.reset();
mDialogueManager.reset();
mJournal.reset();
mScriptManager.reset();
mWindowManager.reset();
mWorld.reset();
mSoundManager.reset();
mInputManager.reset();
mStateManager.reset();
mLuaManager.reset();
mResourceSystem = nullptr;
}
const MWBase::Environment& MWBase::Environment::get()

@ -1,6 +1,8 @@
#ifndef GAME_BASE_ENVIRONMENT_H
#define GAME_BASE_ENVIRONMENT_H
#include <memory>
namespace osg
{
class Stats;
@ -28,25 +30,23 @@ namespace MWBase
///
/// This class allows each mw-subsystem to access any others subsystem's top-level manager class.
///
/// \attention Environment takes ownership of the manager class instances it is handed over in
/// the set* functions.
class Environment
{
static Environment *sThis;
World *mWorld;
SoundManager *mSoundManager;
ScriptManager *mScriptManager;
WindowManager *mWindowManager;
MechanicsManager *mMechanicsManager;
DialogueManager *mDialogueManager;
Journal *mJournal;
InputManager *mInputManager;
StateManager *mStateManager;
LuaManager *mLuaManager;
Resource::ResourceSystem *mResourceSystem;
float mFrameDuration;
float mFrameRateLimit;
std::unique_ptr<World> mWorld;
std::unique_ptr<SoundManager> mSoundManager;
std::unique_ptr<ScriptManager> mScriptManager;
std::unique_ptr<WindowManager> mWindowManager;
std::unique_ptr<MechanicsManager> mMechanicsManager;
std::unique_ptr<DialogueManager> mDialogueManager;
std::unique_ptr<Journal> mJournal;
std::unique_ptr<InputManager> mInputManager;
std::unique_ptr<StateManager> mStateManager;
std::unique_ptr<LuaManager> mLuaManager;
Resource::ResourceSystem* mResourceSystem{};
float mFrameDuration{};
float mFrameRateLimit{};
Environment (const Environment&);
///< not implemented
@ -60,25 +60,25 @@ namespace MWBase
~Environment();
void setWorld (World *world);
void setWorld (std::unique_ptr<World>&& world);
void setSoundManager (SoundManager *soundManager);
void setSoundManager (std::unique_ptr<SoundManager>&& soundManager);
void setScriptManager (MWBase::ScriptManager *scriptManager);
void setScriptManager (std::unique_ptr<ScriptManager>&& scriptManager);
void setWindowManager (WindowManager *windowManager);
void setWindowManager (std::unique_ptr<WindowManager>&& windowManager);
void setMechanicsManager (MechanicsManager *mechanicsManager);
void setMechanicsManager (std::unique_ptr<MechanicsManager>&& mechanicsManager);
void setDialogueManager (DialogueManager *dialogueManager);
void setDialogueManager (std::unique_ptr<DialogueManager>&& dialogueManager);
void setJournal (Journal *journal);
void setJournal (std::unique_ptr<Journal>&& journal);
void setInputManager (InputManager *inputManager);
void setInputManager (std::unique_ptr<InputManager>&& inputManager);
void setStateManager (StateManager *stateManager);
void setStateManager (std::unique_ptr<StateManager>&& stateManager);
void setLuaManager (LuaManager *luaManager);
void setLuaManager (std::unique_ptr<LuaManager>&& luaManager);
void setResourceSystem (Resource::ResourceSystem *resourceSystem);

Loading…
Cancel
Save