diff --git a/src/openrct2/ParkImporter.cpp b/src/openrct2/ParkImporter.cpp new file mode 100644 index 0000000000..9e1ca85930 --- /dev/null +++ b/src/openrct2/ParkImporter.cpp @@ -0,0 +1,35 @@ +#pragma region Copyright (c) 2014-2016 OpenRCT2 Developers +/***************************************************************************** + * OpenRCT2, an open source clone of Roller Coaster Tycoon 2. + * + * OpenRCT2 is the work of many authors, a full list can be found in contributors.md + * For more information, visit https://github.com/OpenRCT2/OpenRCT2 + * + * OpenRCT2 is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * A full copy of the GNU General Public License can be found in licence.txt + *****************************************************************************/ +#pragma endregion + +#include "core/Path.hpp" +#include "core/String.hpp" +#include "ParkImporter.h" + +IParkImporter * CreateParkImporterForPath(const std::string &path) +{ + IParkImporter * parkImporter = nullptr; + std::string extension = Path::GetExtension(path); + if (String::Equals(extension, ".sc4", true) || + String::Equals(extension, ".sv4", true)) + { + parkImporter = CreateS4Importer(); + } + else + { + parkImporter = CreateS6Importer(); + } + return parkImporter; +} diff --git a/src/openrct2/ParkImporter.h b/src/openrct2/ParkImporter.h index 5a3a495b46..1d5cac6938 100644 --- a/src/openrct2/ParkImporter.h +++ b/src/openrct2/ParkImporter.h @@ -16,6 +16,7 @@ #pragma once +#include #include "common.h" #include "scenario/ScenarioRepository.h" @@ -38,3 +39,4 @@ public: IParkImporter * CreateS4Importer(); IParkImporter * CreateS6Importer(); +IParkImporter * CreateParkImporterForPath(const std::string &path); diff --git a/src/openrct2/core/FileStream.hpp b/src/openrct2/core/FileStream.hpp index de10f7857e..0b17273324 100644 --- a/src/openrct2/core/FileStream.hpp +++ b/src/openrct2/core/FileStream.hpp @@ -33,11 +33,12 @@ enum class FileStream final : public IStream { private: - SDL_RWops * _file; - bool _canRead; - bool _canWrite; - bool _disposed; - uint64 _fileSize; + SDL_RWops * _file = 0; + bool _ownsFilePtr = false; + bool _canRead = false; + bool _canWrite = false; + bool _disposed = false; + uint64 _fileSize = 0; public: FileStream(const std::string &path, sint32 fileMode) : @@ -68,9 +69,8 @@ public: { throw IOException(SDL_GetError()); } - - _disposed = false; _fileSize = SDL_RWsize(_file); + _ownsFilePtr = true; } FileStream(SDL_RWops * ops, sint32 fileMode) @@ -88,8 +88,6 @@ public: default: throw; } - - _disposed = false; _fileSize = SDL_RWsize(_file); } @@ -98,7 +96,10 @@ public: if (!_disposed) { _disposed = true; - SDL_RWclose(_file); + if (_ownsFilePtr) + { + SDL_RWclose(_file); + } } } diff --git a/src/openrct2/libopenrct2.vcxproj b/src/openrct2/libopenrct2.vcxproj index 06650fd1a9..3d9c3089a1 100644 --- a/src/openrct2/libopenrct2.vcxproj +++ b/src/openrct2/libopenrct2.vcxproj @@ -73,6 +73,7 @@ + diff --git a/src/openrct2/rct2/S6Importer.cpp b/src/openrct2/rct2/S6Importer.cpp index b0f74d93d3..791d5a9aa8 100644 --- a/src/openrct2/rct2/S6Importer.cpp +++ b/src/openrct2/rct2/S6Importer.cpp @@ -14,6 +14,7 @@ *****************************************************************************/ #pragma endregion +#include "../core/Console.hpp" #include "../core/Exception.hpp" #include "../core/FileStream.hpp" #include "../core/IStream.hpp" @@ -127,6 +128,7 @@ public: { throw Exception("Park is not a scenario."); } + SawyerEncoding::ReadChunkTolerant(&_s6.info, sizeof(_s6.info), stream); } else { @@ -136,8 +138,6 @@ public: } } - SawyerEncoding::ReadChunkTolerant(&_s6.info, sizeof(_s6.info), stream); - // Read packed objects // TODO try to contain this more and not store objects until later for (uint16 i = 0; i < _s6.header.num_packed_objects; i++) @@ -407,6 +407,11 @@ public: } }; +IParkImporter * CreateS6Importer() +{ + return new S6Importer(); +} + extern "C" { /** @@ -436,11 +441,13 @@ extern "C" sprite_position_tween_reset(); result = true; } - catch (const ObjectLoadException &) + catch (const ObjectLoadException &ex) { + Console::Error::WriteLine(ex.GetMessage()); } - catch (const Exception &) + catch (const Exception &ex) { + Console::Error::WriteLine(ex.GetMessage()); } delete s6Importer; diff --git a/src/openrct2/title/TitleSequencePlayer.cpp b/src/openrct2/title/TitleSequencePlayer.cpp index 915f6a8951..72a27c98ca 100644 --- a/src/openrct2/title/TitleSequencePlayer.cpp +++ b/src/openrct2/title/TitleSequencePlayer.cpp @@ -14,6 +14,7 @@ *****************************************************************************/ #pragma endregion +#include #include "../common.h" #include #include "../core/Console.hpp" @@ -352,33 +353,19 @@ private: bool LoadParkFromFile(const utf8 * path) { + log_verbose("TitleSequencePlayer::LoadParkFromFile(%s)", path); bool success = false; - const utf8 * extension = Path::GetExtension(path); - if (String::Equals(extension, ".sc4", true) || - String::Equals(extension, ".sv4", true)) + try { - try - { - bool isScenario = String::Equals(extension, ".sc4", true); - IParkImporter * s4Importer = CreateS4Importer(); - s4Importer->Load(path); - s4Importer->Import(); - PrepareParkForPlayback(isScenario); - success = true; - } - catch (Exception) - { - } + auto parkImporter = std::unique_ptr(CreateParkImporterForPath(path)); + parkImporter->Load(path); + parkImporter->Import(); + PrepareParkForPlayback(); + success = true; } - else + catch (Exception) { - bool isScenario = String::Equals(extension, ".sc6", true); - SDL_RWops * rw = SDL_RWFromFile(path, "rb"); - if (rw != nullptr) - { - success = LoadParkFromRW(rw, isScenario); - SDL_RWclose(rw); - } + Console::Error::WriteLine("Unable to load park: %s", path); } return success; } @@ -389,12 +376,12 @@ private: game_load_sv6(rw); if (successfulLoad) { - PrepareParkForPlayback(isScenario); + PrepareParkForPlayback(); } return successfulLoad; } - void PrepareParkForPlayback(bool isScenario) + void PrepareParkForPlayback() { rct_window * w = window_get_main(); w->viewport_target_sprite = -1; @@ -425,10 +412,6 @@ private: reset_sprite_spatial_index(); reset_all_sprite_quadrant_placements(); window_new_ride_init_vars(); - if (!isScenario) - { - sub_684AC3(); - } scenery_set_default_placement_configuration(); news_item_init_queue(); load_palette();