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