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