getting stuff working on windows again
[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 vg_dir dir;
432 if( !vg_dir_open( &dir, folder.buffer ) ){
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 while( vg_dir_next_entry(&dir) ){
439 if( vg_dir_entry_type(&dir) == k_vg_entry_type_file ){
440 const char *d_name = vg_dir_entry_name(&dir);
441 if( d_name[0] == '.' ) continue;
442
443 vg_str file = folder;
444 vg_strcat( &file, "/" );
445 vg_strcat( &file, 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", d_name );
453 break;
454 }
455 }
456 vg_dir_close(&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 vg_dir dir;
582 if( !vg_dir_open( &dir, folder.buffer ) ){
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 while( vg_dir_next_entry(&dir) ){
592 if( vg_dir_entry_type(&dir) == k_vg_entry_type_file ){
593 const char *d_name = vg_dir_entry_name(&dir);
594 if( d_name[0] == '.' ) continue;
595
596 vg_str file = folder;
597 vg_strcat( &file, "/" );
598 vg_strcat( &file, 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 vg_dir_close(&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 /*
660 * Load the image located at ./workshop_preview.jpg into our framebuffer
661 */
662 VG_STATIC void _workshop_load_preview_thread( void *data )
663 {
664 char path_buf[ 4096 ];
665 vg_str path;
666 vg_strnull( &path, path_buf, 4096 );
667 vg_strcat( &path, "boards/" );
668 vg_strcat( &path, workshop_form.addon_folder );
669 vg_strcat( &path, "/preview.jpg" );
670
671 if( vg_strgood( &path ) ){
672 stbi_set_flip_vertically_on_load(1);
673 int x, y, nc;
674 u8 *rgb = stbi_load( path.buffer, &x, &y, &nc, 3 );
675
676 if( rgb ){
677 if( (x == WORKSHOP_PREVIEW_WIDTH) && (y == WORKSHOP_PREVIEW_HEIGHT) ){
678 vg_async_call( workshop_form_async_imageload, rgb, x*y*3 );
679 }
680 else{
681 vg_error( "Resolution does not match framebuffer, so we can't"
682 " show it\n" );
683 stbi_image_free( rgb );
684 vg_async_call( workshop_form_async_imageload, NULL, 0 );
685 }
686 }
687 else{
688 vg_error( "Failed to load workshop_preview.jpg: '%s'\n",
689 stbi_failure_reason() );
690 vg_async_call( workshop_form_async_imageload, NULL, 0 );
691 }
692 }
693 else{
694 vg_async_call( workshop_form_async_imageload, NULL, 0 );
695 }
696 }
697
698 #if 0
699 /*
700 * Reciever for the preview download result
701 */
702 VG_STATIC void on_workshop_download_ugcpreview( void *data, void *user )
703 {
704 struct workshop_loadpreview_info *info = user;
705 RemoteStorageDownloadUGCResult_t *result = data;
706
707 if( result->m_eResult == k_EResultOK ){
708 ISteamRemoteStorage *hSteamRemoteStorage = SteamAPI_SteamRemoteStorage();
709 vg_loader_start( _workshop_load_preview_thread, info );
710 }
711 else{
712 vg_error( "Error while donwloading UGC preview( %d )\n",
713 result->m_eResult );
714 skaterift_end_op();
715 }
716 }
717 #endif
718
719 /*
720 * Entry point to view operation
721 */
722 VG_STATIC void workshop_op_download_and_view_submission( int result_index )
723 {
724 ISteamUGC *hSteamUGC = SteamAPI_SteamUGC();
725 ISteamRemoteStorage *hSteamRemoteStorage = SteamAPI_SteamRemoteStorage();
726 SteamUGCDetails_t details;
727 if( SteamAPI_ISteamUGC_GetQueryUGCResult( hSteamUGC,
728 workshop_form.ugc_query.handle,
729 result_index,
730 &details ) )
731 {
732 skaterift_begin_op( k_workshop_form_op_downloading_submission );
733 workshop_reset_submission_data();
734 workshop_form.submission.submit_description = 0;
735 workshop_form.submission.submit_file_and_image = 0;
736 workshop_form.submission.submit_title = 0;
737
738 u8 metadata_buf[512];
739 char metadata_str[1024+1];
740 int have_meta = SteamAPI_ISteamUGC_GetQueryUGCMetadata( hSteamUGC,
741 workshop_form.ugc_query.handle,
742 result_index, metadata_str,
743 1024+1 );
744
745 vg_strncpy( details.m_rgchDescription,
746 workshop_form.submission.description,
747 vg_list_size( workshop_form.submission.description ),
748 k_strncpy_always_add_null );
749
750 vg_strncpy( details.m_rgchTitle,
751 workshop_form.submission.title,
752 vg_list_size( workshop_form.submission.title ),
753 k_strncpy_always_add_null );
754
755 snprintf( workshop_form.addon_folder,
756 vg_list_size( workshop_form.addon_folder ),
757 "Steam Cloud ("PRINTF_U64")", details.m_nPublishedFileId );
758
759 workshop_form.submission.file_id = details.m_nPublishedFileId;
760 workshop_form.file_intent = k_workshop_form_file_intent_keep_old;
761 workshop_form.page = k_workshop_form_edit;
762 workshop_form.submission.visibility.value = details.m_eVisibility;
763 workshop_form.submission.type = k_workshop_file_type_none;
764 workshop_form.submission.submission_type_selection.index = 0;
765 workshop_form.submission.submission_type_selection.value =
766 k_workshop_file_type_none;
767
768 if( have_meta ){
769 u32 len = strlen(metadata_str);
770 vg_info( "Metadata: %s\n", metadata_str );
771 vg_str_bin( metadata_str, metadata_buf, len );
772 vg_msg msg;
773 vg_msg_init( &msg, metadata_buf, len/2 );
774
775 vg_msg_cmd cmd;
776 while( vg_msg_next( &msg, &cmd ) ){
777 if( (msg.depth == 1) && (cmd.code == k_vg_msg_code_frame) ){
778 if( VG_STRDJB2_EQ( "workshop", cmd.key, cmd.key_djb2 ) ){
779 u32 depth = msg.depth;
780 while( (msg.depth == depth) && vg_msg_next( &msg, &cmd ) ){
781 if( cmd.code & k_vg_msg_code_unsigned ){
782 if( VG_STRDJB2_EQ( "type", cmd.key, cmd.key_djb2 ) ){
783 workshop_form.submission.type = cmd.value._u32;
784 workshop_form.submission.submission_type_selection.value = cmd.value._u32;
785 }
786 }
787 else if( cmd.code == k_vg_msg_code_kvstring ){
788 if( VG_STRDJB2_EQ( "folder", cmd.key, cmd.key_djb2 ) ){
789 vg_strncpy( cmd.value._buf,
790 workshop_form.addon_folder,
791 sizeof(workshop_form.addon_folder),
792 k_strncpy_always_add_null );
793 }
794 }
795 }
796 }
797 }
798 }
799 }
800 else{
801 vg_error( "No metadata was returned with this item.\n" );
802 }
803
804 /* TODO.... */
805 for( i32 i=0; i<vg_list_size(workshop_form_visibility_opts); i++ ){
806 if( workshop_form_visibility_opts[i].value == details.m_eVisibility ){
807 workshop_form.submission.visibility.index = i;
808 break;
809 }
810 }
811 for( i32 i=0; i<vg_list_size(workshop_form_type_opts); i++ ){
812 if( workshop_form_type_opts[i].value ==
813 workshop_form.submission.submission_type_selection.value ){
814 workshop_form.submission.submission_type_selection.index = i;
815 break;
816 }
817 }
818
819 render_fb_bind( gpipeline.fb_workshop_preview, 0 );
820 glClearColor( 0.2f, 0.0f, 0.0f, 1.0f );
821 glClear( GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT );
822 glBindFramebuffer( GL_FRAMEBUFFER, 0 );
823 glViewport( 0,0, vg.window_x, vg.window_y );
824
825 vg_loader_start( _workshop_load_preview_thread, NULL );
826
827 #if 0
828 if( details.m_hPreviewFile == 0 ){
829 vg_error( "m_hPreviewFile is 0\n" );
830 skaterift_end_op();
831 }
832 else{
833 /* Now need to begin downloading the image so we can display it */
834 vg_steam_async_call *call = vg_alloc_async_steam_api_call();
835
836 struct workshop_loadpreview_info *info =
837 vg_linear_alloc( vg_mem.scratch,
838 sizeof( struct workshop_loadpreview_info ) );
839
840 snprintf( info->abs_preview_image, 1024,
841 "%smodels/boards/workshop/" PRINTF_U64 ".jpg",
842 vg.base_path, details.m_hPreviewFile );
843
844 call->p_handler = on_workshop_download_ugcpreview;
845 call->userdata = info;
846 call->id = SteamAPI_ISteamRemoteStorage_UGCDownloadToLocation(
847 hSteamRemoteStorage,
848 details.m_hPreviewFile, info->abs_preview_image, 1 );
849
850 vg_info( "preview file id: " PRINTF_U64 "\n",
851 details.m_hPreviewFile );
852 }
853 #endif
854 }
855 else{
856 vg_error( "GetQueryUGCResult: Index out of range\n" );
857 skaterift_end_op();
858 }
859 }
860
861 /*
862 * Regular stuff
863 * -----------------------------------------------------------------------------
864 */
865
866 /*
867 * View a page of results on the sidebar
868 */
869 VG_STATIC void workshop_view_page( int req )
870 {
871 if( workshop_form.ugc_query.result != k_EResultOK ){
872 vg_error( "Tried to change page without complete data\n" );
873 return;
874 }
875
876 int page = VG_MAX(VG_MIN(req, workshop_form.view_published_page_count-1),0),
877 start = page * WORKSHOP_VIEW_PER_PAGE,
878 end = VG_MIN( (page+1) * WORKSHOP_VIEW_PER_PAGE,
879 workshop_form.ugc_query.returned_item_count ),
880 count = end-start;
881
882 vg_info( "View page %d\n", page );
883
884 workshop_form.view_published_page_id = page;
885 workshop_form.published_files_list_length = count;
886 ISteamUGC *hSteamUGC = SteamAPI_SteamUGC();
887
888 for( int i=0; i<count; i ++ ){
889 struct published_file *pfile = &workshop_form.published_files_list[i];
890
891 SteamUGCDetails_t details;
892 if( SteamAPI_ISteamUGC_GetQueryUGCResult( hSteamUGC,
893 workshop_form.ugc_query.handle,
894 start+i,
895 &details ) )
896 {
897 if( details.m_eResult != k_EResultOK ){
898 snprintf( pfile->title, 80, "Error (%d)", details.m_eResult );
899 }
900 else{
901 vg_strncpy( details.m_rgchTitle, pfile->title, 80,
902 k_strncpy_always_add_null );
903 }
904
905 pfile->result = details.m_eResult;
906 pfile->result_index = start+i;
907 }
908 else{
909 pfile->result = k_EResultValueOutOfRange;
910 pfile->result_index = -1;
911 snprintf( pfile->title, 80, "Error (invalid index)" );
912 }
913 }
914 }
915
916 /*
917 * Steam API result for when we recieve submitted UGC information about the user
918 */
919 VG_STATIC void on_workshop_UGCQueryComplete( void *data, void *userdata )
920 {
921 SteamUGCQueryCompleted_t *query = data;
922 workshop_form.ugc_query.result = query->m_eResult;
923
924 if( query->m_eResult == k_EResultOK ){
925 if( query->m_unTotalMatchingResults > 50 ){
926 vg_warn( "You have %d items submitted, "
927 "we can only view the last 50\n" );
928 }
929
930 workshop_form.ugc_query.all_item_count = query->m_unTotalMatchingResults;
931 workshop_form.ugc_query.returned_item_count =
932 query->m_unNumResultsReturned;
933
934 workshop_form.ugc_query.handle = query->m_handle;
935 workshop_form.view_published_page_count =
936 (query->m_unNumResultsReturned+WORKSHOP_VIEW_PER_PAGE-1)/
937 WORKSHOP_VIEW_PER_PAGE;
938 workshop_form.view_published_page_id = 0;
939 workshop_form.published_files_list_length = 0;
940
941 workshop_view_page( 0 );
942 }
943 else{
944 vg_error( "Steam UGCQuery failed (%d)\n", query->m_eResult );
945 workshop_form.view_published_page_count = 0;
946 workshop_form.view_published_page_id = 0;
947 workshop_form.published_files_list_length = 0;
948
949 ISteamUGC *hSteamUGC = SteamAPI_SteamUGC();
950 SteamAPI_ISteamUGC_ReleaseQueryUGCRequest( hSteamUGC, query->m_handle );
951 }
952 }
953
954 /*
955 * Console command to open the workshop publisher
956 */
957 VG_STATIC int workshop_submit_command( int argc, const char *argv[] )
958 {
959 if( !steam_ready ){
960 vg_error( "Steam API is not ready or loaded\n" );
961 return 0;
962 }
963
964 workshop_form.page = k_workshop_form_open;
965 workshop_form.view_published_page_count = 0;
966 workshop_form.view_published_page_id = 0;
967 workshop_form.published_files_list_length = 0;
968 workshop_form.ugc_query.result = k_EResultNone;
969
970 vg_steam_async_call *call = vg_alloc_async_steam_api_call();
971
972 ISteamUser *hSteamUser = SteamAPI_SteamUser();
973 CSteamID steamid;
974 steamid.m_unAll64Bits = SteamAPI_ISteamUser_GetSteamID( hSteamUser );
975
976 ISteamUGC *hSteamUGC = SteamAPI_SteamUGC();
977 call->p_handler = on_workshop_UGCQueryComplete;
978 call->userdata = NULL;
979 UGCQueryHandle_t handle = SteamAPI_ISteamUGC_CreateQueryUserUGCRequest
980 (
981 hSteamUGC,
982 steamid.m_comp.m_unAccountID,
983 k_EUserUGCList_Published,
984 k_EUGCMatchingUGCType_Items,
985 k_EUserUGCListSortOrder_CreationOrderDesc,
986 SKATERIFT_APPID, SKATERIFT_APPID,
987 1 );
988 SteamAPI_ISteamUGC_SetReturnMetadata( hSteamUGC, handle, 1 );
989 call->id = SteamAPI_ISteamUGC_SendQueryUGCRequest( hSteamUGC, handle );
990 return 0;
991 }
992
993 VG_STATIC void workshop_init(void)
994 {
995 vg_console_reg_cmd( "workshop_submit", workshop_submit_command, NULL );
996 }
997
998 VG_STATIC void workshop_find_preview_entity(void)
999 {
1000 workshop_form.view_world = world_current_instance();
1001
1002 if( mdl_arrcount( &workshop_form.view_world->ent_swspreview ) ){
1003 workshop_form.ptr_ent =
1004 mdl_arritm( &workshop_form.view_world->ent_swspreview, 0 );
1005 workshop_form.page = k_workshop_form_edit;
1006 }
1007 else{
1008 vg_error( "There is no ent_swspreview in the level. "
1009 "Cannot publish here\n" );
1010 }
1011 }
1012
1013 /*
1014 * Redraw the model file into the workshop framebuffer
1015 */
1016 VG_STATIC void workshop_render_preview(void)
1017 {
1018 if( !workshop_form.ptr_ent ){
1019 return;
1020 }
1021
1022 render_fb_bind( gpipeline.fb_workshop_preview, 0 );
1023
1024 glClearColor( 0.0f, 0.0f, 0.3f, 1.0f );
1025 glClear( GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT );
1026 glEnable( GL_DEPTH_TEST );
1027 glDisable( GL_BLEND );
1028
1029 ent_swspreview *swsprev = workshop_form.ptr_ent;
1030 world_instance *world = workshop_form.view_world;
1031
1032 ent_camera *ref = mdl_arritm( &world->ent_camera,
1033 mdl_entity_id_id(swsprev->id_camera) );
1034 ent_marker *display = mdl_arritm( &world->ent_marker,
1035 mdl_entity_id_id(swsprev->id_display) ),
1036 *display1= mdl_arritm( &world->ent_marker,
1037 mdl_entity_id_id(swsprev->id_display1) );
1038
1039 v3f baseco;
1040 v3_add( display->transform.co, display1->transform.co, baseco );
1041 v3_muls( baseco, 0.5f, baseco );
1042
1043 camera cam;
1044 v3f basevector;
1045 v3_sub( display->transform.co, ref->transform.co, basevector );
1046 float dist = v3_length( basevector );
1047
1048 v3f baseangles;
1049 player_vector_angles( baseangles, basevector, 1.0f, 0.0f );
1050
1051 v2_add( workshop_form.view_angles, baseangles, cam.angles );
1052 cam.angles[2] = 0.0f;
1053
1054 float sX = sinf( cam.angles[0] ),
1055 cX = cosf( cam.angles[0] ),
1056 sY = sinf( cam.angles[1] ),
1057 cY = cosf( cam.angles[1] );
1058
1059 v3f offset = { -sX * cY, sY, cX * cY };
1060
1061 v3_muladds( display->transform.co, offset,
1062 dist*workshop_form.view_dist, cam.pos );
1063
1064 cam.pos[0] += -sX*workshop_form.view_offset[2];
1065 cam.pos[2] += cX*workshop_form.view_offset[2];
1066 cam.pos[0] += cX*workshop_form.view_offset[0];
1067 cam.pos[2] += sX*workshop_form.view_offset[0];
1068 cam.pos[1] += workshop_form.view_offset[1];
1069
1070 cam.nearz = 0.01f;
1071 cam.farz = 100.0f;
1072 cam.fov = ref->fov;
1073
1074 camera_update_transform( &cam );
1075 camera_update_view( &cam );
1076 camera_update_projection( &cam );
1077 camera_finalize( &cam );
1078
1079 m4x3f mmdl, mmdl1;
1080 mdl_transform_m4x3( &display->transform, mmdl );
1081 mdl_transform_m4x3( &display1->transform, mmdl1 );
1082
1083 /* force update this for nice shadows. its usually set in the world
1084 * pre-render step, but that includes timer stuff
1085 */
1086 struct player_board *board = &workshop_form.board_model;
1087 struct ub_world_lighting *ubo = &world->ub_lighting;
1088 v3f vp0, vp1;
1089 v3_copy((v3f){0.0f,0.1f, board->truck_positions[0][2]}, vp0 );
1090 v3_copy((v3f){0.0f,0.1f, board->truck_positions[1][2]}, vp1 );
1091 m4x3_mulv( mmdl1, vp0, ubo->g_board_0 );
1092 m4x3_mulv( mmdl1, vp1, ubo->g_board_1 );
1093 glBindBuffer( GL_UNIFORM_BUFFER, world->ubo_lighting );
1094 glBufferSubData( GL_UNIFORM_BUFFER, 0,
1095 sizeof(struct ub_world_lighting), &world->ub_lighting );
1096
1097 render_world( world, &cam, 1 );
1098 render_board( &cam, world, board, mmdl, k_board_shader_entity );
1099 render_board( &cam, world, board, mmdl1, k_board_shader_entity );
1100
1101 glBindFramebuffer( GL_FRAMEBUFFER, 0 );
1102 glViewport( 0,0, vg.window_x, vg.window_y );
1103 }
1104
1105 /*
1106 * ImGUI section for workshop form
1107 * -----------------------------------------------------------------------------
1108 */
1109
1110 VG_STATIC void workshop_changed_model_path( char *buf, u32 len ){
1111 workshop_form.submission.submit_file_and_image = 1;
1112 }
1113
1114 VG_STATIC void workshop_changed_title( char *buf, u32 len ){
1115 workshop_form.submission.submit_title = 1;
1116 }
1117
1118 VG_STATIC void workshop_changed_description( char *buf, u32 len ){
1119 workshop_form.submission.submit_description = 1;
1120 }
1121
1122 VG_STATIC void workshop_form_gui_edit_page( ui_rect content )
1123 {
1124 if( workshop_form.submission.type == k_workshop_file_type_none ){
1125 ui_rect box;
1126 rect_copy( content, box );
1127 box[3] = 128;
1128 box[2] = (box[2]*2)/3;
1129 ui_rect_center( content, box );
1130
1131 ui_rect row;
1132 ui_split( box, k_ui_axis_h, 28, 0, row, box );
1133 ui_text( row, "Select the type of item\n", 1, k_ui_align_middle_center,0);
1134 ui_split( box, k_ui_axis_h, 28, 0, row, box );
1135 ui_enum( row, "Type:", workshop_form_type_opts,
1136 3, &workshop_form.submission.submission_type_selection );
1137 ui_split( box, k_ui_axis_h, 8, 0, row, box );
1138 ui_split( box, k_ui_axis_h, 28, 0, row, box );
1139
1140 ui_rect button_l, button_r;
1141 rect_copy( row, button_l );
1142 button_l[2] = 128*2;
1143 ui_rect_center( row, button_l );
1144 ui_split_ratio( button_l, k_ui_axis_v, 0.5f, 2, button_l, button_r );
1145
1146 if( workshop_form.submission.submission_type_selection.value !=
1147 k_workshop_file_type_none ){
1148 if( ui_button_text( button_l, "OK", 1 ) ){
1149 workshop_form.submission.type =
1150 workshop_form.submission.submission_type_selection.value;
1151 }
1152 }
1153 else{
1154 ui_fill( button_l, ui_colour(k_ui_bg) );
1155 ui_text( button_l, "OK", 1, k_ui_align_middle_center,
1156 ui_colour(k_ui_bg+4) );
1157 }
1158
1159 if( ui_button_text( button_r, "Cancel", 1 ) ){
1160 workshop_form.page = k_workshop_form_open;
1161 workshop_form.file_intent = k_workshop_form_file_intent_none;
1162 }
1163 return;
1164 }
1165
1166 if( workshop_form.submission.type == k_workshop_file_type_world ){
1167 ui_rect box;
1168 rect_copy( content, box );
1169 box[3] = 128;
1170 box[2] = (box[2]*2)/3;
1171 ui_rect_center( content, box );
1172
1173 ui_rect row;
1174 ui_split( box, k_ui_axis_h, 28, 0, row, box );
1175 ui_text( row, "World submissions are currently not ready, sorry.",
1176 1, k_ui_align_middle_center,0);
1177 ui_split( box, k_ui_axis_h, 8, 0, row, box );
1178 ui_split( box, k_ui_axis_h, 28, 0, row, box );
1179
1180 ui_rect button;
1181 rect_copy( row, button );
1182 button[2] = 128;
1183 ui_rect_center( row, button );
1184 if( ui_button_text( button, "OK", 1 ) ){
1185 workshop_form.page = k_workshop_form_open;
1186 workshop_form.file_intent = k_workshop_form_file_intent_none;
1187 }
1188
1189 return;
1190 }
1191
1192 ui_rect image_plane;
1193 ui_split( content, k_ui_axis_h, 300, 0, image_plane, content );
1194 ui_fill( image_plane, ui_colour( k_ui_bg+0 ) );
1195
1196 ui_rect img_box;
1197 ui_fit_item( image_plane, (ui_px[2]){ 3, 2 }, img_box );
1198
1199 if( workshop_form.file_intent == k_workshop_form_file_intent_keep_old ){
1200 ui_image( img_box, gpipeline.fb_workshop_preview->attachments[0].id );
1201 }
1202 else if( workshop_form.file_intent == k_workshop_form_file_intent_new ){
1203 ui_image( img_box, gpipeline.fb_workshop_preview->attachments[0].id );
1204 int hover = ui_inside_rect( img_box, vg_ui.mouse ),
1205 target = ui_inside_rect( img_box, vg_ui.mouse_click );
1206
1207 if( ui_click_down(UI_MOUSE_MIDDLE) && target ){
1208 v3_copy( workshop_form.view_offset,
1209 workshop_form.view_offset_begin );
1210 }
1211 else if( ui_click_down(UI_MOUSE_LEFT) && target ){
1212 v2_copy( workshop_form.view_angles,
1213 workshop_form.view_angles_begin );
1214 }
1215
1216 if( ui_clicking(UI_MOUSE_MIDDLE) && target ){
1217 v2f delta = { vg_ui.mouse[0]-vg_ui.mouse_click[0],
1218 vg_ui.mouse[1]-vg_ui.mouse_click[1] };
1219
1220 float *begin = workshop_form.view_offset_begin,
1221 *offset = workshop_form.view_offset;
1222 offset[0] = vg_clampf( begin[0]-delta[0]*0.002f, -1.0f, 1.0f );
1223 offset[2] = vg_clampf( begin[2]-delta[1]*0.002f, -1.0f, 1.0f );
1224 workshop_form.view_changed = 1;
1225 }
1226 else if( ui_clicking(UI_MOUSE_LEFT) && target ){
1227 v2f delta = { vg_ui.mouse[0]-vg_ui.mouse_click[0],
1228 vg_ui.mouse[1]-vg_ui.mouse_click[1] };
1229
1230 v2f angles;
1231 v2_muladds( workshop_form.view_angles_begin, delta, 0.002f, angles);
1232
1233 float limit = VG_PIf*0.2f;
1234
1235 angles[0] = vg_clampf( angles[0], -limit, limit );
1236 angles[1] = vg_clampf( angles[1], -limit, limit );
1237
1238 v2_copy( angles, workshop_form.view_angles );
1239 workshop_form.view_changed = 1;
1240 }
1241
1242 if( !ui_clicking(UI_MOUSE_LEFT) && hover ){
1243 float zoom = workshop_form.view_dist;
1244 zoom += vg.mouse_wheel[1] * -0.07f;
1245 zoom = vg_clampf( zoom, 0.4f, 2.0f );
1246
1247 if( zoom != workshop_form.view_dist ){
1248 workshop_form.view_changed = 1;
1249 workshop_form.view_dist = zoom;
1250 }
1251 }
1252 }
1253 else{
1254 ui_text( img_box, "No image", 1, k_ui_align_middle_center,
1255 ui_colour( k_ui_orange ) );
1256 }
1257
1258 /* file path */
1259 ui_rect null, file_entry, file_button, file_label;
1260 ui_split( content, k_ui_axis_h, 8, 0, null, content );
1261 ui_split( content, k_ui_axis_h, 28, 0, file_entry, content );
1262 ui_split( file_entry, k_ui_axis_v, -128, 0, file_entry, file_button );
1263
1264 if( workshop_form.submission.type == k_workshop_file_type_board ){
1265 ui_label( file_entry, "Addon folder: skaterift/boards/",
1266 1, 8, file_entry );
1267 }
1268 else{
1269 ui_label( file_entry, "Addon folder: skaterift/maps/",
1270 1, 8, file_entry );
1271 }
1272
1273 if( workshop_form.file_intent != k_workshop_form_file_intent_none ){
1274 ui_text( file_entry, workshop_form.addon_folder, 1,
1275 k_ui_align_middle_left, ui_colour( k_ui_fg+4 ) );
1276
1277 if( ui_button_text( file_button, "Remove", 1 ) ){
1278 player_board_unload( &workshop_form.board_model );
1279 workshop_form.file_intent = k_workshop_form_file_intent_none;
1280 workshop_form.addon_folder[0] = '\0';
1281 }
1282 }
1283 else{
1284 struct ui_textbox_callbacks callbacks = {
1285 .change = workshop_changed_model_path
1286 };
1287
1288 ui_textbox( file_entry, workshop_form.addon_folder,
1289 vg_list_size(workshop_form.addon_folder), 0, &callbacks );
1290
1291 if( ui_button_text( file_button, "Load", 1 ) ){
1292 workshop_find_preview_entity();
1293 workshop_op_load_model();
1294 }
1295 }
1296
1297 ui_rect title_entry, label;
1298 ui_split( content, k_ui_axis_h, 8, 0, null, content );
1299 ui_split( content, k_ui_axis_h, 28, 0, title_entry, content );
1300
1301 const char *str_title = "Title:", *str_desc = "Description:";
1302 ui_split( title_entry, k_ui_axis_v,
1303 ui_text_line_width(str_title)+8, 0, label, title_entry );
1304
1305 ui_rect vis_dropdown;
1306 ui_split_ratio( title_entry, k_ui_axis_v, 0.6f, 16,
1307 title_entry, vis_dropdown );
1308
1309 /* title box */
1310 {
1311 struct ui_textbox_callbacks callbacks = {
1312 .change = workshop_changed_title
1313 };
1314 ui_text( label, str_title, 1, k_ui_align_middle_left, 0 );
1315 ui_textbox( title_entry, workshop_form.submission.title,
1316 vg_list_size(workshop_form.submission.title), 0, &callbacks );
1317 }
1318
1319 /* visibility option */
1320 {
1321 ui_enum( vis_dropdown, "Visibility:", workshop_form_visibility_opts,
1322 4, &workshop_form.submission.visibility );
1323 }
1324
1325 /* description box */
1326 {
1327 struct ui_textbox_callbacks callbacks = {
1328 .change = workshop_changed_description
1329 };
1330 ui_rect desc_entry;
1331 ui_split( content, k_ui_axis_h, 8, 0, null, content );
1332 ui_split( content, k_ui_axis_h, 28, 0, label, content );
1333 ui_split( content, k_ui_axis_h, 28*4, 0, desc_entry, content );
1334 ui_text( label, str_desc, 1, k_ui_align_middle_left, 0 );
1335 ui_textbox( desc_entry, workshop_form.submission.description,
1336 vg_list_size(workshop_form.submission.description),
1337 UI_TEXTBOX_MULTILINE|UI_TEXTBOX_WRAP, &callbacks );
1338 }
1339
1340 /* submissionable */
1341 ui_rect submission_row;
1342 ui_split( content, k_ui_axis_h, 8, 0, null, content );
1343 ui_split( content, k_ui_axis_h, content[3]-32-8, 0, content,
1344 submission_row );
1345
1346 ui_rect submission_center;
1347 rect_copy( submission_row, submission_center );
1348 submission_center[2] = 256;
1349 ui_rect_center( submission_row, submission_center );
1350
1351 ui_rect btn_left, btn_right;
1352 ui_split_ratio( submission_center, k_ui_axis_v, 0.5f, 8,
1353 btn_left, btn_right );
1354
1355 if( ui_button_text( btn_left, "Publish", 1 ) ){
1356 workshop_op_submit();
1357 }
1358 if( ui_button_text( btn_right, "Cancel", 1 ) ){
1359 workshop_form.page = k_workshop_form_open;
1360 player_board_unload( &workshop_form.board_model );
1361 workshop_form.file_intent = k_workshop_form_file_intent_none;
1362 }
1363
1364 /* disclaimer */
1365 const char *disclaimer_text =
1366 "By submitting this item, you agree to the workshop terms of service";
1367
1368 ui_rect disclaimer_row, inner, link;
1369 ui_split( content, k_ui_axis_h, 8, 0, null, content );
1370 ui_split( content, k_ui_axis_h, content[3]-32, 0, content,
1371 disclaimer_row );
1372
1373 ui_px btn_width = 32;
1374
1375 rect_copy( disclaimer_row, inner );
1376 inner[2] = ui_text_line_width( disclaimer_text ) + btn_width+8;
1377
1378 ui_rect_center( disclaimer_row, inner );
1379 ui_split( inner, k_ui_axis_v, inner[2]-btn_width, 0, label, btn_right);
1380 ui_rect_pad( btn_right, (ui_px[2]){2,2} );
1381
1382 if( ui_button_text( btn_right, "\x91", 2 ) ){
1383 ISteamFriends *hSteamFriends = SteamAPI_SteamFriends();
1384 SteamAPI_ISteamFriends_ActivateGameOverlayToWebPage( hSteamFriends,
1385 "https://steamcommunity.com/sharedfiles/workshoplegalagreement",
1386 k_EActivateGameOverlayToWebPageMode_Default );
1387 }
1388
1389 ui_text( label, disclaimer_text, 1, k_ui_align_middle_left,
1390 ui_colour( k_ui_fg+4 ) );
1391 }
1392
1393 VG_STATIC void workshop_form_gui_sidebar( ui_rect sidebar )
1394 {
1395 ui_fill( sidebar, ui_colour( k_ui_bg+2 ) );
1396
1397 ui_rect title;
1398 ui_split( sidebar, k_ui_axis_h, 28, 0, title, sidebar );
1399
1400 if( workshop_form.page == k_workshop_form_edit ){
1401 ui_text( title, "Editing", 1, k_ui_align_middle_center, 0 );
1402 ui_split( sidebar, k_ui_axis_h, 28, 0, title, sidebar );
1403
1404 if( workshop_form.submission.type != k_workshop_file_type_none ){
1405 char buf[512];
1406 vg_str str;
1407 vg_strnull( &str, buf, 512 );
1408
1409 if( workshop_form.submission.file_id )
1410 vg_strcat( &str, "Editing an existing " );
1411 else
1412 vg_strcat( &str, "Creating a new " );
1413
1414 if( workshop_form.submission.type == k_workshop_file_type_board )
1415 vg_strcat( &str, "skateboard." );
1416 else if( workshop_form.submission.type == k_workshop_file_type_world )
1417 vg_strcat( &str, "world." );
1418 else
1419 vg_strcat( &str, "???." );
1420
1421 ui_text( title, buf, 1, k_ui_align_middle_center,
1422 ui_colour(k_ui_fg+4) );
1423 }
1424 return;
1425 }
1426
1427 /*
1428 * sidebar existing entries panel
1429 */
1430 ui_text( title, "Your submissions", 1, k_ui_align_middle_center, 0 );
1431
1432 ui_rect controls, btn_create_new;
1433 ui_split( sidebar, k_ui_axis_h, 32, 0, controls, sidebar );
1434 ui_split( sidebar, k_ui_axis_h, -32, 0, sidebar, btn_create_new );
1435 ui_fill( controls, ui_colour( k_ui_bg+1 ) );
1436
1437 char buf[32];
1438 strcpy( buf, "page " );
1439 int i = 5;
1440 i += highscore_intl( buf+i, workshop_form.view_published_page_id+1, 4 );
1441 buf[ i ++ ] = '/';
1442 i += highscore_intl( buf+i, workshop_form.view_published_page_count, 4 );
1443 buf[ i ++ ] = '\0';
1444
1445 ui_rect_pad( controls, (ui_px[2]){0,4} );
1446 ui_rect info;
1447 ui_split_ratio( controls, k_ui_axis_v, 0.25f, 0, info, controls );
1448 ui_text( info, buf, 1, k_ui_align_middle_center, 0 );
1449
1450 ui_rect btn_left, btn_right;
1451 ui_split_ratio( controls, k_ui_axis_v, 0.5f, 2, btn_left, btn_right );
1452
1453 if( ui_button_text( btn_left, "newer", 1 ) ){
1454 workshop_view_page( workshop_form.view_published_page_id-1 );
1455 }
1456
1457 if( ui_button_text( btn_right, "older", 1 ) ){
1458 workshop_view_page( workshop_form.view_published_page_count+1 );
1459 }
1460
1461 if( ui_button_text( btn_create_new, "Create New Item", 1 ) ){
1462 workshop_reset_submission_data();
1463 workshop_form.submission.submit_title = 1;
1464 workshop_form.submission.submit_description = 1;
1465 workshop_form.submission.submit_file_and_image = 1;
1466 workshop_form.page = k_workshop_form_edit;
1467 workshop_find_preview_entity();
1468 }
1469
1470 for( int i=0; i<workshop_form.published_files_list_length; i++ ){
1471 ui_rect item;
1472 ui_split( sidebar, k_ui_axis_h, 28, 0, item, sidebar );
1473 ui_rect_pad( item, (ui_px[2]){4,4} );
1474
1475 struct published_file *pfile = &workshop_form.published_files_list[i];
1476 if( ui_button_text( item, pfile->title, 1 ) ){
1477 if( pfile->result == k_EResultOK ){
1478 vg_info( "Select index: %d\n", pfile->result_index );
1479 workshop_op_download_and_view_submission( pfile->result_index );
1480 }
1481 else{
1482 vg_warn( "Cannot select that item, result not OK\n" );
1483 }
1484 }
1485 }
1486 }
1487
1488 VG_STATIC void workshop_form_gui(void)
1489 {
1490 enum workshop_form_page stable_page = workshop_form.page;
1491 if( stable_page == k_workshop_form_hidden ) return;
1492
1493 ui_rect null;
1494 ui_rect screen = { 0, 0, vg.window_x, vg.window_y };
1495 ui_rect window = { 0, 0, 1000, 700 };
1496 ui_rect_center( screen, window );
1497 vg_ui.wants_mouse = 1;
1498
1499 ui_fill( window, ui_colour( k_ui_bg+1 ) );
1500 ui_outline( window, 1, ui_colour( k_ui_bg+7 ) );
1501
1502 ui_rect title, panel;
1503 ui_split( window, k_ui_axis_h, 28, 0, title, panel );
1504 ui_fill( title, ui_colour( k_ui_bg+7 ) );
1505 ui_text( title, "Workshop tool", 1, k_ui_align_middle_center,
1506 ui_colourcont(k_ui_bg+7) );
1507
1508 ui_rect quit_button;
1509 ui_split( title, k_ui_axis_v, title[2]-title[3], 2, title, quit_button );
1510
1511 if( skaterift.async_op == k_async_op_none ){
1512 if( ui_button_text( quit_button, "X", 1 ) ){
1513 workshop_quit_form();
1514 return;
1515 }
1516 }
1517
1518 /*
1519 * temporary operation blinders, we don't yet have a nice way to show the
1520 * user that we're doing something uninterruptable, so the code just
1521 * escapes here and we show them a basic string
1522 */
1523
1524 if( skaterift.async_op != k_async_op_none ){
1525 const char *op_string = "The programmer has not bothered to describe "
1526 "the current operation that is running.";
1527
1528 switch( skaterift.async_op ){
1529 case k_workshop_form_op_loading_model:
1530 op_string = "Operation in progress: Loading model file.";
1531 break;
1532 case k_workshop_form_op_publishing_update:
1533 op_string = "Operation in progress: publishing submission update "
1534 "to steam.";
1535 break;
1536 case k_workshop_form_op_downloading_submission:
1537 op_string = "Operation in progress: downloading existing submission"
1538 " from Steam services.";
1539 break;
1540 default: break;
1541 }
1542
1543 ui_text( panel, op_string, 1, k_ui_align_middle_center, 0 );
1544 return;
1545 }
1546
1547 /* re draw board preview if need to */
1548 if( (stable_page == k_workshop_form_edit) &&
1549 workshop_form.view_changed &&
1550 workshop_form.file_intent == k_workshop_form_file_intent_new )
1551 {
1552 workshop_render_preview();
1553 workshop_form.view_changed = 0;
1554 }
1555
1556 struct workshop_form *form = &workshop_form;
1557
1558 ui_rect sidebar, content;
1559 ui_split_ratio( panel, k_ui_axis_v, 0.3f, 1, sidebar, content );
1560
1561 /* content page */
1562 ui_rect_pad( content, (ui_px[2]){8,8} );
1563
1564 if( stable_page == k_workshop_form_edit ){
1565 workshop_form_gui_edit_page( content );
1566 }
1567 else if( stable_page == k_workshop_form_open ){
1568 ui_text( content, "Nothing selected.", 1, k_ui_align_middle_center,
1569 ui_colour( k_ui_fg+4 ) );
1570 }
1571 else if( stable_page >= k_workshop_form_cclosing ){
1572 ui_rect submission_row;
1573 ui_split( content, k_ui_axis_h, content[3]-32-8, 0, content,
1574 submission_row );
1575
1576 u32 colour;
1577
1578 if( stable_page == k_workshop_form_closing_bad )
1579 colour = ui_colour( k_ui_red+k_ui_brighter );
1580 else
1581 colour = ui_colour( k_ui_green+k_ui_brighter );
1582
1583 ui_text( content, workshop_form.failure_or_success_string, 1,
1584 k_ui_align_middle_center, colour );
1585
1586 ui_rect submission_center;
1587 rect_copy( submission_row, submission_center );
1588 submission_center[2] = 128;
1589 ui_rect_center( submission_row, submission_center );
1590 ui_rect_pad( submission_center, (ui_px[2]){8,8} );
1591
1592 if( ui_button_text( submission_center, "OK", 1 ) ){
1593 workshop_form.page = k_workshop_form_open;
1594 }
1595 }
1596
1597 workshop_form_gui_sidebar( sidebar );
1598 }
1599
1600 /*
1601 * Some async api stuff
1602 * -----------------------------------------------------------------------------
1603 */
1604
1605 VG_STATIC void async_workshop_get_filepath( void *data, u32 len )
1606 {
1607 struct async_workshop_filepath_info *info = data;
1608
1609 u64 _size;
1610 u32 _ts;
1611
1612 ISteamUGC *hSteamUGC = SteamAPI_SteamUGC();
1613 if( !SteamAPI_ISteamUGC_GetItemInstallInfo( hSteamUGC, info->id, &_size,
1614 info->buf, info->len, &_ts ))
1615 {
1616 vg_error( "GetItemInstallInfo failed\n" );
1617 info->buf[0] = '\0';
1618 }
1619 }
1620
1621 VG_STATIC void async_workshop_get_installed_files( void *data, u32 len )
1622 {
1623 struct async_workshop_installed_files_info *info = data;
1624
1625 ISteamUGC *hSteamUGC = SteamAPI_SteamUGC();
1626 u32 count = SteamAPI_ISteamUGC_GetSubscribedItems( hSteamUGC, info->buffer,
1627 *info->len );
1628
1629 vg_info( "Found %u subscribed items\n", count );
1630
1631 u32 j=0;
1632 for( u32 i=0; i<count; i++ ){
1633 u32 state = SteamAPI_ISteamUGC_GetItemState( hSteamUGC, info->buffer[i] );
1634 if( state & k_EItemStateInstalled ){
1635 info->buffer[j ++] = info->buffer[i];
1636 }
1637 }
1638
1639 *info->len = j;
1640 }
1641
1642 #if 0
1643 VG_STATIC void vg_strsan_ascii( char *buf, u32 len )
1644 {
1645 for( u32 i=0; i<len-1; i ++ ){
1646 if( buf[i] == 0 ) return;
1647
1648 if( buf[i] < 32 || buf[i] > 126 ){
1649 buf[i] = '?';
1650 }
1651 }
1652 buf[len-1] = '\0';
1653 }
1654
1655 #define VG_STRSAN_ASCII( X ) vg_strsan_ascii( X, vg_list_size(X) )
1656
1657 VG_STATIC void workshop_load_metadata( const char *path,
1658 struct workshop_file_info *info )
1659 {
1660 FILE *fp = fopen( path, "rb" );
1661
1662 if( fp ){
1663 if( fread( info, sizeof( struct workshop_file_info ), 1, fp ) ){
1664 VG_STRSAN_ASCII( info->author_name );
1665 VG_STRSAN_ASCII( info->title );
1666 }
1667 fclose( fp );
1668 }
1669 }
1670 #endif
1671
1672 #endif /* WORKSHOP_C */