Skip to content

Commit

Permalink
fix: fix DefaultDataLoadHelper
Browse files Browse the repository at this point in the history
  • Loading branch information
ShrBox committed Aug 5, 2024
1 parent fbebe28 commit a33d674
Show file tree
Hide file tree
Showing 10 changed files with 74 additions and 53 deletions.
2 changes: 1 addition & 1 deletion src/legacy/api/BlockAPI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -510,7 +510,7 @@ Local<Value> McClass::setBlock(const Arguments& args) {
return Boolean::newBoolean(bs.setBlock(pos.getBlockPos(), bl, 3, nullptr, nullptr));
} else if (IsInstanceOf<NbtCompoundClass>(block)) {
// Nbt
CompoundTag* nbt = NbtCompoundClass::extract(block);
auto nbt = NbtCompoundClass::extract(block);
optional_ref<const Block> bl = Block::tryGetFromRegistry(*nbt);
if (!bl.has_value()) {
return Boolean::newBoolean(false);
Expand Down
5 changes: 2 additions & 3 deletions src/legacy/api/BlockEntityAPI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
#include "mc/world/level/block/actor/BlockActor.h"
#include "mc/world/level/dimension/Dimension.h"


//////////////////// Class Definition ////////////////////

ClassDefine<BlockEntityClass> BlockEntityClassBuilder = defineClass<BlockEntityClass>("LLSE_BlockEntity")
Expand Down Expand Up @@ -93,10 +92,10 @@ Local<Value> BlockEntityClass::setNbt(const Arguments& args) {

try {
auto nbt = NbtCompoundClass::extract(args[0]);
if (!nbt || !MoreGlobal::defaultDataLoadHelper) {
if (!nbt) {
return Local<Value>();
}
blockEntity->load(*ll::service::getLevel(), *nbt, *MoreGlobal::defaultDataLoadHelper);
blockEntity->load(*ll::service::getLevel(), *nbt, MoreGlobal::defaultDataLoadHelper());
return Boolean::newBoolean(true);
}
CATCH("Fail in setNbt!")
Expand Down
5 changes: 2 additions & 3 deletions src/legacy/api/EntityAPI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@
#include <mc/world/level/material/Material.h>
#include <mc/world/phys/AABB.h>


using magic_enum::enum_integer;

//////////////////// Class Definition ////////////////////
Expand Down Expand Up @@ -1304,11 +1303,11 @@ Local<Value> EntityClass::setNbt(const Arguments& args) {
if (!entity) return Local<Value>();

auto nbt = NbtCompoundClass::extract(args[0]);
if (!nbt || !MoreGlobal::defaultDataLoadHelper) {
if (!nbt) {
return Local<Value>();
}

return Boolean::newBoolean(entity->load(*nbt, *MoreGlobal::defaultDataLoadHelper));
return Boolean::newBoolean(entity->load(*nbt, MoreGlobal::defaultDataLoadHelper()));
}
CATCH("Fail in setNbt!")
}
Expand Down
2 changes: 1 addition & 1 deletion src/legacy/api/ItemAPI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -446,7 +446,7 @@ Local<Value> McClass::newItem(const Arguments& args) {
return Local<Value>();
}
} else {
CompoundTag* nbt = NbtCompoundClass::extract(args[0]);
auto nbt = NbtCompoundClass::extract(args[0]);
if (nbt) {
auto newItem = new ItemStack{ItemStack::EMPTY_ITEM};
newItem->load(*nbt);
Expand Down
4 changes: 2 additions & 2 deletions src/legacy/api/NbtAPI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1475,9 +1475,9 @@ NbtCompoundClass* NbtCompoundClass::constructor(const Arguments& args) {
CATCH_C("Fail in Create CompoundTag!");
}

CompoundTag* NbtCompoundClass::extract(Local<Value> v) {
std::unique_ptr<CompoundTag> NbtCompoundClass::extract(Local<Value> v) {
if (EngineScope::currentEngine()->isInstanceOf<NbtCompoundClass>(v))
return EngineScope::currentEngine()->getNativeInstance<NbtCompoundClass>(v)->nbt.get();
return std::move(EngineScope::currentEngine()->getNativeInstance<NbtCompoundClass>(v)->nbt);
else return nullptr;
}

Expand Down
9 changes: 5 additions & 4 deletions src/legacy/api/NbtAPI.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include <mc/nbt/ListTag.h>
#include <mc/nbt/ShortTag.h>
#include <mc/nbt/StringTag.h>
#include <memory>

// NBT Static
class NbtStatic : public ScriptClass {
Expand Down Expand Up @@ -272,10 +273,10 @@ class NbtCompoundClass : public ScriptClass {
if (!canDelete) nbt.release();
}

static NbtCompoundClass* constructor(const Arguments& args);
static CompoundTag* extract(Local<Value> v);
static Local<Value> pack(CompoundTag* tag, bool noDelete = false);
static Local<Value> pack(std::unique_ptr<CompoundTag> tag);
static NbtCompoundClass* constructor(const Arguments& args);
static std::unique_ptr<CompoundTag> extract(Local<Value> v);
static Local<Value> pack(CompoundTag* tag, bool noDelete = false);
static Local<Value> pack(std::unique_ptr<CompoundTag> tag);

Local<Value> getType(const Arguments& args);
Local<Value> getKeys(const Arguments& args);
Expand Down
87 changes: 54 additions & 33 deletions src/legacy/api/PlayerAPI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -352,17 +352,22 @@ Local<Value> McClass::setPlayerNbt(const Arguments& args) {
CHECK_ARGS_COUNT(args, 2);
CHECK_ARG_TYPE(args[0], ValueKind::kString);
try {
mce::UUID uuid = mce::UUID::fromString(args[0].asString().toString());
CompoundTag* tag = NbtCompoundClass::extract(args[1]);
DBStorage* db = MoreGlobal::dbStorage;
if (db && db->hasKey("player_" + uuid.asString(), DBHelpers::Category::Player)) {
std::unique_ptr<CompoundTag> playerTag =
db->getCompoundTag("player_" + uuid.asString(), DBHelpers::Category::Player);
if (playerTag) {
std::string serverId = playerTag->at("ServerId");
if (!serverId.empty()) {
db->saveData(serverId, tag->toBinaryNbt(), DBHelpers::Category::Player);
return Boolean::newBoolean(true);
mce::UUID uuid = mce::UUID::fromString(args[0].asString().toString());
auto tag = NbtCompoundClass::extract(args[1]);
Player* player = ll::service::getLevel()->getPlayer(uuid);
if (player && tag) {
player->load(*tag, MoreGlobal::defaultDataLoadHelper());
} else if (tag) {
DBStorage* db = MoreGlobal::dbStorage;
if (db && db->hasKey("player_" + uuid.asString(), DBHelpers::Category::Player)) {
std::unique_ptr<CompoundTag> playerTag =
db->getCompoundTag("player_" + uuid.asString(), DBHelpers::Category::Player);
if (playerTag) {
std::string serverId = playerTag->at("ServerId");
if (!serverId.empty()) {
db->saveData(serverId, tag->toBinaryNbt(), DBHelpers::Category::Player);
return Boolean::newBoolean(true);
}
}
}
}
Expand All @@ -376,31 +381,47 @@ Local<Value> McClass::setPlayerNbtTags(const Arguments& args) {
CHECK_ARG_TYPE(args[0], ValueKind::kString);
CHECK_ARG_TYPE(args[2], ValueKind::kArray);
try {
mce::UUID uuid = mce::UUID::fromString(args[0].asString().toString());
CompoundTag* tag = NbtCompoundClass::extract(args[1]);
Local<Array> arr = args[2].asArray();
DBStorage* db = MoreGlobal::dbStorage;
if (db && db->hasKey("player_" + uuid.asString(), DBHelpers::Category::Player)) {
std::unique_ptr<CompoundTag> playerTag =
db->getCompoundTag("player_" + uuid.asString(), DBHelpers::Category::Player);
if (playerTag) {
std::string serverId = playerTag->at("ServerId");
if (!serverId.empty() && db->hasKey(serverId, DBHelpers::Category::Player)) {
auto loadedTag = db->getCompoundTag(serverId, DBHelpers::Category::Player);
if (loadedTag) {
for (int i = 0; i < arr.size(); ++i) {
auto value = arr.get(i);
if (value.getKind() == ValueKind::kString) {
std::string tagName = value.asString().toString();
if (!tag->at(tagName).is_null()) {
loadedTag->at(tagName) = tag->at(tagName);
mce::UUID uuid = mce::UUID::fromString(args[0].asString().toString());
auto tag = NbtCompoundClass::extract(args[1]);
Local<Array> arr = args[2].asArray();
Player* player = ll::service::getLevel()->getPlayer(uuid);
if (player && tag) {
CompoundTag loadedTag;
player->save(loadedTag);
for (int i = 0; i < arr.size(); ++i) {
auto value = arr.get(i);
if (value.getKind() == ValueKind::kString) {
std::string tagName = value.asString().toString();
if (!tag->at(tagName).is_null()) {
loadedTag.at(tagName) = tag->at(tagName);
}
}
}
player->load(loadedTag, MoreGlobal::defaultDataLoadHelper());
} else if (tag) {
DBStorage* db = MoreGlobal::dbStorage;
if (db && db->hasKey("player_" + uuid.asString(), DBHelpers::Category::Player)) {
std::unique_ptr<CompoundTag> playerTag =
db->getCompoundTag("player_" + uuid.asString(), DBHelpers::Category::Player);
if (playerTag) {
std::string serverId = playerTag->at("ServerId");
if (!serverId.empty() && db->hasKey(serverId, DBHelpers::Category::Player)) {
auto loadedTag = db->getCompoundTag(serverId, DBHelpers::Category::Player);
if (loadedTag) {
for (int i = 0; i < arr.size(); ++i) {
auto value = arr.get(i);
if (value.getKind() == ValueKind::kString) {
std::string tagName = value.asString().toString();
if (!tag->at(tagName).is_null()) {
loadedTag->at(tagName) = tag->at(tagName);
}
}
}
db->saveData(serverId, loadedTag->toBinaryNbt(), DBHelpers::Category::Player);
return Boolean::newBoolean(true);
}
db->saveData(serverId, loadedTag->toBinaryNbt(), DBHelpers::Category::Player);
return Boolean::newBoolean(true);
}
return Boolean::newBoolean(true);
}
}
}
Expand Down Expand Up @@ -2957,10 +2978,10 @@ Local<Value> PlayerClass::setNbt(const Arguments& args) {
if (!player) return Local<Value>();

auto nbt = NbtCompoundClass::extract(args[0]);
if (!nbt || !MoreGlobal::defaultDataLoadHelper) {
if (!nbt) {
return Local<Value>();
}
return Boolean::newBoolean(player->load(*nbt, *MoreGlobal::defaultDataLoadHelper));
return Boolean::newBoolean(player->load(*nbt, MoreGlobal::defaultDataLoadHelper()));
}
CATCH("Fail in setNbt!")
}
Expand Down
2 changes: 1 addition & 1 deletion src/legacy/api/RemoteCallAPI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ RemoteCall::ValueType pack(Local<Object> value) {
auto pos = IntPos::extractPos(value);
return std::make_pair(pos->getBlockPos(), pos->getDimensionId());
}
if (IsInstanceOf<NbtCompoundClass>(value)) return NbtCompoundClass::extract(value);
if (IsInstanceOf<NbtCompoundClass>(value)) return NbtCompoundClass::extract(value).get();
std::unordered_map<std::string, RemoteCall::ValueType> result;
for (auto& k : value.getKeyNames()) result.emplace(k, pack(value.get(k)));
return std::move(result);
Expand Down
9 changes: 5 additions & 4 deletions src/lse/api/MoreGlobal.cpp
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
#include "MoreGlobal.h"

#include "ll/api/memory/Hook.h"
#include "mc/dataloadhelper/DefaultDataLoadHelper.h"
#include "mc/world/level/storage/DBStorage.h"
#include "mc/world/level/storage/DBStorageConfig.h"

namespace MoreGlobal {
DBStorage* dbStorage;
DefaultDataLoadHelper* defaultDataLoadHelper;
DefaultDataLoadHelper* helper;
DefaultDataLoadHelper& defaultDataLoadHelper() { return (DefaultDataLoadHelper&)helper; }

LL_TYPE_INSTANCE_HOOK(
DBStorageHook,
Expand All @@ -25,9 +27,8 @@ LL_TYPE_INSTANCE_HOOK(
void onLoad() { DBStorageHook::hook(); }

bool onEnable() {
defaultDataLoadHelper =
static_cast<DefaultDataLoadHelper*>(ll::memory::resolveSymbol("??_7DefaultDataLoadHelper@@6B@"));
if (defaultDataLoadHelper && dbStorage && DBStorageHook::unhook()) {
helper = static_cast<DefaultDataLoadHelper*>(ll::memory::resolveSymbol("??_7DefaultDataLoadHelper@@6B@"));
if (helper && dbStorage && DBStorageHook::unhook()) {
return true;
}
return false;
Expand Down
2 changes: 1 addition & 1 deletion src/lse/api/MoreGlobal.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
class DBStorage;
namespace MoreGlobal {
extern DBStorage* dbStorage;
extern DefaultDataLoadHelper* defaultDataLoadHelper;
extern DefaultDataLoadHelper& defaultDataLoadHelper();
extern void onLoad();
extern bool onEnable();
}; // namespace MoreGlobal

0 comments on commit a33d674

Please sign in to comment.