Add File::GetSize to get object size in a simpler way

Drastically reduces startup speed because of the
optimization applied to ObjectAsset::GetSize - now the file
size is obtained without reading the entire file into memory.
This commit is contained in:
Silent 2021-05-21 16:55:07 +02:00
parent dc11021192
commit 95388cfbb1
No known key found for this signature in database
GPG Key ID: AE53149BB0C45AF1
8 changed files with 46 additions and 17 deletions

View File

@ -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)
------------------------------------------------------------------------

View File

@ -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
@ -123,6 +123,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)

View File

@ -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<std::string> 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

View File

@ -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
{

View File

@ -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<OpenRCT2::IStream> GetStream() const;
};

View File

@ -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;

View File

@ -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;

View File

@ -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();