threading fixes for skateshop
authorhgn <hgodden00@gmail.com>
Mon, 8 May 2023 04:29:07 +0000 (05:29 +0100)
committerhgn <hgodden00@gmail.com>
Mon, 8 May 2023 04:29:07 +0000 (05:29 +0100)
ent_skateshop.c
ent_skateshop.h
player.h
player_common.c
skaterift_imgui_dev.c
workshop.c
workshop.h [new file with mode: 0644]

index e985582c4c4438a3bd04de4cc8bf5743b586999d..708e6483e40e9bad3edc4f365dff61b913473220 100644 (file)
@@ -6,6 +6,9 @@
 #include "player.h"
 #include "gui.h"
 
+/*
+ * Checks string equality but does a hash check first
+ */
 static inline int const_str_eq( u32 hash, const char *str, const char *cmp )
 {
    if( hash == vg_strdjb2(cmp) )
@@ -14,222 +17,208 @@ static inline int const_str_eq( u32 hash, const char *str, const char *cmp )
    return 0;
 }
 
-static int skateshop_workshop_name_blacklisted( u32 hash, const char *name )
+/*
+ * Get an existing cache instance, allocate a new one to be loaded, or NULL if
+ * there is no space
+ */
+VG_STATIC struct cache_board *skateshop_cache_fetch( u32 registry_index )
 {
-   if( const_str_eq( hash, name, "skaterift_fish.mdl" ) ) return 1;
-   return 0;
-}
-
-VG_STATIC void skateshop_loader_start( void (*pfn)(void *data) )
-{
-   if( global_skateshop.loading )
-      vg_fatal_error( "skateshop thread sync failure\n" );
+   struct registry_board *reg = NULL;
 
-   global_skateshop.loading = 1;
-   vg_loader_start( pfn, NULL );
-}
+   if( registry_index < global_skateshop.registry_count ){
+      reg = &global_skateshop.registry[ registry_index ];
 
-VG_STATIC void skateshop_async_post( void *payload, u32 size )
-{
-   global_skateshop.loading = 0;
-}
+      if( reg->cache_ptr ){
+         return reg->cache_ptr;
+      }
+   }
 
-VG_STATIC 
-struct dynamic_board *skateshop_lru_alloc( u32 id )
-{
+   /* lru eviction. should be a linked list maybe... */
    double min_time = 1e300;
-   struct dynamic_board *min_board = NULL;
+   struct cache_board *min_board = NULL;
 
-   for( u32 i=0; i<MAX_DYNAMIC_BOARDS; i++ ){
-      struct dynamic_board *db = &global_skateshop.dynamic_boards[i];
+   SDL_AtomicLock( &global_skateshop.sl_cache_access );
+   for( u32 i=0; i<SKATESHOP_BOARD_CACHE_MAX; i++ ){
+      struct cache_board *cache_ptr = &global_skateshop.cache[i];
 
-      if( db->state == k_dynamic_board_state_loading )
-         continue;
+      if( cache_ptr->state == k_cache_board_state_load_request ) continue;
+      if( cache_ptr->ref_count ) continue;
 
-      if( db->ref_count )
-         continue;
-
-      if( db->last_use_time < min_time ){
-         min_time = db->last_use_time;
-         min_board = db;
+      if( cache_ptr->last_use_time < min_time ){
+         min_time = cache_ptr->last_use_time;
+         min_board = cache_ptr;
       }
    }
 
    if( min_board ){
-      if( min_board->state == k_dynamic_board_state_loaded ){
-         struct board_registry *other = 
-            &global_skateshop.registry[min_board->registry_id];
+      if( min_board->state == k_cache_board_state_loaded ){
+         struct registry_board *other = 
+            &global_skateshop.registry[ min_board->registry_id ];
 
          vg_info( "Deallocating board: '%s'\n", min_board, other->filename );
 
          player_board_unload( &min_board->board );
-         other->dynamic = NULL;
+         other->cache_ptr = NULL;
       }
 
-      struct board_registry *reg = &global_skateshop.registry[id];
-
-      vg_info( "Allocating board '%s' @%p\n", reg->filename, min_board );
+      if( reg ){
+         vg_info( "Allocating board (reg:%u) '%s'\n", 
+                  registry_index, reg->filename );
+      }
+      else{
+         vg_info( "Pre-allocating board (reg:%u) 'null'\n", registry_index );
+      }
 
-      min_board->state = k_dynamic_board_state_loading;
-      min_board->registry_id = id;
+      min_board->registry_id = registry_index;
       min_board->last_use_time = vg.time;
       min_board->ref_count = 0;
+      min_board->state = k_cache_board_state_load_request;
    }
    else{
       vg_error( "No free boards to load registry!\n" );
    }
 
+   SDL_AtomicUnlock( &global_skateshop.sl_cache_access );
    return min_board;
 }
 
-VG_STATIC 
-void skateshop_board_registry_path( struct board_registry *reg, char path[256] )
+VG_STATIC void skateshop_update_viewpage(void)
 {
-   if( reg->workshop ){
-      snprintf( path, 256, "models/boards/workshop/%s/something.mdl", 
-                reg->filename );
-   }
-   else{
-      snprintf( path, 256, "models/boards/%s", reg->filename );
-   }
-}
+   u32 page = global_skateshop.selected_registry_id/SKATESHOP_VIEW_SLOT_MAX;
+   
+   for( u32 i=0; i<SKATESHOP_VIEW_SLOT_MAX; i++ ){
+      struct shop_view_slot *slot = &global_skateshop.shop_view_slots[i];
+      u32 request_id = page*SKATESHOP_VIEW_SLOT_MAX + i;
+      
+      if( slot->cache_ptr ) unwatch_cache_board( slot->cache_ptr );
 
-/* we can only keep using a viewslot pointer for multiple frames if we watch it
- * using this function */
-VG_STATIC void watch_dynamic_board( struct dynamic_board *db )
-{
-   if( db->ref_count >= 32 ){
-      vg_fatal_error( "dynamic board watch missmatch (limit is 32)\n" );
+      slot->cache_ptr = skateshop_cache_fetch( request_id );
+      if( slot->cache_ptr ) watch_cache_board( slot->cache_ptr );
    }
-
-   db->last_use_time = vg.time;
-   db->ref_count ++;
 }
 
-/* after this is called, the calling code only has access to the pointer for the
- * duration of a frame */
-VG_STATIC void unwatch_dynamic_board( struct dynamic_board *db )
+/* generic reciever */
+VG_STATIC void workshop_async_any_complete( void *data, u32 size )
 {
-   if( db->ref_count == 0 ){
-      vg_fatal_error( "dynamic board unwatch missmatch (no watchers)\n" );
-   }
-
-   db->ref_count --;
+   workshop_end_op();
 }
 
-VG_STATIC void skateshop_async_board_complete( void *payload, u32 size )
-{
-   struct dynamic_board *db = payload;
-
-   /* all possible view slots are 'listening' for this event, 
-    * which must be checked here */
-   for( u32 i=0; i<vg_list_size(global_skateshop.shop_view_slots); i++ ){
-      if( global_skateshop.shop_view_slots[i].db == db ){
-         watch_dynamic_board( db );
-      }
-   }
+/*
+ * op/subroutine: k_workshop_op_item_load
+ * -----------------------------------------------------------------------------
+ */
 
-   if( localplayer.board_view_slot == db ){
-      watch_dynamic_board( db );
-   }
-
-   db->last_use_time = vg.time;
-   db->state = k_dynamic_board_state_loaded;
+/*
+ * Reciever for board completion; only promotes the status in the main thread
+ */
+VG_STATIC void skateshop_async_board_loaded( void *payload, u32 size )
+{
+   SDL_AtomicLock( &global_skateshop.sl_cache_access );
+   struct cache_board *cache_ptr = payload;
+   cache_ptr->last_use_time = vg.time;
+   cache_ptr->state = k_cache_board_state_loaded;
 
-   struct board_registry *reg = &global_skateshop.registry[ db->registry_id ];
-   reg->dynamic = db;
+   struct registry_board *reg = 
+      &global_skateshop.registry[ cache_ptr->registry_id ];
+   reg->cache_ptr = cache_ptr;
+   SDL_AtomicUnlock( &global_skateshop.sl_cache_access );
 
    vg_success( "Async board loaded (%s)\n", reg->filename );
 }
 
-VG_STATIC void skateshop_load_requested_boards(void)
+/*
+ * Thread(or subroutine of thread), for checking view slots that weve installed.
+ * Load the model if a view slot wants it
+ */
+VG_STATIC void workshop_visibile_load_loop_thread( void *_args )
 {
-   char path[256];
-   for( u32 i=0; i<MAX_DYNAMIC_BOARDS; i++ ){
-      struct dynamic_board *db = &global_skateshop.dynamic_boards[i];
-   
-      if( db->state == k_dynamic_board_state_loading ){
-         struct board_registry *reg = 
-            &global_skateshop.registry[ db->registry_id ];
-
-         skateshop_board_registry_path( reg, path );
-         player_board_load( &db->board, path );
-         vg_async_call( skateshop_async_board_complete, db, 0 );
-      }
-   }
-}
+   char path[1024];
+   for( u32 i=0; i<SKATESHOP_BOARD_CACHE_MAX; i++ ){
+      struct cache_board *cache_ptr = &global_skateshop.cache[i];
+
+      SDL_AtomicLock( &global_skateshop.sl_cache_access );
+      if( cache_ptr->state == k_cache_board_state_load_request ){
+         if( cache_ptr->registry_id >= global_skateshop.registry_count ){
+            /* should maybe have a different value for this case */
+            cache_ptr->state = k_cache_board_state_none;
+            SDL_AtomicUnlock( &global_skateshop.sl_cache_access );
+            continue;
+         }
 
-VG_STATIC void skateshop_thread1_refresh( void *data )
-{
-   skateshop_load_requested_boards();
-   vg_async_call( skateshop_async_post, NULL, 0 );
-}
+         /* continue with the request */
+         SDL_AtomicUnlock( &global_skateshop.sl_cache_access );
 
-VG_STATIC int skateshop_use_board( int argc, const char *argv[] )
-{
-   if( global_skateshop.loading ){
-      vg_error( "Cannot use skateshop currently (loader thread missing)\n" );
-      return 0;
-   }
+         struct registry_board *reg = 
+            &global_skateshop.registry[ cache_ptr->registry_id ];
 
-   if( argc == 1 ){
-      u32 hash = vg_strdjb2( argv[0] );
+         if( reg->workshop_id ){
+            vg_async_item *call = 
+               vg_async_alloc( sizeof(struct async_workshop_filepath_info) );
 
-      for( u32 i=0; i<global_skateshop.registry_count; i++ ){
-         struct board_registry *reg = &global_skateshop.registry[i];
+            struct async_workshop_filepath_info *info = call->payload;
+            info->buf = path;
+            info->id = reg->workshop_id;
+            info->len = vg_list_size(path) - strlen("/board.mdl")-1;
+            vg_async_dispatch( call, async_workshop_get_filepath );
+            vg_async_stall(); /* too bad! */
 
-         if( const_str_eq( hash, argv[0], reg->filename ) ){
-            if( reg->dynamic ){
-               struct dynamic_board *db = reg->dynamic;
+            if( path[0] == '\0' ){
+               SDL_AtomicLock( &global_skateshop.sl_cache_access );
+               cache_ptr->state = k_cache_board_state_none;
+               SDL_AtomicUnlock( &global_skateshop.sl_cache_access );
 
-               if( db->state == k_dynamic_board_state_loaded ){
-                  localplayer.board_view_slot = db;
-                  watch_dynamic_board( db );
-               }
-               else{
-                  vg_fatal_error( "Invalid state while loading board\n" );
-               }
+               vg_error( "Failed SteamAPI_GetItemInstallInfo(" PRINTF_U64 ")\n",
+                           reg->workshop_id );
+               continue;
             }
             else{
-               if( localplayer.board_view_slot ){
-                  unwatch_dynamic_board( localplayer.board_view_slot );
-                  localplayer.board_view_slot = NULL;
-               }
-
-               struct dynamic_board *db = skateshop_lru_alloc( i );
-               localplayer.board_view_slot = db;
-               db->state = k_dynamic_board_state_loading;
-               skateshop_loader_start( skateshop_thread1_refresh );
+               strcat( path, "/board.mdl" );
             }
-            return 1;
          }
+         else{
+            snprintf( path, 256, "models/boards/%s", reg->filename );
+         }
+
+         player_board_load( &cache_ptr->board, path );
+         vg_async_call( skateshop_async_board_loaded, cache_ptr, 0 );
       }
+      else
+         SDL_AtomicUnlock( &global_skateshop.sl_cache_access );
    }
-   return 0;
+   vg_async_call( workshop_async_any_complete, NULL, 0 );
 }
 
-VG_STATIC void skateshop_use_board_suggest( int argc, const char *argv[] )
-{
-   if( argc == 1 ){
-      for( u32 i=0; i<global_skateshop.registry_count; i++ ){
-         struct board_registry *reg = &global_skateshop.registry[i];
-
-         if( reg->ghost ) continue; /* we probably can't load these */
+/*
+ * op: k_workshop_op_item_scan
+ * -----------------------------------------------------------------------------
+ */
 
-         console_suggest_score_text( reg->filename, argv[0], 0 );
-      }
-   }
+/*
+ * Reciever for scan completion. copies the registry_count back into t0
+ */
+VG_STATIC void workshop_async_reg_update( void *data, u32 size )
+{
+   vg_info( "Registry update notify\n" );
+   global_skateshop.registry_count = global_skateshop.t1_registry_count;
 }
 
-VG_STATIC void skateshop_scan_for_items( void *data )
+/*
+ * Async thread which scans local files for boards, as well as scheduling 
+ * synchronous calls to the workshop
+ */
+VG_STATIC void workshop_scan_thread( void *_args )
 {
    vg_linear_clear( vg_mem.scratch );
 
-   for( u32 i=0; i<global_skateshop.registry_count; i++ ){
-      struct board_registry *reg = &global_skateshop.registry[i];
-      reg->ghost = 1;
+   for( u32 i=0; i<global_skateshop.t1_registry_count; i++ ){
+      struct registry_board *reg = &global_skateshop.registry[i];
+      reg->state = k_registry_board_state_indexed_absent;
    }
 
+   /*
+    * Local disk scan
+    */
+   vg_info( "Scanning models/boards/*.mdl\n" );
    tinydir_dir dir;
    tinydir_open( &dir, "models/boards" );
 
@@ -240,31 +229,30 @@ VG_STATIC void skateshop_scan_for_items( void *data )
       if( file.is_reg ){
          u32 hash = vg_strdjb2( file.name );
 
-         for( u32 i=0; i<global_skateshop.registry_count; i++ ){
-            struct board_registry *reg = &global_skateshop.registry[i];
+         for( u32 i=0; i<global_skateshop.t1_registry_count; i++ ){
+            struct registry_board *reg = &global_skateshop.registry[i];
 
             if( const_str_eq( hash, file.name, reg->filename ) ){
-               reg->ghost = 0;
+               reg->state = k_registry_board_state_indexed;
                goto next_file;
             }
          }
 
-         if( global_skateshop.registry_count == MAX_LOCAL_BOARDS ){
+         if( global_skateshop.t1_registry_count == SKATESHOP_REGISTRY_MAX ){
             vg_error( "You have too many boards installed!\n" );
             break;
          }
 
          vg_info( "new listing!: %s\n", file.name );
 
-         struct board_registry *reg = &global_skateshop.registry[
-                                          global_skateshop.registry_count ++ ];
+         struct registry_board *reg = 
+            &global_skateshop.registry[global_skateshop.t1_registry_count ++];
 
-         reg->dynamic = NULL;
+         reg->cache_ptr = NULL;
          vg_strncpy( file.name, reg->filename, 64, k_strncpy_always_add_null );
          reg->filename_hash = hash;
-         reg->uid = 0;
-         reg->workshop = 0;
-         reg->ghost = 0;
+         reg->workshop_id = 0;
+         reg->state = k_registry_board_state_indexed;
       }
 
 next_file: tinydir_next( &dir );
@@ -272,92 +260,139 @@ next_file: tinydir_next( &dir );
 
    tinydir_close(&dir);
 
-   skateshop_load_requested_boards();
-   vg_async_call( skateshop_async_post, NULL, 0 );
-}
-
-VG_STATIC void global_skateshop_exit(void)
-{
-   vg_info( "exit skateshop\n" );
-   localplayer.immobile = 0;
-   global_skateshop.active = 0;
-   srinput.ignore_input_frames = 2;
-}
-
-VG_STATIC void skateshop_request_viewpage( u32 page )
-{
-   u32 slot_count = vg_list_size(global_skateshop.shop_view_slots);
-   u32 start = page * slot_count;
+   /*
+    * 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;
+         }
+      }
 
-   for( u32 i=0; i<slot_count; i++ ){
-      struct shop_view_slot *slot = &global_skateshop.shop_view_slots[i];
-      
-      if( slot->db ){
-         slot->db->ref_count --;
-         slot->db = NULL;
+      if( global_skateshop.t1_registry_count == SKATESHOP_REGISTRY_MAX ){
+         vg_error( "You have too many boards installed!\n" );
+         break;
       }
 
-      u32 reg_index = start+i;
-      if( reg_index < global_skateshop.registry_count ){
-         struct board_registry *reg = &global_skateshop.registry[ reg_index ];
+      vg_info( "new listing from the steam workshop!: "PRINTF_U64"\n", id );
 
-         if( reg->dynamic ){
-            struct dynamic_board *db = reg->dynamic;
+      struct registry_board *reg = &global_skateshop.registry[
+                                       global_skateshop.t1_registry_count ++ ];
 
-            if( db->state == k_dynamic_board_state_loaded ){
-               db->last_use_time = vg.time;
-               db->ref_count ++;
-               slot->db = db;
-            }
-            else{
-               vg_fatal_error( "Invalid state while loading page\n" );
-            }
-         }
-         else{
-            struct dynamic_board *db = skateshop_lru_alloc( reg_index );
+      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;
 
-            if( db ){
-               db->ref_count ++;
-               slot->db = db;
-            }
-         }
-      }
+next_file_workshop:;
    }
+   
+   vg_async_call( workshop_async_reg_update, NULL, 0 );
+   vg_async_stall();
+   workshop_visibile_load_loop_thread(NULL);
 }
 
-VG_STATIC void ent_skateshop_call( world_instance *world, ent_call *call )
+/*
+ * Asynchronous scan of local disk for items and add them to the registry
+ */
+VG_STATIC void workshop_op_item_scan(void)
 {
-   u32 index = mdl_entity_id_id( call->id );
-   ent_skateshop *shop = mdl_arritm( &world->ent_skateshop, index );
-   vg_info( "skateshop_call\n" );
+   workshop_begin_op( k_workshop_op_item_scan );
+   vg_loader_start( workshop_scan_thread, NULL );
+}
 
-   if( global_skateshop.loading ){
-      vg_error( "Cannot enter skateshop currently (loader thread missing)\n" );
-      return;
+/*
+ * Regular stuff
+ * -----------------------------------------------------------------------------
+ */
+
+/* we can only keep using a viewslot pointer for multiple frames if we watch it
+ * using this function */
+VG_STATIC void watch_cache_board( struct cache_board *ptr )
+{
+   if( ptr->ref_count >= 32 ){
+      vg_fatal_error( "dynamic board watch missmatch (limit is 32)\n" );
    }
 
-   if( call->function == k_ent_function_trigger ){
-      if( localplayer.subsystem != k_player_subsystem_walk ){
-         return;
-      }
-      
-      vg_info( "Entering skateshop\n" );
+   ptr->last_use_time = vg.time;
+   ptr->ref_count ++;
+}
 
-      localplayer.immobile = 1;
-      global_skateshop.active = 1;
-      global_skateshop.interface_loc = k_skateshop_loc_page__viewing;
+/* after this is called, the calling code only has access to the pointer for the
+ * duration of the rest of the frame */
+VG_STATIC void unwatch_cache_board( struct cache_board *ptr )
+{
+   if( ptr->ref_count == 0 ){
+      vg_fatal_error( "dynamic board unwatch missmatch (no watchers)\n" );
+   }
 
-      v3_zero( localplayer.rb.v );
-      v3_zero( localplayer.rb.w );
-      localplayer._walk.move_speed = 0.0f;
+   ptr->ref_count --;
+}
 
-      global_skateshop.ptr_ent = shop;
+/*
+ * VG event init
+ */
+VG_STATIC void skateshop_init(void)
+{
+   u32 reg_size   = sizeof(struct registry_board)*SKATESHOP_REGISTRY_MAX,
+       cache_size = sizeof(struct cache_board)*SKATESHOP_BOARD_CACHE_MAX;
+   
+   global_skateshop.registry = vg_linear_alloc( vg_mem.rtmemory, reg_size );
+   global_skateshop.registry_count = 0;
+   global_skateshop.cache = vg_linear_alloc( vg_mem.rtmemory, cache_size );
 
-      skateshop_request_viewpage(0);
-      skateshop_loader_start( skateshop_scan_for_items );
+   memset( global_skateshop.cache, 0, cache_size );
+
+   for( u32 i=0; i<SKATESHOP_BOARD_CACHE_MAX; i++ ){
+      struct cache_board *board = &global_skateshop.cache[i];
+      board->state = k_cache_board_state_none;
+      board->registry_id = 0xffffffff;
+      board->last_use_time = -99999.9;
+      board->ref_count = 0;
    }
 }
 
+VG_STATIC struct cache_board *skateshop_selected_cache_if_loaded(void)
+{
+   if( global_skateshop.registry_count > 0 ){
+      u32 reg_id = global_skateshop.selected_registry_id;
+      struct registry_board *reg = &global_skateshop.registry[ reg_id ];
+
+      SDL_AtomicLock( &global_skateshop.sl_cache_access );
+      if( reg->cache_ptr && 
+         (reg->cache_ptr->state == k_cache_board_state_loaded ) )
+      {
+         SDL_AtomicUnlock( &global_skateshop.sl_cache_access );
+         return reg->cache_ptr;
+      }
+      SDL_AtomicUnlock( &global_skateshop.sl_cache_access );
+   }
+
+   return NULL;
+}
+
+/*
+ * VG event preupdate 
+ */
 VG_STATIC void global_skateshop_preupdate(void)
 {
    float rate = vg_minf( 1.0f, vg.time_frame_delta * 2.0f );
@@ -386,104 +421,75 @@ VG_STATIC void global_skateshop_preupdate(void)
 
    v3_copy( ref->transform.co, localplayer.cam_override_pos );
    localplayer.cam_override_fov = ref->fov;
-
    localplayer.cam_override_strength = global_skateshop.factive;
 
+   gui_helper_action( axis_display_string( k_sraxis_mbrowse_h ), "browse" );
+   gui_helper_action( button_display_string( k_srbind_mback ), "exit" );
+
+   int moved = 0;
+   struct cache_board *selected_cache = skateshop_selected_cache_if_loaded();
+
+   if( selected_cache ){
+      gui_helper_action( button_display_string( k_srbind_maccept ), "pick" );
+   }
+
+   /*
+    * Controls
+    * ----------------------
+    */
+
    if( global_skateshop.interaction_cooldown > 0.0f ){
       global_skateshop.interaction_cooldown -= vg.time_delta;
       return;
    }
 
-   if( global_skateshop.interface_loc <= k_skateshop_loc_page__viewing ){
-
-      gui_helper_action( axis_display_string( k_sraxis_mbrowse_h ), "browse" );
-      gui_helper_action( button_display_string( k_srbind_mback ), "exit" );
-
-      u32 reg_id = global_skateshop.selected_registry_id;
-      struct board_registry *picker = &global_skateshop.registry[ reg_id ];
-
-      int pick_availible = 0;
-      if( picker->dynamic && 
-            (picker->dynamic->state == k_dynamic_board_state_loaded ) )
-      {
-         pick_availible = 1;
-         gui_helper_action( button_display_string( k_srbind_maccept ), "pick" );
+   if( button_down( k_srbind_mleft ) ){
+      if( global_skateshop.selected_registry_id > 0 ){
+         global_skateshop.selected_registry_id --;
+         moved = 1;
       }
-      
-      int moved = 0;
+   }
 
-      if( button_down( k_srbind_mleft ) ){
-         if( global_skateshop.selected_registry_id > 0 ){
-            global_skateshop.selected_registry_id --;
-            moved = 1;
-         }
+   if( button_down( k_srbind_mright ) ){
+      if( global_skateshop.selected_registry_id+1 < 
+               global_skateshop.registry_count )
+      {
+         global_skateshop.selected_registry_id ++;
+         moved = 1;
       }
+   }
 
-      if( button_down( k_srbind_mright ) ){
-         if( global_skateshop.selected_registry_id < 
-                  global_skateshop.registry_count-1 )
-         {
-            global_skateshop.selected_registry_id ++;
-            moved = 1;
-         }
-      }
+   if( moved ){
+      vg_info( "Select registry: %u\n", 
+               global_skateshop.selected_registry_id );
+      global_skateshop.interaction_cooldown = 0.125f;
+      return;
+   }
 
-      if( moved ){
-         vg_info( "Select registry: %u\n", 
+   if( selected_cache && button_down( k_srbind_maccept ) ){
+      vg_info( "chose board from skateshop (%u)\n", 
                   global_skateshop.selected_registry_id );
-         global_skateshop.interaction_cooldown = 0.125f;
-         return;
-      }
 
-      if( pick_availible && button_down( k_srbind_maccept ) ){
-         vg_info( "chose board from skateshop (%u)\n", reg_id );
-
-         if( localplayer.board_view_slot ){
-            unwatch_dynamic_board( localplayer.board_view_slot );
-            localplayer.board_view_slot = NULL;
-         }
-
-         localplayer.board_view_slot = picker->dynamic;
-         watch_dynamic_board( localplayer.board_view_slot );
-
-         global_skateshop_exit();
-         return;
+      if( localplayer.board_view_slot ){
+         unwatch_cache_board( localplayer.board_view_slot );
       }
 
-      if( button_down( k_srbind_mback ) ){
-         global_skateshop_exit();
-         return;
-      }
-   }
-}
+      localplayer.board_view_slot = selected_cache;
+      watch_cache_board( localplayer.board_view_slot );
 
-VG_STATIC void skateshop_init(void)
-{
-   global_skateshop.registry =
-      vg_linear_alloc( vg_mem.rtmemory, 
-                       sizeof(struct board_registry)*MAX_LOCAL_BOARDS );
-   global_skateshop.registry_count = 0;
-   global_skateshop.dynamic_boards =
-      vg_linear_alloc( vg_mem.rtmemory, 
-                       sizeof(struct dynamic_board)*MAX_DYNAMIC_BOARDS );
-
-   memset( global_skateshop.dynamic_boards, 0,
-            sizeof(struct dynamic_board)*MAX_DYNAMIC_BOARDS );
-
-   for( u32 i=0; i<MAX_DYNAMIC_BOARDS; i++ ){
-      struct dynamic_board *board = &global_skateshop.dynamic_boards[i];
-      board->state = k_dynamic_board_state_none;
-      board->registry_id = 0xffffffff;
-      board->last_use_time = -99999.9;
-      board->ref_count = 0;
+      global_skateshop_exit();
+      return;
    }
 
-   vg_console_reg_cmd( "use_board", 
-                       skateshop_use_board, skateshop_use_board_suggest );
-
-   //skateshop_scan_for_items(NULL);
+   if( button_down( k_srbind_mback ) ){
+      global_skateshop_exit();
+      return;
+   }
 }
 
+/*
+ * World: render event
+ */
 VG_STATIC void skateshop_render(void)
 {
    if( !global_skateshop.active ) return;
@@ -497,26 +503,35 @@ VG_STATIC void skateshop_render(void)
                                   mdl_entity_id_id(shop->id_rack)),
               *mark_display = mdl_arritm( &world->ent_marker,
                                   mdl_entity_id_id(shop->id_display));
+
+   int visibility[ SKATESHOP_VIEW_SLOT_MAX ];
+   SDL_AtomicLock( &global_skateshop.sl_cache_access );
+   for( u32 i=0; i<SKATESHOP_VIEW_SLOT_MAX; i++ ){
+      struct shop_view_slot *slot = &global_skateshop.shop_view_slots[i];
+
+      visibility[i] = 1;
+
+      if( slot->cache_ptr == NULL ) visibility[i] = 0;
+      else if( slot->cache_ptr->state != k_cache_board_state_loaded )
+         visibility[i] = 0;
+   }
+   SDL_AtomicUnlock( &global_skateshop.sl_cache_access );
    
+   /* Render loaded boards in the view slots */
    for( u32 i=0; i<slot_count; i++ ){
       struct shop_view_slot *slot = &global_skateshop.shop_view_slots[i];
-
       float selected = 0.0f;
-      
-      if( !slot->db ) 
-         goto set_fade_amt;
 
-      if( slot->db->state != k_dynamic_board_state_loaded ) 
-         goto set_fade_amt;
+      if( !visibility[i] ) goto fade_out;
 
       mdl_transform xform;
       transform_identity( &xform );
 
       xform.co[0] = -((float)i - ((float)slot_count)*0.5f)*0.45f;
-
       mdl_transform_mul( &mark_rack->transform, &xform, &xform );
 
-      if( slot->db->registry_id == global_skateshop.selected_registry_id ){
+      if( slot->cache_ptr->registry_id == 
+          global_skateshop.selected_registry_id ){
          selected = 1.0f;
       }
 
@@ -527,16 +542,16 @@ VG_STATIC void skateshop_render(void)
 
       m4x3f mmdl;
       mdl_transform_m4x3( &xform, mmdl );
-      render_board( &main_camera, world, &slot->db->board, mmdl,
+      render_board( &main_camera, world, &slot->cache_ptr->board, mmdl,
                     k_board_shader_entity );
 
-set_fade_amt:;
+fade_out:;
       float rate = 5.0f*vg.time_delta;
       slot->view_blend = vg_lerpf( slot->view_blend, selected, rate );
    }
 
    ent_marker *mark_info = mdl_arritm( &world->ent_marker, 
-                                  mdl_entity_id_id(shop->id_info));
+                                        mdl_entity_id_id(shop->id_info));
    m4x3f mtext, mrack;
    mdl_transform_m4x3( &mark_info->transform, mtext );
    mdl_transform_m4x3( &mark_rack->transform, mrack );
@@ -547,20 +562,47 @@ set_fade_amt:;
    m4x3f mlocal, mmdl;
    m4x3_identity( mlocal );
 
-   /* Skin title
-    * ----------------------------------------------------------------- */
    float scale = 0.2f,
          thickness = 0.03f;
 
+   font3d_bind( &world_global.font, &main_camera );
+   shader_model_font_uColour( (v4f){1.0f,1.0f,1.0f,1.0f} );
+
+   /* Selection counter
+    * ------------------------------------------------------------------ */
+   m3x3_zero( mlocal );
+   v3_zero( mlocal[3] );
+   mlocal[0][0] = -scale*2.0f;
+   mlocal[1][2] = -scale*2.0f;
+   mlocal[2][1] = -thickness;
+   mlocal[3][2] = -0.7f;
+   m4x3_mul( mrack, mlocal, mmdl );
+
+   if( global_skateshop.registry_count == 0 ){
+      font3d_simple_draw( &world_global.font, 0, 
+                          "Nothing installed", &main_camera, mmdl );
+   }
+   else{
+      char buf[16];
+      int i=0;
+      i+=highscore_intl( buf+i, global_skateshop.selected_registry_id+1, 3 );
+      buf[i++] = '/';
+      i+=highscore_intl( buf+i, global_skateshop.registry_count, 3 );
+      buf[i++] = '\0';
+
+      font3d_simple_draw( &world_global.font, 0, buf, &main_camera, mmdl );
+   }
+
+   struct cache_board *cache_ptr = skateshop_selected_cache_if_loaded();
+   if( !cache_ptr ) return;
+
+   /* Skin title
+    * ----------------------------------------------------------------- */
    m3x3_setdiagonalv3( mlocal, (v3f){ scale, scale, thickness } );
    mlocal[3][0] = -font3d_string_width(&world_global.font,0,text_title);
    mlocal[3][0] *= scale*0.5f;
    mlocal[3][1] = 0.1f;
    m4x3_mul( mtext, mlocal, mmdl );
-
-   font3d_bind( &world_global.font, &main_camera );
-
-   shader_model_font_uColour( (v4f){1.0f,1.0f,1.0f,1.0f} );
    font3d_simple_draw( &world_global.font, 0, text_title, &main_camera, mmdl );
 
    /* Author name
@@ -572,26 +614,46 @@ set_fade_amt:;
    mlocal[3][1] = 0.0f;
    m4x3_mul( mtext, mlocal, mmdl );
    font3d_simple_draw( &world_global.font, 0, text_author, &main_camera, mmdl );
+}
 
-   /* Selection counter
-    * ------------------------------------------------------------------ */
-   scale = 0.4f;
-   m3x3_zero( mlocal );
-   v3_zero( mlocal[3] );
-   mlocal[0][0] = -scale;
-   mlocal[1][2] = -scale;
-   mlocal[2][1] = -thickness;
-   mlocal[3][2] = -0.7f;
-   m4x3_mul( mrack, mlocal, mmdl );
+/*
+ * Entity logic: entrance event
+ */
+VG_STATIC void ent_skateshop_call( world_instance *world, ent_call *call )
+{
+   u32 index = mdl_entity_id_id( call->id );
+   ent_skateshop *shop = mdl_arritm( &world->ent_skateshop, index );
+   vg_info( "skateshop_call\n" );
 
-   char buf[16];
-   int i=0;
-   i+=highscore_intl( buf+i, global_skateshop.selected_registry_id+1, 3 );
-   buf[i++] = '/';
-   i+=highscore_intl( buf+i, global_skateshop.registry_count, 3 );
-   buf[i++] = '\0';
+   if( call->function == k_ent_function_trigger ){
+      if( localplayer.subsystem != k_player_subsystem_walk ){
+         return;
+      }
+      
+      vg_info( "Entering skateshop\n" );
+
+      localplayer.immobile = 1;
+      global_skateshop.active = 1;
 
-   font3d_simple_draw( &world_global.font, 0, buf, &main_camera, mmdl );
+      v3_zero( localplayer.rb.v );
+      v3_zero( localplayer.rb.w );
+      localplayer._walk.move_speed = 0.0f;
+      global_skateshop.ptr_ent = shop;
+      
+      skateshop_update_viewpage();
+      workshop_op_item_scan();
+   }
+}
+
+/*
+ * Entity logic: exit event
+ */
+VG_STATIC void global_skateshop_exit(void)
+{
+   vg_info( "exit skateshop\n" );
+   localplayer.immobile = 0;
+   global_skateshop.active = 0;
+   srinput.ignore_input_frames = 2;
 }
 
 #endif /* ENT_SKATESHOP_C */
index 7e2fcf220ac335be03bf5c9365aa61456871a21f..e68816861cd9a583f29d25b8f44d1954d87e19f1 100644 (file)
@@ -3,10 +3,13 @@
 
 #include "world.h"
 #include "player.h"
+#include "vg/vg_steam_remote_storage.h"
+#include "workshop.h"
 
-#define MAX_LOCAL_BOARDS 64
-#define BILL_TIN_BOARDS  1
-#define MAX_DYNAMIC_BOARDS 9
+#define SKATESHOP_REGISTRY_MAX     64
+#define SKATESHOP_BOARD_CACHE_MAX  10
+#define SKATESHOP_VIEW_SLOT_MAX    6
+#define SKATESHOP_REGISTRYID_NONE  0xffffffff
 
 struct{
    v3f look_target;
@@ -15,63 +18,53 @@ struct{
    int active;
    float factive;
 
-   enum skateshop_loc{
-      k_skateshop_loc_page__viewing,
-
-      k_skateshop_loc_select_use,
-      k_skateshop_loc_select_cancel,
-      k_skateshop_loc_select_upload,
-      k_skateshop_loc_page__selected,
-
-      k_skateshop_loc_page__upload,
-   }
-   interface_loc;
-
-   struct dynamic_board
-   {
-      enum dynamic_board_state{
-         k_dynamic_board_state_none,
-         k_dynamic_board_state_loaded,
-         k_dynamic_board_state_loading,
+   struct cache_board{
+      enum cache_board_state{
+         k_cache_board_state_none,
+         k_cache_board_state_loaded,
+         k_cache_board_state_load_request
       }
       state;
 
-      u32 ref_count;
-
       struct player_board board;
-
       u32 registry_id;
+
+      u32 ref_count;
       double last_use_time;
    }
-   *dynamic_boards;
+   *cache;
+   SDL_SpinLock sl_cache_access;
 
-   struct shop_view_slot
-   {
-      struct dynamic_board *db;
+   struct shop_view_slot{
+      struct cache_board *cache_ptr;
       float view_blend;
    }
-   shop_view_slots[6];
-
-   struct board_registry
-   {
-      int workshop;
-      u64 uid;
+   shop_view_slots[ SKATESHOP_VIEW_SLOT_MAX ];
 
-      struct dynamic_board *dynamic;
+   struct registry_board{
+      PublishedFileId_t workshop_id;
+      struct cache_board *cache_ptr;
 
-      char filename[64];   /* if workshop, string version of uid. */
+      char filename[64]; /* if workshop, string version of its published ID. */
       u32 filename_hash;
 
-      int ghost;
+      enum registry_board_state{
+         k_registry_board_state_none,
+         k_registry_board_state_indexed,
+         k_registry_board_state_indexed_absent /*was found but is now missing*/
+      }
+      state;
    } 
    *registry;
-   u32 registry_count;
-
-   int loading;
-   float interaction_cooldown;
+   u32 t1_registry_count,
+          registry_count;
 
    u32 selected_registry_id;
+   float interaction_cooldown;
 }
 static global_skateshop;
 
+VG_STATIC void watch_cache_board( struct cache_board *ptr );
+VG_STATIC void unwatch_cache_board( struct cache_board *ptr );
+
 #endif /* ENT_SKATESHOP_H */
index 0a7486013716a670bfb57de29e7c031550f29ed1..0911e8fc03380d776d84bd27e0d4759b30080476 100644 (file)
--- a/player.h
+++ b/player.h
@@ -72,7 +72,7 @@ struct player_instance
    struct player_model   *playermodel;
    struct player_ragdoll  ragdoll;
    //struct player_board   *board;
-   struct dynamic_board  *board_view_slot;
+   struct cache_board  *board_view_slot;
 
    player_pose            holdout_pose;
    float                  holdout_time;
index 5c5dda1fef5554398698b41437e16246688a9fca..f807e3a580f2213b41700d96fc85b9c7e15d73ef 100644 (file)
@@ -264,9 +264,9 @@ struct player_board *player_get_player_board( struct player_instance *player )
    struct player_board *board = NULL;
 
    if( localplayer.board_view_slot ){
-      struct dynamic_board *vs = localplayer.board_view_slot;
-      if( vs->state == k_dynamic_board_state_loaded ){
-         board = &vs->board;
+      struct cache_board *cache_view = localplayer.board_view_slot;
+      if( cache_view->state == k_cache_board_state_loaded ){
+         board = &cache_view->board;
       }
    }
 
index 383ff8c86b3fed63ac67de90f9d04231af4552b9..c51acbe082b2d05b6ba3fac8ce92b3ff6632eb04 100644 (file)
@@ -101,6 +101,30 @@ VG_STATIC void vg_gui(void)
 {
    if( !skaterift_loaded ) return;
 
-   workshop_form_gui();
+   ui_rect null;
+   ui_rect screen = { 0, 0, vg.window_x, vg.window_y };
+   ui_rect window = { 0, 0, 1000, 700 };
+   ui_rect_center( screen, window );
+   vg_ui.wants_mouse = 1;
+
+   ui_fill( window, ui_colour( k_ui_bg+1 ) );
+   ui_outline( window, 1, ui_colour( k_ui_bg+7 ) );
+
+   ui_rect title, panel;
+   ui_split_px( window, k_ui_axis_h, 28, 0, title, panel );
+   ui_fill( title, ui_colour( k_ui_bg+7 ) );
+   ui_text( title, "Workshop tool", 1, k_ui_align_middle_center, 
+            ui_colourcont(k_ui_bg+7) );
+
+   ui_rect quit_button;
+   ui_split_px( title, k_ui_axis_v, title[2]-title[3], 2, title, quit_button );
+
+   if( workshop_form.operation == k_workshop_form_op_none ){
+      if( ui_button_text( quit_button, "X", 1 ) ){
+         workshop_quit_form();
+         return;
+      }
+   }
+
    ui_dev_colourview();
 }
index 18c7c3c321d0b061f6037afa740a713a7b303314..c82ca6ef563a12167635daab87db2ab1ae3dbd9c 100644 (file)
@@ -18,7 +18,8 @@ struct workshop_form{
       char description[512];
 
       PublishedFileId_t file_id; /* 0 if not published yet */
-      ERemoteStoragePublishedFileVisibility visibility;
+
+      struct ui_dropdown_value visibility;
 
       int submit_title,       /* set if the respective controls are touched */
           submit_description,
@@ -36,14 +37,6 @@ struct workshop_form{
    }
    page;
 
-   enum workshop_form_operation{
-      k_workshop_form_op_none,
-      k_workshop_form_op_loading_model,
-      k_workshop_form_op_downloading_submission,
-      k_workshop_form_op_publishing_update,
-   }
-   operation;
-
    /* model viewer 
     * -----------------------------
     */
@@ -107,28 +100,12 @@ struct workshop_form{
 }
 static workshop_form;
 
-/*
- * Start a new operation and crash if we are already running one.
- */
-VG_STATIC void workshop_begin_op( enum workshop_form_operation op )
-{
-   if( workshop_form.operation != k_workshop_form_op_none ){
-      vg_fatal_error( "Workshop form currently executing op(%d), tried to "
-                      "start op(%d)\n", workshop_form.operation, op );
-   }
-   
-   workshop_form.operation = op;
-   vg_info( "Starting op( %d )\n", op );
-}
-
-/*
- * Finished operation, otheres can now run
- */
-VG_STATIC void workshop_end_op(void)
-{
-   vg_info( "Finishing op( %d )\n", workshop_form.operation );
-   workshop_form.operation = k_workshop_form_op_none;
-}
+static struct ui_dropdown_opt workshop_form_visibility_opts[] = {
+ { "Public",       k_ERemoteStoragePublishedFileVisibilityPublic },
+ { "Unlisted",     k_ERemoteStoragePublishedFileVisibilityUnlisted },
+ { "Friends Only", k_ERemoteStoragePublishedFileVisibilityFriendsOnly },
+ { "Private",      k_ERemoteStoragePublishedFileVisibilityPrivate },
+};
 
 /* 
  * Close the form and discard UGC query result
@@ -148,6 +125,7 @@ VG_STATIC void workshop_quit_form(void)
    }
 
    workshop_form.page = k_workshop_form_hidden;
+   workshop_end_op();
 }
 
 /*
@@ -158,6 +136,11 @@ VG_STATIC void workshop_reset_submission_data(void)
    workshop_form.submission.file_id = 0; /* assuming id of 0 is none/invalid */
    workshop_form.submission.description[0] = '\0';
    workshop_form.submission.title[0] = '\0';
+
+   workshop_form.submission.visibility.value = 
+      k_ERemoteStoragePublishedFileVisibilityPublic;
+   workshop_form.submission.visibility.index = 0;
+
    workshop_form.model_path[0] = '\0';
    player_board_unload( &workshop_form.board_model );
    workshop_form.file_intent = k_workshop_form_file_intent_none;
@@ -301,7 +284,7 @@ VG_STATIC void workshop_form_async_package_complete( void *data, u32 size )
 
    vg_info( "Setting visibility\n" );
    SteamAPI_ISteamUGC_SetItemVisibility( hSteamUGC, handle, 
-                                         workshop_form.submission.visibility );
+                                 workshop_form.submission.visibility.value );
 
    vg_info( "Submitting updates\n" );
    vg_steam_async_call *call = vg_alloc_async_steam_api_call();
@@ -390,7 +373,7 @@ VG_STATIC void on_workshop_createitem( void *data, void *user )
       if( result->m_bUserNeedsToAcceptWorkshopLegalAgreement ){
          vg_warn( "Workshop agreement currently not accepted\n" );
       }
-
+      
       workshop_package_submission( result->m_nPublishedFileId );
    }
    else{
@@ -414,7 +397,20 @@ VG_STATIC void workshop_form_async_submit_begin( void *payload, u32 size )
 {
    /* use existing file */
    if( workshop_form.submission.file_id ){
-      workshop_package_submission( workshop_form.submission.file_id );
+      if( workshop_form.submission.submit_file_and_image ){
+         workshop_package_submission( workshop_form.submission.file_id );
+      }
+      else{
+         struct workshop_package_info info;
+         info.abs_content_file[0] = '\0';
+         info.abs_content_folder[0] = '\0';
+         info.abs_preview_image[0] = '\0';
+         info.failure_reason = NULL;
+         info.publishing_file_id = workshop_form.submission.file_id;
+         info.success = 1;
+         workshop_form_async_package_complete( &info, 
+                                       sizeof(struct workshop_package_info) );
+      }
    }
    else{
       vg_steam_async_call *call = vg_alloc_async_steam_api_call();
@@ -422,7 +418,7 @@ VG_STATIC void workshop_form_async_submit_begin( void *payload, u32 size )
       call->p_handler = on_workshop_createitem;
       ISteamUGC *hSteamUGC = SteamAPI_SteamUGC();
       call->id = SteamAPI_ISteamUGC_CreateItem( hSteamUGC, SKATERIFT_APPID, 
-                                             k_EWorkshopFileTypeCommunity );
+                                                k_EWorkshopFileTypeCommunity );
    }
 }
 
@@ -631,6 +627,9 @@ VG_STATIC void workshop_op_download_and_view_submission( int result_index )
    {
       workshop_begin_op( k_workshop_form_op_downloading_submission );
       workshop_reset_submission_data();
+      workshop_form.submission.submit_description = 0;
+      workshop_form.submission.submit_file_and_image = 0;
+      workshop_form.submission.submit_title = 0;
 
       vg_strncpy( details.m_rgchDescription, 
                   workshop_form.submission.description, 
@@ -649,6 +648,14 @@ VG_STATIC void workshop_op_download_and_view_submission( int result_index )
       workshop_form.submission.file_id = details.m_nPublishedFileId;
       workshop_form.file_intent = k_workshop_form_file_intent_keep_old;
       workshop_form.page = k_workshop_form_edit;
+      workshop_form.submission.visibility.value = details.m_eVisibility;
+
+      for( i32 i=0; i<vg_list_size(workshop_form_visibility_opts); i++ ){
+         if( workshop_form_visibility_opts[i].value == details.m_eVisibility ){
+            workshop_form.submission.visibility.index = i;
+            break;
+         }
+      }
 
       if( details.m_hPreviewFile == 0 ){
          vg_error( "m_hPreviewFile is 0\n" );
@@ -663,7 +670,7 @@ VG_STATIC void workshop_op_download_and_view_submission( int result_index )
                              sizeof( struct workshop_loadpreview_info ) );
 
          snprintf( info->abs_preview_image, 1024, 
-                   "%smodels/boards/workshop/%lu.jpg", 
+                   "%smodels/boards/workshop/" PRINTF_U64 ".jpg", 
                    vg.base_path, details.m_hPreviewFile );
 
          call->p_handler = on_workshop_download_ugcpreview;
@@ -672,7 +679,8 @@ VG_STATIC void workshop_op_download_and_view_submission( int result_index )
                hSteamRemoteStorage,
                details.m_hPreviewFile, info->abs_preview_image, 1 );
 
-         vg_info( "preview file id: %lu\n", details.m_hPreviewFile );
+         vg_info( "preview file id: " PRINTF_U64 "\n", 
+               details.m_hPreviewFile );
       }
    }
    else{
@@ -944,7 +952,7 @@ VG_STATIC void workshop_changed_description( char *buf, u32 len ){
 VG_STATIC void workshop_form_gui_edit_page( ui_rect content )
 {
    ui_rect image_plane;
-   ui_split_px( content, k_ui_axis_h, 300, 0, image_plane, content );
+   ui_split( content, k_ui_axis_h, 300, 0, image_plane, content );
    ui_fill( image_plane, ui_colour( k_ui_bg+0 ) );
 
    ui_rect img_box;
@@ -1010,11 +1018,12 @@ VG_STATIC void workshop_form_gui_edit_page( ui_rect content )
    }
 
    /* file path */
-   ui_rect null, file_entry, file_button;
-   ui_split_px( content, k_ui_axis_h, 8, 0, null, content );
-   ui_split_px( content, k_ui_axis_h, 28, 0, file_entry, content );
-   ui_split_px( file_entry, k_ui_axis_v, file_entry[2]-128, 0, 
-                file_entry, file_button );
+   ui_rect null, file_entry, file_button, file_label;
+   ui_split( content, k_ui_axis_h, 8, 0, null, content );
+   ui_split( content, k_ui_axis_h, 28, 0, file_entry, content );
+   ui_split( file_entry, k_ui_axis_v, -128, 0, file_entry, file_button );
+   ui_split_label( file_entry, "File:", 1, 8, file_label, file_entry );
+   ui_text( file_label, "File:", 1, k_ui_align_middle_left, 0 );
 
    if( workshop_form.file_intent != k_workshop_form_file_intent_none ){
       ui_text( file_entry, workshop_form.model_path, 1, k_ui_align_middle_left,
@@ -1041,13 +1050,17 @@ VG_STATIC void workshop_form_gui_edit_page( ui_rect content )
    }
 
    ui_rect title_entry, label;
-   ui_split_px( content, k_ui_axis_h, 8, 0, null, content );
-   ui_split_px( content, k_ui_axis_h, 28, 0, title_entry, content );
+   ui_split( content, k_ui_axis_h, 8, 0, null, content );
+   ui_split( content, k_ui_axis_h, 28, 0, title_entry, content );
 
    const char *str_title = "Title:", *str_desc = "Description:";
-   ui_split_px( title_entry, k_ui_axis_v, 
+   ui_split( title_entry, k_ui_axis_v, 
                 ui_text_line_width(str_title)+8, 0, label, title_entry );
 
+   ui_rect vis_dropdown;
+   ui_split_ratio( title_entry, k_ui_axis_v, 0.6f, 16, 
+                   title_entry, vis_dropdown );
+
    /* title box */
    {
       struct ui_textbox_callbacks callbacks = {
@@ -1058,15 +1071,22 @@ VG_STATIC void workshop_form_gui_edit_page( ui_rect content )
                   vg_list_size(workshop_form.submission.title), 0, &callbacks );
    }
 
+   /* visibility option */
+   {
+      ui_enum( vis_dropdown, "Visibility:", workshop_form_visibility_opts,
+               4, &workshop_form.submission.visibility );
+   }
+
    /* description box */
    {
       struct ui_textbox_callbacks callbacks = {
          .change = workshop_changed_description
       };
       ui_rect desc_entry;
-      ui_split_px( content, k_ui_axis_h, 8, 0, null, content );
-      ui_split_px( content, k_ui_axis_h, 28, 0, label, content );
-      ui_split_px( content, k_ui_axis_h, 28*4, 0, desc_entry, content );
+      /* TODO: Tommora, new split_px_gap and split_px() */
+      ui_split( content, k_ui_axis_h, 8, 0, null, content );
+      ui_split( content, k_ui_axis_h, 28, 0, label, content );
+      ui_split( content, k_ui_axis_h, 28*4, 0, desc_entry, content );
       ui_text( label, str_desc, 1, k_ui_align_middle_left, 0 );
       ui_textbox( desc_entry, workshop_form.submission.description,
                   vg_list_size(workshop_form.submission.description), 
@@ -1075,8 +1095,8 @@ VG_STATIC void workshop_form_gui_edit_page( ui_rect content )
 
    /* submissionable */
    ui_rect submission_row;
-   ui_split_px( content, k_ui_axis_h, 8, 0, null, content );
-   ui_split_px( content, k_ui_axis_h, content[3]-32-8, 0, content, 
+   ui_split( content, k_ui_axis_h, 8, 0, null, content );
+   ui_split( content, k_ui_axis_h, content[3]-32-8, 0, content, 
                 submission_row  );
 
    ui_rect submission_center;
@@ -1092,17 +1112,18 @@ VG_STATIC void workshop_form_gui_edit_page( ui_rect content )
       workshop_op_submit();
    }
    if( ui_button_text( btn_right, "Cancel", 1 ) ){
-      vg_info( "left\n" );
+      workshop_form.page = k_workshop_form_open;
+      player_board_unload( &workshop_form.board_model );
+      workshop_form.file_intent = k_workshop_form_file_intent_none;
    }
 
    /* disclaimer */
-   /* TODO!! OPEN THIS LINK */
    const char *disclaimer_text = 
       "By submitting this item, you agree to the workshop terms of service";
 
    ui_rect disclaimer_row, inner, link;
-   ui_split_px( content, k_ui_axis_h, 8, 0, null, content );
-   ui_split_px( content, k_ui_axis_h, content[3]-32, 0, content, 
+   ui_split( content, k_ui_axis_h, 8, 0, null, content );
+   ui_split( content, k_ui_axis_h, content[3]-32, 0, content, 
                 disclaimer_row );
 
    ui_px btn_width = 32;
@@ -1111,8 +1132,8 @@ VG_STATIC void workshop_form_gui_edit_page( ui_rect content )
    inner[2] = ui_text_line_width( disclaimer_text ) + btn_width+8;
 
    ui_rect_center( disclaimer_row, inner );
-   ui_split_px( inner, k_ui_axis_v, inner[2]-btn_width, 0, label, btn_right);
-   ui_rect_pad( btn_right, 2 );
+   ui_split( inner, k_ui_axis_v, inner[2]-btn_width, 0, label, btn_right);
+   ui_rect_pad( btn_right, (ui_px[2]){2,2} );
 
    if( ui_button_text( btn_right, "\x91", 2 ) ){
       ISteamFriends *hSteamFriends = SteamAPI_SteamFriends();
@@ -1133,29 +1154,25 @@ VG_STATIC void workshop_form_gui_sidebar( ui_rect sidebar )
    ui_fill( sidebar, ui_colour( k_ui_bg+2 ) );
 
    ui_rect title;
-   ui_split_px( sidebar, k_ui_axis_h, 28, 0, title, sidebar );
+   ui_split( sidebar, k_ui_axis_h, 28, 0, title, sidebar );
    ui_text( title, "Your submissions", 1, k_ui_align_middle_center, 0 );
 
    ui_rect controls, btn_create_new;
-   ui_split_px( sidebar, k_ui_axis_h, 32, 0, controls, sidebar );
-   ui_split_px( sidebar, k_ui_axis_h, sidebar[3]-32, 0, 
-                sidebar, btn_create_new );
+   ui_split( sidebar, k_ui_axis_h,  32, 0, controls, sidebar );
+   ui_split( sidebar, k_ui_axis_h, -32, 0, sidebar, btn_create_new );
    ui_fill( controls, ui_colour( k_ui_bg+1 ) );
-   ui_outline( controls, -1, ui_colour( k_ui_bg+4 ) );
 
    char buf[32];
    strcpy( buf, "page " );
    int i  = 5;
 
    /* TODO: for what it is, this code is getting a bit.. special */
-   if( workshop_form.view_published_page_id )
-       i += highscore_intl( buf+i, workshop_form.view_published_page_id, 4 );
-   else     buf[ i ++ ] = '0';
-   ;
+       i += highscore_intl( buf+i, workshop_form.view_published_page_id+1, 4 );
             buf[ i ++ ] = '/';
        i += highscore_intl( buf+i, workshop_form.view_published_page_count, 4 );
             buf[ i ++ ] = '\0';
 
+   ui_rect_pad( controls, (ui_px[2]){0,4} );
    ui_rect info;
    ui_split_ratio( controls, k_ui_axis_v, 0.25f, 0, info, controls );
    ui_text( info, buf, 1, k_ui_align_middle_center, 0 );
@@ -1182,8 +1199,8 @@ VG_STATIC void workshop_form_gui_sidebar( ui_rect sidebar )
 
    for( int i=0; i<workshop_form.published_files_list_length; i++ ){
       ui_rect item;
-      ui_split_px( sidebar, k_ui_axis_h, 28, 0, item, sidebar );
-      ui_rect_pad( item, 4 );
+      ui_split( sidebar, k_ui_axis_h, 28, 0, item, sidebar );
+      ui_rect_pad( item, (ui_px[2]){4,4} );
       
       struct published_file *pfile = &workshop_form.published_files_list[i];
       if( ui_button_text( item, pfile->title, 1 ) ){
@@ -1213,15 +1230,15 @@ VG_STATIC void workshop_form_gui(void)
    ui_outline( window, 1, ui_colour( k_ui_bg+7 ) );
 
    ui_rect title, panel;
-   ui_split_px( window, k_ui_axis_h, 28, 0, title, panel );
+   ui_split( window, k_ui_axis_h, 28, 0, title, panel );
    ui_fill( title, ui_colour( k_ui_bg+7 ) );
    ui_text( title, "Workshop tool", 1, k_ui_align_middle_center, 
             ui_colourcont(k_ui_bg+7) );
 
    ui_rect quit_button;
-   ui_split_px( title, k_ui_axis_v, title[2]-title[3], 2, title, quit_button );
+   ui_split( title, k_ui_axis_v, title[2]-title[3], 2, title, quit_button );
 
-   if( workshop_form.operation == k_workshop_form_op_none ){
+   if( workshop.operation == k_workshop_form_op_none ){
       if( ui_button_text( quit_button, "X", 1 ) ){
          workshop_quit_form();
          return;
@@ -1234,11 +1251,11 @@ VG_STATIC void workshop_form_gui(void)
     * escapes here and we show them a basic string
     */
 
-   if( workshop_form.operation != k_workshop_form_op_none ){
+   if( workshop.operation != k_workshop_form_op_none ){
       const char *op_string = "The programmer has not bothered to describe "
                               "the current operation that is running.";
 
-      switch(workshop_form.operation){
+      switch(workshop.operation){
          case k_workshop_form_op_loading_model:
             op_string = "Operation in progress: Loading model file.";
          break;
@@ -1272,7 +1289,7 @@ VG_STATIC void workshop_form_gui(void)
    ui_split_ratio( panel, k_ui_axis_v, 0.3f, 1, sidebar, content );
 
    /* content page */
-   ui_rect_pad( content, 8 );
+   ui_rect_pad( content, (ui_px[2]){8,8} );
 
    if( stable_page == k_workshop_form_edit ){
       workshop_form_gui_edit_page( content );
@@ -1283,7 +1300,7 @@ VG_STATIC void workshop_form_gui(void)
    }
    else if( stable_page >= k_workshop_form_cclosing ){
       ui_rect submission_row;
-      ui_split_px( content, k_ui_axis_h, content[3]-32-8, 0, content, 
+      ui_split( content, k_ui_axis_h, content[3]-32-8, 0, content, 
                    submission_row );
 
       u32 colour;
@@ -1300,7 +1317,7 @@ VG_STATIC void workshop_form_gui(void)
       rect_copy( submission_row, submission_center );
       submission_center[2] = 128;
       ui_rect_center( submission_row, submission_center );
-      ui_rect_pad( submission_center, 8 );
+      ui_rect_pad( submission_center, (ui_px[2]){8,8} );
 
       if( ui_button_text( submission_center, "OK", 1 ) ){
          workshop_form.page = k_workshop_form_open;
@@ -1310,4 +1327,45 @@ VG_STATIC void workshop_form_gui(void)
    workshop_form_gui_sidebar( sidebar );
 }
 
+/*
+ * Some async api stuff
+ * -----------------------------------------------------------------------------
+ */
+
+VG_STATIC void async_workshop_get_filepath( void *data, u32 len )
+{
+   struct async_workshop_filepath_info *info = data;
+
+   u64 _size;
+   u32 _ts;
+
+   ISteamUGC *hSteamUGC = SteamAPI_SteamUGC();
+   if( !SteamAPI_ISteamUGC_GetItemInstallInfo( hSteamUGC, info->id, &_size,
+                                               info->buf, info->len, &_ts ))
+   {
+      info->buf[0] = '\0';
+   }
+}
+
+VG_STATIC void async_workshop_get_installed_files( void *data, u32 len )
+{
+   struct async_workshop_installed_files_info *info = data;
+
+   ISteamUGC *hSteamUGC = SteamAPI_SteamUGC();
+   u32 count = SteamAPI_ISteamUGC_GetSubscribedItems( hSteamUGC, info->buffer,
+                                                      *info->len );
+
+   vg_info( "Found %u subscribed items\n", count );
+
+   u32 j=0;
+   for( u32 i=0; i<count; i++ ){
+      u32 state = SteamAPI_ISteamUGC_GetItemState( hSteamUGC, info->buffer[i] );
+      if( state & k_EItemStateInstalled ){
+         info->buffer[j ++] = info->buffer[i];
+      }
+   }
+
+   *info->len = j;
+}
+
 #endif /* WORKSHOP_C */
diff --git a/workshop.h b/workshop.h
new file mode 100644 (file)
index 0000000..a08bc4d
--- /dev/null
@@ -0,0 +1,60 @@
+#ifndef WORKSHOP_H
+#define WORKSHOP_H
+
+#define VG_GAME
+#include "vg/vg.h"
+#include "vg/vg_steam_remote_storage.h"
+
+struct workshop{
+   enum workshop_operation{
+      k_workshop_form_op_none,
+      k_workshop_form_op_loading_model,
+      k_workshop_form_op_downloading_submission,
+      k_workshop_form_op_publishing_update,
+      k_workshop_op_item_scan,
+      k_workshop_op_item_load
+   }
+   operation;
+}
+static workshop;
+
+struct async_workshop_filepath_info{
+   PublishedFileId_t id;
+   char *buf;
+   u32 len;
+};
+
+struct async_workshop_installed_files_info{
+   PublishedFileId_t *buffer;
+   u32 *len; /* inout */
+};
+
+VG_STATIC void async_workshop_get_filepath( void *data, u32 len );
+VG_STATIC void async_workshop_get_installed_files( void *data, u32 len );
+
+/*
+ * Start a new operation and crash if we are already running one.
+ */
+VG_STATIC int workshop_begin_op( enum workshop_operation op )
+{
+   if( workshop.operation != k_workshop_form_op_none ){
+      vg_error( "Workshop form currently executing op(%d), tried to "
+                "start op(%d)\n", workshop.operation, op );
+      return 0;
+   }
+   
+   workshop.operation = op;
+   vg_info( "Starting op( %d )\n", op );
+   return 1;
+}
+
+/*
+ * Finished operation, otheres can now run
+ */
+VG_STATIC void workshop_end_op(void)
+{
+   vg_info( "Finishing op( %d )\n", workshop.operation );
+   workshop.operation = k_workshop_form_op_none;
+}
+
+#endif /* WORKSHOP_H */