2020-08-03 05:35:00 +02:00
|
|
|
#include "MakeAndPlay.h"
|
|
|
|
|
|
|
|
#ifndef MAKEANDPLAY
|
|
|
|
|
2020-01-01 21:29:24 +01:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <SDL.h>
|
|
|
|
|
|
|
|
/* 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)(
|
|
|
|
intptr_t,
|
|
|
|
int32_t,
|
|
|
|
int32_t,
|
|
|
|
const char*
|
|
|
|
);
|
|
|
|
typedef uint8_t (*SteamAPI_ISteamUserStats_RequestCurrentStatsFunc)(intptr_t);
|
|
|
|
typedef uint8_t (*SteamAPI_ISteamUserStats_StoreStatsFunc)(intptr_t);
|
|
|
|
typedef uint8_t (*SteamAPI_ISteamUserStats_GetStatFunc)(
|
|
|
|
intptr_t,
|
|
|
|
const char*,
|
|
|
|
int32_t*
|
|
|
|
);
|
|
|
|
typedef uint8_t (*SteamAPI_ISteamUserStats_SetStatFunc)(
|
|
|
|
intptr_t,
|
|
|
|
const char*,
|
|
|
|
int32_t
|
|
|
|
);
|
|
|
|
typedef uint8_t (*SteamAPI_ISteamUserStats_SetAchievementFunc)(
|
|
|
|
intptr_t,
|
|
|
|
const char*
|
|
|
|
);
|
|
|
|
|
|
|
|
/* 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
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 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__)
|
2020-01-13 17:15:17 +01:00
|
|
|
return 0;
|
2020-01-10 22:38:33 +01:00
|
|
|
#endif
|
2020-01-01 21:29:24 +01:00
|
|
|
intptr_t steamClient;
|
|
|
|
int32_t steamUser, steamPipe;
|
|
|
|
|
2020-01-13 17:15:17 +01:00
|
|
|
libHandle = SDL_LoadObject(STEAM_LIBRARY);
|
2020-01-01 21:29:24 +01:00
|
|
|
if (!libHandle)
|
|
|
|
{
|
2020-01-13 17:15:17 +01:00
|
|
|
printf("%s not found!\n", STEAM_LIBRARY);
|
2020-01-01 21:29:24 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
#define LOAD_FUNC(name) \
|
|
|
|
name = (name##Func) SDL_LoadFunction(libHandle, #name); \
|
|
|
|
if (!name) \
|
|
|
|
{ \
|
2020-01-13 17:15:17 +01:00
|
|
|
printf("%s symbol %s not found!\n", STEAM_LIBRARY, #name); \
|
2020-01-01 21:29:24 +01:00
|
|
|
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())
|
|
|
|
{
|
|
|
|
printf("Steamworks not initialized!\n");
|
|
|
|
ClearPointers();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
steamClient = SteamInternal_CreateInterface(VVVVVV_STEAMCLIENT);
|
|
|
|
steamUser = SteamAPI_GetHSteamUser();
|
|
|
|
steamPipe = SteamAPI_GetHSteamPipe();
|
|
|
|
if (!steamClient || !steamUser || !steamPipe)
|
|
|
|
{
|
|
|
|
SteamAPI_Shutdown();
|
|
|
|
printf(VVVVVV_STEAMCLIENT " not created!\n");
|
|
|
|
ClearPointers();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
steamUserStats = SteamAPI_ISteamClient_GetISteamUserStats(
|
|
|
|
steamClient,
|
|
|
|
steamUser,
|
|
|
|
steamPipe,
|
|
|
|
VVVVVV_STEAMUSERSTATS
|
|
|
|
);
|
|
|
|
if (!steamUserStats)
|
|
|
|
{
|
|
|
|
SteamAPI_Shutdown();
|
|
|
|
printf(VVVVVV_STEAMUSERSTATS " not created!\n");
|
|
|
|
ClearPointers();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
SteamAPI_ISteamUserStats_RequestCurrentStats(steamUserStats);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
|
|
|
if (libHandle)
|
|
|
|
{
|
|
|
|
SteamAPI_Shutdown();
|
|
|
|
ClearPointers();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
|
|
|
if (libHandle)
|
|
|
|
{
|
|
|
|
SteamAPI_RunCallbacks();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-13 17:15:17 +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-13 17:15:17 +01:00
|
|
|
int32_t STEAM_getAchievementProgress(const char *name)
|
2020-01-01 21:29:24 +01:00
|
|
|
{
|
|
|
|
int32_t result = -1;
|
|
|
|
if (libHandle)
|
|
|
|
{
|
|
|
|
SteamAPI_ISteamUserStats_GetStat(
|
|
|
|
steamUserStats,
|
|
|
|
name,
|
|
|
|
&result
|
|
|
|
);
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
|
|
|
if (libHandle)
|
|
|
|
{
|
|
|
|
SteamAPI_ISteamUserStats_SetStat(
|
|
|
|
steamUserStats,
|
|
|
|
name,
|
|
|
|
stat
|
|
|
|
);
|
|
|
|
SteamAPI_ISteamUserStats_StoreStats(steamUserStats);
|
|
|
|
}
|
|
|
|
}
|
2020-08-03 05:35:00 +02:00
|
|
|
|
|
|
|
#endif /* MAKEANDPLAY */
|