7 #include "ent_skateshop.h"
9 #include "vg/vg_steam_auth.h"
10 #include "vg/vg_steam_ugc.h"
11 #include "vg/vg_steam_friends.h"
13 #define WORKSHOP_VIEW_PER_PAGE 15
18 char description
[512];
20 PublishedFileId_t file_id
; /* 0 if not published yet */
22 struct ui_dropdown_value visibility
;
24 int submit_title
, /* set if the respective controls are touched */
26 submit_file_and_image
;
30 enum workshop_form_page
{
31 k_workshop_form_hidden
,
32 k_workshop_form_open
, /* open but not looking at anything */
33 k_workshop_form_edit
, /* editing a submission */
34 k_workshop_form_cclosing
,
35 k_workshop_form_closing_good
, /* post upload screen */
36 k_workshop_form_closing_bad
,
41 * -----------------------------
46 struct player_board board_model
;
48 /* what does the user want to do with the image preview? */
49 enum workshop_form_file_intent
{
50 k_workshop_form_file_intent_none
, /* loading probably */
51 k_workshop_form_file_intent_new
, /* board_model is valid */
52 k_workshop_form_file_intent_keep_old
/* just browsing */
56 world_instance
*view_world
;
57 ent_swspreview
*ptr_ent
;
67 * published UGC request
68 * ------------------------------
72 UGCQueryHandle_t handle
;
82 * ------------------------------------------
85 const char *failure_or_success_string
;
90 int view_published_page_count
,
91 view_published_page_id
;
93 struct published_file
{
98 published_files_list
[WORKSHOP_VIEW_PER_PAGE
];
99 int published_files_list_length
;
101 static workshop_form
;
103 static struct ui_dropdown_opt workshop_form_visibility_opts
[] = {
104 { "Public", k_ERemoteStoragePublishedFileVisibilityPublic
},
105 { "Unlisted", k_ERemoteStoragePublishedFileVisibilityUnlisted
},
106 { "Friends Only", k_ERemoteStoragePublishedFileVisibilityFriendsOnly
},
107 { "Private", k_ERemoteStoragePublishedFileVisibilityPrivate
},
111 * Close the form and discard UGC query result
113 VG_STATIC
void workshop_quit_form(void)
115 workshop_begin_op( k_workshop_form_op_none
); /* safeguard */
116 player_board_unload( &workshop_form
.board_model
);
117 workshop_form
.file_intent
= k_workshop_form_file_intent_none
;
119 if( workshop_form
.ugc_query
.result
== k_EResultOK
){
120 workshop_form
.ugc_query
.result
= k_EResultNone
;
122 ISteamUGC
*hSteamUGC
= SteamAPI_SteamUGC();
123 SteamAPI_ISteamUGC_ReleaseQueryUGCRequest(
124 hSteamUGC
, workshop_form
.ugc_query
.handle
);
127 workshop_form
.page
= k_workshop_form_hidden
;
132 * Delete all information about the submission
134 VG_STATIC
void workshop_reset_submission_data(void)
136 workshop_form
.submission
.file_id
= 0; /* assuming id of 0 is none/invalid */
137 workshop_form
.submission
.description
[0] = '\0';
138 workshop_form
.submission
.title
[0] = '\0';
140 workshop_form
.submission
.visibility
.value
=
141 k_ERemoteStoragePublishedFileVisibilityPublic
;
142 workshop_form
.submission
.visibility
.index
= 0;
144 workshop_form
.model_path
[0] = '\0';
145 player_board_unload( &workshop_form
.board_model
);
146 workshop_form
.file_intent
= k_workshop_form_file_intent_none
;
151 * Mostly copies of what it sais on the Steam API documentation
153 VG_STATIC
const char *workshop_EResult_user_string( EResult result
)
156 case k_EResultInsufficientPrivilege
:
157 return "Your account is currently restricted from uploading content "
158 "due to a hub ban, account lock, or community ban. You need to "
159 "contact Steam Support to resolve the issue.";
160 case k_EResultBanned
:
161 return "You do not have permission to upload content to this hub "
162 "because you have an active VAC or Game ban.";
163 case k_EResultTimeout
:
164 return "The operation took longer than expected, so it was discarded. "
166 case k_EResultNotLoggedOn
:
167 return "You are currently not logged into Steam.";
168 case k_EResultServiceUnavailable
:
169 return "The workshop server is having issues or is unavailable, "
171 case k_EResultInvalidParam
:
172 return "One of the submission fields contains something not being "
173 "accepted by that field.";
174 case k_EResultAccessDenied
:
175 return "There was a problem trying to save the title and description. "
176 "Access was denied.";
177 case k_EResultLimitExceeded
:
178 return "You have exceeded your Steam Cloud quota. If you wish to "
179 "upload this file, you must remove some published items.";
180 case k_EResultFileNotFound
:
181 return "The uploaded file could not be found.";
182 case k_EResultDuplicateRequest
:
183 return "The file was already successfully uploaded.";
184 case k_EResultDuplicateName
:
185 return "You already have a Steam Workshop item with that name.";
186 case k_EResultServiceReadOnly
:
187 return "Due to a recent password or email change, you are not allowed "
188 "to upload new content. Usually this restriction will expire in"
189 " 5 days, but can last up to 30 days if the account has been "
190 "inactive recently.";
192 return "Operation failed for an error which has not been accounted for "
193 "by the programmer. Try again, sorry :)";
198 * op: k_workshop_form_op_publishing_update
199 * ----------------------------------------------------------------------------
203 * The endpoint of this operation
205 VG_STATIC
void on_workshop_update_result( void *data
, void *user
)
207 vg_info( "Recieved workshop update result\n" );
208 SubmitItemUpdateResult_t
*result
= data
;
210 /* this seems to be set here, but my account definitely has accepted it */
211 if( result
->m_bUserNeedsToAcceptWorkshopLegalAgreement
){
212 vg_warn( "Workshop agreement currently not accepted\n" );
215 if( result
->m_eResult
== k_EResultOK
){
216 workshop_form
.page
= k_workshop_form_closing_good
;
217 workshop_form
.failure_or_success_string
= "Uploaded workshop file!";
218 vg_success( "file uploaded\n" );
221 workshop_form
.page
= k_workshop_form_closing_bad
;
222 workshop_form
.failure_or_success_string
=
223 workshop_EResult_user_string( result
->m_eResult
);
225 vg_error( "Error with the submitted file (%d)\n", result
->m_eResult
);
231 struct workshop_package_info
{
232 PublishedFileId_t publishing_file_id
;
235 char abs_preview_image
[ 1024 ];
236 char abs_content_folder
[ 1024 ];
237 char abs_content_file
[ 1024 ];
239 const char *failure_reason
;
243 * reciever on completion of packaging the files, it will then start the item
244 * update with Steam API
246 VG_STATIC
void workshop_form_async_package_complete( void *data
, u32 size
)
248 struct workshop_package_info
*info
= data
;
249 if( !info
->success
){
250 workshop_form
.page
= k_workshop_form_closing_bad
;
251 workshop_form
.failure_or_success_string
= info
->failure_reason
;
256 ISteamUGC
*hSteamUGC
= SteamAPI_SteamUGC();
257 UGCUpdateHandle_t handle
258 = SteamAPI_ISteamUGC_StartItemUpdate( hSteamUGC
, SKATERIFT_APPID
,
259 info
->publishing_file_id
);
261 /* TODO: Handle failure cases for these */
263 if( workshop_form
.submission
.submit_title
){
264 vg_info( "Setting title\n" );
265 SteamAPI_ISteamUGC_SetItemTitle( hSteamUGC
, handle
,
266 workshop_form
.submission
.title
);
269 if( workshop_form
.submission
.submit_description
){
270 vg_info( "Setting description\n" );
271 SteamAPI_ISteamUGC_SetItemDescription( hSteamUGC
, handle
,
272 workshop_form
.submission
.description
);
275 if( workshop_form
.submission
.submit_file_and_image
){
276 vg_info( "Setting preview image\n" );
277 SteamAPI_ISteamUGC_SetItemPreview( hSteamUGC
,
278 handle
, info
->abs_preview_image
);
280 vg_info( "Setting item content\n" );
281 SteamAPI_ISteamUGC_SetItemContent( hSteamUGC
, handle
,
282 info
->abs_content_folder
);
285 vg_info( "Setting visibility\n" );
286 SteamAPI_ISteamUGC_SetItemVisibility( hSteamUGC
, handle
,
287 workshop_form
.submission
.visibility
.value
);
289 vg_info( "Submitting updates\n" );
290 vg_steam_async_call
*call
= vg_alloc_async_steam_api_call();
291 call
->userdata
= NULL
;
292 call
->p_handler
= on_workshop_update_result
;
293 call
->id
= SteamAPI_ISteamUGC_SubmitItemUpdate( hSteamUGC
, handle
, "" );
297 * Async thread for rearranging files into a reasonable workshop folder
299 struct workshop_package_thread_args
{
300 PublishedFileId_t file_id
;
302 VG_STATIC
void _workshop_package_thread( void *_args
)
304 struct workshop_package_thread_args
*args
= _args
;
305 PublishedFileId_t file_id
= args
->file_id
;
307 vg_info( "Packaging workshop content folder\n" );
309 vg_async_item
*call
= vg_async_alloc( sizeof(struct workshop_package_info
) );
310 struct workshop_package_info
*info
= call
->payload
;
311 info
->publishing_file_id
= file_id
;
312 info
->failure_reason
= "Unknown failure reason";
314 /* build content folder path */
315 snprintf( info
->abs_content_folder
, 1024, "%smodels/boards/workshop/%lu",
316 vg
.base_path
, file_id
);
318 /* build content file path */
319 snprintf( info
->abs_content_file
, 1024, "%s/%s", info
->abs_content_folder
,
322 /* build workshop preview file path */
323 snprintf( info
->abs_preview_image
, 1024, "%sworkshop_preview.jpg",
327 if( !vg_mkdir( info
->abs_content_folder
) ){
329 info
->failure_reason
= "Could not make directory.";
330 vg_async_dispatch( call
, workshop_form_async_package_complete
);
334 vg_linear_clear( vg_mem
.scratch
);
335 if( !vg_file_copy( workshop_form
.model_path
, info
->abs_content_file
,
338 info
->failure_reason
= "Copy file failed.";
339 vg_async_dispatch( call
, workshop_form_async_package_complete
);
344 vg_async_dispatch( call
, workshop_form_async_package_complete
);
348 * Begins the packaging thread using file_id. Thread definition above.
350 VG_STATIC
void workshop_package_submission( PublishedFileId_t file_id
)
352 vg_linear_clear( vg_mem
.scratch
);
353 struct workshop_package_thread_args
*args
=
354 vg_linear_alloc( vg_mem
.scratch
,
355 sizeof(struct workshop_package_thread_args
));
357 args
->file_id
= file_id
;
358 vg_loader_start( _workshop_package_thread
, args
);
362 * Steam API call result for when we've created a new item on their network, or
363 * not, if it has failed
365 VG_STATIC
void on_workshop_createitem( void *data
, void *user
)
367 CreateItemResult_t
*result
= data
;
369 if( result
->m_eResult
== k_EResultOK
){
370 vg_info( "Created workshop file with id: %lu\n",
371 result
->m_nPublishedFileId
);
373 if( result
->m_bUserNeedsToAcceptWorkshopLegalAgreement
){
374 vg_warn( "Workshop agreement currently not accepted\n" );
377 workshop_package_submission( result
->m_nPublishedFileId
);
380 const char *errstr
= workshop_EResult_user_string( result
->m_eResult
);
383 vg_error( "ISteamUGC_CreateItem() failed(%d): '%s' \n",
384 result
->m_eResult
, errstr
);
387 workshop_form
.page
= k_workshop_form_closing_bad
;
388 workshop_form
.failure_or_success_string
= errstr
;
394 * Starts the workshop upload process through Steam API
396 VG_STATIC
void workshop_form_async_submit_begin( void *payload
, u32 size
)
398 /* use existing file */
399 if( workshop_form
.submission
.file_id
){
400 if( workshop_form
.submission
.submit_file_and_image
){
401 workshop_package_submission( workshop_form
.submission
.file_id
);
404 struct workshop_package_info info
;
405 info
.abs_content_file
[0] = '\0';
406 info
.abs_content_folder
[0] = '\0';
407 info
.abs_preview_image
[0] = '\0';
408 info
.failure_reason
= NULL
;
409 info
.publishing_file_id
= workshop_form
.submission
.file_id
;
411 workshop_form_async_package_complete( &info
,
412 sizeof(struct workshop_package_info
) );
416 vg_steam_async_call
*call
= vg_alloc_async_steam_api_call();
417 call
->userdata
= NULL
;
418 call
->p_handler
= on_workshop_createitem
;
419 ISteamUGC
*hSteamUGC
= SteamAPI_SteamUGC();
420 call
->id
= SteamAPI_ISteamUGC_CreateItem( hSteamUGC
, SKATERIFT_APPID
,
421 k_EWorkshopFileTypeCommunity
);
426 * Downloads the framebuffer into scratch memory
428 VG_STATIC
void workshop_form_async_download_image( void *payload
, u32 size
)
431 render_fb_get_current_res( gpipeline
.fb_workshop_preview
, &w
, &h
);
432 vg_linear_clear( vg_mem
.scratch
);
433 workshop_form
.img_buffer
= vg_linear_alloc( vg_mem
.scratch
, w
*h
*3 );
435 vg_info( "read framebuffer: glReadPixels( %dx%d )\n", w
,h
);
437 glBindFramebuffer( GL_READ_FRAMEBUFFER
, gpipeline
.fb_workshop_preview
->fb
);
438 glReadBuffer( GL_COLOR_ATTACHMENT0
);
439 glReadPixels( 0,0, w
,h
, GL_RGB
, GL_UNSIGNED_BYTE
, workshop_form
.img_buffer
);
441 workshop_form
.img_w
= w
;
442 workshop_form
.img_h
= h
;
446 * Thread which kicks off the upload process
448 VG_STATIC
void _workshop_form_submit_thread( void *data
)
450 vg_async_call( workshop_form_async_download_image
, NULL
, 0 );
453 int w
= workshop_form
.img_w
,
454 h
= workshop_form
.img_h
;
456 vg_info( "writing: workshop_preview.jpg (%dx%d @90%%)\n", w
,h
);
457 stbi_flip_vertically_on_write(1);
458 stbi_write_jpg( "workshop_preview.jpg", w
,h
, 3,
459 workshop_form
.img_buffer
, 90 );
461 vg_async_call( workshop_form_async_submit_begin
, NULL
, 0 );
465 * Entry point for the publishing submission operation
467 VG_STATIC
void workshop_op_submit(void)
469 /* TODO: Show these errors to the user */
471 if( workshop_form
.submission
.submit_title
){
472 if( !workshop_form
.submission
.title
[0] ){
473 vg_error( "Cannot submit because a title is required\n" );
478 if( workshop_form
.submission
.submit_description
){
479 if( !workshop_form
.submission
.description
[0] ){
480 vg_error( "Cannot submit because a description is required\n" );
485 if( workshop_form
.submission
.submit_file_and_image
){
486 if( workshop_form
.file_intent
== k_workshop_form_file_intent_none
){
487 vg_error( "Cannot submit because the file is empty or unspecified\n" );
492 workshop_begin_op( k_workshop_form_op_publishing_update
);
494 player_board_unload( &workshop_form
.board_model
);
495 workshop_form
.file_intent
= k_workshop_form_file_intent_none
;
497 vg_loader_start( _workshop_form_submit_thread
, NULL
);
501 * op: k_workshop_form_op_loading_model
502 * -----------------------------------------------------------------------------
506 * Reciever for completion of the model file load
508 VG_STATIC
void workshop_form_loadmodel_async_complete( void *payload
, u32 size
)
510 v2_zero( workshop_form
.view_angles
);
511 v3_zero( workshop_form
.view_offset
);
512 workshop_form
.view_dist
= 1.0f
;
513 workshop_form
.view_changed
= 1;
514 workshop_form
.file_intent
= k_workshop_form_file_intent_new
;
516 vg_success( "workshop async load complete\n" );
521 * Thread which loads the model from the disk
523 VG_STATIC
void _workshop_form_load_thread( void *data
)
525 player_board_load( &workshop_form
.board_model
, workshop_form
.model_path
);
526 vg_async_call( workshop_form_loadmodel_async_complete
, NULL
, 0 );
530 * Entry point for load model operation
532 VG_STATIC
void workshop_op_load_model(void)
534 workshop_begin_op( k_workshop_form_op_loading_model
);
535 vg_loader_start( _workshop_form_load_thread
, NULL
);
539 * op: k_workshop_form_op_downloading_submission
540 * -----------------------------------------------------------------------------
544 * The image has been decoded and is ready to slap into the framebuffer
546 VG_STATIC
void workshop_form_async_imageload( void *data
, u32 len
)
549 struct framebuffer_attachment
*a
=
550 &gpipeline
.fb_workshop_preview
->attachments
[0];
552 glBindTexture( GL_TEXTURE_2D
, a
->id
);
553 glTexSubImage2D( GL_TEXTURE_2D
, 0,0,0,
554 WORKSHOP_PREVIEW_WIDTH
, WORKSHOP_PREVIEW_HEIGHT
,
555 a
->format
, a
->type
, data
);
556 stbi_image_free( data
);
557 vg_success( "Loaded workshop preview image\n" );
563 struct workshop_loadpreview_info
{
564 char abs_preview_image
[ 1024 ];
568 * Load the image located at ./workshop_preview.jpg into our framebuffer
570 VG_STATIC
void _workshop_load_preview_thread( void *data
)
572 struct workshop_loadpreview_info
*info
= data
;
574 stbi_set_flip_vertically_on_load(1);
576 u8
*rgb
= stbi_load( info
->abs_preview_image
, &x
, &y
, &nc
, 3 );
579 if( (x
== WORKSHOP_PREVIEW_WIDTH
) && (y
== WORKSHOP_PREVIEW_HEIGHT
) ){
580 vg_async_call( workshop_form_async_imageload
, rgb
, x
*y
*3 );
583 vg_error( "Resolution does not match framebuffer, so we can't"
585 stbi_image_free( rgb
);
586 vg_async_call( workshop_form_async_imageload
, NULL
, 0 );
590 vg_error( "Failed to load workshop_preview.jpg: '%s'\n",
591 stbi_failure_reason() );
592 vg_async_call( workshop_form_async_imageload
, NULL
, 0 );
597 * Reciever for the preview download result
599 VG_STATIC
void on_workshop_download_ugcpreview( void *data
, void *user
)
601 struct workshop_loadpreview_info
*info
= user
;
602 RemoteStorageDownloadUGCResult_t
*result
= data
;
604 if( result
->m_eResult
== k_EResultOK
){
605 ISteamRemoteStorage
*hSteamRemoteStorage
= SteamAPI_SteamRemoteStorage();
606 vg_loader_start( _workshop_load_preview_thread
, info
);
609 vg_error( "Error while donwloading UGC preview( %d )\n",
616 * Entry point to view operation
618 VG_STATIC
void workshop_op_download_and_view_submission( int result_index
)
620 ISteamUGC
*hSteamUGC
= SteamAPI_SteamUGC();
621 ISteamRemoteStorage
*hSteamRemoteStorage
= SteamAPI_SteamRemoteStorage();
622 SteamUGCDetails_t details
;
623 if( SteamAPI_ISteamUGC_GetQueryUGCResult( hSteamUGC
,
624 workshop_form
.ugc_query
.handle
,
628 workshop_begin_op( k_workshop_form_op_downloading_submission
);
629 workshop_reset_submission_data();
630 workshop_form
.submission
.submit_description
= 0;
631 workshop_form
.submission
.submit_file_and_image
= 0;
632 workshop_form
.submission
.submit_title
= 0;
634 vg_strncpy( details
.m_rgchDescription
,
635 workshop_form
.submission
.description
,
636 vg_list_size( workshop_form
.submission
.description
),
637 k_strncpy_always_add_null
);
639 vg_strncpy( details
.m_rgchTitle
,
640 workshop_form
.submission
.title
,
641 vg_list_size( workshop_form
.submission
.title
),
642 k_strncpy_always_add_null
);
644 snprintf( workshop_form
.model_path
,
645 vg_list_size( workshop_form
.model_path
),
646 "Steam Cloud (%lu)", details
.m_nPublishedFileId
);
648 workshop_form
.submission
.file_id
= details
.m_nPublishedFileId
;
649 workshop_form
.file_intent
= k_workshop_form_file_intent_keep_old
;
650 workshop_form
.page
= k_workshop_form_edit
;
651 workshop_form
.submission
.visibility
.value
= details
.m_eVisibility
;
653 for( i32 i
=0; i
<vg_list_size(workshop_form_visibility_opts
); i
++ ){
654 if( workshop_form_visibility_opts
[i
].value
== details
.m_eVisibility
){
655 workshop_form
.submission
.visibility
.index
= i
;
660 if( details
.m_hPreviewFile
== 0 ){
661 vg_error( "m_hPreviewFile is 0\n" );
665 /* Now need to begin downloading the image so we can display it */
666 vg_steam_async_call
*call
= vg_alloc_async_steam_api_call();
668 struct workshop_loadpreview_info
*info
=
669 vg_linear_alloc( vg_mem
.scratch
,
670 sizeof( struct workshop_loadpreview_info
) );
672 snprintf( info
->abs_preview_image
, 1024,
673 "%smodels/boards/workshop/" PRINTF_U64
".jpg",
674 vg
.base_path
, details
.m_hPreviewFile
);
676 call
->p_handler
= on_workshop_download_ugcpreview
;
677 call
->userdata
= info
;
678 call
->id
= SteamAPI_ISteamRemoteStorage_UGCDownloadToLocation(
680 details
.m_hPreviewFile
, info
->abs_preview_image
, 1 );
682 vg_info( "preview file id: " PRINTF_U64
"\n",
683 details
.m_hPreviewFile
);
687 vg_error( "GetQueryUGCResult: Index out of range\n" );
694 * -----------------------------------------------------------------------------
698 * View a page of results on the sidebar
700 VG_STATIC
void workshop_view_page( int req
)
702 if( workshop_form
.ugc_query
.result
!= k_EResultOK
){
703 vg_error( "Tried to change page without complete data\n" );
707 int page
= VG_MAX(VG_MIN(req
, workshop_form
.view_published_page_count
-1),0),
708 start
= page
* WORKSHOP_VIEW_PER_PAGE
,
709 end
= VG_MIN( (page
+1) * WORKSHOP_VIEW_PER_PAGE
,
710 workshop_form
.ugc_query
.returned_item_count
),
713 vg_info( "View page %d\n", page
);
715 workshop_form
.view_published_page_id
= page
;
716 workshop_form
.published_files_list_length
= count
;
717 ISteamUGC
*hSteamUGC
= SteamAPI_SteamUGC();
719 for( int i
=0; i
<count
; i
++ ){
720 struct published_file
*pfile
= &workshop_form
.published_files_list
[i
];
722 SteamUGCDetails_t details
;
723 if( SteamAPI_ISteamUGC_GetQueryUGCResult( hSteamUGC
,
724 workshop_form
.ugc_query
.handle
,
728 if( details
.m_eResult
!= k_EResultOK
){
729 snprintf( pfile
->title
, 80, "Error (%d)", details
.m_eResult
);
732 vg_strncpy( details
.m_rgchTitle
, pfile
->title
, 80,
733 k_strncpy_always_add_null
);
736 pfile
->result
= details
.m_eResult
;
737 pfile
->result_index
= start
+i
;
740 pfile
->result
= k_EResultValueOutOfRange
;
741 pfile
->result_index
= -1;
742 snprintf( pfile
->title
, 80, "Error (invalid index)" );
748 * Steam API result for when we recieve submitted UGC information about the user
750 VG_STATIC
void on_workshop_UGCQueryComplete( void *data
, void *userdata
)
752 SteamUGCQueryCompleted_t
*query
= data
;
753 workshop_form
.ugc_query
.result
= query
->m_eResult
;
755 if( query
->m_eResult
== k_EResultOK
){
756 if( query
->m_unTotalMatchingResults
> 50 ){
757 vg_warn( "You have %d items submitted, "
758 "we can only view the last 50\n" );
761 workshop_form
.ugc_query
.all_item_count
= query
->m_unTotalMatchingResults
;
762 workshop_form
.ugc_query
.returned_item_count
=
763 query
->m_unNumResultsReturned
;
765 workshop_form
.ugc_query
.handle
= query
->m_handle
;
766 workshop_form
.view_published_page_count
=
767 (query
->m_unNumResultsReturned
+WORKSHOP_VIEW_PER_PAGE
-1)/
768 WORKSHOP_VIEW_PER_PAGE
;
769 workshop_form
.view_published_page_id
= 0;
770 workshop_form
.published_files_list_length
= 0;
772 workshop_view_page( 0 );
775 vg_error( "Steam UGCQuery failed (%d)\n", query
->m_eResult
);
776 workshop_form
.view_published_page_count
= 0;
777 workshop_form
.view_published_page_id
= 0;
778 workshop_form
.published_files_list_length
= 0;
780 ISteamUGC
*hSteamUGC
= SteamAPI_SteamUGC();
781 SteamAPI_ISteamUGC_ReleaseQueryUGCRequest( hSteamUGC
, query
->m_handle
);
786 * Console command to open the workshop publisher
788 VG_STATIC
int workshop_submit_command( int argc
, const char *argv
[] )
791 vg_error( "Steam API is not ready or loaded\n" );
795 workshop_form
.page
= k_workshop_form_open
;
796 workshop_form
.view_published_page_count
= 0;
797 workshop_form
.view_published_page_id
= 0;
798 workshop_form
.published_files_list_length
= 0;
799 workshop_form
.ugc_query
.result
= k_EResultNone
;
801 vg_steam_async_call
*call
= vg_alloc_async_steam_api_call();
803 ISteamUser
*hSteamUser
= SteamAPI_SteamUser();
805 steamid
.m_unAll64Bits
= SteamAPI_ISteamUser_GetSteamID( hSteamUser
);
807 ISteamUGC
*hSteamUGC
= SteamAPI_SteamUGC();
808 call
->p_handler
= on_workshop_UGCQueryComplete
;
809 call
->userdata
= NULL
;
810 UGCQueryHandle_t handle
= SteamAPI_ISteamUGC_CreateQueryUserUGCRequest
813 steamid
.m_comp
.m_unAccountID
,
814 k_EUserUGCList_Published
,
815 k_EUGCMatchingUGCType_Items
,
816 k_EUserUGCListSortOrder_CreationOrderDesc
,
817 SKATERIFT_APPID
, SKATERIFT_APPID
,
819 call
->id
= SteamAPI_ISteamUGC_SendQueryUGCRequest( hSteamUGC
, handle
);
823 VG_STATIC
void workshop_init(void)
825 vg_console_reg_cmd( "workshop_submit", workshop_submit_command
, NULL
);
828 VG_STATIC
void workshop_find_preview_entity(void)
830 workshop_form
.view_world
= get_active_world();
832 if( mdl_arrcount( &workshop_form
.view_world
->ent_swspreview
) ){
833 workshop_form
.ptr_ent
=
834 mdl_arritm( &workshop_form
.view_world
->ent_swspreview
, 0 );
835 workshop_form
.page
= k_workshop_form_edit
;
838 vg_error( "There is no ent_swspreview in the level. "
839 "Cannot publish here\n" );
844 * Redraw the model file into the workshop framebuffer
846 VG_STATIC
void workshop_render_preview(void)
848 if( !workshop_form
.ptr_ent
){
852 render_fb_bind( gpipeline
.fb_workshop_preview
, 0 );
854 glClearColor( 0.0f
, 0.0f
, 0.3f
, 1.0f
);
855 glClear( GL_COLOR_BUFFER_BIT
|GL_DEPTH_BUFFER_BIT
);
856 glEnable( GL_DEPTH_TEST
);
857 glDisable( GL_BLEND
);
859 ent_swspreview
*swsprev
= workshop_form
.ptr_ent
;
860 world_instance
*world
= workshop_form
.view_world
;
862 ent_camera
*ref
= mdl_arritm( &world
->ent_camera
,
863 mdl_entity_id_id(swsprev
->id_camera
) );
864 ent_marker
*display
= mdl_arritm( &world
->ent_marker
,
865 mdl_entity_id_id(swsprev
->id_display
) ),
866 *display1
= mdl_arritm( &world
->ent_marker
,
867 mdl_entity_id_id(swsprev
->id_display1
) );
870 v3_add( display
->transform
.co
, display1
->transform
.co
, baseco
);
871 v3_muls( baseco
, 0.5f
, baseco
);
875 v3_sub( display
->transform
.co
, ref
->transform
.co
, basevector
);
876 float dist
= v3_length( basevector
);
879 player_vector_angles( baseangles
, basevector
, 1.0f
, 0.0f
);
881 v2_add( workshop_form
.view_angles
, baseangles
, cam
.angles
);
882 cam
.angles
[2] = 0.0f
;
884 float sX
= sinf( cam
.angles
[0] ),
885 cX
= cosf( cam
.angles
[0] ),
886 sY
= sinf( cam
.angles
[1] ),
887 cY
= cosf( cam
.angles
[1] );
889 v3f offset
= { -sX
* cY
, sY
, cX
* cY
};
891 v3_muladds( display
->transform
.co
, offset
,
892 dist
*workshop_form
.view_dist
, cam
.pos
);
894 cam
.pos
[0] += -sX
*workshop_form
.view_offset
[2];
895 cam
.pos
[2] += cX
*workshop_form
.view_offset
[2];
896 cam
.pos
[0] += cX
*workshop_form
.view_offset
[0];
897 cam
.pos
[2] += sX
*workshop_form
.view_offset
[0];
898 cam
.pos
[1] += workshop_form
.view_offset
[1];
904 camera_update_transform( &cam
);
905 camera_update_view( &cam
);
906 camera_update_projection( &cam
);
907 camera_finalize( &cam
);
910 mdl_transform_m4x3( &display
->transform
, mmdl
);
911 mdl_transform_m4x3( &display1
->transform
, mmdl1
);
913 /* force update this for nice shadows. its usually set in the world
914 * pre-render step, but that includes timer stuff
916 struct player_board
*board
= &workshop_form
.board_model
;
917 struct ub_world_lighting
*ubo
= &world
->ub_lighting
;
919 v3_copy((v3f
){0.0f
,0.1f
, board
->truck_positions
[0][2]}, vp0
);
920 v3_copy((v3f
){0.0f
,0.1f
, board
->truck_positions
[1][2]}, vp1
);
921 m4x3_mulv( mmdl1
, vp0
, ubo
->g_board_0
);
922 m4x3_mulv( mmdl1
, vp1
, ubo
->g_board_1
);
923 glBindBuffer( GL_UNIFORM_BUFFER
, world
->ubo_lighting
);
924 glBufferSubData( GL_UNIFORM_BUFFER
, 0,
925 sizeof(struct ub_world_lighting
), &world
->ub_lighting
);
927 render_world( world
, &cam
, 1 );
928 render_board( &cam
, world
, board
, mmdl
, k_board_shader_entity
);
929 render_board( &cam
, world
, board
, mmdl1
, k_board_shader_entity
);
931 glBindFramebuffer( GL_FRAMEBUFFER
, 0 );
932 glViewport( 0,0, vg
.window_x
, vg
.window_y
);
936 * ImGUI section for workshop form
937 * -----------------------------------------------------------------------------
940 VG_STATIC
void workshop_changed_model_path( char *buf
, u32 len
){
941 workshop_form
.submission
.submit_file_and_image
= 1;
944 VG_STATIC
void workshop_changed_title( char *buf
, u32 len
){
945 workshop_form
.submission
.submit_title
= 1;
948 VG_STATIC
void workshop_changed_description( char *buf
, u32 len
){
949 workshop_form
.submission
.submit_description
= 1;
952 VG_STATIC
void workshop_form_gui_edit_page( ui_rect content
)
955 ui_split( content
, k_ui_axis_h
, 300, 0, image_plane
, content
);
956 ui_fill( image_plane
, ui_colour( k_ui_bg
+0 ) );
959 ui_fit_item( image_plane
, (ui_px
[2]){ 3, 2 }, img_box
);
961 if( workshop_form
.file_intent
== k_workshop_form_file_intent_keep_old
){
962 ui_image( img_box
, gpipeline
.fb_workshop_preview
->attachments
[0].id
);
964 else if( workshop_form
.file_intent
== k_workshop_form_file_intent_new
){
965 ui_image( img_box
, gpipeline
.fb_workshop_preview
->attachments
[0].id
);
966 int hover
= ui_inside_rect( img_box
, vg_ui
.mouse
),
967 target
= ui_inside_rect( img_box
, vg_ui
.mouse_click
);
969 if( ui_click_down(UI_MOUSE_MIDDLE
) && target
){
970 v3_copy( workshop_form
.view_offset
,
971 workshop_form
.view_offset_begin
);
973 else if( ui_click_down(UI_MOUSE_LEFT
) && target
){
974 v2_copy( workshop_form
.view_angles
,
975 workshop_form
.view_angles_begin
);
978 if( ui_clicking(UI_MOUSE_MIDDLE
) && target
){
979 v2f delta
= { vg_ui
.mouse
[0]-vg_ui
.mouse_click
[0],
980 vg_ui
.mouse
[1]-vg_ui
.mouse_click
[1] };
982 float *begin
= workshop_form
.view_offset_begin
,
983 *offset
= workshop_form
.view_offset
;
984 offset
[0] = vg_clampf( begin
[0]-delta
[0]*0.002f
, -1.0f
, 1.0f
);
985 offset
[2] = vg_clampf( begin
[2]-delta
[1]*0.002f
, -1.0f
, 1.0f
);
986 workshop_form
.view_changed
= 1;
988 else if( ui_clicking(UI_MOUSE_LEFT
) && target
){
989 v2f delta
= { vg_ui
.mouse
[0]-vg_ui
.mouse_click
[0],
990 vg_ui
.mouse
[1]-vg_ui
.mouse_click
[1] };
993 v2_muladds( workshop_form
.view_angles_begin
, delta
, 0.002f
, angles
);
995 float limit
= VG_PIf
*0.2f
;
997 angles
[0] = vg_clampf( angles
[0], -limit
, limit
);
998 angles
[1] = vg_clampf( angles
[1], -limit
, limit
);
1000 v2_copy( angles
, workshop_form
.view_angles
);
1001 workshop_form
.view_changed
= 1;
1004 if( !ui_clicking(UI_MOUSE_LEFT
) && hover
){
1005 float zoom
= workshop_form
.view_dist
;
1006 zoom
+= vg
.mouse_wheel
[1] * -0.07f
;
1007 zoom
= vg_clampf( zoom
, 0.4f
, 2.0f
);
1009 if( zoom
!= workshop_form
.view_dist
){
1010 workshop_form
.view_changed
= 1;
1011 workshop_form
.view_dist
= zoom
;
1016 ui_text( img_box
, "No image", 1, k_ui_align_middle_center
,
1017 ui_colour( k_ui_orange
) );
1021 ui_rect null
, file_entry
, file_button
, file_label
;
1022 ui_split( content
, k_ui_axis_h
, 8, 0, null
, content
);
1023 ui_split( content
, k_ui_axis_h
, 28, 0, file_entry
, content
);
1024 ui_split( file_entry
, k_ui_axis_v
, -128, 0, file_entry
, file_button
);
1025 ui_split_label( file_entry
, "File:", 1, 8, file_label
, file_entry
);
1026 ui_text( file_label
, "File:", 1, k_ui_align_middle_left
, 0 );
1028 if( workshop_form
.file_intent
!= k_workshop_form_file_intent_none
){
1029 ui_text( file_entry
, workshop_form
.model_path
, 1, k_ui_align_middle_left
,
1030 ui_colour( k_ui_fg
+4 ) );
1032 if( ui_button_text( file_button
, "Remove", 1 ) ){
1033 player_board_unload( &workshop_form
.board_model
);
1034 workshop_form
.file_intent
= k_workshop_form_file_intent_none
;
1035 workshop_form
.model_path
[0] = '\0';
1039 struct ui_textbox_callbacks callbacks
= {
1040 .change
= workshop_changed_model_path
1043 ui_textbox( file_entry
, workshop_form
.model_path
,
1044 vg_list_size(workshop_form
.model_path
), 0, &callbacks
);
1046 if( ui_button_text( file_button
, "Load", 1 ) ){
1047 workshop_find_preview_entity();
1048 workshop_op_load_model();
1052 ui_rect title_entry
, label
;
1053 ui_split( content
, k_ui_axis_h
, 8, 0, null
, content
);
1054 ui_split( content
, k_ui_axis_h
, 28, 0, title_entry
, content
);
1056 const char *str_title
= "Title:", *str_desc
= "Description:";
1057 ui_split( title_entry
, k_ui_axis_v
,
1058 ui_text_line_width(str_title
)+8, 0, label
, title_entry
);
1060 ui_rect vis_dropdown
;
1061 ui_split_ratio( title_entry
, k_ui_axis_v
, 0.6f
, 16,
1062 title_entry
, vis_dropdown
);
1066 struct ui_textbox_callbacks callbacks
= {
1067 .change
= workshop_changed_title
1069 ui_text( label
, str_title
, 1, k_ui_align_middle_left
, 0 );
1070 ui_textbox( title_entry
, workshop_form
.submission
.title
,
1071 vg_list_size(workshop_form
.submission
.title
), 0, &callbacks
);
1074 /* visibility option */
1076 ui_enum( vis_dropdown
, "Visibility:", workshop_form_visibility_opts
,
1077 4, &workshop_form
.submission
.visibility
);
1080 /* description box */
1082 struct ui_textbox_callbacks callbacks
= {
1083 .change
= workshop_changed_description
1086 /* TODO: Tommora, new split_px_gap and split_px() */
1087 ui_split( content
, k_ui_axis_h
, 8, 0, null
, content
);
1088 ui_split( content
, k_ui_axis_h
, 28, 0, label
, content
);
1089 ui_split( content
, k_ui_axis_h
, 28*4, 0, desc_entry
, content
);
1090 ui_text( label
, str_desc
, 1, k_ui_align_middle_left
, 0 );
1091 ui_textbox( desc_entry
, workshop_form
.submission
.description
,
1092 vg_list_size(workshop_form
.submission
.description
),
1093 UI_TEXTBOX_MULTILINE
|UI_TEXTBOX_WRAP
, &callbacks
);
1096 /* submissionable */
1097 ui_rect submission_row
;
1098 ui_split( content
, k_ui_axis_h
, 8, 0, null
, content
);
1099 ui_split( content
, k_ui_axis_h
, content
[3]-32-8, 0, content
,
1102 ui_rect submission_center
;
1103 rect_copy( submission_row
, submission_center
);
1104 submission_center
[2] = 256;
1105 ui_rect_center( submission_row
, submission_center
);
1107 ui_rect btn_left
, btn_right
;
1108 ui_split_ratio( submission_center
, k_ui_axis_v
, 0.5f
, 8,
1109 btn_left
, btn_right
);
1111 if( ui_button_text( btn_left
, "Publish", 1 ) ){
1112 workshop_op_submit();
1114 if( ui_button_text( btn_right
, "Cancel", 1 ) ){
1115 workshop_form
.page
= k_workshop_form_open
;
1116 player_board_unload( &workshop_form
.board_model
);
1117 workshop_form
.file_intent
= k_workshop_form_file_intent_none
;
1121 const char *disclaimer_text
=
1122 "By submitting this item, you agree to the workshop terms of service";
1124 ui_rect disclaimer_row
, inner
, link
;
1125 ui_split( content
, k_ui_axis_h
, 8, 0, null
, content
);
1126 ui_split( content
, k_ui_axis_h
, content
[3]-32, 0, content
,
1129 ui_px btn_width
= 32;
1131 rect_copy( disclaimer_row
, inner
);
1132 inner
[2] = ui_text_line_width( disclaimer_text
) + btn_width
+8;
1134 ui_rect_center( disclaimer_row
, inner
);
1135 ui_split( inner
, k_ui_axis_v
, inner
[2]-btn_width
, 0, label
, btn_right
);
1136 ui_rect_pad( btn_right
, (ui_px
[2]){2,2} );
1138 if( ui_button_text( btn_right
, "\x91", 2 ) ){
1139 ISteamFriends
*hSteamFriends
= SteamAPI_SteamFriends();
1140 SteamAPI_ISteamFriends_ActivateGameOverlayToWebPage( hSteamFriends
,
1141 "https://steamcommunity.com/sharedfiles/workshoplegalagreement",
1142 k_EActivateGameOverlayToWebPageMode_Default
);
1145 ui_text( label
, disclaimer_text
, 1, k_ui_align_middle_left
,
1146 ui_colour( k_ui_fg
+4 ) );
1149 VG_STATIC
void workshop_form_gui_sidebar( ui_rect sidebar
)
1152 * sidebar existing entries panel
1154 ui_fill( sidebar
, ui_colour( k_ui_bg
+2 ) );
1157 ui_split( sidebar
, k_ui_axis_h
, 28, 0, title
, sidebar
);
1158 ui_text( title
, "Your submissions", 1, k_ui_align_middle_center
, 0 );
1160 ui_rect controls
, btn_create_new
;
1161 ui_split( sidebar
, k_ui_axis_h
, 32, 0, controls
, sidebar
);
1162 ui_split( sidebar
, k_ui_axis_h
, -32, 0, sidebar
, btn_create_new
);
1163 ui_fill( controls
, ui_colour( k_ui_bg
+1 ) );
1166 strcpy( buf
, "page " );
1169 /* TODO: for what it is, this code is getting a bit.. special */
1170 i
+= highscore_intl( buf
+i
, workshop_form
.view_published_page_id
+1, 4 );
1172 i
+= highscore_intl( buf
+i
, workshop_form
.view_published_page_count
, 4 );
1175 ui_rect_pad( controls
, (ui_px
[2]){0,4} );
1177 ui_split_ratio( controls
, k_ui_axis_v
, 0.25f
, 0, info
, controls
);
1178 ui_text( info
, buf
, 1, k_ui_align_middle_center
, 0 );
1180 ui_rect btn_left
, btn_right
;
1181 ui_split_ratio( controls
, k_ui_axis_v
, 0.5f
, 2, btn_left
, btn_right
);
1183 if( ui_button_text( btn_left
, "newer", 1 ) ){
1184 workshop_view_page( workshop_form
.view_published_page_id
-1 );
1187 if( ui_button_text( btn_right
, "older", 1 ) ){
1188 workshop_view_page( workshop_form
.view_published_page_count
+1 );
1191 if( ui_button_text( btn_create_new
, "Create New Item", 1 ) ){
1192 workshop_reset_submission_data();
1193 workshop_form
.submission
.submit_title
= 1;
1194 workshop_form
.submission
.submit_description
= 1;
1195 workshop_form
.submission
.submit_file_and_image
= 1;
1196 workshop_form
.page
= k_workshop_form_edit
;
1197 workshop_find_preview_entity();
1200 for( int i
=0; i
<workshop_form
.published_files_list_length
; i
++ ){
1202 ui_split( sidebar
, k_ui_axis_h
, 28, 0, item
, sidebar
);
1203 ui_rect_pad( item
, (ui_px
[2]){4,4} );
1205 struct published_file
*pfile
= &workshop_form
.published_files_list
[i
];
1206 if( ui_button_text( item
, pfile
->title
, 1 ) ){
1207 if( pfile
->result
== k_EResultOK
){
1208 vg_info( "Select index: %d\n", pfile
->result_index
);
1209 workshop_op_download_and_view_submission( pfile
->result_index
);
1212 vg_warn( "Cannot select that item, result not OK\n" );
1218 VG_STATIC
void workshop_form_gui(void)
1220 enum workshop_form_page stable_page
= workshop_form
.page
;
1221 if( stable_page
== k_workshop_form_hidden
) return;
1224 ui_rect screen
= { 0, 0, vg
.window_x
, vg
.window_y
};
1225 ui_rect window
= { 0, 0, 1000, 700 };
1226 ui_rect_center( screen
, window
);
1227 vg_ui
.wants_mouse
= 1;
1229 ui_fill( window
, ui_colour( k_ui_bg
+1 ) );
1230 ui_outline( window
, 1, ui_colour( k_ui_bg
+7 ) );
1232 ui_rect title
, panel
;
1233 ui_split( window
, k_ui_axis_h
, 28, 0, title
, panel
);
1234 ui_fill( title
, ui_colour( k_ui_bg
+7 ) );
1235 ui_text( title
, "Workshop tool", 1, k_ui_align_middle_center
,
1236 ui_colourcont(k_ui_bg
+7) );
1238 ui_rect quit_button
;
1239 ui_split( title
, k_ui_axis_v
, title
[2]-title
[3], 2, title
, quit_button
);
1241 if( workshop
.operation
== k_workshop_form_op_none
){
1242 if( ui_button_text( quit_button
, "X", 1 ) ){
1243 workshop_quit_form();
1249 * temporary operation blinders, we don't yet have a nice way to show the
1250 * user that we're doing something uninterruptable, so the code just
1251 * escapes here and we show them a basic string
1254 if( workshop
.operation
!= k_workshop_form_op_none
){
1255 const char *op_string
= "The programmer has not bothered to describe "
1256 "the current operation that is running.";
1258 switch(workshop
.operation
){
1259 case k_workshop_form_op_loading_model
:
1260 op_string
= "Operation in progress: Loading model file.";
1262 case k_workshop_form_op_publishing_update
:
1263 op_string
= "Operation in progress: publishing submission update "
1266 case k_workshop_form_op_downloading_submission
:
1267 op_string
= "Operation in progress: downloading existing submission"
1268 " from Steam services.";
1273 ui_text( panel
, op_string
, 1, k_ui_align_middle_center
, 0 );
1277 /* re draw board preview if need to */
1278 if( (stable_page
== k_workshop_form_edit
) &&
1279 workshop_form
.view_changed
&&
1280 workshop_form
.file_intent
== k_workshop_form_file_intent_new
)
1282 workshop_render_preview();
1283 workshop_form
.view_changed
= 0;
1286 struct workshop_form
*form
= &workshop_form
;
1288 ui_rect sidebar
, content
;
1289 ui_split_ratio( panel
, k_ui_axis_v
, 0.3f
, 1, sidebar
, content
);
1292 ui_rect_pad( content
, (ui_px
[2]){8,8} );
1294 if( stable_page
== k_workshop_form_edit
){
1295 workshop_form_gui_edit_page( content
);
1297 else if( stable_page
== k_workshop_form_open
){
1298 ui_text( content
, "Nothing selected.", 1, k_ui_align_middle_center
,
1299 ui_colour( k_ui_fg
+4 ) );
1301 else if( stable_page
>= k_workshop_form_cclosing
){
1302 ui_rect submission_row
;
1303 ui_split( content
, k_ui_axis_h
, content
[3]-32-8, 0, content
,
1308 if( stable_page
== k_workshop_form_closing_bad
)
1309 colour
= ui_colour( k_ui_red
+k_ui_brighter
);
1311 colour
= ui_colour( k_ui_green
+k_ui_brighter
);
1313 ui_text( content
, workshop_form
.failure_or_success_string
, 1,
1314 k_ui_align_middle_center
, colour
);
1316 ui_rect submission_center
;
1317 rect_copy( submission_row
, submission_center
);
1318 submission_center
[2] = 128;
1319 ui_rect_center( submission_row
, submission_center
);
1320 ui_rect_pad( submission_center
, (ui_px
[2]){8,8} );
1322 if( ui_button_text( submission_center
, "OK", 1 ) ){
1323 workshop_form
.page
= k_workshop_form_open
;
1327 workshop_form_gui_sidebar( sidebar
);
1331 * Some async api stuff
1332 * -----------------------------------------------------------------------------
1335 VG_STATIC
void async_workshop_get_filepath( void *data
, u32 len
)
1337 struct async_workshop_filepath_info
*info
= data
;
1342 ISteamUGC
*hSteamUGC
= SteamAPI_SteamUGC();
1343 if( !SteamAPI_ISteamUGC_GetItemInstallInfo( hSteamUGC
, info
->id
, &_size
,
1344 info
->buf
, info
->len
, &_ts
))
1346 info
->buf
[0] = '\0';
1350 VG_STATIC
void async_workshop_get_installed_files( void *data
, u32 len
)
1352 struct async_workshop_installed_files_info
*info
= data
;
1354 ISteamUGC
*hSteamUGC
= SteamAPI_SteamUGC();
1355 u32 count
= SteamAPI_ISteamUGC_GetSubscribedItems( hSteamUGC
, info
->buffer
,
1358 vg_info( "Found %u subscribed items\n", count
);
1361 for( u32 i
=0; i
<count
; i
++ ){
1362 u32 state
= SteamAPI_ISteamUGC_GetItemState( hSteamUGC
, info
->buffer
[i
] );
1363 if( state
& k_EItemStateInstalled
){
1364 info
->buffer
[j
++] = info
->buffer
[i
];
1371 #endif /* WORKSHOP_C */