Skip to content

Commit 6eef468

Browse files
committed
Changing cdrom-loader API to use buffers.
1 parent c698b85 commit 6eef468

File tree

3 files changed

+37
-27
lines changed

3 files changed

+37
-27
lines changed

src/mips/psyqo-paths/cdrom-loader.hh

Lines changed: 32 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,11 @@ SOFTWARE.
2727
#pragma once
2828

2929
#include <EASTL/string_view.h>
30-
#include <EASTL/vector.h>
3130
#include <stdint.h>
3231

3332
#include <coroutine>
3433

35-
#include "psyqo/gpu.hh"
34+
#include "psyqo/buffer.hh"
3635
#include "psyqo/iso9660-parser.hh"
3736
#include "psyqo/task.hh"
3837

@@ -44,7 +43,7 @@ namespace psyqo::paths {
4443
* @details This class provides a PSYQo path to read files from the CDRom.
4544
* The way to use it is to instantiate the class somewhere persistent, and
4645
* then call readFile() with a callback. The callback will be called with
47-
* the data of the file, or an empty vector if the file could not be read.
46+
* the data of the file, or an empty buffer if the file could not be read.
4847
* This is going to allocate memory in different places. Only one file can
4948
* be read at a time, but it is safe to call readFile() again from the
5049
* callback. If preferred, the loader can be cascaded into another `TaskQueue`.
@@ -54,59 +53,70 @@ namespace psyqo::paths {
5453

5554
class CDRomLoader {
5655
struct ReadFileAwaiter {
57-
ReadFileAwaiter(eastl::string_view path, GPU &gpu, ISO9660Parser &parser, CDRomLoader &loader)
58-
: m_path(path), m_gpu(gpu), m_parser(parser), m_loader(loader) {}
56+
ReadFileAwaiter(eastl::string_view path, ISO9660Parser &parser, CDRomLoader &loader)
57+
: m_path(path), m_parser(parser), m_loader(loader) {}
5958
~ReadFileAwaiter() {}
6059
constexpr bool await_ready() const { return false; }
6160
template <typename U>
6261
void await_suspend(std::coroutine_handle<U> handle) {
63-
m_loader.readFile(m_path, m_gpu, m_parser, [handle, this](eastl::vector<uint8_t> &&data) {
62+
m_loader.readFile(m_path, m_parser, [handle, this](Buffer<uint8_t> &&data) {
6463
m_data = eastl::move(data);
6564
handle.resume();
6665
});
6766
}
68-
eastl::vector<uint8_t> await_resume() { return eastl::move(m_data); }
67+
Buffer<uint8_t> await_resume() { return eastl::move(m_data); }
6968

7069
private:
7170
eastl::string_view m_path;
72-
GPU &m_gpu;
7371
ISO9660Parser &m_parser;
7472
CDRomLoader &m_loader;
75-
eastl::vector<uint8_t> m_data;
73+
Buffer<uint8_t> m_data;
7674
};
7775

7876
public:
77+
/**
78+
* @brief Set the Buffer object for the next read operation.
79+
*
80+
* @details This function sets the buffer to be used for the next read
81+
* operation. By default, the archive manager will allocate a buffer of the
82+
* appropriate size for the file being read. However, if the user wants to
83+
* use an already allocated buffer, they can use this function to set the buffer
84+
* to be used.
85+
*
86+
* @param buffer The buffer to be used for the next read operation.
87+
*/
88+
void setBuffer(Buffer<uint8_t> &&buffer) { m_data = eastl::move(buffer); }
89+
7990
/**
8091
* @brief Reads a file from the CDRom.
8192
*
8293
* @param path The path to the file to read. The view must be persistent
8394
* until the callback is called.
84-
* @param gpu The GPU class used by the application, in order to set timers.
8595
* @param parser The ISO9660Parser to use for reading the file.
8696
* @param callback The callback to call when the file is read. The callback
87-
* will be called with the data of the file, or an empty vector if the file
97+
* will be called with the data of the file, or an empty buffer if the file
8898
* could not be read.
8999
*/
90-
void readFile(eastl::string_view path, GPU &gpu, ISO9660Parser &parser,
91-
eastl::function<void(eastl::vector<uint8_t> &&)> &&callback) {
92-
setupQueue(path, gpu, parser, eastl::move(callback));
100+
void readFile(eastl::string_view path, ISO9660Parser &parser,
101+
eastl::function<void(Buffer<uint8_t> &&)> &&callback) {
102+
setupQueue(path, parser, eastl::move(callback));
93103
m_queue.run();
94104
}
95-
psyqo::TaskQueue::Task scheduleReadFile(eastl::string_view path, GPU &gpu, ISO9660Parser &parser) {
96-
setupQueue(path, gpu, parser, {});
105+
psyqo::TaskQueue::Task scheduleReadFile(eastl::string_view path, ISO9660Parser &parser) {
106+
setupQueue(path, parser, {});
97107
return m_queue.schedule();
98108
}
99-
ReadFileAwaiter readFile(eastl::string_view path, GPU &gpu, ISO9660Parser &parser) {
100-
return {path, gpu, parser, *this};
109+
ReadFileAwaiter readFile(eastl::string_view path, ISO9660Parser &parser) {
110+
return {path, parser, *this};
101111
}
102112

103113
private:
104-
void setupQueue(eastl::string_view path, GPU &gpu, ISO9660Parser &parser,
105-
eastl::function<void(eastl::vector<uint8_t> &&)> &&callback);
106-
eastl::function<void(eastl::vector<uint8_t> &&)> m_callback;
114+
void setupQueue(eastl::string_view path, ISO9660Parser &parser,
115+
eastl::function<void(Buffer<uint8_t> &&)> &&callback);
116+
eastl::function<void(Buffer<uint8_t> &&)> m_callback;
107117
psyqo::TaskQueue m_queue;
108118
ISO9660Parser::ReadRequest m_request;
109-
eastl::vector<uint8_t> m_data;
119+
Buffer<uint8_t> m_data;
110120
bool m_pending = false;
111121
};
112122

src/mips/psyqo-paths/examples/cdrom-loader/cdrom-loader.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ class CDRomLoaderExample final : public psyqo::Application {
4444
psyqo::CDRomDevice m_cdrom;
4545
psyqo::ISO9660Parser m_isoParser = psyqo::ISO9660Parser(&m_cdrom);
4646
psyqo::paths::CDRomLoader m_cdromLoader;
47-
eastl::vector<uint8_t> m_buffer;
47+
psyqo::Buffer<uint8_t> m_buffer;
4848
bool m_callbackCalled = false;
4949
};
5050

@@ -70,8 +70,8 @@ void CDRomLoaderExample::prepare() {
7070
void CDRomLoaderExample::createScene() {
7171
m_font.uploadSystemFont(gpu());
7272
pushScene(&cdromLoaderExampleScene);
73-
m_cdromLoader.readFile("SYSTEM.CNF;1", cdromLoaderExample.gpu(), cdromLoaderExample.m_isoParser,
74-
[this](eastl::vector<uint8_t>&& buffer) {
73+
m_cdromLoader.readFile("SYSTEM.CNF;1", cdromLoaderExample.m_isoParser,
74+
[this](psyqo::Buffer<uint8_t>&& buffer) {
7575
m_buffer = eastl::move(buffer);
7676
m_callbackCalled = true;
7777
});

src/mips/psyqo-paths/src/cdrom-loader.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ SOFTWARE.
2828

2929
#include "psyqo/kernel.hh"
3030

31-
void psyqo::paths::CDRomLoader::setupQueue(eastl::string_view path, GPU& gpu, psyqo::ISO9660Parser& parser,
32-
eastl::function<void(eastl::vector<uint8_t>&&)>&& callback) {
31+
void psyqo::paths::CDRomLoader::setupQueue(eastl::string_view path, psyqo::ISO9660Parser& parser,
32+
eastl::function<void(Buffer<uint8_t>&&)>&& callback) {
3333
Kernel::assert(!m_pending, "Only one file can be read at a time");
3434
m_queue.reset();
3535
m_pending = true;

0 commit comments

Comments
 (0)