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