stuff
[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 #include "vg/vg_steam.h"
10 #include "vg/vg_steam_utils.h"
11 #include "vg/vg_steam_networking.h"
12 #include "vg/vg_steam_auth.h"
13 #include "vg/vg_steam_http.h"
14 #include "vg/vg_steam_friends.h"
15 #include "vg/vg_steam_user_stats.h"
16
17 /*
18 * We only want to use steamworks if building for the networked version,
19 * theres not much point otherwise. We mainly want steamworks for setting
20 * achievements etc.. so that includes our own server too.
21 *
22 * This file also wraps the functions and interfaces that we want to use to
23 * make them a bit easier to read, since they are the flat API they have very
24 * long names. in non-networked builds they will return default errors or do
25 * nothing.
26 */
27
28 VG_STATIC char steam_username_at_startup[128];
29
30 VG_STATIC void recv_steam_warning( int severity, const char *msg )
31 {
32 if( severity == 0 )
33 vg_low( "%s\n", msg );
34 else
35 vg_info( "%s\n", msg );
36 }
37
38 VG_STATIC int steam_ready = 0,
39 steam_stats_ready = 0;
40
41 VG_STATIC void *hSteamNetworkingSockets,
42 *hSteamUser;
43
44 VG_STATIC ISteamUserStats *hSteamUserStats;
45 VG_STATIC HSteamPipe hSteamClientPipe;
46
47 VG_STATIC const char *steam_achievement_names[] =
48 {
49 "ALBERT", "MARC",
50 "ROUTE_MPY", "ROUTE_MPG", "ROUTE_MPB", "ROUTE_MPR",
51 "ROUTE_TO", "ROUTE_TC"
52 };
53
54 VG_STATIC void steam_store_achievements(void)
55 {
56 if( steam_ready && steam_stats_ready )
57 {
58 SteamAPI_ISteamUserStats_StoreStats( hSteamUserStats );
59 }
60 }
61
62 VG_STATIC void steam_set_achievement( const char *name )
63 {
64 if( steam_ready && steam_stats_ready )
65 {
66 if( SteamAPI_ISteamUserStats_SetAchievement( hSteamUserStats, name ) )
67 {
68 vg_success( "Achievement set! '%s'\n", name );
69 }
70 else
71 {
72 vg_warn( "Failed to set achievement: %s\n", name );
73 }
74 }
75 else
76 {
77 vg_warn( "Failed to set achievement (steam not ready): %s\n", name );
78 }
79 }
80
81 VG_STATIC void steam_clear_achievement( const char *name )
82 {
83 if( steam_ready && steam_stats_ready )
84 {
85 if( SteamAPI_ISteamUserStats_ClearAchievement( hSteamUserStats, name ) )
86 {
87 vg_info( "Achievement cleared: '%s'\n", name );
88 }
89 else
90 {
91 vg_warn( "Failed to clear achievement: %s\n", name );
92 }
93 }
94 else
95 {
96 vg_warn( "Failed to clear achievement (steam not ready): %s\n", name );
97 }
98 }
99
100
101 VG_STATIC int steam_list_achievements( int argc, char const *argv[] )
102 {
103 vg_info( "Achievements: \n" );
104
105 if( steam_ready && steam_stats_ready )
106 {
107 for( int i=0; i<vg_list_size(steam_achievement_names); i++ )
108 {
109 int set = 0;
110 const char *name = steam_achievement_names[i];
111
112 if( SteamAPI_ISteamUserStats_GetAchievement(
113 hSteamUserStats, name, &set ) )
114 {
115 vg_info( " %s %s\n", (set? "[YES]": "[ ]"), name );
116 }
117 else
118 {
119 vg_warn( " Error while fetching achievement status '%s'\n", name );
120 }
121 }
122 }
123 else
124 {
125 vg_warn( " Steam is not initialized, no results\n" );
126 }
127
128 return 0;
129 }
130
131 VG_STATIC int steam_clear_all_achievements( int argc, char const *argv[] )
132 {
133 if( steam_ready && steam_stats_ready )
134 {
135 for( int i=0; i<vg_list_size(steam_achievement_names); i++ )
136 {
137 steam_clear_achievement( steam_achievement_names[i] );
138 }
139
140 steam_store_achievements();
141 }
142 else
143 {
144 vg_warn( "steam is not initialized, cannot clear\n" );
145 }
146
147 return 0;
148 }
149
150 VG_STATIC int steam_set_achievemnt_test( int argc, char const *argv[] )
151 {
152 if( argc < 2 )
153 return 0;
154
155 if( strcmp( argv[0], "monkey_island" ) )
156 return 0;
157
158 steam_set_achievement( argv[1] );
159 steam_store_achievements();
160
161 return 0;
162 }
163
164 VG_STATIC void steam_on_recieve_current_stats( CallbackMsg_t *msg )
165 {
166 UserStatsReceived_t *rec = (UserStatsReceived_t *)msg->m_pubParam;
167
168 if( rec->m_eResult == k_EResultOK )
169 {
170 vg_info( "Recieved stats for: %lu (user: %lu)\n", rec->m_nGameID,
171 rec->m_steamIDUser );
172 steam_stats_ready = 1;
173 }
174 else
175 {
176 vg_error( "Error recieveing stats for user (%u)\n", rec->m_eResult );
177 }
178 }
179
180 VG_STATIC int steam_init(void)
181 {
182 const char *username = NULL;
183
184 #ifdef SR_NETWORKED
185 vg_info( "Initializing steamworks\n" );
186
187 if( !SteamAPI_Init() )
188 {
189 printf("\n");
190 vg_error( "Steamworks failed to initialize\n" );
191 return 1;
192 }
193
194 steam_ready = 1;
195
196 SteamAPI_ManualDispatch_Init();
197
198 /* Connect interfaces */
199 hSteamClientPipe = SteamAPI_GetHSteamPipe();
200 hSteamNetworkingSockets = SteamAPI_SteamNetworkingSockets_SteamAPI();
201 hSteamUser = SteamAPI_SteamUser();
202
203 ISteamUtils *utils = SteamAPI_SteamUtils();
204 SteamAPI_ISteamUtils_SetWarningMessageHook( utils, recv_steam_warning );
205
206 printf("\n");
207 vg_success( "\nSteamworks API running\n" );
208
209 ISteamFriends *hSteamFriends = SteamAPI_SteamFriends();
210 username = SteamAPI_ISteamFriends_GetPersonaName( hSteamFriends );
211
212 /*
213 * Request stats
214 * --------------------------------------------------------
215 */
216 hSteamUserStats = SteamAPI_SteamUserStats();
217
218 steam_register_callback( k_iUserStatsReceived,
219 steam_on_recieve_current_stats );
220
221 if( !SteamAPI_ISteamUserStats_RequestCurrentStats( hSteamUserStats ) )
222 vg_warn( "No Steam Logon: Cannot request stats\n" );
223
224
225 vg_function_push( (struct vg_cmd)
226 {
227 .name = "ach_list",
228 .function = steam_list_achievements
229 });
230
231 vg_function_push( (struct vg_cmd)
232 {
233 .name = "ach_clear_all",
234 .function = steam_clear_all_achievements
235 });
236
237 vg_function_push( (struct vg_cmd)
238 {
239 .name = "ach_set",
240 .function = steam_set_achievemnt_test
241 });
242
243 vg_info( "Checking controller type\n" );
244
245 ISteamInput *hInput = SteamAPI_SteamInput();
246 SteamAPI_ISteamInput_Init( hInput, 0 );
247 SteamAPI_ISteamInput_RunFrame( hInput, 0 );
248 InputHandle_t joy0 = SteamAPI_ISteamInput_GetControllerForGamepadIndex(
249 hInput, 0 );
250
251 if( joy0 != 0 )
252 {
253 ESteamInputType type = SteamAPI_ISteamInput_GetInputTypeForHandle(
254 hInput, joy0 );
255
256 if( type == k_ESteamInputType_SteamController )
257 vg.gamepad_use_trackpad_look = 1;
258
259 vg_info( "Type: %d\n", type );
260 }
261 else
262 {
263 vg_warn( "none found\n" );
264 }
265
266 #endif
267
268 /* TODO: On username update callback */
269 str_utf8_collapse( username, steam_username_at_startup,
270 vg_list_size(steam_username_at_startup) );
271
272 return 1;
273 }
274
275 VG_STATIC void steam_update(void)
276 {
277 if( steam_ready )
278 steamworks_event_loop( hSteamClientPipe );
279 }
280
281 VG_STATIC void steam_end(void *nothing)
282 {
283 if( steam_ready )
284 {
285 vg_info( "Shutting down\n..." );
286 SteamAPI_Shutdown();
287 }
288 }
289
290 #endif /* STEAM_H */