Skip to content

Commit

Permalink
fix: update data structure StringByteInput for CompoundTag::fromBinar…
Browse files Browse the repository at this point in the history
…yNBT (#1166)
  • Loading branch information
KawaiiNahida authored Mar 26, 2023
1 parent ef432bf commit 40034d9
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 10 deletions.
2 changes: 2 additions & 0 deletions LiteLoader/include/llapi/mc/CompoundTag.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,8 @@ class CompoundTag : public Tag {
LIAPI std::string toBinaryNBT(bool isLittleEndian = true);
LIAPI static std::string nbtListToBinary(std::vector<std::unique_ptr<CompoundTag>> tags, bool isLittleEndian = true);
LIAPI static std::unique_ptr<CompoundTag> fromSNBT(const std::string& snbt);

LIAPI static std::unique_ptr<CompoundTag> fromBinaryNBT(std::string_view dataView, size_t& offset, bool isLittleEndian = true);
LIAPI static std::unique_ptr<CompoundTag> fromBinaryNBT(void* data, size_t len, bool isLittleEndian = true);
LIAPI static std::unique_ptr<CompoundTag> fromBinaryNBT(void* data, size_t len, size_t& offset, bool isLittleEndian = true);
LIAPI static std::unique_ptr<CompoundTag> fromBinaryNBT(std::string const& data, size_t& offset, bool isLittleEndian = true);
Expand Down
30 changes: 20 additions & 10 deletions LiteLoader/src/llapi/nbt/CompoundTagAPI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -292,20 +292,30 @@ std::string CompoundTag::nbtListToBinary(std::vector<std::unique_ptr<CompoundTag

#pragma region From Binary
//////////////////// From Binary ////////////////////
std::unique_ptr<CompoundTag> CompoundTag::fromBinaryNBT(void* data, size_t len, size_t& offset, bool isLittleEndian) {
void* vtbl;
if (isLittleEndian)
vtbl = dlsym("??_7StringByteInput@@6B@");
else
vtbl = dlsym("??_7BigEndianStringByteInput@@6B@");

uintptr_t iDataInput[4] = {(uintptr_t)vtbl, offset, len, (uintptr_t)data};
auto rtn = NbtIo::read((IDataInput&)iDataInput);

offset = iDataInput[1];
std::unique_ptr<CompoundTag> CompoundTag::fromBinaryNBT(std::string_view dataView, size_t& offset, bool isLittleEndian) {
struct {
__int64* mVtbl;
size_t mOffset;
std::string_view mBuffer;
} tStringByteInput;

if (isLittleEndian) tStringByteInput.mVtbl = (__int64*)dlsym("??_7StringByteInput@@6B@");
else tStringByteInput.mVtbl = (__int64*)dlsym("??_7BigEndianStringByteInput@@6B@");

tStringByteInput.mOffset = offset;
tStringByteInput.mBuffer = dataView;
auto rtn = NbtIo::read(*reinterpret_cast<IDataInput*>(&tStringByteInput));

//update currentOffset
offset = tStringByteInput.mOffset;
return rtn;
}

std::unique_ptr<CompoundTag> CompoundTag::fromBinaryNBT(void* data, size_t len, size_t& offset, bool isLittleEndian) {
return fromBinaryNBT(std::string_view((char*)data, len), offset, isLittleEndian);
}

std::unique_ptr<CompoundTag> CompoundTag::fromBinaryNBT(void* data, size_t len, bool isLittleEndian) {
size_t endOffset = 0;
return fromBinaryNBT(data, len, endOffset, isLittleEndian);
Expand Down

0 comments on commit 40034d9

Please sign in to comment.