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