2 * Copyright (C) 2021-2023 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"
17 #include "submodules/anyascii/impl/c/anyascii.c"
20 * We only want to use steamworks if building for the networked version,
21 * theres not much point otherwise. We mainly want steamworks for setting
22 * achievements etc.. so that includes our own server too.
24 * This file also wraps the functions and interfaces that we want to use to
25 * make them a bit easier to read, since they are the flat API they have very
26 * long names. in non-networked builds they will return default errors or do
30 static char steam_username_at_startup
[128];
32 static void recv_steam_warning( int severity
, const char *msg
)
35 vg_low( "%s\n", msg
);
37 vg_info( "%s\n", msg
);
40 static int steam_ready
= 0,
41 steam_stats_ready
= 0;
43 static void *hSteamNetworkingSockets
,
46 static ISteamUserStats
*hSteamUserStats
;
47 static HSteamPipe hSteamClientPipe
;
49 static const char *steam_achievement_names
[] =
52 "ROUTE_MPY", "ROUTE_MPG", "ROUTE_MPB", "ROUTE_MPR",
53 "ROUTE_TO", "ROUTE_TC"
56 static void steam_store_achievements(void)
58 if( steam_ready
&& steam_stats_ready
){
59 SteamAPI_ISteamUserStats_StoreStats( hSteamUserStats
);
63 static void steam_set_achievement( const char *name
)
65 if( steam_ready
&& steam_stats_ready
){
66 if( SteamAPI_ISteamUserStats_SetAchievement( hSteamUserStats
, name
) ){
67 vg_success( "Achievement set! '%s'\n", name
);
70 vg_warn( "Failed to set achievement: %s\n", name
);
74 vg_warn( "Failed to set achievement (steam not ready): %s\n", name
);
78 static void steam_clear_achievement( const char *name
)
80 if( steam_ready
&& steam_stats_ready
){
81 if( SteamAPI_ISteamUserStats_ClearAchievement( hSteamUserStats
, name
) ){
82 vg_info( "Achievement cleared: '%s'\n", name
);
85 vg_warn( "Failed to clear achievement: %s\n", name
);
89 vg_warn( "Failed to clear achievement (steam not ready): %s\n", name
);
94 static void steam_print_all_achievements(void){
95 vg_info( "Achievements: \n" );
97 if( steam_ready
&& steam_stats_ready
){
98 for( int i
=0; i
<vg_list_size(steam_achievement_names
); i
++ ){
99 steamapi_bool set
= 0;
100 const char *name
= steam_achievement_names
[i
];
102 if( SteamAPI_ISteamUserStats_GetAchievement(
103 hSteamUserStats
, name
, &set
) )
105 vg_info( " %s %s\n", (set
? "[YES]": "[ ]"), name
);
108 vg_warn( " Error while fetching achievement status '%s'\n", name
);
113 vg_warn( " Steam is not initialized, no results\n" );
117 static int steam_achievement_ccmd( int argc
, char const *argv
[] )
119 if( !(steam_ready
&& steam_stats_ready
) ) return 1;
122 if( !strcmp( argv
[0], "list" ) ){
123 steam_print_all_achievements();
126 else if( !strcmp( argv
[0], "clearall" )){
127 for( int i
=0; i
<vg_list_size(steam_achievement_names
); i
++ )
128 steam_clear_achievement( steam_achievement_names
[i
] );
130 steam_store_achievements();
135 if( !strcmp( argv
[0], "set" ) ){
136 steam_set_achievement( argv
[1] );
137 steam_store_achievements();
140 else if( strcmp( argv
[0], "clear" ) ){
141 steam_clear_achievement( argv
[1] );
142 steam_store_achievements();
150 static void steam_on_recieve_current_stats( CallbackMsg_t
*msg
)
152 UserStatsReceived_t
*rec
= (UserStatsReceived_t
*)msg
->m_pubParam
;
154 if( rec
->m_eResult
== k_EResultOK
){
155 vg_info( "Recieved stats for: %lu (user: %lu)\n", rec
->m_nGameID
,
156 rec
->m_steamIDUser
);
157 steam_stats_ready
= 1;
160 vg_error( "Error recieveing stats for user (%u)\n", rec
->m_eResult
);
164 static u32
utf8_byte0_byte_count( u8 char0
)
166 for( u32 k
=2; k
<4; k
++ ){
167 if( !(char0
& (0x80 >> k
)) )
174 static u32
str_utf8_collapse( const char *str
, char *buf
, u32 length
){
175 u8
*ustr
= (u8
*)str
;
176 u32 utf32_code
= 0x00000000;
177 u32 i
=0, j
=0, utf32_byte_ct
=0;
180 if( ustr
[i
] == 0x00 )
183 if( ustr
[i
] & 0x80 ){
186 utf32_code
|= (ustr
[i
] & 0x3F) << (utf32_byte_ct
*6);
188 if( !utf32_byte_ct
){
190 size_t chars
= anyascii( utf32_code
, &match
);
192 for( u32 k
=0; k
<VG_MIN(chars
, length
-1-j
); k
++ ){
193 buf
[ j
++ ] = (u8
)match
[k
];
198 utf32_byte_ct
= utf8_byte0_byte_count( ustr
[i
] )-1;
199 utf32_code
= ustr
[i
] & (0x3F >> utf32_byte_ct
);
200 utf32_code
<<= utf32_byte_ct
*6;
204 utf32_byte_ct
= 0x00;
215 static int steam_init(void){
216 const char *username
= "offline player";
219 vg_info( "Initializing steamworks\n" );
221 if( !SteamAPI_Init() ){
223 vg_error( "Steamworks failed to initialize\n" );
229 SteamAPI_ManualDispatch_Init();
231 /* Connect interfaces */
232 hSteamClientPipe
= SteamAPI_GetHSteamPipe();
233 hSteamNetworkingSockets
= SteamAPI_SteamNetworkingSockets_SteamAPI();
234 hSteamUser
= SteamAPI_SteamUser();
236 ISteamUtils
*utils
= SteamAPI_SteamUtils();
237 SteamAPI_ISteamUtils_SetWarningMessageHook( utils
, recv_steam_warning
);
240 vg_success( "\nSteamworks API running\n" );
242 ISteamFriends
*hSteamFriends
= SteamAPI_SteamFriends();
243 username
= SteamAPI_ISteamFriends_GetPersonaName( hSteamFriends
);
247 * --------------------------------------------------------
249 hSteamUserStats
= SteamAPI_SteamUserStats();
250 steam_register_callback( k_iUserStatsReceived
,
251 steam_on_recieve_current_stats
);
253 if( !SteamAPI_ISteamUserStats_RequestCurrentStats( hSteamUserStats
) )
254 vg_warn( "No Steam Logon: Cannot request stats\n" );
257 vg_console_reg_cmd( "ach", steam_achievement_ccmd
, NULL
);
261 /* TODO: On username update callback */
262 str_utf8_collapse( username
, steam_username_at_startup
,
263 vg_list_size(steam_username_at_startup
) );
268 static void steam_update(void)
271 steamworks_event_loop( hSteamClientPipe
);
275 static void steam_end(void)
278 vg_info( "Shutting down\n..." );