Skip to content

Commit

Permalink
Minor improvements (#378)
Browse files Browse the repository at this point in the history
* [Syntax] Added synchronized macros

* [Syntax] Dynamically build event_filter based on EVENT_TYPES

* [Optimization] Removed State.rawEvents

* [Optimization] Replaced liveReplayEvents.push_back with emplace_back

improve performance

* [Optimization] Avoid unnecessary copies by using const lvalue references

* [Bugfix] Player position may not be up to date in replay live mode

* [Feature] Failed murder animations can be shown when "See Protections" is on
  • Loading branch information
cddjr committed Jun 8, 2022
1 parent c58d8e6 commit a5d802c
Show file tree
Hide file tree
Showing 31 changed files with 356 additions and 330 deletions.
3 changes: 2 additions & 1 deletion appdata/il2cpp-functions.h
Original file line number Diff line number Diff line change
Expand Up @@ -210,4 +210,5 @@ DO_APP_FUNC(bool, SaveManager_GetPurchase, (String* itemKey, String* bundleKey,
DO_APP_FUNC(void, PlayerControl_TurnOnProtection, (PlayerControl* __this, bool visible, int32_t colorId, MethodInfo* method), "Assembly-CSharp, System.Void PlayerControl::TurnOnProtection(System.Boolean, System.Int32)");
DO_APP_FUNC(void, PlayerControl_RemoveProtection, (PlayerControl* __this, MethodInfo* method), "Assembly-CSharp, System.Void PlayerControl::RemoveProtection()");

DO_APP_FUNC(bool, Object_1_op_Implicit, (Object_1* exists, MethodInfo* method), "UnityEngine.CoreModule, System.Boolean UnityEngine.Object::op_Implicit(UnityEngine.Object)");
DO_APP_FUNC(bool, Object_1_op_Implicit, (Object_1* exists, MethodInfo* method), "UnityEngine.CoreModule, System.Boolean UnityEngine.Object::op_Implicit(UnityEngine.Object)");
DO_APP_FUNC(void, PlayerControl_ShowFailedMurder, (PlayerControl* __this, MethodInfo* method), "Assembly-CSharp, System.Void PlayerControl::ShowFailedMurder()");
2 changes: 1 addition & 1 deletion events/CastVoteEvent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#include "_events.h"
#include "utility.h"

CastVoteEvent::CastVoteEvent(EVENT_PLAYER source, std::optional<EVENT_PLAYER> target) : EventInterface(source, EVENT_TYPES::EVENT_VOTE) {
CastVoteEvent::CastVoteEvent(const EVENT_PLAYER& source, const std::optional<EVENT_PLAYER>& target) : EventInterface(source, EVENT_TYPES::EVENT_VOTE) {
this->target = target;
}

Expand Down
2 changes: 1 addition & 1 deletion events/CheatDetectedEvent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#include "_events.h"
#include "utility.h"

CheatDetectedEvent::CheatDetectedEvent(EVENT_PLAYER source, CHEAT_ACTIONS action) : EventInterface(source, EVENT_TYPES::EVENT_CHEAT) {
CheatDetectedEvent::CheatDetectedEvent(const EVENT_PLAYER& source, CHEAT_ACTIONS action) : EventInterface(source, EVENT_TYPES::EVENT_CHEAT) {
this->action = action;
}

Expand Down
2 changes: 1 addition & 1 deletion events/DisconnectEvent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#include "_events.h"
#include "utility.h"

DisconnectEvent::DisconnectEvent(EVENT_PLAYER source) : EventInterface(source, EVENT_TYPES::EVENT_DISCONNECT) { }
DisconnectEvent::DisconnectEvent(const EVENT_PLAYER& source) : EventInterface(source, EVENT_TYPES::EVENT_DISCONNECT) { }

void DisconnectEvent::Output() {
ImGui::TextColored(AmongUsColorToImVec4(GetPlayerColor(source.colorId)), source.playerName.c_str());
Expand Down
10 changes: 9 additions & 1 deletion events/KillEvent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#include "_events.h"
#include "utility.h"

KillEvent::KillEvent(EVENT_PLAYER source, EVENT_PLAYER target, Vector2 position, Vector2 targetPosition) : EventInterface(source, EVENT_TYPES::EVENT_KILL) {
KillEvent::KillEvent(const EVENT_PLAYER& source, const EVENT_PLAYER& target, const Vector2& position, const Vector2& targetPosition) : EventInterface(source, EVENT_TYPES::EVENT_KILL) {
this->target = target;
this->targetPosition = targetPosition;
this->position = position;
Expand All @@ -17,6 +17,14 @@ void KillEvent::Output() {
ImGui::TextColored(AmongUsColorToImVec4(GetPlayerColor(target.colorId)), target.playerName.c_str());
ImGui::SameLine();
ImGui::Text("(%s)", TranslateSystemTypes(systemType));
if (target.isProtected) {
ImGui::SameLine();
ImGui::Text("[");
ImGui::SameLine(0.f, 0.f);
ImGui::TextColored(ImVec4(0.1f, 0.75f, 0.75f, 1.f), "Protected");
ImGui::SameLine(0.f, 0.f);
ImGui::Text("]");
}
ImGui::SameLine();
ImGui::Text("[%s ago]", std::format("{:%OM:%OS}", (std::chrono::system_clock::now() - this->timestamp)).c_str());
}
Expand Down
2 changes: 1 addition & 1 deletion events/ProtectPlayerEvent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#include "_events.h"
#include "utility.h"

ProtectPlayerEvent::ProtectPlayerEvent(EVENT_PLAYER source, EVENT_PLAYER target) : EventInterface(source, EVENT_TYPES::EVENT_PROTECTPLAYER) {
ProtectPlayerEvent::ProtectPlayerEvent(const EVENT_PLAYER& source, const EVENT_PLAYER& target) : EventInterface(source, EVENT_TYPES::EVENT_PROTECTPLAYER) {
this->target = target;
}

Expand Down
2 changes: 1 addition & 1 deletion events/ReportDeadBodyEvent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#include "_events.h"
#include "utility.h"

ReportDeadBodyEvent::ReportDeadBodyEvent(EVENT_PLAYER source, std::optional<EVENT_PLAYER> target, Vector2 position, std::optional<Vector2> targetPosition)
ReportDeadBodyEvent::ReportDeadBodyEvent(const EVENT_PLAYER& source, const std::optional<EVENT_PLAYER>& target, const Vector2& position, const std::optional<Vector2>& targetPosition)
: EventInterface(source, (target.has_value() ? EVENT_TYPES::EVENT_REPORT : EVENT_TYPES::EVENT_MEETING)) {
this->target = target;
this->position = position;
Expand Down
2 changes: 1 addition & 1 deletion events/ShapeShiftEvent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#include "_events.h"
#include "utility.h"

ShapeShiftEvent::ShapeShiftEvent(EVENT_PLAYER source, EVENT_PLAYER target) : EventInterface(source, EVENT_TYPES::EVENT_SHAPESHIFT) {
ShapeShiftEvent::ShapeShiftEvent(const EVENT_PLAYER& source, const EVENT_PLAYER& target) : EventInterface(source, EVENT_TYPES::EVENT_SHAPESHIFT) {
this->target = target;
}

Expand Down
2 changes: 1 addition & 1 deletion events/TaskCompletedEvent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#include "_events.h"
#include "utility.h"

TaskCompletedEvent::TaskCompletedEvent(EVENT_PLAYER source, std::optional<TaskTypes__Enum> taskType, Vector2 position) : EventInterface(source, EVENT_TYPES::EVENT_TASK) {
TaskCompletedEvent::TaskCompletedEvent(const EVENT_PLAYER& source, const std::optional<TaskTypes__Enum>& taskType, const Vector2& position) : EventInterface(source, EVENT_TYPES::EVENT_TASK) {
this->taskType = taskType;
this->position = position;
this->systemType = GetSystemTypes(position);
Expand Down
2 changes: 1 addition & 1 deletion events/VentEvent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#include "_events.h"
#include "utility.h"

VentEvent::VentEvent(EVENT_PLAYER source, Vector2 position, VENT_ACTIONS action) : EventInterface(source, EVENT_TYPES::EVENT_VENT)
VentEvent::VentEvent(const EVENT_PLAYER& source, const Vector2& position, VENT_ACTIONS action) : EventInterface(source, EVENT_TYPES::EVENT_VENT)
{
this->position = position;
this->systemType = GetSystemTypes(position);
Expand Down
2 changes: 1 addition & 1 deletion events/WalkEvent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#include "_events.h"
#include "utility.h"

WalkEvent::WalkEvent(EVENT_PLAYER source, Vector2 position) : EventInterface(source, EVENT_TYPES::EVENT_WALK) {
WalkEvent::WalkEvent(const EVENT_PLAYER& source, const Vector2& position) : EventInterface(source, EVENT_TYPES::EVENT_WALK) {
this->position = position;
}

Expand Down
70 changes: 34 additions & 36 deletions events/_events.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,24 @@

using namespace app;

#define EVENT_TYPES_SIZE 11
#define ALL_EVENTS \
ADD_EVENT (KILL, "Kill"), \
ADD_EVENT (VENT, "Vent"), \
ADD_EVENT (TASK, "Task"), \
ADD_EVENT (REPORT, "Report"), \
ADD_EVENT (MEETING, "Meeting"), \
ADD_EVENT (VOTE, "Vote"), \
ADD_EVENT (CHEAT, "Cheat"), \
ADD_EVENT (DISCONNECT, "Disconnect"), \
ADD_EVENT (SHAPESHIFT, "Shapeshift"), \
ADD_EVENT (PROTECTPLAYER, "Protect"), \
ADD_EVENT (WALK, "Walk")

enum class EVENT_TYPES {
EVENT_KILL,
EVENT_VENT,
EVENT_TASK,
EVENT_REPORT,
EVENT_MEETING,
EVENT_VOTE,
EVENT_CHEAT,
EVENT_DISCONNECT,
EVENT_SHAPESHIFT,
EVENT_PROTECTPLAYER,
EVENT_WALK
#define ADD_EVENT(name, desc) EVENT_ ## name
ALL_EVENTS,
#undef ADD_EVENT
EVENT_TYPES_SIZE
};

enum class VENT_ACTIONS {
Expand All @@ -40,29 +44,22 @@ struct EVENT_PLAYER {
std::string playerName;
bool isDead;
bool isAngel;
bool isProtected;

EVENT_PLAYER() {
playerId = 0;
colorId = 0;
playerName = "";
isDead = false;
isAngel = false;
isProtected = false;
}

EVENT_PLAYER(GameData_PlayerInfo* playerInfo) {
playerId = playerInfo->fields.PlayerId;

// rolling GetPlayerOutfit into this func to avoid circular dependencies
GameData_PlayerOutfit* outfit = nullptr;
for (auto& kvp : il2cpp::Dictionary(playerInfo->fields.Outfits))
{
if (kvp.key == PlayerOutfitType__Enum::Default)
{
outfit = kvp.value;
break;
}
}

GameData_PlayerOutfit* outfit = il2cpp::Dictionary(playerInfo->fields.Outfits)[PlayerOutfitType__Enum::Default];
if (outfit != nullptr)
{
colorId = outfit->fields.ColorId;
Expand All @@ -76,6 +73,7 @@ struct EVENT_PLAYER {

isDead = playerInfo->fields.IsDead;
isAngel = (playerInfo->fields.Role) ? playerInfo->fields.Role->fields.Role == RoleTypes__Enum::GuardianAngel : false;
isProtected = playerInfo->fields._object ? playerInfo->fields._object->fields.protectedByGuardian : false;
}
};

Expand All @@ -85,17 +83,17 @@ class EventInterface {
EVENT_PLAYER source;
std::chrono::system_clock::time_point timestamp;
public:
EventInterface(EVENT_PLAYER source, EVENT_TYPES eventType) {
EventInterface(const EVENT_PLAYER& source, EVENT_TYPES eventType) {
this->source = source;
this->eventType = eventType;
this->timestamp = std::chrono::system_clock::now();
}
virtual ~EventInterface() {}
virtual void Output() = 0;
virtual void ColoredEventOutput() = 0;
EVENT_TYPES getType() { return this->eventType; }
EVENT_PLAYER getSource() { return this->source; }
std::chrono::system_clock::time_point GetTimeStamp() { return this->timestamp; }
EVENT_TYPES getType() const { return this->eventType; }
const EVENT_PLAYER& getSource() const { return this->source; }
const std::chrono::system_clock::time_point& GetTimeStamp() const { return this->timestamp; }
};

class KillEvent : public EventInterface {
Expand All @@ -105,7 +103,7 @@ class KillEvent : public EventInterface {
Vector2 targetPosition;
SystemTypes__Enum systemType;
public:
KillEvent(EVENT_PLAYER source, EVENT_PLAYER target, Vector2 position, Vector2 targetPosition);
KillEvent(const EVENT_PLAYER& source, const EVENT_PLAYER& target, const Vector2& position, const Vector2& targetPosition);
virtual void Output() override;
virtual void ColoredEventOutput() override;
std::optional<EVENT_PLAYER> GetTarget() { return this->target; }
Expand All @@ -120,7 +118,7 @@ class VentEvent : public EventInterface {
SystemTypes__Enum systemType;
VENT_ACTIONS action;
public:
VentEvent(EVENT_PLAYER source, Vector2 position, VENT_ACTIONS action);
VentEvent(const EVENT_PLAYER& source, const Vector2& position, VENT_ACTIONS action);
virtual void Output() override;
virtual void ColoredEventOutput() override;
Vector2 GetPosition() { return this->position; }
Expand Down Expand Up @@ -149,7 +147,7 @@ class TaskCompletedEvent : public EventInterface {
Vector2 position;
SystemTypes__Enum systemType;
public:
TaskCompletedEvent(EVENT_PLAYER source, std::optional<TaskTypes__Enum> taskType, Vector2 position);
TaskCompletedEvent(const EVENT_PLAYER& source, const std::optional<TaskTypes__Enum>& taskType, const Vector2& position);
virtual void Output() override;
virtual void ColoredEventOutput() override;
std::optional<TaskTypes__Enum> GetTaskType() { return this->taskType; }
Expand All @@ -165,7 +163,7 @@ class ReportDeadBodyEvent : public EventInterface {
std::optional<Vector2> targetPosition;
SystemTypes__Enum systemType;
public:
ReportDeadBodyEvent(EVENT_PLAYER source, std::optional<EVENT_PLAYER> target, Vector2 position, std::optional<Vector2> targetPosition);
ReportDeadBodyEvent(const EVENT_PLAYER& source, const std::optional<EVENT_PLAYER>& target, const Vector2& position, const std::optional<Vector2>& targetPosition);
virtual void Output() override;
virtual void ColoredEventOutput() override;
std::optional<EVENT_PLAYER> GetTarget() { return this->target; }
Expand All @@ -178,7 +176,7 @@ class CastVoteEvent : public EventInterface {
private:
std::optional<EVENT_PLAYER> target;
public:
CastVoteEvent(EVENT_PLAYER source, std::optional<EVENT_PLAYER> target);
CastVoteEvent(const EVENT_PLAYER& source, const std::optional<EVENT_PLAYER>& target);
virtual void Output() override;
virtual void ColoredEventOutput() override;
std::optional<EVENT_PLAYER> GetTarget() { return this->target; }
Expand All @@ -188,15 +186,15 @@ class CheatDetectedEvent : public EventInterface {
private:
CHEAT_ACTIONS action;
public:
CheatDetectedEvent(EVENT_PLAYER source, CHEAT_ACTIONS action);
CheatDetectedEvent(const EVENT_PLAYER& source, CHEAT_ACTIONS action);
virtual void Output() override;
virtual void ColoredEventOutput() override;
CHEAT_ACTIONS GetCheatAction() { return this->action; }
};

class DisconnectEvent : public EventInterface {
public:
DisconnectEvent(EVENT_PLAYER source);
DisconnectEvent(const EVENT_PLAYER& source);
virtual void Output() override;
virtual void ColoredEventOutput() override;
};
Expand All @@ -205,7 +203,7 @@ class ShapeShiftEvent : public EventInterface {
private:
EVENT_PLAYER target;
public:
ShapeShiftEvent(EVENT_PLAYER source, EVENT_PLAYER target);
ShapeShiftEvent(const EVENT_PLAYER& source, const EVENT_PLAYER& target);
virtual void Output() override;
virtual void ColoredEventOutput() override;
EVENT_PLAYER GetTarget() { return this->target; }
Expand All @@ -215,7 +213,7 @@ class ProtectPlayerEvent : public EventInterface {
private:
EVENT_PLAYER target;
public:
ProtectPlayerEvent(EVENT_PLAYER source, EVENT_PLAYER target);
ProtectPlayerEvent(const EVENT_PLAYER& source, const EVENT_PLAYER& target);
virtual void Output() override;
virtual void ColoredEventOutput() override;
EVENT_PLAYER GetTarget() { return this->target; }
Expand All @@ -225,7 +223,7 @@ class WalkEvent : public EventInterface {
private:
Vector2 position;
public:
WalkEvent(EVENT_PLAYER source, Vector2 position);
WalkEvent(const EVENT_PLAYER& source, const Vector2& position);
virtual void Output() override;
virtual void ColoredEventOutput() override;
Vector2 GetPosition() { return this->position; }
Expand Down
31 changes: 14 additions & 17 deletions gui/console.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,14 @@

namespace ConsoleGui
{
// TODO: improve this by building it dynamically based on the EVENT_TYPES enum
std::vector<std::pair<const char*, bool>> event_filter =
{
{"Kill", false},
{"Vent", false},
{"Task", false},
{"Report", false},
{"Meeting", false},
{"Vote", false},
{"Cheat", false},
{"Disconnect", false},
{"Shapeshift", false},
{"Protect", false}
#define ADD_EVENT(name, desc) {desc, false}
ALL_EVENTS
#undef ADD_EVENT
};

std::vector<std::pair<PlayerSelection, bool>> player_filter;
std::array<std::pair<PlayerSelection, bool>, MAX_PLAYERS> player_filter;

bool init = false;
void Init() {
Expand All @@ -31,9 +23,13 @@ namespace ConsoleGui

if (!init)
{
// setup player_filter list based on MAX_PLAYERS definition
for (int i = 0; i < MAX_PLAYERS; i++) {
ConsoleGui::player_filter.push_back({ PlayerSelection(), false });
for (auto it = event_filter.begin(); it != event_filter.end(); it++) {
// Exclude the following events
switch (static_cast<EVENT_TYPES>(it - event_filter.begin())) {
case EVENT_TYPES::EVENT_WALK:
it->first = "";
break;
}
}
init = true;
}
Expand Down Expand Up @@ -73,8 +69,8 @@ namespace ConsoleGui
}
}

std::lock_guard<std::mutex> replayLock(Replay::replayEventMutex);
size_t i = State.liveReplayEvents.size() - 1;
synchronized(Replay::replayEventMutex) {
size_t i = State.liveReplayEvents.size() - 1;
for (auto rit = State.liveReplayEvents.rbegin(); rit != State.liveReplayEvents.rend(); ++rit, --i) {
EventInterface* evt = (*rit).get();
if (evt == NULL)
Expand All @@ -101,6 +97,7 @@ namespace ConsoleGui
ImGui::SameLine();
evt->Output();
}
}
ImGui::EndChild();
ImGui::End();
}
Expand Down

0 comments on commit a5d802c

Please sign in to comment.