Skip to content

Commit

Permalink
update(scripting): Adding OnClientDisconnect
Browse files Browse the repository at this point in the history
  • Loading branch information
skuzzis committed Dec 1, 2023
1 parent 9a4bdf9 commit 83325db
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 3 deletions.
12 changes: 12 additions & 0 deletions plugin_files/scripting/includes/swiftly/swiftly.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ void OnPluginStop() __attribute__((weak));
void OnProgramLoad(const char *pluginName, const char *mainFilePath);
void OnClientConnected(Player *player) __attribute__((weak));
bool OnClientConnect(Player *player) __attribute__((weak));
void OnClientDisconnect(Player *player) __attribute__((weak));
void OnPlayerSpawn(Player *player) __attribute__((weak));
void OnGameTick(bool simulating, bool bFirstTick, bool bLastTick) __attribute__((weak));
bool OnPlayerChat(Player *player, const char *text, bool teamonly) __attribute__((weak));
Expand Down Expand Up @@ -99,6 +100,17 @@ extern "C"

OnClientConnected(player);
}
void Internal_OnClientDisconnect(uint32_t slot)
{
if (!OnClientDisconnect)
return;

Player *player = g_playerManager->GetPlayer(slot);
if (player == nullptr)
return;

OnClientDisconnect(player);
}
bool Internal_OnClientConnect(uint32_t slot)
{
if (!OnClientConnect)
Expand Down
1 change: 1 addition & 0 deletions src/components/Plugins/inc/Plugin.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ const std::string funcsToLoad[] = {
"OnPluginStop",
"OnClientConnected",
"OnClientConnect",
"OnClientDisconnect",
"OnPlayerSpawn",
"OnGameTick",
"OnPlayerChat",
Expand Down
1 change: 1 addition & 0 deletions src/components/Plugins/inc/Scripting.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

typedef void (*Plugin_OnClientConnected)(uint32);
typedef bool (*Plugin_OnClientConnect)(uint32);
typedef void (*Plugin_OnClientDisconnect)(uint32);
typedef void (*Plugin_OnPlayerSpawn)(uint32);
typedef void (*Plugin_OnRoundPrestart)();
typedef void (*Plugin_OnRoundStart)(long, long, const char *);
Expand Down
18 changes: 16 additions & 2 deletions src/components/Plugins/src/scripting/GameEvents.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ void scripting_OnClientConnected(const OnClientConnected *e)
reinterpret_cast<Plugin_OnClientConnected>(plugin_OnClientConnected)(e->slot->Get());
}
}
};
}

bool scripting_OnClientConnect(const OnClientConnect *e)
{
Expand All @@ -40,7 +40,21 @@ bool scripting_OnClientConnect(const OnClientConnect *e)
}

return true;
};
}

void scripting_OnClientDisconnec(const OnClientDisconnect *e)
{
for (uint32 i = 0; i < plugins.size(); i++)
{
Plugin *plugin = plugins[i];
if (plugin->IsPluginLoaded())
{
void *plugin_OnClientDisconnect = plugin->FetchFunction("Internal_OnClientDisconnect");
if (plugin_OnClientDisconnect)
reinterpret_cast<Plugin_OnClientDisconnect>(plugin_OnClientDisconnect)(e->slot->Get());
}
}
}

void scripting_OnClientSpawn(const OnPlayerSpawn *e)
{
Expand Down
7 changes: 6 additions & 1 deletion src/entrypoint.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -245,9 +245,14 @@ void SwiftlyPlugin::Hook_ClientPutInServer(CPlayerSlot slot, char const *pszName
hooks::emit(OnClientPutInServer(&slot, pszName, type, xuid));
}

void scripting_OnClientDisconnect(const OnClientDisconnect *e);

void SwiftlyPlugin::Hook_ClientDisconnect(CPlayerSlot slot, int reason, const char *pszName, uint64 xuid, const char *pszNetworkID)
{
hooks::emit(OnClientDisconnect(&slot, reason, pszName, xuid, pszNetworkID));
OnClientDisconnect clientDisconnectEvent = OnClientDisconnect(&slot, reason, pszName, xuid, pszNetworkID);
scripting_OnClientDisconnect(&clientDisconnectEvent);

hooks::emit(clientDisconnectEvent);
}

void SwiftlyPlugin::Hook_GameFrame(bool simulating, bool bFirstTick, bool bLastTick)
Expand Down

0 comments on commit 83325db

Please sign in to comment.