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