diff --git a/src/openrct2/libopenrct2.vcxproj b/src/openrct2/libopenrct2.vcxproj index 60b10e0631..d3140f3abf 100644 --- a/src/openrct2/libopenrct2.vcxproj +++ b/src/openrct2/libopenrct2.vcxproj @@ -1000,6 +1000,7 @@ + diff --git a/src/openrct2/object/Object.cpp b/src/openrct2/object/Object.cpp index 821eb7afa1..19235dbf1f 100644 --- a/src/openrct2/object/Object.cpp +++ b/src/openrct2/object/Object.cpp @@ -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 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 ObjectAsset::GetStream() const - { - try - { - if (_zipPath.empty()) - { - return std::make_unique(_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(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 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)) + "." diff --git a/src/openrct2/object/ObjectAsset.cpp b/src/openrct2/object/ObjectAsset.cpp new file mode 100644 index 0000000000..6a7e758470 --- /dev/null +++ b/src/openrct2/object/ObjectAsset.cpp @@ -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 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 ObjectAsset::GetStream() const + { + try + { + if (_zipPath.empty()) + { + return std::make_unique(_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(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 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 diff --git a/src/openrct2/object/ObjectAsset.h b/src/openrct2/object/ObjectAsset.h index 2bda3de114..bb7e4a2572 100644 --- a/src/openrct2/object/ObjectAsset.h +++ b/src/openrct2/object/ObjectAsset.h @@ -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: diff --git a/src/openrct2/object/ObjectFactory.cpp b/src/openrct2/object/ObjectFactory.cpp index ee26068eb8..1a145aaa72 100644 --- a/src/openrct2/object/ObjectFactory.cpp +++ b/src/openrct2/object/ObjectFactory.cpp @@ -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"