initial gui for remote players lobby
[carveJwlIkooP6JGAAIwe30JlM.git] / steam.h
1 /*
2 * Copyright (C) 2021-2023 Mt.ZERO Software, Harry Godden - All Rights Reserved
3 * All trademarks are property of their respective owners
4 */
5
6 #ifndef STEAM_H
7 #define STEAM_H
8
9 #define VG_GAME
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"
18
19 /*
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.
23 *
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
27 * nothing.
28 */
29
30 static char steam_username_at_startup[128] = "Unassigned";
31
32 static void recv_steam_warning( int severity, const char *msg )
33 {
34 if( severity == 0 )
35 vg_low( "%s\n", msg );
36 else
37 vg_info( "%s\n", msg );
38 }
39
40 static int steam_ready = 0,
41 steam_stats_ready = 0;
42
43 static void *hSteamNetworkingSockets,
44 *hSteamUser;
45
46 static ISteamUserStats *hSteamUserStats;
47 static HSteamPipe hSteamClientPipe;
48
49 static const char *steam_achievement_names[] =
50 {
51 "ALBERT", "MARC",
52 "ROUTE_MPY", "ROUTE_MPG", "ROUTE_MPB", "ROUTE_MPR",
53 "ROUTE_TO", "ROUTE_TC"
54 };
55
56 static void steam_store_achievements(void)
57 {
58 if( steam_ready && steam_stats_ready ){
59 SteamAPI_ISteamUserStats_StoreStats( hSteamUserStats );
60 }
61 }
62
63 static void steam_set_achievement( const char *name )
64 {
65 if( steam_ready && steam_stats_ready ){
66 if( SteamAPI_ISteamUserStats_SetAchievement( hSteamUserStats, name ) ){
67 vg_success( "Achievement set! '%s'\n", name );
68 }
69 else{
70 vg_warn( "Failed to set achievement: %s\n", name );
71 }
72 }
73 else{
74 vg_warn( "Failed to set achievement (steam not ready): %s\n", name );
75 }
76 }
77
78 static void steam_clear_achievement( const char *name )
79 {
80 if( steam_ready && steam_stats_ready ){
81 if( SteamAPI_ISteamUserStats_ClearAchievement( hSteamUserStats, name ) ){
82 vg_info( "Achievement cleared: '%s'\n", name );
83 }
84 else{
85 vg_warn( "Failed to clear achievement: %s\n", name );
86 }
87 }
88 else{
89 vg_warn( "Failed to clear achievement (steam not ready): %s\n", name );
90 }
91 }
92
93
94 static void steam_print_all_achievements(void){
95 vg_info( "Achievements: \n" );
96
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];
101
102 if( SteamAPI_ISteamUserStats_GetAchievement(
103 hSteamUserStats, name, &set ) )
104 {
105 vg_info( " %s %s\n", (set? "[YES]": "[ ]"), name );
106 }
107 else{
108 vg_warn( " Error while fetching achievement status '%s'\n", name );
109 }
110 }
111 }
112 else{
113 vg_warn( " Steam is not initialized, no results\n" );
114 }
115 }
116
117 static int steam_achievement_ccmd( int argc, char const *argv[] )
118 {
119 if( !(steam_ready && steam_stats_ready) ) return 1;
120
121 if( argc == 1 ){
122 if( !strcmp( argv[0], "list" ) ){
123 steam_print_all_achievements();
124 return 0;
125 }
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] );
129
130 steam_store_achievements();
131 }
132 }
133
134 if( argc == 2 ){
135 if( !strcmp( argv[0], "set" ) ){
136 steam_set_achievement( argv[1] );
137 steam_store_achievements();
138 return 0;
139 }
140 else if( strcmp( argv[0], "clear" ) ){
141 steam_clear_achievement( argv[1] );
142 steam_store_achievements();
143 return 0;
144 }
145 }
146
147 return 1;
148 }
149
150 static void steam_on_recieve_current_stats( CallbackMsg_t *msg )
151 {
152 UserStatsReceived_t *rec = (UserStatsReceived_t *)msg->m_pubParam;
153
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;
158 }
159 else{
160 vg_error( "Error recieveing stats for user (%u)\n", rec->m_eResult );
161 }
162 }
163
164 static u32 utf8_byte0_byte_count( u8 char0 )
165 {
166 for( u32 k=2; k<4; k++ ){
167 if( !(char0 & (0x80 >> k)) )
168 return k;
169 }
170
171 return 0;
172 }
173
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;
178
179 for(;j < length-1;){
180 if( ustr[i] == 0x00 )
181 break;
182
183 if( ustr[i] & 0x80 ){
184 if( utf32_byte_ct ){
185 utf32_byte_ct --;
186 utf32_code |= (ustr[i] & 0x3F) << (utf32_byte_ct*6);
187
188 if( !utf32_byte_ct ){
189 const char *match;
190 size_t chars = anyascii( utf32_code, &match );
191
192 for( u32 k=0; k<VG_MIN(chars, length-1-j); k++ ){
193 buf[ j++ ] = (u8)match[k];
194 }
195 }
196 }
197 else{
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;
201 }
202 }
203 else{
204 utf32_byte_ct = 0x00;
205 buf[j ++] = str[i];
206 }
207
208 i++;
209 }
210
211 buf[j] = 0x00;
212 return j;
213 }
214
215 static int steam_init(void){
216 const char *username = "offline player";
217
218 #ifdef SR_NETWORKED
219 vg_info( "Initializing steamworks\n" );
220
221 if( !SteamAPI_Init() ){
222 printf("\n");
223 vg_error( "Steamworks failed to initialize\n" );
224 return 1;
225 }
226
227 steam_ready = 1;
228
229 SteamAPI_ManualDispatch_Init();
230
231 /* Connect interfaces */
232 hSteamClientPipe = SteamAPI_GetHSteamPipe();
233 hSteamNetworkingSockets = SteamAPI_SteamNetworkingSockets_SteamAPI();
234 hSteamUser = SteamAPI_SteamUser();
235
236 ISteamUtils *utils = SteamAPI_SteamUtils();
237 SteamAPI_ISteamUtils_SetWarningMessageHook( utils, recv_steam_warning );
238
239 printf("\n");
240 vg_success( "\nSteamworks API running\n" );
241
242 ISteamFriends *hSteamFriends = SteamAPI_SteamFriends();
243 username = SteamAPI_ISteamFriends_GetPersonaName( hSteamFriends );
244
245 /*
246 * Request stats
247 * --------------------------------------------------------
248 */
249 hSteamUserStats = SteamAPI_SteamUserStats();
250 steam_register_callback( k_iUserStatsReceived,
251 steam_on_recieve_current_stats );
252
253 if( !SteamAPI_ISteamUserStats_RequestCurrentStats( hSteamUserStats ) )
254 vg_warn( "No Steam Logon: Cannot request stats\n" );
255
256
257 vg_console_reg_cmd( "ach", steam_achievement_ccmd, NULL );
258
259 #endif
260
261 /* TODO: On username update callback */
262 str_utf8_collapse( username, steam_username_at_startup,
263 vg_list_size(steam_username_at_startup) );
264
265 return 1;
266 }
267
268 static void steam_update(void)
269 {
270 if( steam_ready ){
271 steamworks_event_loop( hSteamClientPipe );
272 }
273 }
274
275 static void steam_end(void)
276 {
277 if( steam_ready ){
278 vg_info( "Shutting down\n..." );
279 SteamAPI_Shutdown();
280 }
281 }
282
283 #endif /* STEAM_H */