1 #include "vg/vg_steam.h"
2 #include "vg/vg_steam_utils.h"
3 #include "vg/vg_steam_networking.h"
4 #include "vg/vg_steam_auth.h"
5 #include "vg/vg_steam_http.h"
6 #include "vg/vg_steam_friends.h"
7 #include "vg/vg_steam_user_stats.h"
8 #include "submodules/anyascii/impl/c/anyascii.c"
13 * We only want to use steamworks if building for the networked version,
14 * theres not much point otherwise. We mainly want steamworks for setting
15 * achievements etc.. so that includes our own server too.
17 * This file also wraps the functions and interfaces that we want to use to
18 * make them a bit easier to read, since they are the flat API they have very
19 * long names. in non-networked builds they will return default errors or do
23 char steam_username_at_startup
[128] = "Unassigned";
25 static void recv_steam_warning( int severity
, const char *msg
)
28 vg_low( "%s\n", msg
);
30 vg_info( "%s\n", msg
);
34 steam_stats_ready
= 0;
36 void *hSteamNetworkingSockets
, *hSteamUser
, *hSteamUserStats
;
37 static HSteamPipe hSteamClientPipe
;
39 static const char *steam_achievement_names
[] =
41 "ALBERT", "MARC", "JANET", "BERNADETTA",
42 "ROUTE_MPY", "ROUTE_MPG", "ROUTE_MPB", "ROUTE_MPR",
43 "ROUTE_TO", "ROUTE_TC", "CITY_COMPLETE", "MTZERO_SILVER", "MTZERO_GOLD",
47 void steam_store_achievements(void)
49 if( steam_ready
&& steam_stats_ready
){
50 SteamAPI_ISteamUserStats_StoreStats( hSteamUserStats
);
54 void update_ach_models(void);
55 void steam_set_achievement( const char *name
)
57 if( skaterift
.demo_mode
)
61 if( !strcmp(name
,"MARC") ) skaterift
.achievements
|= 0x1;
62 if( !strcmp(name
,"ALBERT") ) skaterift
.achievements
|= 0x2;
63 if( !strcmp(name
,"JANET") ) skaterift
.achievements
|= 0x4;
64 if( !strcmp(name
,"BERNADETTA") ) skaterift
.achievements
|= 0x8;
67 if( steam_ready
&& steam_stats_ready
){
68 if( SteamAPI_ISteamUserStats_SetAchievement( hSteamUserStats
, name
) ){
69 vg_success( "Achievement set! '%s'\n", name
);
73 vg_warn( "Failed to set achievement: %s\n", name
);
77 vg_warn( "Failed to set achievement (steam not ready): %s\n", name
);
81 void steam_clear_achievement( const char *name
)
83 if( steam_ready
&& steam_stats_ready
){
84 if( SteamAPI_ISteamUserStats_ClearAchievement( hSteamUserStats
, name
) ){
85 vg_info( "Achievement cleared: '%s'\n", name
);
88 vg_warn( "Failed to clear achievement: %s\n", name
);
92 vg_warn( "Failed to clear achievement (steam not ready): %s\n", name
);
97 void steam_print_all_achievements(void)
99 vg_info( "Achievements: \n" );
101 if( steam_ready
&& steam_stats_ready
){
102 for( int i
=0; i
<vg_list_size(steam_achievement_names
); i
++ ){
103 steamapi_bool set
= 0;
104 const char *name
= steam_achievement_names
[i
];
106 if( SteamAPI_ISteamUserStats_GetAchievement(
107 hSteamUserStats
, name
, &set
) )
109 vg_info( " %s %s\n", (set
? "[YES]": "[ ]"), name
);
112 vg_warn( " Error while fetching achievement status '%s'\n", name
);
117 vg_warn( " Steam is not initialized, no results\n" );
121 int steam_achievement_ccmd( int argc
, char const *argv
[] )
123 if( !(steam_ready
&& steam_stats_ready
) ) return 1;
126 if( !strcmp( argv
[0], "list" ) ){
127 steam_print_all_achievements();
130 else if( !strcmp( argv
[0], "clearall" )){
131 for( int i
=0; i
<vg_list_size(steam_achievement_names
); i
++ )
132 steam_clear_achievement( steam_achievement_names
[i
] );
134 steam_store_achievements();
139 if( !strcmp( argv
[0], "set" ) ){
140 steam_set_achievement( argv
[1] );
141 steam_store_achievements();
144 else if( strcmp( argv
[0], "clear" ) ){
145 steam_clear_achievement( argv
[1] );
146 steam_store_achievements();
154 static void steam_on_recieve_current_stats( CallbackMsg_t
*msg
)
156 UserStatsReceived_t
*rec
= (UserStatsReceived_t
*)msg
->m_pubParam
;
158 if( rec
->m_eResult
== k_EResultOK
){
159 vg_info( "Recieved stats for: %lu (user: %lu)\n", rec
->m_nGameID
,
160 rec
->m_steamIDUser
);
161 steam_stats_ready
= 1;
163 steamapi_bool set
= 0;
164 if( SteamAPI_ISteamUserStats_GetAchievement(
165 hSteamUserStats
, "MARC", &set
) ){
166 if( set
) skaterift
.achievements
|= 0x1;
168 if( SteamAPI_ISteamUserStats_GetAchievement(
169 hSteamUserStats
, "ALBERT", &set
) ){
170 if( set
) skaterift
.achievements
|= 0x2;
172 if( SteamAPI_ISteamUserStats_GetAchievement(
173 hSteamUserStats
, "JANET", &set
) ){
174 if( set
) skaterift
.achievements
|= 0x4;
176 if( SteamAPI_ISteamUserStats_GetAchievement(
177 hSteamUserStats
, "BERNADETTA", &set
) ){
178 if( set
) skaterift
.achievements
|= 0x8;
183 vg_error( "Error recieveing stats for user (%u)\n", rec
->m_eResult
);
187 static u32
utf8_byte0_byte_count( u8 char0
)
189 for( u32 k
=2; k
<4; k
++ ){
190 if( !(char0
& (0x80 >> k
)) )
197 u32
str_utf8_collapse( const char *str
, char *buf
, u32 length
)
199 u8
*ustr
= (u8
*)str
;
200 u32 utf32_code
= 0x00000000;
201 u32 i
=0, j
=0, utf32_byte_ct
=0;
204 if( ustr
[i
] == 0x00 )
207 if( ustr
[i
] & 0x80 ){
210 utf32_code
|= (ustr
[i
] & 0x3F) << (utf32_byte_ct
*6);
212 if( !utf32_byte_ct
){
214 size_t chars
= anyascii( utf32_code
, &match
);
216 for( u32 k
=0; k
<VG_MIN(chars
, length
-1-j
); k
++ ){
217 buf
[ j
++ ] = (u8
)match
[k
];
222 utf32_byte_ct
= utf8_byte0_byte_count( ustr
[i
] )-1;
223 utf32_code
= ustr
[i
] & (0x3F >> utf32_byte_ct
);
224 utf32_code
<<= utf32_byte_ct
*6;
228 utf32_byte_ct
= 0x00;
241 const char *username
= "offline player";
243 vg_info( "Initializing steamworks\n" );
245 if( !SteamAPI_Init() ){
247 vg_error( "Steamworks failed to initialize\n" );
253 SteamAPI_ManualDispatch_Init();
255 /* Connect interfaces */
256 hSteamClientPipe
= SteamAPI_GetHSteamPipe();
257 hSteamNetworkingSockets
= SteamAPI_SteamNetworkingSockets_SteamAPI();
258 hSteamUser
= SteamAPI_SteamUser();
260 ISteamUtils
*utils
= SteamAPI_SteamUtils();
261 SteamAPI_ISteamUtils_SetWarningMessageHook( utils
, recv_steam_warning
);
264 vg_success( "\nSteamworks API running\n" );
266 ISteamFriends
*hSteamFriends
= SteamAPI_SteamFriends();
267 username
= SteamAPI_ISteamFriends_GetPersonaName( hSteamFriends
);
271 * --------------------------------------------------------
273 hSteamUserStats
= SteamAPI_SteamUserStats();
274 steam_register_callback( k_iUserStatsReceived
,
275 steam_on_recieve_current_stats
);
277 if( !SteamAPI_ISteamUserStats_RequestCurrentStats( hSteamUserStats
) )
278 vg_warn( "No Steam Logon: Cannot request stats\n" );
281 vg_console_reg_cmd( "ach", steam_achievement_ccmd
, NULL
);
283 /* TODO: On username update callback */
284 str_utf8_collapse( username
, steam_username_at_startup
,
285 vg_list_size(steam_username_at_startup
) );
290 void steam_update(void)
293 steamworks_event_loop( hSteamClientPipe
);
300 vg_info( "Shutting down\n..." );