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