Skip to content
This repository has been archived by the owner on Oct 10, 2024. It is now read-only.

Commit

Permalink
fix #516
Browse files Browse the repository at this point in the history
  • Loading branch information
cddjr committed Oct 28, 2023
1 parent 8fbb9e4 commit 221832e
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 55 deletions.
10 changes: 7 additions & 3 deletions gui/gui-helpers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,11 @@ bool CustomListBoxPlayerSelectionMultiple(const char* label, std::array<std::pai
response = false;
auto localData = GetPlayerData(*Game::pLocalPlayer);
for (auto playerData : GetAllPlayerData()) {
if (playerData->fields.Disconnected) // maybe make that an option for replays ? (parameter based on "state.showDisconnected" related data)
auto playerSelection = PlayerSelection(playerData);
const auto& player = playerSelection.validate();
if (!player.has_value())
continue;
if (player.is_Disconnected()) // maybe make that an option for replays ? (parameter based on "state.showDisconnected" related data)
continue;

app::GameData_PlayerOutfit* outfit = GetPlayerOutfit(playerData);
Expand All @@ -149,7 +153,7 @@ bool CustomListBoxPlayerSelectionMultiple(const char* label, std::array<std::pai
{
if (const auto& result = item.first.validate();
!result.has_value() || result.is_Disconnected())
item.first = PlayerSelection(playerData);
item.first = playerSelection;
}
response = true;
}
Expand All @@ -171,7 +175,7 @@ bool CustomListBoxPlayerSelectionMultiple(const char* label, std::array<std::pai
}
else if (PlayerIsImpostor(localData) && PlayerIsImpostor(playerData))
nameColor = AmongUsColorToImVec4(Palette__TypeInfo->static_fields->ImpostorRed);
else if (PlayerSelection(playerData).is_LocalPlayer() || std::count(State.aumUsers.begin(), State.aumUsers.end(), playerData->fields.PlayerId))
else if (player.is_LocalPlayer() || std::count(State.aumUsers.begin(), State.aumUsers.end(), playerData->fields.PlayerId))
nameColor = AmongUsColorToImVec4(Palette__TypeInfo->static_fields->Orange);

if (playerData->fields.IsDead)
Expand Down
37 changes: 23 additions & 14 deletions gui/tabs/players_tab.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,20 @@ namespace PlayersTab {
bool shouldEndListBox = ImGui::ListBoxHeader("###players#list", ImVec2(200, 150) * State.dpiScale);
auto localData = GetPlayerData(*Game::pLocalPlayer);
for (auto playerData : GetAllPlayerData()) {
if (playerData->fields.Disconnected)
const auto& player = PlayerSelection(playerData).validate();
if (!player.has_value())
continue;
if (player.is_Disconnected())
continue;

app::GameData_PlayerOutfit* outfit = GetPlayerOutfit(playerData);
if (outfit == NULL) continue;
std::string playerName = convert_from_string(GameData_PlayerOutfit_get_PlayerName(outfit, nullptr));
ImGui::PushStyleVar(ImGuiStyleVar_ItemSpacing, ImVec2(0, 0) * State.dpiScale);
ImGui::PushStyleVar(ImGuiStyleVar_FramePadding, ImVec2(0, 0) * State.dpiScale);
if (ImGui::Selectable(std::string("##" + playerName).c_str(), selectedPlayer.equals(playerData))) {
State.selectedPlayer = PlayerSelection(playerData);
selectedPlayer = State.selectedPlayer.validate();
if (ImGui::Selectable(std::string("##" + playerName).c_str(), selectedPlayer.equals(player))) {
State.selectedPlayer = player;
selectedPlayer = player;
}
ImGui::SameLine();
ImGui::ColorButton(std::string("##" + playerName + "_ColorButton").c_str(), AmongUsColorToImVec4(GetPlayerColor(outfit->fields.ColorId)), ImGuiColorEditFlags_NoBorder | ImGuiColorEditFlags_NoTooltip);
Expand All @@ -46,7 +49,7 @@ namespace PlayersTab {
}
else if(PlayerIsImpostor(localData) && PlayerIsImpostor(playerData))
nameColor = AmongUsColorToImVec4(Palette__TypeInfo->static_fields->ImpostorRed);
else if (PlayerSelection(playerData).is_LocalPlayer() || std::count(State.aumUsers.begin(), State.aumUsers.end(), playerData->fields.PlayerId))
else if (player.is_LocalPlayer() || std::count(State.aumUsers.begin(), State.aumUsers.end(), playerData->fields.PlayerId))
nameColor = AmongUsColorToImVec4(Palette__TypeInfo->static_fields->Orange);

if (playerData->fields.IsDead)
Expand Down Expand Up @@ -77,7 +80,7 @@ namespace PlayersTab {
GameOptions options;
if (IsInGame() && options.GetGameMode() != GameModes__Enum::HideNSeek && !GetPlayerData(*Game::pLocalPlayer)->fields.IsDead) { //Player selection doesn't matter
if (ImGui::Button("Call Meeting")) {
State.rpcQueue.push(new RpcReportPlayer(PlayerSelection()));
State.rpcQueue.push(new RpcReportPlayer({}));
}
}
if (State.activeImpersonation)
Expand All @@ -100,28 +103,30 @@ namespace PlayersTab {
queue->push(new RpcSetName(State.originalName));
State.activeImpersonation = false;
}
if (IsInLobby())
ResetOriginalAppearance();
}
}
if (selectedPlayer.has_value())
{
if (IsInGame() && !GetPlayerData(*Game::pLocalPlayer)->fields.IsDead && selectedPlayer.get_PlayerData()->fields.IsDead) {
ImGui::NewLine();
if (ImGui::Button("Report Body")) {
State.rpcQueue.push(new RpcReportPlayer(State.selectedPlayer));
State.rpcQueue.push(new RpcReportPlayer(selectedPlayer));
}
}
ImGui::NewLine();

if (IsInGame() && !selectedPlayer.is_Disconnected() && !selectedPlayer.is_LocalPlayer())
{
if (State.playerToFollow.equals(State.selectedPlayer)) {
if (State.playerToFollow.equals(selectedPlayer)) {
if (ImGui::Button("Stop Spectating")) {
State.playerToFollow = PlayerSelection();
State.playerToFollow.reset();
}
} else {
if (ImGui::Button("Spectate")) {
State.FreeCam = false;
State.playerToFollow = State.selectedPlayer;
State.playerToFollow = selectedPlayer;
}
}
}
Expand All @@ -136,7 +141,7 @@ namespace PlayersTab {
}
else if(!selectedPlayer.is_LocalPlayer()) {
if ((IsInMultiplayerGame() || IsInLobby()) && ImGui::Button("Steal Name")) {
ImpersonateName(State.selectedPlayer);
ImpersonateName(selectedPlayer.get_PlayerData());
}
}
if ((IsInGame() || IsInLobby())) {
Expand All @@ -155,8 +160,12 @@ namespace PlayersTab {

if (IsInGame())
queue = &State.rpcQueue;
else if (IsInLobby())
else if (IsInLobby()) {
queue = &State.lobbyRpcQueue;
if (State.originalColor == Game::NoColorId) {
SaveOriginalAppearance();
}
}

if (queue != nullptr) {
if (IsHost())
Expand All @@ -168,7 +177,7 @@ namespace PlayersTab {
queue->push(new RpcSetVisor(visorId));
queue->push(new RpcSetHat(hatId));
queue->push(new RpcSetNamePlate(namePlateId));
ImpersonateName(State.selectedPlayer);
ImpersonateName(selectedPlayer.get_PlayerData());
State.activeImpersonation = true;
}
}
Expand All @@ -186,7 +195,7 @@ namespace PlayersTab {
if (ImGui::Button("Kill Player"))
{
previousPlayerPosition = GetTrueAdjustedPosition(*Game::pLocalPlayer);
State.rpcQueue.push(new CmdCheckMurder(State.selectedPlayer));
State.rpcQueue.push(new CmdCheckMurder(selectedPlayer));
framesPassed = 40;
}
}
Expand Down
50 changes: 21 additions & 29 deletions user/utility.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,7 @@ Vector2 GetTrueAdjustedPosition(PlayerControl* playerControl)
#pragma region PlayerSelection
PlayerSelection::PlayerSelection() noexcept
{
this->clientId = Game::NoClientId;
this->playerId = Game::NoPlayerId;
this->reset();
}

PlayerSelection::PlayerSelection(const PlayerControl* playerControl) {
Expand All @@ -134,14 +133,18 @@ PlayerSelection::PlayerSelection(const PlayerControl* playerControl) {
this->playerId = playerControl->fields.PlayerId;
}
else {
new (this)PlayerSelection();
this->reset();
}
}

PlayerSelection::PlayerSelection(GameData_PlayerInfo* playerData) {
new (this)PlayerSelection(app::GameData_PlayerInfo_get_Object(playerData, nullptr));
}

PlayerSelection::PlayerSelection(const PlayerSelection::Result& result) {
new (this)PlayerSelection(result.has_value() ? result.get_PlayerControl() : nullptr);
}

PlayerSelection::Result PlayerSelection::validate() {
auto playerControl = this->get_PlayerControl();
if (playerControl) {
Expand All @@ -150,17 +153,27 @@ PlayerSelection::Result PlayerSelection::validate() {
return { (*playerControl), playerData };
}
}
this->clientId = Game::NoClientId;
this->playerId = Game::NoPlayerId;
this->reset();
return {};
}

bool PlayerSelection::equals(const PlayerSelection& selectedPlayer) const
{
if (this == &selectedPlayer) return true;
if (!this->has_value() || !selectedPlayer.has_value()) return false;
return std::tie(clientId, playerId) == std::tie(selectedPlayer.clientId, selectedPlayer.playerId);
}

bool PlayerSelection::equals(const PlayerSelection::Result& selectedPlayer) const {
if (!this->has_value() || !selectedPlayer.has_value()) return false;
if (clientId == Game::HostInherit) {
return playerId == selectedPlayer.get_PlayerControl()->fields.PlayerId;
}
return std::tie(clientId, playerId) ==
std::tie(selectedPlayer.get_PlayerControl()->fields._.OwnerId,
selectedPlayer.get_PlayerControl()->fields.PlayerId);
}

std::optional<PlayerControl*> PlayerSelection::get_PlayerControl() const {
if (!this->has_value())
return std::nullopt;
Expand Down Expand Up @@ -204,27 +217,6 @@ std::optional<GameData_PlayerInfo*> PlayerSelection::get_PlayerData() const
}
return std::nullopt;
}

Game::PlayerId PlayerSelection::get_PlayerId() const noexcept {
#if 0//_DEBUG
LOG_ASSERT(this->has_value());
#endif
return this->playerId;
}

Game::ClientId PlayerSelection::get_ClientId() const noexcept {
#if 0//_DEBUG
LOG_ASSERT(this->has_value());
#endif
return this->clientId;
}

bool PlayerSelection::is_LocalPlayer() const noexcept {
#if 0//_DEBUG
LOG_ASSERT(this->has_value());
#endif
return this->clientId == (*Game::pAmongUsClient)->fields._.ClientId;
}
#pragma endregion

ImVec4 AmongUsColorToImVec4(const Color& color) {
Expand Down Expand Up @@ -608,10 +600,10 @@ std::string GetGitBranch()
return "unavailable";
}

void ImpersonateName(PlayerSelection& _player)
void ImpersonateName(__maybenull GameData_PlayerInfo* data)
{
auto player = _player.validate(); if (!player.has_value()) return;
app::GameData_PlayerOutfit* outfit = GetPlayerOutfit(player.get_PlayerData());
if (!data) return;
app::GameData_PlayerOutfit* outfit = GetPlayerOutfit(data);
if (!(IsInGame() || IsInLobby() || outfit)) return;
const auto& playerName = convert_from_string(GameData_PlayerOutfit_get_PlayerName(outfit, nullptr));
if (playerName.length() < 10) {
Expand Down
30 changes: 21 additions & 9 deletions user/utility.h
Original file line number Diff line number Diff line change
Expand Up @@ -84,13 +84,14 @@ class PlayerSelection {
#endif
return _playerData->fields.Disconnected;
}
constexpr bool equals(_Maybenull_ const PlayerControl* playerControl) const {
return _playerControl == playerControl;
constexpr bool equals(const Result& _Right) const noexcept {
if (!this->has_value() || !_Right.has_value()) return false;
return _playerControl == _Right._playerControl;
}
constexpr bool equals(_Maybenull_ const GameData_PlayerInfo* playerData) const {
return _playerData == playerData;
constexpr bool has_value() const noexcept {
return _has_value;
}
constexpr bool has_value() const {
constexpr operator bool() const noexcept {
return _has_value;
}
private:
Expand All @@ -116,13 +117,24 @@ class PlayerSelection {
PlayerSelection() noexcept;
explicit PlayerSelection(const PlayerControl* playerControl);
explicit PlayerSelection(GameData_PlayerInfo* playerData);
PlayerSelection(const PlayerSelection::Result& result);

PlayerSelection::Result validate();

bool equals(const PlayerSelection& selectedPlayer) const;
Game::PlayerId get_PlayerId() const noexcept;
Game::ClientId get_ClientId() const noexcept;
bool is_LocalPlayer() const noexcept;
bool equals(const PlayerSelection::Result& selectedPlayer) const;
Game::PlayerId get_PlayerId() const noexcept {
return this->playerId;
}
Game::ClientId get_ClientId() const noexcept {
return this->clientId;
}

constexpr void reset() noexcept {
this->clientId = Game::NoClientId;
this->playerId = Game::NoPlayerId;
}

bool has_value() const noexcept {
return (this->clientId != Game::NoClientId || this->playerId != Game::NoPlayerId);
}
Expand Down Expand Up @@ -181,7 +193,7 @@ std::string ToString(__maybenull PlayerControl* player);
std::string ToString(__maybenull GameData_PlayerInfo* data);
std::string GetGitCommit();
std::string GetGitBranch();
void ImpersonateName(PlayerSelection& player);
void ImpersonateName(__maybenull GameData_PlayerInfo* data);
Game::ColorId GetRandomColorId();
void SaveOriginalAppearance();
void ResetOriginalAppearance();
Expand Down

0 comments on commit 221832e

Please sign in to comment.