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