Skip to content

Commit 654d8e8

Browse files
committed
Fixed crash issue and rcon chat being duplicated on originating server
1 parent 4f281b3 commit 654d8e8

9 files changed

Lines changed: 80 additions & 5 deletions

ArkCrossServerChat/ArkCrossServerChat.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include "Plugin.h"
55
#include "Hooks.h"
66
#include "DatabaseCleanup.h"
7+
#include "DebugTimer.h"
78
#include "MessageHandlers.h"
89

910
#pragma comment(lib, "ArkApi.lib")
@@ -159,6 +160,7 @@ void Load()
159160
}
160161

161162
plugin.NextCleanupTime = std::chrono::system_clock::now() + std::chrono::seconds(300); // init with cleanup in 5 min
163+
plugin.last_test_time = std::chrono::system_clock::now();
162164

163165
auto& hooks = ArkApi::GetHooks();
164166
auto& commands = ArkApi::GetCommands();
@@ -169,6 +171,8 @@ void Load()
169171

170172
commands.AddOnChatMessageCallback("ChatMessageCallback", &ChatMessageCallback);
171173
commands.AddOnTimerCallback("CleanupTimer", &CleanupTimer);
174+
if (plugin.json.value("Debug", false) == true) commands.AddOnTimerCallback("DebugTimer", &DebugTimer);
175+
commands.AddOnTickCallback("MessageTimer", &MessageTimer);
172176
}
173177

174178
void Unload()
@@ -182,6 +186,8 @@ void Unload()
182186

183187
commands.RemoveOnChatMessageCallback("ChatMessageCallback");
184188
commands.RemoveOnTimerCallback("CleanupTimer");
189+
if (plugin.json.value("Debug", false) == true) commands.RemoveOnTimerCallback("DebugTimer");
190+
commands.RemoveOnTickCallback("MessageTimer");
185191

186192
SetEvent(plugin.handle_mre_thread);
187193

ArkCrossServerChat/ArkCrossServerChat.vcxproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@
117117
</ItemDefinitionGroup>
118118
<ItemGroup>
119119
<ClInclude Include="DatabaseCleanup.h" />
120+
<ClInclude Include="DebugTimer.h" />
120121
<ClInclude Include="Hooks.h" />
121122
<ClInclude Include="MessageHandlers.h" />
122123
<ClInclude Include="Plugin.h" />
@@ -125,6 +126,7 @@
125126
<ItemGroup>
126127
<ClCompile Include="ArkCrossServerChat.cpp" />
127128
<ClCompile Include="DatabaseCleanup.cpp" />
129+
<ClCompile Include="DebugTimer.cpp" />
128130
<ClCompile Include="Hooks.cpp" />
129131
<ClCompile Include="MessageHandlers.cpp" />
130132
<ClCompile Include="Plugin.cpp" />

ArkCrossServerChat/ArkCrossServerChat.vcxproj.filters

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@
3030
<ClInclude Include="Plugin.h">
3131
<Filter>Header Files</Filter>
3232
</ClInclude>
33+
<ClInclude Include="DebugTimer.h">
34+
<Filter>Header Files</Filter>
35+
</ClInclude>
3336
</ItemGroup>
3437
<ItemGroup>
3538
<ClCompile Include="ArkCrossServerChat.cpp">
@@ -50,5 +53,8 @@
5053
<ClCompile Include="Plugin.cpp">
5154
<Filter>Source Files</Filter>
5255
</ClCompile>
56+
<ClCompile Include="DebugTimer.cpp">
57+
<Filter>Source Files</Filter>
58+
</ClCompile>
5359
</ItemGroup>
5460
</Project>

ArkCrossServerChat/DebugTimer.cpp

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#include "DatabaseCleanup.h"
2+
3+
std::string get_current_time()
4+
{
5+
auto now = time(NULL);
6+
struct tm tstruct;
7+
char buffer[40];
8+
tstruct = *localtime(&now);
9+
strftime(buffer, sizeof(buffer), "%X", &tstruct);
10+
11+
return buffer;
12+
}
13+
14+
void DebugTimer()
15+
{
16+
auto& plugin = Plugin::Get();
17+
18+
auto now = std::chrono::system_clock::now();
19+
auto diff = std::chrono::duration_cast<std::chrono::milliseconds>(now - plugin.last_test_time);
20+
21+
// send a test message every two seconds
22+
if (diff.count() >= 2000)
23+
{
24+
plugin.last_test_time = now;
25+
26+
auto world = ArkApi::GetApiUtils().GetWorld();
27+
if (!world) return;
28+
29+
auto playerController = world->GetFirstPlayerController();
30+
if (!playerController) return;
31+
32+
auto aShooterPC = static_cast<AShooterPlayerController*>(playerController);
33+
auto when = get_current_time();
34+
35+
Log::GetLog()->info("Sending test message ({})", when);
36+
aShooterPC->ServerSendChatMessage(&FString::Format("{}", when), EChatSendMode::GlobalChat);
37+
}
38+
}

ArkCrossServerChat/DebugTimer.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#pragma once
2+
#include "Plugin.h"
3+
4+
void DebugTimer();

ArkCrossServerChat/MessageHandlers.cpp

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include "MessageHandlers.h"
22

3+
// thread waiting for message event signaling
34
DWORD WINAPI WaitForNewMessagesThreadProc(LPVOID lpParam)
45
{
56
UNREFERENCED_PARAMETER(lpParam);
@@ -16,7 +17,8 @@ DWORD WINAPI WaitForNewMessagesThreadProc(LPVOID lpParam)
1617
case WAIT_OBJECT_0 + 0:
1718
if (ArkApi::GetApiUtils().GetWorld())
1819
{
19-
OnNewMessagesFromDatabase();
20+
std::lock_guard<std::mutex> lock(plugin.new_message_available_mutex);
21+
plugin.new_message_available = true;
2022
}
2123
break;
2224
case WAIT_OBJECT_0 + 1:
@@ -34,6 +36,19 @@ DWORD WINAPI WaitForNewMessagesThreadProc(LPVOID lpParam)
3436
return 0;
3537
}
3638

39+
// check for new messages on game tick
40+
void MessageTimer(float delta)
41+
{
42+
auto &plugin = Plugin::Get();
43+
if (plugin.new_message_available)
44+
{
45+
std::lock_guard<std::mutex> lock(plugin.new_message_available_mutex);
46+
plugin.new_message_available = false;
47+
48+
OnNewMessagesFromDatabase();
49+
}
50+
}
51+
3752
// when the database has been updated with new messages
3853
void OnNewMessagesFromDatabase()
3954
{
@@ -82,14 +97,14 @@ void HandleMessageFromDatabase(
8297
int icon)
8398
{
8499
auto &plugin = Plugin::Get();
100+
auto isLocal = serverKey.compare(plugin.serverKey) == 0;
101+
85102
//send chat message to users
86103
if (rcon == 0)
87104
{
88105
auto chatIcon = static_cast<ChatIcon>(icon);
89106
UTexture2D *iconTexture = nullptr;
90107

91-
auto isLocal = serverKey.compare(plugin.serverKey) == 0;
92-
93108
// get chat icon
94109
if (chatIcon == ChatIcon::Admin)
95110
{
@@ -132,7 +147,7 @@ void HandleMessageFromDatabase(
132147
FromUTF16(message).c_str(),
133148
iconTexture);
134149
}
135-
else
150+
else if (!isLocal)
136151
{
137152
SendRconChatMessageToAll(FromUTF16(message));
138153
}

ArkCrossServerChat/MessageHandlers.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#include "Plugin.h"
33

44
DWORD WINAPI WaitForNewMessagesThreadProc(LPVOID lpParam);
5+
void MessageTimer(float delta);
56
void OnNewMessagesFromDatabase();
67
void HandleMessageFromDatabase(
78
long long id,

ArkCrossServerChat/Plugin.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ class Plugin
1717
std::wstring mre_cluster_name;
1818
std::chrono::time_point<std::chrono::system_clock> NextCleanupTime;
1919
long long lastRowId = 0;
20+
bool new_message_available;
21+
std::mutex new_message_available_mutex;
22+
std::chrono::time_point<std::chrono::system_clock> last_test_time;
2023

2124
static Plugin& Get();
2225

Configs/PluginInfo.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"FullName":"ARK Cross Server Chat",
33
"Description": "",
4-
"Version":2.01,
4+
"Version":2.02,
55
"MinApiVersion":2.2
66
}

0 commit comments

Comments
 (0)