camera shake
[carveJwlIkooP6JGAAIwe30JlM.git] / workshop.c
index cf0e25a8628e43c384ad5dded311ff9dc0fb4610..61a4899793505e344fc227f252a15d1e6f420f1e 100644 (file)
@@ -428,32 +428,32 @@ VG_STATIC void _workshop_form_submit_thread( void *data )
    descriptor_str[descriptor.cur*2] = '\0';
    vg_info( "binstr: %s\n", descriptor_str );
 
-   DIR *dir = opendir( folder.buffer );
-   if( !dir ){
+   vg_dir dir;
+   if( !vg_dir_open( &dir, folder.buffer ) ){
       vg_error( "could not open addon folder '%s'\n", folder.buffer );
       vg_async_call( workshop_async_any_complete, NULL, 0 );
       return;
    }
 
-   struct dirent *entry;
-   while( (entry = readdir(dir)) ){
-      if( entry->d_type == DT_REG ){
-         if( entry->d_name[0] == '.' ) continue;
+   while( vg_dir_next_entry(&dir) ){
+      if( vg_dir_entry_type(&dir) == k_vg_entry_type_file ){
+         const char *d_name = vg_dir_entry_name(&dir);
+         if( d_name[0] == '.' ) continue;
 
          vg_str file = folder;
          vg_strcat( &file, "/" );
-         vg_strcat( &file, entry->d_name );
+         vg_strcat( &file, d_name );
          if( !vg_strgood( &file ) ) continue;
 
          char *ext = vg_strch( &file, '.' );
          if( !ext ) continue;
          if( strcmp(ext,".mdl") ) continue;
 
-         vg_msg_wkvstr( &descriptor, "content", entry->d_name );
+         vg_msg_wkvstr( &descriptor, "content", d_name );
          break;
       }
    }
-   closedir(dir);
+   vg_dir_close(&dir);
 
    vg_str descriptor_file = folder;
    vg_strcat( &descriptor_file, "/addon.inf" );
@@ -578,8 +578,8 @@ VG_STATIC void _workshop_form_load_thread( void *data )
       return;
    }
 
-   DIR *dir = opendir( folder.buffer );
-   if( !dir ){
+   vg_dir dir;
+   if( !vg_dir_open( &dir, folder.buffer ) ){
       vg_error( "workshop async load failed: could not open folder\n" );
       vg_async_call( workshop_form_loadmodel_async_error, NULL, 0 );
       return;
@@ -588,14 +588,14 @@ VG_STATIC void _workshop_form_load_thread( void *data )
    vg_info( "Searching %s for model files\n", folder.buffer );
 
    int found_mdl = 0;
-   struct dirent *entry;
-   while( (entry = readdir(dir)) ){
-      if( entry->d_type == DT_REG ){
-         if( entry->d_name[0] == '.' ) continue;
+   while( vg_dir_next_entry(&dir) ){
+      if( vg_dir_entry_type(&dir) == k_vg_entry_type_file ){
+         const char *d_name = vg_dir_entry_name(&dir);
+         if( d_name[0] == '.' ) continue;
 
          vg_str file = folder;
          vg_strcat( &file, "/" );
-         vg_strcat( &file, entry->d_name );
+         vg_strcat( &file, d_name );
          if( !vg_strgood( &file ) ) continue;
 
          char *ext = vg_strch( &file, '.' );
@@ -605,7 +605,7 @@ VG_STATIC void _workshop_form_load_thread( void *data )
          break;
       }
    }
-   closedir(dir);
+   vg_dir_close(&dir);
 
    if( !found_mdl ){
       vg_error( "workshop async load failed: no model files found\n" );
@@ -656,39 +656,46 @@ VG_STATIC void workshop_form_async_imageload( void *data, u32 len )
    skaterift_end_op();
 }
 
-struct workshop_loadpreview_info {
-   char abs_preview_image[ 1024 ];
-};
-
 /*
  * Load the image located at ./workshop_preview.jpg into our framebuffer
  */
 VG_STATIC void _workshop_load_preview_thread( void *data )
 {
-   struct workshop_loadpreview_info *info = data;
-
-   stbi_set_flip_vertically_on_load(1);
-   int x, y, nc;
-   u8 *rgb = stbi_load( info->abs_preview_image, &x, &y, &nc, 3 );
-
-   if( rgb ){
-      if( (x == WORKSHOP_PREVIEW_WIDTH) && (y == WORKSHOP_PREVIEW_HEIGHT) ){
-         vg_async_call( workshop_form_async_imageload, rgb, x*y*3 );
+   char path_buf[ 4096 ];
+   vg_str path;
+   vg_strnull( &path, path_buf, 4096 );
+   vg_strcat( &path, "boards/" );
+   vg_strcat( &path, workshop_form.addon_folder );
+   vg_strcat( &path, "/preview.jpg" );
+
+   if( vg_strgood( &path ) ){
+      stbi_set_flip_vertically_on_load(1);
+      int x, y, nc;
+      u8 *rgb = stbi_load( path.buffer, &x, &y, &nc, 3 );
+
+      if( rgb ){
+         if( (x == WORKSHOP_PREVIEW_WIDTH) && (y == WORKSHOP_PREVIEW_HEIGHT) ){
+            vg_async_call( workshop_form_async_imageload, rgb, x*y*3 );
+         }
+         else{
+            vg_error( "Resolution does not match framebuffer, so we can't"
+                      " show it\n" );
+            stbi_image_free( rgb );
+            vg_async_call( workshop_form_async_imageload, NULL, 0 );
+         }
       }
       else{
-         vg_error( "Resolution does not match framebuffer, so we can't"
-                   " show it\n" );
-         stbi_image_free( rgb );
+         vg_error( "Failed to load workshop_preview.jpg: '%s'\n", 
+                     stbi_failure_reason() );
          vg_async_call( workshop_form_async_imageload, NULL, 0 );
       }
    }
    else{
-      vg_error( "Failed to load workshop_preview.jpg: '%s'\n", 
-                  stbi_failure_reason() );
       vg_async_call( workshop_form_async_imageload, NULL, 0 );
    }
 }
 
+#if 0
 /*
  * Reciever for the preview download result
  */
@@ -707,6 +714,7 @@ VG_STATIC void on_workshop_download_ugcpreview( void *data, void *user )
       skaterift_end_op();
    }
 }
+#endif
 
 /*
  * Entry point to view operation
@@ -746,7 +754,7 @@ VG_STATIC void workshop_op_download_and_view_submission( int result_index )
 
       snprintf( workshop_form.addon_folder, 
                  vg_list_size( workshop_form.addon_folder ),
-                 "Steam Cloud (%lu)", details.m_nPublishedFileId );
+                 "Steam Cloud ("PRINTF_U64")", details.m_nPublishedFileId );
 
       workshop_form.submission.file_id = details.m_nPublishedFileId;
       workshop_form.file_intent = k_workshop_form_file_intent_keep_old;
@@ -761,31 +769,25 @@ VG_STATIC void workshop_op_download_and_view_submission( int result_index )
          u32 len = strlen(metadata_str);
          vg_info( "Metadata: %s\n", metadata_str );
          vg_str_bin( metadata_str, metadata_buf, len );
-         vg_msg msg;
-         vg_msg_init( &msg, metadata_buf, len/2 );
+         vg_msg root;
+         vg_msg_init( &root, metadata_buf, len/2 );
          
-         vg_msg_cmd cmd;
-         while( vg_msg_next( &msg, &cmd ) ){
-            if( (msg.depth == 1) && (cmd.code == k_vg_msg_code_frame) ){
-               if( VG_STRDJB2_EQ( "workshop", cmd.key, cmd.key_djb2 ) ){
-                  u32 depth = msg.depth;
-                  while( (msg.depth == depth) && vg_msg_next( &msg, &cmd ) ){
-                     if( cmd.code & k_vg_msg_code_unsigned ){
-                        if( VG_STRDJB2_EQ( "type", cmd.key, cmd.key_djb2 ) ){
-                           workshop_form.submission.type = cmd.value._u32;
-   workshop_form.submission.submission_type_selection.value = cmd.value._u32;
-                        }
-                     }
-                     else if( cmd.code == k_vg_msg_code_kvstring ){
-                        if( VG_STRDJB2_EQ( "folder", cmd.key, cmd.key_djb2 ) ){
-                           vg_strncpy( cmd.value._buf,
-                                       workshop_form.addon_folder,
-                                       sizeof(workshop_form.addon_folder),
-                                       k_strncpy_always_add_null );
-                        }
-                     }
-                  }
-               }
+         vg_msg workshop;
+         if( vg_msg_seekframe( &workshop, "workshop", k_vg_msg_first )){
+            vg_msg_cmd kv_type = vg_msg_seekkv( &workshop, "type", 
+                                                k_vg_msg_first );
+            if( kv_type.code & k_vg_msg_code_integer ){
+               u32 u = kv_type.value._u32;
+               workshop_form.submission.type = u;
+               workshop_form.submission.submission_type_selection.value = u;
+            }
+
+            const char *kv_folder = vg_msg_seekkvstr( &workshop, "folder",
+                                                      k_vg_msg_first );
+            if( kv_folder ){
+               vg_strncpy( kv_folder, workshop_form.addon_folder,
+                           sizeof(workshop_form.addon_folder),
+                           k_strncpy_always_add_null );
             }
          }
       }
@@ -808,14 +810,13 @@ VG_STATIC void workshop_op_download_and_view_submission( int result_index )
          }
       }
 
-      vg_error( "m_hPreviewFile is 0\n" );
       render_fb_bind( gpipeline.fb_workshop_preview, 0 );
       glClearColor( 0.2f, 0.0f, 0.0f, 1.0f );
       glClear( GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT );
       glBindFramebuffer( GL_FRAMEBUFFER, 0 );
       glViewport( 0,0, vg.window_x, vg.window_y );
 
-      skaterift_end_op();
+      vg_loader_start( _workshop_load_preview_thread, NULL );
 
 #if 0
       if( details.m_hPreviewFile == 0 ){
@@ -1156,6 +1157,32 @@ VG_STATIC void workshop_form_gui_edit_page( ui_rect content )
       return;
    }
 
+   if( workshop_form.submission.type == k_workshop_file_type_world ){
+      ui_rect box;
+      rect_copy( content, box );
+      box[3] = 128;
+      box[2] = (box[2]*2)/3;
+      ui_rect_center( content, box );
+
+      ui_rect row;
+      ui_split( box, k_ui_axis_h, 28, 0, row, box );
+      ui_text( row, "World submissions are currently not ready, sorry.", 
+               1, k_ui_align_middle_center,0);
+      ui_split( box, k_ui_axis_h, 8, 0, row, box );
+      ui_split( box, k_ui_axis_h, 28, 0, row, box );
+
+      ui_rect button;
+      rect_copy( row, button );
+      button[2] = 128;
+      ui_rect_center( row, button );
+      if( ui_button_text( button, "OK", 1 ) ){
+         workshop_form.page = k_workshop_form_open;
+         workshop_form.file_intent = k_workshop_form_file_intent_none;
+      }
+
+      return;
+   }
+
    ui_rect image_plane;
    ui_split( content, k_ui_axis_h, 300, 0, image_plane, content );
    ui_fill( image_plane, ui_colour( k_ui_bg+0 ) );