From 724a3c0579ff09ac19de3744d60c937e46a172a5 Mon Sep 17 00:00:00 2001 From: Silent Date: Fri, 21 May 2021 17:51:26 +0200 Subject: [PATCH] Obtain file sizes without seeking where possible --- src/openrct2/core/File.cpp | 4 +--- src/openrct2/core/FileStream.cpp | 13 +++++++++---- 2 files changed, 10 insertions(+), 7 deletions(-) diff --git a/src/openrct2/core/File.cpp b/src/openrct2/core/File.cpp index 99bc40b93d..4e7eba3fea 100644 --- a/src/openrct2/core/File.cpp +++ b/src/openrct2/core/File.cpp @@ -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); } diff --git a/src/openrct2/core/FileStream.cpp b/src/openrct2/core/FileStream.cpp index 3a1ff1654b..3eb85d8987 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; }