Skip to content

Commit

Permalink
feat: support LeviLamina 1.0.0 (WIP)
Browse files Browse the repository at this point in the history
  • Loading branch information
Dofes committed Jan 2, 2025
1 parent 7c53b69 commit d7dcf3e
Show file tree
Hide file tree
Showing 12 changed files with 216 additions and 212 deletions.
2 changes: 1 addition & 1 deletion .clang-format
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,4 @@ PackConstructorInitializers: CurrentLine
PointerAlignment: Left
TabWidth: 4
UseTab: Never
SortIncludes: CaseSensitive
SortIncludes: Never
13 changes: 8 additions & 5 deletions src/levioptimize/LeviOptimize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,16 @@
#include "ll/api/mod/RegisterHelper.h"
#include "ll/api/utils/ErrorUtils.h"

#include <stdexcept>

namespace lo {
namespace command {
extern void registerTimingCommand();
}
static std::unique_ptr<LeviOptimize> instance;

LeviOptimize& LeviOptimize::getInstance() { return *instance; }
LeviOptimize& LeviOptimize::getInstance() {
static LeviOptimize instance;
return instance;
}

bool LeviOptimize::load() { return loadConfig(); }

Expand All @@ -38,7 +39,9 @@ bool LeviOptimize::disable() { // NOLINT
return true;
}

ll::Logger& LeviOptimize::getLogger() const { return getSelf().getLogger(); }
ll::io::Logger& LeviOptimize::getLogger() const { return getSelf().getLogger(); }

ll::io::Logger& getLogger() { return LeviOptimize::getInstance().getLogger(); }

std::string const& LeviOptimize::getName() const { return getSelf().getManifest().name; }

Expand Down Expand Up @@ -68,4 +71,4 @@ bool LeviOptimize::saveConfig() { return ll::config::saveConfig(mConfig.value(),
bool LeviOptimize::isEnabled() const { return getSelf().isEnabled(); }
} // namespace lo

LL_REGISTER_MOD(lo::LeviOptimize, lo::instance);
LL_REGISTER_MOD(lo::LeviOptimize, lo::LeviOptimize::getInstance());
9 changes: 5 additions & 4 deletions src/levioptimize/LeviOptimize.h
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
#pragma once

#include "Config.h"
#include "ll/api/Logger.h"
#include "ll/api/io/Logger.h"
#include "ll/api/mod/NativeMod.h"

#include <string_view>

namespace lo {

ll::io::Logger& getLogger();

class LeviOptimize {

public:
LeviOptimize(ll::mod::NativeMod& self) : mSelf(self) {}
LeviOptimize() : mSelf(*ll::mod::NativeMod::current()) {}

static LeviOptimize& getInstance();

Expand All @@ -23,7 +24,7 @@ class LeviOptimize {

bool disable();

ll::Logger& getLogger() const;
ll::io::Logger& getLogger() const;

std::string const& getName() const;

Expand Down
16 changes: 9 additions & 7 deletions src/levioptimize/features/ChunkLeakFix.cpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
#include "features.h"

#include "ll/api/memory/Memory.h"
#include <ll/api/event/EventBus.h>
#include <ll/api/event/player/PlayerLeaveEvent.h>
#include <ll/api/event/player/PlayerDisconnectEvent.h>
#include <mc/server/ServerLevel.h>
#include <mc/world/ActorUniqueID.h>
#include <mc/common/ActorUniqueID.h>
#include <mc/world/level/saveddata/maps/MapItemTrackedActor.h>

namespace lo::chunk_leak_fix {
Expand All @@ -14,9 +15,8 @@ struct ChunkLeakFix::Impl {
Impl() {
auto& eventBus = ll::event::EventBus::getInstance();

playerLeaveEventListener =
eventBus.emplaceListener<ll::event::player::PlayerLeaveEvent>([&](ll::event::player::PlayerLeaveEvent& event
) {
playerLeaveEventListener = eventBus.emplaceListener<ll::event::player::PlayerDisconnectEvent>(
[&](ll::event::player::PlayerDisconnectEvent& event) {
auto& player = event.self();
auto& level = static_cast<ServerLevel&>(event.self().getLevel());
auto& manager = level._getMapDataManager();
Expand All @@ -28,10 +28,12 @@ struct ChunkLeakFix::Impl {
for (auto& [id, data] : allMapData) {
auto& v = ll::memory::dAccess<std::vector<std::shared_ptr<MapItemTrackedActor>>>(data.get(), 96);
std::erase_if(v, [&player](auto& ptr) {
return ll::memory::dAccess<ActorUniqueID>(ptr.get(), 8).id == player.getOrCreateUniqueID().id;
return ll::memory::dAccess<ActorUniqueID>(ptr.get(), 8).rawID
== player.getOrCreateUniqueID().rawID;
});
}
});
}
);
}

~Impl() {
Expand Down
5 changes: 3 additions & 2 deletions src/levioptimize/features/HopperItemOpt.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include "features.h"
#include "ll/api/memory/Hook.h"
#include "mc/world/item/registry/ItemStack.h"
#include "mc/world/item/ItemStack.h"
#include "mc/world/level/BlockSource.h"
#include "mc/world/level/Level.h"
#include "mc/world/level/block/actor/HopperBlockActor.h"
Expand Down Expand Up @@ -29,7 +29,8 @@ LL_TYPE_INSTANCE_HOOK(
continue;
}
auto& containerItem = *containerItemRef;
if (containerItem.isValid()) {
if (containerItem.mValid_DeprecatedSeeComment && containerItem.mItem && !containerItem.isNull()
&& containerItem.mCount > 0) {
auto maxSize = containerItem.getMaxStackSize();
if (containerItem.mCount == maxSize || !containerItem.isStackable(item)) {
continue;
Expand Down
27 changes: 14 additions & 13 deletions src/levioptimize/features/MovingBlockOpt.cpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
#include "features.h"
#include "ll/api/memory/Hook.h"
#include "mc/nbt/CompoundTag.h"
#include "mc/network/packet/BlockActorDataPacket.h"
#include "mc/world/level/BlockSource.h"
#include "mc/world/level/block/Block.h"
#include "mc/world/level/block/actor/BlockActor.h"
#include "mc/world/level/block/actor/MovingBlockActor.h"
#include "mc/world/level/BlockPos.h"
#include "levioptimize/LeviOptimize.h"

namespace lo::moving_block_opt {

Expand All @@ -28,26 +31,24 @@ LL_TYPE_INSTANCE_HOOK(
MovingBlockActorSaveHook,
ll::memory::HookPriority::Normal,
MovingBlockActor,
"?save@MovingBlockActor@@UEBA_NAEAVCompoundTag@@@Z",
&MovingBlockActor::$save,
bool,
CompoundTag& tag
::CompoundTag& tag,
const ::SaveContext& saveContext
) {
if (!updatePacketFlag) {
return origin(tag);
return origin(tag, saveContext);
}

if (!::BlockActor::save(tag)) return false; // NOLINT
if (!::BlockActor::save(tag, saveContext)) return false; // NOLINT

Block* block = ll::memory::dAccess<Block*>(this, 200);
Block* extraBlock = ll::memory::dAccess<Block*>(this, 208);
tag["movingBlock"] = block->getSerializationId();
tag["movingBlockExtra"] = extraBlock->getSerializationId();
tag["movingBlock"] = mWrappedBlock->getSerializationId();
tag["movingBlockExtra"] = mWrappedExtraBlock->getSerializationId();

BlockPos& pos = ll::memory::dAccess<BlockPos>(this, 58 * sizeof(int));
tag["pistonPosX"] = pos.x;
tag["pistonPosY"] = pos.y;
tag["pistonPosZ"] = pos.z;
tag["expanding"] = ll::memory::dAccess<bool>(this, 244);
tag["pistonPosX"] = mPosition->x;
tag["pistonPosY"] = mPosition->y;
tag["pistonPosZ"] = mPosition->z;
tag["expanding"] = mPistonBlockExpanding;

return true;
}
Expand Down
90 changes: 36 additions & 54 deletions src/levioptimize/features/PacketSenderOpt.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#include "features.h"
#include "ll/api/memory/Hook.h"
#include "ll/api/memory/Memory.h"
#include "mc/deps/core/utility/BinaryStream.h"
#include "mc/deps/raknet/AddressOrGUID.h"
#include "mc/deps/raknet/RakPeer.h"
Expand All @@ -9,19 +8,21 @@
#include "mc/network/NetworkSystem.h"
#include "mc/network/RakNetConnector.h"
#include "mc/network/packet/Packet.h"
#include "mc/resources/PacketPriority.h"
#include "mc/resources/PacketReliability.h"
#include "mc/server/LoopbackPacketSender.h"
#include "mc/deps/raknet/PacketPriority.h"
#include "mc/deps/raknet/PacketReliability.h"
#include "mc/network/LoopbackPacketSender.h"
#include "mc/deps/raknet/RakPeerInterface.h"
#include "mc/network/ClientOrServerNetworkSystemRef.h"
#include "mc/network/NetworkIdentifierWithSubId.h"
#include "levioptimize/LeviOptimize.h"

namespace lo::packet_sender_opt {

LL_TYPE_INSTANCE_HOOK(
RakPeerSendPacketHook,
ll::memory::HookPriority::Normal,
RakNetConnector::RakNetNetworkPeer,
// &RakNetConnector::RakNetNetworkPeer::sendPacket,
"?sendPacket@RakNetNetworkPeer@RakNetConnector@@UEAAXAEBV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@"
"std@@W4Reliability@NetworkPeer@@W4Compressibility@@@Z",
&RakNetConnector::RakNetNetworkPeer::$sendPacket,
void,
std::string const& data,
::NetworkPeer::Reliability reliability,
Expand All @@ -47,30 +48,8 @@ LL_TYPE_INSTANCE_HOOK(

std::array<char const*, 2> datas{"\xFE", data.c_str()};
std::array<int, 2> lengths{1, (int)data.size()};

ll::memory::virtualCall<
uint,
char const**,
int const*,
int,
::PacketPriority,
::PacketReliability,
char,
struct RakNet::AddressOrGUID,
bool,
uint>(
ll::memory::dAccess<RakNet::RakPeerInterface*>(this, 0x18),
24, // sendList
datas.data(),
lengths.data(),
2,
PacketPriority::Medium,
raknetRel,
0,
ll::memory::dAccess<NetworkIdentifier>(this, 0x20).mGuid,
0,
0
);
mRakPeer
.SendList(datas.data(), lengths.data(), 2, PacketPriority::MediumPriority, raknetRel, 0, mId.mGuid, false, 0);
}

LL_TYPE_INSTANCE_HOOK(
Expand All @@ -84,39 +63,43 @@ LL_TYPE_INSTANCE_HOOK(
::SubClientId senderSubId
) {
BinaryStream stream;
packet.writeWithHeader(senderSubId, stream);
return _sendInternal(id, packet, *stream.mBuffer);
// packet.writeWithHeader(senderSubId, stream);
stream.writeUnsignedVarInt(
std::to_underlying(packet.getId()) | (std::to_underlying(senderSubId) << 10)
| ((std::to_underlying(packet.mClientSubId) << 12))
);
packet.write(stream);
return _sendInternal(id, packet, stream.mBuffer);
}

LL_TYPE_INSTANCE_HOOK(
NetworkSystemSendMultiHook,
ll::memory::HookPriority::Normal,
LoopbackPacketSender,
"?sendToClients@LoopbackPacketSender@@UEAAXAEBV?$vector@UNetworkIdentifierWithSubId@@V?$allocator@"
"UNetworkIdentifierWithSubId@@@std@@@std@@AEBVPacket@@@Z",
&LoopbackPacketSender::$sendToClients,
void,
std::vector<struct NetworkIdentifierWithSubId> const& ids,
Packet const& packet
) {
if (ids.empty()) {
return;
}
auto& networkSystem = ll::memory::dAccess<NetworkSystem>(&mNetwork.toServerNetworkSystem(), 24);
auto& networkSystem = mNetwork->toServerNetworkSystem();

BinaryStream pktstream;
pktstream.write("\0\0\0\0\0", 5);
packet.write(pktstream);
auto res = std::move(*pktstream.mBuffer);
auto res = std::move(pktstream.mBuffer);

BinaryStream headerstream;
for (auto& id : ids) {
headerstream.reset();
headerstream.writeUnsignedVarInt(
((std::to_underlying(packet.mClientSubId) & 3) << 12) | (std::to_underlying(packet.getId()) & 0x3FF)
| ((std::to_underlying(id.mSubClientId) & 3) << 10)
| ((std::to_underlying(id.subClientId) & 3) << 10)
);
auto size = headerstream.mBuffer->size();
memcpy(res.data() + (5 - size), headerstream.mBuffer->data(), size);
auto size = headerstream.mBuffer.size();
memcpy(res.data() + (5 - size), headerstream.mBuffer.data(), size);
struct {
char const* data;
void* filler{};
Expand All @@ -127,70 +110,69 @@ LL_TYPE_INSTANCE_HOOK(
.size = res.size() - 5 + size,
.cap = res.capacity() > 16 ? res.capacity() : 16
};
networkSystem._sendInternal(id.mIdentifier, packet, *(std::string*)(&datas));
networkSystem._sendInternal(*id.id, packet, *(std::string*)(&datas));
}
}

LL_TYPE_INSTANCE_HOOK(
BatchedPeerCtorHook,
ll::memory::HookPriority::Normal,
BatchedNetworkPeer,
"??0BatchedNetworkPeer@@QEAA@V?$shared_ptr@VNetworkPeer@@@std@@AEAVScheduler@@@Z",
BatchedNetworkPeer*,
&BatchedNetworkPeer::$ctor,
void*,
std::shared_ptr<class NetworkPeer> peer,
class Scheduler& scheduler
) {
auto res = origin(std::move(peer), scheduler);
*(std::recursive_mutex**)(&mSendQueue.mCachelineFiller[32]) = new std::recursive_mutex;
auto res = origin(std::move(peer), scheduler);
*(std::recursive_mutex**)(&mSendQueue->mCachelineFiller[32]) = new std::recursive_mutex;
return res;
}

LL_TYPE_INSTANCE_HOOK(
BatchedPeerDtorHook,
ll::memory::HookPriority::Normal,
BatchedNetworkPeer,
"??_GBatchedNetworkPeer@@UEAAPEAXI@Z",
&BatchedNetworkPeer::$dtor,
void
) {
delete *(std::recursive_mutex**)(&mSendQueue.mCachelineFiller[32]);
delete *(std::recursive_mutex**)(&mSendQueue->mCachelineFiller[32]);
origin();
}

LL_TYPE_INSTANCE_HOOK(
BatchedPeerSendPacketHook,
ll::memory::HookPriority::Normal,
BatchedNetworkPeer,
"?sendPacket@BatchedNetworkPeer@@UEAAXAEBV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@"
"W4Reliability@NetworkPeer@@W4Compressibility@@@Z",
&BatchedNetworkPeer::$sendPacket,
void,
std::string const& data,
::NetworkPeer::Reliability reliability,
::Compressibility compressibility
) {
std::lock_guard l(**(std::recursive_mutex**)&mSendQueue.mCachelineFiller[32]);
std::lock_guard l(**(std::recursive_mutex**)&mSendQueue->mCachelineFiller[32]);
origin(data, reliability, compressibility);
}

LL_TYPE_INSTANCE_HOOK(
BatchedPeerFlushHook,
ll::memory::HookPriority::Normal,
BatchedNetworkPeer,
"?flush@BatchedNetworkPeer@@UEAAX$$QEAV?$function@$$A6AXXZ@std@@@Z",
&BatchedNetworkPeer::$flush,
void,
std::function<void()>&& callback
) {
std::lock_guard l(**(std::recursive_mutex**)&mSendQueue.mCachelineFiller[32]);
std::lock_guard l(**(std::recursive_mutex**)&mSendQueue->mCachelineFiller[32]);
origin(std::move(callback));
}

LL_TYPE_INSTANCE_HOOK(
BatchedPeerUpdateHook,
ll::memory::HookPriority::Normal,
BatchedNetworkPeer,
"?update@BatchedNetworkPeer@@UEAAXXZ",
&BatchedNetworkPeer::$update,
void
) {
std::lock_guard l(**(std::recursive_mutex**)&mSendQueue.mCachelineFiller[32]);
std::lock_guard l(**(std::recursive_mutex**)&mSendQueue->mCachelineFiller[32]);
origin();
}

Expand Down
Loading

0 comments on commit d7dcf3e

Please sign in to comment.