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

@ -18,68 +18,64 @@
MWBase::Environment *MWBase::Environment::sThis = nullptr; MWBase::Environment *MWBase::Environment::sThis = nullptr;
MWBase::Environment::Environment() 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; sThis = this;
} }
MWBase::Environment::~Environment() MWBase::Environment::~Environment()
{ {
cleanup();
sThis = nullptr; 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) void MWBase::Environment::setResourceSystem (Resource::ResourceSystem *resourceSystem)
@ -105,61 +101,61 @@ float MWBase::Environment::getFrameRateLimit() const
MWBase::World *MWBase::Environment::getWorld() const MWBase::World *MWBase::Environment::getWorld() const
{ {
assert (mWorld); assert (mWorld);
return mWorld; return mWorld.get();
} }
MWBase::SoundManager *MWBase::Environment::getSoundManager() const MWBase::SoundManager *MWBase::Environment::getSoundManager() const
{ {
assert (mSoundManager); assert (mSoundManager);
return mSoundManager; return mSoundManager.get();
} }
MWBase::ScriptManager *MWBase::Environment::getScriptManager() const MWBase::ScriptManager *MWBase::Environment::getScriptManager() const
{ {
assert (mScriptManager); assert (mScriptManager);
return mScriptManager; return mScriptManager.get();
} }
MWBase::WindowManager *MWBase::Environment::getWindowManager() const MWBase::WindowManager *MWBase::Environment::getWindowManager() const
{ {
assert (mWindowManager); assert (mWindowManager);
return mWindowManager; return mWindowManager.get();
} }
MWBase::MechanicsManager *MWBase::Environment::getMechanicsManager() const MWBase::MechanicsManager *MWBase::Environment::getMechanicsManager() const
{ {
assert (mMechanicsManager); assert (mMechanicsManager);
return mMechanicsManager; return mMechanicsManager.get();
} }
MWBase::DialogueManager *MWBase::Environment::getDialogueManager() const MWBase::DialogueManager *MWBase::Environment::getDialogueManager() const
{ {
assert (mDialogueManager); assert (mDialogueManager);
return mDialogueManager; return mDialogueManager.get();
} }
MWBase::Journal *MWBase::Environment::getJournal() const MWBase::Journal *MWBase::Environment::getJournal() const
{ {
assert (mJournal); assert (mJournal);
return mJournal; return mJournal.get();
} }
MWBase::InputManager *MWBase::Environment::getInputManager() const MWBase::InputManager *MWBase::Environment::getInputManager() const
{ {
assert (mInputManager); assert (mInputManager);
return mInputManager; return mInputManager.get();
} }
MWBase::StateManager *MWBase::Environment::getStateManager() const MWBase::StateManager *MWBase::Environment::getStateManager() const
{ {
assert (mStateManager); assert (mStateManager);
return mStateManager; return mStateManager.get();
} }
MWBase::LuaManager *MWBase::Environment::getLuaManager() const MWBase::LuaManager *MWBase::Environment::getLuaManager() const
{ {
assert (mLuaManager); assert (mLuaManager);
return mLuaManager; return mLuaManager.get();
} }
Resource::ResourceSystem *MWBase::Environment::getResourceSystem() const Resource::ResourceSystem *MWBase::Environment::getResourceSystem() const
@ -174,35 +170,17 @@ float MWBase::Environment::getFrameDuration() const
void MWBase::Environment::cleanup() void MWBase::Environment::cleanup()
{ {
delete mMechanicsManager; mMechanicsManager.reset();
mMechanicsManager = nullptr; mDialogueManager.reset();
mJournal.reset();
delete mDialogueManager; mScriptManager.reset();
mDialogueManager = nullptr; mWindowManager.reset();
mWorld.reset();
delete mJournal; mSoundManager.reset();
mJournal = nullptr; mInputManager.reset();
mStateManager.reset();
delete mScriptManager; mLuaManager.reset();
mScriptManager = nullptr; mResourceSystem = 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;
} }
const MWBase::Environment& MWBase::Environment::get() const MWBase::Environment& MWBase::Environment::get()

@ -1,6 +1,8 @@
#ifndef GAME_BASE_ENVIRONMENT_H #ifndef GAME_BASE_ENVIRONMENT_H
#define GAME_BASE_ENVIRONMENT_H #define GAME_BASE_ENVIRONMENT_H
#include <memory>
namespace osg namespace osg
{ {
class Stats; class Stats;
@ -28,25 +30,23 @@ namespace MWBase
/// ///
/// This class allows each mw-subsystem to access any others subsystem's top-level manager class. /// 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 class Environment
{ {
static Environment *sThis; static Environment *sThis;
World *mWorld; std::unique_ptr<World> mWorld;
SoundManager *mSoundManager; std::unique_ptr<SoundManager> mSoundManager;
ScriptManager *mScriptManager; std::unique_ptr<ScriptManager> mScriptManager;
WindowManager *mWindowManager; std::unique_ptr<WindowManager> mWindowManager;
MechanicsManager *mMechanicsManager; std::unique_ptr<MechanicsManager> mMechanicsManager;
DialogueManager *mDialogueManager; std::unique_ptr<DialogueManager> mDialogueManager;
Journal *mJournal; std::unique_ptr<Journal> mJournal;
InputManager *mInputManager; std::unique_ptr<InputManager> mInputManager;
StateManager *mStateManager; std::unique_ptr<StateManager> mStateManager;
LuaManager *mLuaManager; std::unique_ptr<LuaManager> mLuaManager;
Resource::ResourceSystem *mResourceSystem; Resource::ResourceSystem* mResourceSystem{};
float mFrameDuration; float mFrameDuration{};
float mFrameRateLimit; float mFrameRateLimit{};
Environment (const Environment&); Environment (const Environment&);
///< not implemented ///< not implemented
@ -60,25 +60,25 @@ namespace MWBase
~Environment(); ~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); void setResourceSystem (Resource::ResourceSystem *resourceSystem);

Loading…
Cancel
Save