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