performance measurements
[carveJwlIkooP6JGAAIwe30JlM.git] / workshop.c
1 #ifndef WORKSHOP_C
2 #define WORKSHOP_C
3
4 #include "workshop.h"
5
6 #define VG_GAME
7 #include "vg/vg.h"
8 #include "vg/vg_tex.h"
9 #include "vg/vg_msg.h"
10 #include "vg/vg_binstr.h"
11 #include "ent_skateshop.h"
12
13 #include "vg/vg_steam_auth.h"
14 #include "vg/vg_steam_ugc.h"
15 #include "vg/vg_steam_friends.h"
16 #include "steam.h"
17 #include "highscores.h"
18
19 static struct ui_enum_opt workshop_form_visibility_opts[] = {
20 { k_ERemoteStoragePublishedFileVisibilityPublic, "Public" },
21 { k_ERemoteStoragePublishedFileVisibilityUnlisted, "Unlisted" },
22 { k_ERemoteStoragePublishedFileVisibilityFriendsOnly, "Friends Only" },
23 { k_ERemoteStoragePublishedFileVisibilityPrivate, "Private" },
24 };
25
26 static struct ui_enum_opt workshop_form_type_opts[] = {
27 { k_addon_type_none, "None" },
28 { k_addon_type_board, "Board" },
29 { k_addon_type_world, "World" },
30 { k_addon_type_player, "Player" },
31 };
32
33 /*
34 * Close the form and discard UGC query result
35 */
36 static void workshop_quit_form(void){
37 player_board_unload( &workshop_form.board_model );
38 workshop_form.file_intent = k_workshop_form_file_intent_none;
39
40 if( workshop_form.ugc_query.result == k_EResultOK ){
41 workshop_form.ugc_query.result = k_EResultNone;
42
43 ISteamUGC *hSteamUGC = SteamAPI_SteamUGC();
44 SteamAPI_ISteamUGC_ReleaseQueryUGCRequest(
45 hSteamUGC, workshop_form.ugc_query.handle );
46 }
47
48 workshop_form.page = k_workshop_form_hidden;
49 workshop_form.op = k_workshop_op_none;
50 }
51
52 /*
53 * Delete all information about the submission
54 */
55 static void workshop_reset_submission_data(void)
56 {
57 workshop_form.submission.file_id = 0; /* assuming id of 0 is none/invalid */
58 workshop_form.submission.description[0] = '\0';
59 workshop_form.submission.title[0] = '\0';
60 workshop_form.submission.author[0] = '\0';
61 workshop_form.submission.submission_type_selection =
62 k_addon_type_none;
63 workshop_form.submission.type = k_addon_type_none;
64
65 workshop_form.submission.visibility =
66 k_ERemoteStoragePublishedFileVisibilityPublic;
67
68 workshop_form.addon_folder[0] = '\0';
69 player_board_unload( &workshop_form.board_model );
70 workshop_form.file_intent = k_workshop_form_file_intent_none;
71 }
72
73
74 /*
75 * Mostly copies of what it sais on the Steam API documentation
76 */
77 static const char *workshop_EResult_user_string( EResult result )
78 {
79 switch( result ){
80 case k_EResultInsufficientPrivilege:
81 return "Your account is currently restricted from uploading content "
82 "due to a hub ban, account lock, or community ban. You need to "
83 "contact Steam Support to resolve the issue.";
84 case k_EResultBanned:
85 return "You do not have permission to upload content to this hub "
86 "because you have an active VAC or Game ban.";
87 case k_EResultTimeout:
88 return "The operation took longer than expected, so it was discarded. "
89 "Please try again.";
90 case k_EResultNotLoggedOn:
91 return "You are currently not logged into Steam.";
92 case k_EResultServiceUnavailable:
93 return "The workshop server is having issues or is unavailable, "
94 "please try again.";
95 case k_EResultInvalidParam:
96 return "One of the submission fields contains something not being "
97 "accepted by that field.";
98 case k_EResultAccessDenied:
99 return "There was a problem trying to save the title and description. "
100 "Access was denied.";
101 case k_EResultLimitExceeded:
102 return "You have exceeded your Steam Cloud quota. If you wish to "
103 "upload this file, you must remove some published items.";
104 case k_EResultFileNotFound:
105 return "The uploaded file could not be found.";
106 case k_EResultDuplicateRequest:
107 return "The file was already successfully uploaded.";
108 case k_EResultDuplicateName:
109 return "You already have a Steam Workshop item with that name.";
110 case k_EResultServiceReadOnly:
111 return "Due to a recent password or email change, you are not allowed "
112 "to upload new content. Usually this restriction will expire in"
113 " 5 days, but can last up to 30 days if the account has been "
114 "inactive recently.";
115 default:
116 return "Operation failed for an error which has not been accounted for "
117 "by the programmer. Try again, sorry :)";
118 }
119 }
120
121 /*
122 * op: k_workshop_form_op_publishing_update
123 * ----------------------------------------------------------------------------
124 */
125
126 /*
127 * The endpoint of this operation
128 */
129 static void on_workshop_update_result( void *data, void *user )
130 {
131 vg_info( "Recieved workshop update result\n" );
132 SubmitItemUpdateResult_t *result = data;
133
134 /* this seems to be set here, but my account definitely has accepted it */
135 if( result->m_bUserNeedsToAcceptWorkshopLegalAgreement ){
136 vg_warn( "Workshop agreement currently not accepted\n" );
137 }
138
139 if( result->m_eResult == k_EResultOK ){
140 workshop_form.page = k_workshop_form_closing_good;
141 workshop_form.failure_or_success_string = "Uploaded workshop file!";
142 vg_success( "file uploaded\n" );
143 }
144 else{
145 workshop_form.page = k_workshop_form_closing_bad;
146 workshop_form.failure_or_success_string =
147 workshop_EResult_user_string( result->m_eResult );
148
149 vg_error( "Error with the submitted file (%d)\n", result->m_eResult );
150 }
151 workshop_form.op = k_workshop_op_none;
152 }
153
154 static const char *workshop_filetype_folder(void){
155 enum addon_type type = workshop_form.submission.type;
156 if ( type == k_addon_type_board ) return "boards/";
157 else if( type == k_addon_type_player ) return "playermodels/";
158 else if( type == k_addon_type_world ) return "maps/";
159
160 return "unknown_addon_type/";
161 }
162
163 /*
164 * reciever on completion of packaging the files, it will then start the item
165 * update with Steam API
166 */
167 static void workshop_form_upload_submission( PublishedFileId_t file_id,
168 char *metadata )
169 {
170 ISteamUGC *hSteamUGC = SteamAPI_SteamUGC();
171 UGCUpdateHandle_t handle
172 = SteamAPI_ISteamUGC_StartItemUpdate( hSteamUGC, SKATERIFT_APPID,
173 file_id );
174
175 /* TODO: Handle failure cases for these */
176
177 SteamAPI_ISteamUGC_SetItemMetadata( hSteamUGC, handle, metadata );
178
179 if( workshop_form.submission.submit_title ){
180 vg_info( "Setting title\n" );
181 SteamAPI_ISteamUGC_SetItemTitle( hSteamUGC, handle,
182 workshop_form.submission.title );
183 }
184
185 if( workshop_form.submission.submit_description ){
186 vg_info( "Setting description\n" );
187 SteamAPI_ISteamUGC_SetItemDescription( hSteamUGC, handle,
188 workshop_form.submission.description);
189 }
190
191 if( workshop_form.submission.submit_file_and_image ){
192 char path_buf[4096];
193 vg_str folder;
194 vg_strnull( &folder, path_buf, 4096 );
195 vg_strcat( &folder, vg.base_path );
196
197 vg_strcat( &folder, workshop_filetype_folder() );
198 vg_strcat( &folder, workshop_form.addon_folder );
199
200 vg_info( "Setting item content\n" );
201 SteamAPI_ISteamUGC_SetItemContent( hSteamUGC, handle, folder.buffer );
202
203 vg_str preview = folder;
204 vg_strcat( &preview, "/preview.jpg" );
205
206 vg_info( "Setting preview image\n" );
207 SteamAPI_ISteamUGC_SetItemPreview( hSteamUGC, handle, preview.buffer );
208 }
209
210 vg_info( "Setting visibility\n" );
211 SteamAPI_ISteamUGC_SetItemVisibility( hSteamUGC, handle,
212 workshop_form.submission.visibility );
213
214 vg_info( "Submitting updates\n" );
215 vg_steam_async_call *call = vg_alloc_async_steam_api_call();
216 call->userdata = NULL;
217 call->p_handler = on_workshop_update_result;
218 call->id = SteamAPI_ISteamUGC_SubmitItemUpdate( hSteamUGC, handle, "" );
219 }
220
221 /*
222 * Steam API call result for when we've created a new item on their network, or
223 * not, if it has failed
224 */
225 static void on_workshop_createitem( void *data, void *user )
226 {
227 CreateItemResult_t *result = data;
228
229 if( result->m_eResult == k_EResultOK ){
230 vg_info( "Created workshop file with id: %lu\n",
231 result->m_nPublishedFileId );
232
233 if( result->m_bUserNeedsToAcceptWorkshopLegalAgreement ){
234 vg_warn( "Workshop agreement currently not accepted\n" );
235 }
236
237 workshop_form_upload_submission( result->m_nPublishedFileId, user );
238 }
239 else{
240 const char *errstr = workshop_EResult_user_string( result->m_eResult );
241
242 if( errstr ){
243 vg_error( "ISteamUGC_CreateItem() failed(%d): '%s' \n",
244 result->m_eResult, errstr );
245 }
246
247 workshop_form.page = k_workshop_form_closing_bad;
248 workshop_form.failure_or_success_string = errstr;
249 }
250 }
251
252 /*
253 * Starts the workshop upload process through Steam API
254 */
255 static void workshop_form_async_submit_begin( void *payload, u32 size )
256 {
257
258 /* use existing file */
259 if( workshop_form.submission.file_id ){
260 workshop_form_upload_submission( workshop_form.submission.file_id,
261 payload );
262 }
263 else{
264 vg_steam_async_call *call = vg_alloc_async_steam_api_call();
265 call->userdata = payload;
266 call->p_handler = on_workshop_createitem;
267 ISteamUGC *hSteamUGC = SteamAPI_SteamUGC();
268 call->id = SteamAPI_ISteamUGC_CreateItem( hSteamUGC, SKATERIFT_APPID,
269 k_EWorkshopFileTypeCommunity );
270 }
271 }
272
273 /*
274 * Downloads the framebuffer into scratch memory
275 */
276 static void workshop_form_async_download_image( void *payload, u32 size )
277 {
278 int w, h;
279 render_fb_get_current_res( gpipeline.fb_workshop_preview, &w, &h );
280 vg_linear_clear( vg_mem.scratch );
281 workshop_form.img_buffer = vg_linear_alloc( vg_mem.scratch, w*h*3 );
282
283 vg_info( "read framebuffer: glReadPixels( %dx%d )\n", w,h );
284
285 glBindFramebuffer( GL_READ_FRAMEBUFFER, gpipeline.fb_workshop_preview->fb );
286 glReadBuffer( GL_COLOR_ATTACHMENT0 );
287 glReadPixels( 0,0, w,h, GL_RGB, GL_UNSIGNED_BYTE, workshop_form.img_buffer );
288
289 workshop_form.img_w = w;
290 workshop_form.img_h = h;
291 }
292
293 /*
294 * Thread which kicks off the upload process
295 */
296 static void _workshop_form_submit_thread( void *data )
297 {
298 vg_async_call( workshop_form_async_download_image, NULL, 0 );
299 vg_async_stall();
300
301 char path_buf[4096];
302 vg_str folder;
303 vg_strnull( &folder, path_buf, 4096 );
304
305 vg_strcat( &folder, workshop_filetype_folder() );
306 vg_strcat( &folder, workshop_form.addon_folder );
307
308 if( !vg_strgood(&folder) ){
309 vg_error( "addon folder path too long\n" );
310 workshop_form.op = k_workshop_op_none;
311 return;
312 }
313
314 /*
315 * Create the metadata file
316 * -----------------------------------------------------------------------*/
317 u8 descriptor_buf[ 512 ];
318 vg_msg descriptor;
319 vg_msg_init( &descriptor, descriptor_buf, sizeof(descriptor_buf) );
320 vg_linear_clear( vg_mem.scratch );
321
322 /* short description */
323 vg_msg_frame( &descriptor, "workshop" );
324 vg_msg_wkvstr( &descriptor, "title", workshop_form.submission.title );
325 //vg_msg_wkvstr( &descriptor, "author", "unknown" );
326 vg_msg_wkvu32( &descriptor, "type", workshop_form.submission.type );
327 vg_msg_wkvstr( &descriptor, "folder", workshop_form.addon_folder );
328 vg_msg_end_frame( &descriptor );
329 //vg_msg_wkvstr( &descriptor, "location", "USA" );
330
331 char *short_descriptor_str =
332 vg_linear_alloc( vg_mem.scratch, vg_align8(descriptor.cur.co*2+1));
333 vg_bin_str( descriptor_buf, short_descriptor_str, descriptor.cur.co );
334 short_descriptor_str[descriptor.cur.co*2] = '\0';
335 vg_info( "binstr: %s\n", short_descriptor_str );
336
337 vg_dir dir;
338 if( !vg_dir_open( &dir, folder.buffer ) ){
339 vg_error( "could not open addon folder '%s'\n", folder.buffer );
340 workshop_form.op = k_workshop_op_none;
341 return;
342 }
343
344 while( vg_dir_next_entry(&dir) ){
345 if( vg_dir_entry_type(&dir) == k_vg_entry_type_file ){
346 const char *d_name = vg_dir_entry_name(&dir);
347 if( d_name[0] == '.' ) continue;
348
349 vg_str file = folder;
350 vg_strcat( &file, "/" );
351 vg_strcat( &file, d_name );
352 if( !vg_strgood( &file ) ) continue;
353
354 char *ext = vg_strch( &file, '.' );
355 if( !ext ) continue;
356 if( strcmp(ext,".mdl") ) continue;
357
358 vg_msg_wkvstr( &descriptor, "content", d_name );
359 break;
360 }
361 }
362 vg_dir_close(&dir);
363
364 vg_str descriptor_file = folder;
365 vg_strcat( &descriptor_file, "/addon.inf" );
366 if( !vg_strgood(&descriptor_file) ){
367 vg_error( "Addon info path too long\n" );
368 workshop_form.op = k_workshop_op_none;
369 return;
370 }
371
372 FILE *fp = fopen( descriptor_file.buffer, "wb" );
373 if( !fp ){
374 vg_error( "Could not open addon info file '%s'\n",
375 descriptor_file.buffer );
376 workshop_form.op = k_workshop_op_none;
377 return;
378 }
379 fwrite( descriptor_buf, descriptor.cur.co, 1, fp );
380 fclose( fp );
381
382 /* Save the preview
383 * -----------------------------------------------------------------------*/
384 vg_str preview = folder;
385 vg_strcat( &preview, "/preview.jpg" );
386
387 if( !vg_strgood(&preview) ){
388 vg_error( "preview image path too long\n" );
389 workshop_form.op = k_workshop_op_none;
390 return;
391 }
392
393 int w = workshop_form.img_w,
394 h = workshop_form.img_h;
395
396 vg_info( "writing: %s (%dx%d @90%%)\n", preview.buffer, w,h );
397 stbi_flip_vertically_on_write(1);
398 stbi_write_jpg( preview.buffer, w,h, 3, workshop_form.img_buffer, 90 );
399
400 vg_async_call( workshop_form_async_submit_begin, short_descriptor_str, 0 );
401 }
402
403 /*
404 * Entry point for the publishing submission operation
405 */
406 static void workshop_op_submit(void){
407 /* TODO: Show these errors to the user */
408 if( workshop_form.submission.submit_title ){
409 if( !workshop_form.submission.title[0] ){
410 ui_start_modal( "Cannot submit because a title is required\n",
411 UI_MODAL_WARN);
412 workshop_form.op = k_workshop_op_none;
413 return;
414 }
415 }
416
417 if( workshop_form.submission.submit_description ){
418 if( !workshop_form.submission.description[0] ){
419 ui_start_modal( "Cannot submit because a description is required\n",
420 UI_MODAL_WARN );
421 workshop_form.op = k_workshop_op_none;
422 return;
423 }
424 }
425
426 if( workshop_form.submission.submit_file_and_image ){
427 if( workshop_form.file_intent == k_workshop_form_file_intent_none ){
428 ui_start_modal( "Cannot submit because the file is "
429 "empty or unspecified\n", UI_MODAL_WARN );
430 workshop_form.op = k_workshop_op_none;
431 return;
432 }
433 }
434
435 player_board_unload( &workshop_form.board_model );
436 workshop_form.file_intent = k_workshop_form_file_intent_none;
437 workshop_form.op = k_workshop_op_publishing_update;
438
439 vg_loader_start( _workshop_form_submit_thread, NULL );
440 }
441
442 /*
443 * op: k_workshop_form_op_loading_model
444 * -----------------------------------------------------------------------------
445 */
446
447 /*
448 * Reciever for completion of the model file load
449 */
450 static void workshop_form_loadmodel_async_complete( void *payload, u32 size )
451 {
452 v2_zero( workshop_form.view_angles );
453 v3_zero( workshop_form.view_offset );
454 workshop_form.view_dist = 1.0f;
455 workshop_form.view_changed = 1;
456 workshop_form.file_intent = k_workshop_form_file_intent_new;
457
458 vg_success( "workshop async load complete\n" );
459 workshop_form.op = k_workshop_op_none;
460 }
461
462 /*
463 * Reciever for failure to load
464 */
465 static void workshop_form_loadmodel_async_error( void *payload, u32 size ){
466 }
467
468 /*
469 * Thread which loads the model from the disk
470 */
471 static void _workshop_form_load_thread( void *data )
472 {
473 char path_buf[4096];
474 vg_str folder;
475 vg_strnull( &folder, path_buf, 4096 );
476
477 vg_strcat( &folder, workshop_filetype_folder() );
478 vg_strcat( &folder, workshop_form.addon_folder );
479
480 if( !vg_strgood(&folder) ){
481 vg_error( "workshop async load failed: path too long\n" );
482 vg_async_call( workshop_form_loadmodel_async_error, NULL, 0 );
483 workshop_form.op = k_workshop_op_none;
484 return;
485 }
486
487 vg_dir dir;
488 if( !vg_dir_open( &dir, folder.buffer ) ){
489 vg_error( "workshop async load failed: could not open folder\n" );
490 vg_async_call( workshop_form_loadmodel_async_error, NULL, 0 );
491 workshop_form.op = k_workshop_op_none;
492 return;
493 }
494
495 vg_info( "Searching %s for model files\n", folder.buffer );
496
497 int found_mdl = 0;
498 while( vg_dir_next_entry(&dir) ){
499 if( vg_dir_entry_type(&dir) == k_vg_entry_type_file ){
500 const char *d_name = vg_dir_entry_name(&dir);
501 if( d_name[0] == '.' ) continue;
502
503 vg_str file = folder;
504 vg_strcat( &file, "/" );
505 vg_strcat( &file, d_name );
506 if( !vg_strgood( &file ) ) continue;
507
508 char *ext = vg_strch( &file, '.' );
509 if( !ext ) continue;
510 if( strcmp(ext,".mdl") ) continue;
511 found_mdl = 1;
512 break;
513 }
514 }
515 vg_dir_close(&dir);
516
517 if( !found_mdl ){
518 vg_error( "workshop async load failed: no model files found\n" );
519 vg_async_call( workshop_form_loadmodel_async_error, NULL, 0 );
520 workshop_form.op = k_workshop_op_none;
521 return;
522 }
523
524 if( workshop_form.submission.type == k_addon_type_board )
525 player_board_load( &workshop_form.board_model, path_buf );
526 else if( workshop_form.submission.type == k_addon_type_player )
527 player_model_load( &workshop_form.player_model, path_buf );
528
529 vg_async_call( workshop_form_loadmodel_async_complete, NULL, 0 );
530 }
531
532 /*
533 * Entry point for load model operation
534 */
535 static void workshop_op_load_model(void){
536 world_instance *world = world_current_instance();
537 workshop_form.view_world = world;
538
539 if( workshop_form.submission.type == k_addon_type_board ){
540 if( mdl_arrcount( &world->ent_swspreview ) ){
541 workshop_form.ptr_ent =
542 mdl_arritm( &world->ent_swspreview, 0 );
543 }
544 else{
545 ui_start_modal( "There is no ent_swspreview in the level. \n"
546 "Cannot publish here\n", UI_MODAL_BAD );
547 workshop_form.op = k_workshop_op_none;
548 return;
549 }
550 }
551 else if( workshop_form.submission.type == k_addon_type_player ){}
552 else {
553 ui_start_modal( "Don't know how to prepare for this item type. \n"
554 "Please contact the developers.\n", UI_MODAL_BAD );
555 workshop_form.op = k_workshop_op_none;
556 return;
557 }
558
559 workshop_form.op = k_workshop_op_loading_model;
560 vg_loader_start( _workshop_form_load_thread, NULL );
561 }
562
563 /*
564 * op: k_workshop_form_op_downloading_submission
565 * -----------------------------------------------------------------------------
566 */
567
568 /*
569 * The image has been decoded and is ready to slap into the framebuffer
570 */
571 static void workshop_form_async_imageload( void *data, u32 len )
572 {
573 if( data ){
574 struct framebuffer_attachment *a =
575 &gpipeline.fb_workshop_preview->attachments[0];
576
577 glBindTexture( GL_TEXTURE_2D, a->id );
578 glTexSubImage2D( GL_TEXTURE_2D, 0,0,0,
579 WORKSHOP_PREVIEW_WIDTH, WORKSHOP_PREVIEW_HEIGHT,
580 a->format, a->type, data );
581 stbi_image_free( data );
582 vg_success( "Loaded workshop preview image\n" );
583 }
584 else{
585 snprintf( workshop_form.error_msg, sizeof(workshop_form.error_msg),
586 "Preview image could not be loaded. Reason: %s\n",
587 stbi_failure_reason() );
588 ui_start_modal( workshop_form.error_msg, UI_MODAL_BAD );
589 }
590 workshop_form.op = k_workshop_op_none;
591 }
592
593 /*
594 * Load the image located at ./workshop_preview.jpg into our framebuffer
595 */
596 static void _workshop_load_preview_thread( void *data ){
597 char path_buf[ 4096 ];
598 vg_str path;
599 vg_strnull( &path, path_buf, 4096 );
600 vg_strcat( &path, workshop_filetype_folder() );
601 vg_strcat( &path, workshop_form.addon_folder );
602 vg_strcat( &path, "/preview.jpg" );
603
604 if( vg_strgood( &path ) ){
605 stbi_set_flip_vertically_on_load(1);
606 int x, y, nc;
607 u8 *rgb = stbi_load( path.buffer, &x, &y, &nc, 3 );
608
609 if( rgb ){
610 if( (x == WORKSHOP_PREVIEW_WIDTH) && (y == WORKSHOP_PREVIEW_HEIGHT) ){
611 vg_async_call( workshop_form_async_imageload, rgb, x*y*3 );
612 }
613 else{
614 vg_error( "Resolution does not match framebuffer, so we can't"
615 " show it\n" );
616 stbi_image_free( rgb );
617 vg_async_call( workshop_form_async_imageload, NULL, 0 );
618 }
619 }
620 else{
621 vg_async_call( workshop_form_async_imageload, NULL, 0 );
622 }
623 }
624 else{
625 vg_async_call( workshop_form_async_imageload, NULL, 0 );
626 }
627 }
628
629 /*
630 * Entry point to view operation
631 */
632 static void workshop_op_download_and_view_submission( int result_index )
633 {
634 workshop_form.op = k_workshop_op_downloading_submission;
635 ISteamUGC *hSteamUGC = SteamAPI_SteamUGC();
636 ISteamRemoteStorage *hSteamRemoteStorage = SteamAPI_SteamRemoteStorage();
637 SteamUGCDetails_t details;
638 if( SteamAPI_ISteamUGC_GetQueryUGCResult( hSteamUGC,
639 workshop_form.ugc_query.handle,
640 result_index,
641 &details ) )
642 {
643 workshop_reset_submission_data();
644 workshop_form.submission.submit_description = 0;
645 workshop_form.submission.submit_file_and_image = 0;
646 workshop_form.submission.submit_title = 0;
647
648 u8 metadata_buf[512];
649 char metadata_str[1024+1];
650 int have_meta = SteamAPI_ISteamUGC_GetQueryUGCMetadata( hSteamUGC,
651 workshop_form.ugc_query.handle,
652 result_index, metadata_str,
653 1024+1 );
654
655 vg_strncpy( details.m_rgchDescription,
656 workshop_form.submission.description,
657 vg_list_size( workshop_form.submission.description ),
658 k_strncpy_always_add_null );
659
660 vg_strncpy( details.m_rgchTitle,
661 workshop_form.submission.title,
662 vg_list_size( workshop_form.submission.title ),
663 k_strncpy_always_add_null );
664
665 snprintf( workshop_form.addon_folder,
666 vg_list_size( workshop_form.addon_folder ),
667 "Steam Cloud ("PRINTF_U64")", details.m_nPublishedFileId );
668
669 workshop_form.submission.file_id = details.m_nPublishedFileId;
670 workshop_form.file_intent = k_workshop_form_file_intent_keep_old;
671 workshop_form.page = k_workshop_form_edit;
672 workshop_form.submission.visibility = details.m_eVisibility;
673 workshop_form.submission.type = k_addon_type_none;
674 workshop_form.submission.submission_type_selection = k_addon_type_none;
675
676 if( have_meta ){
677 u32 len = strlen(metadata_str);
678 vg_info( "Metadata: %s\n", metadata_str );
679 vg_str_bin( metadata_str, metadata_buf, len );
680 vg_msg msg;
681 vg_msg_init( &msg, metadata_buf, len/2 );
682
683 if( vg_msg_seekframe( &msg, "workshop" )){
684 u32 type = vg_msg_getkvu32( &msg, "type", 0 );
685 workshop_form.submission.type = type;
686 workshop_form.submission.submission_type_selection = type;
687
688 const char *kv_folder = vg_msg_getkvstr( &msg, "folder" );
689 if( kv_folder ){
690 vg_strncpy( kv_folder, workshop_form.addon_folder,
691 sizeof(workshop_form.addon_folder),
692 k_strncpy_always_add_null );
693 }
694 }
695 }
696 else{
697 vg_error( "No metadata was returned with this item.\n" );
698 }
699
700 render_fb_bind( gpipeline.fb_workshop_preview, 0 );
701 glClearColor( 0.2f, 0.0f, 0.0f, 1.0f );
702 glClear( GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT );
703 glBindFramebuffer( GL_FRAMEBUFFER, 0 );
704 glViewport( 0,0, vg.window_x, vg.window_y );
705
706 vg_loader_start( _workshop_load_preview_thread, NULL );
707 }
708 else{
709 vg_error( "GetQueryUGCResult: Index out of range\n" );
710 workshop_form.op = k_workshop_op_none;
711 }
712 }
713
714 /*
715 * Regular stuff
716 * -----------------------------------------------------------------------------
717 */
718
719 /*
720 * View a page of results on the sidebar
721 */
722 static void workshop_view_page( int req )
723 {
724 if( workshop_form.ugc_query.result != k_EResultOK ){
725 vg_error( "Tried to change page without complete data\n" );
726 workshop_form.op = k_workshop_op_none;
727 return;
728 }
729
730 int page = VG_MAX(VG_MIN(req, workshop_form.view_published_page_count-1),0),
731 start = page * WORKSHOP_VIEW_PER_PAGE,
732 end = VG_MIN( (page+1) * WORKSHOP_VIEW_PER_PAGE,
733 workshop_form.ugc_query.returned_item_count ),
734 count = end-start;
735
736 vg_info( "View page %d\n", page );
737
738 workshop_form.view_published_page_id = page;
739 workshop_form.published_files_list_length = count;
740 ISteamUGC *hSteamUGC = SteamAPI_SteamUGC();
741
742 for( int i=0; i<count; i ++ ){
743 struct published_file *pfile = &workshop_form.published_files_list[i];
744
745 SteamUGCDetails_t details;
746 if( SteamAPI_ISteamUGC_GetQueryUGCResult( hSteamUGC,
747 workshop_form.ugc_query.handle,
748 start+i,
749 &details ) )
750 {
751 if( details.m_eResult != k_EResultOK ){
752 snprintf( pfile->title, 80, "Error (%d)", details.m_eResult );
753 }
754 else{
755 vg_strncpy( details.m_rgchTitle, pfile->title, 80,
756 k_strncpy_always_add_null );
757 }
758
759 pfile->result = details.m_eResult;
760 pfile->result_index = start+i;
761 }
762 else{
763 pfile->result = k_EResultValueOutOfRange;
764 pfile->result_index = -1;
765 snprintf( pfile->title, 80, "Error (invalid index)" );
766 }
767 }
768 }
769
770 /*
771 * Steam API result for when we recieve submitted UGC information about the user
772 */
773 static void on_workshop_UGCQueryComplete( void *data, void *userdata )
774 {
775 SteamUGCQueryCompleted_t *query = data;
776 workshop_form.ugc_query.result = query->m_eResult;
777
778 if( query->m_eResult == k_EResultOK ){
779 if( query->m_unTotalMatchingResults > 50 ){
780 vg_warn( "You have %d items submitted, "
781 "we can only view the last 50\n" );
782 }
783
784 workshop_form.ugc_query.all_item_count = query->m_unTotalMatchingResults;
785 workshop_form.ugc_query.returned_item_count =
786 query->m_unNumResultsReturned;
787
788 workshop_form.ugc_query.handle = query->m_handle;
789 workshop_form.view_published_page_count =
790 (query->m_unNumResultsReturned+WORKSHOP_VIEW_PER_PAGE-1)/
791 WORKSHOP_VIEW_PER_PAGE;
792 workshop_form.view_published_page_id = 0;
793 workshop_form.published_files_list_length = 0;
794
795 workshop_view_page( 0 );
796 }
797 else{
798 vg_error( "Steam UGCQuery failed (%d)\n", query->m_eResult );
799 workshop_form.view_published_page_count = 0;
800 workshop_form.view_published_page_id = 0;
801 workshop_form.published_files_list_length = 0;
802
803 ISteamUGC *hSteamUGC = SteamAPI_SteamUGC();
804 SteamAPI_ISteamUGC_ReleaseQueryUGCRequest( hSteamUGC, query->m_handle );
805 }
806 }
807
808 /*
809 * Console command to open the workshop publisher
810 */
811 static int workshop_submit_command( int argc, const char *argv[] )
812 {
813 if( !steam_ready ){
814 ui_start_modal( "Steam API is not initialized\n", UI_MODAL_BAD );
815 return 0;
816 }
817
818 workshop_form.page = k_workshop_form_open;
819 workshop_form.view_published_page_count = 0;
820 workshop_form.view_published_page_id = 0;
821 workshop_form.published_files_list_length = 0;
822 workshop_form.ugc_query.result = k_EResultNone;
823
824 vg_steam_async_call *call = vg_alloc_async_steam_api_call();
825
826 ISteamUser *hSteamUser = SteamAPI_SteamUser();
827 CSteamID steamid;
828 steamid.m_unAll64Bits = SteamAPI_ISteamUser_GetSteamID( hSteamUser );
829
830 ISteamUGC *hSteamUGC = SteamAPI_SteamUGC();
831 call->p_handler = on_workshop_UGCQueryComplete;
832 call->userdata = NULL;
833 UGCQueryHandle_t handle = SteamAPI_ISteamUGC_CreateQueryUserUGCRequest
834 (
835 hSteamUGC,
836 steamid.m_comp.m_unAccountID,
837 k_EUserUGCList_Published,
838 k_EUGCMatchingUGCType_Items,
839 k_EUserUGCListSortOrder_CreationOrderDesc,
840 SKATERIFT_APPID, SKATERIFT_APPID,
841 1 );
842 SteamAPI_ISteamUGC_SetReturnMetadata( hSteamUGC, handle, 1 );
843 call->id = SteamAPI_ISteamUGC_SendQueryUGCRequest( hSteamUGC, handle );
844 return 0;
845 }
846
847 static void workshop_init(void)
848 {
849 vg_console_reg_cmd( "workshop_submit", workshop_submit_command, NULL );
850 }
851
852 static void workshop_render_world_preview(void){
853 render_fb_bind( gpipeline.fb_workshop_preview, 0 );
854
855 glClearColor( 0.0f, 0.0f, 0.3f, 1.0f );
856 glClear( GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT );
857 glEnable( GL_DEPTH_TEST );
858 glDisable( GL_BLEND );
859
860 render_world( world_current_instance(), &skaterift.cam, 0, 0, 1, 1 );
861
862 glBindFramebuffer( GL_FRAMEBUFFER, 0 );
863 glViewport( 0,0, vg.window_x, vg.window_y );
864 }
865
866 /*
867 * Redraw the playermodel into the workshop framebuffer
868 */
869 static void workshop_render_player_preview(void){
870 render_fb_bind( gpipeline.fb_workshop_preview, 0 );
871 glClearColor( 0.16f, 0.15f, 0.15f, 1.0f );
872 glClear( GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT );
873 glEnable( GL_DEPTH_TEST );
874 glDisable( GL_BLEND );
875
876 struct skeleton *sk = &localplayer.skeleton;
877
878 player_pose res;
879 res.type = k_player_pose_type_ik;
880
881 struct skeleton_anim *anim = skeleton_get_anim( sk, "idle_cycle+y" );
882 skeleton_sample_anim( sk, anim, vg.time*0.1f, res.keyframes );
883 q_axis_angle( res.root_q, (v3f){0.0f,1.0f,0.0f}, VG_PIf );
884 v3_zero( res.root_co );
885 res.root_co[1] = 200.0f;
886
887 m4x3f transform;
888 q_m3x3( res.root_q, transform );
889 v3_copy( res.root_co, transform[3] );
890
891 /* TODO: Function. */
892 skeleton_apply_pose( sk, res.keyframes, k_anim_apply_defer_ik,
893 localplayer.final_mtx );
894 skeleton_apply_ik_pass( sk, localplayer.final_mtx );
895 skeleton_apply_pose( sk, res.keyframes, k_anim_apply_deffered_only,
896 localplayer.final_mtx );
897 skeleton_apply_inverses( sk, localplayer.final_mtx );
898 skeleton_apply_transform( sk, transform, localplayer.final_mtx );
899
900 camera cam;
901 v3_copy( (v3f){ 0.0f, 201.7f, 1.2f }, cam.pos );
902
903 cam.nearz = 0.01f;
904 cam.farz = 100.0f;
905 cam.fov = 57.0f;
906 v3_zero( cam.angles );
907
908 camera_update_transform( &cam );
909 camera_update_view( &cam );
910 camera_update_projection( &cam );
911 camera_finalize( &cam );
912
913 render_playermodel( &cam, world_current_instance(), 0,
914 &workshop_form.player_model, sk, localplayer.final_mtx );
915
916 glBindFramebuffer( GL_FRAMEBUFFER, 0 );
917 glViewport( 0,0, vg.window_x, vg.window_y );
918 }
919
920 /*
921 * Redraw the model file into the workshop framebuffer
922 */
923 static void workshop_render_board_preview(void){
924 if( !workshop_form.ptr_ent ){
925 return;
926 }
927
928 render_fb_bind( gpipeline.fb_workshop_preview, 0 );
929
930 glClearColor( 0.0f, 0.0f, 0.3f, 1.0f );
931 glClear( GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT );
932 glEnable( GL_DEPTH_TEST );
933 glDisable( GL_BLEND );
934
935 ent_swspreview *swsprev = workshop_form.ptr_ent;
936 world_instance *world = workshop_form.view_world;
937
938 ent_camera *ref = mdl_arritm( &world->ent_camera,
939 mdl_entity_id_id(swsprev->id_camera) );
940 ent_marker *display = mdl_arritm( &world->ent_marker,
941 mdl_entity_id_id(swsprev->id_display) ),
942 *display1= mdl_arritm( &world->ent_marker,
943 mdl_entity_id_id(swsprev->id_display1) );
944
945 v3f baseco;
946 v3_add( display->transform.co, display1->transform.co, baseco );
947 v3_muls( baseco, 0.5f, baseco );
948
949 camera cam;
950 v3f basevector;
951 v3_sub( display->transform.co, ref->transform.co, basevector );
952 float dist = v3_length( basevector );
953
954 v3f baseangles;
955 v3_angles( basevector, baseangles );
956
957 v2_add( workshop_form.view_angles, baseangles, cam.angles );
958 cam.angles[2] = 0.0f;
959
960 float sX = sinf( cam.angles[0] ),
961 cX = cosf( cam.angles[0] ),
962 sY = sinf( cam.angles[1] ),
963 cY = cosf( cam.angles[1] );
964
965 v3f offset = { -sX * cY, sY, cX * cY };
966
967 v3_muladds( display->transform.co, offset,
968 dist*workshop_form.view_dist, cam.pos );
969
970 cam.pos[0] += -sX*workshop_form.view_offset[2];
971 cam.pos[2] += cX*workshop_form.view_offset[2];
972 cam.pos[0] += cX*workshop_form.view_offset[0];
973 cam.pos[2] += sX*workshop_form.view_offset[0];
974 cam.pos[1] += workshop_form.view_offset[1];
975
976 cam.nearz = 0.01f;
977 cam.farz = 100.0f;
978 cam.fov = ref->fov;
979
980 camera_update_transform( &cam );
981 camera_update_view( &cam );
982 camera_update_projection( &cam );
983 camera_finalize( &cam );
984
985 m4x3f mmdl, mmdl1;
986 mdl_transform_m4x3( &display->transform, mmdl );
987 mdl_transform_m4x3( &display1->transform, mmdl1 );
988
989 /* force update this for nice shadows. its usually set in the world
990 * pre-render step, but that includes timer stuff
991 */
992 struct player_board *board = &workshop_form.board_model;
993 struct ub_world_lighting *ubo = &world->ub_lighting;
994 v3f vp0, vp1;
995 v3_copy((v3f){0.0f,0.1f, board->truck_positions[0][2]}, vp0 );
996 v3_copy((v3f){0.0f,0.1f, board->truck_positions[1][2]}, vp1 );
997 m4x3_mulv( mmdl1, vp0, ubo->g_board_0 );
998 m4x3_mulv( mmdl1, vp1, ubo->g_board_1 );
999 glBindBuffer( GL_UNIFORM_BUFFER, world->ubo_lighting );
1000 glBufferSubData( GL_UNIFORM_BUFFER, 0,
1001 sizeof(struct ub_world_lighting), &world->ub_lighting );
1002
1003 render_world( world, &cam, 0, 0, 1, 1 );
1004 struct player_board_pose pose = {0};
1005 render_board( &cam, world, board, mmdl, &pose, k_board_shader_entity );
1006 render_board( &cam, world, board, mmdl1, &pose, k_board_shader_entity );
1007
1008 glBindFramebuffer( GL_FRAMEBUFFER, 0 );
1009 glViewport( 0,0, vg.window_x, vg.window_y );
1010 }
1011
1012 /*
1013 * ImGUI section for workshop form
1014 * -----------------------------------------------------------------------------
1015 */
1016
1017 static void workshop_changed_model_path( char *buf, u32 len ){
1018 workshop_form.submission.submit_file_and_image = 1;
1019 }
1020
1021 static void workshop_changed_title( char *buf, u32 len ){
1022 workshop_form.submission.submit_title = 1;
1023 }
1024
1025 static void workshop_changed_description( char *buf, u32 len ){
1026 workshop_form.submission.submit_description = 1;
1027 }
1028
1029 static void workshop_form_gui_page_undecided( ui_rect content ){
1030 ui_rect box;
1031 rect_copy( content, box );
1032 box[3] = 128;
1033 box[2] = (box[2]*2)/3;
1034 ui_rect_center( content, box );
1035
1036 ui_rect row;
1037 ui_split( box, k_ui_axis_h, 28, 0, row, box );
1038 ui_text( row, "Select the type of item\n", 1, k_ui_align_middle_center,0);
1039 ui_split( box, k_ui_axis_h, 28, 0, row, box );
1040 ui_enum( row, "Type:", workshop_form_type_opts,
1041 4, &workshop_form.submission.submission_type_selection );
1042 ui_split( box, k_ui_axis_h, 8, 0, row, box );
1043 ui_split( box, k_ui_axis_h, 28, 0, row, box );
1044
1045 ui_rect button_l, button_r;
1046 rect_copy( row, button_l );
1047 button_l[2] = 128*2;
1048 ui_rect_center( row, button_l );
1049 ui_split_ratio( button_l, k_ui_axis_v, 0.5f, 2, button_l, button_r );
1050
1051 enum addon_type type = workshop_form.submission.submission_type_selection;
1052 if( type != k_addon_type_none){
1053 if( ui_button_text( button_l, "OK", 1 ) == 1 ){
1054 workshop_form.submission.type = type;
1055
1056 if( type == k_addon_type_world ){
1057 workshop_form.view_changed = 1;
1058 workshop_form.file_intent = k_workshop_form_file_intent_new;
1059 }
1060 }
1061 }
1062 else{
1063 ui_fill( button_l, ui_colour(k_ui_bg) );
1064 ui_text( button_l, "OK", 1, k_ui_align_middle_center,
1065 ui_colour(k_ui_bg+4) );
1066 }
1067
1068 if( ui_button_text( button_r, "Cancel", 1 ) == 1 ){
1069 workshop_form.page = k_workshop_form_open;
1070 workshop_form.file_intent = k_workshop_form_file_intent_none;
1071 }
1072 }
1073
1074 static void workshop_form_gui_draw_preview( ui_rect img_box ){
1075 enum addon_type type = workshop_form.submission.type;
1076 if( workshop_form.file_intent == k_workshop_form_file_intent_keep_old ){
1077 ui_image( img_box, gpipeline.fb_workshop_preview->attachments[0].id );
1078 }
1079 else if( workshop_form.file_intent == k_workshop_form_file_intent_new ){
1080 ui_image( img_box, gpipeline.fb_workshop_preview->attachments[0].id );
1081
1082 if( type == k_addon_type_world ){
1083 return;
1084 }
1085
1086 int hover = ui_inside_rect( img_box, vg_ui.mouse ),
1087 target = ui_inside_rect( img_box, vg_ui.mouse_click );
1088
1089 if( ui_click_down(UI_MOUSE_MIDDLE) && target ){
1090 v3_copy( workshop_form.view_offset,
1091 workshop_form.view_offset_begin );
1092 }
1093 else if( ui_click_down(UI_MOUSE_LEFT) && target ){
1094 v2_copy( workshop_form.view_angles,
1095 workshop_form.view_angles_begin );
1096 }
1097
1098 if( ui_clicking(UI_MOUSE_MIDDLE) && target ){
1099 v2f delta = { vg_ui.mouse[0]-vg_ui.mouse_click[0],
1100 vg_ui.mouse[1]-vg_ui.mouse_click[1] };
1101
1102 float *begin = workshop_form.view_offset_begin,
1103 *offset = workshop_form.view_offset;
1104 offset[0] = vg_clampf( begin[0]-delta[0]*0.002f, -1.0f, 1.0f );
1105 offset[2] = vg_clampf( begin[2]-delta[1]*0.002f, -1.0f, 1.0f );
1106 workshop_form.view_changed = 1;
1107 }
1108 else if( ui_clicking(UI_MOUSE_LEFT) && target ){
1109 v2f delta = { vg_ui.mouse[0]-vg_ui.mouse_click[0],
1110 vg_ui.mouse[1]-vg_ui.mouse_click[1] };
1111
1112 v2f angles;
1113 v2_muladds( workshop_form.view_angles_begin, delta, 0.002f, angles);
1114
1115 float limit = VG_PIf*0.2f;
1116
1117 angles[0] = vg_clampf( angles[0], -limit, limit );
1118 angles[1] = vg_clampf( angles[1], -limit, limit );
1119
1120 v2_copy( angles, workshop_form.view_angles );
1121 workshop_form.view_changed = 1;
1122 }
1123
1124 if( !ui_clicking(UI_MOUSE_LEFT) && hover ){
1125 float zoom = workshop_form.view_dist;
1126 zoom += vg.mouse_wheel[1] * -0.07f;
1127 zoom = vg_clampf( zoom, 0.4f, 2.0f );
1128
1129 if( zoom != workshop_form.view_dist ){
1130 workshop_form.view_changed = 1;
1131 workshop_form.view_dist = zoom;
1132 }
1133 }
1134 }
1135 else{
1136 ui_text( img_box, "No image", 1, k_ui_align_middle_center,
1137 ui_colour( k_ui_orange ) );
1138 }
1139 }
1140
1141 static void workshop_form_gui_edit_page( ui_rect content ){
1142 enum addon_type type = workshop_form.submission.type;
1143
1144 if( type == k_addon_type_none ){
1145 workshop_form_gui_page_undecided( content );
1146 return;
1147 }
1148
1149 ui_rect image_plane;
1150 ui_split( content, k_ui_axis_h, 300, 0, image_plane, content );
1151 ui_fill( image_plane, ui_colour( k_ui_bg+0 ) );
1152
1153 ui_rect img_box;
1154 ui_fit_item( image_plane, (ui_px[2]){ 3, 2 }, img_box );
1155 workshop_form_gui_draw_preview( img_box );
1156
1157 /* file path */
1158 ui_rect file_button, file_label;
1159
1160 char buf[128];
1161 snprintf( buf, 128,
1162 "Addon folder: skaterift/%s", workshop_filetype_folder() );
1163
1164 if( type == k_addon_type_world ){
1165 struct ui_textbox_callbacks callbacks = {
1166 .change = workshop_changed_model_path
1167 };
1168 ui_textbox( content, buf, workshop_form.addon_folder,
1169 vg_list_size(workshop_form.addon_folder), 1, 0, &callbacks );
1170 }
1171 else{
1172 ui_rect file_entry;
1173 ui_standard_widget( content, file_entry, 1 );
1174 ui_split( file_entry, k_ui_axis_v, -128, 0, file_entry, file_button );
1175
1176 if( workshop_form.file_intent != k_workshop_form_file_intent_none ){
1177 ui_text( file_entry, workshop_form.addon_folder, 1,
1178 k_ui_align_middle_left, ui_colour( k_ui_fg+4 ) );
1179
1180 if( ui_button_text( file_button, "Remove", 1 ) == 1 ){
1181 if( type == k_addon_type_board )
1182 player_board_unload( &workshop_form.board_model );
1183 else if( type == k_addon_type_player )
1184 player_model_unload( &workshop_form.player_model );
1185
1186 workshop_form.file_intent = k_workshop_form_file_intent_none;
1187 workshop_form.addon_folder[0] = '\0';
1188 }
1189 }
1190 else{
1191 struct ui_textbox_callbacks callbacks = {
1192 .change = workshop_changed_model_path
1193 };
1194
1195 ui_textbox( file_entry, buf, workshop_form.addon_folder,
1196 vg_list_size(workshop_form.addon_folder), 1,
1197 0, &callbacks );
1198
1199 if( ui_button_text( file_button, "Load", 1 ) == 1 ){
1200 workshop_op_load_model();
1201 }
1202 }
1203 }
1204
1205 const char *str_title = "Title:", *str_desc = "Description:";
1206
1207 /* title box */
1208 {
1209 struct ui_textbox_callbacks callbacks = {
1210 .change = workshop_changed_title
1211 };
1212 ui_textbox( content, str_title, workshop_form.submission.title,
1213 vg_list_size(workshop_form.submission.title), 1,
1214 0, &callbacks );
1215 }
1216
1217 /* visibility option */
1218 {
1219 ui_enum( content, "Visibility:", workshop_form_visibility_opts,
1220 4, &workshop_form.submission.visibility );
1221 }
1222
1223 /* description box */
1224 {
1225 struct ui_textbox_callbacks callbacks = {
1226 .change = workshop_changed_description
1227 };
1228 ui_textbox( content, str_desc, workshop_form.submission.description,
1229 vg_list_size(workshop_form.submission.description), 4,
1230 UI_TEXTBOX_MULTILINE|UI_TEXTBOX_WRAP, &callbacks );
1231 }
1232
1233 /* submissionable */
1234 ui_rect final_row;
1235 ui_split( content, k_ui_axis_h, content[3]-32-8, 0, content, final_row );
1236
1237 ui_rect submission_center;
1238 rect_copy( final_row, submission_center );
1239 submission_center[2] = 256;
1240 ui_rect_center( final_row, submission_center );
1241
1242 ui_rect btn_left, btn_right;
1243 ui_split_ratio( submission_center, k_ui_axis_v, 0.5f, 8,
1244 btn_left, btn_right );
1245
1246 if( ui_button_text( btn_left, "Publish", 1 ) == 1 ){
1247 workshop_op_submit();
1248 }
1249 if( ui_button_text( btn_right, "Cancel", 1 ) == 1 ){
1250 workshop_form.page = k_workshop_form_open;
1251 player_board_unload( &workshop_form.board_model );
1252 workshop_form.file_intent = k_workshop_form_file_intent_none;
1253 }
1254
1255 /* disclaimer */
1256 const char *disclaimer_text =
1257 "By submitting this item, you agree to the workshop terms of service";
1258
1259 ui_rect disclaimer_row, inner, link;
1260 ui_split( content, k_ui_axis_h, content[3]-32, 0, content, disclaimer_row );
1261
1262 ui_px btn_width = 32;
1263
1264 rect_copy( disclaimer_row, inner );
1265 inner[2] = ui_text_line_width( disclaimer_text ) + btn_width+8;
1266
1267 ui_rect label;
1268 ui_rect_center( disclaimer_row, inner );
1269 ui_split( inner, k_ui_axis_v, inner[2]-btn_width, 0, label, btn_right);
1270 ui_rect_pad( btn_right, (ui_px[2]){2,2} );
1271
1272 if( ui_button_text( btn_right, "\xbf", 2 ) == 1 ){
1273 ISteamFriends *hSteamFriends = SteamAPI_SteamFriends();
1274 SteamAPI_ISteamFriends_ActivateGameOverlayToWebPage( hSteamFriends,
1275 "https://steamcommunity.com/sharedfiles/workshoplegalagreement",
1276 k_EActivateGameOverlayToWebPageMode_Default );
1277 }
1278
1279 ui_text( label, disclaimer_text, 1, k_ui_align_middle_left,
1280 ui_colour( k_ui_fg+4 ) );
1281 }
1282
1283 static void workshop_form_gui_sidebar( ui_rect sidebar )
1284 {
1285 ui_fill( sidebar, ui_colour( k_ui_bg+2 ) );
1286
1287 ui_rect title;
1288 ui_split( sidebar, k_ui_axis_h, 28, 0, title, sidebar );
1289
1290 if( workshop_form.page == k_workshop_form_edit ){
1291 ui_text( title, "Editing", 1, k_ui_align_middle_center, 0 );
1292 ui_split( sidebar, k_ui_axis_h, 28, 0, title, sidebar );
1293
1294 if( workshop_form.submission.type != k_addon_type_none ){
1295 char buf[512];
1296 vg_str str;
1297 vg_strnull( &str, buf, 512 );
1298
1299 if( workshop_form.submission.file_id )
1300 vg_strcat( &str, "Editing an existing " );
1301 else
1302 vg_strcat( &str, "Creating a new " );
1303
1304 if( workshop_form.submission.type == k_addon_type_board )
1305 vg_strcat( &str, "skateboard." );
1306 else if( workshop_form.submission.type == k_addon_type_world )
1307 vg_strcat( &str, "world." );
1308 else if( workshop_form.submission.type == k_addon_type_player )
1309 vg_strcat( &str, "playermodel." );
1310 else
1311 vg_strcat( &str, "???." );
1312
1313 ui_text( title, buf, 1, k_ui_align_middle_center,
1314 ui_colour(k_ui_fg+4) );
1315 }
1316 return;
1317 }
1318
1319 /*
1320 * sidebar existing entries panel
1321 */
1322 ui_text( title, "Your submissions", 1, k_ui_align_middle_center, 0 );
1323
1324 ui_rect controls, btn_create_new;
1325 ui_split( sidebar, k_ui_axis_h, 32, 0, controls, sidebar );
1326 ui_split( sidebar, k_ui_axis_h, -32, 0, sidebar, btn_create_new );
1327 ui_fill( controls, ui_colour( k_ui_bg+1 ) );
1328
1329 char buf[32];
1330 strcpy( buf, "page " );
1331 int i = 5;
1332 i += highscore_intl( buf+i, workshop_form.view_published_page_id+1, 4 );
1333 buf[ i ++ ] = '/';
1334 i += highscore_intl( buf+i, workshop_form.view_published_page_count, 4 );
1335 buf[ i ++ ] = '\0';
1336
1337 ui_rect_pad( controls, (ui_px[2]){0,4} );
1338 ui_rect info;
1339 ui_split_ratio( controls, k_ui_axis_v, 0.25f, 0, info, controls );
1340 ui_text( info, buf, 1, k_ui_align_middle_center, 0 );
1341
1342 ui_rect btn_left, btn_right;
1343 ui_split_ratio( controls, k_ui_axis_v, 0.5f, 2, btn_left, btn_right );
1344
1345 if( ui_button_text( btn_left, "newer", 1 ) == 1 ){
1346 workshop_view_page( workshop_form.view_published_page_id-1 );
1347 }
1348
1349 if( ui_button_text( btn_right, "older", 1 ) == 1 ){
1350 workshop_view_page( workshop_form.view_published_page_count+1 );
1351 }
1352
1353 if( ui_button_text( btn_create_new, "Create New Item", 1 ) == 1 ){
1354 workshop_reset_submission_data();
1355 workshop_form.submission.submit_title = 1;
1356 workshop_form.submission.submit_description = 1;
1357 workshop_form.submission.submit_file_and_image = 1;
1358 workshop_form.page = k_workshop_form_edit;
1359 }
1360
1361 for( int i=0; i<workshop_form.published_files_list_length; i++ ){
1362 ui_rect item;
1363 ui_split( sidebar, k_ui_axis_h, 28, 0, item, sidebar );
1364 ui_rect_pad( item, (ui_px[2]){4,4} );
1365
1366 struct published_file *pfile = &workshop_form.published_files_list[i];
1367 if( ui_button_text( item, pfile->title, 1 ) == 1 ){
1368 if( pfile->result == k_EResultOK ){
1369 vg_info( "Select index: %d\n", pfile->result_index );
1370 workshop_op_download_and_view_submission( pfile->result_index );
1371 }
1372 else{
1373 vg_warn( "Cannot select that item, result not OK\n" );
1374 }
1375 }
1376 }
1377 }
1378
1379 static void workshop_form_gui(void)
1380 {
1381 enum workshop_form_page stable_page = workshop_form.page;
1382 if( stable_page == k_workshop_form_hidden ) return;
1383
1384 ui_rect null;
1385 ui_rect screen = { 0, 0, vg.window_x, vg.window_y };
1386 ui_rect window = { 0, 0, 1000, 700 };
1387 ui_rect_center( screen, window );
1388 vg_ui.wants_mouse = 1;
1389
1390 ui_fill( window, ui_colour( k_ui_bg+1 ) );
1391 ui_outline( window, 1, ui_colour( k_ui_bg+7 ), 0 );
1392
1393 ui_rect title, panel;
1394 ui_split( window, k_ui_axis_h, 28, 0, title, panel );
1395 ui_fill( title, ui_colour( k_ui_bg+7 ) );
1396 ui_text( title, "Workshop tool", 1, k_ui_align_middle_center,
1397 ui_colourcont(k_ui_bg+7) );
1398
1399 ui_rect quit_button;
1400 ui_split( title, k_ui_axis_v, title[2]-title[3], 2, title, quit_button );
1401
1402 if( vg_loader_availible() ){
1403 if( ui_button_text( quit_button, "X", 1 ) == 1 ){
1404 workshop_quit_form();
1405 return;
1406 }
1407 }
1408
1409 /*
1410 * temporary operation blinders, we don't yet have a nice way to show the
1411 * user that we're doing something uninterruptable, so the code just
1412 * escapes here and we show them a basic string
1413 */
1414
1415 if( workshop_form.op != k_workshop_op_none ){
1416 const char *op_string = "The programmer has not bothered to describe "
1417 "the current operation that is running.";
1418
1419 switch( workshop_form.op ){
1420 case k_workshop_op_loading_model:
1421 op_string = "Operation in progress: Loading model file.";
1422 break;
1423 case k_workshop_op_publishing_update:
1424 op_string = "Operation in progress: publishing submission update "
1425 "to steam.";
1426 break;
1427 case k_workshop_op_downloading_submission:
1428 op_string = "Operation in progress: downloading existing submission"
1429 " from Steam services.";
1430 break;
1431 default: break;
1432 }
1433
1434 ui_text( panel, op_string, 1, k_ui_align_middle_center, 0 );
1435 return;
1436 }
1437
1438 /* re draw board preview if need to */
1439 if( (stable_page == k_workshop_form_edit) &&
1440 workshop_form.view_changed &&
1441 workshop_form.file_intent == k_workshop_form_file_intent_new )
1442 {
1443 enum addon_type type = workshop_form.submission.type;
1444 if( type == k_addon_type_board ){
1445 workshop_render_board_preview();
1446 }
1447 else if( type == k_addon_type_world ){
1448 vg_success( "Renders world preview\n" );
1449 workshop_render_world_preview();
1450 }
1451 else if( type == k_addon_type_player ){
1452 workshop_render_player_preview();
1453 }
1454
1455 workshop_form.view_changed = 0;
1456 }
1457
1458 struct workshop_form *form = &workshop_form;
1459
1460 ui_rect sidebar, content;
1461 ui_split_ratio( panel, k_ui_axis_v, 0.3f, 1, sidebar, content );
1462
1463 /* content page */
1464 ui_rect_pad( content, (ui_px[2]){8,8} );
1465
1466 if( stable_page == k_workshop_form_edit ){
1467 workshop_form_gui_edit_page( content );
1468 }
1469 else if( stable_page == k_workshop_form_open ){
1470 ui_text( content, "Nothing selected.", 1, k_ui_align_middle_center,
1471 ui_colour( k_ui_fg+4 ) );
1472 }
1473 else if( stable_page >= k_workshop_form_cclosing ){
1474 ui_rect submission_row;
1475 ui_split( content, k_ui_axis_h, content[3]-32-8, 0, content,
1476 submission_row );
1477
1478 u32 colour;
1479
1480 if( stable_page == k_workshop_form_closing_bad )
1481 colour = ui_colour( k_ui_red+k_ui_brighter );
1482 else
1483 colour = ui_colour( k_ui_green+k_ui_brighter );
1484
1485 ui_text( content, workshop_form.failure_or_success_string, 1,
1486 k_ui_align_middle_center, colour );
1487
1488 ui_rect submission_center;
1489 rect_copy( submission_row, submission_center );
1490 submission_center[2] = 128;
1491 ui_rect_center( submission_row, submission_center );
1492 ui_rect_pad( submission_center, (ui_px[2]){8,8} );
1493
1494 if( ui_button_text( submission_center, "OK", 1 ) == 1 ){
1495 workshop_form.page = k_workshop_form_open;
1496 }
1497 }
1498
1499 workshop_form_gui_sidebar( sidebar );
1500 }
1501
1502 /*
1503 * Some async api stuff
1504 * -----------------------------------------------------------------------------
1505 */
1506
1507 static void async_workshop_get_filepath( void *data, u32 len )
1508 {
1509 struct async_workshop_filepath_info *info = data;
1510
1511 u64 _size;
1512 u32 _ts;
1513
1514 ISteamUGC *hSteamUGC = SteamAPI_SteamUGC();
1515 if( !SteamAPI_ISteamUGC_GetItemInstallInfo( hSteamUGC, info->id, &_size,
1516 info->buf, info->len, &_ts ))
1517 {
1518 vg_error( "GetItemInstallInfo failed\n" );
1519 info->buf[0] = '\0';
1520 }
1521 }
1522
1523 static void async_workshop_get_installed_files( void *data, u32 len )
1524 {
1525 struct async_workshop_installed_files_info *info = data;
1526
1527 ISteamUGC *hSteamUGC = SteamAPI_SteamUGC();
1528 u32 count = SteamAPI_ISteamUGC_GetSubscribedItems( hSteamUGC, info->buffer,
1529 *info->len );
1530
1531 vg_info( "Found %u subscribed items\n", count );
1532
1533 u32 j=0;
1534 for( u32 i=0; i<count; i++ ){
1535 u32 state = SteamAPI_ISteamUGC_GetItemState( hSteamUGC, info->buffer[i] );
1536 if( state & k_EItemStateInstalled ){
1537 info->buffer[j ++] = info->buffer[i];
1538 }
1539 }
1540
1541 *info->len = j;
1542 }
1543
1544 #endif /* WORKSHOP_C */