diff --git a/distribution/changelog.txt b/distribution/changelog.txt index f0c2a3da24..eda7cd7591 100644 --- a/distribution/changelog.txt +++ b/distribution/changelog.txt @@ -20,6 +20,7 @@ - Fix: [#14587] Confusing message when joining server with mismatched network version. - Fix: [#14604] American-style Steam Trains are not imported correctly from RCT1 saves. - Fix: [#14638] The “About OpenRCT2” window cannot be themed. +- Improved: [#14712]: Improve startup times. 0.3.3 (2021-03-13) ------------------------------------------------------------------------ diff --git a/src/openrct2/core/File.cpp b/src/openrct2/core/File.cpp index df722e1196..4e7eba3fea 100644 --- a/src/openrct2/core/File.cpp +++ b/src/openrct2/core/File.cpp @@ -1,5 +1,5 @@ /***************************************************************************** - * Copyright (c) 2014-2020 OpenRCT2 developers + * Copyright (c) 2014-2021 OpenRCT2 developers * * For a complete list of all authors, please refer to contributors.md * Interested in contributing? Visit https://github.com/OpenRCT2/OpenRCT2 @@ -57,8 +57,7 @@ namespace File } std::vector result; - fs.seekg(0, std::ios::end); - auto fsize = static_cast(fs.tellg()); + auto fsize = Platform::GetFileSize(path); if (fsize > SIZE_MAX) { std::string message = String::StdFormat( @@ -68,7 +67,6 @@ namespace File else { result.resize(fsize); - fs.seekg(0); fs.read(reinterpret_cast(result.data()), result.size()); fs.exceptions(fs.failbit); } @@ -123,6 +121,11 @@ namespace File { return Platform::GetLastModified(path); } + + uint64_t GetSize(std::string_view path) + { + return Platform::GetFileSize(path); + } } // namespace File bool writeentirefile(const utf8* path, const void* buffer, size_t length) diff --git a/src/openrct2/core/File.h b/src/openrct2/core/File.h index 9dc216042e..cdd6179fa7 100644 --- a/src/openrct2/core/File.h +++ b/src/openrct2/core/File.h @@ -1,5 +1,5 @@ /***************************************************************************** - * Copyright (c) 2014-2020 OpenRCT2 developers + * Copyright (c) 2014-2021 OpenRCT2 developers * * For a complete list of all authors, please refer to contributors.md * Interested in contributing? Visit https://github.com/OpenRCT2/OpenRCT2 @@ -26,4 +26,5 @@ namespace File std::vector ReadAllLines(std::string_view path); void WriteAllBytes(const std::string& path, const void* buffer, size_t length); uint64_t GetLastModified(const std::string& path); + uint64_t GetSize(std::string_view path); } // namespace File diff --git a/src/openrct2/core/FileStream.cpp b/src/openrct2/core/FileStream.cpp index 3a1ff1654b..68c0b52692 100644 --- a/src/openrct2/core/FileStream.cpp +++ b/src/openrct2/core/FileStream.cpp @@ -1,5 +1,5 @@ /***************************************************************************** - * Copyright (c) 2014-2020 OpenRCT2 developers + * Copyright (c) 2014-2021 OpenRCT2 developers * * For a complete list of all authors, please refer to contributors.md * Interested in contributing? Visit https://github.com/OpenRCT2/OpenRCT2 @@ -16,6 +16,8 @@ #ifndef _WIN32 # include +#else +# include #endif #if defined(__linux__) && !defined(__ANDROID__) @@ -98,9 +100,12 @@ namespace OpenRCT2 throw IOException(String::StdFormat("Unable to open '%s'", path)); } - Seek(0, STREAM_SEEK_END); - _fileSize = GetPosition(); - Seek(0, STREAM_SEEK_BEGIN); +#ifdef _WIN32 + _fileSize = _filelengthi64(_fileno(_file)); +#else + std::error_code ec; + _fileSize = fs::file_size(fs::u8path(path), ec); +#endif _ownsFilePtr = true; } @@ -160,13 +165,9 @@ namespace OpenRCT2 void FileStream::Read(void* buffer, uint64_t length) { - uint64_t remainingBytes = GetLength() - GetPosition(); - if (length <= remainingBytes) + if (fread(buffer, 1, static_cast(length), _file) == length) { - if (fread(buffer, static_cast(length), 1, _file) == 1) - { - return; - } + return; } throw IOException("Attempted to read past end of file."); } diff --git a/src/openrct2/object/Object.cpp b/src/openrct2/object/Object.cpp index 0d221293e5..6de569acdf 100644 --- a/src/openrct2/object/Object.cpp +++ b/src/openrct2/object/Object.cpp @@ -1,5 +1,5 @@ /***************************************************************************** - * Copyright (c) 2014-2020 OpenRCT2 developers + * Copyright (c) 2014-2021 OpenRCT2 developers * * For a complete list of all authors, please refer to contributors.md * Interested in contributing? Visit https://github.com/OpenRCT2/OpenRCT2 @@ -252,18 +252,11 @@ bool ObjectAsset::IsAvailable() const } } -size_t ObjectAsset::GetSize() const +uint64_t ObjectAsset::GetSize() const { if (_zipPath.empty()) { - try - { - return File::ReadAllBytes(_path).size(); - } - catch (...) - { - return 0; - } + return File::GetSize(_path); } else { diff --git a/src/openrct2/object/Object.h b/src/openrct2/object/Object.h index 47e9ee87d5..7e3a496e86 100644 --- a/src/openrct2/object/Object.h +++ b/src/openrct2/object/Object.h @@ -1,5 +1,5 @@ /***************************************************************************** - * Copyright (c) 2014-2020 OpenRCT2 developers + * Copyright (c) 2014-2021 OpenRCT2 developers * * For a complete list of all authors, please refer to contributors.md * Interested in contributing? Visit https://github.com/OpenRCT2/OpenRCT2 @@ -223,7 +223,7 @@ public: } bool IsAvailable() const; - size_t GetSize() const; + uint64_t GetSize() const; std::unique_ptr GetStream() const; }; diff --git a/src/openrct2/platform/Platform.Posix.cpp b/src/openrct2/platform/Platform.Posix.cpp index 8889749720..dc1184564d 100644 --- a/src/openrct2/platform/Platform.Posix.cpp +++ b/src/openrct2/platform/Platform.Posix.cpp @@ -1,5 +1,5 @@ /***************************************************************************** - * Copyright (c) 2014-2020 OpenRCT2 developers + * Copyright (c) 2014-2021 OpenRCT2 developers * * For a complete list of all authors, please refer to contributors.md * Interested in contributing? Visit https://github.com/OpenRCT2/OpenRCT2 @@ -177,6 +177,19 @@ namespace Platform return lastModified; } + uint64_t GetFileSize(std::string_view path) + { + uint64_t size = 0; + struct stat statInfo + { + }; + if (stat(std::string(path).c_str(), &statInfo) == 0) + { + size = statInfo.st_size; + } + return size; + } + bool ShouldIgnoreCase() { return false; diff --git a/src/openrct2/platform/Platform.Win32.cpp b/src/openrct2/platform/Platform.Win32.cpp index d7d22fbbea..4a7f203e96 100644 --- a/src/openrct2/platform/Platform.Win32.cpp +++ b/src/openrct2/platform/Platform.Win32.cpp @@ -1,5 +1,5 @@ /***************************************************************************** - * Copyright (c) 2014-2020 OpenRCT2 developers + * Copyright (c) 2014-2021 OpenRCT2 developers * * For a complete list of all authors, please refer to contributors.md * Interested in contributing? Visit https://github.com/OpenRCT2/OpenRCT2 @@ -554,6 +554,21 @@ namespace Platform return lastModified; } + uint64_t GetFileSize(std::string_view path) + { + uint64_t size = 0; + auto pathW = String::ToWideChar(path); + WIN32_FILE_ATTRIBUTE_DATA attributes; + if (GetFileAttributesExW(pathW.c_str(), GetFileExInfoStandard, &attributes) != FALSE) + { + ULARGE_INTEGER fileSize; + fileSize.LowPart = attributes.nFileSizeLow; + fileSize.HighPart = attributes.nFileSizeHigh; + size = fileSize.QuadPart; + } + return size; + } + bool ShouldIgnoreCase() { return true; diff --git a/src/openrct2/platform/Platform2.h b/src/openrct2/platform/Platform2.h index 6189f21b96..98d886d282 100644 --- a/src/openrct2/platform/Platform2.h +++ b/src/openrct2/platform/Platform2.h @@ -1,5 +1,5 @@ /***************************************************************************** - * Copyright (c) 2014-2020 OpenRCT2 developers + * Copyright (c) 2014-2021 OpenRCT2 developers * * For a complete list of all authors, please refer to contributors.md * Interested in contributing? Visit https://github.com/OpenRCT2/OpenRCT2 @@ -39,6 +39,7 @@ namespace Platform bool IsPathSeparator(char c); utf8* GetAbsolutePath(utf8* buffer, size_t bufferSize, const utf8* relativePath); uint64_t GetLastModified(const std::string& path); + uint64_t GetFileSize(std::string_view path); std::string ResolveCasing(const std::string& path, bool fileExists); rct2_time GetTimeLocal(); rct2_date GetDateLocal();