dead
[carveJwlIkooP6JGAAIwe30JlM.git] / steam.h
1 /*
2 * Copyright (C) 2021-2022 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
18 /*
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.
22 *
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
26 * nothing.
27 */
28
29 VG_STATIC char steam_username_at_startup[128];
30
31 VG_STATIC void recv_steam_warning( int severity, const char *msg )
32 {
33 if( severity == 0 )
34 vg_low( "%s\n", msg );
35 else
36 vg_info( "%s\n", msg );
37 }
38
39 VG_STATIC int steam_ready = 0,
40 steam_stats_ready = 0;
41
42 VG_STATIC void *hSteamNetworkingSockets,
43 *hSteamUser;
44
45 VG_STATIC ISteamUserStats *hSteamUserStats;
46 VG_STATIC HSteamPipe hSteamClientPipe;
47
48 VG_STATIC const char *steam_achievement_names[] =
49 {
50 "ALBERT", "MARC",
51 "ROUTE_MPY", "ROUTE_MPG", "ROUTE_MPB", "ROUTE_MPR",
52 "ROUTE_TO", "ROUTE_TC"
53 };
54
55 VG_STATIC void steam_store_achievements(void)
56 {
57 if( steam_ready && steam_stats_ready )
58 {
59 SteamAPI_ISteamUserStats_StoreStats( hSteamUserStats );
60 }
61 }
62
63 VG_STATIC void steam_set_achievement( const char *name )
64 {
65 if( steam_ready && steam_stats_ready )
66 {
67 if( SteamAPI_ISteamUserStats_SetAchievement( hSteamUserStats, name ) )
68 {
69 vg_success( "Achievement set! '%s'\n", name );
70 }
71 else
72 {
73 vg_warn( "Failed to set achievement: %s\n", name );
74 }
75 }
76 else
77 {
78 vg_warn( "Failed to set achievement (steam not ready): %s\n", name );
79 }
80 }
81
82 VG_STATIC void steam_clear_achievement( const char *name )
83 {
84 if( steam_ready && steam_stats_ready )
85 {
86 if( SteamAPI_ISteamUserStats_ClearAchievement( hSteamUserStats, name ) )
87 {
88 vg_info( "Achievement cleared: '%s'\n", name );
89 }
90 else
91 {
92 vg_warn( "Failed to clear achievement: %s\n", name );
93 }
94 }
95 else
96 {
97 vg_warn( "Failed to clear achievement (steam not ready): %s\n", name );
98 }
99 }
100
101
102 VG_STATIC int steam_list_achievements( int argc, char const *argv[] )
103 {
104 vg_info( "Achievements: \n" );
105
106 if( steam_ready && steam_stats_ready )
107 {
108 for( int i=0; i<vg_list_size(steam_achievement_names); i++ )
109 {
110 int set = 0;
111 const char *name = steam_achievement_names[i];
112
113 if( SteamAPI_ISteamUserStats_GetAchievement(
114 hSteamUserStats, name, &set ) )
115 {
116 vg_info( " %s %s\n", (set? "[YES]": "[ ]"), name );
117 }
118 else
119 {
120 vg_warn( " Error while fetching achievement status '%s'\n", name );
121 }
122 }
123 }
124 else
125 {
126 vg_warn( " Steam is not initialized, no results\n" );
127 }
128
129 return 0;
130 }
131
132 VG_STATIC int steam_clear_all_achievements( int argc, char const *argv[] )
133 {
134 if( steam_ready && steam_stats_ready )
135 {
136 for( int i=0; i<vg_list_size(steam_achievement_names); i++ )
137 {
138 steam_clear_achievement( steam_achievement_names[i] );
139 }
140
141 steam_store_achievements();
142 }
143 else
144 {
145 vg_warn( "steam is not initialized, cannot clear\n" );
146 }
147
148 return 0;
149 }
150
151 VG_STATIC int steam_set_achievemnt_test( int argc, char const *argv[] )
152 {
153 if( argc < 2 )
154 return 0;
155
156 if( strcmp( argv[0], "monkey_island" ) )
157 return 0;
158
159 steam_set_achievement( argv[1] );
160 steam_store_achievements();
161
162 return 0;
163 }
164
165 VG_STATIC void steam_on_recieve_current_stats( CallbackMsg_t *msg )
166 {
167 UserStatsReceived_t *rec = (UserStatsReceived_t *)msg->m_pubParam;
168
169 if( rec->m_eResult == k_EResultOK )
170 {
171 vg_info( "Recieved stats for: %lu (user: %lu)\n", rec->m_nGameID,
172 rec->m_steamIDUser );
173 steam_stats_ready = 1;
174 }
175 else
176 {
177 vg_error( "Error recieveing stats for user (%u)\n", rec->m_eResult );
178 }
179 }
180
181 VG_STATIC ISteamInput *steam_hInput;
182
183 VG_STATIC int steam_init(void)
184 {
185 const char *username = NULL;
186
187 #ifdef SR_NETWORKED
188 vg_info( "Initializing steamworks\n" );
189
190 if( !SteamAPI_Init() )
191 {
192 printf("\n");
193 vg_error( "Steamworks failed to initialize\n" );
194 return 1;
195 }
196
197 steam_ready = 1;
198
199 SteamAPI_ManualDispatch_Init();
200
201 /* Connect interfaces */
202 hSteamClientPipe = SteamAPI_GetHSteamPipe();
203 hSteamNetworkingSockets = SteamAPI_SteamNetworkingSockets_SteamAPI();
204 hSteamUser = SteamAPI_SteamUser();
205
206 ISteamUtils *utils = SteamAPI_SteamUtils();
207 SteamAPI_ISteamUtils_SetWarningMessageHook( utils, recv_steam_warning );
208
209 printf("\n");
210 vg_success( "\nSteamworks API running\n" );
211
212 ISteamFriends *hSteamFriends = SteamAPI_SteamFriends();
213 username = SteamAPI_ISteamFriends_GetPersonaName( hSteamFriends );
214
215 /*
216 * Request stats
217 * --------------------------------------------------------
218 */
219 hSteamUserStats = SteamAPI_SteamUserStats();
220
221 steam_register_callback( k_iUserStatsReceived,
222 steam_on_recieve_current_stats );
223
224 if( !SteamAPI_ISteamUserStats_RequestCurrentStats( hSteamUserStats ) )
225 vg_warn( "No Steam Logon: Cannot request stats\n" );
226
227
228 vg_function_push( (struct vg_cmd)
229 {
230 .name = "ach_list",
231 .function = steam_list_achievements
232 });
233
234 vg_function_push( (struct vg_cmd)
235 {
236 .name = "ach_clear_all",
237 .function = steam_clear_all_achievements
238 });
239
240 vg_function_push( (struct vg_cmd)
241 {
242 .name = "ach_set",
243 .function = steam_set_achievemnt_test
244 });
245
246 steam_hInput = SteamAPI_SteamInput();
247 SteamAPI_ISteamInput_Init( steam_hInput, 0 );
248 SteamAPI_ISteamInput_RunFrame( steam_hInput, 0 );
249
250 #endif
251
252 /* TODO: On username update callback */
253 str_utf8_collapse( username, steam_username_at_startup,
254 vg_list_size(steam_username_at_startup) );
255
256 return 1;
257 }
258
259 VG_STATIC void steam_update(void)
260 {
261 if( steam_ready )
262 {
263 steamworks_event_loop( hSteamClientPipe );
264
265 /* TODO
266 * We can probably request this from SDL too
267 */
268 if( steam_hInput )
269 {
270 SteamAPI_ISteamInput_RunFrame( steam_hInput, 0 );
271
272 InputHandle_t joy0 = SteamAPI_ISteamInput_GetControllerForGamepadIndex(
273 steam_hInput, 0 );
274
275 vg_input.controller_should_use_trackpad_look = 0;
276 if( joy0 != 0 )
277 {
278 ESteamInputType type = SteamAPI_ISteamInput_GetInputTypeForHandle(
279 steam_hInput, joy0 );
280
281 if( type == k_ESteamInputType_SteamController )
282 {
283 vg_input.controller_should_use_trackpad_look = 1;
284 menu_display_controller = k_menu_controller_type_steam;
285 }
286 else if( type == k_ESteamInputType_SteamDeckController )
287 {
288 menu_display_controller = k_menu_controller_type_steam_deck;
289 }
290 else if( type == k_ESteamInputType_PS3Controller ||
291 type == k_ESteamInputType_PS4Controller ||
292 type == k_ESteamInputType_PS5Controller )
293 {
294 menu_display_controller = k_menu_controller_type_playstation;
295 }
296 else if( type == k_ESteamInputType_XBox360Controller ||
297 type == k_ESteamInputType_XBoxOneController )
298 {
299 menu_display_controller = k_menu_controller_type_xbox;
300 }
301 else
302 {
303 /* currently unsupported controller */
304 menu_display_controller = k_menu_controller_type_xbox;
305 }
306 }
307 else
308 menu_display_controller = k_menu_controller_type_keyboard;
309 }
310 }
311 }
312
313 VG_STATIC void steam_end(void)
314 {
315 if( steam_ready )
316 {
317 vg_info( "Shutting down\n..." );
318 SteamAPI_Shutdown();
319 }
320 }
321
322 #endif /* STEAM_H */