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