2 * Copyright (C) 2021-2022 Mt.ZERO Software, Harry Godden - All Rights Reserved
3 * All trademarks are property of their respective owners
10 #include "vg/vg_steam.h"
11 #include "vg/vg_steam_utils.h"
12 #include "vg/vg_steam_networking.h"
13 #include "vg/vg_steam_auth.h"
14 #include "vg/vg_steam_http.h"
15 #include "vg/vg_steam_friends.h"
16 #include "vg/vg_steam_user_stats.h"
19 * We only want to use steamworks if building for the networked version,
20 * theres not much point otherwise. We mainly want steamworks for setting
21 * achievements etc.. so that includes our own server too.
23 * This file also wraps the functions and interfaces that we want to use to
24 * make them a bit easier to read, since they are the flat API they have very
25 * long names. in non-networked builds they will return default errors or do
29 VG_STATIC
char steam_username_at_startup
[128];
31 VG_STATIC
void recv_steam_warning( int severity
, const char *msg
)
34 vg_low( "%s\n", msg
);
36 vg_info( "%s\n", msg
);
39 VG_STATIC
int steam_ready
= 0,
40 steam_stats_ready
= 0;
42 VG_STATIC
void *hSteamNetworkingSockets
,
45 VG_STATIC ISteamUserStats
*hSteamUserStats
;
46 VG_STATIC HSteamPipe hSteamClientPipe
;
48 VG_STATIC
const char *steam_achievement_names
[] =
51 "ROUTE_MPY", "ROUTE_MPG", "ROUTE_MPB", "ROUTE_MPR",
52 "ROUTE_TO", "ROUTE_TC"
55 VG_STATIC
void steam_store_achievements(void)
57 if( steam_ready
&& steam_stats_ready
)
59 SteamAPI_ISteamUserStats_StoreStats( hSteamUserStats
);
63 VG_STATIC
void steam_set_achievement( const char *name
)
65 if( steam_ready
&& steam_stats_ready
)
67 if( SteamAPI_ISteamUserStats_SetAchievement( hSteamUserStats
, name
) )
69 vg_success( "Achievement set! '%s'\n", name
);
73 vg_warn( "Failed to set achievement: %s\n", name
);
78 vg_warn( "Failed to set achievement (steam not ready): %s\n", name
);
82 VG_STATIC
void steam_clear_achievement( const char *name
)
84 if( steam_ready
&& steam_stats_ready
)
86 if( SteamAPI_ISteamUserStats_ClearAchievement( hSteamUserStats
, name
) )
88 vg_info( "Achievement cleared: '%s'\n", name
);
92 vg_warn( "Failed to clear achievement: %s\n", name
);
97 vg_warn( "Failed to clear achievement (steam not ready): %s\n", name
);
102 VG_STATIC
int steam_list_achievements( int argc
, char const *argv
[] )
104 vg_info( "Achievements: \n" );
106 if( steam_ready
&& steam_stats_ready
)
108 for( int i
=0; i
<vg_list_size(steam_achievement_names
); i
++ )
111 const char *name
= steam_achievement_names
[i
];
113 if( SteamAPI_ISteamUserStats_GetAchievement(
114 hSteamUserStats
, name
, &set
) )
116 vg_info( " %s %s\n", (set
? "[YES]": "[ ]"), name
);
120 vg_warn( " Error while fetching achievement status '%s'\n", name
);
126 vg_warn( " Steam is not initialized, no results\n" );
132 VG_STATIC
int steam_clear_all_achievements( int argc
, char const *argv
[] )
134 if( steam_ready
&& steam_stats_ready
)
136 for( int i
=0; i
<vg_list_size(steam_achievement_names
); i
++ )
138 steam_clear_achievement( steam_achievement_names
[i
] );
141 steam_store_achievements();
145 vg_warn( "steam is not initialized, cannot clear\n" );
151 VG_STATIC
int steam_set_achievemnt_test( int argc
, char const *argv
[] )
156 if( strcmp( argv
[0], "monkey_island" ) )
159 steam_set_achievement( argv
[1] );
160 steam_store_achievements();
165 VG_STATIC
void steam_on_recieve_current_stats( CallbackMsg_t
*msg
)
167 UserStatsReceived_t
*rec
= (UserStatsReceived_t
*)msg
->m_pubParam
;
169 if( rec
->m_eResult
== k_EResultOK
)
171 vg_info( "Recieved stats for: %lu (user: %lu)\n", rec
->m_nGameID
,
172 rec
->m_steamIDUser
);
173 steam_stats_ready
= 1;
177 vg_error( "Error recieveing stats for user (%u)\n", rec
->m_eResult
);
181 VG_STATIC ISteamInput
*steam_hInput
;
183 VG_STATIC
int steam_init(void)
185 const char *username
= NULL
;
188 vg_info( "Initializing steamworks\n" );
190 if( !SteamAPI_Init() )
193 vg_error( "Steamworks failed to initialize\n" );
199 SteamAPI_ManualDispatch_Init();
201 /* Connect interfaces */
202 hSteamClientPipe
= SteamAPI_GetHSteamPipe();
203 hSteamNetworkingSockets
= SteamAPI_SteamNetworkingSockets_SteamAPI();
204 hSteamUser
= SteamAPI_SteamUser();
206 ISteamUtils
*utils
= SteamAPI_SteamUtils();
207 SteamAPI_ISteamUtils_SetWarningMessageHook( utils
, recv_steam_warning
);
210 vg_success( "\nSteamworks API running\n" );
212 ISteamFriends
*hSteamFriends
= SteamAPI_SteamFriends();
213 username
= SteamAPI_ISteamFriends_GetPersonaName( hSteamFriends
);
217 * --------------------------------------------------------
219 hSteamUserStats
= SteamAPI_SteamUserStats();
221 steam_register_callback( k_iUserStatsReceived
,
222 steam_on_recieve_current_stats
);
224 if( !SteamAPI_ISteamUserStats_RequestCurrentStats( hSteamUserStats
) )
225 vg_warn( "No Steam Logon: Cannot request stats\n" );
228 vg_function_push( (struct vg_cmd
)
231 .function
= steam_list_achievements
234 vg_function_push( (struct vg_cmd
)
236 .name
= "ach_clear_all",
237 .function
= steam_clear_all_achievements
240 vg_function_push( (struct vg_cmd
)
243 .function
= steam_set_achievemnt_test
246 steam_hInput
= SteamAPI_SteamInput();
247 SteamAPI_ISteamInput_Init( steam_hInput
, 0 );
248 SteamAPI_ISteamInput_RunFrame( steam_hInput
, 0 );
252 /* TODO: On username update callback */
253 str_utf8_collapse( username
, steam_username_at_startup
,
254 vg_list_size(steam_username_at_startup
) );
259 VG_STATIC
void steam_update(void)
263 steamworks_event_loop( hSteamClientPipe
);
266 * We can probably request this from SDL too
270 SteamAPI_ISteamInput_RunFrame( steam_hInput
, 0 );
272 InputHandle_t joy0
= SteamAPI_ISteamInput_GetControllerForGamepadIndex(
275 vg_input
.controller_should_use_trackpad_look
= 0;
278 ESteamInputType type
= SteamAPI_ISteamInput_GetInputTypeForHandle(
279 steam_hInput
, joy0
);
281 if( type
== k_ESteamInputType_SteamController
)
283 vg_input
.controller_should_use_trackpad_look
= 1;
284 menu_display_controller
= k_menu_controller_type_steam
;
286 else if( type
== k_ESteamInputType_SteamDeckController
)
288 menu_display_controller
= k_menu_controller_type_steam_deck
;
290 else if( type
== k_ESteamInputType_PS3Controller
||
291 type
== k_ESteamInputType_PS4Controller
||
292 type
== k_ESteamInputType_PS5Controller
)
294 menu_display_controller
= k_menu_controller_type_playstation
;
296 else if( type
== k_ESteamInputType_XBox360Controller
||
297 type
== k_ESteamInputType_XBoxOneController
)
299 menu_display_controller
= k_menu_controller_type_xbox
;
303 /* currently unsupported controller */
304 menu_display_controller
= k_menu_controller_type_xbox
;
308 menu_display_controller
= k_menu_controller_type_keyboard
;
313 VG_STATIC
void steam_end(void)
317 vg_info( "Shutting down\n..." );