Skip to content

Commit

Permalink
Refine Stream state checks and clean up RamStream ctor usage
Browse files Browse the repository at this point in the history
  • Loading branch information
malleoz committed Jan 2, 2025
1 parent b0bc801 commit 0355dcf
Show file tree
Hide file tree
Showing 13 changed files with 46 additions and 47 deletions.
22 changes: 15 additions & 7 deletions source/egg/util/Stream.cc
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ Stream::~Stream() = default;

void Stream::skip(u32 count) {
m_index += count;
ASSERT(!eof());
ASSERT(!bad());
}

void Stream::jump(u32 index) {
m_index = index;
ASSERT(!eof());
ASSERT(!bad());
}

void Stream::setEndian(std::endian endian) {
Expand Down Expand Up @@ -66,8 +66,8 @@ f64 Stream::read_f64() {

RamStream::RamStream() : m_buffer(nullptr), m_size(0) {}

RamStream::RamStream(u8 *buffer, u32 size) {
setBufferAndSize(buffer, size);
RamStream::RamStream(const void *buffer, u32 size) {
setBufferAndSize(const_cast<void *>(buffer), size);
}

RamStream::~RamStream() = default;
Expand All @@ -86,15 +86,23 @@ void RamStream::write(void *input, u32 size) {
}
}

bool RamStream::eof() {
bool RamStream::eof() const {
return m_index == m_size;
}

bool RamStream::safe(u32 size) const {
return m_index + size <= m_size;
}

bool RamStream::bad() const {
return m_index > m_size;
}

// Expects a null-terminated char array, and moves the index past the null terminator
std::string RamStream::read_string() {
std::string ret(reinterpret_cast<char *>(m_buffer + m_index));
m_index += ret.size() + 1;
ASSERT(!eof());
ASSERT(!bad());
return ret;
}

Expand All @@ -118,7 +126,7 @@ u8 *RamStream::dataAtIndex() {
RamStream RamStream::split(u32 size) {
RamStream stream = RamStream(m_buffer + m_index, size);
m_index += size;
ASSERT(!eof());
ASSERT(!bad());

return stream;
}
Expand Down
12 changes: 8 additions & 4 deletions source/egg/util/Stream.hh
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ public:

virtual void read(void *output, u32 size) = 0;
virtual void write(void *input, u32 size) = 0;
virtual bool eof() = 0;
virtual bool eof() const = 0;
virtual bool safe(u32 size) const = 0;
virtual bool bad() const = 0;

void skip(u32 count);
void jump(u32 index);
Expand All @@ -41,10 +43,10 @@ protected:
private:
template <ParseableType T>
[[nodiscard]] T read() {
ASSERT(safe(sizeof(T)));
T val;
read(&val, sizeof(val));
m_index += sizeof(val);
ASSERT(!eof());

return parse<T>(val, m_endian);
}
Expand All @@ -58,12 +60,14 @@ private:
class RamStream : public Stream {
public:
RamStream();
RamStream(u8 *buffer, u32 size);
RamStream(const void *buffer, u32 size);
~RamStream() override;

void read(void *output, u32 size) override;
void write(void *input, u32 size) override;
[[nodiscard]] bool eof() override;
[[nodiscard]] bool eof() const override;
[[nodiscard]] bool safe(u32 size) const override;
[[nodiscard]] bool bad() const override;

[[nodiscard]] std::string read_string();
[[nodiscard]] RamStream split(u32 size);
Expand Down
14 changes: 4 additions & 10 deletions source/game/field/KColData.cc
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@ KColData::KColData(const void *file) {
auto addOffset = [](const void *file, u32 offset) -> const void * {
return reinterpret_cast<const void *>(reinterpret_cast<const u8 *>(file) + offset);
};
u8 *unsafeData = reinterpret_cast<u8 *>(const_cast<void *>(file));
EGG::RamStream stream = EGG::RamStream(unsafeData, sizeof(KColHeader));
EGG::RamStream stream = EGG::RamStream(file, sizeof(KColHeader));

u32 posOffset = stream.read_u32();
u32 nrmOffset = stream.read_u32();
Expand Down Expand Up @@ -288,8 +287,7 @@ void KColData::preloadPrisms() {
(reinterpret_cast<uintptr_t>(m_blockData) - reinterpret_cast<uintptr_t>(m_prismData)) /
sizeof(KCollisionPrism);

u8 *unsafeData = reinterpret_cast<u8 *>(const_cast<void *>(m_prismData));
EGG::RamStream stream = EGG::RamStream(unsafeData, sizeof(KCollisionPrism) * prismCount);
EGG::RamStream stream = EGG::RamStream(m_prismData, sizeof(KCollisionPrism) * prismCount);

m_prisms = std::span<KCollisionPrism>(new KCollisionPrism[prismCount], prismCount);

Expand Down Expand Up @@ -317,9 +315,7 @@ void KColData::preloadNormals() {
sizeof(EGG::Vector3f);

m_nrms = std::span<EGG::Vector3f>(new EGG::Vector3f[normalCount], normalCount);

u8 *unsafeData = reinterpret_cast<u8 *>(const_cast<void *>(m_nrmData));
EGG::RamStream stream = EGG::RamStream(unsafeData, sizeof(EGG::Vector3f) * normalCount);
EGG::RamStream stream = EGG::RamStream(m_nrmData, sizeof(EGG::Vector3f) * normalCount);

for (auto &nrm : m_nrms) {
nrm.read(stream);
Expand All @@ -335,9 +331,7 @@ void KColData::preloadVertices() {
sizeof(EGG::Vector3f);

m_vertices = std::span<EGG::Vector3f>(new EGG::Vector3f[vertexCount], vertexCount);

u8 *unsafeData = reinterpret_cast<u8 *>(const_cast<void *>(m_posData));
EGG::RamStream stream = EGG::RamStream(unsafeData, sizeof(EGG::Vector3f) * vertexCount);
EGG::RamStream stream = EGG::RamStream(m_posData, sizeof(EGG::Vector3f) * vertexCount);

for (auto &vert : m_vertices) {
vert.read(stream);
Expand Down
2 changes: 1 addition & 1 deletion source/game/field/ObjectHitTable.cc
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ ObjectHitTable::ObjectHitTable(const char *filename) {
void *file =
System::ResourceManager::Instance()->getFile(filename, &size, System::ArchiveId::Core);

EGG::RamStream stream = EGG::RamStream(reinterpret_cast<u8 *>(file), size);
EGG::RamStream stream = EGG::RamStream(file, size);

m_count = stream.read_s16();
m_fieldCount = stream.read_s16();
Expand Down
11 changes: 4 additions & 7 deletions source/game/kart/KartParamFileManager.cc
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,7 @@ EGG::RamStream KartParamFileManager::getDriverStream(Character character) const
auto *file = reinterpret_cast<ParamFile<KartParam::Stats> *>(m_driverParam.file);
ASSERT(file);
void *offset = &file->params[idx];
u32 size = sizeof(KartParam::Stats);
return EGG::RamStream(reinterpret_cast<u8 *>(offset), size);
return EGG::RamStream(offset, sizeof(KartParam::Stats));
}

EGG::RamStream KartParamFileManager::getVehicleStream(Vehicle vehicle) const {
Expand All @@ -77,8 +76,7 @@ EGG::RamStream KartParamFileManager::getVehicleStream(Vehicle vehicle) const {
auto *file = reinterpret_cast<ParamFile<KartParam::Stats> *>(m_kartParam.file);
ASSERT(file);
void *offset = &file->params[idx];
u32 size = sizeof(KartParam::Stats);
return EGG::RamStream(reinterpret_cast<u8 *>(offset), size);
return EGG::RamStream(offset, sizeof(KartParam::Stats));
}

EGG::RamStream KartParamFileManager::getHitboxStream(Vehicle vehicle) const {
Expand All @@ -92,7 +90,7 @@ EGG::RamStream KartParamFileManager::getHitboxStream(Vehicle vehicle) const {
auto *file = resourceManager->getBsp(vehicle, &size);
ASSERT(file);
ASSERT(size == sizeof(BSP));
return EGG::RamStream(reinterpret_cast<u8 *>(file), size);
return EGG::RamStream(file, size);
}

EGG::RamStream KartParamFileManager::getBikeDispParamsStream(Vehicle vehicle) const {
Expand All @@ -107,8 +105,7 @@ EGG::RamStream KartParamFileManager::getBikeDispParamsStream(Vehicle vehicle) co
auto *file = reinterpret_cast<ParamFile<KartParam::BikeDisp> *>(m_bikeDispParam.file);
ASSERT(file);
void *offset = &file->params[idx];
u32 size = sizeof(KartParam::BikeDisp);
return EGG::RamStream(reinterpret_cast<u8 *>(offset), size);
return EGG::RamStream(offset, sizeof(KartParam::BikeDisp));
}

KartParamFileManager *KartParamFileManager::CreateInstance() {
Expand Down
11 changes: 7 additions & 4 deletions source/game/system/KPadController.cc
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,7 @@ void KPadGhostController::readGhostBuffer(const u8 *buffer, bool driftIsAuto) {
m_ghostBuffer = buffer;
m_driftIsAuto = driftIsAuto;

EGG::RamStream stream =
EGG::RamStream(const_cast<u8 *>(buffer), RKG_UNCOMPRESSED_INPUT_DATA_SECTION_SIZE);
EGG::RamStream stream = EGG::RamStream(buffer, RKG_UNCOMPRESSED_INPUT_DATA_SECTION_SIZE);

u16 faceCount = stream.read_u16();
u16 directionCount = stream.read_u16();
Expand Down Expand Up @@ -270,11 +269,15 @@ u8 KPadGhostButtonsStream::readFrame() {
} else {
if (readIsNewSequence()) {
readSequenceFrames = 0;
currentSequence = buffer.read_u16();

// In the base game, this check normally occurs before the read. As a result, the base
// game does not know that it has run out of inputs until the frame that it tries to
// access past the last valid input. We stray from this behavior so that we can know
// when we are on the last frame of input.
if (buffer.eof()) {
state = 2;
return 0;
}
currentSequence = buffer.read_u16();
}
}

Expand Down
3 changes: 1 addition & 2 deletions source/game/system/map/MapdataCannonPoint.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@
namespace System {

MapdataCannonPoint::MapdataCannonPoint(const SData *data) : m_rawData(data) {
u8 *unsafeData = reinterpret_cast<u8 *>(const_cast<SData *>(data));
EGG::RamStream stream = EGG::RamStream(unsafeData, sizeof(SData));
EGG::RamStream stream = EGG::RamStream(data, sizeof(SData));
read(stream);
}

Expand Down
3 changes: 1 addition & 2 deletions source/game/system/map/MapdataCheckPath.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ namespace System {

/// @addr{0x80515098}
MapdataCheckPath::MapdataCheckPath(const SData *data) : m_rawData(data), m_depth(-1) {
u8 *unsafeData = reinterpret_cast<u8 *>(const_cast<SData *>(data));
EGG::RamStream stream = EGG::RamStream(unsafeData, sizeof(SData));
EGG::RamStream stream = EGG::RamStream(data, sizeof(SData));
read(stream);
m_oneOverCount = 1.0f / m_size;
}
Expand Down
3 changes: 1 addition & 2 deletions source/game/system/map/MapdataCheckPoint.cc
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ namespace System {
/// @addr{0x805154E4}
MapdataCheckPoint::MapdataCheckPoint(const SData *data)
: m_rawData(data), m_nextCount(0), m_prevCount(0) {
u8 *unsafeData = reinterpret_cast<u8 *>(const_cast<SData *>(data));
EGG::RamStream stream = EGG::RamStream(unsafeData, sizeof(SData));
EGG::RamStream stream = EGG::RamStream(data, sizeof(SData));
read(stream);
m_midpoint = 0.5f * (m_left + m_right);
m_dir = EGG::Vector2f(m_right.y - m_left.y, m_left.x - m_right.x);
Expand Down
3 changes: 1 addition & 2 deletions source/game/system/map/MapdataGeoObj.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@
namespace System {

MapdataGeoObj::MapdataGeoObj(const SData *data) : m_rawData(data) {
u8 *unsafeData = reinterpret_cast<u8 *>(const_cast<SData *>(data));
EGG::RamStream stream = EGG::RamStream(unsafeData, sizeof(SData));
EGG::RamStream stream = EGG::RamStream(data, sizeof(SData));
read(stream);
}

Expand Down
3 changes: 1 addition & 2 deletions source/game/system/map/MapdataJugemPoint.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ namespace System {

/// @addr{0x805183A8}
MapdataJugemPoint::MapdataJugemPoint(const SData *data) : m_rawData(data) {
u8 *unsafeData = reinterpret_cast<u8 *>(const_cast<SData *>(data));
EGG::RamStream stream = EGG::RamStream(unsafeData, sizeof(SData));
EGG::RamStream stream = EGG::RamStream(data, sizeof(SData));
read(stream);
}

Expand Down
3 changes: 1 addition & 2 deletions source/game/system/map/MapdataStageInfo.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@
namespace System {

MapdataStageInfo::MapdataStageInfo(const SData *data) : m_rawData(data) {
u8 *unsafeData = reinterpret_cast<u8 *>(const_cast<SData *>(data));
EGG::RamStream stream = EGG::RamStream(unsafeData, sizeof(SData));
EGG::RamStream stream = EGG::RamStream(data, sizeof(SData));
read(stream);
}

Expand Down
3 changes: 1 addition & 2 deletions source/game/system/map/MapdataStartPoint.cc
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,7 @@ static constexpr s8 Z_TRANSLATION_TABLE[12][12] = {
};

MapdataStartPoint::MapdataStartPoint(const SData *data) : m_rawData(data) {
u8 *unsafeData = reinterpret_cast<u8 *>(const_cast<SData *>(data));
EGG::RamStream stream = EGG::RamStream(unsafeData, sizeof(SData));
EGG::RamStream stream = EGG::RamStream(data, sizeof(SData));
read(stream);
}

Expand Down

0 comments on commit 0355dcf

Please sign in to comment.