8#define TWL_ATTR_PACKED __attribute__((packed))
24 using Fn = std::function<void()>;
31 ScopeGuard(Fn &&fn) : run(
true), exit_fn(fn) {}
39 inline void Cancel() {
46#define TWL_R_SUCCEED() return ::twl::ResultSuccess
48#define TWL_R_FAIL(rc) { \
49 const auto _tmp_rc = (rc); \
53#define TWL_R_TRY(rc) { \
54 const auto _tmp_rc = (rc); \
55 if(_tmp_rc.IsFailure()) { \
67 static constexpr BaseType SuccessValue = BaseType();
69 constexpr Result() : value() {}
70 constexpr Result(
const BaseType val) : value(val) {}
72 inline constexpr bool IsSuccess()
const {
73 return this->value == SuccessValue;
76 inline constexpr bool IsFailure()
const {
77 return !this->IsSuccess();
80 inline std::string GetDescription()
const;
83 constexpr Result ResultSuccess = 0;
84 constexpr Result ResultUnknownResult = 0x0002;
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;
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;
116 constexpr Result ResultNitroFsDirectoryNotFound = 0x0301;
117 constexpr Result ResultNitroFsFileNotFound = 0x0302;
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;
128 constexpr Result ResultNCGRInvalidHeader = 0x0501;
129 constexpr Result ResultNCGRInvalidCharacterDataBlock = 0x0502;
130 constexpr Result ResultNCGRInvalidCharacterPositionBlock = 0x0503;
132 constexpr Result ResultNCLRInvalidHeader = 0x0601;
133 constexpr Result ResultNCLRInvalidPaletteDataBlock = 0x0602;
135 constexpr Result ResultNSCRInvalidHeader = 0x0701;
136 constexpr Result ResultNSCRInvalidScreenDataBlock = 0x0702;
137 constexpr Result ResultNSCRWriteNotSupported = 0x0703;
139 constexpr Result ResultNARCInvalidHeader = 0x0801;
140 constexpr Result ResultNARCInvalidFileAllocationTableBlock = 0x0802;
141 constexpr Result ResultNARCInvalidFileNameTableBlock = 0x0803;
142 constexpr Result ResultNARCInvalidFileImageBlock = 0x0804;
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;
153 constexpr Result ResultSSEQInvalidHeader = 0x0a01;
154 constexpr Result ResultSSEQInvalidDataSection = 0x0a02;
155 constexpr Result ResultSSEQWriteNotSupported = 0x0a03;
157 constexpr Result ResultSBNKInvalidHeader = 0x0b01;
158 constexpr Result ResultSBNKInvalidDataSection = 0x0b02;
159 constexpr Result ResultSBNKWriteNotSupported = 0x0b03;
161 constexpr Result ResultSWARInvalidHeader = 0x0c01;
162 constexpr Result ResultSWARInvalidDataSection = 0x0c02;
163 constexpr Result ResultSWARWriteNotSupported = 0x0c03;
165 constexpr Result ResultSTRMInvalidHeader = 0x0d01;
166 constexpr Result ResultSTRMInvalidHeadSection = 0x0d02;
167 constexpr Result ResultSTRMInvalidDataSection = 0x0d03;
168 constexpr Result ResultSTRMWriteNotSupported = 0x0d04;
170 constexpr Result ResultROMInvalidUnitCode = 0x0e01;
171 constexpr Result ResultROMInvalidNintendoLogoCRC16 = 0x0e02;
173 constexpr Result ResultCompressionInvalidLzFormat = 0x0f01;
174 constexpr Result ResultCompressionTooBigCompressSize = 0x0f02;
175 constexpr Result ResultCompressionInvalidRepeatSize = 0x0f03;
177 constexpr Result ResultUtilityInvalidSections = 0x1001;
179 using ResultDescriptionEntry = std::pair<Result, const char*>;
181 constexpr ResultDescriptionEntry ResultDescriptionTable[] = {
182 { ResultSuccess,
"Success" },
183 { ResultUnknownResult,
"Unknown result value" },
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" },
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" },
215 { ResultNitroFsDirectoryNotFound,
"NitroFs directory not found" },
216 { ResultNitroFsFileNotFound,
"NitroFs file not found" },
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" },
227 { ResultNCGRInvalidHeader,
"Invalid NCGR header" },
228 { ResultNCGRInvalidCharacterDataBlock,
"Invalid NCGR character data block" },
229 { ResultNCGRInvalidCharacterPositionBlock,
"Invalid NCGR character position block" },
231 { ResultNCLRInvalidHeader,
"Invalid NCLR header" },
232 { ResultNCLRInvalidPaletteDataBlock,
"Invalid NCGR palette data block" },
234 { ResultNSCRInvalidHeader,
"Invalid NSCR header" },
235 { ResultNSCRInvalidScreenDataBlock,
"Invalid NSCR screen data block" },
236 { ResultNSCRWriteNotSupported,
"Unsupported feature: writing to NSCR" },
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" },
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" },
252 { ResultSSEQInvalidHeader,
"Invalid SSEQ header" },
253 { ResultSSEQInvalidDataSection,
"Invalid SSEQ data section" },
254 { ResultSSEQWriteNotSupported,
"Unsupported feature: writing to SSEQ" },
256 { ResultSBNKInvalidHeader,
"Invalid SBNK header" },
257 { ResultSBNKInvalidDataSection,
"Invalid SBNK data section" },
258 { ResultSBNKWriteNotSupported,
"Unsupported feature: writing to SBNK" },
260 { ResultSWARInvalidHeader,
"Invalid SWAR header" },
261 { ResultSWARInvalidDataSection,
"Invalid SWAR data section" },
262 { ResultSWARWriteNotSupported,
"Unsupported feature: writing to SWAR" },
264 { ResultSTRMInvalidHeader,
"Invalid STRM header" },
265 { ResultSTRMInvalidHeadSection,
"Invalid STRM head section" },
266 { ResultSTRMInvalidDataSection,
"Invalid STRM data section" },
267 { ResultSTRMWriteNotSupported,
"Unsupported feature: writing to STRM" },
269 { ResultUtilityInvalidSections,
"Invalid DWC utility sections" }
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;
280 TWL_R_FAIL(ResultUnknownResult);
283 inline std::string Result::GetDescription()
const {
284 std::string desc =
"<unknown result: " + std::to_string(this->value) +
">";
285 GetResultDescription(*
this, desc);
291inline constexpr size_t operator ""_KB(
unsigned long long n) {
295inline constexpr size_t operator ""_MB(
unsigned long long n) {
296 return operator ""_KB(n) * 0x400;
299inline constexpr size_t operator ""_GB(
unsigned long long n) {
300 return operator ""_MB(n) * 0x400;
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)); \
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)); \
311#define TWL_BITMASK(n) (1 << n)
Definition twl_Include.hpp:62