/
NetEventLog.cpp
106 lines (84 loc) · 2.35 KB
/
NetEventLog.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#include <StdInc.h>
#include <ResourceEventComponent.h>
#include <ResourceManager.h>
#include <NetLibrary.h>
#include <boost/circular_buffer.hpp>
#include <ConsoleHost.h>
#include <imgui.h>
#include <CoreConsole.h>
struct EventLogEntry
{
std::string eventName;
size_t payloadSize;
bool out;
};
static boost::circular_buffer<EventLogEntry> g_eventLog(150);
static std::mutex g_eventLogMutex;
static void EventLog_Add(const std::string& eventName, size_t payloadSize, bool out)
{
EventLogEntry eve;
eve.eventName = eventName;
eve.payloadSize = payloadSize;
eve.out = out;
std::unique_lock<std::mutex> lock(g_eventLogMutex);
g_eventLog.push_front(std::move(eve));
}
static InitFunction initFunction([]()
{
static bool eventLogEnabled;
static ConVar<bool> eventLogVar("netEventLog", ConVar_Archive, false, &eventLogEnabled);
ConHost::OnShouldDrawGui.Connect([](bool* should)
{
*should = *should || eventLogEnabled;
});
ConHost::OnDrawGui.Connect([]()
{
if (!eventLogEnabled)
{
return;
}
ImGui::SetNextWindowSize(ImVec2(500.0f, 300.0f), ImGuiCond_FirstUseEver);
if (ImGui::Begin("Network Event Log", &eventLogEnabled))
{
ImGui::Columns(3);
{
ImGui::Text("Dir");
ImGui::NextColumn();
ImGui::Text("Name");
ImGui::NextColumn();
ImGui::Text("Size");
ImGui::NextColumn();
std::unique_lock<std::mutex> lock(g_eventLogMutex);
for (auto& entry : g_eventLog)
{
ImGui::Text((entry.out) ? "C->S" : "S->C");
ImGui::NextColumn();
ImGui::Text("%s", entry.eventName.c_str());
ImGui::NextColumn();
ImGui::Text("%lld B", entry.payloadSize);
ImGui::NextColumn();
}
}
ImGui::Columns();
}
ImGui::End();
});
fx::ResourceManager::OnInitializeInstance.Connect([](fx::ResourceManager* manager)
{
auto eventManager = manager->GetComponent<fx::ResourceEventManagerComponent>();
eventManager->OnQueueEvent.Connect([](const std::string& eventName, const std::string& eventPayload, const std::string& eventSource)
{
if (eventSource.find("net:") == 0)
{
EventLog_Add(eventName, eventPayload.size(), false);
}
});
}, INT32_MAX);
NetLibrary::OnNetLibraryCreate.Connect([](NetLibrary* library)
{
library->OnTriggerServerEvent.Connect([](const std::string& eventName, const std::string& eventPayload)
{
EventLog_Add(eventName, eventPayload.size(), true);
});
});
});