A reimplementation of Mario Kart Wii's physics engine in C++
Loading...
Searching...
No Matches
Stream.hh
1#pragma once
2
3#include <Common.hh>
4
5#include <string>
6
7namespace EGG {
8
10class Stream {
11public:
12 Stream();
13 virtual ~Stream();
14
15 virtual void read(void *output, u32 size) = 0;
16 virtual void write(void *input, u32 size) = 0;
17 virtual bool eof() const = 0;
18 virtual bool safe(u32 size) const = 0;
19 virtual bool bad() const = 0;
20
21 void skip(u32 count);
22 void jump(u32 index);
23
24 void setEndian(std::endian endian) {
25 m_endian = endian;
26 }
27
28 [[nodiscard]] u32 index() const {
29 return m_index;
30 }
31
32 [[nodiscard]] u8 read_u8();
33 [[nodiscard]] u16 read_u16();
34 [[nodiscard]] u32 read_u32();
35 [[nodiscard]] u64 read_u64();
36 [[nodiscard]] s8 read_s8();
37 [[nodiscard]] s16 read_s16();
38 [[nodiscard]] s32 read_s32();
39 [[nodiscard]] s64 read_s64();
40 [[nodiscard]] f32 read_f32();
41 [[nodiscard]] f64 read_f64();
42
43protected:
44 std::endian m_endian;
45 u32 m_index;
46
47private:
48 template <ParseableType T>
49 [[nodiscard]] T read() {
50 ASSERT(safe(sizeof(T)));
51 T val;
52 read(&val, sizeof(val));
53 m_index += sizeof(val);
54
55 return parse<T>(val, m_endian);
56 }
57};
58
64class RamStream : public Stream {
65public:
66 RamStream();
67 RamStream(const void *buffer, u32 size);
68 ~RamStream() override = default;
69
70 void read(void *output, u32 size) override;
71 void write(void *input, u32 size) override;
72
73 [[nodiscard]] bool eof() const override {
74 return m_index == m_size;
75 }
76
77 [[nodiscard]] bool safe(u32 size) const override {
78 return m_index + size <= m_size;
79 }
80
81 [[nodiscard]] bool bad() const override {
82 return m_index > m_size;
83 }
84
85 [[nodiscard]] std::string read_string();
86 [[nodiscard]] RamStream split(u32 size);
87 void setBufferAndSize(void *buffer, u32 size);
88
89 [[nodiscard]] u8 *data() {
90 return m_buffer;
91 }
92
93 [[nodiscard]] u8 *dataAtIndex() {
94 return m_buffer + m_index;
95 }
96
97private:
98 u8 *m_buffer;
99 u32 m_size;
100};
101
102} // namespace EGG
This header houses common data types such as our integral types and enums.
A stream of data stored in memory.
Definition Stream.hh:64
RamStream split(u32 size)
Splits the current stream into two.
Definition Stream.cc:96
A stream of data, abstracted to allow for continuous seeking.
Definition Stream.hh:10
EGG core library.
Definition Archive.cc:6