1#include "Library/Yaml/Writer/ByamlWriterData.h"
2
3#include <stream/seadStream.h>
4
5#include "Library/Yaml/ByamlUtil.h"
6#include "Library/Yaml/Writer/ByamlWriterBigDataList.h"
7#include "Library/Yaml/Writer/ByamlWriterStringTable.h"
8
9namespace al {
10
11void ByamlWriterData::printIndent(s32) const {}
12
13ByamlWriterBool::ByamlWriterBool(bool value) : mValue(value) {}
14
15u8 ByamlWriterBool::getTypeCode() const {
16 return 0xD0;
17}
18
19void ByamlWriterBool::print(s32) const {}
20
21void ByamlWriterBool::write(sead::WriteStream* stream) const {
22 stream->writeU32(mValue);
23}
24
25ByamlWriterInt::ByamlWriterInt(s32 value) : mValue(value) {}
26
27u8 ByamlWriterInt::getTypeCode() const {
28 return 0xD1;
29}
30
31void ByamlWriterInt::print(s32) const {}
32
33void ByamlWriterInt::write(sead::WriteStream* stream) const {
34 stream->writeS32(mValue);
35}
36
37ByamlWriterFloat::ByamlWriterFloat(f32 value) : mValue(value) {}
38
39u8 ByamlWriterFloat::getTypeCode() const {
40 return 0xD2;
41}
42
43void ByamlWriterFloat::print(s32) const {}
44
45void ByamlWriterFloat::write(sead::WriteStream* stream) const {
46 stream->writeF32(mValue);
47}
48
49ByamlWriterUInt::ByamlWriterUInt(u32 value) : mValue(value) {}
50
51u8 ByamlWriterUInt::getTypeCode() const {
52 return 0xD3;
53}
54
55void ByamlWriterUInt::print(s32) const {}
56
57void ByamlWriterUInt::write(sead::WriteStream* stream) const {
58 stream->writeU32(mValue);
59}
60
61ByamlWriterNull::ByamlWriterNull() = default;
62
63u8 ByamlWriterNull::getTypeCode() const {
64 return 0xFF;
65}
66
67void ByamlWriterNull::print(s32) const {}
68
69void ByamlWriterNull::write(sead::WriteStream* stream) const {
70 stream->writeU32(0);
71}
72
73ByamlWriterString::ByamlWriterString(const char* string, ByamlWriterStringTable* stringTable)
74 : mString(nullptr), mStringTable(stringTable) {
75 mString = mStringTable->tryAdd(string);
76}
77
78u8 ByamlWriterString::getTypeCode() const {
79 return 0xA0;
80}
81
82void ByamlWriterString::print(s32) const {}
83
84void ByamlWriterString::write(sead::WriteStream* stream) const {
85 stream->writeU32(mStringTable->calcIndex(mString));
86}
87
88ByamlWriterBigData::ByamlWriterBigData(ByamlWriterBigDataList* list) : mList(list) {
89 mList->addData(this);
90}
91
92void ByamlWriterBigData::write(sead::WriteStream* stream) const {
93 stream->writeU32(mOffset);
94}
95
96ByamlWriterInt64::ByamlWriterInt64(s64 value, ByamlWriterBigDataList* list)
97 : ByamlWriterBigData(list), mValue(value) {}
98
99u8 ByamlWriterInt64::getTypeCode() const {
100 return 0xD4;
101}
102
103void ByamlWriterInt64::writeBigData(sead::WriteStream* stream) const {
104 stream->writeS64(mValue);
105}
106
107void ByamlWriterInt64::print(s32) const {}
108
109ByamlWriterUInt64::ByamlWriterUInt64(u64 value, ByamlWriterBigDataList* list)
110 : ByamlWriterBigData(list), mValue(value) {}
111
112u8 ByamlWriterUInt64::getTypeCode() const {
113 return 0xD5;
114}
115
116void ByamlWriterUInt64::writeBigData(sead::WriteStream* stream) const {
117 stream->writeU64(mValue);
118}
119
120void ByamlWriterUInt64::print(s32) const {}
121
122ByamlWriterDouble::ByamlWriterDouble(f64 value, ByamlWriterBigDataList* list)
123 : ByamlWriterBigData(list), mValue(value) {}
124
125u8 ByamlWriterDouble::getTypeCode() const {
126 return 0xD6;
127}
128
129void ByamlWriterDouble::writeBigData(sead::WriteStream* stream) const {
130 stream->writeU64(*reinterpret_cast<const u64*>(&mValue));
131}
132
133void ByamlWriterDouble::print(s32) const {}
134
135ByamlWriterArray::ByamlWriterArray(ByamlWriterStringTable* stringTable)
136 : mStringTable(stringTable) {}
137
138ByamlWriterArray::~ByamlWriterArray() {
139 while (auto* node = mList.popBack())
140 delete node;
141}
142
143void ByamlWriterArray::deleteData() {
144 for (auto it = mList.robustBegin(); it != mList.robustEnd(); ++it) {
145 if (!it->mData->isContainer()) {
146 delete it->mData;
147 it->mData = nullptr;
148 }
149 }
150}
151
152u32 ByamlWriterArray::calcPackSize() const {
153 return mList.size() * 4 + ((mList.size() + 7) & 0xFFFFFFFC);
154}
155
156void ByamlWriterArray::addData(ByamlWriterData* data) {
157 mList.pushBack(item: new sead::TListNode<ByamlWriterData*>(data));
158}
159
160void ByamlWriterArray::addBool(bool value) {
161 addData(data: new ByamlWriterBool(value));
162}
163
164void ByamlWriterArray::addInt(s32 value) {
165 addData(data: new ByamlWriterInt(value));
166}
167
168void ByamlWriterArray::addUInt(u32 value) {
169 addData(data: new ByamlWriterUInt(value));
170}
171
172void ByamlWriterArray::addFloat(f32 value) {
173 addData(data: new ByamlWriterFloat(value));
174}
175
176void ByamlWriterArray::addInt64(s64 value, ByamlWriterBigDataList* list) {
177 addData(data: new ByamlWriterInt64(value, list));
178}
179
180void ByamlWriterArray::addUInt64(u64 value, ByamlWriterBigDataList* list) {
181 addData(data: new ByamlWriterUInt64(value, list));
182}
183
184void ByamlWriterArray::addDouble(f64 value, ByamlWriterBigDataList* list) {
185 addData(data: new ByamlWriterDouble(value, list));
186}
187
188void ByamlWriterArray::addString(const char* value) {
189 addData(data: new ByamlWriterString(value, mStringTable));
190}
191
192void ByamlWriterArray::addHash(ByamlWriterHash* hash) {
193 addData(data: hash);
194}
195
196void ByamlWriterArray::addArray(ByamlWriterArray* array) {
197 addData(data: array);
198}
199
200void ByamlWriterArray::addNull() {
201 addData(data: new ByamlWriterNull());
202}
203
204u8 ByamlWriterArray::getTypeCode() const {
205 return 0xC0;
206}
207
208void ByamlWriterArray::writeContainer(sead::WriteStream* stream) const {
209 stream->writeU8(0xC0);
210 alByamlLocalUtil::writeU24(stream, val: mList.size());
211
212 for (auto& node : mList)
213 stream->writeU8(node->getTypeCode());
214
215 s32 i = mList.size();
216 s32 v12 = i < 0 ? i + 3 : i;
217 s32 v14 = i - (v12 & 0xFFFFFFFC);
218 s32 v15 = 4 - v14;
219 s32 v16 = v14 == 0 ? 0 : v15;
220 for (s32 j = 0; j < v16; j++)
221 stream->writeU8(0);
222 for (auto& node : mList)
223 node->write(stream);
224}
225
226void ByamlWriterArray::write(sead::WriteStream* stream) const {
227 stream->writeU32(getOffset());
228}
229
230void ByamlWriterArray::print(s32 unknown) const {
231 for (auto node : mList)
232 node->print(unknown + 1);
233}
234
235ByamlWriterHashPair::ByamlWriterHashPair(const char* key, ByamlWriterData* value)
236 : mKey(key), mValue(value) {}
237
238ByamlWriterHash::ByamlWriterHash(ByamlWriterStringTable* stringTable1,
239 ByamlWriterStringTable* stringTable2)
240 : mStringTable1(stringTable1), mStringTable2(stringTable2) {}
241
242ByamlWriterHash::~ByamlWriterHash() {
243 while (auto* node = mList.popBack())
244 delete node;
245}
246
247u32 ByamlWriterHash::calcPackSize() const {
248 return mList.size() * 8 + 4;
249}
250
251void ByamlWriterHash::addBool(const char* key, bool value) {
252 addData(key, new ByamlWriterBool(value));
253}
254
255void ByamlWriterHash::addInt(const char* key, s32 value) {
256 addData(key, new ByamlWriterInt(value));
257}
258
259void ByamlWriterHash::addUInt(const char* key, u32 value) {
260 addData(key, new ByamlWriterUInt(value));
261}
262
263void ByamlWriterHash::addFloat(const char* key, f32 value) {
264 addData(key, new ByamlWriterFloat(value));
265}
266
267void ByamlWriterHash::addInt64(const char* key, s64 value, ByamlWriterBigDataList* list) {
268 addData(key, new ByamlWriterInt64(value, list));
269}
270
271void ByamlWriterHash::addUInt64(const char* key, u64 value, ByamlWriterBigDataList* list) {
272 addData(key, new ByamlWriterUInt64(value, list));
273}
274
275void ByamlWriterHash::addDouble(const char* key, f64 value, ByamlWriterBigDataList* list) {
276 addData(key, new ByamlWriterDouble(value, list));
277}
278
279void ByamlWriterHash::addString(const char* key, const char* value) {
280 addData(key, new ByamlWriterString(value, mStringTable2));
281}
282
283void ByamlWriterHash::addHash(const char* key, ByamlWriterHash* value) {
284 addData(key, value);
285}
286
287void ByamlWriterHash::addArray(const char* key, ByamlWriterArray* value) {
288 addData(key, value);
289}
290
291void ByamlWriterHash::addNull(const char* key) {
292 addData(key, new ByamlWriterNull());
293}
294
295u8 ByamlWriterHash::getTypeCode() const {
296 return 0xC1;
297}
298
299void ByamlWriterHash::write(sead::WriteStream* stream) const {
300 stream->writeU32(getOffset());
301}
302
303} // namespace al
304