1
0
mirror of https://github.com/TerryCavanagh/VVVVVV.git synced 2024-06-01 18:43:33 +02:00
VVVVVV/desktop_version/src/SteamNetwork.c
Misa 58c518c856 Silence GCC warnings about void*-to-function-pointer casts
GCC warns on casting `void*` to function pointers. This is because the C
standard makes a clear distinction between pointers to objects (`void*`)
and pointers to functions (function pointers), and does not specify
anything related to being able to cast object pointers to function
pointers.

The warning message is factually wrong, though - it states that it is
forbidden by ISO C, when in fact it is not, and is actually just
unspecified.

We can't get rid of the cast entirely, because we need the explicit cast
(the C standard _does_ mandate you need an explicit cast when converting
between object pointers and function pointers), and at the end of the
day, this is simply how `SDL_LoadFunction()` works (and more
importantly, how `dlsym()` works), so we can't get rid of it, and we
have no reason to anyways since it means we don't have a hard runtime
dependency on Steam (unlike some other games) and casting `void*` to
function pointers always behaves well on every single platform we ship
on that supports Steam.

Unfortunately, this warning seems to be a part of -Wpedantic, and
there's no way to disable this warning specifically without disabling
-Wpedantic. Luckily, I've found a workaround - just cast to `intptr_t`
before casting to the function pointer. Hopefully the compiler doesn't
get smarter in the future and this ends up breaking or anything...
2021-10-21 01:00:06 -07:00

162 lines
4.0 KiB
C

#include "MakeAndPlay.h"
#ifndef MAKEANDPLAY
#include <stdint.h>
#include <SDL.h>
#include "Vlogging.h"
/* 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"
#elif defined(__APPLE__)
#define STEAM_LIBRARY "libsteam_api.dylib"
#elif defined(__linux__) || defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__HAIKU__) || defined(__DragonFly__)
#define STEAM_LIBRARY "libsteam_api.so"
#else
#error STEAM_LIBRARY: Unrecognized platform!
#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* \
))
static void *libHandle = NULL;
static struct ISteamUserStats *steamUserStats = NULL;
#define FOREACH_FUNC(rettype, name, params) static rettype (*name) params = NULL;
FUNC_LIST
#undef FOREACH_FUNC
/* Clean up after ourselves... */
static void ClearPointers(void)
{
SDL_UnloadObject(libHandle);
libHandle = NULL;
steamUserStats = NULL;
#define FOREACH_FUNC(rettype, name, params) name = NULL;
FUNC_LIST
#undef FOREACH_FUNC
}
/* NETWORK API Implementation */
int32_t STEAM_init(void)
{
#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;
}
void STEAM_shutdown(void)
{
if (libHandle)
{
SteamAPI_Shutdown();
ClearPointers();
}
}
void STEAM_update(void)
{
if (libHandle)
{
SteamAPI_RunCallbacks();
}
}
void STEAM_unlockAchievement(const char *name)
{
if (libHandle)
{
SteamAPI_ISteamUserStats_SetAchievement(
steamUserStats,
name
);
SteamAPI_ISteamUserStats_StoreStats(steamUserStats);
}
}
#endif /* MAKEANDPLAY */