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