From 0ffed7f5e92118406c0716df448c42270463f0fa Mon Sep 17 00:00:00 2001 From: Nick Whitlock Date: Mon, 17 Dec 2018 11:53:54 -0500 Subject: [PATCH] Steam Integration. Only works on Windows. If com_steamIntegration is 1 on startup (maybe it can default to 1?), the game will look for a steam_api.dll and steam_appid.txt and attempt to initialize the Steam API. With an extra download of these two files, one can have the Steam Overlay and hours tracking without launching OpenJK through Steam itself. --- code/qcommon/common.cpp | 4 ++ codemp/qcommon/common.cpp | 4 ++ shared/sys/sys_local.h | 4 ++ shared/sys/sys_unix.cpp | 22 ++++++++++ shared/sys/sys_win32.cpp | 85 +++++++++++++++++++++++++++++++++++++++ 5 files changed, 119 insertions(+) diff --git a/code/qcommon/common.cpp b/code/qcommon/common.cpp index 7fe6adcbf7..2d92e3b61f 100644 --- a/code/qcommon/common.cpp +++ b/code/qcommon/common.cpp @@ -1077,6 +1077,8 @@ void Com_Init( char *commandLine ) { FS_InitFilesystem (); //uses z_malloc //re.R_InitWorldEffects(); // this doesn't do much but I want to be sure certain variables are intialized. + Sys_SteamInit(); + Com_ExecuteCfg(); // override anything from the config files with command line args @@ -1580,6 +1582,8 @@ void Com_Shutdown (void) { com_journalFile = 0; } + Sys_SteamShutdown(); + #ifdef JK2_MODE JK2SP_Shutdown(); #else diff --git a/codemp/qcommon/common.cpp b/codemp/qcommon/common.cpp index 52c14464f8..4386e9be7d 100644 --- a/codemp/qcommon/common.cpp +++ b/codemp/qcommon/common.cpp @@ -1167,6 +1167,8 @@ void Com_Init( char *commandLine ) { FS_InitFilesystem (); + Sys_SteamInit(); + Com_InitJournaling(); // Add some commands here already so users can use them from config files @@ -1676,6 +1678,8 @@ void Com_Shutdown (void) com_journalFile = 0; } + Sys_SteamShutdown(); + MSG_shutdownHuffman(); /* // Only used for testing changes to huffman frequency table when tuning. diff --git a/shared/sys/sys_local.h b/shared/sys/sys_local.h index 815adc5eff..7839e93979 100644 --- a/shared/sys/sys_local.h +++ b/shared/sys/sys_local.h @@ -38,6 +38,10 @@ void Sys_SigHandler( int signal ); void Sys_AnsiColorPrint( const char *msg ); #endif +// Steam integration +void Sys_SteamInit(); +void Sys_SteamShutdown(); + struct UnpackDLLResult { bool succeeded; diff --git a/shared/sys/sys_unix.cpp b/shared/sys/sys_unix.cpp index 96f5b7aab3..49ccff1528 100644 --- a/shared/sys/sys_unix.cpp +++ b/shared/sys/sys_unix.cpp @@ -608,3 +608,25 @@ void Sys_AnsiColorPrint( const char *msg ) fputs( buffer, stderr ); } } + +/* +================ +Sys_SteamInit + +Only Windows has this feature at the moment. +================ +*/ +void Sys_SteamInit() +{ +} + +/* +================ +Sys_SteamShutdown + +Only Windows has this feature at the moment. +================ +*/ +void Sys_SteamShutdown() +{ +} \ No newline at end of file diff --git a/shared/sys/sys_win32.cpp b/shared/sys/sys_win32.cpp index f0b09d2675..8048a6d79b 100644 --- a/shared/sys/sys_win32.cpp +++ b/shared/sys/sys_win32.cpp @@ -20,6 +20,7 @@ along with this program; if not, see . */ #include "sys_local.h" +#include "sys_loadlib.h" #include #include #include @@ -590,3 +591,87 @@ void Sys_Sleep( int msec ) Sleep( msec ); #endif } + +/* +================ +Sys_SteamInit + +Steam initialization is done here. +In order for Steam to work, two things are needed: +- steam_api.dll (not included with retail Jedi Academy or Jedi Outcast!) +- steam_appid.txt (likewise) +steam_appid.txt is a text file containing either "6020" or "6030". +These correspond to Jedi Academy and Jedi Outcast, respectively. + +Steamworks SDK is required to use the playtime tracking and overlay features +without launching the app manually through Steam. +Unfortunately, the SDK does not play nice with copyleft licenses. +Fortunately! we can invoke the library directly and avoid this entirely, +provided the end-user has the goods. +Unfortunately! this is platform specific and so we have to do it here. +================ +*/ + +typedef bool(__stdcall* SteamAPIInit_Type)(); +typedef void(__stdcall* SteamAPIShutdown_Type)(); +static SteamAPIInit_Type SteamAPI_Init; +static SteamAPIShutdown_Type SteamAPI_Shutdown; +static void* gp_steamLibrary = nullptr; + +void Sys_SteamInit() +{ + if (!Cvar_VariableIntegerValue("com_steamIntegration")) + { + // Don't do anything if com_steamIntegration is disabled + return; + } + + // Load the library + gp_steamLibrary = Sys_LoadLibrary("steam_api" DLL_EXT); + if (!gp_steamLibrary) + { + Com_Printf(S_COLOR_RED "Steam integration failed: Couldn't find steam_api" DLL_EXT "\n"); + return; + } + + // Load the functions + SteamAPI_Init = (SteamAPIInit_Type)Sys_LoadFunction(gp_steamLibrary, "SteamAPI_Init"); + SteamAPI_Shutdown = (SteamAPIShutdown_Type)Sys_LoadFunction(gp_steamLibrary, "SteamAPI_Shutdown"); + + if (!SteamAPI_Shutdown || !SteamAPI_Init) + { + Com_Printf(S_COLOR_RED "Steam integration failed: Library invalid\n"); + Sys_UnloadLibrary(gp_steamLibrary); + gp_steamLibrary = nullptr; + return; + } + + // Finally, call the init function in Steam, which should pop up the overlay if everything went correctly + if (!SteamAPI_Init()) + { + Com_Printf(S_COLOR_RED "Steam integration failed: Steam init failed. Ensure steam_appid.txt exists and is valid.\n"); + Sys_UnloadLibrary(gp_steamLibrary); + gp_steamLibrary = nullptr; + return; + } +} + +/* +================ +Sys_SteamShutdown + +Platform-specific exit code +================ +*/ +void Sys_SteamShutdown() +{ + if (!gp_steamLibrary) + { + Com_Printf("Skipping Steam integration shutdown...\n"); + return; + } + + SteamAPI_Shutdown(); + Sys_UnloadLibrary(gp_steamLibrary); + gp_steamLibrary = nullptr; +} \ No newline at end of file