more refactors..
[carveJwlIkooP6JGAAIwe30JlM.git] / ent_skateshop.c
index 2957c8f4be07e8240af807a42feb931091a49b1d..9bee505bc1412c124330ca33a9830d673f03b4ef 100644 (file)
@@ -43,29 +43,9 @@ 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;
-
-   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( cache_ptr->state == k_cache_board_state_load_request ) continue;
-      if( cache_ptr->ref_count ) continue;
-
-      if( cache_ptr->last_use_time < min_time ){
-         min_time = cache_ptr->last_use_time;
-         min_board = cache_ptr;
-      }
-   }
-#else
-
-   SDL_AtomicLock( &global_skateshop.sl_cache_access );
-   struct cache_board *min_board = lru_volatile_cache_board();
-
-#endif
+   SDL_AtomicLock( &addon_system.sl_cache );
+   struct cache_board *min_board = 
+      vg_pool_lru( &addon_system.board_cache );
 
    if( min_board ){
       if( min_board->state == k_cache_board_state_loaded ){
@@ -83,14 +63,13 @@ 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->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 );
+   SDL_AtomicUnlock( &addon_system.sl_cache );
    return min_board;
 }
 
@@ -102,10 +81,13 @@ VG_STATIC void skateshop_update_viewpage(void)
       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 );
+      vg_pool *cache = &addon_system.board_cache;
+      if( slot->cache_ptr ) 
+         vg_pool_unwatch( cache, slot->cache_ptr );
 
       slot->cache_ptr = skateshop_cache_fetch_board( request_id );
-      if( slot->cache_ptr ) watch_cache_board( slot->cache_ptr );
+      if( slot->cache_ptr ) 
+         vg_pool_watch( cache, slot->cache_ptr );
    }
 }
 
@@ -119,12 +101,12 @@ VG_STATIC void skateshop_update_viewpage(void)
  */
 VG_STATIC void skateshop_async_board_loaded( void *payload, u32 size )
 {
-   SDL_AtomicLock( &global_skateshop.sl_cache_access );
+   SDL_AtomicLock( &addon_system.sl_cache );
    struct cache_board *cache_ptr = payload;
    cache_ptr->state = k_cache_board_state_loaded;
 
    cache_ptr->reg_ptr->userdata = cache_ptr;
-   SDL_AtomicUnlock( &global_skateshop.sl_cache_access );
+   SDL_AtomicUnlock( &addon_system.sl_cache );
    vg_success( "Async board loaded (%s)\n", cache_ptr->reg_ptr->foldername );
 }
 
@@ -137,20 +119,20 @@ VG_STATIC void workshop_visibile_load_loop(void)
    vg_info( "Running load loop\n" );
    char path_buf[4096];
 
-   for( u32 i=0; i<SKATESHOP_BOARD_CACHE_MAX; i++ ){
-      struct cache_board *cache_ptr = &global_skateshop.cache[i];
+   for( u32 i=0; i<CACHE_BOARD_MAX; i++ ){
+      struct cache_board *cache_ptr = &addon_system.boards[i];
 
-      SDL_AtomicLock( &global_skateshop.sl_cache_access );
+      SDL_AtomicLock( &addon_system.sl_cache );
       if( cache_ptr->state == k_cache_board_state_load_request ){
          if( cache_ptr->reg_index >= addon_count(k_workshop_file_type_board) ){
             /* should maybe have a different value for this case */
             cache_ptr->state = k_cache_board_state_none;
-            SDL_AtomicUnlock( &global_skateshop.sl_cache_access );
+            SDL_AtomicUnlock( &addon_system.sl_cache );
             continue;
          }
 
          /* continue with the request */
-         SDL_AtomicUnlock( &global_skateshop.sl_cache_access );
+         SDL_AtomicUnlock( &addon_system.sl_cache );
          addon_reg *reg = get_addon_from_index( k_workshop_file_type_board,
                                                 cache_ptr->reg_index );
          cache_ptr->reg_ptr = reg;
@@ -163,10 +145,8 @@ VG_STATIC void workshop_visibile_load_loop(void)
 
          /* load content files
           * --------------------------------- */
-
          vg_str content_path = folder;
 
-
          vg_msg root = {0};
          root.buf = reg->metadata;
          root.len = reg->metadata_len;
@@ -193,12 +173,12 @@ VG_STATIC void workshop_visibile_load_loop(void)
          continue;
 
 file_is_broken:;
-         SDL_AtomicLock( &global_skateshop.sl_cache_access );
+         SDL_AtomicLock( &addon_system.sl_cache );
          cache_ptr->state = k_cache_board_state_none;
-         SDL_AtomicUnlock( &global_skateshop.sl_cache_access );
+         SDL_AtomicUnlock( &addon_system.sl_cache );
       }
       else
-         SDL_AtomicUnlock( &global_skateshop.sl_cache_access );
+         SDL_AtomicUnlock( &addon_system.sl_cache );
    }
 }
 
@@ -246,76 +226,27 @@ VG_STATIC void skateshop_op_processview(void){
  * -----------------------------------------------------------------------------
  */
 
-/* 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->ref_count ++;
-}
-
-/* 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
  */
 VG_STATIC void skateshop_init(void){
-   u32 cache_size = sizeof(struct cache_board)*SKATESHOP_BOARD_CACHE_MAX;
-   global_skateshop.cache = vg_linear_alloc( vg_mem.rtmemory, cache_size );
-   memset( global_skateshop.cache, 0, cache_size );
-
-   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];
+   u32 cache_size = sizeof(struct cache_board)*CACHE_BOARD_MAX;
+   addon_system.boards = vg_linear_alloc( vg_mem.rtmemory, cache_size );
+   memset( addon_system.boards, 0, cache_size );
+
+   for( i32 i=0; i<CACHE_BOARD_MAX; i++ ){
+      struct cache_board *board = &addon_system.boards[i];
+      board->state = k_cache_board_state_none;
+      board->reg_ptr= NULL;
+      board->reg_index = 0xffffffff;
+   }
+
+   vg_pool *cache = &addon_system.board_cache;
+   cache->buffer = addon_system.boards;
+   cache->count = CACHE_BOARD_MAX;
+   cache->stride = sizeof( struct cache_board );
+   cache->offset = offsetof( struct cache_board, cachenode );
+   vg_pool_init( cache );
 }
 
 VG_STATIC struct cache_board *skateshop_selected_cache_if_loaded(void)
@@ -324,15 +255,15 @@ VG_STATIC struct cache_board *skateshop_selected_cache_if_loaded(void)
       addon_reg *reg = get_addon_from_index(k_workshop_file_type_board,
                                             global_skateshop.selected_board_id);
 
-      SDL_AtomicLock( &global_skateshop.sl_cache_access );
+      SDL_AtomicLock( &addon_system.sl_cache );
       if( reg->userdata ){
          struct cache_board *cache_ptr = reg->userdata;
          if( cache_ptr->state == k_cache_board_state_loaded ){
-            SDL_AtomicUnlock( &global_skateshop.sl_cache_access );
+            SDL_AtomicUnlock( &addon_system.sl_cache );
             return cache_ptr;
          }
       }
-      SDL_AtomicUnlock( &global_skateshop.sl_cache_access );
+      SDL_AtomicUnlock( &addon_system.sl_cache );
    }
 
    return NULL;
@@ -447,6 +378,8 @@ VG_STATIC void global_skateshop_preupdate(void)
 
    /* input */
    if( shop->type == k_skateshop_type_boardshop ){
+      if( skaterift.async_op != k_async_op_none ) return;
+
       gui_helper_action( axis_display_string( k_sraxis_mbrowse_h ), "browse" );
       gui_helper_action( button_display_string( k_srbind_mback ), "exit" );
 
@@ -459,6 +392,8 @@ VG_STATIC void global_skateshop_preupdate(void)
       /*
        * Controls
        * ----------------------
+       *
+       *  TODO: Crash if switch page too quick, delist browse if loading....
        */
 
       u32 opage = global_skateshop.selected_board_id/SKATESHOP_VIEW_SLOT_MAX;
@@ -488,11 +423,13 @@ VG_STATIC void global_skateshop_preupdate(void)
                      global_skateshop.selected_board_id );
 
          if( localplayer.board_view_slot ){
-            unwatch_cache_board( localplayer.board_view_slot );
+            vg_pool_unwatch( &addon_system.board_cache,
+                                 localplayer.board_view_slot );
          }
 
          localplayer.board_view_slot = selected_cache;
-         watch_cache_board( localplayer.board_view_slot );
+         vg_pool_watch( &addon_system.board_cache,
+                             localplayer.board_view_slot );
          global_skateshop_exit();
          skaterift_write_savedata();
          return;
@@ -630,7 +567,7 @@ VG_STATIC void skateshop_render_boardshop(void)
                                   mdl_entity_id_id(shop->boards.id_display));
 
    int visibility[ SKATESHOP_VIEW_SLOT_MAX ];
-   SDL_AtomicLock( &global_skateshop.sl_cache_access );
+   SDL_AtomicLock( &addon_system.sl_cache );
    for( u32 i=0; i<SKATESHOP_VIEW_SLOT_MAX; i++ ){
       struct shop_view_slot *slot = &global_skateshop.shop_view_slots[i];
 
@@ -640,7 +577,7 @@ VG_STATIC void skateshop_render_boardshop(void)
       else if( slot->cache_ptr->state != k_cache_board_state_loaded )
          visibility[i] = 0;
    }
-   SDL_AtomicUnlock( &global_skateshop.sl_cache_access );
+   SDL_AtomicUnlock( &addon_system.sl_cache );
    
    /* Render loaded boards in the view slots */
    for( u32 i=0; i<slot_count; i++ ){