LRU dummy
[carveJwlIkooP6JGAAIwe30JlM.git] / ent_skateshop.c
index 47d8de29333b9a6b4971f4ad3574ef2f5e45f8a3..2957c8f4be07e8240af807a42feb931091a49b1d 100644 (file)
@@ -14,6 +14,7 @@
 #include "highscores.h"
 #include "steam.h"
 #include "addon.h"
+#include "save.h"
 
 /*
  * Checks string equality but does a hash check first
@@ -42,6 +43,7 @@ VG_STATIC struct cache_board *skateshop_cache_fetch_board( u32 registry_index )
       }
    }
 
+#if 0
    /* lru eviction. should be a linked list maybe... */
    double min_time = 1e300;
    struct cache_board *min_board = NULL;
@@ -58,6 +60,12 @@ VG_STATIC struct cache_board *skateshop_cache_fetch_board( u32 registry_index )
          min_board = cache_ptr;
       }
    }
+#else
+
+   SDL_AtomicLock( &global_skateshop.sl_cache_access );
+   struct cache_board *min_board = lru_volatile_cache_board();
+
+#endif
 
    if( min_board ){
       if( min_board->state == k_cache_board_state_loaded ){
@@ -75,7 +83,6 @@ VG_STATIC struct cache_board *skateshop_cache_fetch_board( u32 registry_index )
 
       min_board->reg_ptr = reg;
       min_board->reg_index = registry_index;
-      min_board->last_use_time = vg.time;
       min_board->ref_count = 0;
       min_board->state = k_cache_board_state_load_request;
    }
@@ -114,7 +121,6 @@ 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;
 
    cache_ptr->reg_ptr->userdata = cache_ptr;
@@ -130,7 +136,6 @@ VG_STATIC void workshop_visibile_load_loop(void)
 {
    vg_info( "Running load loop\n" );
    char path_buf[4096];
-   vg_str folder;
 
    for( u32 i=0; i<SKATESHOP_BOARD_CACHE_MAX; i++ ){
       struct cache_board *cache_ptr = &global_skateshop.cache[i];
@@ -146,45 +151,26 @@ VG_STATIC void workshop_visibile_load_loop(void)
 
          /* continue with the request */
          SDL_AtomicUnlock( &global_skateshop.sl_cache_access );
+         addon_reg *reg = get_addon_from_index( k_workshop_file_type_board,
+                                                cache_ptr->reg_index );
+         cache_ptr->reg_ptr = reg;
 
-         cache_ptr->reg_ptr = get_addon_from_index( k_workshop_file_type_board,
-                                                    cache_ptr->reg_index );
-
-         if( cache_ptr->reg_ptr->workshop_id ){
-            vg_async_item *call = 
-               vg_async_alloc( sizeof(struct async_workshop_filepath_info) );
-
-            struct async_workshop_filepath_info *info = call->payload;
-            info->buf = path_buf;
-            info->id = cache_ptr->reg_ptr->workshop_id;
-            info->len = vg_list_size(path_buf);
-            vg_async_dispatch( call, async_workshop_get_filepath );
-            vg_async_stall(); /* too bad! */
-
-            if( path_buf[0] == '\0' ){
-               vg_error( "Failed SteamAPI_GetItemInstallInfo(" PRINTF_U64 ")\n",
-                           cache_ptr->reg_ptr->workshop_id );
-               goto file_is_broken;
-            }
+         vg_str folder;
+         vg_strnull( &folder, path_buf, 4096 );
+         if( !addon_get_content_folder( reg, &folder ) )
+            goto file_is_broken;
 
-            folder.buffer = path_buf;
-            folder.i = strlen(path_buf);
-            folder.len = 4096;
-         }
-         else{
-            vg_strnull( &folder, path_buf, 4096 );
-            vg_strcat( &folder, "boards/" );
-            vg_strcat( &folder, cache_ptr->reg_ptr->foldername );
-         }
 
          /* load content files
           * --------------------------------- */
 
          vg_str content_path = folder;
 
-         vg_msg root;
-         vg_msg_init( &root, cache_ptr->reg_ptr->metadata, 
-                      cache_ptr->reg_ptr->metadata_len );
+
+         vg_msg root = {0};
+         root.buf = reg->metadata;
+         root.len = reg->metadata_len;
+         root.max = sizeof(reg->metadata);
 
          const char *kv_content = vg_msg_seekkvstr( &root, "content", 0 );
          if( kv_content ){
@@ -232,13 +218,17 @@ VG_STATIC void skateshop_op_world_scan(void){
    vg_loader_start( world_scan_thread, NULL );
 }
 
+VG_STATIC void board_processview_thread( void *_args ){
+   workshop_visibile_load_loop();
+   skaterift_end_op();
+}
+
 VG_STATIC void board_scan_thread( void *_args ){
    addon_mount_content_folder( k_workshop_file_type_board, "boards", ".mdl" );
    addon_mount_workshop_items();
    vg_async_call( async_addon_reg_update, NULL, 0 );
    vg_async_stall();
-   workshop_visibile_load_loop();
-   skaterift_end_op();
+   board_processview_thread(NULL);
 }
 
 VG_STATIC void skateshop_op_board_scan(void){
@@ -246,32 +236,59 @@ VG_STATIC void skateshop_op_board_scan(void){
    vg_loader_start( board_scan_thread, NULL );
 }
 
+VG_STATIC void skateshop_op_processview(void){
+   skaterift_begin_op( k_async_op_board_scan );
+   vg_loader_start( board_processview_thread, NULL );
+}
+
 /*
  * Regular stuff
  * -----------------------------------------------------------------------------
  */
 
-/* we can only keep using a viewslot pointer for multiple frames if we watch it
- * using this function */
+/* adds one more watch */
 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" );
    }
 
-   ptr->last_use_time = vg.time;
    ptr->ref_count ++;
 }
 
-/* after this is called, the calling code only has access to the pointer for the
- * duration of the rest of the frame */
+/* if after this no more watches, places back into the volatile list */
 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" );
    }
 
    ptr->ref_count --;
+   if( !ptr->ref_count ){
+      struct cache_board *head = global_skateshop.cache_head,
+                         *tail = global_skateshop.cache_tail;
+      
+      if( tail ) tail->right = ptr;
+      ptr->left = tail;
+      global_skateshop.cache_tail = ptr;
+
+      if( !head ) global_skateshop.cache_head = ptr;
+   }
 }
 
+/* retrieve oldest pointer from the volatile list (and remove it) */
+VG_STATIC struct cache_board *lru_volatile_cache_board(void){
+   struct cache_board *head = global_skateshop.cache_head,
+                      *tail = global_skateshop.cache_tail;
+
+   if( head ){
+      if( head == tail ) global_skateshop.cache_tail = NULL;
+      global_skateshop.cache_head = head->right;
+      
+      head->left = NULL;
+      head->right = NULL;
+      return head;
+   }
+   else return NULL;
+}
 
 /*
  * VG event init
@@ -281,14 +298,24 @@ VG_STATIC void skateshop_init(void){
    global_skateshop.cache = vg_linear_alloc( vg_mem.rtmemory, cache_size );
    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->reg_ptr= NULL;
-      board->reg_index = 0xffffffff;
-      board->last_use_time = -99999.9;
-      board->ref_count = 0;
+   for( i32 ib=0; ib<SKATESHOP_BOARD_CACHE_MAX; ib++ ){
+      i32 ia = ib-1, ic = ib+1;
+      struct cache_board *arr = global_skateshop.cache,
+                         *pb = &arr[ib],
+                         *pa = ia>=0? &arr[ia]: NULL,
+                         *pc = ic<SKATESHOP_BOARD_CACHE_MAX? &arr[ic]: NULL;
+      pb->left = pa;
+      pb->right = pc;
+
+      pb->state = k_cache_board_state_none;
+      pb->reg_ptr= NULL;
+      pb->reg_index = 0xffffffff;
+      pb->ref_count = 0;
    }
+
+   global_skateshop.cache_head = global_skateshop.cache;
+   global_skateshop.cache_tail = 
+      &global_skateshop.cache[SKATESHOP_BOARD_CACHE_MAX-1];
 }
 
 VG_STATIC struct cache_board *skateshop_selected_cache_if_loaded(void)
@@ -314,65 +341,61 @@ VG_STATIC struct cache_board *skateshop_selected_cache_if_loaded(void)
 VG_STATIC void pointcloud_async_end(void *_, u32 __)
 {
    pointcloud_animate( k_pointcloud_anim_opening );
-   skaterift_end_op();
 }
 
 VG_STATIC void pointcloud_clear_async(void *_, u32 __)
 {
    pointcloud.count = 0;
    pointcloud_animate( k_pointcloud_anim_opening );
-   skaterift_end_op();
 }
 
 VG_STATIC void skateshop_preview_loader_thread( void *_data )
 {
    addon_reg *reg = _data;
+
+   char path_buf[4096];
+   vg_str path;
+   vg_strnull( &path, path_buf, 4096 );
+   addon_get_content_folder( reg, &path );
+   vg_strcat( &path, "/preview.bin" );
+
+   vg_linear_clear(vg_mem.scratch);
+   u32 size;
    
-   if( reg->workshop_id ){
-      vg_error( "Workshop files unsupported\n" );
-      vg_async_call( pointcloud_clear_async, NULL, 0 );
-   }
-   else{
-      char path_buf[4096];
-      vg_str path;
-      vg_strnull( &path, path_buf, 4096 );
-      vg_strcat( &path, "maps/" );
-      vg_strcat( &path, reg->foldername );
-      vg_strcat( &path, "/preview.bin" );
-
-      vg_linear_clear(vg_mem.scratch);
-      u32 size;
-      
-      void *data = vg_file_read( vg_mem.scratch, path_buf, &size );
-      if( data ){
-         if( size < sizeof(pointcloud_buffer) ){
-            vg_async_call( pointcloud_clear_async, NULL, 0 );
-            return;
-         }
-         
-         vg_async_item *call = vg_async_alloc(size);
-         pointcloud_buffer *pcbuf = call->payload;
-         memcpy( pcbuf, data, size );
-
-         u32 point_count = (size-sizeof(pointcloud_buffer)) /
-                              sizeof(struct pointcloud_vert);
-         pcbuf->max = point_count;
-         pcbuf->count = point_count;
-         pcbuf->op = k_pointcloud_op_clear;
-
-         vg_async_dispatch( call, async_pointcloud_sub );
-         vg_async_call( pointcloud_async_end, NULL, 0 );
-      }
-      else{
+   void *data = vg_file_read( vg_mem.scratch, path_buf, &size );
+   if( data ){
+      if( size < sizeof(pointcloud_buffer) ){
          vg_async_call( pointcloud_clear_async, NULL, 0 );
+         return;
       }
+      
+      vg_async_item *call = vg_async_alloc(size);
+      pointcloud_buffer *pcbuf = call->payload;
+      memcpy( pcbuf, data, size );
+
+      u32 point_count = (size-sizeof(pointcloud_buffer)) /
+                           sizeof(struct pointcloud_vert);
+      pcbuf->max = point_count;
+      pcbuf->count = point_count;
+      pcbuf->op = k_pointcloud_op_clear;
+
+      vg_async_dispatch( call, async_pointcloud_sub );
+      vg_async_call( pointcloud_async_end, NULL, 0 );
+   }
+   else{
+      vg_async_call( pointcloud_clear_async, NULL, 0 );
    }
 }
 
+VG_STATIC void skateshop_preview_loader_thread_and_end( void *_data ){
+   skateshop_preview_loader_thread( _data );
+   skaterift_end_op();
+}
+
 VG_STATIC void skateshop_load_world_preview( addon_reg *reg )
 {
    skaterift_begin_op( k_async_op_world_load_preview );
-   vg_loader_start( skateshop_preview_loader_thread, reg );
+   vg_loader_start( skateshop_preview_loader_thread_and_end, reg );
 }
 
 /*
@@ -438,6 +461,8 @@ VG_STATIC void global_skateshop_preupdate(void)
        * ----------------------
        */
 
+      u32 opage = global_skateshop.selected_board_id/SKATESHOP_VIEW_SLOT_MAX;
+
       if( button_down( k_srbind_mleft ) ){
          if( global_skateshop.selected_board_id > 0 ){
             global_skateshop.selected_board_id --;
@@ -452,7 +477,13 @@ VG_STATIC void global_skateshop_preupdate(void)
          }
       }
 
-      if( selected_cache && button_down( k_srbind_maccept ) ){
+      u32 npage = global_skateshop.selected_board_id/SKATESHOP_VIEW_SLOT_MAX;
+
+      if( opage != npage ){
+         skateshop_update_viewpage();
+         skateshop_op_processview();
+      }
+      else if( selected_cache && button_down( k_srbind_maccept ) ){
          vg_info( "chose board from skateshop (%u)\n", 
                      global_skateshop.selected_board_id );
 
@@ -463,6 +494,7 @@ VG_STATIC void global_skateshop_preupdate(void)
          localplayer.board_view_slot = selected_cache;
          watch_cache_board( localplayer.board_view_slot );
          global_skateshop_exit();
+         skaterift_write_savedata();
          return;
       }
    }
@@ -472,21 +504,21 @@ VG_STATIC void global_skateshop_preupdate(void)
       gui_helper_action( button_display_string( k_srbind_maccept ), "pick" );
 
       if( button_down( k_srbind_mleft ) ){
-         if( cl_playermdl_id > 0 ){
-            cl_playermdl_id --;
+         if( k_playermdl_id > 0 ){
+            k_playermdl_id --;
          }
          else{
-            cl_playermdl_id = 2; /* HACK */
+            k_playermdl_id = 2; /* HACK */
          }
          temp_update_playermodel(); /* HACK */
       }
 
       if( button_down( k_srbind_mright ) ){
-         if( cl_playermdl_id+1 < 3 ){
-            cl_playermdl_id ++;
+         if( k_playermdl_id+1 < 3 ){
+            k_playermdl_id ++;
          }
          else{
-            cl_playermdl_id = 0;  /* HACK */
+            k_playermdl_id = 0;  /* HACK */
          }
          temp_update_playermodel(); /* HACK */
          /*lol*/
@@ -547,7 +579,9 @@ VG_STATIC void global_skateshop_preupdate(void)
          if( loadable && button_down( k_srbind_maccept ) ){
             vg_info( "Select rift (%u)\n", 
                       global_skateshop.selected_world_id );
-            skaterift_change_world( reg->foldername );
+            world_loader.reg = reg;
+            world_loader.override_name[0] = '\0';
+            skaterift_change_world_start();
             return;
          }
          else{
@@ -694,14 +728,21 @@ fade_out:;
    }
 
    if( global_skateshop.render.reg_id != global_skateshop.selected_board_id ){
+      global_skateshop.render.item_title = "";
+      global_skateshop.render.item_desc = "";
       addon_reg *reg = cache_ptr->reg_ptr;
-      vg_msg root;
-      vg_msg_init( &root, reg->metadata, reg->metadata_len );
+      vg_msg root = {0};
+      root.buf = reg->metadata;
+      root.len = reg->metadata_len;
+      root.max = sizeof(reg->metadata);
 
       vg_msg workshop = root;
       if( vg_msg_seekframe( &workshop, "workshop", 0 ) ){
          const char *title = vg_msg_seekkvstr( &workshop, "title", 0 );
          if( title ) global_skateshop.render.item_title = title;
+
+         const char *dsc = vg_msg_seekkvstr( &workshop, "author", 0 );
+         if( dsc ) global_skateshop.render.item_desc = dsc;
       }
 
       global_skateshop.render.reg_id = global_skateshop.selected_board_id;
@@ -726,7 +767,8 @@ fade_out:;
     * ----------------------------------------------------------------- */
    scale *= 0.4f;
    m3x3_setdiagonalv3( mlocal, (v3f){ scale, scale, thickness } );
-   mlocal[3][0] = -font3d_string_width( &gui.font, 0, "JA" );
+   mlocal[3][0] = -font3d_string_width( &gui.font, 0, 
+                                        global_skateshop.render.item_desc );
    mlocal[3][0] *= scale*0.5f;
    mlocal[3][1] = 0.0f;
    mlocal[3][2] = 0.0f;
@@ -750,10 +792,14 @@ VG_STATIC void skateshop_render_worldshop(void)
                                   mdl_entity_id_id(shop->boards.id_info));
 
    if( global_skateshop.render.world_reg != global_skateshop.selected_world_id){
+      global_skateshop.render.world_title = "";
+
       addon_reg *reg = get_addon_from_index( k_workshop_file_type_world,
                                        global_skateshop.selected_world_id );
-      vg_msg root;
-      vg_msg_init( &root, reg->metadata, reg->metadata_len );
+      vg_msg root = {0};
+      root.buf = reg->metadata;
+      root.len = reg->metadata_len;
+      root.max = sizeof(reg->metadata);
       vg_msg workshop = root;
       if( vg_msg_seekframe( &workshop, "workshop", 0 ) ){
          global_skateshop.render.world_title = vg_msg_seekkvstr( &workshop, 
@@ -787,6 +833,12 @@ VG_STATIC void skateshop_render_worldshop(void)
          vg_strcat( &subtext, "Loading..." );
       }
       else{
+         addon_reg *reg = get_addon_from_index( k_workshop_file_type_world,
+                                          global_skateshop.selected_world_id );
+
+         if( reg->workshop_id )
+            vg_strcat( &subtext, "(Workshop) " );
+
          vg_strcat( &subtext, global_skateshop.render.world_loc );
       }
    }