diff --git a/src/openrct2-ui/TextComposition.cpp b/src/openrct2-ui/TextComposition.cpp index 1063f69da3..a960080de3 100644 --- a/src/openrct2-ui/TextComposition.cpp +++ b/src/openrct2-ui/TextComposition.cpp @@ -150,7 +150,7 @@ void TextComposition::HandleMessage(const SDL_Event* e) auto [key, scancode] = ProcessKeyPress(rawKey, rawScancode); - GetContext()->GetUiContext()->SetKeysPressed(key, scancode); + GetContext()->GetUiContext().SetKeysPressed(key, scancode); // Text input if (_session.Buffer == nullptr) @@ -218,7 +218,7 @@ void TextComposition::HandleMessage(const SDL_Event* e) case SDLK_c: if ((modifier & KB_PRIMARY_MODIFIER) && _session.Length) { - GetContext()->GetUiContext()->SetClipboardText(_session.Buffer->c_str()); + GetContext()->GetUiContext().SetClipboardText(_session.Buffer->c_str()); ContextShowError(STR_COPY_INPUT_TO_CLIPBOARD, kStringIdNone, {}); } break; @@ -234,7 +234,7 @@ void TextComposition::HandleMessage(const SDL_Event* e) case SDLK_x: if ((modifier & KB_PRIMARY_MODIFIER) && _session.Length) { - GetContext()->GetUiContext()->SetClipboardText(_session.Buffer->c_str()); + GetContext()->GetUiContext().SetClipboardText(_session.Buffer->c_str()); Clear(); Windows::WindowUpdateTextbox(); ContextShowError(STR_COPY_INPUT_TO_CLIPBOARD, kStringIdNone, {}); diff --git a/src/openrct2-ui/Ui.cpp b/src/openrct2-ui/Ui.cpp index 214fbe15f6..230f879550 100644 --- a/src/openrct2-ui/Ui.cpp +++ b/src/openrct2-ui/Ui.cpp @@ -67,19 +67,19 @@ int main(int argc, const char** argv) else { // Run OpenRCT2 with a UI context - auto env = ToShared(CreatePlatformEnvironment()); - std::shared_ptr audioContext; + auto env = CreatePlatformEnvironment(); + std::unique_ptr audioContext; try { - audioContext = ToShared(CreateAudioContext()); + audioContext = CreateAudioContext(); } catch (const SDLException& e) { LOG_WARNING("Failed to create audio context. Using dummy audio context. Error message was: %s", e.what()); - audioContext = ToShared(CreateDummyAudioContext()); + audioContext = CreateDummyAudioContext(); } - auto uiContext = ToShared(CreateUiContext(env)); - context = CreateContext(env, audioContext, uiContext); + auto uiContext = CreateUiContext(*env); + context = CreateContext(std::move(env), std::move(audioContext), std::move(uiContext)); } rc = context->RunOpenRCT2(argc, argv); } diff --git a/src/openrct2-ui/UiContext.Linux.cpp b/src/openrct2-ui/UiContext.Linux.cpp index b56236a011..b7ab3c0b94 100644 --- a/src/openrct2-ui/UiContext.Linux.cpp +++ b/src/openrct2-ui/UiContext.Linux.cpp @@ -413,9 +413,9 @@ namespace OpenRCT2::Ui static void ThrowMissingDialogApp() { - auto uiContext = GetContext()->GetUiContext(); + auto& uiContext = GetContext()->GetUiContext(); std::string dialogMissingWarning = LanguageGetString(STR_MISSING_DIALOG_APPLICATION_ERROR); - uiContext->ShowMessageBox(dialogMissingWarning); + uiContext.ShowMessageBox(dialogMissingWarning); throw std::runtime_error(dialogMissingWarning); } diff --git a/src/openrct2-ui/UiContext.cpp b/src/openrct2-ui/UiContext.cpp index 0a300e84f3..604363a745 100644 --- a/src/openrct2-ui/UiContext.cpp +++ b/src/openrct2-ui/UiContext.cpp @@ -115,7 +115,7 @@ public: return _shortcutManager; } - explicit UiContext(const std::shared_ptr& env) + explicit UiContext(IPlatformEnvironment& env) : _platformUiContext(CreatePlatformUiContext()) , _windowManager(CreateWindowManager()) , _shortcutManager(env) @@ -1054,8 +1054,8 @@ private: void SetAudioVolume(float value) { - auto audioContext = GetContext()->GetAudioContext(); - auto mixer = audioContext->GetMixer(); + auto& audioContext = GetContext()->GetAudioContext(); + auto* mixer = audioContext.GetMixer(); if (mixer != nullptr) { mixer->SetVolume(value); @@ -1063,25 +1063,25 @@ private: } }; -std::unique_ptr OpenRCT2::Ui::CreateUiContext(const std::shared_ptr& env) +std::unique_ptr OpenRCT2::Ui::CreateUiContext(IPlatformEnvironment& env) { return std::make_unique(env); } InGameConsole& OpenRCT2::Ui::GetInGameConsole() { - auto uiContext = std::static_pointer_cast(GetContext()->GetUiContext()); - return uiContext->GetInGameConsole(); + auto& uiContext = static_cast(GetContext()->GetUiContext()); + return uiContext.GetInGameConsole(); } InputManager& OpenRCT2::Ui::GetInputManager() { - auto uiContext = std::static_pointer_cast(GetContext()->GetUiContext()); - return uiContext->GetInputManager(); + auto& uiContext = static_cast(GetContext()->GetUiContext()); + return uiContext.GetInputManager(); } ShortcutManager& OpenRCT2::Ui::GetShortcutManager() { - auto uiContext = std::static_pointer_cast(GetContext()->GetUiContext()); - return uiContext->GetShortcutManager(); + auto& uiContext = static_cast(GetContext()->GetUiContext()); + return uiContext.GetShortcutManager(); } diff --git a/src/openrct2-ui/UiContext.h b/src/openrct2-ui/UiContext.h index d05913b144..a6a0cf058e 100644 --- a/src/openrct2-ui/UiContext.h +++ b/src/openrct2-ui/UiContext.h @@ -50,7 +50,7 @@ namespace OpenRCT2::Ui virtual bool HasFilePicker() const = 0; }; - [[nodiscard]] std::unique_ptr CreateUiContext(const std::shared_ptr& env); + [[nodiscard]] std::unique_ptr CreateUiContext(IPlatformEnvironment& env); [[nodiscard]] std::unique_ptr CreatePlatformUiContext(); [[nodiscard]] InGameConsole& GetInGameConsole(); diff --git a/src/openrct2-ui/WindowManager.cpp b/src/openrct2-ui/WindowManager.cpp index 9a54bebf3a..4d2dcfba2d 100644 --- a/src/openrct2-ui/WindowManager.cpp +++ b/src/openrct2-ui/WindowManager.cpp @@ -737,9 +737,9 @@ public: static ScreenCoordsXY GetAutoPositionForNewWindow(int32_t width, int32_t height) { - auto uiContext = GetContext()->GetUiContext(); - auto screenWidth = uiContext->GetWidth(); - auto screenHeight = uiContext->GetHeight(); + auto& uiContext = GetContext()->GetUiContext(); + auto screenWidth = uiContext.GetWidth(); + auto screenHeight = uiContext.GetHeight(); // Place window in an empty corner of the screen const ScreenCoordsXY cornerPositions[] = { @@ -827,9 +827,9 @@ public: static ScreenCoordsXY GetCentrePositionForNewWindow(int32_t width, int32_t height) { - auto uiContext = GetContext()->GetUiContext(); - auto screenWidth = uiContext->GetWidth(); - auto screenHeight = uiContext->GetHeight(); + auto& uiContext = GetContext()->GetUiContext(); + auto screenWidth = uiContext.GetWidth(); + auto screenHeight = uiContext.GetHeight(); return ScreenCoordsXY{ (screenWidth - width) / 2, std::max(kTopToolbarHeight + 1, (screenHeight - height) / 2) }; } diff --git a/src/openrct2-ui/audio/SDLAudioSource.cpp b/src/openrct2-ui/audio/SDLAudioSource.cpp index ca267a3b8d..3999d7170e 100644 --- a/src/openrct2-ui/audio/SDLAudioSource.cpp +++ b/src/openrct2-ui/audio/SDLAudioSource.cpp @@ -53,11 +53,8 @@ IAudioMixer* SDLAudioSource::GetMixer() if (ctx == nullptr) return nullptr; - auto audioContext = ctx->GetAudioContext(); - if (audioContext == nullptr) - return nullptr; - - return audioContext->GetMixer(); + auto& audioContext = ctx->GetAudioContext(); + return audioContext.GetMixer(); } int32_t SDLAudioSource::GetBytesPerSecond() const diff --git a/src/openrct2-ui/drawing/engines/DrawingEngineFactory.hpp b/src/openrct2-ui/drawing/engines/DrawingEngineFactory.hpp index 12aa974acb..e582fa5990 100644 --- a/src/openrct2-ui/drawing/engines/DrawingEngineFactory.hpp +++ b/src/openrct2-ui/drawing/engines/DrawingEngineFactory.hpp @@ -17,18 +17,15 @@ namespace OpenRCT2::Ui { struct IUiContext; - [[nodiscard]] std::unique_ptr CreateHardwareDisplayDrawingEngine( - const std::shared_ptr& uiContext); + [[nodiscard]] std::unique_ptr CreateHardwareDisplayDrawingEngine(IUiContext& uiContext); #ifndef DISABLE_OPENGL - [[nodiscard]] std::unique_ptr CreateOpenGLDrawingEngine( - const std::shared_ptr& uiContext); + [[nodiscard]] std::unique_ptr CreateOpenGLDrawingEngine(IUiContext& uiContext); #endif class DrawingEngineFactory final : public Drawing::IDrawingEngineFactory { public: - [[nodiscard]] std::unique_ptr Create( - DrawingEngine type, const std::shared_ptr& uiContext) override + [[nodiscard]] std::unique_ptr Create(DrawingEngine type, IUiContext& uiContext) override { switch (type) { diff --git a/src/openrct2-ui/drawing/engines/HardwareDisplayDrawingEngine.cpp b/src/openrct2-ui/drawing/engines/HardwareDisplayDrawingEngine.cpp index 85f70fbf54..61553069aa 100644 --- a/src/openrct2-ui/drawing/engines/HardwareDisplayDrawingEngine.cpp +++ b/src/openrct2-ui/drawing/engines/HardwareDisplayDrawingEngine.cpp @@ -33,7 +33,7 @@ private: constexpr static uint32_t kDirtyVisualTime = 40; constexpr static uint32_t kDirtyRegionAlpha = 100; - std::shared_ptr const _uiContext; + IUiContext& _uiContext; SDL_Window* _window = nullptr; SDL_Renderer* _sdlRenderer = nullptr; SDL_Texture* _screenTexture = nullptr; @@ -54,11 +54,11 @@ private: bool smoothNN = false; public: - explicit HardwareDisplayDrawingEngine(const std::shared_ptr& uiContext) + explicit HardwareDisplayDrawingEngine(IUiContext& uiContext) : X8DrawingEngine(uiContext) , _uiContext(uiContext) { - _window = static_cast(_uiContext->GetWindow()); + _window = static_cast(_uiContext.GetWindow()); } ~HardwareDisplayDrawingEngine() override @@ -128,7 +128,7 @@ public: } } - ScaleQuality scaleQuality = GetContext()->GetUiContext()->GetScaleQuality(); + ScaleQuality scaleQuality = GetContext()->GetUiContext().GetScaleQuality(); if (scaleQuality == ScaleQuality::SmoothNearestNeighbour) { scaleQuality = ScaleQuality::Linear; @@ -270,7 +270,7 @@ private: RenderDirtyVisuals(); } - bool isSteamOverlayActive = GetContext()->GetUiContext()->IsSteamOverlayActive(); + bool isSteamOverlayActive = GetContext()->GetUiContext().IsSteamOverlayActive(); if (isSteamOverlayActive && Config::Get().general.SteamOverlayPause) { OverlayPreRenderCheck(); @@ -428,7 +428,7 @@ private: } }; -std::unique_ptr OpenRCT2::Ui::CreateHardwareDisplayDrawingEngine(const std::shared_ptr& uiContext) +std::unique_ptr OpenRCT2::Ui::CreateHardwareDisplayDrawingEngine(IUiContext& uiContext) { return std::make_unique(uiContext); } diff --git a/src/openrct2-ui/drawing/engines/opengl/OpenGLDrawingEngine.cpp b/src/openrct2-ui/drawing/engines/opengl/OpenGLDrawingEngine.cpp index 9aca2047ce..9dcd8674da 100644 --- a/src/openrct2-ui/drawing/engines/opengl/OpenGLDrawingEngine.cpp +++ b/src/openrct2-ui/drawing/engines/opengl/OpenGLDrawingEngine.cpp @@ -193,7 +193,7 @@ public: class OpenGLDrawingEngine final : public IDrawingEngine { private: - std::shared_ptr const _uiContext; + IUiContext& _uiContext; SDL_Window* _window = nullptr; SDL_GLContext _context = nullptr; @@ -218,12 +218,12 @@ public: SDL_Color Palette[256]; vec4 GLPalette[256]; - explicit OpenGLDrawingEngine(const std::shared_ptr& uiContext) + explicit OpenGLDrawingEngine(IUiContext& uiContext) : _uiContext(uiContext) , _drawingContext(std::make_unique(*this)) , _weatherDrawer(_drawingContext.get()) { - _window = static_cast(_uiContext->GetWindow()); + _window = static_cast(_uiContext.GetWindow()); _mainRT.DrawingEngine = this; LightFx::SetAvailable(false); } @@ -555,11 +555,11 @@ private: _screenFramebuffer = std::make_unique(_window); _smoothScaleFramebuffer.reset(); _scaleFramebuffer.reset(); - if (GetContext()->GetUiContext()->GetScaleQuality() != ScaleQuality::NearestNeighbour) + if (GetContext()->GetUiContext().GetScaleQuality() != ScaleQuality::NearestNeighbour) { _scaleFramebuffer = std::make_unique(_width, _height, false, false); } - if (GetContext()->GetUiContext()->GetScaleQuality() == ScaleQuality::SmoothNearestNeighbour) + if (GetContext()->GetUiContext().GetScaleQuality() == ScaleQuality::SmoothNearestNeighbour) { uint32_t scale = std::ceil(Config::Get().general.WindowScale); _smoothScaleFramebuffer = std::make_unique(_width * scale, _height * scale, false, false); @@ -572,7 +572,7 @@ private: } }; -std::unique_ptr OpenRCT2::Ui::CreateOpenGLDrawingEngine(const std::shared_ptr& uiContext) +std::unique_ptr OpenRCT2::Ui::CreateOpenGLDrawingEngine(IUiContext& uiContext) { return std::make_unique(uiContext); } diff --git a/src/openrct2-ui/drawing/engines/opengl/OpenGLShaderProgram.cpp b/src/openrct2-ui/drawing/engines/opengl/OpenGLShaderProgram.cpp index fcd5ba2888..29e296e8fe 100644 --- a/src/openrct2-ui/drawing/engines/opengl/OpenGLShaderProgram.cpp +++ b/src/openrct2-ui/drawing/engines/opengl/OpenGLShaderProgram.cpp @@ -58,8 +58,8 @@ GLuint OpenGLShader::GetShaderId() std::string OpenGLShader::GetPath(const std::string& name) { - auto env = GetContext()->GetPlatformEnvironment(); - auto shadersPath = env->GetDirectoryPath(DirBase::openrct2, DirId::shaders); + auto& env = GetContext()->GetPlatformEnvironment(); + auto shadersPath = env.GetDirectoryPath(DirBase::openrct2, DirId::shaders); auto path = Path::Combine(shadersPath, name); if (_type == GL_VERTEX_SHADER) { diff --git a/src/openrct2-ui/input/ShortcutManager.cpp b/src/openrct2-ui/input/ShortcutManager.cpp index eaeff43e32..52f3c09bc3 100644 --- a/src/openrct2-ui/input/ShortcutManager.cpp +++ b/src/openrct2-ui/input/ShortcutManager.cpp @@ -108,7 +108,7 @@ std::string RegisteredShortcut::GetDisplayString() const return result; } -ShortcutManager::ShortcutManager(const std::shared_ptr& env) +ShortcutManager::ShortcutManager(IPlatformEnvironment& env) : _env(env) { RegisterDefaultShortcuts(); @@ -196,7 +196,7 @@ void ShortcutManager::LoadUserBindings() { try { - auto path = fs::u8path(_env->GetFilePath(PathId::configShortcuts)); + auto path = fs::u8path(_env.GetFilePath(PathId::configShortcuts)); if (fs::exists(path)) { LoadUserBindings(path); @@ -206,7 +206,7 @@ void ShortcutManager::LoadUserBindings() try { Console::WriteLine("Importing legacy shortcuts..."); - auto legacyPath = fs::u8path(_env->GetFilePath(PathId::configShortcutsLegacy)); + auto legacyPath = fs::u8path(_env.GetFilePath(PathId::configShortcutsLegacy)); if (fs::exists(legacyPath)) { LoadLegacyBindings(legacyPath); @@ -317,7 +317,7 @@ void ShortcutManager::SaveUserBindings() { try { - auto path = fs::u8path(_env->GetFilePath(PathId::configShortcuts)); + auto path = fs::u8path(_env.GetFilePath(PathId::configShortcuts)); SaveUserBindings(path); } catch (const std::exception& e) diff --git a/src/openrct2-ui/input/ShortcutManager.h b/src/openrct2-ui/input/ShortcutManager.h index bbe6d9442d..50e9250818 100644 --- a/src/openrct2-ui/input/ShortcutManager.h +++ b/src/openrct2-ui/input/ShortcutManager.h @@ -112,7 +112,7 @@ namespace OpenRCT2::Ui class ShortcutManager { private: - std::shared_ptr _env; + IPlatformEnvironment& _env; std::string _pendingShortcutChange; static std::optional ConvertLegacyBinding(uint16_t binding); @@ -126,7 +126,7 @@ namespace OpenRCT2::Ui public: std::unordered_map Shortcuts; - ShortcutManager(const std::shared_ptr& env); + ShortcutManager(IPlatformEnvironment& env); ShortcutManager(const ShortcutManager&) = delete; void LoadUserBindings(); diff --git a/src/openrct2-ui/interface/FileBrowser.cpp b/src/openrct2-ui/interface/FileBrowser.cpp index 0fc656bbf9..588d5b277d 100644 --- a/src/openrct2-ui/interface/FileBrowser.cpp +++ b/src/openrct2-ui/interface/FileBrowser.cpp @@ -37,7 +37,7 @@ namespace OpenRCT2::Ui::FileBrowser { RegisterCallback(callback); - auto hasFilePicker = OpenRCT2::GetContext()->GetUiContext()->HasFilePicker(); + auto hasFilePicker = OpenRCT2::GetContext()->GetUiContext().HasFilePicker(); auto& config = Config::Get().general; // Open system file picker? @@ -144,11 +144,11 @@ namespace OpenRCT2::Ui::FileBrowser break; } - auto env = GetContext()->GetPlatformEnvironment(); + auto& env = GetContext()->GetPlatformEnvironment(); if (subdir.has_value()) - return env->GetDirectoryPath(DirBase::user, subdir.value()); + return env.GetDirectoryPath(DirBase::user, subdir.value()); else - return env->GetDirectoryPath(DirBase::user); + return env.GetDirectoryPath(DirBase::user); } const char* GetFilterPatternByType(const LoadSaveType type, const bool isSave) diff --git a/src/openrct2-ui/interface/Theme.cpp b/src/openrct2-ui/interface/Theme.cpp index 223b3b076c..f35a8ad7a9 100644 --- a/src/openrct2-ui/interface/Theme.cpp +++ b/src/openrct2-ui/interface/Theme.cpp @@ -686,8 +686,8 @@ static constexpr UIThemeWindowEntry PredefinedThemeRCT1_Entries[] = std::string GetThemePath() { auto context = GetContext(); - auto env = context->GetPlatformEnvironment(); - return env->GetDirectoryPath(DirBase::user, DirId::themes); + auto& env = context->GetPlatformEnvironment(); + return env.GetDirectoryPath(DirBase::user, DirId::themes); } } // namespace ThemeManager diff --git a/src/openrct2-ui/windows/About.cpp b/src/openrct2-ui/windows/About.cpp index e796481b4e..5eb639b36f 100644 --- a/src/openrct2-ui/windows/About.cpp +++ b/src/openrct2-ui/windows/About.cpp @@ -132,7 +132,7 @@ namespace OpenRCT2::Ui::Windows SetPage(widgetIndex - WIDX_TAB_ABOUT_OPENRCT2); break; case WIDX_JOIN_DISCORD: - OpenRCT2::GetContext()->GetUiContext()->OpenURL("https://discord.gg/ZXZd8D8"); + OpenRCT2::GetContext()->GetUiContext().OpenURL("https://discord.gg/ZXZd8D8"); break; case WIDX_CHANGELOG: ContextOpenWindow(WindowClass::Changelog); @@ -141,7 +141,7 @@ namespace OpenRCT2::Ui::Windows ContextOpenWindowView(WV_NEW_VERSION_INFO); break; case WIDX_COPY_BUILD_INFO: - OpenRCT2::GetContext()->GetUiContext()->SetClipboardText(gVersionInfoFull); + OpenRCT2::GetContext()->GetUiContext().SetClipboardText(gVersionInfoFull); break; case WIDX_CONTRIBUTORS_BUTTON: ContextOpenWindowView(WV_CONTRIBUTORS); diff --git a/src/openrct2-ui/windows/Changelog.cpp b/src/openrct2-ui/windows/Changelog.cpp index 770c9ade75..3a6680053f 100644 --- a/src/openrct2-ui/windows/Changelog.cpp +++ b/src/openrct2-ui/windows/Changelog.cpp @@ -63,8 +63,8 @@ namespace OpenRCT2::Ui::Windows */ const std::string GetText(PathId pathId) { - auto env = GetContext()->GetPlatformEnvironment(); - auto path = env->GetFilePath(pathId); + auto& env = GetContext()->GetPlatformEnvironment(); + auto path = env.GetFilePath(pathId); auto fs = std::ifstream(fs::u8path(path), std::ios::in); if (!fs.is_open()) { @@ -165,7 +165,7 @@ namespace OpenRCT2::Ui::Windows case WIDX_OPEN_URL: if (_newVersionInfo != nullptr) { - GetContext()->GetUiContext()->OpenURL("https://openrct2.io/download/release"); + GetContext()->GetUiContext().OpenURL("https://openrct2.io/download/release"); } else { diff --git a/src/openrct2-ui/windows/InstallTrack.cpp b/src/openrct2-ui/windows/InstallTrack.cpp index 5a351dfb59..dea22082cd 100644 --- a/src/openrct2-ui/windows/InstallTrack.cpp +++ b/src/openrct2-ui/windows/InstallTrack.cpp @@ -362,8 +362,8 @@ namespace OpenRCT2::Ui::Windows void InstallTrackDesign() { - auto env = OpenRCT2::GetContext()->GetPlatformEnvironment(); - auto destPath = env->GetDirectoryPath(OpenRCT2::DirBase::user, OpenRCT2::DirId::trackDesigns); + auto& env = OpenRCT2::GetContext()->GetPlatformEnvironment(); + auto destPath = env.GetDirectoryPath(OpenRCT2::DirBase::user, OpenRCT2::DirId::trackDesigns); if (!Path::CreateDirectory(destPath)) { LOG_ERROR("Unable to create directory '%s'", destPath.c_str()); diff --git a/src/openrct2-ui/windows/LoadSave.cpp b/src/openrct2-ui/windows/LoadSave.cpp index 32ac054340..778c044514 100644 --- a/src/openrct2-ui/windows/LoadSave.cpp +++ b/src/openrct2-ui/windows/LoadSave.cpp @@ -365,7 +365,7 @@ namespace OpenRCT2::Ui::Windows } }, [](const ParkPreview preview) { - auto* windowMgr = GetContext()->GetUiContext()->GetWindowManager(); + auto* windowMgr = GetContext()->GetUiContext().GetWindowManager(); auto* wnd = windowMgr->FindByClass(WindowClass::Loadsave); if (wnd == nullptr) { @@ -497,8 +497,8 @@ namespace OpenRCT2::Ui::Windows { SetWidgets(window_loadsave_widgets); - const auto uiContext = OpenRCT2::GetContext()->GetUiContext(); - if (!uiContext->HasFilePicker()) + const auto& uiContext = OpenRCT2::GetContext()->GetUiContext(); + if (!uiContext.HasFilePicker()) { disabled_widgets |= (1uLL << WIDX_SYSTEM_BROWSER); widgets[WIDX_SYSTEM_BROWSER].type = WindowWidgetType::Empty; diff --git a/src/openrct2-ui/windows/ObjectLoadError.cpp b/src/openrct2-ui/windows/ObjectLoadError.cpp index 55b68aa761..174a2ddb47 100644 --- a/src/openrct2-ui/windows/ObjectLoadError.cpp +++ b/src/openrct2-ui/windows/ObjectLoadError.cpp @@ -377,7 +377,7 @@ namespace OpenRCT2::Ui::Windows } const auto clip = stream.str(); - OpenRCT2::GetContext()->GetUiContext()->SetClipboardText(clip.c_str()); + OpenRCT2::GetContext()->GetUiContext().SetClipboardText(clip.c_str()); } void SelectObjectFromList(const int32_t index) @@ -421,7 +421,7 @@ namespace OpenRCT2::Ui::Windows if (selected_list_item > -1 && selected_list_item < no_list_items) { const auto name = std::string(_invalidEntries[selected_list_item].GetName()); - OpenRCT2::GetContext()->GetUiContext()->SetClipboardText(name.c_str()); + OpenRCT2::GetContext()->GetUiContext().SetClipboardText(name.c_str()); } break; case WIDX_COPY_ALL: diff --git a/src/openrct2-ui/windows/Options.cpp b/src/openrct2-ui/windows/Options.cpp index 93388ad7c0..c33bec87e0 100644 --- a/src/openrct2-ui/windows/Options.cpp +++ b/src/openrct2-ui/windows/Options.cpp @@ -697,7 +697,7 @@ namespace OpenRCT2::Ui::Windows SetPressedTab(); disabled_widgets = 0; - auto hasFilePicker = OpenRCT2::GetContext()->GetUiContext()->HasFilePicker(); + auto hasFilePicker = OpenRCT2::GetContext()->GetUiContext().HasFilePicker(); const bool advancedTabSelected = (WIDX_FIRST_TAB + page) == WIDX_TAB_ADVANCED; if (!hasFilePicker && advancedTabSelected) { @@ -788,7 +788,7 @@ namespace OpenRCT2::Ui::Windows { case WIDX_RESOLUTION_DROPDOWN: { - const auto& resolutions = OpenRCT2::GetContext()->GetUiContext()->GetFullscreenResolutions(); + const auto& resolutions = OpenRCT2::GetContext()->GetUiContext().GetFullscreenResolutions(); int32_t selectedResolution = -1; for (size_t i = 0; i < resolutions.size(); i++) @@ -866,7 +866,7 @@ namespace OpenRCT2::Ui::Windows { case WIDX_RESOLUTION_DROPDOWN: { - const auto& resolutions = OpenRCT2::GetContext()->GetUiContext()->GetFullscreenResolutions(); + const auto& resolutions = OpenRCT2::GetContext()->GetUiContext().GetFullscreenResolutions(); const Resolution& resolution = resolutions[dropdownIndex]; if (resolution.Width != Config::Get().general.FullscreenWidth @@ -1420,16 +1420,16 @@ namespace OpenRCT2::Ui::Windows OpenRCT2::Audio::InitRideSounds(dropdownIndex); if (dropdownIndex < OpenRCT2::Audio::GetDeviceCount()) { - auto audioContext = GetContext()->GetAudioContext(); + auto& audioContext = GetContext()->GetAudioContext(); if (dropdownIndex == 0) { - audioContext->SetOutputDevice(""); + audioContext.SetOutputDevice(""); Config::Get().sound.Device = ""; } else { const auto& deviceName = GetDeviceName(dropdownIndex); - audioContext->SetOutputDevice(deviceName); + audioContext.SetOutputDevice(deviceName); Config::Get().sound.Device = deviceName; } Config::Save(); @@ -1981,7 +1981,7 @@ namespace OpenRCT2::Ui::Windows break; case WIDX_PATH_TO_RCT1_BROWSE: { - auto rct1path = OpenRCT2::GetContext()->GetUiContext()->ShowDirectoryDialog( + auto rct1path = OpenRCT2::GetContext()->GetUiContext().ShowDirectoryDialog( LanguageGetString(STR_PATH_TO_RCT1_BROWSER)); if (!rct1path.empty()) { @@ -2279,8 +2279,8 @@ namespace OpenRCT2::Ui::Windows static bool IsRCT1TitleMusicAvailable() { - auto env = GetContext()->GetPlatformEnvironment(); - auto rct1path = env->GetDirectoryPath(DirBase::rct1); + auto& env = GetContext()->GetPlatformEnvironment(); + auto rct1path = env.GetDirectoryPath(DirBase::rct1); return !rct1path.empty(); } diff --git a/src/openrct2-ui/windows/TitleMenu.cpp b/src/openrct2-ui/windows/TitleMenu.cpp index b89264f0c1..582226de66 100644 --- a/src/openrct2-ui/windows/TitleMenu.cpp +++ b/src/openrct2-ui/windows/TitleMenu.cpp @@ -244,9 +244,9 @@ namespace OpenRCT2::Ui::Windows case DDIDX_OPEN_CONTENT_FOLDER: { auto context = OpenRCT2::GetContext(); - auto env = context->GetPlatformEnvironment(); - auto uiContext = context->GetUiContext(); - uiContext->OpenFolder(env->GetDirectoryPath(OpenRCT2::DirBase::user)); + auto& env = context->GetPlatformEnvironment(); + auto& uiContext = context->GetUiContext(); + uiContext.OpenFolder(env.GetDirectoryPath(OpenRCT2::DirBase::user)); break; } default: diff --git a/src/openrct2-ui/windows/TopToolbar.cpp b/src/openrct2-ui/windows/TopToolbar.cpp index 471682c4cc..74700da9e9 100644 --- a/src/openrct2-ui/windows/TopToolbar.cpp +++ b/src/openrct2-ui/windows/TopToolbar.cpp @@ -493,7 +493,7 @@ namespace OpenRCT2::Ui::Windows // Automatically fill the "OpenRCT2 build" input auto versionStr = String::urlEncode(gVersionInfoFull); url.append("&f299dd2a20432827d99b648f73eb4649b23f8ec98d158d6f82b81e43196ee36b=" + versionStr); - OpenRCT2::GetContext()->GetUiContext()->OpenURL(url); + OpenRCT2::GetContext()->GetUiContext().OpenURL(url); } break; case DDIDX_UPDATE_AVAILABLE: diff --git a/src/openrct2/AssetPackManager.cpp b/src/openrct2/AssetPackManager.cpp index eb891b2218..9f63a02529 100644 --- a/src/openrct2/AssetPackManager.cpp +++ b/src/openrct2/AssetPackManager.cpp @@ -68,12 +68,12 @@ void AssetPackManager::Scan() ClearAssetPacks(); auto context = GetContext(); - auto env = context->GetPlatformEnvironment(); + auto& env = context->GetPlatformEnvironment(); - auto openrct2Dir = fs::u8path(env->GetDirectoryPath(DirBase::openrct2, DirId::assetPacks)); + auto openrct2Dir = fs::u8path(env.GetDirectoryPath(DirBase::openrct2, DirId::assetPacks)); Scan(openrct2Dir); - auto userDirectory = fs::u8path(env->GetDirectoryPath(DirBase::user, DirId::assetPacks)); + auto userDirectory = fs::u8path(env.GetDirectoryPath(DirBase::user, DirId::assetPacks)); Path::CreateDirectory(userDirectory.u8string()); Scan(userDirectory); } diff --git a/src/openrct2/Context.cpp b/src/openrct2/Context.cpp index 438699fc83..68ab8a8391 100644 --- a/src/openrct2/Context.cpp +++ b/src/openrct2/Context.cpp @@ -105,9 +105,9 @@ namespace OpenRCT2 { private: // Dependencies - std::shared_ptr const _env; - std::shared_ptr const _audioContext; - std::shared_ptr const _uiContext; + std::unique_ptr const _env; + std::unique_ptr const _audioContext; + std::unique_ptr const _uiContext; // Services std::unique_ptr _localisationService; @@ -170,21 +170,21 @@ namespace OpenRCT2 public: Context( - const std::shared_ptr& env, const std::shared_ptr& audioContext, - const std::shared_ptr& uiContext) - : _env(env) - , _audioContext(audioContext) - , _uiContext(uiContext) - , _localisationService(std::make_unique(env)) + std::unique_ptr&& env, std::unique_ptr&& audioContext, + std::unique_ptr&& uiContext) + : _env(std::move(env)) + , _audioContext(std::move(audioContext)) + , _uiContext(std::move(uiContext)) + , _localisationService(std::make_unique(*_env)) , _replayManager(CreateReplayManager()) , _gameStateSnapshots(CreateGameStateSnapshots()) #ifdef ENABLE_SCRIPTING - , _scriptEngine(_stdInOutConsole, *env) + , _scriptEngine(_stdInOutConsole, *_env) #endif #ifndef DISABLE_NETWORK , _network(*this) #endif - , _painter(std::make_unique(uiContext)) + , _painter(std::make_unique(*_uiContext)) { // Can't have more than one context currently. Guard::Assert(Instance == nullptr); @@ -227,14 +227,14 @@ namespace OpenRCT2 Instance = nullptr; } - std::shared_ptr GetAudioContext() override + IAudioContext& GetAudioContext() override { - return _audioContext; + return *_audioContext; } - std::shared_ptr GetUiContext() override + IUiContext& GetUiContext() override { - return _uiContext; + return *_uiContext; } #ifdef ENABLE_SCRIPTING @@ -244,9 +244,9 @@ namespace OpenRCT2 } #endif - std::shared_ptr GetPlatformEnvironment() override + IPlatformEnvironment& GetPlatformEnvironment() override { - return _env; + return *_env; } Localisation::LocalisationService& GetLocalisationService() override @@ -436,13 +436,13 @@ namespace OpenRCT2 catch (const std::exception& eFallback) { LOG_FATAL("Failed to open fallback language: %s", eFallback.what()); - auto uiContext = GetContext()->GetUiContext(); + auto& uiContext = GetContext()->GetUiContext(); #ifdef __ANDROID__ - uiContext->ShowMessageBox( + uiContext.ShowMessageBox( "You need to copy some additional files to finish your install.\n\nSee " "https://docs.openrct2.io/en/latest/installing/installing-on-android.html for more details."); #else - uiContext->ShowMessageBox("Failed to load language file!\nYour installation may be damaged."); + uiContext.ShowMessageBox("Failed to load language file!\nYour installation may be damaged."); #endif return false; } @@ -466,10 +466,10 @@ namespace OpenRCT2 // The repositories are all dependent on the RCT2 path being set, // so they cannot be set in the constructor. - _objectRepository = CreateObjectRepository(_env); + _objectRepository = CreateObjectRepository(*_env); _objectManager = CreateObjectManager(*_objectRepository); - _trackDesignRepository = CreateTrackDesignRepository(_env); - _scenarioRepository = CreateScenarioRepository(_env); + _trackDesignRepository = CreateTrackDesignRepository(*_env); + _scenarioRepository = CreateScenarioRepository(*_env); if (!gOpenRCT2Headless) { @@ -616,7 +616,7 @@ namespace OpenRCT2 try { auto drawingEngineFactory = _uiContext->GetDrawingEngineFactory(); - auto drawingEngine = drawingEngineFactory->Create(engine, _uiContext); + auto drawingEngine = drawingEngineFactory->Create(engine, *_uiContext); if (drawingEngine == nullptr) { LOG_FATAL("Unable to create a drawing engine."); @@ -1581,10 +1581,10 @@ namespace OpenRCT2 } std::unique_ptr CreateContext( - const std::shared_ptr& env, const std::shared_ptr& audioContext, - const std::shared_ptr& uiContext) + std::unique_ptr&& env, std::unique_ptr&& audioContext, + std::unique_ptr&& uiContext) { - return std::make_unique(env, audioContext, uiContext); + return std::make_unique(std::move(env), std::move(audioContext), std::move(uiContext)); } IContext* GetContext() @@ -1611,27 +1611,27 @@ void OpenRCT2Finish() void ContextSetCurrentCursor(CursorID cursor) { - GetContext()->GetUiContext()->SetCursor(cursor); + GetContext()->GetUiContext().SetCursor(cursor); } void ContextUpdateCursorScale() { - GetContext()->GetUiContext()->SetCursorScale(static_cast(std::round(Config::Get().general.WindowScale))); + GetContext()->GetUiContext().SetCursorScale(static_cast(std::round(Config::Get().general.WindowScale))); } void ContextHideCursor() { - GetContext()->GetUiContext()->SetCursorVisible(false); + GetContext()->GetUiContext().SetCursorVisible(false); } void ContextShowCursor() { - GetContext()->GetUiContext()->SetCursorVisible(true); + GetContext()->GetUiContext().SetCursorVisible(true); } ScreenCoordsXY ContextGetCursorPosition() { - return GetContext()->GetUiContext()->GetCursorPosition(); + return GetContext()->GetUiContext().GetCursorPosition(); } ScreenCoordsXY ContextGetCursorPositionScaled() @@ -1644,72 +1644,72 @@ ScreenCoordsXY ContextGetCursorPositionScaled() void ContextSetCursorPosition(const ScreenCoordsXY& cursorPosition) { - GetContext()->GetUiContext()->SetCursorPosition(cursorPosition); + GetContext()->GetUiContext().SetCursorPosition(cursorPosition); } const CursorState* ContextGetCursorState() { - return GetContext()->GetUiContext()->GetCursorState(); + return GetContext()->GetUiContext().GetCursorState(); } const uint8_t* ContextGetKeysState() { - return GetContext()->GetUiContext()->GetKeysState(); + return GetContext()->GetUiContext().GetKeysState(); } const uint8_t* ContextGetKeysPressed() { - return GetContext()->GetUiContext()->GetKeysPressed(); + return GetContext()->GetUiContext().GetKeysPressed(); } TextInputSession* ContextStartTextInput(u8string& buffer, size_t maxLength) { - return GetContext()->GetUiContext()->StartTextInput(buffer, maxLength); + return GetContext()->GetUiContext().StartTextInput(buffer, maxLength); } void ContextStopTextInput() { - GetContext()->GetUiContext()->StopTextInput(); + GetContext()->GetUiContext().StopTextInput(); } bool ContextIsInputActive() { - return GetContext()->GetUiContext()->IsTextInputActive(); + return GetContext()->GetUiContext().IsTextInputActive(); } void ContextTriggerResize() { - return GetContext()->GetUiContext()->TriggerResize(); + return GetContext()->GetUiContext().TriggerResize(); } void ContextSetFullscreenMode(int32_t mode) { - return GetContext()->GetUiContext()->SetFullscreenMode(static_cast(mode)); + return GetContext()->GetUiContext().SetFullscreenMode(static_cast(mode)); } void ContextRecreateWindow() { - GetContext()->GetUiContext()->RecreateWindow(); + GetContext()->GetUiContext().RecreateWindow(); } int32_t ContextGetWidth() { - return GetContext()->GetUiContext()->GetWidth(); + return GetContext()->GetUiContext().GetWidth(); } int32_t ContextGetHeight() { - return GetContext()->GetUiContext()->GetHeight(); + return GetContext()->GetUiContext().GetHeight(); } bool ContextHasFocus() { - return GetContext()->GetUiContext()->HasFocus(); + return GetContext()->GetUiContext().HasFocus(); } void ContextSetCursorTrap(bool value) { - GetContext()->GetUiContext()->SetCursorTrap(value); + GetContext()->GetUiContext().SetCursorTrap(value); } WindowBase* ContextOpenWindow(WindowClass wc) @@ -1775,7 +1775,7 @@ u8string ContextOpenCommonFileDialog(OpenRCT2::Ui::FileDialogDesc& desc) { try { - return GetContext()->GetUiContext()->ShowFileDialog(desc); + return GetContext()->GetUiContext().ShowFileDialog(desc); } catch (const std::exception& ex) { diff --git a/src/openrct2/Context.h b/src/openrct2/Context.h index bb866af868..52fc372578 100644 --- a/src/openrct2/Context.h +++ b/src/openrct2/Context.h @@ -124,9 +124,9 @@ namespace OpenRCT2 { virtual ~IContext() = default; - [[nodiscard]] virtual std::shared_ptr GetAudioContext() = 0; - [[nodiscard]] virtual std::shared_ptr GetUiContext() = 0; - [[nodiscard]] virtual std::shared_ptr GetPlatformEnvironment() = 0; + [[nodiscard]] virtual Audio::IAudioContext& GetAudioContext() = 0; + [[nodiscard]] virtual Ui::IUiContext& GetUiContext() = 0; + [[nodiscard]] virtual IPlatformEnvironment& GetPlatformEnvironment() = 0; virtual Localisation::LocalisationService& GetLocalisationService() = 0; virtual IObjectManager& GetObjectManager() = 0; virtual IObjectRepository& GetObjectRepository() = 0; @@ -184,8 +184,8 @@ namespace OpenRCT2 [[nodiscard]] std::unique_ptr CreateContext(); [[nodiscard]] std::unique_ptr CreateContext( - const std::shared_ptr& env, const std::shared_ptr& audioContext, - const std::shared_ptr& uiContext); + std::unique_ptr&& env, std::unique_ptr&& audioContext, + std::unique_ptr&& uiContext); [[nodiscard]] IContext* GetContext(); } // namespace OpenRCT2 diff --git a/src/openrct2/Game.cpp b/src/openrct2/Game.cpp index 5903d89d99..1922c45f16 100644 --- a/src/openrct2/Game.cpp +++ b/src/openrct2/Game.cpp @@ -366,7 +366,7 @@ void GameLoadInit() WindowUnfollowSprite(*mainWindow); } - auto windowManager = context->GetUiContext()->GetWindowManager(); + auto windowManager = context->GetUiContext().GetWindowManager(); auto& gameState = getGameState(); windowManager->SetMainView(gameState.savedView, gameState.savedViewZoom, gameState.savedViewRotation); @@ -474,8 +474,8 @@ void SaveGameCmd(u8string_view name /* = {} */) } else { - auto env = GetContext()->GetPlatformEnvironment(); - auto savePath = Path::Combine(env->GetDirectoryPath(DirBase::user, DirId::saves), u8string(name) + u8".park"); + auto& env = GetContext()->GetPlatformEnvironment(); + auto savePath = Path::Combine(env.GetDirectoryPath(DirBase::user, DirId::saves), u8string(name) + u8".park"); SaveGameWithName(savePath); } } @@ -517,12 +517,12 @@ static void LimitAutosaveCount(const size_t numberOfFilesToKeep, bool processLan size_t autosavesCount = 0; size_t numAutosavesToDelete = 0; - auto environment = GetContext()->GetPlatformEnvironment(); - auto folderDirectory = environment->GetDirectoryPath(DirBase::user, DirId::saves); + auto& environment = GetContext()->GetPlatformEnvironment(); + auto folderDirectory = environment.GetDirectoryPath(DirBase::user, DirId::saves); char const* fileFilter = "autosave_*.park"; if (processLandscapeFolder) { - folderDirectory = environment->GetDirectoryPath(DirBase::user, DirId::landscapes); + folderDirectory = environment.GetDirectoryPath(DirBase::user, DirId::landscapes); fileFilter = "autosave_*.park"; } @@ -595,8 +595,8 @@ void GameAutosave() int32_t autosavesToKeep = Config::Get().general.AutosaveAmount; LimitAutosaveCount(autosavesToKeep - 1, isInEditorMode()); - auto env = GetContext()->GetPlatformEnvironment(); - auto autosaveDir = Path::Combine(env->GetDirectoryPath(DirBase::user, subDirectory), u8"autosave"); + auto& env = GetContext()->GetPlatformEnvironment(); + auto autosaveDir = Path::Combine(env.GetDirectoryPath(DirBase::user, subDirectory), u8"autosave"); Path::CreateDirectory(autosaveDir); auto path = Path::Combine(autosaveDir, timeName); @@ -711,7 +711,7 @@ void GameLoadOrQuitNoSavePrompt() void StartSilentRecord() { std::string name = Path::Combine( - OpenRCT2::GetContext()->GetPlatformEnvironment()->GetDirectoryPath(OpenRCT2::DirBase::user), u8"debug_replay.parkrep"); + OpenRCT2::GetContext()->GetPlatformEnvironment().GetDirectoryPath(OpenRCT2::DirBase::user), u8"debug_replay.parkrep"); auto* replayManager = OpenRCT2::GetContext()->GetReplayManager(); if (replayManager->StartRecording(name, OpenRCT2::k_MaxReplayTicks, OpenRCT2::IReplayManager::RecordType::SILENT)) { diff --git a/src/openrct2/GameState.cpp b/src/openrct2/GameState.cpp index eb2e15442e..9b8c4feac0 100644 --- a/src/openrct2/GameState.cpp +++ b/src/openrct2/GameState.cpp @@ -111,7 +111,7 @@ namespace OpenRCT2 if (GameIsNotPaused() && gPreviewingTitleSequenceInGame) { - auto player = GetContext()->GetUiContext()->GetTitleSequencePlayer(); + auto player = GetContext()->GetUiContext().GetTitleSequencePlayer(); if (player != nullptr) { player->Update(); diff --git a/src/openrct2/ReplayManager.cpp b/src/openrct2/ReplayManager.cpp index dae22be659..f9729fc1c6 100644 --- a/src/openrct2/ReplayManager.cpp +++ b/src/openrct2/ReplayManager.cpp @@ -413,7 +413,7 @@ namespace OpenRCT2 // If there are difference write a log to the desyncs folder if (res != cmpData.spriteChanges.end()) { - std::string outputPath = GetContext()->GetPlatformEnvironment()->GetDirectoryPath( + std::string outputPath = GetContext()->GetPlatformEnvironment().GetDirectoryPath( DirBase::user, DirId::desyncLogs); char uniqueFileName[128] = {}; snprintf(uniqueFileName, sizeof(uniqueFileName), "replay_desync_%u.txt", currentTicks); @@ -624,7 +624,7 @@ namespace OpenRCT2 fileName += ".parkrep"; } - std::string outPath = GetContext()->GetPlatformEnvironment()->GetDirectoryPath( + std::string outPath = GetContext()->GetPlatformEnvironment().GetDirectoryPath( DirBase::user, DirId::replayRecordings); std::string outFile = Path::Combine(outPath, fileName); diff --git a/src/openrct2/actions/MapChangeSizeAction.cpp b/src/openrct2/actions/MapChangeSizeAction.cpp index 754c2bb424..ed60cbf00a 100644 --- a/src/openrct2/actions/MapChangeSizeAction.cpp +++ b/src/openrct2/actions/MapChangeSizeAction.cpp @@ -83,8 +83,8 @@ GameActions::Result MapChangeSizeAction::Execute() const } auto* ctx = OpenRCT2::GetContext(); - auto uiContext = ctx->GetUiContext(); - auto* windowManager = uiContext->GetWindowManager(); + auto& uiContext = ctx->GetUiContext(); + auto* windowManager = uiContext.GetWindowManager(); OpenRCT2::Park::UpdateSize(gameState); windowManager->BroadcastIntent(Intent(INTENT_ACTION_MAP)); diff --git a/src/openrct2/audio/Audio.cpp b/src/openrct2/audio/Audio.cpp index 450185930f..9505c952ab 100644 --- a/src/openrct2/audio/Audio.cpp +++ b/src/openrct2/audio/Audio.cpp @@ -76,15 +76,15 @@ namespace OpenRCT2::Audio void Init() { - auto audioContext = GetContext()->GetAudioContext(); + auto& audioContext = GetContext()->GetAudioContext(); if (Config::Get().sound.Device.empty()) { - audioContext->SetOutputDevice(""); + audioContext.SetOutputDevice(""); _currentAudioDevice = 0; } else { - audioContext->SetOutputDevice(Config::Get().sound.Device); + audioContext.SetOutputDevice(Config::Get().sound.Device); PopulateDevices(); for (int32_t i = 0; i < GetDeviceCount(); i++) @@ -115,8 +115,8 @@ namespace OpenRCT2::Audio void PopulateDevices() { - auto audioContext = OpenRCT2::GetContext()->GetAudioContext(); - std::vector devices = audioContext->GetOutputDevices(); + auto& audioContext = OpenRCT2::GetContext()->GetAudioContext(); + std::vector devices = audioContext.GetOutputDevices(); // Replace blanks with localised unknown string for (auto& device : devices) @@ -252,8 +252,8 @@ namespace OpenRCT2::Audio static bool IsRCT1TitleMusicAvailable() { - auto env = GetContext()->GetPlatformEnvironment(); - auto rct1path = env->GetDirectoryPath(DirBase::rct1); + auto& env = GetContext()->GetPlatformEnvironment(); + auto rct1path = env.GetDirectoryPath(DirBase::rct1); return !rct1path.empty(); } @@ -453,8 +453,8 @@ namespace OpenRCT2::Audio static IAudioMixer* GetMixer() { - auto audioContext = GetContext()->GetAudioContext(); - return audioContext->GetMixer(); + auto& audioContext = GetContext()->GetAudioContext(); + return audioContext.GetMixer(); } std::shared_ptr CreateAudioChannel( diff --git a/src/openrct2/command_line/RootCommands.cpp b/src/openrct2/command_line/RootCommands.cpp index 4f5b964c43..ac18b1badc 100644 --- a/src/openrct2/command_line/RootCommands.cpp +++ b/src/openrct2/command_line/RootCommands.cpp @@ -401,7 +401,7 @@ static exitcode_t HandleCommandScanObjects([[maybe_unused]] CommandLineArgEnumer gOpenRCT2NoGraphics = true; auto context = OpenRCT2::CreateContext(); - auto env = context->GetPlatformEnvironment(); + auto& env = context->GetPlatformEnvironment(); auto objectRepository = CreateObjectRepository(env); objectRepository->Construct(Config::Get().general.Language); return EXITCODE_OK; diff --git a/src/openrct2/config/Config.cpp b/src/openrct2/config/Config.cpp index 23dd2f48fe..a5ced5da04 100644 --- a/src/openrct2/config/Config.cpp +++ b/src/openrct2/config/Config.cpp @@ -817,8 +817,8 @@ namespace OpenRCT2::Config u8string GetDefaultPath() { - auto env = GetContext()->GetPlatformEnvironment(); - return Path::Combine(env->GetDirectoryPath(DirBase::user), u8"config.ini"); + auto& env = GetContext()->GetPlatformEnvironment(); + return Path::Combine(env.GetDirectoryPath(DirBase::user), u8"config.ini"); } bool SaveToPath(u8string_view path) @@ -846,10 +846,10 @@ namespace OpenRCT2::Config return false; } - auto uiContext = GetContext()->GetUiContext(); - if (!uiContext->HasFilePicker()) + auto& uiContext = GetContext()->GetUiContext(); + if (!uiContext.HasFilePicker()) { - uiContext->ShowMessageBox(LanguageGetString(STR_NEEDS_RCT2_FILES_MANUAL)); + uiContext.ShowMessageBox(LanguageGetString(STR_NEEDS_RCT2_FILES_MANUAL)); return false; } @@ -858,18 +858,18 @@ namespace OpenRCT2::Config const char* g1DatPath = PATH_SEPARATOR "Data" PATH_SEPARATOR "g1.dat"; while (true) { - uiContext->ShowMessageBox(LanguageGetString(STR_NEEDS_RCT2_FILES)); + uiContext.ShowMessageBox(LanguageGetString(STR_NEEDS_RCT2_FILES)); std::string gog = LanguageGetString(STR_OWN_ON_GOG); std::string hdd = LanguageGetString(STR_INSTALLED_ON_HDD); std::vector options; std::string chosenOption; - if (uiContext->HasMenuSupport()) + if (uiContext.HasMenuSupport()) { options.push_back(hdd); options.push_back(gog); - int optionIndex = uiContext->ShowMenuDialog( + int optionIndex = uiContext.ShowMenuDialog( options, LanguageGetString(STR_OPENRCT2_SETUP), LanguageGetString(STR_WHICH_APPLIES_BEST)); if (optionIndex < 0 || static_cast(optionIndex) >= options.size()) { @@ -889,7 +889,7 @@ namespace OpenRCT2::Config std::vector possibleInstallPaths{}; if (chosenOption == hdd) { - possibleInstallPaths.emplace_back(uiContext->ShowDirectoryDialog(LanguageGetString(STR_PICK_RCT2_DIR))); + possibleInstallPaths.emplace_back(uiContext.ShowDirectoryDialog(LanguageGetString(STR_PICK_RCT2_DIR))); } else if (chosenOption == gog) { @@ -897,16 +897,16 @@ namespace OpenRCT2::Config std::string dummy; if (!Platform::FindApp("innoextract", &dummy)) { - uiContext->ShowMessageBox(LanguageGetString(STR_INSTALL_INNOEXTRACT)); + uiContext.ShowMessageBox(LanguageGetString(STR_INSTALL_INNOEXTRACT)); return false; } const std::string dest = Path::Combine( - GetContext()->GetPlatformEnvironment()->GetDirectoryPath(DirBase::config), "rct2"); + GetContext()->GetPlatformEnvironment().GetDirectoryPath(DirBase::config), "rct2"); while (true) { - uiContext->ShowMessageBox(LanguageGetString(STR_PLEASE_SELECT_GOG_INSTALLER)); + uiContext.ShowMessageBox(LanguageGetString(STR_PLEASE_SELECT_GOG_INSTALLER)); auto gogPath = SelectGogInstaller(); if (gogPath.empty()) { @@ -914,12 +914,12 @@ namespace OpenRCT2::Config return false; } - uiContext->ShowMessageBox(LanguageGetString(STR_THIS_WILL_TAKE_A_FEW_MINUTES)); + uiContext.ShowMessageBox(LanguageGetString(STR_THIS_WILL_TAKE_A_FEW_MINUTES)); if (ExtractGogInstaller(gogPath, dest)) break; - uiContext->ShowMessageBox(LanguageGetString(STR_NOT_THE_GOG_INSTALLER)); + uiContext.ShowMessageBox(LanguageGetString(STR_NOT_THE_GOG_INSTALLER)); } // New installer extracts to ‘dest’, old installer installs in ‘dest/app’. @@ -940,7 +940,7 @@ namespace OpenRCT2::Config } } - uiContext->ShowMessageBox(FormatStringIDLegacy(STR_COULD_NOT_FIND_AT_PATH, &g1DatPath)); + uiContext.ShowMessageBox(FormatStringIDLegacy(STR_COULD_NOT_FIND_AT_PATH, &g1DatPath)); } } catch (const std::exception& ex) diff --git a/src/openrct2/drawing/Drawing.Sprite.cpp b/src/openrct2/drawing/Drawing.Sprite.cpp index acfa78724e..1884d44240 100644 --- a/src/openrct2/drawing/Drawing.Sprite.cpp +++ b/src/openrct2/drawing/Drawing.Sprite.cpp @@ -495,8 +495,8 @@ bool GfxLoadG1(const IPlatformEnvironment& env) LOG_FATAL("Unable to load g1 graphics"); if (!gOpenRCT2Headless) { - auto uiContext = GetContext()->GetUiContext(); - uiContext->ShowMessageBox("Unable to load g1.dat. Your RollerCoaster Tycoon 2 path may be incorrectly set."); + auto& uiContext = GetContext()->GetUiContext(); + uiContext.ShowMessageBox("Unable to load g1.dat. Your RollerCoaster Tycoon 2 path may be incorrectly set."); } return false; } @@ -529,9 +529,9 @@ void GfxUnloadCsg() static bool GfxLoadOpenRCT2Gx(std::string filename, Gx& target, size_t expectedNumItems) { LOG_VERBOSE("GfxLoadOpenRCT2Gx(\"%s\")", filename.c_str()); - auto env = GetContext()->GetPlatformEnvironment(); + auto& env = GetContext()->GetPlatformEnvironment(); - std::string path = Path::Combine(env->GetDirectoryPath(DirBase::openrct2), filename.c_str()); + std::string path = Path::Combine(env.GetDirectoryPath(DirBase::openrct2), filename.c_str()); try { @@ -555,9 +555,9 @@ static bool GfxLoadOpenRCT2Gx(std::string filename, Gx& target, size_t expectedN if (!gOpenRCT2Headless) { - auto uiContext = GetContext()->GetUiContext(); - uiContext->ShowMessageBox(errorMessage); - uiContext->ShowMessageBox( + auto& uiContext = GetContext()->GetUiContext(); + uiContext.ShowMessageBox(errorMessage); + uiContext.ShowMessageBox( "Warning: You may experience graphical glitches if you continue. It's recommended " "that you update " + filename + " if you're seeing this message"); @@ -586,8 +586,8 @@ static bool GfxLoadOpenRCT2Gx(std::string filename, Gx& target, size_t expectedN LOG_FATAL("Unable to load %s graphics", filename.c_str()); if (!gOpenRCT2Headless) { - auto uiContext = GetContext()->GetUiContext(); - uiContext->ShowMessageBox("Unable to load " + filename); + auto& uiContext = GetContext()->GetUiContext(); + uiContext.ShowMessageBox("Unable to load " + filename); } } return false; diff --git a/src/openrct2/drawing/IDrawingEngine.h b/src/openrct2/drawing/IDrawingEngine.h index 9b23bfe685..94c7d66270 100644 --- a/src/openrct2/drawing/IDrawingEngine.h +++ b/src/openrct2/drawing/IDrawingEngine.h @@ -82,8 +82,8 @@ namespace OpenRCT2::Drawing virtual ~IDrawingEngineFactory() { } - [[nodiscard]] virtual std::unique_ptr - Create(DrawingEngine type, const std::shared_ptr& uiContext) = 0; + [[nodiscard]] virtual std::unique_ptr Create(DrawingEngine type, OpenRCT2::Ui::IUiContext& uiContext) + = 0; }; struct IWeatherDrawer diff --git a/src/openrct2/drawing/NewDrawing.cpp b/src/openrct2/drawing/NewDrawing.cpp index e465aac3d6..6566ac16bb 100644 --- a/src/openrct2/drawing/NewDrawing.cpp +++ b/src/openrct2/drawing/NewDrawing.cpp @@ -68,8 +68,8 @@ void DrawingEngineResize() auto drawingEngine = context->GetDrawingEngine(); if (drawingEngine != nullptr) { - auto uiContext = context->GetUiContext(); - drawingEngine->Resize(uiContext->GetWidth(), uiContext->GetHeight()); + auto& uiContext = context->GetUiContext(); + drawingEngine->Resize(uiContext.GetWidth(), uiContext.GetHeight()); } } } diff --git a/src/openrct2/drawing/Weather.cpp b/src/openrct2/drawing/Weather.cpp index 53ab7c089f..7612b75373 100644 --- a/src/openrct2/drawing/Weather.cpp +++ b/src/openrct2/drawing/Weather.cpp @@ -73,8 +73,8 @@ void DrawWeather(RenderTarget& rt, IWeatherDrawer* weatherDrawer) drawFunc = DrawSnowFunctions[EnumValue(weatherLevel)]; } - auto uiContext = GetContext()->GetUiContext(); - uiContext->DrawWeatherAnimation(weatherDrawer, rt, drawFunc); + auto& uiContext = GetContext()->GetUiContext(); + uiContext.DrawWeatherAnimation(weatherDrawer, rt, drawFunc); } /** diff --git a/src/openrct2/drawing/X8DrawingEngine.cpp b/src/openrct2/drawing/X8DrawingEngine.cpp index b9218ac5f8..d5d5be9510 100644 --- a/src/openrct2/drawing/X8DrawingEngine.cpp +++ b/src/openrct2/drawing/X8DrawingEngine.cpp @@ -118,7 +118,7 @@ void X8WeatherDrawer::Restore(RenderTarget& rt) #pragma GCC diagnostic ignored "-Wsuggest-final-methods" #endif -X8DrawingEngine::X8DrawingEngine([[maybe_unused]] const std::shared_ptr& uiContext) +X8DrawingEngine::X8DrawingEngine([[maybe_unused]] Ui::IUiContext& uiContext) { _drawingContext = new X8DrawingContext(this); _mainRT.DrawingEngine = this; diff --git a/src/openrct2/drawing/X8DrawingEngine.h b/src/openrct2/drawing/X8DrawingEngine.h index 2b6267b2d9..6ed83b1f1a 100644 --- a/src/openrct2/drawing/X8DrawingEngine.h +++ b/src/openrct2/drawing/X8DrawingEngine.h @@ -72,7 +72,7 @@ namespace OpenRCT2 InvalidationGrid _invalidationGrid; public: - explicit X8DrawingEngine(const std::shared_ptr& uiContext); + explicit X8DrawingEngine(Ui::IUiContext& uiContext); #ifdef __WARN_SUGGEST_FINAL_METHODS__ #pragma GCC diagnostic push diff --git a/src/openrct2/interface/InteractiveConsole.cpp b/src/openrct2/interface/InteractiveConsole.cpp index 04982f35b2..14cda2b0ea 100644 --- a/src/openrct2/interface/InteractiveConsole.cpp +++ b/src/openrct2/interface/InteractiveConsole.cpp @@ -1305,8 +1305,8 @@ static void ConsoleCommandLoadPark([[maybe_unused]] InteractiveConsole& console, if (String::indexOf(argv[0].c_str(), '/') == SIZE_MAX && String::indexOf(argv[0].c_str(), '\\') == SIZE_MAX) { // no / or \ was included. File should be in save dir. - auto env = OpenRCT2::GetContext()->GetPlatformEnvironment(); - auto directory = env->GetDirectoryPath(OpenRCT2::DirBase::user, OpenRCT2::DirId::saves); + auto& env = OpenRCT2::GetContext()->GetPlatformEnvironment(); + auto directory = env.GetDirectoryPath(OpenRCT2::DirBase::user, OpenRCT2::DirId::saves); savePath = Path::Combine(directory, argv[0]); } else @@ -1378,7 +1378,7 @@ static void ConsoleCommandReplayStartRecord(InteractiveConsole& console, const a { name += ".parkrep"; } - std::string outPath = OpenRCT2::GetContext()->GetPlatformEnvironment()->GetDirectoryPath( + std::string outPath = OpenRCT2::GetContext()->GetPlatformEnvironment().GetDirectoryPath( OpenRCT2::DirBase::user, OpenRCT2::DirId::replayRecordings); name = Path::Combine(outPath, name); @@ -1504,7 +1504,7 @@ static void ConsoleCommandReplayNormalise(InteractiveConsole& console, const arg { outputFile += ".parkrep"; } - std::string outPath = OpenRCT2::GetContext()->GetPlatformEnvironment()->GetDirectoryPath( + std::string outPath = OpenRCT2::GetContext()->GetPlatformEnvironment().GetDirectoryPath( OpenRCT2::DirBase::user, OpenRCT2::DirId::replayRecordings); outputFile = Path::Combine(outPath, outputFile); diff --git a/src/openrct2/interface/Screenshot.cpp b/src/openrct2/interface/Screenshot.cpp index 5b4d6d3d25..939454b3b8 100644 --- a/src/openrct2/interface/Screenshot.cpp +++ b/src/openrct2/interface/Screenshot.cpp @@ -117,8 +117,8 @@ static std::string ScreenshotGetParkName() static std::string ScreenshotGetDirectory() { - auto env = GetContext()->GetPlatformEnvironment(); - return env->GetDirectoryPath(DirBase::user, DirId::screenshots); + auto& env = GetContext()->GetPlatformEnvironment(); + return env.GetDirectoryPath(DirBase::user, DirId::screenshots); } static std::pair ScreenshotGetDateTime() diff --git a/src/openrct2/interface/Window.cpp b/src/openrct2/interface/Window.cpp index 0c682d8a23..4174d0d144 100644 --- a/src/openrct2/interface/Window.cpp +++ b/src/openrct2/interface/Window.cpp @@ -987,6 +987,6 @@ static constexpr float kWindowScrollLocations[][2] = { // TODO: declared in WindowManager.h; move when refactors continue Ui::IWindowManager* Ui::GetWindowManager() { - return GetContext()->GetUiContext()->GetWindowManager(); + return GetContext()->GetUiContext().GetWindowManager(); } } // namespace OpenRCT2 diff --git a/src/openrct2/localisation/LocalisationService.cpp b/src/openrct2/localisation/LocalisationService.cpp index d7de72a6be..0403f99ab0 100644 --- a/src/openrct2/localisation/LocalisationService.cpp +++ b/src/openrct2/localisation/LocalisationService.cpp @@ -24,7 +24,7 @@ using namespace OpenRCT2::Localisation; static constexpr uint16_t kBaseObjectStringID = 0x2000; static constexpr uint16_t kMaxObjectCachedStrings = 0x5000 - kBaseObjectStringID; -LocalisationService::LocalisationService(const std::shared_ptr& env) +LocalisationService::LocalisationService(IPlatformEnvironment& env) : _env(env) { for (StringId stringId = kBaseObjectStringID + kMaxObjectCachedStrings; stringId >= kBaseObjectStringID; stringId--) @@ -70,7 +70,7 @@ const char* LocalisationService::GetString(StringId id) const std::string LocalisationService::GetLanguagePath(uint32_t languageId) const { auto locale = std::string(LanguagesDescriptors[languageId].locale); - auto languageDirectory = _env->GetDirectoryPath(DirBase::openrct2, DirId::languages); + auto languageDirectory = _env.GetDirectoryPath(DirBase::openrct2, DirId::languages); auto languagePath = Path::Combine(languageDirectory, locale + u8".txt"); return languagePath; } diff --git a/src/openrct2/localisation/LocalisationService.h b/src/openrct2/localisation/LocalisationService.h index eb63644d77..62c798c964 100644 --- a/src/openrct2/localisation/LocalisationService.h +++ b/src/openrct2/localisation/LocalisationService.h @@ -30,7 +30,7 @@ namespace OpenRCT2::Localisation class LocalisationService { private: - const std::shared_ptr _env; + IPlatformEnvironment& _env; int32_t _currentLanguage{}; bool _useTrueTypeFont{}; std::vector _languageOrder; @@ -53,7 +53,7 @@ namespace OpenRCT2::Localisation _useTrueTypeFont = value; } - LocalisationService(const std::shared_ptr& env); + LocalisationService(IPlatformEnvironment& env); ~LocalisationService(); const char* GetString(StringId id) const; diff --git a/src/openrct2/network/NetworkBase.cpp b/src/openrct2/network/NetworkBase.cpp index 86bb4d857d..620d52e66c 100644 --- a/src/openrct2/network/NetworkBase.cpp +++ b/src/openrct2/network/NetworkBase.cpp @@ -1018,8 +1018,8 @@ void NetworkBase::SaveGroups() { if (GetMode() == NETWORK_MODE_SERVER) { - auto env = GetContext().GetPlatformEnvironment(); - auto path = Path::Combine(env->GetDirectoryPath(DirBase::user), u8"groups.json"); + auto& env = GetContext().GetPlatformEnvironment(); + auto path = Path::Combine(env.GetDirectoryPath(DirBase::user), u8"groups.json"); json_t jsonGroups = json_t::array(); for (auto& group : group_list) @@ -1078,8 +1078,8 @@ void NetworkBase::LoadGroups() { group_list.clear(); - auto env = GetContext().GetPlatformEnvironment(); - auto path = Path::Combine(env->GetDirectoryPath(DirBase::user), u8"groups.json"); + auto& env = GetContext().GetPlatformEnvironment(); + auto path = Path::Combine(env.GetDirectoryPath(DirBase::user), u8"groups.json"); json_t jsonGroupConfig; if (File::Exists(path)) @@ -1165,8 +1165,8 @@ void NetworkBase::AppendLog(std::ostream& fs, std::string_view s) void NetworkBase::BeginChatLog() { - auto env = GetContext().GetPlatformEnvironment(); - auto directory = env->GetDirectoryPath(DirBase::user, DirId::chatLogs); + auto& env = GetContext().GetPlatformEnvironment(); + auto directory = env.GetDirectoryPath(DirBase::user, DirId::chatLogs); _chatLogPath = BeginLog(directory, "", _chatLogFilenameFormat); _chat_log_fs.open(fs::u8path(_chatLogPath), std::ios::out | std::ios::app); } @@ -1186,8 +1186,8 @@ void NetworkBase::CloseChatLog() void NetworkBase::BeginServerLog() { - auto env = GetContext().GetPlatformEnvironment(); - auto directory = env->GetDirectoryPath(DirBase::user, DirId::serverLogs); + auto& env = GetContext().GetPlatformEnvironment(); + auto directory = env.GetDirectoryPath(DirBase::user, DirId::serverLogs); _serverLogPath = BeginLog(directory, ServerName, _serverLogFilenameFormat); _server_log_fs.open(fs::u8path(_serverLogPath), std::ios::out | std::ios::app | std::ios::binary); @@ -2556,7 +2556,7 @@ void NetworkBase::Client_Handle_GAMESTATE(NetworkConnection& connection, Network { GameStateCompareData cmpData = snapshots->Compare(serverSnapshot, *desyncSnapshot); - std::string outputPath = GetContext().GetPlatformEnvironment()->GetDirectoryPath(DirBase::user, DirId::desyncLogs); + std::string outputPath = GetContext().GetPlatformEnvironment().GetDirectoryPath(DirBase::user, DirId::desyncLogs); Path::CreateDirectory(outputPath); @@ -4010,8 +4010,8 @@ void NetworkAppendServerLog(const utf8* text) static u8string NetworkGetKeysDirectory() { - auto env = GetContext()->GetPlatformEnvironment(); - return Path::Combine(env->GetDirectoryPath(DirBase::user), u8"keys"); + auto& env = GetContext()->GetPlatformEnvironment(); + return Path::Combine(env.GetDirectoryPath(DirBase::user), u8"keys"); } static u8string NetworkGetPrivateKeyPath(u8string_view playerName) diff --git a/src/openrct2/network/NetworkUser.cpp b/src/openrct2/network/NetworkUser.cpp index 27dab0dee8..c7beec5dcb 100644 --- a/src/openrct2/network/NetworkUser.cpp +++ b/src/openrct2/network/NetworkUser.cpp @@ -212,8 +212,8 @@ NetworkUser* NetworkUserManager::GetOrAddUser(const std::string& hash) u8string NetworkUserManager::GetStorePath() { - auto env = OpenRCT2::GetContext()->GetPlatformEnvironment(); - return Path::Combine(env->GetDirectoryPath(OpenRCT2::DirBase::user), kUserStoreFilename); + auto& env = OpenRCT2::GetContext()->GetPlatformEnvironment(); + return Path::Combine(env.GetDirectoryPath(OpenRCT2::DirBase::user), kUserStoreFilename); } #endif diff --git a/src/openrct2/network/ServerList.cpp b/src/openrct2/network/ServerList.cpp index 3670d0ad30..d68179bc32 100644 --- a/src/openrct2/network/ServerList.cpp +++ b/src/openrct2/network/ServerList.cpp @@ -189,8 +189,8 @@ std::vector ServerList::ReadFavourites() const std::vector entries; try { - auto env = GetContext()->GetPlatformEnvironment(); - auto path = env->GetFilePath(PathId::networkServers); + auto& env = GetContext()->GetPlatformEnvironment(); + auto path = env.GetFilePath(PathId::networkServers); if (File::Exists(path)) { auto fs = FileStream(path, FileMode::open); @@ -242,8 +242,8 @@ bool ServerList::WriteFavourites(const std::vector& entries) co { LOG_VERBOSE("server_list_write(%d, 0x%p)", entries.size(), entries.data()); - auto env = GetContext()->GetPlatformEnvironment(); - auto path = Path::Combine(env->GetDirectoryPath(DirBase::user), u8"servers.cfg"); + auto& env = GetContext()->GetPlatformEnvironment(); + auto path = Path::Combine(env.GetDirectoryPath(DirBase::user), u8"servers.cfg"); try { diff --git a/src/openrct2/object/AudioSampleTable.cpp b/src/openrct2/object/AudioSampleTable.cpp index 7eaf53e3e9..40799b9548 100644 --- a/src/openrct2/object/AudioSampleTable.cpp +++ b/src/openrct2/object/AudioSampleTable.cpp @@ -110,7 +110,6 @@ void AudioSampleTable::LoadFrom(const AudioSampleTable& table, size_t sourceStar void AudioSampleTable::Load() { - auto audioContext = GetContext()->GetAudioContext(); for (size_t i = 0; i < _entries.size(); i++) { auto& entry = _entries[i]; @@ -158,14 +157,14 @@ IAudioSource* AudioSampleTable::LoadSample(uint32_t index) const auto stream = entry.Asset->GetStream(); if (stream != nullptr) { - auto audioContext = GetContext()->GetAudioContext(); + auto& audioContext = GetContext()->GetAudioContext(); if (entry.PathIndex) { - result = audioContext->CreateStreamFromCSS(std::move(stream), *entry.PathIndex); + result = audioContext.CreateStreamFromCSS(std::move(stream), *entry.PathIndex); } else { - result = audioContext->CreateStreamFromWAV(std::move(stream)); + result = audioContext.CreateStreamFromWAV(std::move(stream)); } } } diff --git a/src/openrct2/object/ImageTable.cpp b/src/openrct2/object/ImageTable.cpp index f6b1645e06..f2e7a723cd 100644 --- a/src/openrct2/object/ImageTable.cpp +++ b/src/openrct2/object/ImageTable.cpp @@ -379,8 +379,8 @@ std::vector ImageTable::ParseRange(std::string s) std::string ImageTable::FindLegacyObject(const std::string& name) { - const auto env = GetContext()->GetPlatformEnvironment(); - auto objectsPath = env->GetDirectoryPath(DirBase::rct2, DirId::objects); + const auto& env = GetContext()->GetPlatformEnvironment(); + auto objectsPath = env.GetDirectoryPath(DirBase::rct2, DirId::objects); auto objectPath = Path::Combine(objectsPath, name); if (File::Exists(objectPath)) { diff --git a/src/openrct2/object/MusicObject.cpp b/src/openrct2/object/MusicObject.cpp index 4801e1df5c..3a23520dd6 100644 --- a/src/openrct2/object/MusicObject.cpp +++ b/src/openrct2/object/MusicObject.cpp @@ -47,13 +47,13 @@ void MusicObject::Load() } // Load metadata of samples - auto audioContext = GetContext()->GetAudioContext(); + auto& audioContext = GetContext()->GetAudioContext(); for (auto& track : _tracks) { auto stream = track.Asset.GetStream(); if (stream != nullptr) { - auto source = audioContext->CreateStreamFromWAV(std::move(stream)); + auto source = audioContext.CreateStreamFromWAV(std::move(stream)); if (source != nullptr) { track.BytesPerTick = source->GetBytesPerSecond() / 40; @@ -226,8 +226,8 @@ ObjectAsset MusicObject::GetAsset(IReadObjectContext& context, std::string_view { if (path.find("$RCT2:DATA/") == 0) { - auto env = GetContext()->GetPlatformEnvironment(); - auto path2 = env->FindFile(DirBase::rct2, DirId::data, path.substr(11)); + auto& env = GetContext()->GetPlatformEnvironment(); + auto path2 = env.FindFile(DirBase::rct2, DirId::data, path.substr(11)); return ObjectAsset(path2); } diff --git a/src/openrct2/object/ObjectFactory.cpp b/src/openrct2/object/ObjectFactory.cpp index 41f20fdbc2..d9f3bbcf9f 100644 --- a/src/openrct2/object/ObjectFactory.cpp +++ b/src/openrct2/object/ObjectFactory.cpp @@ -508,8 +508,8 @@ namespace OpenRCT2::ObjectFactory static bool isUsingClassic() { - auto env = OpenRCT2::GetContext()->GetPlatformEnvironment(); - return env->IsUsingClassic(); + auto& env = OpenRCT2::GetContext()->GetPlatformEnvironment(); + return env.IsUsingClassic(); } std::unique_ptr CreateObjectFromJson( diff --git a/src/openrct2/object/ObjectRepository.cpp b/src/openrct2/object/ObjectRepository.cpp index ad54541eae..7adba16cba 100644 --- a/src/openrct2/object/ObjectRepository.cpp +++ b/src/openrct2/object/ObjectRepository.cpp @@ -177,16 +177,16 @@ private: class ObjectRepository final : public IObjectRepository { - std::shared_ptr const _env; + IPlatformEnvironment& _env; ObjectFileIndex const _fileIndex; std::vector _items; ObjectIdentifierMap _newItemMap; ObjectEntryMap _itemMap; public: - explicit ObjectRepository(const std::shared_ptr& env) + explicit ObjectRepository(IPlatformEnvironment& env) : _env(env) - , _fileIndex(*this, *env) + , _fileIndex(*this, env) { } @@ -586,7 +586,7 @@ private: std::string GetPathForNewObject(ObjectGeneration generation, std::string_view name) { // Get object directory and create it if it doesn't exist - auto userObjPath = _env->GetDirectoryPath(DirBase::user, DirId::objects); + auto userObjPath = _env.GetDirectoryPath(DirBase::user, DirId::objects); Path::CreateDirectory(userObjPath); // Find a unique file name @@ -657,7 +657,7 @@ private: } }; -std::unique_ptr CreateObjectRepository(const std::shared_ptr& env) +std::unique_ptr CreateObjectRepository(IPlatformEnvironment& env) { return std::make_unique(env); } diff --git a/src/openrct2/object/ObjectRepository.h b/src/openrct2/object/ObjectRepository.h index 855ab7ce6a..d71c5abab6 100644 --- a/src/openrct2/object/ObjectRepository.h +++ b/src/openrct2/object/ObjectRepository.h @@ -101,8 +101,7 @@ struct IObjectRepository virtual void ExportPackedObject(OpenRCT2::IStream* stream) = 0; }; -[[nodiscard]] std::unique_ptr CreateObjectRepository( - const std::shared_ptr& env); +[[nodiscard]] std::unique_ptr CreateObjectRepository(OpenRCT2::IPlatformEnvironment& env); [[nodiscard]] bool IsObjectCustom(const ObjectRepositoryItem* object); diff --git a/src/openrct2/object/ResourceTable.cpp b/src/openrct2/object/ResourceTable.cpp index a524365f10..f667811c86 100644 --- a/src/openrct2/object/ResourceTable.cpp +++ b/src/openrct2/object/ResourceTable.cpp @@ -71,6 +71,7 @@ ResourceTable::SourceInfo ResourceTable::ParseSource(std::string_view source) } } + auto& env = GetContext()->GetPlatformEnvironment(); if (String::startsWith(base, "$LGX:")) { info.Kind = SourceKind::Gx; @@ -78,35 +79,30 @@ ResourceTable::SourceInfo ResourceTable::ParseSource(std::string_view source) } else if (String::startsWith(base, "$G1")) { - auto env = GetContext()->GetPlatformEnvironment(); - auto dataPath = env->GetDirectoryPath(DirBase::rct2, DirId::data); + auto dataPath = env.GetDirectoryPath(DirBase::rct2, DirId::data); info.Kind = SourceKind::G1; // info.Path = env->FindFile(DirBase::rct2, DirId::data, "g1.dat"); } else if (String::startsWith(base, "$CSG")) { - auto env = GetContext()->GetPlatformEnvironment(); - auto dataPath = env->GetDirectoryPath(DirBase::rct2, DirId::data); + auto dataPath = env.GetDirectoryPath(DirBase::rct2, DirId::data); info.Kind = SourceKind::Csg; // info.Path = env->FindFile(DirBase::rct2, DirId::data, "g1.dat"); } else if (String::startsWith(base, "$RCT1:DATA/")) { - auto env = GetContext()->GetPlatformEnvironment(); info.Kind = SourceKind::Data; - info.Path = env->FindFile(DirBase::rct1, DirId::data, fileName); + info.Path = env.FindFile(DirBase::rct1, DirId::data, fileName); } else if (String::startsWith(base, "$RCT2:DATA/")) { - auto env = GetContext()->GetPlatformEnvironment(); info.Kind = SourceKind::Data; - info.Path = env->FindFile(DirBase::rct2, DirId::data, fileName); + info.Path = env.FindFile(DirBase::rct2, DirId::data, fileName); } else if (String::startsWith(base, "$RCT2:OBJDATA/")) { - auto env = GetContext()->GetPlatformEnvironment(); info.Kind = SourceKind::ObjData; - info.Path = env->FindFile(DirBase::rct2, DirId::objects, fileName); + info.Path = env.FindFile(DirBase::rct2, DirId::objects, fileName); } else if (!String::startsWith(base, "$")) { diff --git a/src/openrct2/paint/Painter.cpp b/src/openrct2/paint/Painter.cpp index 3d3856c535..6f8b411249 100644 --- a/src/openrct2/paint/Painter.cpp +++ b/src/openrct2/paint/Painter.cpp @@ -29,7 +29,7 @@ using namespace OpenRCT2::Drawing; using namespace OpenRCT2::Paint; using namespace OpenRCT2::Ui; -Painter::Painter(const std::shared_ptr& uiContext) +Painter::Painter(IUiContext& uiContext) : _uiContext(uiContext) { } @@ -49,7 +49,7 @@ void Painter::Paint(IDrawingEngine& de) de.PaintWindows(); UpdatePaletteEffects(); - _uiContext->Draw(*rt); + _uiContext.Draw(*rt); GfxDrawPickedUpPeep(*rt); GfxInvalidatePickedUpPeep(); @@ -79,7 +79,7 @@ void Painter::Paint(IDrawingEngine& de) void Painter::PaintReplayNotice(RenderTarget& rt, const char* text) { - ScreenCoordsXY screenCoords(_uiContext->GetWidth() / 2, _uiContext->GetHeight() - 44); + ScreenCoordsXY screenCoords(_uiContext.GetWidth() / 2, _uiContext.GetHeight() - 44); char buffer[64]{}; FormatStringToBuffer(buffer, sizeof(buffer), "{OUTLINE}{RED}{STRING}", text); @@ -115,7 +115,7 @@ void Painter::PaintFPS(RenderTarget& rt) const int32_t stringWidth = GfxGetStringWidth(buffer, FontStyle::Medium); // Figure out where counter should be rendered - ScreenCoordsXY screenCoords(_uiContext->GetWidth() / 2, 2); + ScreenCoordsXY screenCoords(_uiContext.GetWidth() / 2, 2); screenCoords.x = screenCoords.x - (stringWidth / 2); // Move counter below toolbar if buttons are centred diff --git a/src/openrct2/paint/Painter.h b/src/openrct2/paint/Painter.h index 2b55783c26..8721c91996 100644 --- a/src/openrct2/paint/Painter.h +++ b/src/openrct2/paint/Painter.h @@ -35,7 +35,7 @@ namespace OpenRCT2 struct Painter final { private: - std::shared_ptr const _uiContext; + Ui::IUiContext& _uiContext; sfl::segmented_vector _paintSessionPool; std::vector _freePaintSessions; time_t _lastSecond = 0; @@ -43,7 +43,7 @@ namespace OpenRCT2 int32_t _frames = 0; public: - explicit Painter(const std::shared_ptr& uiContext); + explicit Painter(Ui::IUiContext& uiContext); void Paint(Drawing::IDrawingEngine& de); PaintSession* CreateSession(RenderTarget& rt, uint32_t viewFlags, uint8_t rotation); diff --git a/src/openrct2/platform/Crash.cpp b/src/openrct2/platform/Crash.cpp index 3c7d995302..3f7a2cc586 100644 --- a/src/openrct2/platform/Crash.cpp +++ b/src/openrct2/platform/Crash.cpp @@ -316,8 +316,8 @@ static bool OnCrash( static std::wstring GetDumpDirectory() { - auto env = GetContext()->GetPlatformEnvironment(); - auto crashPath = env->GetDirectoryPath(DirBase::user, DirId::crashDumps); + auto& env = GetContext()->GetPlatformEnvironment(); + auto crashPath = env.GetDirectoryPath(DirBase::user, DirId::crashDumps); auto result = String::toWideChar(crashPath); return result; diff --git a/src/openrct2/rct12/ScenarioPatcher.cpp b/src/openrct2/rct12/ScenarioPatcher.cpp index a51ba2e460..bf8f6eeffa 100644 --- a/src/openrct2/rct12/ScenarioPatcher.cpp +++ b/src/openrct2/rct12/ScenarioPatcher.cpp @@ -668,7 +668,6 @@ static void ApplyPathFixes(const json_t& scenarioPatch) static u8string getScenarioSHA256(u8string_view scenarioPath) { - auto env = OpenRCT2::GetContext()->GetPlatformEnvironment(); auto scenarioData = OpenRCT2::File::ReadAllBytes(scenarioPath); #ifdef DISABLE_NETWORK auto scenarioStringHash = picosha2::hash256_hex_string(scenarioData); @@ -682,8 +681,8 @@ static u8string getScenarioSHA256(u8string_view scenarioPath) static u8string GetPatchFileName(u8string_view scenarioHash) { - auto env = OpenRCT2::GetContext()->GetPlatformEnvironment(); - auto scenarioPatches = env->GetDirectoryPath(OpenRCT2::DirBase::openrct2, OpenRCT2::DirId::scenarioPatches); + auto& env = OpenRCT2::GetContext()->GetPlatformEnvironment(); + auto scenarioPatches = env.GetDirectoryPath(OpenRCT2::DirBase::openrct2, OpenRCT2::DirId::scenarioPatches); auto scenarioPatchFile = OpenRCT2::Path::WithExtension(scenarioHash.substr(0, 7), ".parkpatch"); return OpenRCT2::Path::Combine(scenarioPatches, scenarioPatchFile); } diff --git a/src/openrct2/ride/TrackDesignRepository.cpp b/src/openrct2/ride/TrackDesignRepository.cpp index 6a5877f89c..379758cb77 100644 --- a/src/openrct2/ride/TrackDesignRepository.cpp +++ b/src/openrct2/ride/TrackDesignRepository.cpp @@ -112,16 +112,15 @@ private: class TrackDesignRepository final : public ITrackDesignRepository { private: - std::shared_ptr const _env; + IPlatformEnvironment& _env; TrackDesignFileIndex const _fileIndex; std::vector _items; public: - explicit TrackDesignRepository(const std::shared_ptr& env) + explicit TrackDesignRepository(IPlatformEnvironment& env) : _env(env) - , _fileIndex(*env) + , _fileIndex(env) { - Guard::ArgumentNotNull(env); } size_t GetCount() const override @@ -258,7 +257,7 @@ public: std::string Install(const std::string& path, const std::string& name) override { std::string result; - std::string installDir = _env->GetDirectoryPath(DirBase::user, DirId::trackDesigns); + std::string installDir = _env.GetDirectoryPath(DirBase::user, DirId::trackDesigns); std::string newPath = Path::Combine(installDir, name + Path::GetExtension(path)); if (File::Copy(path, newPath, false)) @@ -310,7 +309,7 @@ private: } }; -std::unique_ptr CreateTrackDesignRepository(const std::shared_ptr& env) +std::unique_ptr CreateTrackDesignRepository(IPlatformEnvironment& env) { return std::make_unique(env); } diff --git a/src/openrct2/ride/TrackDesignRepository.h b/src/openrct2/ride/TrackDesignRepository.h index 02a9039d5a..b661bea503 100644 --- a/src/openrct2/ride/TrackDesignRepository.h +++ b/src/openrct2/ride/TrackDesignRepository.h @@ -41,8 +41,7 @@ struct ITrackDesignRepository virtual std::string Install(const std::string& path, const std::string& name) = 0; }; -[[nodiscard]] std::unique_ptr CreateTrackDesignRepository( - const std::shared_ptr& env); +[[nodiscard]] std::unique_ptr CreateTrackDesignRepository(OpenRCT2::IPlatformEnvironment& env); [[nodiscard]] std::string GetNameFromTrackPath(const std::string& path); void TrackRepositoryScan(); diff --git a/src/openrct2/scenario/Scenario.cpp b/src/openrct2/scenario/Scenario.cpp index 041b35a6b4..be5f6b5c0e 100644 --- a/src/openrct2/scenario/Scenario.cpp +++ b/src/openrct2/scenario/Scenario.cpp @@ -120,8 +120,8 @@ void ScenarioReset(GameState_t& gameState) } // Set the last saved game path - auto env = GetContext()->GetPlatformEnvironment(); - auto savePath = env->GetDirectoryPath(DirBase::user, DirId::saves); + auto& env = GetContext()->GetPlatformEnvironment(); + auto savePath = env.GetDirectoryPath(DirBase::user, DirId::saves); gScenarioSavePath = Path::Combine(savePath, gameState.park.Name + u8".park"); gameState.currentExpenditure = 0; diff --git a/src/openrct2/scenario/ScenarioRepository.cpp b/src/openrct2/scenario/ScenarioRepository.cpp index 610c6c7bcc..bb29eefc73 100644 --- a/src/openrct2/scenario/ScenarioRepository.cpp +++ b/src/openrct2/scenario/ScenarioRepository.cpp @@ -251,15 +251,15 @@ class ScenarioRepository final : public IScenarioRepository private: static constexpr uint32_t HighscoreFileVersion = 2; - std::shared_ptr const _env; + IPlatformEnvironment& _env; ScenarioFileIndex const _fileIndex; std::vector _scenarios; std::vector _highscores; public: - explicit ScenarioRepository(const std::shared_ptr& env) + explicit ScenarioRepository(IPlatformEnvironment& env) : _env(env) - , _fileIndex(*env) + , _fileIndex(env) { } @@ -437,10 +437,10 @@ private: */ void ImportMegaPark() { - auto mpdatPath = _env->FindFile(DirBase::rct1, DirId::data, "mp.dat"); + auto mpdatPath = _env.FindFile(DirBase::rct1, DirId::data, "mp.dat"); if (File::Exists(mpdatPath)) { - auto scenarioDirectory = _env->GetDirectoryPath(DirBase::user, DirId::scenarios); + auto scenarioDirectory = _env.GetDirectoryPath(DirBase::user, DirId::scenarios); auto expectedSc21Path = Path::Combine(scenarioDirectory, "sc21.sc4"); auto sc21Path = Path::ResolveCasing(expectedSc21Path); if (!File::Exists(sc21Path)) @@ -526,7 +526,7 @@ private: void LoadScores() { - std::string path = _env->GetFilePath(PathId::scores); + std::string path = _env.GetFilePath(PathId::scores); if (!File::Exists(path)) { return; @@ -566,8 +566,8 @@ private: */ void LoadLegacyScores() { - std::string rct2Path = _env->GetFilePath(PathId::scoresRCT2); - std::string legacyPath = _env->GetFilePath(PathId::scoresLegacy); + std::string rct2Path = _env.GetFilePath(PathId::scoresRCT2); + std::string legacyPath = _env.GetFilePath(PathId::scoresLegacy); LoadLegacyScores(legacyPath); LoadLegacyScores(rct2Path); } @@ -670,7 +670,7 @@ private: void SaveHighscores() { - std::string path = _env->GetFilePath(PathId::scores); + std::string path = _env.GetFilePath(PathId::scores); try { auto fs = FileStream(path, FileMode::write); @@ -692,7 +692,7 @@ private: } }; -std::unique_ptr CreateScenarioRepository(const std::shared_ptr& env) +std::unique_ptr CreateScenarioRepository(IPlatformEnvironment& env) { return std::make_unique(env); } diff --git a/src/openrct2/scenario/ScenarioRepository.h b/src/openrct2/scenario/ScenarioRepository.h index 0dfa81c2c7..ebdb506683 100644 --- a/src/openrct2/scenario/ScenarioRepository.h +++ b/src/openrct2/scenario/ScenarioRepository.h @@ -89,8 +89,7 @@ struct IScenarioRepository virtual bool TryRecordHighscore(int32_t language, const utf8* scenarioFileName, money64 companyValue, const utf8* name) = 0; }; -[[nodiscard]] std::unique_ptr CreateScenarioRepository( - const std::shared_ptr& env); +[[nodiscard]] std::unique_ptr CreateScenarioRepository(OpenRCT2::IPlatformEnvironment& env); [[nodiscard]] IScenarioRepository* GetScenarioRepository(); void ScenarioRepositoryScan(); diff --git a/src/openrct2/scenes/title/TitleScene.cpp b/src/openrct2/scenes/title/TitleScene.cpp index 05a5db29ce..be88bcf953 100644 --- a/src/openrct2/scenes/title/TitleScene.cpp +++ b/src/openrct2/scenes/title/TitleScene.cpp @@ -206,7 +206,7 @@ void TitleScene::TitleInitialise() { if (_sequencePlayer == nullptr) { - _sequencePlayer = GetContext().GetUiContext()->GetTitleSequencePlayer(); + _sequencePlayer = GetContext().GetUiContext().GetTitleSequencePlayer(); } if (Config::Get().interface.RandomTitleSequence) { @@ -288,7 +288,7 @@ bool TitleScene::TryLoadSequence(bool loadPreview) { if (_sequencePlayer == nullptr) { - _sequencePlayer = GetContext().GetUiContext()->GetTitleSequencePlayer(); + _sequencePlayer = GetContext().GetUiContext().GetTitleSequencePlayer(); } size_t numSequences = TitleSequenceManager::GetCount(); diff --git a/src/openrct2/scenes/title/TitleSequenceManager.cpp b/src/openrct2/scenes/title/TitleSequenceManager.cpp index 3cc7f8382d..5f29808365 100644 --- a/src/openrct2/scenes/title/TitleSequenceManager.cpp +++ b/src/openrct2/scenes/title/TitleSequenceManager.cpp @@ -254,14 +254,14 @@ namespace OpenRCT2::TitleSequenceManager static std::string GetDataSequencesPath() { - auto env = GetContext()->GetPlatformEnvironment(); - return env->GetDirectoryPath(DirBase::openrct2, DirId::sequences); + auto& env = GetContext()->GetPlatformEnvironment(); + return env.GetDirectoryPath(DirBase::openrct2, DirId::sequences); } static std::string GetUserSequencesPath() { - auto env = GetContext()->GetPlatformEnvironment(); - return env->GetDirectoryPath(DirBase::user, DirId::sequences); + auto& env = GetContext()->GetPlatformEnvironment(); + return env.GetDirectoryPath(DirBase::user, DirId::sequences); } static bool IsNameReserved(const std::string& name) diff --git a/src/openrct2/ui/DummyUiContext.cpp b/src/openrct2/ui/DummyUiContext.cpp index 06eca64ac7..1f638361f8 100644 --- a/src/openrct2/ui/DummyUiContext.cpp +++ b/src/openrct2/ui/DummyUiContext.cpp @@ -160,8 +160,7 @@ namespace OpenRCT2::Ui class X8DrawingEngineFactory final : public IDrawingEngineFactory { - std::unique_ptr Create( - [[maybe_unused]] DrawingEngine type, const std::shared_ptr& uiContext) override + std::unique_ptr Create([[maybe_unused]] DrawingEngine type, IUiContext& uiContext) override { return std::make_unique(uiContext); } @@ -213,7 +212,7 @@ namespace OpenRCT2::Ui } }; - std::shared_ptr CreateDummyUiContext() + std::unique_ptr CreateDummyUiContext() { return std::make_unique(); } diff --git a/src/openrct2/ui/UiContext.h b/src/openrct2/ui/UiContext.h index e55ddab78a..0d3a3bfd97 100644 --- a/src/openrct2/ui/UiContext.h +++ b/src/openrct2/ui/UiContext.h @@ -168,6 +168,6 @@ namespace OpenRCT2 virtual ITitleSequencePlayer* GetTitleSequencePlayer() = 0; }; - [[nodiscard]] std::shared_ptr CreateDummyUiContext(); + [[nodiscard]] std::unique_ptr CreateDummyUiContext(); } // namespace Ui } // namespace OpenRCT2 diff --git a/test/tests/ScenarioPatcherTests.cpp b/test/tests/ScenarioPatcherTests.cpp index 9d66ee29b1..9f0d6ffe80 100644 --- a/test/tests/ScenarioPatcherTests.cpp +++ b/test/tests/ScenarioPatcherTests.cpp @@ -31,8 +31,8 @@ TEST(FetchAndApplyScenarioPatch, expected_json_format) bool initialised = context->Initialise(); ASSERT_TRUE(initialised); - auto env = context->GetPlatformEnvironment(); - auto scenarioPatches = env->GetDirectoryPath(OpenRCT2::DirBase::openrct2, OpenRCT2::DirId::scenarioPatches); + auto& env = context->GetPlatformEnvironment(); + auto scenarioPatches = env.GetDirectoryPath(OpenRCT2::DirBase::openrct2, OpenRCT2::DirId::scenarioPatches); std::error_code ec; OpenRCT2::RCT12::SetDryRun(true);