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