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