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

270 lines
6.9 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 "CWrappers.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"
#define VVVVVV_STEAMSCREENSHOTS "STEAMSCREENSHOTS_INTERFACE_VERSION003"
2020-01-01 21:29:24 +01:00
/* 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;
struct ISteamScreenshots;
struct CallbackMsg_t
{
int32_t m_hSteamUser;
int32_t m_iCallback;
uint8_t* m_pubParam;
int32_t m_cubParam;
};
struct SteamAPICallCompleted_t
{
uint64_t m_hAsyncCall;
int32_t m_iCallback;
uint32_t m_cubParam;
};
#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* \
)) \
FOREACH_FUNC(struct ISteamScreenshots*, SteamAPI_ISteamClient_GetISteamScreenshots, ( \
struct ISteamClient*, \
int32_t, \
int32_t, \
const char* \
)) \
FOREACH_FUNC(void, SteamAPI_ISteamScreenshots_HookScreenshots, (\
struct ISteamScreenshots*, \
uint8_t \
)) \
FOREACH_FUNC(uint32_t, SteamAPI_ISteamScreenshots_WriteScreenshot, ( \
struct ISteamScreenshots*, \
void*, \
uint32_t, \
int32_t, \
int32_t \
)) \
FOREACH_FUNC(void, SteamAPI_ManualDispatch_Init, (void)) \
FOREACH_FUNC(void, SteamAPI_ManualDispatch_RunFrame, (int32_t)) \
FOREACH_FUNC(uint8_t, SteamAPI_ManualDispatch_GetNextCallback, (int32_t, struct CallbackMsg_t*)) \
FOREACH_FUNC(void, SteamAPI_ManualDispatch_FreeLastCallback, (int32_t)) \
FOREACH_FUNC(uint8_t, SteamAPI_ManualDispatch_GetAPICallResult, ( \
int32_t, \
uint64_t, \
void*, \
int32_t, \
int32_t, \
uint8_t* \
))
#define iScreenshotRequested 2302
static void* libHandle = NULL;
static struct ISteamUserStats* steamUserStats = NULL;
static struct ISteamScreenshots* steamScreenshots = 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
}
static void run_screenshot()
{
if (!libHandle)
{
return;
}
vlog_info("taking a screenshot");
SDL_Surface* surface = GRAPHICS_tempScreenshot();
uint8_t success = UTIL_TakeScreenshot(&surface);
if (!success)
{
return;
}
SDL_Surface* surface2x = GRAPHICS_tempScreenshot2x();
success = UTIL_UpscaleScreenshot2x(surface, &surface2x);
if (!success)
{
return;
}
SteamAPI_ISteamScreenshots_WriteScreenshot(
steamScreenshots,
surface2x->pixels,
surface2x->w * surface2x->h * surface2x->format->BytesPerPixel,
surface2x->w,
surface2x->h
);
}
2020-01-01 21:29:24 +01:00
/* NETWORK API Implementation */
static int32_t steamPipe = 0;
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;
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;
}
SteamAPI_ManualDispatch_Init();
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);
steamScreenshots = SteamAPI_ISteamClient_GetISteamScreenshots(
steamClient,
steamUser,
steamPipe,
VVVVVV_STEAMSCREENSHOTS
);
if (!steamScreenshots)
{
SteamAPI_Shutdown();
vlog_error(VVVVVV_STEAMSCREENSHOTS " not created!");
ClearPointers();
return 0;
}
SteamAPI_ISteamScreenshots_HookScreenshots(steamScreenshots, 1);
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)
{
return;
}
SteamAPI_ManualDispatch_RunFrame(steamPipe);
struct CallbackMsg_t callback;
SDL_zero(callback);
while (SteamAPI_ManualDispatch_GetNextCallback(steamPipe, &callback))
{
if (callback.m_iCallback == iScreenshotRequested)
{
run_screenshot();
}
SteamAPI_ManualDispatch_FreeLastCallback(steamPipe);
}
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 */