mirror of https://github.com/OpenRCT2/OpenRCT2.git
Merge pull request #14081 from ZehMatt/refactor/ReadArray
Refactor IStream::ReadArray to return unique_ptr
This commit is contained in:
commit
90a86cd5f3
|
@ -97,8 +97,7 @@ static bool TryClassifyAsS4(OpenRCT2::IStream* stream, ClassifiedFileInfo* resul
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
size_t dataLength = static_cast<size_t>(stream->GetLength());
|
size_t dataLength = static_cast<size_t>(stream->GetLength());
|
||||||
auto deleter_lambda = [dataLength](uint8_t* ptr) { Memory::FreeArray(ptr, dataLength); };
|
auto data = stream->ReadArray<uint8_t>(dataLength);
|
||||||
std::unique_ptr<uint8_t, decltype(deleter_lambda)> data(stream->ReadArray<uint8_t>(dataLength), deleter_lambda);
|
|
||||||
stream->SetPosition(originalPosition);
|
stream->SetPosition(originalPosition);
|
||||||
int32_t fileTypeVersion = sawyercoding_detect_file_type(data.get(), dataLength);
|
int32_t fileTypeVersion = sawyercoding_detect_file_type(data.get(), dataLength);
|
||||||
|
|
||||||
|
@ -134,8 +133,8 @@ static bool TryClassifyAsTD4_TD6(OpenRCT2::IStream* stream, ClassifiedFileInfo*
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
size_t dataLength = static_cast<size_t>(stream->GetLength());
|
size_t dataLength = static_cast<size_t>(stream->GetLength());
|
||||||
auto deleter_lambda = [dataLength](uint8_t* ptr) { Memory::FreeArray(ptr, dataLength); };
|
|
||||||
std::unique_ptr<uint8_t, decltype(deleter_lambda)> data(stream->ReadArray<uint8_t>(dataLength), deleter_lambda);
|
auto data = stream->ReadArray<uint8_t>(dataLength);
|
||||||
stream->SetPosition(originalPosition);
|
stream->SetPosition(originalPosition);
|
||||||
|
|
||||||
if (sawyercoding_validate_track_checksum(data.get(), dataLength))
|
if (sawyercoding_validate_track_checksum(data.get(), dataLength))
|
||||||
|
|
|
@ -998,7 +998,7 @@ bool RCT1DataPresentAtLocation(const utf8* path)
|
||||||
return Csg1datPresentAtLocation(path) && Csg1idatPresentAtLocation(path) && CsgAtLocationIsUsable(path);
|
return Csg1datPresentAtLocation(path) && Csg1idatPresentAtLocation(path) && CsgAtLocationIsUsable(path);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool CsgIsUsable(rct_gx csg)
|
bool CsgIsUsable(const rct_gx& csg)
|
||||||
{
|
{
|
||||||
return csg.header.num_entries == RCT1_NUM_LL_CSG_ENTRIES;
|
return csg.header.num_entries == RCT1_NUM_LL_CSG_ENTRIES;
|
||||||
}
|
}
|
||||||
|
|
|
@ -258,5 +258,5 @@ std::string FindCsg1datAtLocation(const utf8* path);
|
||||||
bool Csg1datPresentAtLocation(const utf8* path);
|
bool Csg1datPresentAtLocation(const utf8* path);
|
||||||
std::string FindCsg1idatAtLocation(const utf8* path);
|
std::string FindCsg1idatAtLocation(const utf8* path);
|
||||||
bool Csg1idatPresentAtLocation(const utf8* path);
|
bool Csg1idatPresentAtLocation(const utf8* path);
|
||||||
bool CsgIsUsable(rct_gx csg);
|
bool CsgIsUsable(const rct_gx& csg);
|
||||||
bool CsgAtLocationIsUsable(const utf8* path);
|
bool CsgAtLocationIsUsable(const utf8* path);
|
||||||
|
|
|
@ -161,10 +161,8 @@ template<> struct DataSerializerTraits_t<std::string>
|
||||||
res = "";
|
res = "";
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
const char* str = stream->ReadArray<char>(len);
|
auto str = stream->ReadArray<char>(len);
|
||||||
res.assign(str, len);
|
res.assign(str.get(), len);
|
||||||
|
|
||||||
Memory::FreeArray(str, len);
|
|
||||||
}
|
}
|
||||||
static void log(OpenRCT2::IStream* stream, const std::string& str)
|
static void log(OpenRCT2::IStream* stream, const std::string& str)
|
||||||
{
|
{
|
||||||
|
@ -620,9 +618,8 @@ template<> struct DataSerializerTraits_t<rct_object_entry>
|
||||||
uint32_t temp;
|
uint32_t temp;
|
||||||
stream->Read(&temp);
|
stream->Read(&temp);
|
||||||
val.flags = ByteSwapBE(temp);
|
val.flags = ByteSwapBE(temp);
|
||||||
const char* str = stream->ReadArray<char>(12);
|
auto str = stream->ReadArray<char>(12);
|
||||||
memcpy(val.nameWOC, str, 12);
|
memcpy(val.nameWOC, str.get(), 12);
|
||||||
Memory::FreeArray(str, 12);
|
|
||||||
}
|
}
|
||||||
static void log(OpenRCT2::IStream* stream, const rct_object_entry& val)
|
static void log(OpenRCT2::IStream* stream, const rct_object_entry& val)
|
||||||
{
|
{
|
||||||
|
|
|
@ -14,6 +14,7 @@
|
||||||
#include "Memory.hpp"
|
#include "Memory.hpp"
|
||||||
|
|
||||||
#include <istream>
|
#include <istream>
|
||||||
|
#include <memory>
|
||||||
#include <stdexcept>
|
#include <stdexcept>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
@ -191,10 +192,10 @@ namespace OpenRCT2
|
||||||
Write(&value);
|
Write(&value);
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename T> T* ReadArray(size_t count)
|
template<typename T> std::unique_ptr<T[]> ReadArray(size_t count)
|
||||||
{
|
{
|
||||||
T* buffer = Memory::AllocateArray<T>(count);
|
auto buffer = std::make_unique<T[]>(count);
|
||||||
Read(buffer, sizeof(T) * count);
|
Read(buffer.get(), sizeof(T) * count);
|
||||||
return buffer;
|
return buffer;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -220,7 +220,7 @@ bool gfx_load_g1(const IPlatformEnvironment& env)
|
||||||
// Fix entry data offsets
|
// Fix entry data offsets
|
||||||
for (uint32_t i = 0; i < _g1.header.num_entries; i++)
|
for (uint32_t i = 0; i < _g1.header.num_entries; i++)
|
||||||
{
|
{
|
||||||
_g1.elements[i].offset += reinterpret_cast<uintptr_t>(_g1.data);
|
_g1.elements[i].offset += reinterpret_cast<uintptr_t>(_g1.data.get());
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
@ -241,21 +241,21 @@ bool gfx_load_g1(const IPlatformEnvironment& env)
|
||||||
|
|
||||||
void gfx_unload_g1()
|
void gfx_unload_g1()
|
||||||
{
|
{
|
||||||
SafeFree(_g1.data);
|
_g1.data.reset();
|
||||||
_g1.elements.clear();
|
_g1.elements.clear();
|
||||||
_g1.elements.shrink_to_fit();
|
_g1.elements.shrink_to_fit();
|
||||||
}
|
}
|
||||||
|
|
||||||
void gfx_unload_g2()
|
void gfx_unload_g2()
|
||||||
{
|
{
|
||||||
SafeFree(_g2.data);
|
_g2.data.reset();
|
||||||
_g2.elements.clear();
|
_g2.elements.clear();
|
||||||
_g2.elements.shrink_to_fit();
|
_g2.elements.shrink_to_fit();
|
||||||
}
|
}
|
||||||
|
|
||||||
void gfx_unload_csg()
|
void gfx_unload_csg()
|
||||||
{
|
{
|
||||||
SafeFree(_csg.data);
|
_csg.data.reset();
|
||||||
_csg.elements.clear();
|
_csg.elements.clear();
|
||||||
_csg.elements.shrink_to_fit();
|
_csg.elements.shrink_to_fit();
|
||||||
}
|
}
|
||||||
|
@ -283,7 +283,7 @@ bool gfx_load_g2()
|
||||||
// Fix entry data offsets
|
// Fix entry data offsets
|
||||||
for (uint32_t i = 0; i < _g2.header.num_entries; i++)
|
for (uint32_t i = 0; i < _g2.header.num_entries; i++)
|
||||||
{
|
{
|
||||||
_g2.elements[i].offset += reinterpret_cast<uintptr_t>(_g2.data);
|
_g2.elements[i].offset += reinterpret_cast<uintptr_t>(_g2.data.get());
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
@ -340,7 +340,7 @@ bool gfx_load_csg()
|
||||||
// Fix entry data offsets
|
// Fix entry data offsets
|
||||||
for (uint32_t i = 0; i < _csg.header.num_entries; i++)
|
for (uint32_t i = 0; i < _csg.header.num_entries; i++)
|
||||||
{
|
{
|
||||||
_csg.elements[i].offset += reinterpret_cast<uintptr_t>(_csg.data);
|
_csg.elements[i].offset += reinterpret_cast<uintptr_t>(_csg.data.get());
|
||||||
// RCT1 used zoomed offsets that counted from the beginning of the file, rather than from the current sprite.
|
// RCT1 used zoomed offsets that counted from the beginning of the file, rather than from the current sprite.
|
||||||
if (_csg.elements[i].flags & G1_FLAG_HAS_ZOOM_SPRITE)
|
if (_csg.elements[i].flags & G1_FLAG_HAS_ZOOM_SPRITE)
|
||||||
{
|
{
|
||||||
|
|
|
@ -16,6 +16,7 @@
|
||||||
#include "../world/Location.hpp"
|
#include "../world/Location.hpp"
|
||||||
#include "Text.h"
|
#include "Text.h"
|
||||||
|
|
||||||
|
#include <memory>
|
||||||
#include <optional>
|
#include <optional>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
|
@ -97,7 +98,7 @@ struct rct_gx
|
||||||
{
|
{
|
||||||
rct_g1_header header;
|
rct_g1_header header;
|
||||||
std::vector<rct_g1_element> elements;
|
std::vector<rct_g1_element> elements;
|
||||||
void* data;
|
std::unique_ptr<uint8_t[]> data;
|
||||||
};
|
};
|
||||||
|
|
||||||
struct rct_drawpixelinfo
|
struct rct_drawpixelinfo
|
||||||
|
|
|
@ -160,8 +160,7 @@ void RideObject::ReadLegacy(IReadObjectContext* context, IStream* stream)
|
||||||
_legacyType.vehicles[i].peep_loading_waypoint_segments = 0;
|
_legacyType.vehicles[i].peep_loading_waypoint_segments = 0;
|
||||||
|
|
||||||
auto data = stream->ReadArray<int8_t>(numPeepLoadingPositions);
|
auto data = stream->ReadArray<int8_t>(numPeepLoadingPositions);
|
||||||
_peepLoadingPositions[i] = std::vector<int8_t>(data, data + numPeepLoadingPositions);
|
_peepLoadingPositions[i] = std::vector<int8_t>(data.get(), data.get() + numPeepLoadingPositions);
|
||||||
Memory::Free(data);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -301,10 +301,8 @@ private:
|
||||||
{
|
{
|
||||||
auto s4 = std::make_unique<rct1_s4>();
|
auto s4 = std::make_unique<rct1_s4>();
|
||||||
size_t dataSize = stream->GetLength() - stream->GetPosition();
|
size_t dataSize = stream->GetLength() - stream->GetPosition();
|
||||||
auto deleter_lambda = [dataSize](uint8_t* ptr) { Memory::FreeArray(ptr, dataSize); };
|
auto data = stream->ReadArray<uint8_t>(dataSize);
|
||||||
auto data = std::unique_ptr<uint8_t, decltype(deleter_lambda)>(stream->ReadArray<uint8_t>(dataSize), deleter_lambda);
|
auto decodedData = std::make_unique<uint8_t[]>(sizeof(rct1_s4));
|
||||||
auto decodedData = std::unique_ptr<uint8_t, decltype(&Memory::Free<uint8_t>)>(
|
|
||||||
Memory::Allocate<uint8_t>(sizeof(rct1_s4)), &Memory::Free<uint8_t>);
|
|
||||||
|
|
||||||
size_t decodedSize;
|
size_t decodedSize;
|
||||||
int32_t fileType = sawyercoding_detect_file_type(data.get(), dataSize);
|
int32_t fileType = sawyercoding_detect_file_type(data.get(), dataSize);
|
||||||
|
|
|
@ -73,7 +73,8 @@ namespace SawyerEncoding
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
auto data{ stream->ReadArray<uint8_t>(dataSize) };
|
const auto buffer = stream->ReadArray<uint8_t>(dataSize);
|
||||||
|
const auto* data = buffer.get();
|
||||||
uint32_t checksum = 0;
|
uint32_t checksum = 0;
|
||||||
for (size_t i = 0; i < dataSize; i++, ++data)
|
for (size_t i = 0; i < dataSize; i++, ++data)
|
||||||
{
|
{
|
||||||
|
|
|
@ -140,8 +140,7 @@ void S6Exporter::Save(OpenRCT2::IStream* stream, bool isScenario)
|
||||||
|
|
||||||
// Read all written bytes back into a single buffer
|
// Read all written bytes back into a single buffer
|
||||||
stream->SetPosition(0);
|
stream->SetPosition(0);
|
||||||
auto data = std::unique_ptr<uint8_t, std::function<void(uint8_t*)>>(
|
auto data = stream->ReadArray<uint8_t>(fileSize);
|
||||||
stream->ReadArray<uint8_t>(fileSize), Memory::Free<uint8_t>);
|
|
||||||
uint32_t checksum = sawyercoding_calculate_checksum(data.get(), fileSize);
|
uint32_t checksum = sawyercoding_calculate_checksum(data.get(), fileSize);
|
||||||
|
|
||||||
// Write the checksum on the end
|
// Write the checksum on the end
|
||||||
|
|
Loading…
Reference in New Issue