2020-08-03 05:35:00 +02:00
|
|
|
#include "MakeAndPlay.h"
|
|
|
|
|
|
|
|
#ifndef MAKEANDPLAY
|
|
|
|
|
2020-01-01 21:29:24 +01:00
|
|
|
#include <stdint.h>
|
|
|
|
#include <SDL.h>
|
|
|
|
|
2021-02-24 00:21:29 +01:00
|
|
|
#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)
|
2020-01-13 17:15:17 +01:00
|
|
|
#define STEAM_LIBRARY "steam_api.dll"
|
2020-01-01 21:29:24 +01:00
|
|
|
#elif defined(__APPLE__)
|
2020-01-13 17:15:17 +01:00
|
|
|
#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__)
|
2020-01-13 17:15:17 +01:00
|
|
|
#define STEAM_LIBRARY "libsteam_api.so"
|
2020-01-01 21:29:24 +01:00
|
|
|
#else
|
2020-01-13 17:15:17 +01:00
|
|
|
#error STEAM_LIBRARY: Unrecognized platform!
|
2020-01-01 21:29:24 +01:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Function Pointer Types */
|
|
|
|
|
Explicitly declare void for all void parameter functions (#628)
Apparently in C, if you have `void test();`, it's completely okay to do
`test(2);`. The function will take in the argument, but just discard it
and throw it away. It's like a trash can, and a rude one at that. If you
declare it like `void test(void);`, this is prevented.
This is not a problem in C++ - doing `void test();` and `test(2);` is
guaranteed to result in a compile error (this also means that right now,
at least in all `.cpp` files, nobody is ever calling a void parameter
function with arguments and having their arguments be thrown away).
However, we may not be using C++ in the future, so I just want to lay
down the precedent that if a function takes in no arguments, you must
explicitly declare it as such.
I would've added `-Wstrict-prototypes`, but it produces an annoying
warning message saying it doesn't work in C++ mode if you're compiling
in C++ mode. So it can be added later.
2021-02-25 23:23:59 +01:00
|
|
|
typedef uint8_t (*SteamAPI_InitFunc)(void);
|
|
|
|
typedef void (*SteamAPI_ShutdownFunc)(void);
|
|
|
|
typedef void (*SteamAPI_RunCallbacksFunc)(void);
|
2020-01-01 21:29:24 +01:00
|
|
|
typedef intptr_t (*SteamInternal_CreateInterfaceFunc)(const char*);
|
Explicitly declare void for all void parameter functions (#628)
Apparently in C, if you have `void test();`, it's completely okay to do
`test(2);`. The function will take in the argument, but just discard it
and throw it away. It's like a trash can, and a rude one at that. If you
declare it like `void test(void);`, this is prevented.
This is not a problem in C++ - doing `void test();` and `test(2);` is
guaranteed to result in a compile error (this also means that right now,
at least in all `.cpp` files, nobody is ever calling a void parameter
function with arguments and having their arguments be thrown away).
However, we may not be using C++ in the future, so I just want to lay
down the precedent that if a function takes in no arguments, you must
explicitly declare it as such.
I would've added `-Wstrict-prototypes`, but it produces an annoying
warning message saying it doesn't work in C++ mode if you're compiling
in C++ mode. So it can be added later.
2021-02-25 23:23:59 +01:00
|
|
|
typedef int32_t (*SteamAPI_GetHSteamUserFunc)(void);
|
|
|
|
typedef int32_t (*SteamAPI_GetHSteamPipeFunc)(void);
|
2020-01-01 21:29:24 +01:00
|
|
|
typedef intptr_t (*SteamAPI_ISteamClient_GetISteamUserStatsFunc)(
|
2021-09-07 03:56:39 +02:00
|
|
|
intptr_t,
|
|
|
|
int32_t,
|
|
|
|
int32_t,
|
|
|
|
const char*
|
2020-01-01 21:29:24 +01:00
|
|
|
);
|
|
|
|
typedef uint8_t (*SteamAPI_ISteamUserStats_RequestCurrentStatsFunc)(intptr_t);
|
|
|
|
typedef uint8_t (*SteamAPI_ISteamUserStats_StoreStatsFunc)(intptr_t);
|
|
|
|
typedef uint8_t (*SteamAPI_ISteamUserStats_GetStatFunc)(
|
2021-09-07 03:56:39 +02:00
|
|
|
intptr_t,
|
|
|
|
const char*,
|
|
|
|
int32_t*
|
2020-01-01 21:29:24 +01:00
|
|
|
);
|
|
|
|
typedef uint8_t (*SteamAPI_ISteamUserStats_SetStatFunc)(
|
2021-09-07 03:56:39 +02:00
|
|
|
intptr_t,
|
|
|
|
const char*,
|
|
|
|
int32_t
|
2020-01-01 21:29:24 +01:00
|
|
|
);
|
|
|
|
typedef uint8_t (*SteamAPI_ISteamUserStats_SetAchievementFunc)(
|
2021-09-07 03:56:39 +02:00
|
|
|
intptr_t,
|
|
|
|
const char*
|
2020-01-01 21:29:24 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
/* DLL, Entry Points */
|
|
|
|
|
|
|
|
static void *libHandle = NULL;
|
|
|
|
static intptr_t steamUserStats = (intptr_t) NULL;
|
|
|
|
|
|
|
|
#define DEFINE_FUNC(name) static name##Func name = NULL;
|
|
|
|
DEFINE_FUNC(SteamAPI_Init)
|
|
|
|
DEFINE_FUNC(SteamAPI_Shutdown)
|
|
|
|
DEFINE_FUNC(SteamAPI_RunCallbacks)
|
|
|
|
DEFINE_FUNC(SteamInternal_CreateInterface)
|
|
|
|
DEFINE_FUNC(SteamAPI_GetHSteamUser)
|
|
|
|
DEFINE_FUNC(SteamAPI_GetHSteamPipe)
|
|
|
|
DEFINE_FUNC(SteamAPI_ISteamClient_GetISteamUserStats)
|
|
|
|
DEFINE_FUNC(SteamAPI_ISteamUserStats_RequestCurrentStats)
|
|
|
|
DEFINE_FUNC(SteamAPI_ISteamUserStats_StoreStats)
|
|
|
|
DEFINE_FUNC(SteamAPI_ISteamUserStats_GetStat)
|
|
|
|
DEFINE_FUNC(SteamAPI_ISteamUserStats_SetStat)
|
|
|
|
DEFINE_FUNC(SteamAPI_ISteamUserStats_SetAchievement)
|
|
|
|
#undef DEFINE_FUNC
|
|
|
|
|
|
|
|
/* Clean up after ourselves... */
|
|
|
|
|
Explicitly declare void for all void parameter functions (#628)
Apparently in C, if you have `void test();`, it's completely okay to do
`test(2);`. The function will take in the argument, but just discard it
and throw it away. It's like a trash can, and a rude one at that. If you
declare it like `void test(void);`, this is prevented.
This is not a problem in C++ - doing `void test();` and `test(2);` is
guaranteed to result in a compile error (this also means that right now,
at least in all `.cpp` files, nobody is ever calling a void parameter
function with arguments and having their arguments be thrown away).
However, we may not be using C++ in the future, so I just want to lay
down the precedent that if a function takes in no arguments, you must
explicitly declare it as such.
I would've added `-Wstrict-prototypes`, but it produces an annoying
warning message saying it doesn't work in C++ mode if you're compiling
in C++ mode. So it can be added later.
2021-02-25 23:23:59 +01:00
|
|
|
static void ClearPointers(void)
|
2020-01-01 21:29:24 +01:00
|
|
|
{
|
2021-09-07 03:56:39 +02:00
|
|
|
SDL_UnloadObject(libHandle);
|
|
|
|
libHandle = NULL;
|
|
|
|
steamUserStats = (intptr_t) NULL;
|
|
|
|
SteamAPI_Init = NULL;
|
|
|
|
SteamAPI_Shutdown = NULL;
|
|
|
|
SteamAPI_RunCallbacks = NULL;
|
|
|
|
SteamInternal_CreateInterface = NULL;
|
|
|
|
SteamAPI_GetHSteamUser = NULL;
|
|
|
|
SteamAPI_GetHSteamPipe = NULL;
|
|
|
|
SteamAPI_ISteamClient_GetISteamUserStats = NULL;
|
|
|
|
SteamAPI_ISteamUserStats_RequestCurrentStats = NULL;
|
|
|
|
SteamAPI_ISteamUserStats_StoreStats = NULL;
|
|
|
|
SteamAPI_ISteamUserStats_GetStat = NULL;
|
|
|
|
SteamAPI_ISteamUserStats_SetStat = NULL;
|
|
|
|
SteamAPI_ISteamUserStats_SetAchievement = NULL;
|
2020-01-01 21:29:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* NETWORK API Implementation */
|
|
|
|
|
Explicitly declare void for all void parameter functions (#628)
Apparently in C, if you have `void test();`, it's completely okay to do
`test(2);`. The function will take in the argument, but just discard it
and throw it away. It's like a trash can, and a rude one at that. If you
declare it like `void test(void);`, this is prevented.
This is not a problem in C++ - doing `void test();` and `test(2);` is
guaranteed to result in a compile error (this also means that right now,
at least in all `.cpp` files, nobody is ever calling a void parameter
function with arguments and having their arguments be thrown away).
However, we may not be using C++ in the future, so I just want to lay
down the precedent that if a function takes in no arguments, you must
explicitly declare it as such.
I would've added `-Wstrict-prototypes`, but it produces an annoying
warning message saying it doesn't work in C++ mode if you're compiling
in C++ mode. So it can be added later.
2021-02-25 23:23:59 +01:00
|
|
|
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__)
|
2021-09-07 03:56:39 +02:00
|
|
|
return 0;
|
2020-01-10 22:38:33 +01:00
|
|
|
#endif
|
2021-09-07 03:56:39 +02:00
|
|
|
intptr_t steamClient;
|
|
|
|
int32_t steamUser, steamPipe;
|
|
|
|
|
|
|
|
libHandle = SDL_LoadObject(STEAM_LIBRARY);
|
|
|
|
if (!libHandle)
|
|
|
|
{
|
|
|
|
vlog_info("%s not found!", STEAM_LIBRARY);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
#define LOAD_FUNC(name) \
|
|
|
|
name = (name##Func) SDL_LoadFunction(libHandle, #name); \
|
|
|
|
if (!name) \
|
|
|
|
{ \
|
|
|
|
vlog_error("%s symbol %s not found!", STEAM_LIBRARY, #name); \
|
|
|
|
ClearPointers(); \
|
|
|
|
return 0; \
|
|
|
|
}
|
|
|
|
LOAD_FUNC(SteamAPI_Init)
|
|
|
|
LOAD_FUNC(SteamAPI_Shutdown)
|
|
|
|
LOAD_FUNC(SteamAPI_RunCallbacks)
|
|
|
|
LOAD_FUNC(SteamInternal_CreateInterface)
|
|
|
|
LOAD_FUNC(SteamAPI_GetHSteamUser)
|
|
|
|
LOAD_FUNC(SteamAPI_GetHSteamPipe)
|
|
|
|
LOAD_FUNC(SteamAPI_ISteamClient_GetISteamUserStats)
|
|
|
|
LOAD_FUNC(SteamAPI_ISteamUserStats_RequestCurrentStats)
|
|
|
|
LOAD_FUNC(SteamAPI_ISteamUserStats_StoreStats)
|
|
|
|
LOAD_FUNC(SteamAPI_ISteamUserStats_GetStat)
|
|
|
|
LOAD_FUNC(SteamAPI_ISteamUserStats_SetStat)
|
|
|
|
LOAD_FUNC(SteamAPI_ISteamUserStats_SetAchievement)
|
|
|
|
#undef LOAD_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
|
|
|
}
|
|
|
|
|
Explicitly declare void for all void parameter functions (#628)
Apparently in C, if you have `void test();`, it's completely okay to do
`test(2);`. The function will take in the argument, but just discard it
and throw it away. It's like a trash can, and a rude one at that. If you
declare it like `void test(void);`, this is prevented.
This is not a problem in C++ - doing `void test();` and `test(2);` is
guaranteed to result in a compile error (this also means that right now,
at least in all `.cpp` files, nobody is ever calling a void parameter
function with arguments and having their arguments be thrown away).
However, we may not be using C++ in the future, so I just want to lay
down the precedent that if a function takes in no arguments, you must
explicitly declare it as such.
I would've added `-Wstrict-prototypes`, but it produces an annoying
warning message saying it doesn't work in C++ mode if you're compiling
in C++ mode. So it can be added later.
2021-02-25 23:23:59 +01:00
|
|
|
void STEAM_shutdown(void)
|
2020-01-01 21:29:24 +01:00
|
|
|
{
|
2021-09-07 03:56:39 +02:00
|
|
|
if (libHandle)
|
|
|
|
{
|
|
|
|
SteamAPI_Shutdown();
|
|
|
|
ClearPointers();
|
|
|
|
}
|
2020-01-01 21:29:24 +01:00
|
|
|
}
|
|
|
|
|
Explicitly declare void for all void parameter functions (#628)
Apparently in C, if you have `void test();`, it's completely okay to do
`test(2);`. The function will take in the argument, but just discard it
and throw it away. It's like a trash can, and a rude one at that. If you
declare it like `void test(void);`, this is prevented.
This is not a problem in C++ - doing `void test();` and `test(2);` is
guaranteed to result in a compile error (this also means that right now,
at least in all `.cpp` files, nobody is ever calling a void parameter
function with arguments and having their arguments be thrown away).
However, we may not be using C++ in the future, so I just want to lay
down the precedent that if a function takes in no arguments, you must
explicitly declare it as such.
I would've added `-Wstrict-prototypes`, but it produces an annoying
warning message saying it doesn't work in C++ mode if you're compiling
in C++ mode. So it can be added later.
2021-02-25 23:23:59 +01:00
|
|
|
void STEAM_update(void)
|
2020-01-01 21:29:24 +01:00
|
|
|
{
|
2021-09-07 03:56:39 +02:00
|
|
|
if (libHandle)
|
|
|
|
{
|
|
|
|
SteamAPI_RunCallbacks();
|
|
|
|
}
|
2020-01-01 21:29:24 +01:00
|
|
|
}
|
|
|
|
|
2020-01-13 17:15:17 +01:00
|
|
|
void STEAM_unlockAchievement(const char *name)
|
2020-01-01 21:29:24 +01:00
|
|
|
{
|
2021-09-07 03:56:39 +02:00
|
|
|
if (libHandle)
|
|
|
|
{
|
|
|
|
SteamAPI_ISteamUserStats_SetAchievement(
|
|
|
|
steamUserStats,
|
|
|
|
name
|
|
|
|
);
|
|
|
|
SteamAPI_ISteamUserStats_StoreStats(steamUserStats);
|
|
|
|
}
|
2020-01-01 21:29:24 +01:00
|
|
|
}
|
|
|
|
|
2020-01-13 17:15:17 +01:00
|
|
|
int32_t STEAM_getAchievementProgress(const char *name)
|
2020-01-01 21:29:24 +01:00
|
|
|
{
|
2021-09-07 03:56:39 +02:00
|
|
|
int32_t result = -1;
|
|
|
|
if (libHandle)
|
|
|
|
{
|
|
|
|
SteamAPI_ISteamUserStats_GetStat(
|
|
|
|
steamUserStats,
|
|
|
|
name,
|
|
|
|
&result
|
|
|
|
);
|
|
|
|
}
|
|
|
|
return result;
|
2020-01-01 21:29:24 +01:00
|
|
|
}
|
|
|
|
|
2020-01-13 17:15:17 +01:00
|
|
|
void STEAM_setAchievementProgress(const char *name, int32_t stat)
|
2020-01-01 21:29:24 +01:00
|
|
|
{
|
2021-09-07 03:56:39 +02:00
|
|
|
if (libHandle)
|
|
|
|
{
|
|
|
|
SteamAPI_ISteamUserStats_SetStat(
|
|
|
|
steamUserStats,
|
|
|
|
name,
|
|
|
|
stat
|
|
|
|
);
|
|
|
|
SteamAPI_ISteamUserStats_StoreStats(steamUserStats);
|
|
|
|
}
|
2020-01-01 21:29:24 +01:00
|
|
|
}
|
2020-08-03 05:35:00 +02:00
|
|
|
|
|
|
|
#endif /* MAKEANDPLAY */
|