libnedit
Lightweight C++ library for Nintendo DS(i) formats
Loading...
Searching...
No Matches
fmt_BMG.hpp
1
2#pragma once
3#include <ntr/fmt/fmt_Common.hpp>
4
5namespace ntr::fmt {
6
7 struct BMG : public fs::FileFormat {
8
9 enum class Encoding : u8 {
10 // TODO: Unused = 0,
11 CP1252 = 1,
12 UTF16 = 2,
13 ShiftJIS = 3,
14 UTF8 = 4
15 };
16
17 static inline constexpr size_t GetCharacterSize(const Encoding enc) {
18 switch(enc) {
19 case Encoding::CP1252: {
20 // Unsupported anyway
21 return 1;
22 }
23 case Encoding::UTF16: {
24 return sizeof(char16_t);
25 }
26 case Encoding::UTF8: {
27 return sizeof(char);
28 }
29 case Encoding::ShiftJIS: {
30 // Unsupported anyway
31 return 1;
32 }
33 }
34
35 // Default for unknown encodings
36 return 0;
37 }
38
39 static inline constexpr bool IsValidEncoding(const Encoding enc) {
40 return GetCharacterSize(enc) > 0;
41 }
42
43 struct Header : public MagicStartBase<u64, 0x31676D624753454D /* "MESGbmg1" */ > {
44 u32 file_size;
45 u32 section_count;
46 Encoding encoding;
47 u8 unk_1;
48 u16 unk_2;
49 u32 unk_3;
50 u32 unk_4;
51 u32 unk_5;
52 };
53
54 struct InfoSection : public CommonBlock<0x31464E49 /* "INF1" */ > {
55 u16 entry_count;
56 u16 entry_size;
57 u32 file_id;
58
59 static constexpr size_t OffsetSize = sizeof(u32);
60
61 inline size_t GetAttributesSize() {
62 return this->entry_size - OffsetSize;
63 }
64
65 inline void SetAttributesSize(const size_t size) {
66 this->entry_size = OffsetSize + size;
67 }
68 };
69
70 struct DataSection : public CommonBlock<0x31544144 /* "DAT1" */ > {
71 };
72
73 struct MessageIdSection : public CommonBlock<0x3144494D /* "MID1" */ > {
74 u16 id_count;
75 u8 unk_1;
76 u8 unk_2;
77 u32 unk_3;
78 };
79
80 enum class MessageTokenType : u8 {
81 Text,
82 Escape
83 };
84
86 std::vector<u8> esc_data;
87 };
88
89 struct MessageToken {
90 MessageTokenType type;
91 std::u16string text;
92 MessageEscape escape;
93
94 inline size_t GetByteLength(const Encoding enc) const {
95 switch(this->type) {
96 case MessageTokenType::Escape: {
97 return GetCharacterSize(enc) + 1 + this->escape.esc_data.size();
98 }
99 case MessageTokenType::Text: {
100 return this->text.length() * GetCharacterSize(enc);
101 }
102 default: {
103 return 0;
104 }
105 }
106 }
107 };
108
109 struct Message {
110 std::vector<MessageToken> msg;
111 std::vector<u8> attrs;
112 u32 id;
113
114 inline size_t GetByteLength(const Encoding enc) const {
115 size_t size = GetCharacterSize(enc); // All messages end with a null character
116 for(const auto &token: this->msg) {
117 size += token.GetByteLength(enc);
118 }
119 return size;
120 }
121 };
122
123 Header header;
124 InfoSection info;
125 DataSection data;
126 std::optional<MessageIdSection> msg_id;
127 std::vector<Message> messages;
128
129 BMG() {}
130 BMG(const BMG&) = delete;
131
132 inline bool HasMessageIds() {
133 return this->msg_id.has_value();
134 }
135
136 Result CreateFrom(const Encoding enc, const bool has_message_ids, const size_t attr_size, const std::vector<Message> &msgs, const u32 file_id, const ntr::fs::FileCompression comp = ntr::fs::FileCompression::None);
137
138 Result ValidateImpl(const std::string &path, std::shared_ptr<fs::FileHandle> file_handle, const fs::FileCompression comp) override;
139 Result ReadImpl(const std::string &path, std::shared_ptr<fs::FileHandle> file_handle, const fs::FileCompression comp) override;
140 Result WriteImpl(const std::string &path, std::shared_ptr<fs::FileHandle> file_handle, const fs::FileCompression comp) override;
141 };
142
143}
Definition ntr_Include.hpp:75
Definition fmt_BMG.hpp:70
Definition fmt_BMG.hpp:43
Definition fmt_BMG.hpp:54
Definition fmt_BMG.hpp:85
Definition fmt_BMG.hpp:73
Definition fmt_BMG.hpp:89
Definition fmt_BMG.hpp:109
Definition fmt_BMG.hpp:7
Definition fmt_Common.hpp:53
Definition fmt_Common.hpp:8
Definition fs_Base.hpp:45