mirror of
https://github.com/OpenRCT2/OpenRCT2
synced 2025-12-22 15:23:01 +01:00
Refactor MEMORY_ACCESS into a FlagHolder
This commit is contained in:
@@ -739,7 +739,7 @@ namespace OpenRCT2
|
|||||||
if (String::iequals(Path::GetExtension(path), ".sea"))
|
if (String::iequals(Path::GetExtension(path), ".sea"))
|
||||||
{
|
{
|
||||||
auto data = DecryptSea(fs::u8path(path));
|
auto data = DecryptSea(fs::u8path(path));
|
||||||
auto ms = MemoryStream(data.data(), data.size(), MEMORY_ACCESS::READ);
|
auto ms = MemoryStream(data.data(), data.size(), MemoryAccess::read);
|
||||||
if (!LoadParkFromStream(&ms, path, loadTitleScreenOnFail, asScenario))
|
if (!LoadParkFromStream(&ms, path, loadTitleScreenOnFail, asScenario))
|
||||||
{
|
{
|
||||||
throw std::runtime_error(".sea file may have been renamed.");
|
throw std::runtime_error(".sea file may have been renamed.");
|
||||||
@@ -1088,7 +1088,7 @@ namespace OpenRCT2
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
auto ms = MemoryStream(data.data(), data.size(), MEMORY_ACCESS::READ);
|
auto ms = MemoryStream(data.data(), data.size(), MemoryAccess::read);
|
||||||
if (!LoadParkFromStream(&ms, gOpenRCT2StartupActionPath, true))
|
if (!LoadParkFromStream(&ms, gOpenRCT2StartupActionPath, true))
|
||||||
{
|
{
|
||||||
Console::Error::WriteLine("Failed to load '%s'", gOpenRCT2StartupActionPath);
|
Console::Error::WriteLine("Failed to load '%s'", gOpenRCT2StartupActionPath);
|
||||||
|
|||||||
@@ -22,7 +22,7 @@ namespace OpenRCT2
|
|||||||
_dataCapacity = copy._dataCapacity;
|
_dataCapacity = copy._dataCapacity;
|
||||||
_dataSize = copy._dataSize;
|
_dataSize = copy._dataSize;
|
||||||
|
|
||||||
if (_access & MEMORY_ACCESS::OWNER)
|
if (_access.has(MemoryAccess::owner))
|
||||||
{
|
{
|
||||||
_data = Memory::Allocate<uint8_t>(_dataCapacity);
|
_data = Memory::Allocate<uint8_t>(_dataCapacity);
|
||||||
std::memcpy(_data, copy._data, _dataCapacity);
|
std::memcpy(_data, copy._data, _dataCapacity);
|
||||||
@@ -43,7 +43,7 @@ namespace OpenRCT2
|
|||||||
std::memcpy(_data, v.data(), v.size());
|
std::memcpy(_data, v.data(), v.size());
|
||||||
}
|
}
|
||||||
|
|
||||||
MemoryStream::MemoryStream(void* data, size_t dataSize, uint8_t access)
|
MemoryStream::MemoryStream(void* data, size_t dataSize, AccessFlags access)
|
||||||
{
|
{
|
||||||
_access = access;
|
_access = access;
|
||||||
_dataCapacity = dataSize;
|
_dataCapacity = dataSize;
|
||||||
@@ -66,7 +66,7 @@ namespace OpenRCT2
|
|||||||
|
|
||||||
MemoryStream::~MemoryStream()
|
MemoryStream::~MemoryStream()
|
||||||
{
|
{
|
||||||
if (_access & MEMORY_ACCESS::OWNER)
|
if (_access.has(MemoryAccess::owner))
|
||||||
Memory::Free(_data);
|
Memory::Free(_data);
|
||||||
|
|
||||||
_data = nullptr;
|
_data = nullptr;
|
||||||
@@ -79,7 +79,7 @@ namespace OpenRCT2
|
|||||||
{
|
{
|
||||||
if (this != &mv)
|
if (this != &mv)
|
||||||
{
|
{
|
||||||
if (_access & MEMORY_ACCESS::OWNER)
|
if (_access.has(MemoryAccess::owner))
|
||||||
Memory::Free(_data);
|
Memory::Free(_data);
|
||||||
|
|
||||||
_access = mv._access;
|
_access = mv._access;
|
||||||
@@ -188,7 +188,7 @@ namespace OpenRCT2
|
|||||||
{
|
{
|
||||||
if (_position + static_cast<size_t>(length) > _dataCapacity)
|
if (_position + static_cast<size_t>(length) > _dataCapacity)
|
||||||
{
|
{
|
||||||
if (_access & MEMORY_ACCESS::OWNER)
|
if (_access.has(MemoryAccess::owner))
|
||||||
{
|
{
|
||||||
// resize to the larger of the requested capacity and double the current capacity, with a minimum of 8.
|
// resize to the larger of the requested capacity and double the current capacity, with a minimum of 8.
|
||||||
size_t newCapacity = std::max(_position + static_cast<size_t>(length), _dataCapacity * 2);
|
size_t newCapacity = std::max(_position + static_cast<size_t>(length), _dataCapacity * 2);
|
||||||
|
|||||||
@@ -9,6 +9,7 @@
|
|||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
#include "../core/FlagHolder.hpp"
|
||||||
#include "IStream.hpp"
|
#include "IStream.hpp"
|
||||||
|
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
@@ -17,12 +18,13 @@
|
|||||||
|
|
||||||
namespace OpenRCT2
|
namespace OpenRCT2
|
||||||
{
|
{
|
||||||
namespace MEMORY_ACCESS
|
enum class MemoryAccess : uint8_t
|
||||||
{
|
{
|
||||||
constexpr uint8_t READ = 1 << 0;
|
read,
|
||||||
constexpr uint8_t WRITE = 1 << 1;
|
write,
|
||||||
constexpr uint8_t OWNER = 1 << 2;
|
owner,
|
||||||
}; // namespace MEMORY_ACCESS
|
};
|
||||||
|
using AccessFlags = FlagHolder<uint8_t, MemoryAccess>;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A stream for reading and writing to a buffer in memory. By default this buffer can grow.
|
* A stream for reading and writing to a buffer in memory. By default this buffer can grow.
|
||||||
@@ -30,7 +32,7 @@ namespace OpenRCT2
|
|||||||
class MemoryStream final : public IStream
|
class MemoryStream final : public IStream
|
||||||
{
|
{
|
||||||
private:
|
private:
|
||||||
uint8_t _access = MEMORY_ACCESS::READ | MEMORY_ACCESS::WRITE | MEMORY_ACCESS::OWNER;
|
AccessFlags _access = { MemoryAccess::read, MemoryAccess::write, MemoryAccess::owner };
|
||||||
size_t _dataCapacity = 0;
|
size_t _dataCapacity = 0;
|
||||||
size_t _dataSize = 0;
|
size_t _dataSize = 0;
|
||||||
uint8_t* _data = nullptr;
|
uint8_t* _data = nullptr;
|
||||||
@@ -42,9 +44,9 @@ namespace OpenRCT2
|
|||||||
MemoryStream(MemoryStream&& mv) noexcept;
|
MemoryStream(MemoryStream&& mv) noexcept;
|
||||||
explicit MemoryStream(size_t capacity);
|
explicit MemoryStream(size_t capacity);
|
||||||
MemoryStream(const std::vector<uint8_t>& v);
|
MemoryStream(const std::vector<uint8_t>& v);
|
||||||
MemoryStream(void* data, size_t dataSize, uint8_t access = MEMORY_ACCESS::READ);
|
MemoryStream(void* data, size_t dataSize, AccessFlags access = { MemoryAccess::read });
|
||||||
MemoryStream(const void* data, size_t dataSize)
|
MemoryStream(const void* data, size_t dataSize)
|
||||||
: MemoryStream(const_cast<void*>(data), dataSize, MEMORY_ACCESS::READ)
|
: MemoryStream(const_cast<void*>(data), dataSize, MemoryAccess::read)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
virtual ~MemoryStream();
|
virtual ~MemoryStream();
|
||||||
@@ -62,11 +64,11 @@ namespace OpenRCT2
|
|||||||
|
|
||||||
bool CanRead() const override
|
bool CanRead() const override
|
||||||
{
|
{
|
||||||
return (_access & MEMORY_ACCESS::READ) != 0;
|
return _access.has(MemoryAccess::read);
|
||||||
}
|
}
|
||||||
bool CanWrite() const override
|
bool CanWrite() const override
|
||||||
{
|
{
|
||||||
return (_access & MEMORY_ACCESS::WRITE) != 0;
|
return _access.has(MemoryAccess::write);
|
||||||
}
|
}
|
||||||
|
|
||||||
uint64_t GetLength() const override
|
uint64_t GetLength() const override
|
||||||
|
|||||||
Reference in New Issue
Block a user