moved savedata to thread
[carveJwlIkooP6JGAAIwe30JlM.git] / ent_skateshop.c
index b3e648f45aff2a7676a667c792d6d0a3b4543c26..91947fbe2893dc3ecf55741270ec0ff5a366e902 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
@@ -130,7 +131,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,65 +146,39 @@ 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) );
+         vg_str folder;
+         vg_strnull( &folder, path_buf, 4096 );
+         if( !addon_get_content_folder( reg, &folder ) )
+            goto file_is_broken;
 
-            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;
-            }
-
-            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;
 
-         int found = 0;
-         vg_msg msg;
-         vg_msg_init( &msg, cache_ptr->reg_ptr->metadata, 
-                      cache_ptr->reg_ptr->metadata_len );
-         vg_msg_cmd cmd;
-         while( vg_msg_next( &msg, &cmd ) ){
-            if( (msg.depth == 0) && (cmd.code == k_vg_msg_code_kvstring) ){
-               if( VG_STRDJB2_EQ( "content", cmd.key, cmd.key_djb2 ) ){
-                  vg_strcat( &content_path, "/" );
-                  vg_strcat( &content_path, cmd.value._buf );
-                  found = 1;
-                  break;
-               }
-            }
-         }
 
-         if( !vg_strgood( &content_path ) ) {
-            vg_error( "Metadata path too long\n" );
+         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 ){
+            vg_strcat( &content_path, "/" );
+            vg_strcat( &content_path, kv_content );
+         }
+         else{
+            vg_error( "No content paths in metadata\n" );
             goto file_is_broken;
          }
 
-         if( !found ){
-            vg_error( "No content paths in metadata\n" );
+         if( !vg_strgood( &content_path ) ) {
+            vg_error( "Metadata path too long\n" );
             goto file_is_broken;
          }
          
@@ -225,7 +199,7 @@ file_is_broken:;
 
 
 VG_STATIC void world_scan_thread( void *_args ){
-   addon_mount_local_folder( k_workshop_file_type_world, "maps", ".mdl" );
+   addon_mount_content_folder( k_workshop_file_type_world, "maps", ".mdl" );
    addon_mount_workshop_items();
    vg_async_call( async_addon_reg_update, NULL, 0 );
    skaterift_end_op();
@@ -239,13 +213,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_local_folder( k_workshop_file_type_board, "boards", ".mdl" );
+   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){
@@ -253,6 +231,11 @@ 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
  * -----------------------------------------------------------------------------
@@ -321,65 +304,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 );
 }
 
 /*
@@ -445,6 +424,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 --;
@@ -459,7 +440,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 );
 
@@ -470,6 +457,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;
       }
    }
@@ -479,21 +467,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*/
@@ -514,8 +502,10 @@ VG_STATIC void global_skateshop_preupdate(void)
          browseable = 1;
       }
 
-      if( skaterift.async_op == k_async_op_none ){
-         gui_helper_action( button_display_string(k_srbind_maccept), "load" );
+      if( (skaterift.async_op == k_async_op_none) &&
+           global_skateshop.selected_world_id > 0 ){
+         gui_helper_action( button_display_string(k_srbind_maccept), 
+                            "open rift" );
          loadable = 1;
       }
       
@@ -550,9 +540,11 @@ VG_STATIC void global_skateshop_preupdate(void)
 
          /* automatically load in clouds */
          if( loadable && button_down( k_srbind_maccept ) ){
-            vg_info( "Select world (%u)\n", 
+            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{
@@ -691,7 +683,33 @@ fade_out:;
    }
 
    struct cache_board *cache_ptr = skateshop_selected_cache_if_loaded();
-   if( !cache_ptr ) return;
+
+   if( !cache_ptr ){
+      global_skateshop.render.item_title = "";
+      global_skateshop.render.item_desc = "";
+      return;
+   }
+
+   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 = {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;
+   }
 
    addon_reg *reg = cache_ptr->reg_ptr;
 
@@ -699,23 +717,27 @@ fade_out:;
     * ----------------------------------------------------------------- */
    m3x3_zero( mlocal );
    m3x3_setdiagonalv3( mlocal, (v3f){ scale, scale, thickness } );
-   mlocal[3][0] = -font3d_string_width( &gui.font, 0, "Flubber" );
+   mlocal[3][0] = -font3d_string_width( &gui.font, 0, 
+                                        global_skateshop.render.item_title );
    mlocal[3][0] *= scale*0.5f;
    mlocal[3][1] = 0.1f;
    mlocal[3][2] = 0.0f;
    m4x3_mul( mtext, mlocal, mmdl );
-   font3d_simple_draw( &gui.font, 0, "Flubber", &main_camera, mmdl );
+   font3d_simple_draw( &gui.font, 0, global_skateshop.render.item_title, 
+                       &main_camera, mmdl );
 
    /* Author name
     * ----------------------------------------------------------------- */
    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;
    m4x3_mul( mtext, mlocal, mmdl );
-   font3d_simple_draw( &gui.font, 0, "JA", &main_camera, mmdl );
+   font3d_simple_draw( &gui.font, 0, global_skateshop.render.item_desc, 
+                       &main_camera, mmdl );
 }
 
 VG_STATIC void skateshop_render_charshop(void)
@@ -732,6 +754,24 @@ VG_STATIC void skateshop_render_worldshop(void)
               *mark_info = mdl_arritm( &world->ent_marker, 
                                   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 = {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, 
+                                                                 "title", 0 );
+      }
+      global_skateshop.render.world_loc = vg_msg_seekkvstr(&root,"location",0);
+      global_skateshop.render.world_reg = global_skateshop.selected_world_id;
+   }
+
    /* Text */
    char buftext[128], bufsubtext[128];
    vg_str info, subtext;
@@ -750,13 +790,19 @@ VG_STATIC void skateshop_render_worldshop(void)
       info.buffer[info.i++] = ' ';
       info.buffer[info.i] = '\0';
 
-      vg_strcat( &info, "AFAWJFKAW" );
+      vg_strcat( &info, global_skateshop.render.world_title );
       if( skaterift.async_op == k_async_op_world_loading ||
           skaterift.async_op == k_async_op_world_preloading ){
          vg_strcat( &subtext, "Loading..." );
       }
       else{
-         vg_strcat( &subtext, "No information" );
+         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 );
       }
    }
    else{