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 */
21 ERemoteStoragePublishedFileVisibility visibility
;
23 int submit_title
, /* set if the respective controls are touched */
25 submit_file_and_image
;
29 enum workshop_form_page
{
30 k_workshop_form_hidden
,
31 k_workshop_form_open
, /* open but not looking at anything */
32 k_workshop_form_edit
, /* editing a submission */
33 k_workshop_form_cclosing
,
34 k_workshop_form_closing_good
, /* post upload screen */
35 k_workshop_form_closing_bad
,
39 enum workshop_form_operation
{
40 k_workshop_form_op_none
,
41 k_workshop_form_op_loading_model
,
42 k_workshop_form_op_downloading_submission
,
43 k_workshop_form_op_publishing_update
,
48 * -----------------------------
53 struct player_board board_model
;
55 /* what does the user want to do with the image preview? */
56 enum workshop_form_file_intent
{
57 k_workshop_form_file_intent_none
, /* loading probably */
58 k_workshop_form_file_intent_new
, /* board_model is valid */
59 k_workshop_form_file_intent_keep_old
/* just browsing */
63 world_instance
*view_world
;
64 ent_swspreview
*ptr_ent
;
74 * published UGC request
75 * ------------------------------
79 UGCQueryHandle_t handle
;
89 * ------------------------------------------
92 const char *failure_or_success_string
;
97 int view_published_page_count
,
98 view_published_page_id
;
100 struct published_file
{
105 published_files_list
[WORKSHOP_VIEW_PER_PAGE
];
106 int published_files_list_length
;
108 static workshop_form
;
111 * Start a new operation and crash if we are already running one.
113 VG_STATIC
void workshop_begin_op( enum workshop_form_operation op
)
115 if( workshop_form
.operation
!= k_workshop_form_op_none
){
116 vg_fatal_error( "Workshop form currently executing op(%d), tried to "
117 "start op(%d)\n", workshop_form
.operation
, op
);
120 workshop_form
.operation
= op
;
121 vg_info( "Starting op( %d )\n", op
);
125 * Finished operation, otheres can now run
127 VG_STATIC
void workshop_end_op(void)
129 vg_info( "Finishing op( %d )\n", workshop_form
.operation
);
130 workshop_form
.operation
= k_workshop_form_op_none
;
134 * Close the form and discard UGC query result
136 VG_STATIC
void workshop_quit_form(void)
138 workshop_begin_op( k_workshop_form_op_none
); /* safeguard */
139 player_board_unload( &workshop_form
.board_model
);
140 workshop_form
.file_intent
= k_workshop_form_file_intent_none
;
142 if( workshop_form
.ugc_query
.result
== k_EResultOK
){
143 workshop_form
.ugc_query
.result
= k_EResultNone
;
145 ISteamUGC
*hSteamUGC
= SteamAPI_SteamUGC();
146 SteamAPI_ISteamUGC_ReleaseQueryUGCRequest(
147 hSteamUGC
, workshop_form
.ugc_query
.handle
);
150 workshop_form
.page
= k_workshop_form_hidden
;
154 * Delete all information about the submission
156 VG_STATIC
void workshop_reset_submission_data(void)
158 workshop_form
.submission
.file_id
= 0; /* assuming id of 0 is none/invalid */
159 workshop_form
.submission
.description
[0] = '\0';
160 workshop_form
.submission
.title
[0] = '\0';
161 workshop_form
.model_path
[0] = '\0';
162 player_board_unload( &workshop_form
.board_model
);
163 workshop_form
.file_intent
= k_workshop_form_file_intent_none
;
168 * Mostly copies of what it sais on the Steam API documentation
170 VG_STATIC
const char *workshop_EResult_user_string( EResult result
)
173 case k_EResultInsufficientPrivilege
:
174 return "Your account is currently restricted from uploading content "
175 "due to a hub ban, account lock, or community ban. You need to "
176 "contact Steam Support to resolve the issue.";
177 case k_EResultBanned
:
178 return "You do not have permission to upload content to this hub "
179 "because you have an active VAC or Game ban.";
180 case k_EResultTimeout
:
181 return "The operation took longer than expected, so it was discarded. "
183 case k_EResultNotLoggedOn
:
184 return "You are currently not logged into Steam.";
185 case k_EResultServiceUnavailable
:
186 return "The workshop server is having issues or is unavailable, "
188 case k_EResultInvalidParam
:
189 return "One of the submission fields contains something not being "
190 "accepted by that field.";
191 case k_EResultAccessDenied
:
192 return "There was a problem trying to save the title and description. "
193 "Access was denied.";
194 case k_EResultLimitExceeded
:
195 return "You have exceeded your Steam Cloud quota. If you wish to "
196 "upload this file, you must remove some published items.";
197 case k_EResultFileNotFound
:
198 return "The uploaded file could not be found.";
199 case k_EResultDuplicateRequest
:
200 return "The file was already successfully uploaded.";
201 case k_EResultDuplicateName
:
202 return "You already have a Steam Workshop item with that name.";
203 case k_EResultServiceReadOnly
:
204 return "Due to a recent password or email change, you are not allowed "
205 "to upload new content. Usually this restriction will expire in"
206 " 5 days, but can last up to 30 days if the account has been "
207 "inactive recently.";
209 return "Operation failed for an error which has not been accounted for "
210 "by the programmer. Try again, sorry :)";
215 * op: k_workshop_form_op_publishing_update
216 * ----------------------------------------------------------------------------
220 * The endpoint of this operation
222 VG_STATIC
void on_workshop_update_result( void *data
, void *user
)
224 vg_info( "Recieved workshop update result\n" );
225 SubmitItemUpdateResult_t
*result
= data
;
227 /* this seems to be set here, but my account definitely has accepted it */
228 if( result
->m_bUserNeedsToAcceptWorkshopLegalAgreement
){
229 vg_warn( "Workshop agreement currently not accepted\n" );
232 if( result
->m_eResult
== k_EResultOK
){
233 workshop_form
.page
= k_workshop_form_closing_good
;
234 workshop_form
.failure_or_success_string
= "Uploaded workshop file!";
235 vg_success( "file uploaded\n" );
238 workshop_form
.page
= k_workshop_form_closing_bad
;
239 workshop_form
.failure_or_success_string
=
240 workshop_EResult_user_string( result
->m_eResult
);
242 vg_error( "Error with the submitted file (%d)\n", result
->m_eResult
);
248 struct workshop_package_info
{
249 PublishedFileId_t publishing_file_id
;
252 char abs_preview_image
[ 1024 ];
253 char abs_content_folder
[ 1024 ];
254 char abs_content_file
[ 1024 ];
256 const char *failure_reason
;
260 * reciever on completion of packaging the files, it will then start the item
261 * update with Steam API
263 VG_STATIC
void workshop_form_async_package_complete( void *data
, u32 size
)
265 struct workshop_package_info
*info
= data
;
266 if( !info
->success
){
267 workshop_form
.page
= k_workshop_form_closing_bad
;
268 workshop_form
.failure_or_success_string
= info
->failure_reason
;
273 ISteamUGC
*hSteamUGC
= SteamAPI_SteamUGC();
274 UGCUpdateHandle_t handle
275 = SteamAPI_ISteamUGC_StartItemUpdate( hSteamUGC
, SKATERIFT_APPID
,
276 info
->publishing_file_id
);
278 /* TODO: Handle failure cases for these */
280 if( workshop_form
.submission
.submit_title
){
281 vg_info( "Setting title\n" );
282 SteamAPI_ISteamUGC_SetItemTitle( hSteamUGC
, handle
,
283 workshop_form
.submission
.title
);
286 if( workshop_form
.submission
.submit_description
){
287 vg_info( "Setting description\n" );
288 SteamAPI_ISteamUGC_SetItemDescription( hSteamUGC
, handle
,
289 workshop_form
.submission
.description
);
292 if( workshop_form
.submission
.submit_file_and_image
){
293 vg_info( "Setting preview image\n" );
294 SteamAPI_ISteamUGC_SetItemPreview( hSteamUGC
,
295 handle
, info
->abs_preview_image
);
297 vg_info( "Setting item content\n" );
298 SteamAPI_ISteamUGC_SetItemContent( hSteamUGC
, handle
,
299 info
->abs_content_folder
);
302 vg_info( "Setting visibility\n" );
303 SteamAPI_ISteamUGC_SetItemVisibility( hSteamUGC
, handle
,
304 workshop_form
.submission
.visibility
);
306 vg_info( "Submitting updates\n" );
307 vg_steam_async_call
*call
= vg_alloc_async_steam_api_call();
308 call
->userdata
= NULL
;
309 call
->p_handler
= on_workshop_update_result
;
310 call
->id
= SteamAPI_ISteamUGC_SubmitItemUpdate( hSteamUGC
, handle
, "" );
314 * Async thread for rearranging files into a reasonable workshop folder
316 struct workshop_package_thread_args
{
317 PublishedFileId_t file_id
;
319 VG_STATIC
void _workshop_package_thread( void *_args
)
321 struct workshop_package_thread_args
*args
= _args
;
322 PublishedFileId_t file_id
= args
->file_id
;
324 vg_info( "Packaging workshop content folder\n" );
326 vg_async_item
*call
= vg_async_alloc( sizeof(struct workshop_package_info
) );
327 struct workshop_package_info
*info
= call
->payload
;
328 info
->publishing_file_id
= file_id
;
329 info
->failure_reason
= "Unknown failure reason";
331 /* build content folder path */
332 snprintf( info
->abs_content_folder
, 1024, "%smodels/boards/workshop/%lu",
333 vg
.base_path
, file_id
);
335 /* build content file path */
336 snprintf( info
->abs_content_file
, 1024, "%s/%s", info
->abs_content_folder
,
339 /* build workshop preview file path */
340 snprintf( info
->abs_preview_image
, 1024, "%sworkshop_preview.jpg",
344 if( !vg_mkdir( info
->abs_content_folder
) ){
346 info
->failure_reason
= "Could not make directory.";
347 vg_async_dispatch( call
, workshop_form_async_package_complete
);
351 vg_linear_clear( vg_mem
.scratch
);
352 if( !vg_file_copy( workshop_form
.model_path
, info
->abs_content_file
,
355 info
->failure_reason
= "Copy file failed.";
356 vg_async_dispatch( call
, workshop_form_async_package_complete
);
361 vg_async_dispatch( call
, workshop_form_async_package_complete
);
365 * Begins the packaging thread using file_id. Thread definition above.
367 VG_STATIC
void workshop_package_submission( PublishedFileId_t file_id
)
369 vg_linear_clear( vg_mem
.scratch
);
370 struct workshop_package_thread_args
*args
=
371 vg_linear_alloc( vg_mem
.scratch
,
372 sizeof(struct workshop_package_thread_args
));
374 args
->file_id
= file_id
;
375 vg_loader_start( _workshop_package_thread
, args
);
379 * Steam API call result for when we've created a new item on their network, or
380 * not, if it has failed
382 VG_STATIC
void on_workshop_createitem( void *data
, void *user
)
384 CreateItemResult_t
*result
= data
;
386 if( result
->m_eResult
== k_EResultOK
){
387 vg_info( "Created workshop file with id: %lu\n",
388 result
->m_nPublishedFileId
);
390 if( result
->m_bUserNeedsToAcceptWorkshopLegalAgreement
){
391 vg_warn( "Workshop agreement currently not accepted\n" );
394 workshop_package_submission( result
->m_nPublishedFileId
);
397 const char *errstr
= workshop_EResult_user_string( result
->m_eResult
);
400 vg_error( "ISteamUGC_CreateItem() failed(%d): '%s' \n",
401 result
->m_eResult
, errstr
);
404 workshop_form
.page
= k_workshop_form_closing_bad
;
405 workshop_form
.failure_or_success_string
= errstr
;
411 * Starts the workshop upload process through Steam API
413 VG_STATIC
void workshop_form_async_submit_begin( void *payload
, u32 size
)
415 /* use existing file */
416 if( workshop_form
.submission
.file_id
){
417 workshop_package_submission( workshop_form
.submission
.file_id
);
420 vg_steam_async_call
*call
= vg_alloc_async_steam_api_call();
421 call
->userdata
= NULL
;
422 call
->p_handler
= on_workshop_createitem
;
423 ISteamUGC
*hSteamUGC
= SteamAPI_SteamUGC();
424 call
->id
= SteamAPI_ISteamUGC_CreateItem( hSteamUGC
, SKATERIFT_APPID
,
425 k_EWorkshopFileTypeCommunity
);
430 * Downloads the framebuffer into scratch memory
432 VG_STATIC
void workshop_form_async_download_image( void *payload
, u32 size
)
435 render_fb_get_current_res( gpipeline
.fb_workshop_preview
, &w
, &h
);
436 vg_linear_clear( vg_mem
.scratch
);
437 workshop_form
.img_buffer
= vg_linear_alloc( vg_mem
.scratch
, w
*h
*3 );
439 vg_info( "read framebuffer: glReadPixels( %dx%d )\n", w
,h
);
441 glBindFramebuffer( GL_READ_FRAMEBUFFER
, gpipeline
.fb_workshop_preview
->fb
);
442 glReadBuffer( GL_COLOR_ATTACHMENT0
);
443 glReadPixels( 0,0, w
,h
, GL_RGB
, GL_UNSIGNED_BYTE
, workshop_form
.img_buffer
);
445 workshop_form
.img_w
= w
;
446 workshop_form
.img_h
= h
;
450 * Thread which kicks off the upload process
452 VG_STATIC
void _workshop_form_submit_thread( void *data
)
454 vg_async_call( workshop_form_async_download_image
, NULL
, 0 );
457 int w
= workshop_form
.img_w
,
458 h
= workshop_form
.img_h
;
460 vg_info( "writing: workshop_preview.jpg (%dx%d @90%%)\n", w
,h
);
461 stbi_flip_vertically_on_write(1);
462 stbi_write_jpg( "workshop_preview.jpg", w
,h
, 3,
463 workshop_form
.img_buffer
, 90 );
465 vg_async_call( workshop_form_async_submit_begin
, NULL
, 0 );
469 * Entry point for the publishing submission operation
471 VG_STATIC
void workshop_op_submit(void)
473 /* TODO: Show these errors to the user */
475 if( workshop_form
.submission
.submit_title
){
476 if( !workshop_form
.submission
.title
[0] ){
477 vg_error( "Cannot submit because a title is required\n" );
482 if( workshop_form
.submission
.submit_description
){
483 if( !workshop_form
.submission
.description
[0] ){
484 vg_error( "Cannot submit because a description is required\n" );
489 if( workshop_form
.submission
.submit_file_and_image
){
490 if( workshop_form
.file_intent
== k_workshop_form_file_intent_none
){
491 vg_error( "Cannot submit because the file is empty or unspecified\n" );
496 workshop_begin_op( k_workshop_form_op_publishing_update
);
498 player_board_unload( &workshop_form
.board_model
);
499 workshop_form
.file_intent
= k_workshop_form_file_intent_none
;
501 vg_loader_start( _workshop_form_submit_thread
, NULL
);
505 * op: k_workshop_form_op_loading_model
506 * -----------------------------------------------------------------------------
510 * Reciever for completion of the model file load
512 VG_STATIC
void workshop_form_loadmodel_async_complete( void *payload
, u32 size
)
514 v2_zero( workshop_form
.view_angles
);
515 v3_zero( workshop_form
.view_offset
);
516 workshop_form
.view_dist
= 1.0f
;
517 workshop_form
.view_changed
= 1;
518 workshop_form
.file_intent
= k_workshop_form_file_intent_new
;
520 vg_success( "workshop async load complete\n" );
525 * Thread which loads the model from the disk
527 VG_STATIC
void _workshop_form_load_thread( void *data
)
529 player_board_load( &workshop_form
.board_model
, workshop_form
.model_path
);
530 vg_async_call( workshop_form_loadmodel_async_complete
, NULL
, 0 );
534 * Entry point for load model operation
536 VG_STATIC
void workshop_op_load_model(void)
538 workshop_begin_op( k_workshop_form_op_loading_model
);
539 vg_loader_start( _workshop_form_load_thread
, NULL
);
543 * op: k_workshop_form_op_downloading_submission
544 * -----------------------------------------------------------------------------
548 * The image has been decoded and is ready to slap into the framebuffer
550 VG_STATIC
void workshop_form_async_imageload( void *data
, u32 len
)
553 struct framebuffer_attachment
*a
=
554 &gpipeline
.fb_workshop_preview
->attachments
[0];
556 glBindTexture( GL_TEXTURE_2D
, a
->id
);
557 glTexSubImage2D( GL_TEXTURE_2D
, 0,0,0,
558 WORKSHOP_PREVIEW_WIDTH
, WORKSHOP_PREVIEW_HEIGHT
,
559 a
->format
, a
->type
, data
);
560 stbi_image_free( data
);
561 vg_success( "Loaded workshop preview image\n" );
567 struct workshop_loadpreview_info
{
568 char abs_preview_image
[ 1024 ];
572 * Load the image located at ./workshop_preview.jpg into our framebuffer
574 VG_STATIC
void _workshop_load_preview_thread( void *data
)
576 struct workshop_loadpreview_info
*info
= data
;
578 stbi_set_flip_vertically_on_load(1);
580 u8
*rgb
= stbi_load( info
->abs_preview_image
, &x
, &y
, &nc
, 3 );
583 if( (x
== WORKSHOP_PREVIEW_WIDTH
) && (y
== WORKSHOP_PREVIEW_HEIGHT
) ){
584 vg_async_call( workshop_form_async_imageload
, rgb
, x
*y
*3 );
587 vg_error( "Resolution does not match framebuffer, so we can't"
589 stbi_image_free( rgb
);
590 vg_async_call( workshop_form_async_imageload
, NULL
, 0 );
594 vg_error( "Failed to load workshop_preview.jpg: '%s'\n",
595 stbi_failure_reason() );
596 vg_async_call( workshop_form_async_imageload
, NULL
, 0 );
601 * Reciever for the preview download result
603 VG_STATIC
void on_workshop_download_ugcpreview( void *data
, void *user
)
605 struct workshop_loadpreview_info
*info
= user
;
606 RemoteStorageDownloadUGCResult_t
*result
= data
;
608 if( result
->m_eResult
== k_EResultOK
){
609 ISteamRemoteStorage
*hSteamRemoteStorage
= SteamAPI_SteamRemoteStorage();
610 vg_loader_start( _workshop_load_preview_thread
, info
);
613 vg_error( "Error while donwloading UGC preview( %d )\n",
620 * Entry point to view operation
622 VG_STATIC
void workshop_op_download_and_view_submission( int result_index
)
624 ISteamUGC
*hSteamUGC
= SteamAPI_SteamUGC();
625 ISteamRemoteStorage
*hSteamRemoteStorage
= SteamAPI_SteamRemoteStorage();
626 SteamUGCDetails_t details
;
627 if( SteamAPI_ISteamUGC_GetQueryUGCResult( hSteamUGC
,
628 workshop_form
.ugc_query
.handle
,
632 workshop_begin_op( k_workshop_form_op_downloading_submission
);
633 workshop_reset_submission_data();
635 vg_strncpy( details
.m_rgchDescription
,
636 workshop_form
.submission
.description
,
637 vg_list_size( workshop_form
.submission
.description
),
638 k_strncpy_always_add_null
);
640 vg_strncpy( details
.m_rgchTitle
,
641 workshop_form
.submission
.title
,
642 vg_list_size( workshop_form
.submission
.title
),
643 k_strncpy_always_add_null
);
645 snprintf( workshop_form
.model_path
,
646 vg_list_size( workshop_form
.model_path
),
647 "Steam Cloud (%lu)", details
.m_nPublishedFileId
);
649 workshop_form
.submission
.file_id
= details
.m_nPublishedFileId
;
650 workshop_form
.file_intent
= k_workshop_form_file_intent_keep_old
;
651 workshop_form
.page
= k_workshop_form_edit
;
653 if( details
.m_hPreviewFile
== 0 ){
654 vg_error( "m_hPreviewFile is 0\n" );
658 /* Now need to begin downloading the image so we can display it */
659 vg_steam_async_call
*call
= vg_alloc_async_steam_api_call();
661 struct workshop_loadpreview_info
*info
=
662 vg_linear_alloc( vg_mem
.scratch
,
663 sizeof( struct workshop_loadpreview_info
) );
665 snprintf( info
->abs_preview_image
, 1024,
666 "%smodels/boards/workshop/%lu.jpg",
667 vg
.base_path
, details
.m_hPreviewFile
);
669 call
->p_handler
= on_workshop_download_ugcpreview
;
670 call
->userdata
= info
;
671 call
->id
= SteamAPI_ISteamRemoteStorage_UGCDownloadToLocation(
673 details
.m_hPreviewFile
, info
->abs_preview_image
, 1 );
675 vg_info( "preview file id: %lu\n", details
.m_hPreviewFile
);
679 vg_error( "GetQueryUGCResult: Index out of range\n" );
686 * -----------------------------------------------------------------------------
690 * View a page of results on the sidebar
692 VG_STATIC
void workshop_view_page( int req
)
694 if( workshop_form
.ugc_query
.result
!= k_EResultOK
){
695 vg_error( "Tried to change page without complete data\n" );
699 int page
= VG_MAX(VG_MIN(req
, workshop_form
.view_published_page_count
-1),0),
700 start
= page
* WORKSHOP_VIEW_PER_PAGE
,
701 end
= VG_MIN( (page
+1) * WORKSHOP_VIEW_PER_PAGE
,
702 workshop_form
.ugc_query
.returned_item_count
),
705 vg_info( "View page %d\n", page
);
707 workshop_form
.view_published_page_id
= page
;
708 workshop_form
.published_files_list_length
= count
;
709 ISteamUGC
*hSteamUGC
= SteamAPI_SteamUGC();
711 for( int i
=0; i
<count
; i
++ ){
712 struct published_file
*pfile
= &workshop_form
.published_files_list
[i
];
714 SteamUGCDetails_t details
;
715 if( SteamAPI_ISteamUGC_GetQueryUGCResult( hSteamUGC
,
716 workshop_form
.ugc_query
.handle
,
720 if( details
.m_eResult
!= k_EResultOK
){
721 snprintf( pfile
->title
, 80, "Error (%d)", details
.m_eResult
);
724 vg_strncpy( details
.m_rgchTitle
, pfile
->title
, 80,
725 k_strncpy_always_add_null
);
728 pfile
->result
= details
.m_eResult
;
729 pfile
->result_index
= start
+i
;
732 pfile
->result
= k_EResultValueOutOfRange
;
733 pfile
->result_index
= -1;
734 snprintf( pfile
->title
, 80, "Error (invalid index)" );
740 * Steam API result for when we recieve submitted UGC information about the user
742 VG_STATIC
void on_workshop_UGCQueryComplete( void *data
, void *userdata
)
744 SteamUGCQueryCompleted_t
*query
= data
;
745 workshop_form
.ugc_query
.result
= query
->m_eResult
;
747 if( query
->m_eResult
== k_EResultOK
){
748 if( query
->m_unTotalMatchingResults
> 50 ){
749 vg_warn( "You have %d items submitted, "
750 "we can only view the last 50\n" );
753 workshop_form
.ugc_query
.all_item_count
= query
->m_unTotalMatchingResults
;
754 workshop_form
.ugc_query
.returned_item_count
=
755 query
->m_unNumResultsReturned
;
757 workshop_form
.ugc_query
.handle
= query
->m_handle
;
758 workshop_form
.view_published_page_count
=
759 (query
->m_unNumResultsReturned
+WORKSHOP_VIEW_PER_PAGE
-1)/
760 WORKSHOP_VIEW_PER_PAGE
;
761 workshop_form
.view_published_page_id
= 0;
762 workshop_form
.published_files_list_length
= 0;
764 workshop_view_page( 0 );
767 vg_error( "Steam UGCQuery failed (%d)\n", query
->m_eResult
);
768 workshop_form
.view_published_page_count
= 0;
769 workshop_form
.view_published_page_id
= 0;
770 workshop_form
.published_files_list_length
= 0;
772 ISteamUGC
*hSteamUGC
= SteamAPI_SteamUGC();
773 SteamAPI_ISteamUGC_ReleaseQueryUGCRequest( hSteamUGC
, query
->m_handle
);
778 * Console command to open the workshop publisher
780 VG_STATIC
int workshop_submit_command( int argc
, const char *argv
[] )
783 vg_error( "Steam API is not ready or loaded\n" );
787 workshop_form
.page
= k_workshop_form_open
;
788 workshop_form
.view_published_page_count
= 0;
789 workshop_form
.view_published_page_id
= 0;
790 workshop_form
.published_files_list_length
= 0;
791 workshop_form
.ugc_query
.result
= k_EResultNone
;
793 vg_steam_async_call
*call
= vg_alloc_async_steam_api_call();
795 ISteamUser
*hSteamUser
= SteamAPI_SteamUser();
797 steamid
.m_unAll64Bits
= SteamAPI_ISteamUser_GetSteamID( hSteamUser
);
799 ISteamUGC
*hSteamUGC
= SteamAPI_SteamUGC();
800 call
->p_handler
= on_workshop_UGCQueryComplete
;
801 call
->userdata
= NULL
;
802 UGCQueryHandle_t handle
= SteamAPI_ISteamUGC_CreateQueryUserUGCRequest
805 steamid
.m_comp
.m_unAccountID
,
806 k_EUserUGCList_Published
,
807 k_EUGCMatchingUGCType_Items
,
808 k_EUserUGCListSortOrder_CreationOrderDesc
,
809 SKATERIFT_APPID
, SKATERIFT_APPID
,
811 call
->id
= SteamAPI_ISteamUGC_SendQueryUGCRequest( hSteamUGC
, handle
);
815 VG_STATIC
void workshop_init(void)
817 vg_console_reg_cmd( "workshop_submit", workshop_submit_command
, NULL
);
820 VG_STATIC
void workshop_find_preview_entity(void)
822 workshop_form
.view_world
= get_active_world();
824 if( mdl_arrcount( &workshop_form
.view_world
->ent_swspreview
) ){
825 workshop_form
.ptr_ent
=
826 mdl_arritm( &workshop_form
.view_world
->ent_swspreview
, 0 );
827 workshop_form
.page
= k_workshop_form_edit
;
830 vg_error( "There is no ent_swspreview in the level. "
831 "Cannot publish here\n" );
836 * Redraw the model file into the workshop framebuffer
838 VG_STATIC
void workshop_render_preview(void)
840 if( !workshop_form
.ptr_ent
){
844 render_fb_bind( gpipeline
.fb_workshop_preview
, 0 );
846 glClearColor( 0.0f
, 0.0f
, 0.3f
, 1.0f
);
847 glClear( GL_COLOR_BUFFER_BIT
|GL_DEPTH_BUFFER_BIT
);
848 glEnable( GL_DEPTH_TEST
);
849 glDisable( GL_BLEND
);
851 ent_swspreview
*swsprev
= workshop_form
.ptr_ent
;
852 world_instance
*world
= workshop_form
.view_world
;
854 ent_camera
*ref
= mdl_arritm( &world
->ent_camera
,
855 mdl_entity_id_id(swsprev
->id_camera
) );
856 ent_marker
*display
= mdl_arritm( &world
->ent_marker
,
857 mdl_entity_id_id(swsprev
->id_display
) ),
858 *display1
= mdl_arritm( &world
->ent_marker
,
859 mdl_entity_id_id(swsprev
->id_display1
) );
862 v3_add( display
->transform
.co
, display1
->transform
.co
, baseco
);
863 v3_muls( baseco
, 0.5f
, baseco
);
867 v3_sub( display
->transform
.co
, ref
->transform
.co
, basevector
);
868 float dist
= v3_length( basevector
);
871 player_vector_angles( baseangles
, basevector
, 1.0f
, 0.0f
);
873 v2_add( workshop_form
.view_angles
, baseangles
, cam
.angles
);
874 cam
.angles
[2] = 0.0f
;
876 float sX
= sinf( cam
.angles
[0] ),
877 cX
= cosf( cam
.angles
[0] ),
878 sY
= sinf( cam
.angles
[1] ),
879 cY
= cosf( cam
.angles
[1] );
881 v3f offset
= { -sX
* cY
, sY
, cX
* cY
};
883 v3_muladds( display
->transform
.co
, offset
,
884 dist
*workshop_form
.view_dist
, cam
.pos
);
886 cam
.pos
[0] += -sX
*workshop_form
.view_offset
[2];
887 cam
.pos
[2] += cX
*workshop_form
.view_offset
[2];
888 cam
.pos
[0] += cX
*workshop_form
.view_offset
[0];
889 cam
.pos
[2] += sX
*workshop_form
.view_offset
[0];
890 cam
.pos
[1] += workshop_form
.view_offset
[1];
896 camera_update_transform( &cam
);
897 camera_update_view( &cam
);
898 camera_update_projection( &cam
);
899 camera_finalize( &cam
);
902 mdl_transform_m4x3( &display
->transform
, mmdl
);
903 mdl_transform_m4x3( &display1
->transform
, mmdl1
);
905 /* force update this for nice shadows. its usually set in the world
906 * pre-render step, but that includes timer stuff
908 struct player_board
*board
= &workshop_form
.board_model
;
909 struct ub_world_lighting
*ubo
= &world
->ub_lighting
;
911 v3_copy((v3f
){0.0f
,0.1f
, board
->truck_positions
[0][2]}, vp0
);
912 v3_copy((v3f
){0.0f
,0.1f
, board
->truck_positions
[1][2]}, vp1
);
913 m4x3_mulv( mmdl1
, vp0
, ubo
->g_board_0
);
914 m4x3_mulv( mmdl1
, vp1
, ubo
->g_board_1
);
915 glBindBuffer( GL_UNIFORM_BUFFER
, world
->ubo_lighting
);
916 glBufferSubData( GL_UNIFORM_BUFFER
, 0,
917 sizeof(struct ub_world_lighting
), &world
->ub_lighting
);
919 render_world( world
, &cam
, 1 );
920 render_board( &cam
, world
, board
, mmdl
, k_board_shader_entity
);
921 render_board( &cam
, world
, board
, mmdl1
, k_board_shader_entity
);
923 glBindFramebuffer( GL_FRAMEBUFFER
, 0 );
924 glViewport( 0,0, vg
.window_x
, vg
.window_y
);
928 * ImGUI section for workshop form
929 * -----------------------------------------------------------------------------
932 VG_STATIC
void workshop_changed_model_path( char *buf
, u32 len
){
933 workshop_form
.submission
.submit_file_and_image
= 1;
936 VG_STATIC
void workshop_changed_title( char *buf
, u32 len
){
937 workshop_form
.submission
.submit_title
= 1;
940 VG_STATIC
void workshop_changed_description( char *buf
, u32 len
){
941 workshop_form
.submission
.submit_description
= 1;
944 VG_STATIC
void workshop_form_gui_edit_page( ui_rect content
)
947 ui_split_px( content
, k_ui_axis_h
, 300, 0, image_plane
, content
);
948 ui_fill( image_plane
, ui_colour( k_ui_bg
+0 ) );
951 ui_fit_item( image_plane
, (ui_px
[2]){ 3, 2 }, img_box
);
953 if( workshop_form
.file_intent
== k_workshop_form_file_intent_keep_old
){
954 ui_image( img_box
, gpipeline
.fb_workshop_preview
->attachments
[0].id
);
956 else if( workshop_form
.file_intent
== k_workshop_form_file_intent_new
){
957 ui_image( img_box
, gpipeline
.fb_workshop_preview
->attachments
[0].id
);
958 int hover
= ui_inside_rect( img_box
, vg_ui
.mouse
),
959 target
= ui_inside_rect( img_box
, vg_ui
.mouse_click
);
961 if( ui_click_down(UI_MOUSE_MIDDLE
) && target
){
962 v3_copy( workshop_form
.view_offset
,
963 workshop_form
.view_offset_begin
);
965 else if( ui_click_down(UI_MOUSE_LEFT
) && target
){
966 v2_copy( workshop_form
.view_angles
,
967 workshop_form
.view_angles_begin
);
970 if( ui_clicking(UI_MOUSE_MIDDLE
) && target
){
971 v2f delta
= { vg_ui
.mouse
[0]-vg_ui
.mouse_click
[0],
972 vg_ui
.mouse
[1]-vg_ui
.mouse_click
[1] };
974 float *begin
= workshop_form
.view_offset_begin
,
975 *offset
= workshop_form
.view_offset
;
976 offset
[0] = vg_clampf( begin
[0]-delta
[0]*0.002f
, -1.0f
, 1.0f
);
977 offset
[2] = vg_clampf( begin
[2]-delta
[1]*0.002f
, -1.0f
, 1.0f
);
978 workshop_form
.view_changed
= 1;
980 else if( ui_clicking(UI_MOUSE_LEFT
) && target
){
981 v2f delta
= { vg_ui
.mouse
[0]-vg_ui
.mouse_click
[0],
982 vg_ui
.mouse
[1]-vg_ui
.mouse_click
[1] };
985 v2_muladds( workshop_form
.view_angles_begin
, delta
, 0.002f
, angles
);
987 float limit
= VG_PIf
*0.2f
;
989 angles
[0] = vg_clampf( angles
[0], -limit
, limit
);
990 angles
[1] = vg_clampf( angles
[1], -limit
, limit
);
992 v2_copy( angles
, workshop_form
.view_angles
);
993 workshop_form
.view_changed
= 1;
996 if( !ui_clicking(UI_MOUSE_LEFT
) && hover
){
997 float zoom
= workshop_form
.view_dist
;
998 zoom
+= vg
.mouse_wheel
[1] * -0.07f
;
999 zoom
= vg_clampf( zoom
, 0.4f
, 2.0f
);
1001 if( zoom
!= workshop_form
.view_dist
){
1002 workshop_form
.view_changed
= 1;
1003 workshop_form
.view_dist
= zoom
;
1008 ui_text( img_box
, "No image", 1, k_ui_align_middle_center
,
1009 ui_colour( k_ui_orange
) );
1013 ui_rect null
, file_entry
, file_button
;
1014 ui_split_px( content
, k_ui_axis_h
, 8, 0, null
, content
);
1015 ui_split_px( content
, k_ui_axis_h
, 28, 0, file_entry
, content
);
1016 ui_split_px( file_entry
, k_ui_axis_v
, file_entry
[2]-128, 0,
1017 file_entry
, file_button
);
1019 if( workshop_form
.file_intent
!= k_workshop_form_file_intent_none
){
1020 ui_text( file_entry
, workshop_form
.model_path
, 1, k_ui_align_middle_left
,
1021 ui_colour( k_ui_fg
+4 ) );
1023 if( ui_button_text( file_button
, "Remove", 1 ) ){
1024 player_board_unload( &workshop_form
.board_model
);
1025 workshop_form
.file_intent
= k_workshop_form_file_intent_none
;
1026 workshop_form
.model_path
[0] = '\0';
1030 struct ui_textbox_callbacks callbacks
= {
1031 .change
= workshop_changed_model_path
1034 ui_textbox( file_entry
, workshop_form
.model_path
,
1035 vg_list_size(workshop_form
.model_path
), 0, &callbacks
);
1037 if( ui_button_text( file_button
, "Load", 1 ) ){
1038 workshop_find_preview_entity();
1039 workshop_op_load_model();
1043 ui_rect title_entry
, label
;
1044 ui_split_px( content
, k_ui_axis_h
, 8, 0, null
, content
);
1045 ui_split_px( content
, k_ui_axis_h
, 28, 0, title_entry
, content
);
1047 const char *str_title
= "Title:", *str_desc
= "Description:";
1048 ui_split_px( title_entry
, k_ui_axis_v
,
1049 ui_text_line_width(str_title
)+8, 0, label
, title_entry
);
1053 struct ui_textbox_callbacks callbacks
= {
1054 .change
= workshop_changed_title
1056 ui_text( label
, str_title
, 1, k_ui_align_middle_left
, 0 );
1057 ui_textbox( title_entry
, workshop_form
.submission
.title
,
1058 vg_list_size(workshop_form
.submission
.title
), 0, &callbacks
);
1061 /* description box */
1063 struct ui_textbox_callbacks callbacks
= {
1064 .change
= workshop_changed_description
1067 ui_split_px( content
, k_ui_axis_h
, 8, 0, null
, content
);
1068 ui_split_px( content
, k_ui_axis_h
, 28, 0, label
, content
);
1069 ui_split_px( content
, k_ui_axis_h
, 28*4, 0, desc_entry
, content
);
1070 ui_text( label
, str_desc
, 1, k_ui_align_middle_left
, 0 );
1071 ui_textbox( desc_entry
, workshop_form
.submission
.description
,
1072 vg_list_size(workshop_form
.submission
.description
),
1073 UI_TEXTBOX_MULTILINE
|UI_TEXTBOX_WRAP
, &callbacks
);
1076 /* submissionable */
1077 ui_rect submission_row
;
1078 ui_split_px( content
, k_ui_axis_h
, 8, 0, null
, content
);
1079 ui_split_px( content
, k_ui_axis_h
, content
[3]-32-8, 0, content
,
1082 ui_rect submission_center
;
1083 rect_copy( submission_row
, submission_center
);
1084 submission_center
[2] = 256;
1085 ui_rect_center( submission_row
, submission_center
);
1087 ui_rect btn_left
, btn_right
;
1088 ui_split_ratio( submission_center
, k_ui_axis_v
, 0.5f
, 8,
1089 btn_left
, btn_right
);
1091 if( ui_button_text( btn_left
, "Publish", 1 ) ){
1092 workshop_op_submit();
1094 if( ui_button_text( btn_right
, "Cancel", 1 ) ){
1095 vg_info( "left\n" );
1099 /* TODO!! OPEN THIS LINK */
1100 const char *disclaimer_text
=
1101 "By submitting this item, you agree to the workshop terms of service";
1103 ui_rect disclaimer_row
, inner
, link
;
1104 ui_split_px( content
, k_ui_axis_h
, 8, 0, null
, content
);
1105 ui_split_px( content
, k_ui_axis_h
, content
[3]-32, 0, content
,
1108 ui_px btn_width
= 32;
1110 rect_copy( disclaimer_row
, inner
);
1111 inner
[2] = ui_text_line_width( disclaimer_text
) + btn_width
+8;
1113 ui_rect_center( disclaimer_row
, inner
);
1114 ui_split_px( inner
, k_ui_axis_v
, inner
[2]-btn_width
, 0, label
, btn_right
);
1115 ui_rect_pad( btn_right
, 2 );
1117 if( ui_button_text( btn_right
, "\x91", 2 ) ){
1118 ISteamFriends
*hSteamFriends
= SteamAPI_SteamFriends();
1119 SteamAPI_ISteamFriends_ActivateGameOverlayToWebPage( hSteamFriends
,
1120 "https://steamcommunity.com/sharedfiles/workshoplegalagreement",
1121 k_EActivateGameOverlayToWebPageMode_Default
);
1124 ui_text( label
, disclaimer_text
, 1, k_ui_align_middle_left
,
1125 ui_colour( k_ui_fg
+4 ) );
1128 VG_STATIC
void workshop_form_gui_sidebar( ui_rect sidebar
)
1131 * sidebar existing entries panel
1133 ui_fill( sidebar
, ui_colour( k_ui_bg
+2 ) );
1136 ui_split_px( sidebar
, k_ui_axis_h
, 28, 0, title
, sidebar
);
1137 ui_text( title
, "Your submissions", 1, k_ui_align_middle_center
, 0 );
1139 ui_rect controls
, btn_create_new
;
1140 ui_split_px( sidebar
, k_ui_axis_h
, 32, 0, controls
, sidebar
);
1141 ui_split_px( sidebar
, k_ui_axis_h
, sidebar
[3]-32, 0,
1142 sidebar
, btn_create_new
);
1143 ui_fill( controls
, ui_colour( k_ui_bg
+1 ) );
1144 ui_outline( controls
, -1, ui_colour( k_ui_bg
+4 ) );
1147 strcpy( buf
, "page " );
1150 /* TODO: for what it is, this code is getting a bit.. special */
1151 if( workshop_form
.view_published_page_id
)
1152 i
+= highscore_intl( buf
+i
, workshop_form
.view_published_page_id
, 4 );
1153 else buf
[ i
++ ] = '0';
1156 i
+= highscore_intl( buf
+i
, workshop_form
.view_published_page_count
, 4 );
1160 ui_split_ratio( controls
, k_ui_axis_v
, 0.25f
, 0, info
, controls
);
1161 ui_text( info
, buf
, 1, k_ui_align_middle_center
, 0 );
1163 ui_rect btn_left
, btn_right
;
1164 ui_split_ratio( controls
, k_ui_axis_v
, 0.5f
, 2, btn_left
, btn_right
);
1166 if( ui_button_text( btn_left
, "newer", 1 ) ){
1167 workshop_view_page( workshop_form
.view_published_page_id
-1 );
1170 if( ui_button_text( btn_right
, "older", 1 ) ){
1171 workshop_view_page( workshop_form
.view_published_page_count
+1 );
1174 if( ui_button_text( btn_create_new
, "Create New Item", 1 ) ){
1175 workshop_reset_submission_data();
1176 workshop_form
.submission
.submit_title
= 1;
1177 workshop_form
.submission
.submit_description
= 1;
1178 workshop_form
.submission
.submit_file_and_image
= 1;
1179 workshop_form
.page
= k_workshop_form_edit
;
1180 workshop_find_preview_entity();
1183 for( int i
=0; i
<workshop_form
.published_files_list_length
; i
++ ){
1185 ui_split_px( sidebar
, k_ui_axis_h
, 28, 0, item
, sidebar
);
1186 ui_rect_pad( item
, 4 );
1188 struct published_file
*pfile
= &workshop_form
.published_files_list
[i
];
1189 if( ui_button_text( item
, pfile
->title
, 1 ) ){
1190 if( pfile
->result
== k_EResultOK
){
1191 vg_info( "Select index: %d\n", pfile
->result_index
);
1192 workshop_op_download_and_view_submission( pfile
->result_index
);
1195 vg_warn( "Cannot select that item, result not OK\n" );
1201 VG_STATIC
void workshop_form_gui(void)
1203 enum workshop_form_page stable_page
= workshop_form
.page
;
1204 if( stable_page
== k_workshop_form_hidden
) return;
1207 ui_rect screen
= { 0, 0, vg
.window_x
, vg
.window_y
};
1208 ui_rect window
= { 0, 0, 1000, 700 };
1209 ui_rect_center( screen
, window
);
1210 vg_ui
.wants_mouse
= 1;
1212 ui_fill( window
, ui_colour( k_ui_bg
+1 ) );
1213 ui_outline( window
, 1, ui_colour( k_ui_bg
+7 ) );
1215 ui_rect title
, panel
;
1216 ui_split_px( window
, k_ui_axis_h
, 28, 0, title
, panel
);
1217 ui_fill( title
, ui_colour( k_ui_bg
+7 ) );
1218 ui_text( title
, "Workshop tool", 1, k_ui_align_middle_center
,
1219 ui_colourcont(k_ui_bg
+7) );
1221 ui_rect quit_button
;
1222 ui_split_px( title
, k_ui_axis_v
, title
[2]-title
[3], 2, title
, quit_button
);
1224 if( workshop_form
.operation
== k_workshop_form_op_none
){
1225 if( ui_button_text( quit_button
, "X", 1 ) ){
1226 workshop_quit_form();
1232 * temporary operation blinders, we don't yet have a nice way to show the
1233 * user that we're doing something uninterruptable, so the code just
1234 * escapes here and we show them a basic string
1237 if( workshop_form
.operation
!= k_workshop_form_op_none
){
1238 const char *op_string
= "The programmer has not bothered to describe "
1239 "the current operation that is running.";
1241 switch(workshop_form
.operation
){
1242 case k_workshop_form_op_loading_model
:
1243 op_string
= "Operation in progress: Loading model file.";
1245 case k_workshop_form_op_publishing_update
:
1246 op_string
= "Operation in progress: publishing submission update "
1249 case k_workshop_form_op_downloading_submission
:
1250 op_string
= "Operation in progress: downloading existing submission"
1251 " from Steam services.";
1256 ui_text( panel
, op_string
, 1, k_ui_align_middle_center
, 0 );
1260 /* re draw board preview if need to */
1261 if( (stable_page
== k_workshop_form_edit
) &&
1262 workshop_form
.view_changed
&&
1263 workshop_form
.file_intent
== k_workshop_form_file_intent_new
)
1265 workshop_render_preview();
1266 workshop_form
.view_changed
= 0;
1269 struct workshop_form
*form
= &workshop_form
;
1271 ui_rect sidebar
, content
;
1272 ui_split_ratio( panel
, k_ui_axis_v
, 0.3f
, 1, sidebar
, content
);
1275 ui_rect_pad( content
, 8 );
1277 if( stable_page
== k_workshop_form_edit
){
1278 workshop_form_gui_edit_page( content
);
1280 else if( stable_page
== k_workshop_form_open
){
1281 ui_text( content
, "Nothing selected.", 1, k_ui_align_middle_center
,
1282 ui_colour( k_ui_fg
+4 ) );
1284 else if( stable_page
>= k_workshop_form_cclosing
){
1285 ui_rect submission_row
;
1286 ui_split_px( content
, k_ui_axis_h
, content
[3]-32-8, 0, content
,
1291 if( stable_page
== k_workshop_form_closing_bad
)
1292 colour
= ui_colour( k_ui_red
+k_ui_brighter
);
1294 colour
= ui_colour( k_ui_green
+k_ui_brighter
);
1296 ui_text( content
, workshop_form
.failure_or_success_string
, 1,
1297 k_ui_align_middle_center
, colour
);
1299 ui_rect submission_center
;
1300 rect_copy( submission_row
, submission_center
);
1301 submission_center
[2] = 128;
1302 ui_rect_center( submission_row
, submission_center
);
1303 ui_rect_pad( submission_center
, 8 );
1305 if( ui_button_text( submission_center
, "OK", 1 ) ){
1306 workshop_form
.page
= k_workshop_form_open
;
1310 workshop_form_gui_sidebar( sidebar
);
1313 #endif /* WORKSHOP_C */