my fucking fingers
[carveJwlIkooP6JGAAIwe30JlM.git] / workshop.c
1 #ifndef WORKSHOP_C
2 #define WORKSHOP_C
3
4 #define VG_GAME
5 #include "vg/vg.h"
6 #include "vg/vg_tex.h"
7 #include "ent_skateshop.h"
8
9 #include "vg/vg_steam_ugc.h"
10
11 struct workshop_form{
12 char title[80];
13 char description[512];
14 char model_path[128];
15
16 struct player_board board_model;
17
18 world_instance *view_world;
19 ent_swspreview *ptr_ent;
20
21 v2f view_angles,
22 view_angles_begin;
23 v3f view_offset,
24 view_offset_begin;
25
26 float view_dist;
27 int view_changed;
28
29 enum workshop_form_status{
30 k_workshop_form_hidden,
31 k_workshop_form_editing,
32 k_workshop_form_submitting,
33 k_workshop_form_failed
34 }
35 status;
36 const char *failure_reason;
37 PublishedFileId_t publishing_file_id;
38
39 int img_w, img_h;
40 u8 *img_buffer;
41 }
42 static workshop_form;
43
44 struct workshop_package_info {
45 int success;
46 char abs_preview_image[ 1024 ];
47 char abs_content_folder[ 1024 ];
48 char abs_content_file[ 1024 ];
49 };
50
51 VG_STATIC void on_workshop_update_result( void *data, void *user )
52 {
53 vg_info( "Recieved workshop update result\n" );
54 SubmitItemUpdateResult_t *result = data;
55
56 if( result->m_bUserNeedsToAcceptWorkshopLegalAgreement ){
57 vg_warn( "Workshop agreement currently not accepted\n" );
58 }
59
60 if( result->m_eResult == k_EResultOK ){
61 vg_success( "Successfully uploaded workshop file\n" );
62 }
63 else{
64 vg_error( "Error with the submitted file (%d)\n", result->m_eResult );
65 }
66 }
67
68 VG_STATIC void workshop_form_async_package_complete( void *data, u32 size )
69 {
70 struct workshop_package_info *info = data;
71 if( !info->success ){
72 workshop_form.status = k_workshop_form_failed;
73 workshop_form.failure_reason = "Packaging workshop folder failed.";
74 return;
75 }
76
77 ISteamUGC *hSteamUGC = SteamAPI_SteamUGC();
78 UGCUpdateHandle_t handle
79 = SteamAPI_ISteamUGC_StartItemUpdate( hSteamUGC, SKATERIFT_APPID,
80 workshop_form.publishing_file_id );
81
82 /* TODO: Handle failure cases for these */
83 vg_info( "Setting title\n" );
84 SteamAPI_ISteamUGC_SetItemTitle( hSteamUGC, handle, workshop_form.title );
85
86 vg_info( "Setting description\n" );
87 SteamAPI_ISteamUGC_SetItemDescription( hSteamUGC, handle,
88 workshop_form.description );
89 vg_info( "Setting preview image\n" );
90 SteamAPI_ISteamUGC_SetItemPreview( hSteamUGC,
91 handle, info->abs_preview_image );
92 vg_info( "Setting item content\n" );
93 SteamAPI_ISteamUGC_SetItemContent( hSteamUGC, handle,
94 info->abs_content_folder );
95 vg_info( "Setting visibility\n" );
96 SteamAPI_ISteamUGC_SetItemVisibility( hSteamUGC, handle,
97 k_ERemoteStoragePublishedFileVisibilityPublic );
98
99 vg_info( "Submitting updates\n" );
100 vg_steam_async_call *call = vg_alloc_async_steam_api_call();
101 call->userdata = NULL;
102 call->p_handler = on_workshop_update_result;
103 call->id = SteamAPI_ISteamUGC_SubmitItemUpdate( hSteamUGC, handle, "" );
104 }
105
106 VG_STATIC void workshop_package_thread( void *data )
107 {
108 vg_info( "Packaging workshop content folder\n" );
109
110 vg_async_item *call = vg_async_alloc( sizeof(struct workshop_package_info) );
111 struct workshop_package_info *info = call->payload;
112
113 /* build content folder path */
114 snprintf( info->abs_content_folder, 1024, "%smodels/boards/workshop/%lu",
115 vg.base_path, (u64)workshop_form.publishing_file_id );
116
117 /* build content file path */
118 snprintf( info->abs_content_file, 1024, "%s/%s",
119 info->abs_content_folder,
120 vg_path_filename( workshop_form.model_path ) );
121
122 /* build workshop preview file path */
123 snprintf( info->abs_preview_image, 1024, "%sworkshop_preview.jpg",
124 vg.base_path );
125
126 /* arange files */
127 if( !vg_mkdir( info->abs_content_folder ) ){
128 info->success = 0;
129 vg_async_dispatch( call, workshop_form_async_package_complete );
130 return;
131 }
132
133 vg_linear_clear( vg_mem.scratch );
134 if( !vg_file_copy( workshop_form.model_path, info->abs_content_file,
135 vg_mem.scratch ) ){
136 info->success = 0;
137 vg_async_dispatch( call, workshop_form_async_package_complete );
138 return;
139 }
140
141 info->success = 1;
142 vg_async_dispatch( call, workshop_form_async_package_complete );
143 }
144
145 VG_STATIC void on_workshop_createitem( void *data, void *user )
146 {
147 CreateItemResult_t *result = data;
148
149 if( result->m_eResult == k_EResultOK ){
150 vg_info( "Created workshop file with id: %lu\n",
151 result->m_nPublishedFileId );
152
153
154 if( result->m_bUserNeedsToAcceptWorkshopLegalAgreement ){
155 vg_warn( "Workshop agreement currently not accepted\n" );
156 }
157
158 workshop_form.publishing_file_id = result->m_nPublishedFileId;
159 vg_loader_start( workshop_package_thread, NULL );
160 }
161 else{
162 const char *error = NULL;
163 switch( result->m_eResult ){
164 case k_EResultInsufficientPrivilege:
165 error = "Your account currently is restricted from uploading content "
166 "due to a hub ban, account lock, or community ban. You need to "
167 "contact Steam Support to resolve the issue.\n";
168 break;
169 case k_EResultBanned:
170 error = "You do not have permission to upload content to this hub "
171 "because you have an active VAC or Game ban.";
172 break;
173 case k_EResultTimeout:
174 error = "Timeout: The operation took longer than expected, please try "
175 "again.";
176 break;
177 case k_EResultNotLoggedOn:
178 error = "You are currently not logged into Steam.";
179 break;
180 case k_EResultServiceUnavailable:
181 error = "The workshop server is having issues, please try again.";
182 break;
183 case k_EResultInvalidParam:
184 error = "One of the submission fields contains something not being "
185 "accepted by that field.";
186 break;
187 case k_EResultAccessDenied:
188 error = "There was a problem trying to save the title and description. "
189 "Access was denied.";
190 break;
191 case k_EResultLimitExceeded:
192 error = "You have exceeded your Steam Cloud quota. If you wish to "
193 "upload this file, you must remove some published items.";
194 break;
195 case k_EResultFileNotFound:
196 error = "The uploaded file could not be found.";
197 break;
198 case k_EResultDuplicateRequest:
199 error = "The file was already successfully uploaded.";
200 break;
201 case k_EResultDuplicateName:
202 error = "You already have a Steam Workshop item with that name.";
203 break;
204 case k_EResultServiceReadOnly:
205 error = "Due to a recent password or email change, you are not allowed "
206 "to upload new content. Usually this restriction will expire in"
207 " 5 days, but can last up to 30 days if the account has been "
208 "inactive recently.";
209 break;
210 default: workshop_form.failure_reason = "Unknown failure";
211 }
212
213 vg_error( "ISteamUGC_CreateItem() failed(%d): '%s' \n",
214 result->m_eResult, error );
215 workshop_form.failure_reason = error;
216 workshop_form.status = k_workshop_form_failed;
217 }
218 }
219
220 VG_STATIC void workshop_form_async_submit_complete( void *payload, u32 size )
221 {
222 vg_steam_async_call *call = vg_alloc_async_steam_api_call();
223 call->userdata = NULL;
224 call->p_handler = on_workshop_createitem;
225
226 ISteamUGC *hSteamUGC = SteamAPI_SteamUGC();
227 call->id = SteamAPI_ISteamUGC_CreateItem( hSteamUGC, SKATERIFT_APPID,
228 k_EWorkshopFileTypeCommunity );
229 }
230
231 VG_STATIC void workshop_form_sync_download_image( void *payload, u32 size )
232 {
233 int w, h;
234 render_fb_get_current_res( gpipeline.fb_workshop_preview, &w, &h );
235 vg_linear_clear( vg_mem.scratch );
236 workshop_form.img_buffer = vg_linear_alloc( vg_mem.scratch, w*h*3 );
237
238 vg_info( "read framebuffer: glReadPixels( %dx%d )\n", w,h );
239
240 glBindFramebuffer( GL_READ_FRAMEBUFFER, gpipeline.fb_workshop_preview->fb );
241 glReadBuffer( GL_COLOR_ATTACHMENT0 );
242 glReadPixels( 0,0, w,h, GL_RGB, GL_UNSIGNED_BYTE, workshop_form.img_buffer );
243
244 workshop_form.img_w = w;
245 workshop_form.img_h = h;
246 }
247
248 VG_STATIC void workshop_form_submit_thread( void *data )
249 {
250 vg_async_call( workshop_form_sync_download_image, NULL, 0 );
251 vg_async_stall();
252
253 int w = workshop_form.img_w,
254 h = workshop_form.img_h;
255
256 vg_info( "writing: workshop_preview.jpg (%dx%d @90%%)\n", w,h );
257 stbi_flip_vertically_on_write(1);
258 stbi_write_jpg( "workshop_preview.jpg", w,h, 3,
259 workshop_form.img_buffer, 90 );
260
261 vg_async_call( workshop_form_async_submit_complete, NULL, 0 );
262 }
263
264 VG_STATIC void workshop_form_async_complete( void *payload, u32 size )
265 {
266 v2_zero( workshop_form.view_angles );
267 v3_zero( workshop_form.view_offset );
268 workshop_form.view_dist = 1.0f;
269 workshop_form.status = k_workshop_form_editing;
270 workshop_form.view_changed = 1;
271 }
272
273 VG_STATIC void workshop_form_load_thread( void *data )
274 {
275 vg_info( "workshop_form_load_thread()\n" );
276 player_board_load( &workshop_form.board_model, workshop_form.model_path );
277 vg_success( "loaded\n" );
278
279 vg_async_call( workshop_form_async_complete, NULL, 0 );
280 }
281
282 VG_STATIC void workshop_start_submission( const char *path )
283 {
284 if( workshop_form.status != k_workshop_form_hidden ){
285 vg_error( "Workshop form is already open\n" );
286 return;
287 }
288
289 workshop_form.view_world = get_active_world();
290
291 if( mdl_arrcount( &workshop_form.view_world->ent_swspreview ) ){
292 workshop_form.ptr_ent =
293 mdl_arritm( &workshop_form.view_world->ent_swspreview, 0 );
294
295 vg_strncpy( path, workshop_form.model_path, 128,
296 k_strncpy_always_add_null );
297 workshop_form.description[0] = '\0';
298 workshop_form.title[0] = '\0';
299
300 vg_loader_start( workshop_form_load_thread, NULL );
301 }
302 else{
303 vg_error( "There is no ent_swspreview in the level. "
304 "Cannot publish here\n" );
305 }
306 }
307
308 VG_STATIC int workshop_submit_command( int argc, const char *argv[] )
309 {
310 if( argc == 1 ){
311 workshop_start_submission( argv[0] );
312 }
313 else{
314 vg_error( "usage: workshop_submit <path>\n" );
315 }
316 return 0;
317 }
318
319 VG_STATIC void workshop_init(void)
320 {
321 vg_console_reg_cmd( "workshop_submit", workshop_submit_command, NULL );
322 }
323
324 VG_STATIC void workshop_form_gui(void)
325 {
326 if( workshop_form.status == k_workshop_form_hidden ) return;
327
328 ui_rect null;
329 ui_rect screen = { 0, 0, vg.window_x, vg.window_y };
330 ui_rect window = { 0, 0, 1000, 700 };
331 ui_rect_center( screen, window );
332 vg_ui.wants_mouse = 1;
333
334 ui_fill( window, ui_colour( k_ui_bg+1 ) );
335 ui_outline( window, 1, ui_colour( k_ui_bg+7 ) );
336
337 ui_rect title, panel;
338 ui_split_px( window, k_ui_axis_h, 28, 0, title, panel );
339 ui_fill( title, ui_colour( k_ui_bg+7 ) );
340 ui_text( title, "Workshop tool", 1, k_ui_align_middle_center,
341 ui_colourcont(k_ui_bg+7) );
342
343 if( workshop_form.status == k_workshop_form_submitting ){
344 ui_text( panel, "Submitting... status: ...\n", 1,
345 k_ui_align_middle_center, 0 );
346 return;
347 }
348
349 /* re draw board preview if need to */
350 if( workshop_form.view_changed ){
351 render_fb_bind( gpipeline.fb_workshop_preview, 0 );
352
353 glClearColor( 0.0f, 0.0f, 0.3f, 1.0f );
354 glClear( GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT );
355 glEnable( GL_DEPTH_TEST );
356 glDisable( GL_BLEND );
357
358 ent_swspreview *swsprev = workshop_form.ptr_ent;
359 world_instance *world = workshop_form.view_world;
360
361 ent_camera *ref = mdl_arritm( &world->ent_camera,
362 mdl_entity_id_id(swsprev->id_camera) );
363 ent_marker *display = mdl_arritm( &world->ent_marker,
364 mdl_entity_id_id(swsprev->id_display) ),
365 *display1= mdl_arritm( &world->ent_marker,
366 mdl_entity_id_id(swsprev->id_display1) );
367
368 v3f baseco;
369 v3_add( display->transform.co, display1->transform.co, baseco );
370 v3_muls( baseco, 0.5f, baseco );
371
372 camera cam;
373 v3f basevector;
374 v3_sub( display->transform.co, ref->transform.co, basevector );
375 float dist = v3_length( basevector );
376
377 v3f baseangles;
378 player_vector_angles( baseangles, basevector, 1.0f, 0.0f );
379
380 v2_add( workshop_form.view_angles, baseangles, cam.angles );
381 cam.angles[2] = 0.0f;
382
383 float sX = sinf( cam.angles[0] ),
384 cX = cosf( cam.angles[0] ),
385 sY = sinf( cam.angles[1] ),
386 cY = cosf( cam.angles[1] );
387
388 v3f offset = { -sX * cY, sY, cX * cY };
389
390 v3_muladds( display->transform.co, offset,
391 dist*workshop_form.view_dist, cam.pos );
392
393 cam.pos[0] += -sX*workshop_form.view_offset[2];
394 cam.pos[2] += cX*workshop_form.view_offset[2];
395 cam.pos[0] += cX*workshop_form.view_offset[0];
396 cam.pos[2] += sX*workshop_form.view_offset[0];
397 cam.pos[1] += workshop_form.view_offset[1];
398
399 cam.nearz = 0.01f;
400 cam.farz = 100.0f;
401 cam.fov = ref->fov;
402
403 camera_update_transform( &cam );
404 camera_update_view( &cam );
405 camera_update_projection( &cam );
406 camera_finalize( &cam );
407
408 m4x3f mmdl, mmdl1;
409 mdl_transform_m4x3( &display->transform, mmdl );
410 mdl_transform_m4x3( &display1->transform, mmdl1 );
411
412 /* force update this for nice shadows. its usually set in the world
413 * pre-render step, but that includes timer stuff
414 */
415 struct player_board *board = &workshop_form.board_model;
416 struct ub_world_lighting *ubo = &world->ub_lighting;
417 v3f vp0, vp1;
418 v3_copy((v3f){0.0f,0.1f, board->truck_positions[0][2]}, vp0 );
419 v3_copy((v3f){0.0f,0.1f, board->truck_positions[1][2]}, vp1 );
420 m4x3_mulv( mmdl1, vp0, ubo->g_board_0 );
421 m4x3_mulv( mmdl1, vp1, ubo->g_board_1 );
422 glBindBuffer( GL_UNIFORM_BUFFER, world->ubo_lighting );
423 glBufferSubData( GL_UNIFORM_BUFFER, 0,
424 sizeof(struct ub_world_lighting), &world->ub_lighting );
425
426 render_world( world, &cam, 1 );
427 render_board( &cam, world, board, mmdl, k_board_shader_entity );
428 render_board( &cam, world, board, mmdl1, k_board_shader_entity );
429
430 glBindFramebuffer( GL_FRAMEBUFFER, 0 );
431 glViewport( 0,0, vg.window_x, vg.window_y );
432
433 workshop_form.view_changed = 0;
434 }
435
436 struct workshop_form *form = &workshop_form;
437
438 ui_rect sidebar, content;
439 ui_split_ratio( panel, k_ui_axis_v, 0.3f, 1, sidebar, content );
440
441 /* entries panel */
442 ui_fill( sidebar, ui_colour( k_ui_bg+2 ) );
443
444 ui_split_px( sidebar, k_ui_axis_h, 28, 0, title, sidebar );
445 ui_text( title, "Your submissions", 1, k_ui_align_middle_center, 0 );
446
447 ui_rect controls;
448 ui_split_px( sidebar, k_ui_axis_h, 28, 0, controls, sidebar );
449 ui_fill( controls, ui_colour( k_ui_bg+1 ) );
450 ui_outline( controls, -1, ui_colour( k_ui_bg+4 ) );
451
452 ui_rect info;
453 ui_split_ratio( controls, k_ui_axis_v, 0.25f, 0, info, controls );
454 ui_text( info, "page 0/2", 1, k_ui_align_middle_center, 0 );
455
456 ui_rect btn_left, btn_right;
457 ui_split_ratio( controls, k_ui_axis_v, 0.5f, 2, btn_left, btn_right );
458
459 if( ui_button_text( btn_left, "prev", 1 ) ){
460 vg_info( "left\n" );
461 }
462
463 if( ui_button_text( btn_right, "next", 1 ) ){
464 vg_info( "right\n" );
465 }
466
467 /* content page */
468 ui_rect_pad( content, 8 );
469
470 ui_rect image_plane;
471 ui_split_px( content, k_ui_axis_h, 300, 0, image_plane, content );
472 ui_fill( image_plane, ui_colour( k_ui_bg+0 ) );
473
474 ui_rect img_box;
475 ui_fit_item( image_plane, (ui_px[2]){ 3, 2 }, img_box );
476 ui_image( img_box, gpipeline.fb_workshop_preview->attachments[0].id );
477
478 {
479 int hover = ui_inside_rect( img_box, vg_ui.mouse ),
480 target = ui_inside_rect( img_box, vg_ui.mouse_click );
481
482 if( ui_click_down(UI_MOUSE_MIDDLE) && target ){
483 v3_copy( workshop_form.view_offset,
484 workshop_form.view_offset_begin );
485 }
486 else if( ui_click_down(UI_MOUSE_LEFT) && target ){
487 v2_copy( workshop_form.view_angles,
488 workshop_form.view_angles_begin );
489 }
490
491 if( ui_clicking(UI_MOUSE_MIDDLE) && target ){
492 v2f delta = { vg_ui.mouse[0]-vg_ui.mouse_click[0],
493 vg_ui.mouse[1]-vg_ui.mouse_click[1] };
494
495 float *begin = workshop_form.view_offset_begin,
496 *offset = workshop_form.view_offset;
497 offset[0] = vg_clampf( begin[0]-delta[0]*0.002f, -1.0f, 1.0f );
498 offset[2] = vg_clampf( begin[2]-delta[1]*0.002f, -1.0f, 1.0f );
499 workshop_form.view_changed = 1;
500 }
501 else if( ui_clicking(UI_MOUSE_LEFT) && target ){
502 v2f delta = { vg_ui.mouse[0]-vg_ui.mouse_click[0],
503 vg_ui.mouse[1]-vg_ui.mouse_click[1] };
504
505 v2f angles;
506 v2_muladds( workshop_form.view_angles_begin, delta, 0.002f, angles);
507
508 float limit = VG_PIf*0.2f;
509
510 angles[0] = vg_clampf( angles[0], -limit, limit );
511 angles[1] = vg_clampf( angles[1], -limit, limit );
512
513 v2_copy( angles, workshop_form.view_angles );
514 workshop_form.view_changed = 1;
515 }
516
517 if( !ui_clicking(UI_MOUSE_LEFT) && hover ){
518 float zoom = workshop_form.view_dist;
519 zoom += vg.mouse_wheel[1] * -0.07f;
520 zoom = vg_clampf( zoom, 0.4f, 2.0f );
521
522 if( zoom != workshop_form.view_dist ){
523 workshop_form.view_changed = 1;
524 workshop_form.view_dist = zoom;
525 }
526 }
527 }
528
529 /* file path */
530 ui_rect file_label;
531 ui_split_px( content, k_ui_axis_h, 8, 0, null, content );
532 ui_split_px( content, k_ui_axis_h, 28, 0, file_label, content );
533 ui_text( file_label, form->model_path, 1, k_ui_align_middle_center,
534 ui_colour( k_ui_fg+4 ) );
535
536 /* title box */
537 ui_rect title_entry, label;
538 ui_split_px( content, k_ui_axis_h, 8, 0, null, content );
539 ui_split_px( content, k_ui_axis_h, 28, 0, title_entry, content );
540
541 const char *str_title = "Title:", *str_desc = "Description:";
542 ui_split_px( title_entry, k_ui_axis_v,
543 ui_text_line_width(str_title)+8, 0, label, title_entry );
544
545 ui_text( label, str_title, 1, k_ui_align_middle_left, 0 );
546 ui_textbox( title_entry, form->title, vg_list_size(form->title), 0 );
547
548 /* description box */
549 ui_rect desc_entry;
550 ui_split_px( content, k_ui_axis_h, 8, 0, null, content );
551 ui_split_px( content, k_ui_axis_h, 28*4, 0, desc_entry, content );
552 ui_split_px( desc_entry, k_ui_axis_v,
553 ui_text_line_width(str_desc)+8, 0, label, desc_entry );
554 ui_text( label, str_desc, 1, k_ui_align_middle_left, 0 );
555 ui_textbox( desc_entry, form->description,
556 vg_list_size(form->description),
557 UI_TEXTBOX_MULTILINE|UI_TEXTBOX_WRAP );
558
559 /* submissionable */
560 ui_rect submission_row;
561 ui_split_px( content, k_ui_axis_h, 8, 0, null, content );
562 ui_split_px( content, k_ui_axis_h, content[3]-32-8, 0, content,
563 submission_row );
564
565 ui_rect submission_center;
566 rect_copy( submission_row, submission_center );
567 submission_center[2] = 256;
568 ui_rect_center( submission_row, submission_center );
569
570 ui_split_ratio( submission_center, k_ui_axis_v, 0.5f, 8,
571 btn_left, btn_right );
572
573 if( ui_button_text( btn_left, "Publish", 1 ) ){
574 workshop_form.status = k_workshop_form_submitting;
575 vg_loader_start( workshop_form_submit_thread, NULL );
576 }
577 if( ui_button_text( btn_right, "Cancel", 1 ) ){
578 vg_info( "left\n" );
579 }
580
581 /* disclaimer */
582 const char *disclaimer_text =
583 "By submitting this item, you agree to the workshop terms of service";
584
585 ui_rect disclaimer_row, inner, link;
586 ui_split_px( content, k_ui_axis_h, 8, 0, null, content );
587 ui_split_px( content, k_ui_axis_h, content[3]-32, 0, content,
588 disclaimer_row );
589
590 ui_px btn_width = 32;
591
592 rect_copy( disclaimer_row, inner );
593 inner[2] = ui_text_line_width( disclaimer_text ) + btn_width+8;
594
595 ui_rect_center( disclaimer_row, inner );
596 ui_split_px( inner, k_ui_axis_v, inner[2]-btn_width, 0, label, btn_right);
597 ui_rect_pad( btn_right, 2 );
598
599 if( ui_button_text( btn_right, "\x91", 2 ) ){
600 vg_info( "Open link\n" );
601 }
602
603 ui_text( label, disclaimer_text, 1, k_ui_align_middle_left,
604 ui_colour( k_ui_fg+4 ) );
605 }
606
607
608
609 #endif /* WORKSHOP_C */