Close #27199: Move ObjectAsset to its own file (#27205)

Perform the refactor described in #27199, move ObjectAsset to its own cpp file. I also added a brief docstring for the class based on my understanding of its function.
This commit is contained in:
csunday95 authored and GitHub committed 2026-10-11 18:38:30 +02:00
1 parent e87de05792
commit 42184adf10
5 files changed
+121 -91

No files matched your search

+1
View File
@@ -1000,6 +1000,7 @@
<ClCompile Include="object\LargeSceneryObject.cpp" />
<ClCompile Include="object\MusicObject.cpp" />
<ClCompile Include="object\Object.cpp" />
<ClCompile Include="object\ObjectAsset.cpp" />
<ClCompile Include="object\ObjectEntryManager.cpp" />
<ClCompile Include="object\ObjectFactory.cpp" />
<ClCompile Include="object\ObjectList.cpp" />
-91
View File
@@ -308,97 +308,6 @@ namespace OpenRCT2
return !(*this == rhs);
}
bool ObjectAsset::IsAvailable() const
{
if (_zipPath.empty())
{
return File::Exists(_path);
}
auto zipArchive = Zip::TryOpen(_zipPath, ZipAccess::read);
return zipArchive != nullptr && zipArchive->Exists(_path);
}
uint64_t ObjectAsset::GetSize() const
{
if (_zipPath.empty())
{
return File::GetSize(_path);
}
auto zipArchive = Zip::TryOpen(_zipPath, ZipAccess::read);
if (zipArchive != nullptr)
{
auto index = zipArchive->GetIndexFromPath(_path);
if (index.has_value())
{
auto size = zipArchive->GetFileSize(index.value());
return size;
}
}
return 0;
}
std::vector<uint8_t> ObjectAsset::GetData() const
{
if (_zipPath.empty())
{
return File::ReadAllBytes(_path);
}
auto zipArchive = Zip::TryOpen(_zipPath, ZipAccess::read);
if (zipArchive != nullptr)
{
return zipArchive->GetFileData(_path);
}
return {};
}
std::unique_ptr<IStream> ObjectAsset::GetStream() const
{
try
{
if (_zipPath.empty())
{
return std::make_unique<FileStream>(_path, FileMode::open);
}
auto zipArchive = Zip::TryOpen(_zipPath, ZipAccess::read);
if (zipArchive != nullptr)
{
auto stream = zipArchive->GetFileStream(_path);
if (stream != nullptr)
{
return std::make_unique<ZipStreamWrapper>(std::move(zipArchive), std::move(stream));
}
}
}
catch (...)
{
}
return {};
}
const std::string& ObjectAsset::GetZipPath() const
{
return _zipPath;
}
const std::string& ObjectAsset::GetPath() const
{
return _path;
}
size_t ObjectAsset::GetHash() const
{
// Combine hashes of zipPath and path
std::hash<std::string> hasher;
auto h1 = hasher(_zipPath);
auto h2 = hasher(_path);
// Combine the hashes based on example from https://en.cppreference.com/w/cpp/utility/hash.html
return h1 ^ (h2 << 1);
}
u8string VersionString(const ObjectVersion& version)
{
return std::to_string(std::get<0>(version)) + "." + std::to_string(std::get<1>(version)) + "."
+110
View File
@@ -0,0 +1,110 @@
/*****************************************************************************
* Copyright (c) 2014-2026 OpenRCT2 developers
*
* For a complete list of all authors, please refer to contributors.md
* Interested in contributing? Visit https://github.com/OpenRCT2/OpenRCT2
*
* OpenRCT2 is licensed under the GNU General Public License version 3.
*****************************************************************************/
#include "ObjectAsset.h"
#include "../core/File.h"
#include "../core/FileStream.h"
#include "../core/Zip.h"
#include "../core/ZipStream.hpp"
namespace OpenRCT2
{
bool ObjectAsset::IsAvailable() const
{
if (_zipPath.empty())
{
return File::Exists(_path);
}
auto zipArchive = Zip::TryOpen(_zipPath, ZipAccess::read);
return zipArchive != nullptr && zipArchive->Exists(_path);
}
uint64_t ObjectAsset::GetSize() const
{
if (_zipPath.empty())
{
return File::GetSize(_path);
}
auto zipArchive = Zip::TryOpen(_zipPath, ZipAccess::read);
if (zipArchive != nullptr)
{
auto index = zipArchive->GetIndexFromPath(_path);
if (index.has_value())
{
auto size = zipArchive->GetFileSize(index.value());
return size;
}
}
return 0;
}
std::vector<uint8_t> ObjectAsset::GetData() const
{
if (_zipPath.empty())
{
return File::ReadAllBytes(_path);
}
auto zipArchive = Zip::TryOpen(_zipPath, ZipAccess::read);
if (zipArchive != nullptr)
{
return zipArchive->GetFileData(_path);
}
return {};
}
std::unique_ptr<IStream> ObjectAsset::GetStream() const
{
try
{
if (_zipPath.empty())
{
return std::make_unique<FileStream>(_path, FileMode::open);
}
auto zipArchive = Zip::TryOpen(_zipPath, ZipAccess::read);
if (zipArchive != nullptr)
{
auto stream = zipArchive->GetFileStream(_path);
if (stream != nullptr)
{
return std::make_unique<ZipStreamWrapper>(std::move(zipArchive), std::move(stream));
}
}
}
catch (...)
{
}
return {};
}
const std::string& ObjectAsset::GetZipPath() const
{
return _zipPath;
}
const std::string& ObjectAsset::GetPath() const
{
return _path;
}
size_t ObjectAsset::GetHash() const
{
// Combine hashes of zipPath and path
std::hash<std::string> hasher;
auto h1 = hasher(_zipPath);
auto h2 = hasher(_path);
// Combine the hashes based on example from https://en.cppreference.com/w/cpp/utility/hash.html
return h1 ^ (h2 << 1);
}
} // namespace OpenRCT2
+9
View File
@@ -17,6 +17,15 @@ namespace OpenRCT2
{
struct IStream;
/**
* A wrapper for an object held in a file or as a file within a zip
* archive.
*
* If only a `path` is provided, GetStream() and GetData() will return the
* content of the file directly. If a `zipPath` and `path` are provided,
* GetStream() and GetData() will return the content of the file at `path`
* within the zip archive at `zipPath`.
*/
class ObjectAsset
{
private:
+1
View File
@@ -35,6 +35,7 @@
#include "LargeSceneryObject.h"
#include "MusicObject.h"
#include "Object.h"
#include "ObjectAsset.h"
#include "ObjectLimits.h"
#include "PathAdditionObject.h"
#include "PeepAnimationsObject.h"