Skip to content

Commit 83325db

Browse files
committed
update(scripting): Adding OnClientDisconnect
1 parent 9a4bdf9 commit 83325db

File tree

5 files changed

+36
-3
lines changed

5 files changed

+36
-3
lines changed

plugin_files/scripting/includes/swiftly/swiftly.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ void OnPluginStop() __attribute__((weak));
5353
void OnProgramLoad(const char *pluginName, const char *mainFilePath);
5454
void OnClientConnected(Player *player) __attribute__((weak));
5555
bool OnClientConnect(Player *player) __attribute__((weak));
56+
void OnClientDisconnect(Player *player) __attribute__((weak));
5657
void OnPlayerSpawn(Player *player) __attribute__((weak));
5758
void OnGameTick(bool simulating, bool bFirstTick, bool bLastTick) __attribute__((weak));
5859
bool OnPlayerChat(Player *player, const char *text, bool teamonly) __attribute__((weak));
@@ -99,6 +100,17 @@ extern "C"
99100

100101
OnClientConnected(player);
101102
}
103+
void Internal_OnClientDisconnect(uint32_t slot)
104+
{
105+
if (!OnClientDisconnect)
106+
return;
107+
108+
Player *player = g_playerManager->GetPlayer(slot);
109+
if (player == nullptr)
110+
return;
111+
112+
OnClientDisconnect(player);
113+
}
102114
bool Internal_OnClientConnect(uint32_t slot)
103115
{
104116
if (!OnClientConnect)

src/components/Plugins/inc/Plugin.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ const std::string funcsToLoad[] = {
1919
"OnPluginStop",
2020
"OnClientConnected",
2121
"OnClientConnect",
22+
"OnClientDisconnect",
2223
"OnPlayerSpawn",
2324
"OnGameTick",
2425
"OnPlayerChat",

src/components/Plugins/inc/Scripting.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
typedef void (*Plugin_OnClientConnected)(uint32);
1313
typedef bool (*Plugin_OnClientConnect)(uint32);
14+
typedef void (*Plugin_OnClientDisconnect)(uint32);
1415
typedef void (*Plugin_OnPlayerSpawn)(uint32);
1516
typedef void (*Plugin_OnRoundPrestart)();
1617
typedef void (*Plugin_OnRoundStart)(long, long, const char *);

src/components/Plugins/src/scripting/GameEvents.cpp

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ void scripting_OnClientConnected(const OnClientConnected *e)
2323
reinterpret_cast<Plugin_OnClientConnected>(plugin_OnClientConnected)(e->slot->Get());
2424
}
2525
}
26-
};
26+
}
2727

2828
bool scripting_OnClientConnect(const OnClientConnect *e)
2929
{
@@ -40,7 +40,21 @@ bool scripting_OnClientConnect(const OnClientConnect *e)
4040
}
4141

4242
return true;
43-
};
43+
}
44+
45+
void scripting_OnClientDisconnec(const OnClientDisconnect *e)
46+
{
47+
for (uint32 i = 0; i < plugins.size(); i++)
48+
{
49+
Plugin *plugin = plugins[i];
50+
if (plugin->IsPluginLoaded())
51+
{
52+
void *plugin_OnClientDisconnect = plugin->FetchFunction("Internal_OnClientDisconnect");
53+
if (plugin_OnClientDisconnect)
54+
reinterpret_cast<Plugin_OnClientDisconnect>(plugin_OnClientDisconnect)(e->slot->Get());
55+
}
56+
}
57+
}
4458

4559
void scripting_OnClientSpawn(const OnPlayerSpawn *e)
4660
{

src/entrypoint.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -245,9 +245,14 @@ void SwiftlyPlugin::Hook_ClientPutInServer(CPlayerSlot slot, char const *pszName
245245
hooks::emit(OnClientPutInServer(&slot, pszName, type, xuid));
246246
}
247247

248+
void scripting_OnClientDisconnect(const OnClientDisconnect *e);
249+
248250
void SwiftlyPlugin::Hook_ClientDisconnect(CPlayerSlot slot, int reason, const char *pszName, uint64 xuid, const char *pszNetworkID)
249251
{
250-
hooks::emit(OnClientDisconnect(&slot, reason, pszName, xuid, pszNetworkID));
252+
OnClientDisconnect clientDisconnectEvent = OnClientDisconnect(&slot, reason, pszName, xuid, pszNetworkID);
253+
scripting_OnClientDisconnect(&clientDisconnectEvent);
254+
255+
hooks::emit(clientDisconnectEvent);
251256
}
252257

253258
void SwiftlyPlugin::Hook_GameFrame(bool simulating, bool bFirstTick, bool bLastTick)

0 commit comments

Comments
 (0)