From a39fdfc7319f89e499a9dc4dde9be8acf0efe376 Mon Sep 17 00:00:00 2001 From: KyleSanderson Date: Tue, 14 Jan 2014 17:54:32 -0700 Subject: [PATCH] Add and enable SteamWorks GS Detours (+CDetour). --- Extension/Makefile | 8 +++- Extension/extension.cpp | 2 + Extension/extension.h | 2 + Extension/swgsdetours.cpp | 81 +++++++++++++++++++++++++++++++++++++++ Extension/swgsdetours.h | 35 +++++++++++++++++ 5 files changed, 127 insertions(+), 1 deletion(-) create mode 100644 Extension/swgsdetours.cpp create mode 100644 Extension/swgsdetours.h diff --git a/Extension/Makefile b/Extension/Makefile index bdcbd62..517b68c 100644 --- a/Extension/Makefile +++ b/Extension/Makefile @@ -25,8 +25,13 @@ PROJECT = SteamWorks #Uncomment for Metamod: Source enabled extension #USEMETA = true USESH = true +USECD = true -OBJECTS = sdk/smsdk_ext.cpp extension.cpp swgameserver.cpp swgamedata.cpp swforwards.cpp gsnatives.cpp swgshooks.cpp ssnatives.cpp +OBJECTS = sdk/smsdk_ext.cpp extension.cpp swgameserver.cpp swgamedata.cpp swforwards.cpp gsnatives.cpp swgshooks.cpp swgsdetours.cpp ssnatives.cpp + +ifeq "$(USECD)" "true" + OBJECTS += CDetour/detours.cpp asm/asm.c +endif ############################################## ### CONFIGURE ANY OTHER FLAGS/OPTIONS HERE ### @@ -218,6 +223,7 @@ $(BIN_DIR)/%.o: %.cpp all: check mkdir -p $(BIN_DIR)/sdk + mkdir -p $(BIN_DIR)/CDetour if [ "$(USEMETA)" = "true" ]; then \ ln -sf $(HL2LIB)/$(LIB_PREFIX)vstdlib$(LIB_SUFFIX); \ ln -sf $(HL2LIB)/$(LIB_PREFIX)tier0$(LIB_SUFFIX); \ diff --git a/Extension/extension.cpp b/Extension/extension.cpp index e594e22..aca7e9b 100644 --- a/Extension/extension.cpp +++ b/Extension/extension.cpp @@ -48,6 +48,7 @@ bool SteamWorks::SDK_OnLoad(char *error, size_t maxlength, bool late) this->pSWForward = new SteamWorksForwards; this->pGSNatives = new SteamWorksGSNatives; this->pGSHooks = new SteamWorksGSHooks; + this->pGSDetours = new SteamWorksGSDetours; this->pSSNatives = new SteamWorksSSNatives; return true; } @@ -55,6 +56,7 @@ bool SteamWorks::SDK_OnLoad(char *error, size_t maxlength, bool late) void SteamWorks::SDK_OnUnload() { delete this->pSSNatives; + delete this->pGSDetours; delete this->pGSHooks; delete this->pGSNatives; delete this->pSWForward; diff --git a/Extension/extension.h b/Extension/extension.h index b2cb760..8c8d4ed 100644 --- a/Extension/extension.h +++ b/Extension/extension.h @@ -48,6 +48,7 @@ #include "gsnatives.h" #include "swgshooks.h" #include "ssnatives.h" +#include "swgsdetours.h" /** * @brief Sample implementation of the SDK Extension. @@ -136,6 +137,7 @@ public: SteamWorksGSNatives *pGSNatives; SteamWorksGSHooks *pGSHooks; SteamWorksSSNatives *pSSNatives; + SteamWorksGSDetours *pGSDetours; }; extern SteamWorks g_SteamWorks; diff --git a/Extension/swgsdetours.cpp b/Extension/swgsdetours.cpp new file mode 100644 index 0000000..1f72c73 --- /dev/null +++ b/Extension/swgsdetours.cpp @@ -0,0 +1,81 @@ +/* + 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 . + + Author: Kyle Sanderson (KyleS). +*/ + +#include "swgsdetours.h" + +DETOUR_DECL_STATIC0(SteamAPIShutdown, void) +{ + DETOUR_STATIC_CALL(SteamAPIShutdown)(); /* We're not a monster. */ + if (g_SteamWorks.pSWGameServer == NULL) + { + return; + } + + g_SteamWorks.pSWGameServer->Reset(); +} + +SteamWorksGSDetours::SteamWorksGSDetours() +{ +#if defined POSIX + const char *pLibSteamPath = "./bin/libsteam_api.so"; +#elif defined WIN32_LEAN_AND_MEAN + const char *pLibSteamPath = "./bin/steam_api.dll"; /* Naming from SteamTools. */ +#endif + + IGameConfig *pConfig = NULL; + if (g_SteamWorks.pSWGameData) + { + pConfig = g_SteamWorks.pSWGameData->GetGameData(); + if (pConfig) + { + pLibSteamPath = pConfig->GetKeyValue("LibSteamAPI"); + } + } + + ILibrary *pLibrary = libsys->OpenLibrary(pLibSteamPath, NULL, 0); + void *pAddress = NULL; + if (pLibrary != NULL) + { + const char *pFunctionName = "SteamGameServer_Shutdown"; + pAddress = pLibrary->GetSymbolAddress(pFunctionName); + pLibrary->CloseLibrary(); + } + + if (pAddress == NULL) + { + this->m_pShutdownDetour = NULL; + return; + } + + CDetourManager::Init(g_pSM->GetScriptingEngine(), pConfig); + this->m_pShutdownDetour = DETOUR_CREATE_STATIC_FIXED(SteamAPIShutdown, pAddress); + if (this->m_pShutdownDetour != NULL) + { + this->m_pShutdownDetour->EnableDetour(); + } +} + +SteamWorksGSDetours::~SteamWorksGSDetours() +{ + if (this->m_pShutdownDetour != NULL) + { + this->m_pShutdownDetour->Destroy(); + this->m_pShutdownDetour = NULL; + } +} diff --git a/Extension/swgsdetours.h b/Extension/swgsdetours.h new file mode 100644 index 0000000..678030b --- /dev/null +++ b/Extension/swgsdetours.h @@ -0,0 +1,35 @@ +/* + 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 . + + Author: Kyle Sanderson (KyleS). +*/ + +#pragma once +#include "smsdk_ext.h" +#include "steam_gameserver.h" +#include "CDetour/detours.h" + +class SteamWorksGSDetours +{ + public: + SteamWorksGSDetours(); + ~SteamWorksGSDetours(); + + private: + CDetour *m_pShutdownDetour; +}; + +#include "extension.h"