persona-3-dual
A DS demake of Persona 3
Loading...
Searching...
No Matches
IOManager.hpp
Go to the documentation of this file.
1
8
9#pragma once
10#include "core/logger.hpp"
11#include <aegis/manager.hpp>
12#include <cstdlib>
13#include <nds.h>
14#include <type_traits>
15
23{
24 public:
28 FileBuffer() = default;
29
36 {
37 }
38
43 {
44 std::free(data);
45 }
46
50 FileBuffer(const FileBuffer&) = delete;
51
55 FileBuffer& operator=(const FileBuffer&) = delete;
56
61 FileBuffer(FileBuffer&& other) noexcept : data(other.data), size(other.size)
62 {
63 other.data = nullptr;
64 other.size = 0;
65 }
66
72 FileBuffer& operator=(FileBuffer&& other) noexcept
73 {
74 if (this != &other)
75 {
76 std::free(data);
77 data = other.data;
78 size = other.size;
79 other.data = nullptr;
80 other.size = 0;
81 }
82 return *this;
83 }
84
89 void* get() const
90 {
91 return data;
92 }
93
98 u32 length() const
99 {
100 return size;
101 }
102
108 void* release()
109 {
110 void* result = data;
111 data = nullptr;
112 size = 0;
113 return result;
114 }
115
116 private:
117 void* data = nullptr;
118 u32 size = 0;
119};
120
121class IOManager : public ae::Manager, public ae::Singleton<IOManager>
122{
123 public:
124 void init() override;
125
126 void process() override
127 {
128 }
129
130 void shutdown() override
131 {
132 }
133
141 template <typename T>
142 requires std::is_trivially_copyable_v<T>
143 bool readFile(T* data, std::string filePath)
144 {
145 if (data == nullptr)
146 {
147 logMessage(LogSeverity::ERROR, "Called with null destination buffer");
148 return false;
149 }
150
151 // open file
152 std::string path = basePath + filePath;
153 FILE* file = fopen(path.c_str(), "rb");
154 if (!file)
155 {
156 logMessage(LogSeverity::WARNING, "Failed to open file for read: " + path);
157 return false;
158 }
159
160 // read data
161 size_t readCount = fread(data, sizeof(T), 1, file);
162 if (readCount != 1)
163 {
164 logMessage(LogSeverity::ERROR, "Failed to read expected data from: " + path);
165 fclose(file);
166 return false;
167 }
168
169 // close file
170 fclose(file);
171
172 return true;
173 }
174
182 template <typename T>
183 requires std::is_trivially_copyable_v<T>
184 bool writeFile(T* data, std::string filePath)
185 {
186 if (data == nullptr)
187 {
188 logMessage(LogSeverity::ERROR, "Called with null source data");
189 return false;
190 }
191
192 // open file
193 std::string tempPath = basePath + filePath + ".tmp";
194 std::string path = basePath + filePath;
195
196 FILE* file = fopen(tempPath.c_str(), "wb");
197 if (!file)
198 {
199 logMessage(LogSeverity::ERROR, "Failed to open temporary file for write: " + tempPath);
200 return false;
201 }
202
203 // write data
204 size_t written = fwrite(data, sizeof(T), 1, file);
205 if (written != 1)
206 {
207 logMessage(LogSeverity::ERROR, "Failed to write complete data to: " + tempPath);
208 fclose(file);
209 return false;
210 }
211
212 // close file
213 fclose(file);
214
215 // replace old file with new file
216 remove(path.c_str());
217 rename(tempPath.c_str(), path.c_str());
218
219 return true;
220 }
221
228 void* loadToRAM(const std::string& filePath, u32* outSize);
229
234 void unloadFromRAM(void* buffer);
235
241 void* openFile(const std::string& path);
242
250 void* openFile(const std::string& path, u32& size);
251
252 FileBuffer openFileBuffer(const std::string& path);
253
271 std::string getAssetFilePath(const std::string& path, const char* suffix);
272
273 private:
274 friend class Singleton<IOManager>;
275 IOManager() = default;
276
277 std::string basePath;
278};
Owns a heap-allocated file buffer and its length.
Definition IOManager.hpp:23
FileBuffer & operator=(const FileBuffer &)=delete
Copy assignment is disabled to preserve unique ownership.
void * release()
Releases the file data to the caller.
void * get() const
Gets the owned file data without transferring ownership.
Definition IOManager.hpp:89
FileBuffer(FileBuffer &&other) noexcept
Transfers ownership from another file buffer.
Definition IOManager.hpp:61
u32 length() const
Gets the file data length.
Definition IOManager.hpp:98
void * data
~FileBuffer()
Releases the owned file data.
Definition IOManager.hpp:42
FileBuffer(void *data, u32 size)
Constructs a file buffer that takes ownership of data.
Definition IOManager.hpp:35
FileBuffer()=default
Constructs an empty file buffer.
FileBuffer & operator=(FileBuffer &&other) noexcept
Releases current data and transfers ownership from another buffer.
Definition IOManager.hpp:72
FileBuffer(const FileBuffer &)=delete
Copy construction is disabled to preserve unique ownership.
void * loadToRAM(const std::string &filePath, u32 *outSize)
Loads an entire file into a buffer.
Definition IOManager.cpp:11
std::string basePath
bool writeFile(T *data, std::string filePath)
Writes raw data of type T to a file, atomically via a temp file.
IOManager()=default
FileBuffer openFileBuffer(const std::string &path)
void unloadFromRAM(void *buffer)
Releases a buffer previously returned by loadToRAM().
Definition IOManager.cpp:63
void * openFile(const std::string &path)
Open a file and return a pointer to its contents.
Definition IOManager.cpp:96
void shutdown() override
std::string getAssetFilePath(const std::string &path, const char *suffix)
Resolves the on-disk path for an asset, allowing it to be stored either as a flat file or grouped in ...
Definition IOManager.cpp:72
void process() override
void init() override
Definition IOManager.cpp:5
bool readFile(T *data, std::string filePath)
Reads raw data of type T from a file into data.
void logMessage(LogSeverity severity, std::string_view message, std::source_location location)
Logs a diagnostic message and halts only for fatal messages.
Definition logger.cpp:110
Util to handle error logging.