Refactor IStream::ReadArray to return unique_ptr

This commit is contained in:
Matt 2021-02-12 19:30:53 +02:00
parent 34b1506c5d
commit c30bd1266f
No known key found for this signature in database
GPG Key ID: 6D4C24A61C93E208
11 changed files with 27 additions and 32 deletions

View File

@ -97,8 +97,7 @@ static bool TryClassifyAsS4(OpenRCT2::IStream* stream, ClassifiedFileInfo* resul
try
{
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);
int32_t fileTypeVersion = sawyercoding_detect_file_type(data.get(), dataLength);
@ -134,8 +133,8 @@ static bool TryClassifyAsTD4_TD6(OpenRCT2::IStream* stream, ClassifiedFileInfo*
try
{
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);
if (sawyercoding_validate_track_checksum(data.get(), dataLength))

View File

@ -998,7 +998,7 @@ bool RCT1DataPresentAtLocation(const utf8* 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;
}

View File

@ -258,5 +258,5 @@ std::string FindCsg1datAtLocation(const utf8* path);
bool Csg1datPresentAtLocation(const utf8* path);
std::string FindCsg1idatAtLocation(const utf8* path);
bool Csg1idatPresentAtLocation(const utf8* path);
bool CsgIsUsable(rct_gx csg);
bool CsgIsUsable(const rct_gx& csg);
bool CsgAtLocationIsUsable(const utf8* path);

View File

@ -161,10 +161,8 @@ template<> struct DataSerializerTraits_t<std::string>
res = "";
return;
}
const char* str = stream->ReadArray<char>(len);
res.assign(str, len);
Memory::FreeArray(str, len);
auto str = stream->ReadArray<char>(len);
res.assign(str.get(), len);
}
static void log(OpenRCT2::IStream* stream, const std::string& str)
{
@ -620,9 +618,8 @@ template<> struct DataSerializerTraits_t<rct_object_entry>
uint32_t temp;
stream->Read(&temp);
val.flags = ByteSwapBE(temp);
const char* str = stream->ReadArray<char>(12);
memcpy(val.nameWOC, str, 12);
Memory::FreeArray(str, 12);
auto str = stream->ReadArray<char>(12);
memcpy(val.nameWOC, str.get(), 12);
}
static void log(OpenRCT2::IStream* stream, const rct_object_entry& val)
{

View File

@ -14,6 +14,7 @@
#include "Memory.hpp"
#include <istream>
#include <memory>
#include <stdexcept>
#include <string>
#include <vector>
@ -191,10 +192,10 @@ namespace OpenRCT2
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);
Read(buffer, sizeof(T) * count);
auto buffer = std::make_unique<T[]>(count);
Read(buffer.get(), sizeof(T) * count);
return buffer;
}

View File

@ -220,7 +220,7 @@ bool gfx_load_g1(const IPlatformEnvironment& env)
// Fix entry data offsets
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;
}
@ -241,21 +241,21 @@ bool gfx_load_g1(const IPlatformEnvironment& env)
void gfx_unload_g1()
{
SafeFree(_g1.data);
_g1.data.reset();
_g1.elements.clear();
_g1.elements.shrink_to_fit();
}
void gfx_unload_g2()
{
SafeFree(_g2.data);
_g2.data.reset();
_g2.elements.clear();
_g2.elements.shrink_to_fit();
}
void gfx_unload_csg()
{
SafeFree(_csg.data);
_csg.data.reset();
_csg.elements.clear();
_csg.elements.shrink_to_fit();
}
@ -283,7 +283,7 @@ bool gfx_load_g2()
// Fix entry data offsets
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;
}
@ -340,7 +340,7 @@ bool gfx_load_csg()
// Fix entry data offsets
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.
if (_csg.elements[i].flags & G1_FLAG_HAS_ZOOM_SPRITE)
{

View File

@ -16,6 +16,7 @@
#include "../world/Location.hpp"
#include "Text.h"
#include <memory>
#include <optional>
#include <vector>
@ -97,7 +98,7 @@ struct rct_gx
{
rct_g1_header header;
std::vector<rct_g1_element> elements;
void* data;
std::unique_ptr<uint8_t[]> data;
};
struct rct_drawpixelinfo

View File

@ -160,8 +160,7 @@ void RideObject::ReadLegacy(IReadObjectContext* context, IStream* stream)
_legacyType.vehicles[i].peep_loading_waypoint_segments = 0;
auto data = stream->ReadArray<int8_t>(numPeepLoadingPositions);
_peepLoadingPositions[i] = std::vector<int8_t>(data, data + numPeepLoadingPositions);
Memory::Free(data);
_peepLoadingPositions[i] = std::vector<int8_t>(data.get(), data.get() + numPeepLoadingPositions);
}
}

View File

@ -301,10 +301,8 @@ private:
{
auto s4 = std::make_unique<rct1_s4>();
size_t dataSize = stream->GetLength() - stream->GetPosition();
auto deleter_lambda = [dataSize](uint8_t* ptr) { Memory::FreeArray(ptr, dataSize); };
auto data = std::unique_ptr<uint8_t, decltype(deleter_lambda)>(stream->ReadArray<uint8_t>(dataSize), deleter_lambda);
auto decodedData = std::unique_ptr<uint8_t, decltype(&Memory::Free<uint8_t>)>(
Memory::Allocate<uint8_t>(sizeof(rct1_s4)), &Memory::Free<uint8_t>);
auto data = stream->ReadArray<uint8_t>(dataSize);
auto decodedData = std::make_unique<uint8_t[]>(sizeof(rct1_s4));
size_t decodedSize;
int32_t fileType = sawyercoding_detect_file_type(data.get(), dataSize);

View File

@ -73,7 +73,8 @@ namespace SawyerEncoding
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;
for (size_t i = 0; i < dataSize; i++, ++data)
{

View File

@ -140,8 +140,7 @@ void S6Exporter::Save(OpenRCT2::IStream* stream, bool isScenario)
// Read all written bytes back into a single buffer
stream->SetPosition(0);
auto data = std::unique_ptr<uint8_t, std::function<void(uint8_t*)>>(
stream->ReadArray<uint8_t>(fileSize), Memory::Free<uint8_t>);
auto data = stream->ReadArray<uint8_t>(fileSize);
uint32_t checksum = sawyercoding_calculate_checksum(data.get(), fileSize);
// Write the checksum on the end