1
0
Fork 0
mirror of https://github.com/TerryCavanagh/VVVVVV.git synced 2024-11-05 02:39:41 +01:00
VVVVVV/desktop_version/src/SteamNetwork.c

162 lines
4 KiB
C
Raw Normal View History

#include "MakeAndPlay.h"
#ifndef MAKEANDPLAY
2020-01-01 21:29:24 +01:00
#include <stdint.h>
#include <SDL.h>
#include "Vlogging.h"
2020-01-01 21:29:24 +01:00
/* Steamworks interface versions */
#define VVVVVV_STEAMCLIENT "SteamClient017"
#define VVVVVV_STEAMUSERSTATS "STEAMUSERSTATS_INTERFACE_VERSION011"
/* Shared object file name */
#if defined(_WIN32)
#define STEAM_LIBRARY "steam_api.dll"
2020-01-01 21:29:24 +01:00
#elif defined(__APPLE__)
#define STEAM_LIBRARY "libsteam_api.dylib"
2020-04-20 15:41:11 +02:00
#elif defined(__linux__) || defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__HAIKU__) || defined(__DragonFly__)
#define STEAM_LIBRARY "libsteam_api.so"
2020-01-01 21:29:24 +01:00
#else
#error STEAM_LIBRARY: Unrecognized platform!
2020-01-01 21:29:24 +01:00
#endif
/* DLL, Entry Points */
struct ISteamClient;
struct ISteamUserStats;
#define FUNC_LIST \
FOREACH_FUNC(uint8_t, SteamAPI_Init, (void)) \
FOREACH_FUNC(void, SteamAPI_Shutdown, (void)) \
FOREACH_FUNC(void, SteamAPI_RunCallbacks, (void)) \
FOREACH_FUNC(struct ISteamClient*, SteamInternal_CreateInterface, (const char*)) \
FOREACH_FUNC(int32_t, SteamAPI_GetHSteamUser, (void)) \
FOREACH_FUNC(int32_t, SteamAPI_GetHSteamPipe, (void)) \
FOREACH_FUNC(struct ISteamUserStats*, SteamAPI_ISteamClient_GetISteamUserStats, ( \
struct ISteamClient*, \
int32_t, \
int32_t, \
const char* \
)) \
FOREACH_FUNC(uint8_t, SteamAPI_ISteamUserStats_RequestCurrentStats, (struct ISteamUserStats*)) \
FOREACH_FUNC(uint8_t, SteamAPI_ISteamUserStats_StoreStats, (struct ISteamUserStats*)) \
FOREACH_FUNC(uint8_t, SteamAPI_ISteamUserStats_SetAchievement, ( \
struct ISteamUserStats*, \
const char* \
))
2020-01-01 21:29:24 +01:00
static void *libHandle = NULL;
static struct ISteamUserStats *steamUserStats = NULL;
2020-01-01 21:29:24 +01:00
#define FOREACH_FUNC(rettype, name, params) static rettype (*name) params = NULL;
FUNC_LIST
#undef FOREACH_FUNC
2020-01-01 21:29:24 +01:00
/* Clean up after ourselves... */
static void ClearPointers(void)
2020-01-01 21:29:24 +01:00
{
SDL_UnloadObject(libHandle);
libHandle = NULL;
steamUserStats = NULL;
#define FOREACH_FUNC(rettype, name, params) name = NULL;
FUNC_LIST
#undef FOREACH_FUNC
2020-01-01 21:29:24 +01:00
}
/* NETWORK API Implementation */
int32_t STEAM_init(void)
2020-01-01 21:29:24 +01:00
{
2020-04-20 15:41:11 +02:00
#if defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__HAIKU__) || defined(__DragonFly__)
return 0;
#endif
struct ISteamClient *steamClient;
int32_t steamUser, steamPipe;
libHandle = SDL_LoadObject(STEAM_LIBRARY);
if (!libHandle)
{
vlog_info(STEAM_LIBRARY " not found!");
return 0;
}
#define FOREACH_FUNC(rettype, name, params) \
name = (rettype (*) params) (intptr_t) SDL_LoadFunction(libHandle, #name); \
if (!name) \
{ \
vlog_error(STEAM_LIBRARY " symbol " #name " not found!"); \
ClearPointers(); \
return 0; \
}
FUNC_LIST
#undef FOREACH_FUNC
if (!SteamAPI_Init())
{
vlog_error("Steamworks not initialized!");
ClearPointers();
return 0;
}
steamClient = SteamInternal_CreateInterface(VVVVVV_STEAMCLIENT);
steamUser = SteamAPI_GetHSteamUser();
steamPipe = SteamAPI_GetHSteamPipe();
if (!steamClient || !steamUser || !steamPipe)
{
SteamAPI_Shutdown();
vlog_error(VVVVVV_STEAMCLIENT " not created!");
ClearPointers();
return 0;
}
steamUserStats = SteamAPI_ISteamClient_GetISteamUserStats(
steamClient,
steamUser,
steamPipe,
VVVVVV_STEAMUSERSTATS
);
if (!steamUserStats)
{
SteamAPI_Shutdown();
vlog_error(VVVVVV_STEAMUSERSTATS " not created!");
ClearPointers();
return 0;
}
SteamAPI_ISteamUserStats_RequestCurrentStats(steamUserStats);
return 1;
2020-01-01 21:29:24 +01:00
}
void STEAM_shutdown(void)
2020-01-01 21:29:24 +01:00
{
if (libHandle)
{
SteamAPI_Shutdown();
ClearPointers();
}
2020-01-01 21:29:24 +01:00
}
void STEAM_update(void)
2020-01-01 21:29:24 +01:00
{
if (libHandle)
{
SteamAPI_RunCallbacks();
}
2020-01-01 21:29:24 +01:00
}
void STEAM_unlockAchievement(const char *name)
2020-01-01 21:29:24 +01:00
{
if (libHandle)
{
SteamAPI_ISteamUserStats_SetAchievement(
steamUserStats,
name
);
SteamAPI_ISteamUserStats_StoreStats(steamUserStats);
}
2020-01-01 21:29:24 +01:00
}
#endif /* MAKEANDPLAY */