A complete workshop implementation, I guess
[carveJwlIkooP6JGAAIwe30JlM.git] / workshop.c
index c82ca6ef563a12167635daab87db2ab1ae3dbd9c..30aaf7a376b5be2fe1933fb46cb36ed50bcf4bdb 100644 (file)
@@ -42,7 +42,6 @@ struct workshop_form{
     */
 
    char model_path[128];
-
    struct player_board board_model;
 
    /* what does the user want to do with the image preview? */
@@ -298,6 +297,9 @@ VG_STATIC void workshop_form_async_package_complete( void *data, u32 size )
  */
 struct workshop_package_thread_args{
    PublishedFileId_t file_id;
+
+   u64_steamid steamid;
+   char username[32];
 };
 VG_STATIC void _workshop_package_thread( void *_args )
 {
@@ -340,6 +342,40 @@ VG_STATIC void _workshop_package_thread( void *_args )
       return;
    }
 
+   /* write the metadata file */
+   struct workshop_file_info meta;
+   meta.author = args->steamid;
+   vg_strncpy( args->username, meta.author_name, vg_list_size(meta.author_name),
+               k_strncpy_always_add_null );
+   vg_strncpy( workshop_form.submission.title, meta.title, 
+               vg_list_size(meta.title), k_strncpy_always_add_null );
+
+   char _path[1024];
+   vg_str path;
+   vg_strnull( &path, _path, vg_list_size( _path ) );
+   vg_strcat( &path, info->abs_content_file );
+   vg_strcat( &path, ".inf" );
+   
+   if( vg_strgood( &path ) ){
+      FILE *fp = fopen( _path, "wb" );
+
+      if( fp ){
+         fwrite( &meta, sizeof(struct workshop_file_info), 1, fp );
+         fclose( fp );
+      }
+      else{
+         info->success = 0;
+         info->failure_reason = "Cant write .inf file";
+         vg_async_dispatch( call, workshop_form_async_package_complete );
+      }
+   }
+   else{
+      info->success = 0;
+      info->failure_reason = "Path too long";
+      vg_async_dispatch( call, workshop_form_async_package_complete );
+      return;
+   }
+
    info->success = 1;
    vg_async_dispatch( call, workshop_form_async_package_complete );
 }
@@ -354,6 +390,16 @@ VG_STATIC void workshop_package_submission( PublishedFileId_t file_id )
       vg_linear_alloc( vg_mem.scratch, 
                        sizeof(struct workshop_package_thread_args));
 
+   ISteamFriends *hSteamFriends = SteamAPI_SteamFriends();
+   ISteamUser *hSteamUser = SteamAPI_SteamUser();
+
+   args->steamid = SteamAPI_ISteamUser_GetSteamID( hSteamUser );
+
+   const char *username = SteamAPI_ISteamFriends_GetPersonaName(hSteamFriends);
+   str_utf8_collapse( username, args->username, vg_list_size( args->username ));
+   vg_info( "Steamid: "PRINTF_U64", Name: %s(%s)\n", 
+               args->steamid, username, args->username );
+
    args->file_id = file_id;
    vg_loader_start( _workshop_package_thread, args );
 }
@@ -517,13 +563,29 @@ VG_STATIC void workshop_form_loadmodel_async_complete( void *payload, u32 size )
    workshop_end_op();
 }
 
+/*
+ * Reciever for failure to load
+ */
+VG_STATIC void workshop_form_loadmodel_async_error( void *payload, u32 size )
+{
+   workshop_end_op();
+}
+
 /*
  * Thread which loads the model from the disk 
  */
 VG_STATIC void _workshop_form_load_thread( void *data )
 {
-   player_board_load( &workshop_form.board_model, workshop_form.model_path );
-   vg_async_call( workshop_form_loadmodel_async_complete, NULL, 0 );
+   FILE *test = fopen( workshop_form.model_path, "rb" );
+   if( test ){
+      fclose( test );
+      player_board_load( &workshop_form.board_model, workshop_form.model_path );
+      vg_async_call( workshop_form_loadmodel_async_complete, NULL, 0 );
+   }
+   else{
+      vg_error( "workshop async load failed: file not found\n" );
+      vg_async_call( workshop_form_loadmodel_async_error, NULL, 0 );
+   }
 }
 
 /*
@@ -1083,7 +1145,6 @@ VG_STATIC void workshop_form_gui_edit_page( ui_rect content )
          .change = workshop_changed_description
       };
       ui_rect desc_entry;
-      /* 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 );
@@ -1165,8 +1226,6 @@ VG_STATIC void workshop_form_gui_sidebar( ui_rect sidebar )
    char buf[32];
    strcpy( buf, "page " );
    int i  = 5;
-
-   /* TODO: for what it is, this code is getting a bit.. special */
        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 );
@@ -1368,4 +1427,32 @@ VG_STATIC void async_workshop_get_installed_files( void *data, u32 len )
    *info->len = j;
 }
 
+VG_STATIC void vg_strsan_ascii( char *buf, u32 len )
+{
+   for( u32 i=0; i<len-1; i ++ ){
+      if( buf[i] == 0 ) return;
+
+      if( buf[i] < 32 || buf[i] > 126 ){
+         buf[i] = '?';
+      }
+   }
+   buf[len-1] = '\0';
+}
+
+#define VG_STRSAN_ASCII( X ) vg_strsan_ascii( X, vg_list_size(X) )
+
+VG_STATIC void workshop_load_metadata( const char *path,
+                                       struct workshop_file_info *info )
+{
+   FILE *fp = fopen( path, "rb" );
+   
+   if( fp ){
+      if( fread( info, sizeof( struct workshop_file_info ), 1, fp ) ){
+         VG_STRSAN_ASCII( info->author_name );
+         VG_STRSAN_ASCII( info->title );
+      }
+      fclose( fp );
+   }
+}
+
 #endif /* WORKSHOP_C */