Add Hook to WasRestartRequested, provide forward.

This commit is contained in:
KyleSanderson
2014-01-11 12:48:28 -07:00
parent 25209a093b
commit eafdc7f337
5 changed files with 142 additions and 1 deletions
+1 -1
View File
@@ -26,7 +26,7 @@ PROJECT = SteamWorks
#USEMETA = true #USEMETA = true
USESH = true USESH = true
OBJECTS = sdk/smsdk_ext.cpp extension.cpp swgameserver.cpp swgamedata.cpp swforwards.cpp gsnatives.cpp OBJECTS = sdk/smsdk_ext.cpp extension.cpp swgameserver.cpp swgamedata.cpp swforwards.cpp gsnatives.cpp swgshooks.cpp
############################################## ##############################################
### CONFIGURE ANY OTHER FLAGS/OPTIONS HERE ### ### CONFIGURE ANY OTHER FLAGS/OPTIONS HERE ###
+2
View File
@@ -47,11 +47,13 @@ bool SteamWorks::SDK_OnLoad(char *error, size_t maxlength, bool late)
this->pSWForward = new SteamWorksForwards; this->pSWForward = new SteamWorksForwards;
this->pGSNatives = new SteamWorksGSNatives; this->pGSNatives = new SteamWorksGSNatives;
this->pGSHooks = new SteamWorksGSHooks;
return true; return true;
} }
void SteamWorks::SDK_OnUnload() void SteamWorks::SDK_OnUnload()
{ {
delete this->pGSHooks;
delete this->pGSNatives; delete this->pGSNatives;
delete this->pSWForward; delete this->pSWForward;
+2
View File
@@ -46,6 +46,7 @@
#include "swforwards.h" #include "swforwards.h"
#include "gsnatives.h" #include "gsnatives.h"
#include "swgshooks.h"
/** /**
* @brief Sample implementation of the SDK Extension. * @brief Sample implementation of the SDK Extension.
@@ -129,6 +130,7 @@ public:
SteamWorksGameData *pSWGameData; SteamWorksGameData *pSWGameData;
SteamWorksGameServer *pSWGameServer; SteamWorksGameServer *pSWGameServer;
SteamWorksGSNatives *pGSNatives; SteamWorksGSNatives *pGSNatives;
SteamWorksGSHooks *pGSHooks;
}; };
extern SteamWorks g_SteamWorks; extern SteamWorks g_SteamWorks;
+91
View File
@@ -0,0 +1,91 @@
/*
This file is part of SourcePawn SteamWorks.
SourcePawn SteamWorks is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
SourcePawn SteamWorks is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with SourcePawn SteamWorks. If not, see <http://www.gnu.org/licenses/>.
Author: Kyle Sanderson (KyleS).
*/
#include "swgshooks.h"
SH_DECL_HOOK0(ISteamGameServer, WasRestartRequested, SH_NOATTRIB, 0, bool); /* From SteamTools. */
static ISteamGameServer *GetGameServerPointer()
{
return g_SteamWorks.pSWGameServer->GetGameServer();
}
SteamWorksGSHooks::SteamWorksGSHooks()
{
this->bHooked = false;
this->pFORR = forwards->CreateForward("SteamWorks_RestartRequested", ET_Hook, 0, NULL);
smutils->AddGameFrameHook(OurGameFrameHook);
}
SteamWorksGSHooks::~SteamWorksGSHooks()
{
smutils->RemoveGameFrameHook(OurGameFrameHook);
this->RemoveHooks(GetGameServerPointer());
forwards->ReleaseForward(this->pFORR);
}
bool SteamWorksGSHooks::WasRestartRequested(void) /* Mimic SteamTools. */
{
bool bWasRestartRequested = SH_CALL(GetGameServerPointer(), &ISteamGameServer::WasRestartRequested)();
if (bWasRestartRequested && this->pFORR->GetFunctionCount() != 0)
{
cell_t Result = Pl_Continue;
this->pFORR->Execute(&Result);
bWasRestartRequested = (Result >= Pl_Handled);
}
/* With how this function works, all following will be given poisoned values from SH_Call. */
RETURN_META_VALUE(MRES_SUPERCEDE, bWasRestartRequested);
}
void SteamWorksGSHooks::AddHooks(ISteamGameServer *pGameServer)
{
if (this->bHooked == true || pGameServer == NULL)
{
return;
}
this->bHooked = true;
SH_ADD_HOOK(ISteamGameServer, WasRestartRequested, pGameServer, SH_MEMBER(this, &SteamWorksGSHooks::WasRestartRequested), false);
}
void SteamWorksGSHooks::RemoveHooks(ISteamGameServer *pGameServer)
{
if (this->bHooked == false || pGameServer == NULL)
{
return;
}
this->bHooked = false;
SH_REMOVE_HOOK(ISteamGameServer, WasRestartRequested, pGameServer, SH_MEMBER(this, &SteamWorksGSHooks::WasRestartRequested), false);
}
void OurGameFrameHook(bool simulating) /* What we do for SDK independence. */
{
ISteamGameServer *pGameServer = GetGameServerPointer();
if (pGameServer == NULL)
{
return;
}
g_SteamWorks.pGSHooks->AddHooks(pGameServer);
smutils->RemoveGameFrameHook(OurGameFrameHook);
}
+46
View File
@@ -0,0 +1,46 @@
/*
This file is part of SourcePawn SteamWorks.
SourcePawn SteamWorks is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
SourcePawn SteamWorks is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with SourcePawn SteamWorks. If not, see <http://www.gnu.org/licenses/>.
Author: Kyle Sanderson (KyleS).
*/
#pragma once
#include "isteamgameserver.h"
#include "steam_gameserver.h"
#include "smsdk_ext.h"
#include "sourcehook.h"
class SteamWorksGSHooks
{
public:
SteamWorksGSHooks();
~SteamWorksGSHooks();
public:
void AddHooks(ISteamGameServer *pGameServer);
void RemoveHooks(ISteamGameServer *pGameServer);
public:
bool WasRestartRequested(void);
private:
IForward *pFORR; /* On Restart Requested. */
bool bHooked;
};
void OurGameFrameHook(bool simulating);
#include "extension.h"