Skip to content

Commit

Permalink
Merge pull request #12 from zzz-mimimi/master
Browse files Browse the repository at this point in the history
Implement SetScore packet and set-score scripting event
  • Loading branch information
Imrglop authored Nov 23, 2024
2 parents 03359ff + 12ed8e3 commit dfb01a8
Show file tree
Hide file tree
Showing 8 changed files with 98 additions and 0 deletions.
2 changes: 2 additions & 0 deletions LatiteRewrite.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -574,6 +574,7 @@
<ClInclude Include="src\sdk\common\network\PacketSender.h" />
<ClInclude Include="src\sdk\common\network\packet\ActorEventPacket.h" />
<ClInclude Include="src\sdk\common\network\packet\CommandRequestPacket.h" />
<ClInclude Include="src\sdk\common\network\packet\SetScorePacket.h" />
<ClInclude Include="src\sdk\common\network\packet\SetTitlePacket.h" />
<ClInclude Include="src\sdk\common\network\packet\TextPacket.h" />
<ClInclude Include="src\sdk\common\network\RakPeer.h" />
Expand Down Expand Up @@ -797,6 +798,7 @@
<ClCompile Include="src\sdk\common\client\renderer\Tessellator.cpp" />
<ClCompile Include="src\sdk\common\client\util\JpegCommentWriter.cpp" />
<ClCompile Include="src\sdk\common\network\packet\CommandRequestPacket.cpp" />
<ClCompile Include="src\sdk\common\network\packet\SetScorePacket.cpp" />
<ClCompile Include="src\sdk\common\network\packet\TextPacket.cpp" />
<ClCompile Include="src\sdk\common\world\actor\Actor.cpp" />
<ClCompile Include="src\sdk\common\world\actor\player\Player.cpp" />
Expand Down
2 changes: 2 additions & 0 deletions LatiteRewrite.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@
<ClCompile Include="src\client\script\objects\OptionsScriptingObject.cpp" />
<ClCompile Include="src\sdk\common\client\gui\screens\ContainerScreenController.cpp" />
<ClCompile Include="src\sdk\common\world\level\LevelData.cpp" />
<ClCompile Include="src\sdk\common\network\packet\SetScorePacket.cpp" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="assets\lang\en_US.json" />
Expand Down Expand Up @@ -760,6 +761,7 @@
<ClInclude Include="assets\lang\pt_PT.json" />
<ClInclude Include="assets\lang\zh_CN.json" />
<ClInclude Include="assets\lang\zh_TW.json" />
<ClInclude Include="src\sdk\common\network\packet\SetScorePacket.h" />
</ItemGroup>
<ItemGroup>
<ResourceCompile Include="LatiteRewrite.rc" />
Expand Down
9 changes: 9 additions & 0 deletions src/client/hook/impl/PacketHooks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,15 @@ void PacketHooks::PacketHandlerDispatcherInstance_handle(void* instance, void* n
PluginManager::Event sEv{ XW("change-dimension"), {}, false };
Latite::getPluginManager().dispatchEvent(sEv);
}
else if (packetId == SDK::PacketID::SET_SCORE) {
auto pkt = std::static_pointer_cast<SDK::SetScorePacket>(packet);

auto data = PluginManager::Event::Value(L"data");
data.val = pkt->serialize();

PluginManager::Event sEv{ XW("set-score"), { data }, false };
Latite::getPluginManager().dispatchEvent(sEv);
}
else if (packetId == SDK::PacketID::TRANSFER) {
PluginManager::Event sEv{ XW("transfer"), {}, false };
Latite::getPluginManager().dispatchEvent(sEv);
Expand Down
1 change: 1 addition & 0 deletions src/client/hook/impl/PacketHooks.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include "../Hook.h"
#include "sdk/common/network/packet/SetTitlePacket.h"
#include "sdk/common/network/packet/TextPacket.h"
#include "sdk/common/network/packet/SetScorePacket.h"

class PacketHooks : public HookGroup {
static void* SetTitlePacket_readExtended(SDK::SetTitlePacket* pkt, void* b, void* c);
Expand Down
1 change: 1 addition & 0 deletions src/client/script/PluginManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,7 @@ void PluginManager::initListeners()
eventListeners[L"change-dimension"] = {};
eventListeners[L"pre-move"] = {};
eventListeners[L"post-move"] = {};
eventListeners[L"set-score"] = {};
}

void PluginManager::unloadScript(std::shared_ptr<JsPlugin> ptr) {
Expand Down
1 change: 1 addition & 0 deletions src/sdk/common/network/Packet.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ namespace SDK {
SET_TITLE = 0x58,
COMMAND_REQUEST = 0x4D,
TOAST_REQUEST = 0xBA,
SET_SCORE = 0x6c,
COUNT,
};

Expand Down
23 changes: 23 additions & 0 deletions src/sdk/common/network/packet/SetScorePacket.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#include "pch.h"
#include "SetScorePacket.h"

std::wstring SDK::SetScorePacket::serialize() const {
nlohmann::json serialized;

serialized["type"] = static_cast<int>(type);

for (const auto& info : scoreInfo) {
nlohmann::json score;
score["scoreboardId"] = info.scoreboardId.rawId;
score["objectiveName"] = info.objectiveName;
score["scoreValue"] = info.scoreValue;
score["identityType"] = static_cast<unsigned char>(info.identityType);
score["playerId"] = info.playerId;
score["entityId"] = info.entityId;
score["fakePlayerName"] = info.fakePlayerName;

serialized["scoreInfo"].push_back(score);
}

return util::StrToWStr(serialized.dump());
}
59 changes: 59 additions & 0 deletions src/sdk/common/network/packet/SetScorePacket.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
#pragma once
#include "../Packet.h"
#include "sdk/String.h"

namespace SDK {

class SetScorePacket : public Packet {
private:
enum class PacketType : uint8_t {
Change = 0x0,
Remove = 0x1
};

enum class IdentityType : unsigned char {
Invalid = 0x0,
Player = 0x1,
Entity = 0x2,
FakePlayer = 0x3
};

enum class DefinitionType : unsigned char {
Invalid = 0x0,
Player = 0x1,
Entity = 0x2,
FakePlayer = 0x3
};

class IdentityDef;

struct ScoreboardId {
int64_t rawId;
IdentityDef* identityDef;
};

class IdentityDef {
ScoreboardId scoreboardId;
bool isHiddenFakePlayer;
int64_t playerId;
int64_t entityId;
std::string playerName;
DefinitionType identityType;
};

struct ScoreInfo {
ScoreboardId scoreboardId;
std::string objectiveName;
int scoreValue;
DefinitionType identityType;
int64_t playerId;
int64_t entityId;
std::string fakePlayerName;
};

public:
PacketType type;
std::vector<ScoreInfo> scoreInfo;
std::wstring serialize() const;
};
}

0 comments on commit dfb01a8

Please sign in to comment.