|
| 1 | +/** @file JavaSerializedClassParser.cpp |
| 2 | +* |
| 3 | +* @author Zero_DSRS_VX |
| 4 | +* @date 3/22/26 |
| 5 | +* |
| 6 | +* @device mac-8 |
| 7 | +* |
| 8 | +* @copyright Copyright (c) 2026 Team Lodestone |
| 9 | +* @license This project is licensed under the MIT license, see the LICENSE file for details. |
| 10 | +*/ |
| 11 | + |
| 12 | +#include "JavaObject/JavaSerializedClassParser.h" |
| 13 | +#include "BinaryIO/stream/BinaryInputStream.h" |
| 14 | + |
| 15 | +namespace javaobject { |
| 16 | + SerializedField |
| 17 | + SerializedField::parseFieldEntry(const SerializedClass &clazz, bio::stream::BinaryInputStream &strm) { |
| 18 | + // Parse field entry |
| 19 | + const char type = strm.readByte(); |
| 20 | + auto descriptor = std::string(1, type); |
| 21 | + |
| 22 | + const std::string name = strm.readStringWithLength<char>(bio::util::ByteOrder::BIG, |
| 23 | + bio::util::string::StringLengthEncoding::LENGTH_PREFIX); |
| 24 | + |
| 25 | + // Check if descriptor is object or array |
| 26 | + if (type == 'L' || type == '[') { |
| 27 | + descriptor = parseSignature(clazz, type, strm); |
| 28 | + } |
| 29 | + return SerializedField{clazz, name, descriptor}; |
| 30 | + } |
| 31 | + |
| 32 | + std::string SerializedField::parseSignature(const SerializedClass &clazz, const char type, |
| 33 | + bio::stream::BinaryInputStream &strm) { |
| 34 | + auto descriptor = std::string(1, type); |
| 35 | + |
| 36 | + // Check if descriptor is object or array |
| 37 | + if (type == 'L' || type == '[') { |
| 38 | + char typeCode = strm.readByte(); |
| 39 | + if (typeCode == '[') { |
| 40 | + // Read primitive array |
| 41 | + const char arrayType = strm.readByte(); |
| 42 | + if (arrayType != 'L') { |
| 43 | + descriptor += arrayType; |
| 44 | + } else { |
| 45 | + descriptor += parseSignature(clazz, arrayType, strm); |
| 46 | + } |
| 47 | + } else if (typeCode == 't') { |
| 48 | + descriptor = strm.readStringWithLength<char>(bio::util::ByteOrder::BIG, |
| 49 | + bio::util::string::StringLengthEncoding::LENGTH_PREFIX); |
| 50 | + } else if (typeCode == 'q') { |
| 51 | + // Type is the same as the field before this one |
| 52 | + if (!clazz.m_fields.empty()) { |
| 53 | + descriptor = clazz.m_fields.back().desc; |
| 54 | + |
| 55 | + // skip a few bytes |
| 56 | + // TODO: Figure out if this means anything |
| 57 | + for (int i = 0; i < 4; i++) { |
| 58 | + strm.readByte(); |
| 59 | + } |
| 60 | + } |
| 61 | + } else { |
| 62 | + // LOG_DEBUG("Unrecognized type code!"); |
| 63 | + } |
| 64 | + } |
| 65 | + return descriptor; |
| 66 | + } |
| 67 | + |
| 68 | + JavaValue SerializedField::readFieldValue(JavaSerializedClassParser& parser, const SerializedField &field, bio::stream::BinaryInputStream &strm) { |
| 69 | + const auto descriptor = field.desc; |
| 70 | + if (descriptor == "B") { |
| 71 | + return JavaValue { strm.readSignedByte() }; |
| 72 | + } |
| 73 | + if (descriptor == "Z") { |
| 74 | + return JavaValue { (strm.readByte() != 0) }; |
| 75 | + } |
| 76 | + if (descriptor == "C") { |
| 77 | + return JavaValue { static_cast<char>(strm.readByte()) }; |
| 78 | + } |
| 79 | + if (descriptor == "D") { |
| 80 | + return JavaValue { strm.readBE<double>() }; |
| 81 | + } |
| 82 | + if (descriptor == "F") { |
| 83 | + return JavaValue { strm.readBE<float>() }; |
| 84 | + } |
| 85 | + if (descriptor == "I") { |
| 86 | + // Java always stores integers as signed |
| 87 | + // C++ uses two's-complements when reading the integer in |
| 88 | + // which means we need to shift this down to get the |
| 89 | + // correct sign and value. |
| 90 | + return JavaValue { static_cast<int32_t>(strm.readBE<uint32_t>() >> 8) }; |
| 91 | + } |
| 92 | + if (descriptor == "J") { |
| 93 | + return JavaValue { static_cast<int64_t>(strm.readBE<uint64_t>() >> 8) }; |
| 94 | + } |
| 95 | + if (descriptor.starts_with("L")) { |
| 96 | + if (descriptor == "Ljava/lang/String;") { |
| 97 | + return JavaValue { std::string(strm.readStringWithLength<char>(bio::util::ByteOrder::BIG, bio::util::string::StringLengthEncoding::LENGTH_PREFIX)) }; |
| 98 | + } |
| 99 | + strm.seekRelative(-1); |
| 100 | + int offset = strm.getOffset(); |
| 101 | + // Object! |
| 102 | + SerializedClass clazz = parser.parseEntry(); |
| 103 | + return JavaValue { std::make_shared<JavaObject>(clazz)}; |
| 104 | + } |
| 105 | + if (descriptor.starts_with("[")) { |
| 106 | + // Array! |
| 107 | + return readFieldValue(parser, field, strm); |
| 108 | + } |
| 109 | + |
| 110 | + // LOG_DEBUG("Descriptor not yet implemented!"); |
| 111 | + return {}; |
| 112 | + } |
| 113 | + |
| 114 | + std::vector<SerializedClass> JavaSerializedClassParser::parseAllEntries() { |
| 115 | + std::vector<SerializedClass> result; |
| 116 | + if (!this->m_stream.getStream().eof()) { |
| 117 | + SerializedClass clazz = this->parseEntry(); |
| 118 | + } |
| 119 | + |
| 120 | + return result; |
| 121 | + } |
| 122 | + |
| 123 | + SerializedClass JavaSerializedClassParser::parseEntry() { |
| 124 | + SerializedClass result; |
| 125 | + |
| 126 | + if (const uint16_t classMagic = this->m_stream.readBE<uint16_t>(); classMagic != 0x7372) { |
| 127 | + return result; |
| 128 | + } |
| 129 | + |
| 130 | + // Read class name |
| 131 | + result.m_className = this->m_stream.readStringWithLength<char>(bio::util::ByteOrder::BIG, |
| 132 | + bio::util::string::StringLengthEncoding::LENGTH_PREFIX); |
| 133 | + this->m_stream.seekRelative(8); |
| 134 | + |
| 135 | + // TODO: Read fields from serialized class object |
| 136 | + this->m_stream.seekRelative(2); // TODO: Look at this more |
| 137 | + |
| 138 | + const int8_t numberOfFields = this->m_stream.readSignedByte(); |
| 139 | + |
| 140 | + result.m_fields.clear(); |
| 141 | + |
| 142 | + for (int i = 0; i < numberOfFields; i++) { |
| 143 | + SerializedField field = SerializedField::parseFieldEntry(result, this->m_stream); |
| 144 | + result.m_fields.push_back(field); |
| 145 | + } |
| 146 | + |
| 147 | + char tcEndBlockData = this->m_stream.readByte(); |
| 148 | + if (tcEndBlockData != 0x78) { |
| 149 | + // LOG_ERROR( |
| 150 | + // "TCEndBlockData was not found after reading fields! Stream is either corrupted or signature parsing failed!"); |
| 151 | + } |
| 152 | + char tcSuperclassDesc = this->m_stream.readByte(); |
| 153 | + if (tcSuperclassDesc == 0x72) { |
| 154 | + // Superclass was specified |
| 155 | + // TODO: Implement reading of superclass |
| 156 | + |
| 157 | + tcEndBlockData = this->m_stream.readByte(); |
| 158 | + if (tcEndBlockData != 0x78) { |
| 159 | + // LOG_ERROR( |
| 160 | + // "TCEndBlockData was not found after reading superclass! Stream is either corrupted or superclass could not be parsed!"); |
| 161 | + } |
| 162 | + |
| 163 | + // TODO: Remove this hack: |
| 164 | + // This will skip over the superclass declaration if it exists |
| 165 | + while (true) { |
| 166 | + if (this->m_stream.readByte() == 0x78) { |
| 167 | + break; |
| 168 | + } |
| 169 | + } |
| 170 | + } else if (tcSuperclassDesc == 0x70) { |
| 171 | + // Superclass was null |
| 172 | + } |
| 173 | + // Skip one byte |
| 174 | + this->m_stream.seekRelative(1); |
| 175 | + |
| 176 | + // Read field values |
| 177 | + for (int i = 0; i < numberOfFields; i++) { |
| 178 | + SerializedField& field = result.m_fields[i]; |
| 179 | + field.value = field.readFieldValue(*this, field, this->m_stream); |
| 180 | + } |
| 181 | + |
| 182 | + this->m_serializedClasses.push_back(result); |
| 183 | + return result; |
| 184 | + } |
| 185 | +} // lodestone::minecraft::common::java::classic::minev3 |
0 commit comments