/*
- * Copyright (C) 2021-2022 Mt.ZERO Software, Harry Godden - All Rights Reserved
+ * Copyright (C) 2021-2023 Mt.ZERO Software, Harry Godden - All Rights Reserved
* All trademarks are property of their respective owners
*/
#ifndef STEAM_H
#define STEAM_H
+#define VG_GAME
#include "vg/vg_steam.h"
#include "vg/vg_steam_utils.h"
#include "vg/vg_steam_networking.h"
#include "vg/vg_steam_auth.h"
#include "vg/vg_steam_http.h"
+#include "vg/vg_steam_friends.h"
+#include "vg/vg_steam_user_stats.h"
+#include "submodules/anyascii/impl/c/anyascii.c"
/*
* We only want to use steamworks if building for the networked version,
* nothing.
*/
+static char steam_username_at_startup[128];
+
static void recv_steam_warning( int severity, const char *msg )
{
if( severity == 0 )
vg_info( "%s\n", msg );
}
-static int steam_ready = 0;
+static int steam_ready = 0,
+ steam_stats_ready = 0;
+
static void *hSteamNetworkingSockets,
*hSteamUser;
+static ISteamUserStats *hSteamUserStats;
static HSteamPipe hSteamClientPipe;
-static int steam_init(void)
+static const char *steam_achievement_names[] =
+{
+ "ALBERT", "MARC",
+ "ROUTE_MPY", "ROUTE_MPG", "ROUTE_MPB", "ROUTE_MPR",
+ "ROUTE_TO", "ROUTE_TC"
+};
+
+static void steam_store_achievements(void)
+{
+ if( steam_ready && steam_stats_ready ){
+ SteamAPI_ISteamUserStats_StoreStats( hSteamUserStats );
+ }
+}
+
+static void steam_set_achievement( const char *name )
{
+ if( steam_ready && steam_stats_ready ){
+ if( SteamAPI_ISteamUserStats_SetAchievement( hSteamUserStats, name ) ){
+ vg_success( "Achievement set! '%s'\n", name );
+ }
+ else{
+ vg_warn( "Failed to set achievement: %s\n", name );
+ }
+ }
+ else{
+ vg_warn( "Failed to set achievement (steam not ready): %s\n", name );
+ }
+}
+
+static void steam_clear_achievement( const char *name )
+{
+ if( steam_ready && steam_stats_ready ){
+ if( SteamAPI_ISteamUserStats_ClearAchievement( hSteamUserStats, name ) ){
+ vg_info( "Achievement cleared: '%s'\n", name );
+ }
+ else{
+ vg_warn( "Failed to clear achievement: %s\n", name );
+ }
+ }
+ else{
+ vg_warn( "Failed to clear achievement (steam not ready): %s\n", name );
+ }
+}
+
+
+static void steam_print_all_achievements(void){
+ vg_info( "Achievements: \n" );
+
+ if( steam_ready && steam_stats_ready ){
+ for( int i=0; i<vg_list_size(steam_achievement_names); i++ ){
+ steamapi_bool set = 0;
+ const char *name = steam_achievement_names[i];
+
+ if( SteamAPI_ISteamUserStats_GetAchievement(
+ hSteamUserStats, name, &set ) )
+ {
+ vg_info( " %s %s\n", (set? "[YES]": "[ ]"), name );
+ }
+ else{
+ vg_warn( " Error while fetching achievement status '%s'\n", name );
+ }
+ }
+ }
+ else{
+ vg_warn( " Steam is not initialized, no results\n" );
+ }
+}
+
+static int steam_achievement_ccmd( int argc, char const *argv[] )
+{
+ if( !(steam_ready && steam_stats_ready) ) return 1;
+
+ if( argc == 1 ){
+ if( !strcmp( argv[0], "list" ) ){
+ steam_print_all_achievements();
+ return 0;
+ }
+ else if( !strcmp( argv[0], "clearall" )){
+ for( int i=0; i<vg_list_size(steam_achievement_names); i++ )
+ steam_clear_achievement( steam_achievement_names[i] );
+
+ steam_store_achievements();
+ }
+ }
+
+ if( argc == 2 ){
+ if( !strcmp( argv[0], "set" ) ){
+ steam_set_achievement( argv[1] );
+ steam_store_achievements();
+ return 0;
+ }
+ else if( strcmp( argv[0], "clear" ) ){
+ steam_clear_achievement( argv[1] );
+ steam_store_achievements();
+ return 0;
+ }
+ }
+
+ return 1;
+}
+
+static void steam_on_recieve_current_stats( CallbackMsg_t *msg )
+{
+ UserStatsReceived_t *rec = (UserStatsReceived_t *)msg->m_pubParam;
+
+ if( rec->m_eResult == k_EResultOK ){
+ vg_info( "Recieved stats for: %lu (user: %lu)\n", rec->m_nGameID,
+ rec->m_steamIDUser );
+ steam_stats_ready = 1;
+ }
+ else{
+ vg_error( "Error recieveing stats for user (%u)\n", rec->m_eResult );
+ }
+}
+
+static u32 utf8_byte0_byte_count( u8 char0 )
+{
+ for( u32 k=2; k<4; k++ ){
+ if( !(char0 & (0x80 >> k)) )
+ return k;
+ }
+
+ return 0;
+}
+
+static u32 str_utf8_collapse( const char *str, char *buf, u32 length ){
+ u8 *ustr = (u8 *)str;
+ u32 utf32_code = 0x00000000;
+ u32 i=0, j=0, utf32_byte_ct=0;
+
+ for(;j < length-1;){
+ if( ustr[i] == 0x00 )
+ break;
+
+ if( ustr[i] & 0x80 ){
+ if( utf32_byte_ct ){
+ utf32_byte_ct --;
+ utf32_code |= (ustr[i] & 0x3F) << (utf32_byte_ct*6);
+
+ if( !utf32_byte_ct ){
+ const char *match;
+ size_t chars = anyascii( utf32_code, &match );
+
+ for( u32 k=0; k<VG_MIN(chars, length-1-j); k++ ){
+ buf[ j++ ] = (u8)match[k];
+ }
+ }
+ }
+ else{
+ utf32_byte_ct = utf8_byte0_byte_count( ustr[i] )-1;
+ utf32_code = ustr[i] & (0x3F >> utf32_byte_ct);
+ utf32_code <<= utf32_byte_ct*6;
+ }
+ }
+ else{
+ utf32_byte_ct = 0x00;
+ buf[j ++] = str[i];
+ }
+
+ i++;
+ }
+
+ buf[j] = 0x00;
+ return j;
+}
+
+static int steam_init(void){
+ const char *username = "offline player";
+
#ifdef SR_NETWORKED
vg_info( "Initializing steamworks\n" );
- if( !SteamAPI_Init() )
- {
+ if( !SteamAPI_Init() ){
printf("\n");
vg_error( "Steamworks failed to initialize\n" );
return 1;
printf("\n");
vg_success( "\nSteamworks API running\n" );
+
+ ISteamFriends *hSteamFriends = SteamAPI_SteamFriends();
+ username = SteamAPI_ISteamFriends_GetPersonaName( hSteamFriends );
+
+ /*
+ * Request stats
+ * --------------------------------------------------------
+ */
+ hSteamUserStats = SteamAPI_SteamUserStats();
+ steam_register_callback( k_iUserStatsReceived,
+ steam_on_recieve_current_stats );
+
+ if( !SteamAPI_ISteamUserStats_RequestCurrentStats( hSteamUserStats ) )
+ vg_warn( "No Steam Logon: Cannot request stats\n" );
+
+
+ vg_console_reg_cmd( "ach", steam_achievement_ccmd, NULL );
+
#endif
+ /* TODO: On username update callback */
+ str_utf8_collapse( username, steam_username_at_startup,
+ vg_list_size(steam_username_at_startup) );
+
return 1;
}
static void steam_update(void)
{
- if( steam_ready )
+ if( steam_ready ){
steamworks_event_loop( hSteamClientPipe );
+ }
}
-static void steam_end(void *nothing)
+static void steam_end(void)
{
- if( steam_ready )
- {
+ if( steam_ready ){
vg_info( "Shutting down\n..." );
SteamAPI_Shutdown();
}