render fix, menu
[carveJwlIkooP6JGAAIwe30JlM.git] / ent_skateshop.c
index 708e6483e40e9bad3edc4f365dff61b913473220..92e7660255ef06f813b800bb0f4e6dd79e4f8c96 100644 (file)
@@ -1,10 +1,14 @@
 #ifndef ENT_SKATESHOP_C
 #define ENT_SKATESHOP_C
 
+#define VG_GAME
+#include "vg/vg.h"
+#include "vg/vg_steam_ugc.h"
 #include "ent_skateshop.h"
 #include "world.h"
 #include "player.h"
 #include "gui.h"
+#include "menu.h"
 
 /*
  * Checks string equality but does a hash check first
@@ -202,6 +206,74 @@ VG_STATIC void workshop_async_reg_update( void *data, u32 size )
    global_skateshop.registry_count = global_skateshop.t1_registry_count;
 }
 
+VG_STATIC void workshop_steam_scan(void)
+{
+   /*
+    * Steam workshop scan
+    */
+   vg_info( "Scanning steam workshop for boards\n" );
+   PublishedFileId_t workshop_ids[ SKATESHOP_REGISTRY_MAX ];
+   u32 workshop_count = SKATESHOP_REGISTRY_MAX;
+
+   vg_async_item *call = vg_async_alloc(
+                           sizeof(struct async_workshop_installed_files_info));
+   struct async_workshop_installed_files_info *info = call->payload;
+   info->buffer = workshop_ids;
+   info->len = &workshop_count;
+   vg_async_dispatch( call, async_workshop_get_installed_files );
+   vg_async_stall();
+
+   for( u32 j=0; j<workshop_count; j++ ){
+      PublishedFileId_t id = workshop_ids[j];
+
+      for( u32 i=0; i<global_skateshop.t1_registry_count; i++ ){
+         struct registry_board *reg = &global_skateshop.registry[i];
+
+         if( reg->workshop_id == id ){
+            reg->state = k_registry_board_state_indexed;
+            goto next_file_workshop;
+         }
+      }
+
+      if( global_skateshop.t1_registry_count == SKATESHOP_REGISTRY_MAX ){
+         vg_error( "You have too many boards installed!\n" );
+         break;
+      }
+
+      vg_info( "new listing from the steam workshop!: "PRINTF_U64"\n", id );
+
+      struct registry_board *reg = &global_skateshop.registry[
+                                       global_skateshop.t1_registry_count ++ ];
+
+      reg->cache_ptr = NULL;
+      snprintf( reg->filename, 64, PRINTF_U64, id );
+      reg->filename_hash = vg_strdjb2( reg->filename );
+      reg->workshop_id = id;
+      reg->state = k_registry_board_state_indexed;
+
+      workshop_file_info_clear( &reg->workshop );
+      strcpy( reg->workshop.title, "Workshop file" );
+
+      /* load the metadata off the disk */
+      vg_async_item *call = 
+         vg_async_alloc( sizeof(struct async_workshop_filepath_info) );
+
+      const char *meta_file = "/board.mdl.inf";
+      char path[ 1024 ];
+      struct async_workshop_filepath_info *info = call->payload;
+      info->buf = path;
+      info->id = reg->workshop_id;
+      info->len = vg_list_size(path) - strlen(meta_file)-1;
+      vg_async_dispatch( call, async_workshop_get_filepath );
+      vg_async_stall(); /* too bad! */
+
+      strcat( path, meta_file );
+      workshop_load_metadata( path, &reg->workshop );
+
+next_file_workshop:;
+   }
+}
+
 /*
  * Async thread which scans local files for boards, as well as scheduling 
  * synchronous calls to the workshop
@@ -250,9 +322,13 @@ VG_STATIC void workshop_scan_thread( void *_args )
 
          reg->cache_ptr = NULL;
          vg_strncpy( file.name, reg->filename, 64, k_strncpy_always_add_null );
+         vg_strncpy( file.name, reg->workshop.title,
+                     64, k_strncpy_always_add_null );
          reg->filename_hash = hash;
          reg->workshop_id = 0;
          reg->state = k_registry_board_state_indexed;
+         reg->workshop.author = 0;
+         strcpy( reg->workshop.author_name, "custom" );
       }
 
 next_file: tinydir_next( &dir );
@@ -260,51 +336,7 @@ next_file: tinydir_next( &dir );
 
    tinydir_close(&dir);
 
-   /*
-    * Steam workshop scan
-    */
-   vg_info( "Scanning steam workshop for boards\n" );
-   PublishedFileId_t workshop_ids[ SKATESHOP_REGISTRY_MAX ];
-   u32 workshop_count = SKATESHOP_REGISTRY_MAX;
-
-   vg_async_item *call = vg_async_alloc(
-                           sizeof(struct async_workshop_installed_files_info));
-   struct async_workshop_installed_files_info *info = call->payload;
-   info->buffer = workshop_ids;
-   info->len = &workshop_count;
-   vg_async_dispatch( call, async_workshop_get_installed_files );
-   vg_async_stall();
-
-   for( u32 j=0; j<workshop_count; j++ ){
-      PublishedFileId_t id = workshop_ids[j];
-
-      for( u32 i=0; i<global_skateshop.t1_registry_count; i++ ){
-         struct registry_board *reg = &global_skateshop.registry[i];
-
-         if( reg->workshop_id == id ){
-            reg->state = k_registry_board_state_indexed;
-            goto next_file_workshop;
-         }
-      }
-
-      if( global_skateshop.t1_registry_count == SKATESHOP_REGISTRY_MAX ){
-         vg_error( "You have too many boards installed!\n" );
-         break;
-      }
-
-      vg_info( "new listing from the steam workshop!: "PRINTF_U64"\n", id );
-
-      struct registry_board *reg = &global_skateshop.registry[
-                                       global_skateshop.t1_registry_count ++ ];
-
-      reg->cache_ptr = NULL;
-      snprintf( reg->filename, 64, PRINTF_U64, id );
-      reg->filename_hash = vg_strdjb2( reg->filename );
-      reg->workshop_id = id;
-      reg->state = k_registry_board_state_indexed;
-
-next_file_workshop:;
-   }
+   if( steam_ready ) workshop_steam_scan();
    
    vg_async_call( workshop_async_reg_update, NULL, 0 );
    vg_async_stall();
@@ -348,6 +380,27 @@ VG_STATIC void unwatch_cache_board( struct cache_board *ptr )
    ptr->ref_count --;
 }
 
+/*
+ * Callback handler for persona state changes,
+ * it sets the author names on the registries
+ */
+VG_STATIC void callback_persona_statechange( CallbackMsg_t *msg )
+{
+   PersonaStateChange_t *info = (PersonaStateChange_t *)msg->m_pubParam;
+   ISteamFriends *hSteamFriends = SteamAPI_SteamFriends();
+
+   if( info->m_nChangeFlags & k_EPersonaChangeName ){
+      for( u32 i=0; i<global_skateshop.registry_count; i++ ){
+         struct registry_board *reg = &global_skateshop.registry[i];
+         if( reg->workshop.author == info->m_ulSteamID ){
+            const char *name = SteamAPI_ISteamFriends_GetFriendPersonaName( 
+                                    hSteamFriends, info->m_ulSteamID );
+            str_utf8_collapse( name, reg->workshop.author_name, 32 );
+         }
+      }
+   }
+}
+
 /*
  * VG event init
  */
@@ -369,6 +422,11 @@ VG_STATIC void skateshop_init(void)
       board->last_use_time = -99999.9;
       board->ref_count = 0;
    }
+
+   if( steam_ready ){
+      steam_register_callback( k_iPersonaStateChange, 
+                               callback_persona_statechange );
+   }
 }
 
 VG_STATIC struct cache_board *skateshop_selected_cache_if_loaded(void)
@@ -460,8 +518,6 @@ VG_STATIC void global_skateshop_preupdate(void)
    }
 
    if( moved ){
-      vg_info( "Select registry: %u\n", 
-               global_skateshop.selected_registry_id );
       global_skateshop.interaction_cooldown = 0.125f;
       return;
    }
@@ -556,8 +612,10 @@ fade_out:;
    mdl_transform_m4x3( &mark_info->transform, mtext );
    mdl_transform_m4x3( &mark_rack->transform, mrack );
 
+#if 0
    const char *text_title = "Fish - Title";
    const char *text_author = "by Shaniqua";
+#endif
 
    m4x3f mlocal, mmdl;
    m4x3_identity( mlocal );
@@ -596,24 +654,31 @@ fade_out:;
    struct cache_board *cache_ptr = skateshop_selected_cache_if_loaded();
    if( !cache_ptr ) return;
 
+   struct registry_board *reg = 
+      &global_skateshop.registry[cache_ptr->registry_id];
+   struct workshop_file_info *info = &reg->workshop;
+
    /* Skin title
     * ----------------------------------------------------------------- */
+   m3x3_zero( mlocal );
    m3x3_setdiagonalv3( mlocal, (v3f){ scale, scale, thickness } );
-   mlocal[3][0] = -font3d_string_width(&world_global.font,0,text_title);
+   mlocal[3][0] = -font3d_string_width( &world_global.font, 0, info->title );
    mlocal[3][0] *= scale*0.5f;
    mlocal[3][1] = 0.1f;
    m4x3_mul( mtext, mlocal, mmdl );
-   font3d_simple_draw( &world_global.font, 0, text_title, &main_camera, mmdl );
+   font3d_simple_draw( &world_global.font, 0, info->title, &main_camera, mmdl );
 
    /* Author name
     * ----------------------------------------------------------------- */
    scale *= 0.4f;
    m3x3_setdiagonalv3( mlocal, (v3f){ scale, scale, thickness } );
-   mlocal[3][0] = -font3d_string_width(&world_global.font,0,text_author);
+   mlocal[3][0] = -font3d_string_width( &world_global.font, 0, 
+                                        info->author_name );
    mlocal[3][0] *= scale*0.5f;
    mlocal[3][1] = 0.0f;
    m4x3_mul( mtext, mlocal, mmdl );
-   font3d_simple_draw( &world_global.font, 0, text_author, &main_camera, mmdl );
+   font3d_simple_draw( &world_global.font, 0, 
+                       info->author_name, &main_camera, mmdl );
 }
 
 /*
@@ -625,6 +690,8 @@ VG_STATIC void ent_skateshop_call( world_instance *world, ent_call *call )
    ent_skateshop *shop = mdl_arritm( &world->ent_skateshop, index );
    vg_info( "skateshop_call\n" );
 
+   if( menu.active ) return;
+
    if( call->function == k_ent_function_trigger ){
       if( localplayer.subsystem != k_player_subsystem_walk ){
          return;
@@ -633,6 +700,7 @@ VG_STATIC void ent_skateshop_call( world_instance *world, ent_call *call )
       vg_info( "Entering skateshop\n" );
 
       localplayer.immobile = 1;
+      menu.disable_open = 1;
       global_skateshop.active = 1;
 
       v3_zero( localplayer.rb.v );
@@ -653,6 +721,7 @@ VG_STATIC void global_skateshop_exit(void)
    vg_info( "exit skateshop\n" );
    localplayer.immobile = 0;
    global_skateshop.active = 0;
+   menu.disable_open = 0;
    srinput.ignore_input_frames = 2;
 }