libeditwl
Lightweight C++ library for Nintendo DS(i) formats
Loading...
Searching...
No Matches
twl_Include.hpp
1
2#pragma once
3#include <cstdint>
4#include <string>
5#include <functional>
6#include <climits>
7
8#define TWL_ATTR_PACKED __attribute__((packed))
9
10namespace twl {
11
12 using u8 = uint8_t;
13 using u16 = uint16_t;
14 using u32 = uint32_t;
15 using u64 = uint64_t;
16
17 using i8 = int8_t;
18 using i16 = int16_t;
19 using i32 = int32_t;
20 using i64 = int64_t;
21
22 class ScopeGuard {
23 public:
24 using Fn = std::function<void()>;
25
26 private:
27 bool run;
28 Fn exit_fn;
29
30 public:
31 ScopeGuard(Fn &&fn) : run(true), exit_fn(fn) {}
32
33 ~ScopeGuard() {
34 if(this->run) {
35 this->exit_fn();
36 }
37 }
38
39 inline void Cancel() {
40 this->run = false;
41 }
42 };
43
44}
45
46#define TWL_R_SUCCEED() return ::twl::ResultSuccess
47
48#define TWL_R_FAIL(rc) { \
49 const auto _tmp_rc = (rc); \
50 return _tmp_rc; \
51}
52
53#define TWL_R_TRY(rc) { \
54 const auto _tmp_rc = (rc); \
55 if(_tmp_rc.IsFailure()) { \
56 return _tmp_rc; \
57 } \
58}
59
60namespace twl {
61
62 struct Result {
63 using BaseType = u16;
64
65 BaseType value;
66
67 static constexpr BaseType SuccessValue = BaseType();
68
69 constexpr Result() : value() {}
70 constexpr Result(const BaseType val) : value(val) {}
71
72 inline constexpr bool IsSuccess() const {
73 return this->value == SuccessValue;
74 }
75
76 inline constexpr bool IsFailure() const {
77 return !this->IsSuccess();
78 }
79
80 inline std::string GetDescription() const;
81 };
82
83 constexpr Result ResultSuccess = 0;
84 constexpr Result ResultUnknownResult = 0x0002;
85
86 constexpr Result ResultUnsupportedPixelFormat = 0x0101;
87 constexpr Result ResultInvalidSizeForPixelFormat = 0x0102;
88 constexpr Result ResultColorPaletteAllocationFailure = 0x0103;
89 constexpr Result ResultInvalidPixelFormat = 0x0104;
90 constexpr Result ResultInvalidCharacterFormat = 0x0105;
91 constexpr Result ResultInvalidScreenData = 0x0106;
92 constexpr Result ResultScreenDataGenerationUnimplemented = 0x0107;
93 constexpr Result ResultUnexpectedBannerIconDimensions = 0x0108;
94
95 constexpr Result ResultInvalidFile = 0x0201;
96 constexpr Result ResultEndOfData = 0x0202;
97 constexpr Result ResultReadNotSupported = 0x0203;
98 constexpr Result ResultWriteNotSupported = 0x0204;
99 constexpr Result ResultUnexpectedReadSize = 0x0205;
100 constexpr Result ResultFileNotCompressed = 0x0206;
101 constexpr Result ResultInvalidEndSeekOffset = 0x0207;
102 constexpr Result ResultInvalidFileMode = 0x0208;
103 constexpr Result ResultUnableToOpenFile = 0x0209;
104 constexpr Result ResultInvalidSeekWhence = 0x020a;
105 constexpr Result ResultUnableToSeekFile = 0x020b;
106 constexpr Result ResultUnableToReadFile = 0x020c;
107 constexpr Result ResultUnableToWriteFile = 0x020d;
108 constexpr Result ResultUnableToCloseFile = 0x020e;
109 constexpr Result ResultUnableToSeekBuffer = 0x020f;
110 constexpr Result ResultUnableToReadBuffer = 0x0210;
111 constexpr Result ResultUnableToWriteBuffer = 0x0211;
112 constexpr Result ResultFileAlreadyOpened = 0x0212;
113 constexpr Result ResultFileAlreadyClosed = 0x0213;
114 constexpr Result ResultFileNotInitialized = 0x0214;
115
116 constexpr Result ResultNitroFsDirectoryNotFound = 0x0301;
117 constexpr Result ResultNitroFsFileNotFound = 0x0302;
118
119 constexpr Result ResultBMGInvalidHeader = 0x0401;
120 constexpr Result ResultBMGInvalidInfoSection = 0x0402;
121 constexpr Result ResultBMGInvalidDataSection = 0x0403;
122 constexpr Result ResultBMGInvalidUnsupportedEncoding = 0x0404;
123 constexpr Result ResultBMGInvalidMessageAttributeSize = 0x0405;
124 constexpr Result ResultBMGInvalidEscapeSequence = 0x0406;
125 constexpr Result ResultBMGUnexpectedSectionCount = 0x0407;
126 constexpr Result ResultBMGInvalidMessageIdSection = 0x0408;
127
128 constexpr Result ResultNCGRInvalidHeader = 0x0501;
129 constexpr Result ResultNCGRInvalidCharacterDataBlock = 0x0502;
130 constexpr Result ResultNCGRInvalidCharacterPositionBlock = 0x0503;
131
132 constexpr Result ResultNCLRInvalidHeader = 0x0601;
133 constexpr Result ResultNCLRInvalidPaletteDataBlock = 0x0602;
134
135 constexpr Result ResultNSCRInvalidHeader = 0x0701;
136 constexpr Result ResultNSCRInvalidScreenDataBlock = 0x0702;
137 constexpr Result ResultNSCRWriteNotSupported = 0x0703;
138
139 constexpr Result ResultNARCInvalidHeader = 0x0801;
140 constexpr Result ResultNARCInvalidFileAllocationTableBlock = 0x0802;
141 constexpr Result ResultNARCInvalidFileNameTableBlock = 0x0803;
142 constexpr Result ResultNARCInvalidFileImageBlock = 0x0804;
143
144 constexpr Result ResultSDATInvalidHeader = 0x0901;
145 constexpr Result ResultSDATInvalidSymbolBlock = 0x0902;
146 constexpr Result ResultSDATInvalidInfoBlock = 0x0903;
147 constexpr Result ResultSDATInvalidFileAllocationTableBlock = 0x0904;
148 constexpr Result ResultSDATInvalidFileBlock = 0x0905;
149 constexpr Result ResultSDATInvalidSymbols = 0x0906;
150 constexpr Result ResultSDATEntryNotFound = 0x0907;
151 constexpr Result ResultSDATInvalidEntryFormat = 0x0908;
152
153 constexpr Result ResultSSEQInvalidHeader = 0x0a01;
154 constexpr Result ResultSSEQInvalidDataSection = 0x0a02;
155 constexpr Result ResultSSEQWriteNotSupported = 0x0a03;
156
157 constexpr Result ResultSBNKInvalidHeader = 0x0b01;
158 constexpr Result ResultSBNKInvalidDataSection = 0x0b02;
159 constexpr Result ResultSBNKWriteNotSupported = 0x0b03;
160
161 constexpr Result ResultSWARInvalidHeader = 0x0c01;
162 constexpr Result ResultSWARInvalidDataSection = 0x0c02;
163 constexpr Result ResultSWARWriteNotSupported = 0x0c03;
164
165 constexpr Result ResultSTRMInvalidHeader = 0x0d01;
166 constexpr Result ResultSTRMInvalidHeadSection = 0x0d02;
167 constexpr Result ResultSTRMInvalidDataSection = 0x0d03;
168 constexpr Result ResultSTRMWriteNotSupported = 0x0d04;
169
170 constexpr Result ResultROMInvalidUnitCode = 0x0e01;
171 constexpr Result ResultROMInvalidNintendoLogoCRC16 = 0x0e02;
172
173 constexpr Result ResultCompressionInvalidLzFormat = 0x0f01;
174 constexpr Result ResultCompressionTooBigCompressSize = 0x0f02;
175 constexpr Result ResultCompressionInvalidRepeatSize = 0x0f03;
176
177 constexpr Result ResultUtilityInvalidSections = 0x1001;
178
179 using ResultDescriptionEntry = std::pair<Result, const char*>;
180
181 constexpr ResultDescriptionEntry ResultDescriptionTable[] = {
182 { ResultSuccess, "Success" },
183 { ResultUnknownResult, "Unknown result value" },
184
185 { ResultUnsupportedPixelFormat, "Unsupported pixel format" },
186 { ResultInvalidSizeForPixelFormat, "Invalid size for given pixel format" },
187 { ResultColorPaletteAllocationFailure, "Unable to allocate color in palette" },
188 { ResultInvalidPixelFormat, "Invalid pixel format" },
189 { ResultInvalidCharacterFormat, "Invalid (graphics) character format" },
190 { ResultInvalidScreenData, "Invalid screen (NSCR) data" },
191 { ResultScreenDataGenerationUnimplemented, "Unimplemented feature: screen data saving/generation" },
192 { ResultUnexpectedBannerIconDimensions, "Unexpected banner icon dimensions" },
193
194 { ResultInvalidFile, "Invalid file" },
195 { ResultEndOfData, "End of data (EOF)" },
196 { ResultReadNotSupported, "Read not supported in file" },
197 { ResultWriteNotSupported, "Write not supported in file" },
198 { ResultUnexpectedReadSize, "Got unexpected read size" },
199 { ResultFileNotCompressed, "File is not compressed" },
200 { ResultInvalidEndSeekOffset, "Invalid offset for end seeking (must be 0)" },
201 { ResultInvalidFileMode, "Invalid file open mode" },
202 { ResultUnableToOpenFile, "Unable to open file" },
203 { ResultInvalidSeekWhence, "Invalid seek whence" },
204 { ResultUnableToSeekFile, "Unable to seek file" },
205 { ResultUnableToReadFile, "Unable to read file" },
206 { ResultUnableToWriteFile, "Unable to write file" },
207 { ResultUnableToCloseFile, "Unable to close file" },
208 { ResultUnableToSeekBuffer, "Unable to seek buffer" },
209 { ResultUnableToReadBuffer, "Unable to read buffer" },
210 { ResultUnableToWriteBuffer, "Unable to write buffer" },
211 { ResultFileAlreadyOpened, "File is already opened" },
212 { ResultFileAlreadyClosed, "File is already closed" },
213 { ResultFileNotInitialized, "File is not initialized / not valid" },
214
215 { ResultNitroFsDirectoryNotFound, "NitroFs directory not found" },
216 { ResultNitroFsFileNotFound, "NitroFs file not found" },
217
218 { ResultBMGInvalidHeader, "Invalid BMG header" },
219 { ResultBMGInvalidInfoSection, "Invalid BMG INF1 section" },
220 { ResultBMGInvalidDataSection, "Invalid BMG DAT1 section" },
221 { ResultBMGInvalidUnsupportedEncoding, "Invalid or unsupported BMG encoding" },
222 { ResultBMGInvalidMessageAttributeSize, "Invalid BMG message attribute size" },
223 { ResultBMGInvalidEscapeSequence, "Invalid BMG escape sequence" },
224 { ResultBMGUnexpectedSectionCount, "Unexpected BMG section count (< 2)" },
225 { ResultBMGInvalidMessageIdSection, "Invalid BMG MID1 section" },
226
227 { ResultNCGRInvalidHeader, "Invalid NCGR header" },
228 { ResultNCGRInvalidCharacterDataBlock, "Invalid NCGR character data block" },
229 { ResultNCGRInvalidCharacterPositionBlock, "Invalid NCGR character position block" },
230
231 { ResultNCLRInvalidHeader, "Invalid NCLR header" },
232 { ResultNCLRInvalidPaletteDataBlock, "Invalid NCGR palette data block" },
233
234 { ResultNSCRInvalidHeader, "Invalid NSCR header" },
235 { ResultNSCRInvalidScreenDataBlock, "Invalid NSCR screen data block" },
236 { ResultNSCRWriteNotSupported, "Unsupported feature: writing to NSCR" },
237
238 { ResultNARCInvalidHeader, "Invalid NARC header" },
239 { ResultNARCInvalidFileAllocationTableBlock, "Invalid NARC file allocation table block" },
240 { ResultNARCInvalidFileNameTableBlock, "Invalid NARC file name table block" },
241 { ResultNARCInvalidFileImageBlock, "Invalid NARC file image block" },
242
243 { ResultSDATInvalidHeader, "Invalid SDAT header" },
244 { ResultSDATInvalidSymbolBlock, "Invalid SDAT symbol block" },
245 { ResultSDATInvalidInfoBlock, "Invalid SDAT info block" },
246 { ResultSDATInvalidFileAllocationTableBlock, "Invalid SDAT file allocation table block" },
247 { ResultSDATInvalidFileBlock, "Invalid SDAT file block" },
248 { ResultSDATInvalidSymbols, "Invalid SDAT symbols" },
249 { ResultSDATEntryNotFound, "SDAT entry not found" },
250 { ResultSDATInvalidEntryFormat, "Invalid SDAT entry format" },
251
252 { ResultSSEQInvalidHeader, "Invalid SSEQ header" },
253 { ResultSSEQInvalidDataSection, "Invalid SSEQ data section" },
254 { ResultSSEQWriteNotSupported, "Unsupported feature: writing to SSEQ" },
255
256 { ResultSBNKInvalidHeader, "Invalid SBNK header" },
257 { ResultSBNKInvalidDataSection, "Invalid SBNK data section" },
258 { ResultSBNKWriteNotSupported, "Unsupported feature: writing to SBNK" },
259
260 { ResultSWARInvalidHeader, "Invalid SWAR header" },
261 { ResultSWARInvalidDataSection, "Invalid SWAR data section" },
262 { ResultSWARWriteNotSupported, "Unsupported feature: writing to SWAR" },
263
264 { ResultSTRMInvalidHeader, "Invalid STRM header" },
265 { ResultSTRMInvalidHeadSection, "Invalid STRM head section" },
266 { ResultSTRMInvalidDataSection, "Invalid STRM data section" },
267 { ResultSTRMWriteNotSupported, "Unsupported feature: writing to STRM" },
268
269 { ResultUtilityInvalidSections, "Invalid DWC utility sections" }
270 };
271
272 inline constexpr Result GetResultDescription(const Result rc, std::string &out_desc) {
273 for(u32 i = 0; i < std::size(ResultDescriptionTable); i++) {
274 if(ResultDescriptionTable[i].first.value == rc.value) {
275 out_desc = ResultDescriptionTable[i].second;
276 TWL_R_SUCCEED();
277 }
278 }
279
280 TWL_R_FAIL(ResultUnknownResult);
281 }
282
283 inline std::string Result::GetDescription() const {
284 std::string desc = "<unknown result: " + std::to_string(this->value) + ">";
285 GetResultDescription(*this, desc);
286 return desc;
287 }
288
289}
290
291inline constexpr size_t operator ""_KB(unsigned long long n) {
292 return n * 0x400;
293}
294
295inline constexpr size_t operator ""_MB(unsigned long long n) {
296 return operator ""_KB(n) * 0x400;
297}
298
299inline constexpr size_t operator ""_GB(unsigned long long n) {
300 return operator ""_MB(n) * 0x400;
301}
302
303#define TWL_ENUM_BIT_OPERATORS(enum_type, base_type) \
304inline constexpr enum_type operator|(enum_type lhs, enum_type rhs) { \
305 return static_cast<enum_type>(static_cast<base_type>(lhs) | static_cast<base_type>(rhs)); \
306} \
307inline constexpr enum_type operator&(enum_type lhs, enum_type rhs) { \
308 return static_cast<enum_type>(static_cast<base_type>(lhs) & static_cast<base_type>(rhs)); \
309}
310
311#define TWL_BITMASK(n) (1 << n)
Definition twl_Include.hpp:62