a mess but stable
[carveJwlIkooP6JGAAIwe30JlM.git] / workshop.c
index 4d8856bed8a2410b89a6e300897d436f163120ac..6975d601e587d43edcdd6ff7790a18bf69f7ce05 100644 (file)
@@ -1,20 +1,19 @@
-#ifndef WORKSHOP_C
-#define WORKSHOP_C
-
-#include "workshop.h"
-
-#define VG_GAME
-#include "vg/vg.h"
+#include "vg/vg_engine.h"
 #include "vg/vg_tex.h"
+#include "vg/vg_image.h"
 #include "vg/vg_msg.h"
 #include "vg/vg_binstr.h"
+#include "vg/vg_loader.h"
+#include "vg/vg_io.h"
 #include "ent_skateshop.h"
 
 #include "vg/vg_steam_auth.h"
 #include "vg/vg_steam_ugc.h"
 #include "vg/vg_steam_friends.h"
 #include "steam.h"
-#include "highscores.h"
+#include "workshop.h"
+
+struct workshop_form workshop_form;
 
 static struct ui_enum_opt workshop_form_visibility_opts[] = {
  { k_ERemoteStoragePublishedFileVisibilityPublic,       "Public"       },
@@ -276,13 +275,13 @@ static void workshop_form_async_submit_begin( void *payload, u32 size )
 static void workshop_form_async_download_image( void *payload, u32 size )
 {
    int w, h;
-   render_fb_get_current_res( gpipeline.fb_workshop_preview, &w, &h );
+   vg_framebuffer_get_res( g_render.fb_workshop_preview, &w, &h );
    vg_linear_clear( vg_mem.scratch );
    workshop_form.img_buffer = vg_linear_alloc( vg_mem.scratch, w*h*3 );
 
    vg_info( "read framebuffer: glReadPixels( %dx%d )\n", w,h );
 
-   glBindFramebuffer( GL_READ_FRAMEBUFFER, gpipeline.fb_workshop_preview->fb );
+   glBindFramebuffer( GL_READ_FRAMEBUFFER, g_render.fb_workshop_preview->id );
    glReadBuffer( GL_COLOR_ATTACHMENT0 );
    glReadPixels( 0,0, w,h, GL_RGB, GL_UNSIGNED_BYTE, workshop_form.img_buffer );
 
@@ -323,7 +322,8 @@ static void _workshop_form_submit_thread( void *data )
    vg_msg_frame( &descriptor, "workshop" );
       vg_msg_wkvstr( &descriptor, "title", workshop_form.submission.title );
       //vg_msg_wkvstr( &descriptor, "author", "unknown" );
-      vg_msg_wkvu32( &descriptor, "type", workshop_form.submission.type );
+      vg_msg_wkvnum( &descriptor, "type", k_vg_msg_u32, 1,
+                     &workshop_form.submission.type );
       vg_msg_wkvstr( &descriptor, "folder", workshop_form.addon_folder );
    vg_msg_end_frame( &descriptor );
    //vg_msg_wkvstr( &descriptor, "location", "USA" );
@@ -335,14 +335,17 @@ static void _workshop_form_submit_thread( void *data )
    vg_info( "binstr: %s\n", short_descriptor_str );
 
    vg_dir dir;
-   if( !vg_dir_open( &dir, folder.buffer ) ){
+   if( !vg_dir_open( &dir, folder.buffer ) )
+   {
       vg_error( "could not open addon folder '%s'\n", folder.buffer );
       workshop_form.op = k_workshop_op_none;
       return;
    }
 
-   while( vg_dir_next_entry(&dir) ){
-      if( vg_dir_entry_type(&dir) == k_vg_entry_type_file ){
+   while( vg_dir_next_entry(&dir) )
+   {
+      if( vg_dir_entry_type(&dir) == k_vg_entry_type_file )
+      {
          const char *d_name = vg_dir_entry_name(&dir);
          if( d_name[0] == '.' ) continue;
 
@@ -403,29 +406,37 @@ static void _workshop_form_submit_thread( void *data )
 /*
  * Entry point for the publishing submission operation
  */
-static void workshop_op_submit(void){
+static void workshop_op_submit( ui_context *ctx )
+{
    /* TODO: Show these errors to the user */
-   if( workshop_form.submission.submit_title ){
-      if( !workshop_form.submission.title[0] ){
-         ui_start_modal( "Cannot submit because a title is required\n",
-                         UI_MODAL_WARN);
+   if( workshop_form.submission.submit_title )
+   {
+      if( !workshop_form.submission.title[0] )
+      {
+         ui_start_modal( ctx, "Cannot submit because a title is required\n",
+                         UI_MODAL_WARN );
          workshop_form.op = k_workshop_op_none;
          return;
       }
    }
 
-   if( workshop_form.submission.submit_description ){
-      if( !workshop_form.submission.description[0] ){
-         ui_start_modal( "Cannot submit because a description is required\n",
+   if( workshop_form.submission.submit_description )
+   {
+      if( !workshop_form.submission.description[0] )
+      {
+         ui_start_modal( ctx, 
+                         "Cannot submit because a description is required\n",
                          UI_MODAL_WARN );
          workshop_form.op = k_workshop_op_none;
          return;
       }
    }
 
-   if( workshop_form.submission.submit_file_and_image ){
-      if( workshop_form.file_intent == k_workshop_form_file_intent_none ){
-         ui_start_modal( "Cannot submit because the file is "
+   if( workshop_form.submission.submit_file_and_image )
+   {
+      if( workshop_form.file_intent == k_workshop_form_file_intent_none )
+      {
+         ui_start_modal( ctx, "Cannot submit because the file is "
                          "empty or unspecified\n", UI_MODAL_WARN );
          workshop_form.op = k_workshop_op_none;
          return;
@@ -532,25 +543,29 @@ static void _workshop_form_load_thread( void *data )
 /*
  * Entry point for load model operation
  */
-static void workshop_op_load_model(void){
+static void workshop_op_load_model( ui_context *ctx )
+{
    world_instance *world = world_current_instance();
    workshop_form.view_world = world;
 
-   if( workshop_form.submission.type == k_addon_type_board ){
-      if( mdl_arrcount( &world->ent_swspreview ) ){
-         workshop_form.ptr_ent = 
-               mdl_arritm( &world->ent_swspreview, 0 );
+   if( workshop_form.submission.type == k_addon_type_board )
+   {
+      if( mdl_arrcount( &world->ent_swspreview ) )
+      {
+         workshop_form.ptr_ent = mdl_arritm( &world->ent_swspreview, 0 );
       }
-      else{
-         ui_start_modal( "There is no ent_swspreview in the level. \n"
+      else
+      {
+         ui_start_modal( ctx, "There is no ent_swspreview in the level. \n"
                          "Cannot publish here\n", UI_MODAL_BAD );
          workshop_form.op = k_workshop_op_none;
          return;
       }
    }
    else if( workshop_form.submission.type == k_addon_type_player ){}
-   else {
-      ui_start_modal( "Don't know how to prepare for this item type. \n"
+   else 
+   {
+      ui_start_modal( ctx, "Don't know how to prepare for this item type. \n"
                       "Please contact the developers.\n", UI_MODAL_BAD );
       workshop_form.op = k_workshop_op_none;
       return;
@@ -570,9 +585,10 @@ static void workshop_op_load_model(void){
  */
 static void workshop_form_async_imageload( void *data, u32 len )
 {
-   if( data ){
-      struct framebuffer_attachment *a = 
-         &gpipeline.fb_workshop_preview->attachments[0];
+   if( data )
+   {
+      vg_framebuffer_attachment *a = 
+         &g_render.fb_workshop_preview->attachments[0];
 
       glBindTexture( GL_TEXTURE_2D, a->id );
       glTexSubImage2D( GL_TEXTURE_2D, 0,0,0,
@@ -581,11 +597,12 @@ static void workshop_form_async_imageload( void *data, u32 len )
       stbi_image_free( data );
       vg_success( "Loaded workshop preview image\n" );
    }
-   else{
+   else
+   {
       snprintf( workshop_form.error_msg, sizeof(workshop_form.error_msg),
                "Preview image could not be loaded. Reason: %s\n",
                 stbi_failure_reason() );
-      ui_start_modal( workshop_form.error_msg, UI_MODAL_BAD );
+      ui_start_modal( &vg_ui.ctx, workshop_form.error_msg, UI_MODAL_BAD );
    }
    workshop_form.op = k_workshop_op_none;
 }
@@ -601,27 +618,33 @@ static void _workshop_load_preview_thread( void *data ){
    vg_strcat( &path, workshop_form.addon_folder );
    vg_strcat( &path, "/preview.jpg" );
 
-   if( vg_strgood( &path ) ){
+   if( vg_strgood( &path ) )
+   {
       stbi_set_flip_vertically_on_load(1);
       int x, y, nc;
       u8 *rgb = stbi_load( path.buffer, &x, &y, &nc, 3 );
 
-      if( rgb ){
-         if( (x == WORKSHOP_PREVIEW_WIDTH) && (y == WORKSHOP_PREVIEW_HEIGHT) ){
+      if( rgb )
+      {
+         if( (x == WORKSHOP_PREVIEW_WIDTH) && (y == WORKSHOP_PREVIEW_HEIGHT) )
+         {
             vg_async_call( workshop_form_async_imageload, rgb, x*y*3 );
          }
-         else{
+         else
+         {
             vg_error( "Resolution does not match framebuffer, so we can't"
                       " show it\n" );
             stbi_image_free( rgb );
             vg_async_call( workshop_form_async_imageload, NULL, 0 );
          }
       }
-      else{
+      else
+      {
          vg_async_call( workshop_form_async_imageload, NULL, 0 );
       }
    }
-   else{
+   else
+   {
       vg_async_call( workshop_form_async_imageload, NULL, 0 );
    }
 }
@@ -673,31 +696,36 @@ static void workshop_op_download_and_view_submission( int result_index )
       workshop_form.submission.type = k_addon_type_none;
       workshop_form.submission.submission_type_selection = k_addon_type_none;
 
-      if( have_meta ){
+      if( have_meta )
+      {
          u32 len = strlen(metadata_str);
          vg_info( "Metadata: %s\n", metadata_str );
          vg_str_bin( metadata_str, metadata_buf, len );
          vg_msg msg;
          vg_msg_init( &msg, metadata_buf, len/2 );
          
-         if( vg_msg_seekframe( &msg, "workshop" )){
-            u32 type = vg_msg_getkvu32( &msg, "type", 0 );
+         if( vg_msg_seekframe( &msg, "workshop" ))
+         {
+            u32 type;
+            vg_msg_getkvintg( &msg, "type", k_vg_msg_u32, &type, NULL );
             workshop_form.submission.type = type;
             workshop_form.submission.submission_type_selection = type;
 
             const char *kv_folder = vg_msg_getkvstr( &msg, "folder" );
-            if( kv_folder ){
+            if( kv_folder )
+            {
                vg_strncpy( kv_folder, workshop_form.addon_folder,
                            sizeof(workshop_form.addon_folder),
                            k_strncpy_always_add_null );
             }
          }
       }
-      else{
+      else
+      {
          vg_error( "No metadata was returned with this item.\n" );
       }
 
-      render_fb_bind( gpipeline.fb_workshop_preview, 0 );
+      vg_framebuffer_bind( g_render.fb_workshop_preview, 1.0f );
       glClearColor( 0.2f, 0.0f, 0.0f, 1.0f );
       glClear( GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT );
       glBindFramebuffer( GL_FRAMEBUFFER, 0 );
@@ -705,7 +733,8 @@ static void workshop_op_download_and_view_submission( int result_index )
 
       vg_loader_start( _workshop_load_preview_thread, NULL );
    }
-   else{
+   else
+   {
       vg_error( "GetQueryUGCResult: Index out of range\n" );
       workshop_form.op = k_workshop_op_none;
    }
@@ -721,7 +750,8 @@ static void workshop_op_download_and_view_submission( int result_index )
  */
 static void workshop_view_page( int req )
 {
-   if( workshop_form.ugc_query.result != k_EResultOK ){
+   if( workshop_form.ugc_query.result != k_EResultOK )
+   {
       vg_error( "Tried to change page without complete data\n" );
       workshop_form.op = k_workshop_op_none;
       return;
@@ -739,7 +769,8 @@ static void workshop_view_page( int req )
    workshop_form.published_files_list_length = count;
    ISteamUGC *hSteamUGC = SteamAPI_SteamUGC();
 
-   for( int i=0; i<count; i ++ ){
+   for( int i=0; i<count; i ++ )
+   {
       struct published_file *pfile = &workshop_form.published_files_list[i];
 
       SteamUGCDetails_t details;
@@ -748,10 +779,12 @@ static void workshop_view_page( int req )
                                                 start+i,
                                                 &details ) )
       {
-         if( details.m_eResult != k_EResultOK ){
+         if( details.m_eResult != k_EResultOK )
+         {
             snprintf( pfile->title, 80, "Error (%d)", details.m_eResult );
          }
-         else{
+         else
+         {
             vg_strncpy( details.m_rgchTitle, pfile->title, 80,
                         k_strncpy_always_add_null );
          }
@@ -759,7 +792,8 @@ static void workshop_view_page( int req )
          pfile->result = details.m_eResult;
          pfile->result_index = start+i;
       }
-      else{
+      else
+      {
          pfile->result = k_EResultValueOutOfRange;
          pfile->result_index = -1;
          snprintf( pfile->title, 80, "Error (invalid index)" );
@@ -775,8 +809,10 @@ static void on_workshop_UGCQueryComplete( void *data, void *userdata )
    SteamUGCQueryCompleted_t *query = data;
    workshop_form.ugc_query.result = query->m_eResult;
 
-   if( query->m_eResult == k_EResultOK ){
-      if( query->m_unTotalMatchingResults > 50 ){
+   if( query->m_eResult == k_EResultOK )
+   {
+      if( query->m_unTotalMatchingResults > 50 )
+      {
          vg_warn( "You have %d items submitted, "
                   "we can only view the last 50\n" );
       }
@@ -794,7 +830,8 @@ static void on_workshop_UGCQueryComplete( void *data, void *userdata )
 
       workshop_view_page( 0 );
    }
-   else{
+   else
+   {
       vg_error( "Steam UGCQuery failed (%d)\n", query->m_eResult );
       workshop_form.view_published_page_count = 0;
       workshop_form.view_published_page_id = 0;
@@ -808,10 +845,12 @@ static void on_workshop_UGCQueryComplete( void *data, void *userdata )
 /*
  * Console command to open the workshop publisher
  */
-static int workshop_submit_command( int argc, const char *argv[] )
+int workshop_submit_command( int argc, const char *argv[] )
 {
-   if( !steam_ready ){
-      ui_start_modal( "Steam API is not initialized\n", UI_MODAL_BAD );
+   if( !steam_ready )
+   {
+      ui_start_modal( &vg_ui.ctx,
+                      "Steam API is not initialized\n", UI_MODAL_BAD );
       return 0;
    }
 
@@ -844,20 +883,21 @@ static int workshop_submit_command( int argc, const char *argv[] )
    return 0;
 }
 
-static void workshop_init(void)
+void workshop_init(void)
 {
    vg_console_reg_cmd( "workshop_submit", workshop_submit_command, NULL );
 }
 
-static void workshop_render_world_preview(void){
-   render_fb_bind( gpipeline.fb_workshop_preview, 0 );
+static void workshop_render_world_preview(void)
+{
+   vg_framebuffer_bind( g_render.fb_workshop_preview, 1.0f );
 
    glClearColor( 0.0f, 0.0f, 0.3f, 1.0f );
    glClear( GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT );
    glEnable( GL_DEPTH_TEST );
    glDisable( GL_BLEND );
 
-   render_world( localplayer.viewable_world, &skaterift.cam, 1 );
+   render_world( world_current_instance(), &g_render.cam, 0, 0, 1, 1 );
 
    glBindFramebuffer( GL_FRAMEBUFFER, 0 );
    glViewport( 0,0, vg.window_x, vg.window_y );
@@ -866,14 +906,15 @@ static void workshop_render_world_preview(void){
 /*
  * Redraw the playermodel into the workshop framebuffer 
  */
-static void workshop_render_player_preview(void){
-   render_fb_bind( gpipeline.fb_workshop_preview, 0 );
+static void workshop_render_player_preview(void)
+{
+   vg_framebuffer_bind( g_render.fb_workshop_preview, 1.0f );
    glClearColor( 0.16f, 0.15f, 0.15f, 1.0f );
    glClear( GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT );
    glEnable( GL_DEPTH_TEST );
    glDisable( GL_BLEND );
 
-   struct skeleton *sk = &localplayer.playeravatar->sk;
+   struct skeleton *sk = &localplayer.skeleton;
 
    player_pose res;
    res.type = k_player_pose_type_ik;
@@ -897,7 +938,7 @@ static void workshop_render_player_preview(void){
    skeleton_apply_inverses( sk, localplayer.final_mtx );
    skeleton_apply_transform( sk, transform, localplayer.final_mtx );
 
-   camera cam;
+   vg_camera cam;
    v3_copy( (v3f){ 0.0f, 201.7f, 1.2f }, cam.pos );
    
    cam.nearz = 0.01f;
@@ -905,14 +946,13 @@ static void workshop_render_player_preview(void){
    cam.fov = 57.0f;
    v3_zero( cam.angles );
    
-   camera_update_transform( &cam );
-   camera_update_view( &cam );
-   camera_update_projection( &cam );
-   camera_finalize( &cam );
+   vg_camera_update_transform( &cam );
+   vg_camera_update_view( &cam );
+   vg_camera_update_projection( &cam );
+   vg_camera_finalize( &cam );
 
-   world_instance *world = localplayer.viewable_world;
-   render_playermodel( &cam, world, 0, &workshop_form.player_model, sk,
-                        localplayer.final_mtx );
+   render_playermodel( &cam, world_current_instance(), 0, 
+                       &workshop_form.player_model, sk, localplayer.final_mtx );
 
    glBindFramebuffer( GL_FRAMEBUFFER, 0 );
    glViewport( 0,0, vg.window_x, vg.window_y );
@@ -921,12 +961,14 @@ static void workshop_render_player_preview(void){
 /*
  * Redraw the model file into the workshop framebuffer
  */
-static void workshop_render_board_preview(void){
-   if( !workshop_form.ptr_ent ){
+static void workshop_render_board_preview(void)
+{
+   if( !workshop_form.ptr_ent )
+   {
       return;
    }
 
-   render_fb_bind( gpipeline.fb_workshop_preview, 0 );
+   vg_framebuffer_bind( g_render.fb_workshop_preview, 1.0f );
 
    glClearColor( 0.0f, 0.0f, 0.3f, 1.0f );
    glClear( GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT );
@@ -947,13 +989,13 @@ static void workshop_render_board_preview(void){
    v3_add( display->transform.co, display1->transform.co, baseco );
    v3_muls( baseco, 0.5f, baseco );
 
-   camera cam;
+   vg_camera cam;
    v3f basevector;
    v3_sub( display->transform.co, ref->transform.co, basevector );
    float dist = v3_length( basevector );
 
    v3f baseangles;
-   player_vector_angles( baseangles, basevector, 1.0f, 0.0f );
+   v3_angles( basevector, baseangles );
 
    v2_add( workshop_form.view_angles, baseangles, cam.angles );
    cam.angles[2] = 0.0f;
@@ -978,10 +1020,10 @@ static void workshop_render_board_preview(void){
    cam.farz  = 100.0f;
    cam.fov   = ref->fov;
    
-   camera_update_transform( &cam );
-   camera_update_view( &cam );
-   camera_update_projection( &cam );
-   camera_finalize( &cam );
+   vg_camera_update_transform( &cam );
+   vg_camera_update_view( &cam );
+   vg_camera_update_projection( &cam );
+   vg_camera_finalize( &cam );
 
    m4x3f mmdl, mmdl1;
    mdl_transform_m4x3( &display->transform, mmdl );
@@ -1001,7 +1043,7 @@ static void workshop_render_board_preview(void){
    glBufferSubData( GL_UNIFORM_BUFFER, 0, 
                     sizeof(struct ub_world_lighting), &world->ub_lighting );
 
-   render_world( world, &cam, 1 );
+   render_world( world, &cam, 0, 0, 0, 0 );
    struct player_board_pose pose = {0};
    render_board( &cam, world, board, mmdl,  &pose, k_board_shader_entity );
    render_board( &cam, world, board, mmdl1, &pose, k_board_shader_entity );
@@ -1015,19 +1057,23 @@ static void workshop_render_board_preview(void){
  * -----------------------------------------------------------------------------
  */
 
-static void workshop_changed_model_path( char *buf, u32 len ){
+static void workshop_changed_model_path( ui_context *ctx, char *buf, u32 len )
+{
    workshop_form.submission.submit_file_and_image = 1;
 }
 
-static void workshop_changed_title( char *buf, u32 len ){
+static void workshop_changed_title( ui_context *ctx, char *buf, u32 len )
+{
    workshop_form.submission.submit_title = 1;
 }
 
-static void workshop_changed_description( char *buf, u32 len ){
+static void workshop_changed_description( ui_context *ctx, char *buf, u32 len )
+{
    workshop_form.submission.submit_description = 1;
 }
 
-static void workshop_form_gui_page_undecided( ui_rect content ){
+static void workshop_form_gui_page_undecided( ui_context *ctx, ui_rect content )
+{
    ui_rect box;
    rect_copy( content, box );
    box[3] = 128;
@@ -1036,9 +1082,10 @@ static void workshop_form_gui_page_undecided( ui_rect content ){
 
    ui_rect row;
    ui_split( box, k_ui_axis_h, 28, 0, row, box );
-   ui_text( row, "Select the type of item\n", 1, k_ui_align_middle_center,0);
+   ui_text( ctx, row, 
+            "Select the type of item\n", 1, k_ui_align_middle_center,0);
    ui_split( box, k_ui_axis_h, 28, 0, row, box );
-   ui_enum( row, "Type:", workshop_form_type_opts,
+   ui_enum( ctx, row, "Type:", workshop_form_type_opts,
                4, &workshop_form.submission.submission_type_selection );
    ui_split( box, k_ui_axis_h, 8, 0, row, box );
    ui_split( box, k_ui_axis_h, 28, 0, row, box );
@@ -1050,8 +1097,10 @@ static void workshop_form_gui_page_undecided( ui_rect content ){
    ui_split_ratio( button_l, k_ui_axis_v, 0.5f, 2, button_l, button_r );
 
    enum addon_type type = workshop_form.submission.submission_type_selection;
-   if( type != k_addon_type_none){
-      if( ui_button_text( button_l, "OK", 1 ) == 1 ){
+   if( type != k_addon_type_none)
+   {
+      if( ui_button_text( ctx, button_l, "OK", 1 ) == 1 )
+      {
          workshop_form.submission.type = type;
 
          if( type == k_addon_type_world ){
@@ -1060,45 +1109,56 @@ static void workshop_form_gui_page_undecided( ui_rect content ){
          }
       }
    }
-   else{
-      ui_fill( button_l, ui_colour(k_ui_bg) );
-      ui_text( button_l, "OK", 1, k_ui_align_middle_center, 
-               ui_colour(k_ui_bg+4) );
+   else
+   {
+      ui_fill( ctx, button_l, ui_colour(ctx,k_ui_bg) );
+      ui_text( ctx, button_l, "OK", 1, k_ui_align_middle_center, 
+               ui_colour(ctx, k_ui_bg+4) );
    }
    
-   if( ui_button_text( button_r, "Cancel", 1 ) == 1 ){
+   if( ui_button_text( ctx, button_r, "Cancel", 1 ) == 1 )
+   {
       workshop_form.page = k_workshop_form_open;
       workshop_form.file_intent = k_workshop_form_file_intent_none;
    }
 }
 
-static void workshop_form_gui_draw_preview( ui_rect img_box ){
+static void workshop_form_gui_draw_preview( ui_context *ctx, ui_rect img_box )
+{
    enum addon_type type = workshop_form.submission.type;
-   if( workshop_form.file_intent == k_workshop_form_file_intent_keep_old ){
-      ui_image( img_box, gpipeline.fb_workshop_preview->attachments[0].id );
+   if( workshop_form.file_intent == k_workshop_form_file_intent_keep_old )
+   {
+      ui_image( ctx, 
+                img_box, &g_render.fb_workshop_preview->attachments[0].id );
    }
-   else if( workshop_form.file_intent == k_workshop_form_file_intent_new ){
-      ui_image( img_box, gpipeline.fb_workshop_preview->attachments[0].id );
+   else if( workshop_form.file_intent == k_workshop_form_file_intent_new )
+   {
+      ui_image( ctx, 
+                img_box, &g_render.fb_workshop_preview->attachments[0].id );
 
-      if( type == k_addon_type_world ){
+      if( type == k_addon_type_world )
+      {
          return;
       }
 
-      int hover  = ui_inside_rect( img_box, vg_ui.mouse ),
-          target = ui_inside_rect( img_box, vg_ui.mouse_click );
+      int hover  = ui_inside_rect( img_box, ctx->mouse ),
+          target = ui_inside_rect( img_box, ctx->mouse_click );
       
-      if( ui_click_down(UI_MOUSE_MIDDLE) && target ){
+      if( ui_click_down(ctx,UI_MOUSE_MIDDLE) && target )
+      {
          v3_copy( workshop_form.view_offset,
                   workshop_form.view_offset_begin );
       }
-      else if( ui_click_down(UI_MOUSE_LEFT) && target ){
+      else if( ui_click_down(ctx,UI_MOUSE_LEFT) && target )
+      {
          v2_copy( workshop_form.view_angles, 
                   workshop_form.view_angles_begin );
       }
 
-      if( ui_clicking(UI_MOUSE_MIDDLE) && target ){
-         v2f delta = { vg_ui.mouse[0]-vg_ui.mouse_click[0],
-                       vg_ui.mouse[1]-vg_ui.mouse_click[1] };
+      if( ui_clicking(ctx,UI_MOUSE_MIDDLE) && target )
+      {
+         v2f delta = { ctx->mouse[0]-ctx->mouse_click[0],
+                       ctx->mouse[1]-ctx->mouse_click[1] };
 
          float *begin  = workshop_form.view_offset_begin,
                *offset = workshop_form.view_offset;
@@ -1106,9 +1166,10 @@ static void workshop_form_gui_draw_preview( ui_rect img_box ){
          offset[2] = vg_clampf( begin[2]-delta[1]*0.002f, -1.0f, 1.0f );
          workshop_form.view_changed = 1;
       }
-      else if( ui_clicking(UI_MOUSE_LEFT) && target ){
-         v2f delta = { vg_ui.mouse[0]-vg_ui.mouse_click[0],
-                       vg_ui.mouse[1]-vg_ui.mouse_click[1] };
+      else if( ui_clicking(ctx,UI_MOUSE_LEFT) && target )
+      {
+         v2f delta = { ctx->mouse[0]-ctx->mouse_click[0],
+                       ctx->mouse[1]-ctx->mouse_click[1] };
 
          v2f angles;
          v2_muladds( workshop_form.view_angles_begin, delta, 0.002f, angles);
@@ -1122,38 +1183,43 @@ static void workshop_form_gui_draw_preview( ui_rect img_box ){
          workshop_form.view_changed = 1;
       }
 
-      if( !ui_clicking(UI_MOUSE_LEFT) && hover ){
+      if( !ui_clicking(ctx,UI_MOUSE_LEFT) && hover )
+      {
          float zoom = workshop_form.view_dist;
          zoom += vg.mouse_wheel[1] * -0.07f;
          zoom = vg_clampf( zoom, 0.4f, 2.0f );
          
-         if( zoom != workshop_form.view_dist ){
+         if( zoom != workshop_form.view_dist )
+         {
             workshop_form.view_changed = 1;
             workshop_form.view_dist = zoom;
          }
       }
    }
-   else{
-      ui_text( img_box, "No image", 1, k_ui_align_middle_center,
-               ui_colour( k_ui_orange ) );
+   else
+   {
+      ui_text( ctx, img_box, "No image", 1, k_ui_align_middle_center,
+               ui_colour( ctx, k_ui_orange ) );
    }
 }
 
-static void workshop_form_gui_edit_page( ui_rect content ){
+static void workshop_form_gui_edit_page( ui_context *ctx, ui_rect content )
+{
    enum addon_type type = workshop_form.submission.type;
 
-   if( type == k_addon_type_none ){
-      workshop_form_gui_page_undecided( content );
+   if( type == k_addon_type_none )
+   {
+      workshop_form_gui_page_undecided( ctx, content );
       return;
    }
 
    ui_rect image_plane;
    ui_split( content, k_ui_axis_h, 300, 0, image_plane, content );
-   ui_fill( image_plane, ui_colour( k_ui_bg+0 ) );
+   ui_fill( ctx, image_plane, ui_colour( ctx, k_ui_bg+0 ) );
 
    ui_rect img_box;
    ui_fit_item( image_plane, (ui_px[2]){ 3, 2 }, img_box );
-   workshop_form_gui_draw_preview( img_box );
+   workshop_form_gui_draw_preview( ctx, img_box );
 
    /* file path */
    ui_rect file_button, file_label;
@@ -1162,23 +1228,28 @@ static void workshop_form_gui_edit_page( ui_rect content ){
    snprintf( buf, 128, 
              "Addon folder: skaterift/%s", workshop_filetype_folder() );
 
-   if( type == k_addon_type_world ){
-      struct ui_textbox_callbacks callbacks = {
+   if( type == k_addon_type_world )
+   {
+      struct ui_textbox_callbacks callbacks = 
+      {
          .change = workshop_changed_model_path
       };
-      ui_textbox( content, buf, workshop_form.addon_folder,
+      ui_textbox( ctx, content, buf, workshop_form.addon_folder,
                   vg_list_size(workshop_form.addon_folder), 1, 0, &callbacks );
    }
-   else{
+   else
+   {
       ui_rect file_entry;
-      ui_standard_widget( content, file_entry, 1 );
+      ui_standard_widget( ctx, content, file_entry, 1 );
       ui_split( file_entry, k_ui_axis_v, -128, 0, file_entry, file_button );
 
-      if( workshop_form.file_intent != k_workshop_form_file_intent_none ){
-         ui_text( file_entry, workshop_form.addon_folder, 1, 
-                  k_ui_align_middle_left, ui_colour( k_ui_fg+4 ) );
+      if( workshop_form.file_intent != k_workshop_form_file_intent_none )
+      {
+         ui_text( ctx, file_entry, workshop_form.addon_folder, 1, 
+                  k_ui_align_middle_left, ui_colour( ctx, k_ui_fg+4 ) );
 
-         if( ui_button_text( file_button, "Remove", 1 ) == 1 ){
+         if( ui_button_text( ctx, file_button, "Remove", 1 ) == 1 )
+         {
             if( type == k_addon_type_board )
                player_board_unload( &workshop_form.board_model );
             else if( type == k_addon_type_player )
@@ -1188,17 +1259,20 @@ static void workshop_form_gui_edit_page( ui_rect content ){
             workshop_form.addon_folder[0] = '\0';
          }
       }
-      else{
-         struct ui_textbox_callbacks callbacks = {
+      else
+      {
+         struct ui_textbox_callbacks callbacks = 
+         {
             .change = workshop_changed_model_path
          };
 
-         ui_textbox( file_entry, buf, workshop_form.addon_folder,
+         ui_textbox( ctx, file_entry, buf, workshop_form.addon_folder,
                      vg_list_size(workshop_form.addon_folder), 1,
                      0, &callbacks );
 
-         if( ui_button_text( file_button, "Load", 1 ) == 1 ){
-            workshop_op_load_model();
+         if( ui_button_text( ctx, file_button, "Load", 1 ) == 1 )
+         {
+            workshop_op_load_model( ctx );
          }
       }
    }
@@ -1210,23 +1284,24 @@ static void workshop_form_gui_edit_page( ui_rect content ){
       struct ui_textbox_callbacks callbacks = {
          .change = workshop_changed_title
       };
-      ui_textbox( content, str_title, workshop_form.submission.title, 
+      ui_textbox( ctx, content, str_title, workshop_form.submission.title, 
                   vg_list_size(workshop_form.submission.title), 1,
                   0, &callbacks );
    }
 
    /* visibility option */
    {
-      ui_enum( content, "Visibility:", workshop_form_visibility_opts,
+      ui_enum( ctx, content, "Visibility:", workshop_form_visibility_opts,
                4, &workshop_form.submission.visibility );
    }
 
    /* description box */
    {
-      struct ui_textbox_callbacks callbacks = {
+      struct ui_textbox_callbacks callbacks = 
+      {
          .change = workshop_changed_description
       };
-      ui_textbox( content, str_desc, workshop_form.submission.description,
+      ui_textbox( ctx, content, str_desc, workshop_form.submission.description,
                   vg_list_size(workshop_form.submission.description), 4,
                   UI_TEXTBOX_MULTILINE|UI_TEXTBOX_WRAP, &callbacks );
    }
@@ -1244,10 +1319,12 @@ static void workshop_form_gui_edit_page( ui_rect content ){
    ui_split_ratio( submission_center, k_ui_axis_v, 0.5f, 8, 
                    btn_left, btn_right );
 
-   if( ui_button_text( btn_left, "Publish", 1 ) == 1 ){
-      workshop_op_submit();
+   if( ui_button_text( ctx, btn_left, "Publish", 1 ) == 1 )
+   {
+      workshop_op_submit( ctx );
    }
-   if( ui_button_text( btn_right, "Cancel", 1 ) == 1 ){
+   if( ui_button_text( ctx, btn_right, "Cancel", 1 ) == 1 )
+   {
       workshop_form.page = k_workshop_form_open;
       player_board_unload( &workshop_form.board_model );
       workshop_form.file_intent = k_workshop_form_file_intent_none;
@@ -1263,36 +1340,39 @@ static void workshop_form_gui_edit_page( ui_rect content ){
    ui_px btn_width = 32;
 
    rect_copy( disclaimer_row, inner );
-   inner[2] = ui_text_line_width( disclaimer_text ) + btn_width+8;
+   inner[2] = ui_text_line_width( ctx, disclaimer_text ) + btn_width+8;
 
    ui_rect label;
    ui_rect_center( disclaimer_row, inner );
    ui_split( inner, k_ui_axis_v, inner[2]-btn_width, 0, label, btn_right);
    ui_rect_pad( btn_right, (ui_px[2]){2,2} );
 
-   if( ui_button_text( btn_right, "\xbf", 2 ) == 1 ){
+   if( ui_button_text( ctx, btn_right, "\xb2", 2 ) == 1 )
+   {
       ISteamFriends *hSteamFriends = SteamAPI_SteamFriends();
       SteamAPI_ISteamFriends_ActivateGameOverlayToWebPage( hSteamFriends,
             "https://steamcommunity.com/sharedfiles/workshoplegalagreement",
             k_EActivateGameOverlayToWebPageMode_Default );
    }
 
-   ui_text( label, disclaimer_text, 1, k_ui_align_middle_left,
-            ui_colour( k_ui_fg+4 ) );
+   ui_text( ctx, label, disclaimer_text, 1, k_ui_align_middle_left,
+            ui_colour( ctx, k_ui_fg+4 ) );
 }
 
-static void workshop_form_gui_sidebar( ui_rect sidebar )
+static void workshop_form_gui_sidebar( ui_context *ctx, ui_rect sidebar )
 {
-   ui_fill( sidebar, ui_colour( k_ui_bg+2 ) );
+   ui_fill( ctx, sidebar, ui_colour( ctx, k_ui_bg+2 ) );
 
    ui_rect title;
    ui_split( sidebar, k_ui_axis_h, 28, 0, title, sidebar );
 
-   if( workshop_form.page == k_workshop_form_edit ){
-      ui_text( title, "Editing", 1, k_ui_align_middle_center, 0 );
+   if( workshop_form.page == k_workshop_form_edit )
+   {
+      ui_text( ctx, title, "Editing", 1, k_ui_align_middle_center, 0 );
       ui_split( sidebar, k_ui_axis_h, 28, 0, title, sidebar );
 
-      if( workshop_form.submission.type != k_addon_type_none ){
+      if( workshop_form.submission.type != k_addon_type_none )
+      {
          char buf[512];
          vg_str str;
          vg_strnull( &str, buf, 512 );
@@ -1311,8 +1391,8 @@ static void workshop_form_gui_sidebar( ui_rect sidebar )
          else
             vg_strcat( &str, "???." );
 
-         ui_text( title, buf, 1, k_ui_align_middle_center, 
-                  ui_colour(k_ui_fg+4) );
+         ui_text( ctx, title, buf, 1, k_ui_align_middle_center, 
+                  ui_colour(ctx, k_ui_fg+4) );
       }
       return;
    }
@@ -1320,38 +1400,41 @@ static void workshop_form_gui_sidebar( ui_rect sidebar )
    /* 
     * sidebar existing entries panel
     */
-   ui_text( title, "Your submissions", 1, k_ui_align_middle_center, 0 );
+   ui_text( ctx, title, "Your submissions", 1, k_ui_align_middle_center, 0 );
 
    ui_rect controls, btn_create_new;
    ui_split( sidebar, k_ui_axis_h,  32, 0, controls, sidebar );
    ui_split( sidebar, k_ui_axis_h, -32, 0, sidebar, btn_create_new );
-   ui_fill( controls, ui_colour( k_ui_bg+1 ) );
+   ui_fill( ctx, controls, ui_colour( ctx, k_ui_bg+1 ) );
 
    char buf[32];
-   strcpy( buf, "page " );
-   int i  = 5;
-       i += highscore_intl( buf+i, workshop_form.view_published_page_id+1, 4 );
-            buf[ i ++ ] = '/';
-       i += highscore_intl( buf+i, workshop_form.view_published_page_count, 4 );
-            buf[ i ++ ] = '\0';
+   vg_str str;
+   vg_strnull( &str, buf, sizeof(buf) );
+   vg_strcat( &str, "page " );
+   vg_strcati32( &str,  workshop_form.view_published_page_id+1 );
+   vg_strcatch( &str, '/' );
+   vg_strcati32( &str, workshop_form.view_published_page_count );
 
    ui_rect_pad( controls, (ui_px[2]){0,4} );
    ui_rect info;
    ui_split_ratio( controls, k_ui_axis_v, 0.25f, 0, info, controls );
-   ui_text( info, buf, 1, k_ui_align_middle_center, 0 );
+   ui_text( ctx, info, buf, 1, k_ui_align_middle_center, 0 );
 
    ui_rect btn_left, btn_right;
    ui_split_ratio( controls, k_ui_axis_v, 0.5f, 2, btn_left, btn_right );
             
-   if( ui_button_text( btn_left, "newer", 1 ) == 1 ){
+   if( ui_button_text( ctx, btn_left, "newer", 1 ) == 1 )
+   {
       workshop_view_page( workshop_form.view_published_page_id-1 );
    }
 
-   if( ui_button_text( btn_right, "older", 1 ) == 1 ){
+   if( ui_button_text( ctx, btn_right, "older", 1 ) == 1 )
+   {
       workshop_view_page( workshop_form.view_published_page_count+1 );
    }
 
-   if( ui_button_text( btn_create_new, "Create New Item", 1 ) == 1 ){
+   if( ui_button_text( ctx, btn_create_new, "Create New Item", 1 ) == 1 )
+   {
       workshop_reset_submission_data();
       workshop_form.submission.submit_title = 1;
       workshop_form.submission.submit_description = 1;
@@ -1359,25 +1442,29 @@ static void workshop_form_gui_sidebar( ui_rect sidebar )
       workshop_form.page = k_workshop_form_edit;
    }
 
-   for( int i=0; i<workshop_form.published_files_list_length; i++ ){
+   for( int i=0; i<workshop_form.published_files_list_length; i++ )
+   {
       ui_rect item;
       ui_split( sidebar, k_ui_axis_h, 28, 0, item, sidebar );
       ui_rect_pad( item, (ui_px[2]){4,4} );
       
       struct published_file *pfile = &workshop_form.published_files_list[i];
-      if( ui_button_text( item, pfile->title, 1 ) == 1 ){
-         if( pfile->result == k_EResultOK ){
+      if( ui_button_text( ctx, item, pfile->title, 1 ) == 1 )
+      {
+         if( pfile->result == k_EResultOK )
+         {
             vg_info( "Select index: %d\n", pfile->result_index );
             workshop_op_download_and_view_submission( pfile->result_index );
          }
-         else{
+         else
+         {
             vg_warn( "Cannot select that item, result not OK\n" );
          }
       }
    }
 }
 
-static void workshop_form_gui(void)
+void workshop_form_gui( ui_context *ctx )
 {
    enum workshop_form_page stable_page = workshop_form.page;
    if( stable_page == k_workshop_form_hidden ) return;
@@ -1386,22 +1473,24 @@ static void workshop_form_gui(void)
    ui_rect screen = { 0, 0, vg.window_x, vg.window_y };
    ui_rect window = { 0, 0, 1000, 700 };
    ui_rect_center( screen, window );
-   vg_ui.wants_mouse = 1;
+   ctx->wants_mouse = 1;
 
-   ui_fill( window, ui_colour( k_ui_bg+1 ) );
-   ui_outline( window, 1, ui_colour( k_ui_bg+7 ), 0 );
+   ui_fill( ctx, window, ui_colour( ctx, k_ui_bg+1 ) );
+   ui_outline( ctx, window, 1, ui_colour( ctx, k_ui_bg+7 ), 0 );
 
    ui_rect title, panel;
    ui_split( window, k_ui_axis_h, 28, 0, title, panel );
-   ui_fill( title, ui_colour( k_ui_bg+7 ) );
-   ui_text( title, "Workshop tool", 1, k_ui_align_middle_center, 
-            ui_colourcont(k_ui_bg+7) );
+   ui_fill( ctx, title, ui_colour( ctx, k_ui_bg+7 ) );
+   ui_text( ctx, title, "Workshop tool", 1, k_ui_align_middle_center, 
+            ui_colourcont( ctx, k_ui_bg+7 ) );
 
    ui_rect quit_button;
    ui_split( title, k_ui_axis_v, title[2]-title[3], 2, title, quit_button );
 
-   if( vg_loader_availible() ){
-      if( ui_button_text( quit_button, "X", 1 ) == 1 ){
+   if( vg_loader_availible() )
+   {
+      if( ui_button_text( ctx, quit_button, "X", 1 ) == 1 )
+      {
          workshop_quit_form();
          return;
       }
@@ -1413,11 +1502,13 @@ static void workshop_form_gui(void)
     * escapes here and we show them a basic string
     */
 
-   if( workshop_form.op != k_workshop_op_none ){
+   if( workshop_form.op != k_workshop_op_none )
+   {
       const char *op_string = "The programmer has not bothered to describe "
                               "the current operation that is running.";
 
-      switch( workshop_form.op ){
+      switch( workshop_form.op )
+      {
          case k_workshop_op_loading_model:
             op_string = "Operation in progress: Loading model file.";
          break;
@@ -1432,7 +1523,7 @@ static void workshop_form_gui(void)
          default: break;
       }
 
-      ui_text( panel, op_string, 1, k_ui_align_middle_center, 0 );
+      ui_text( ctx, panel, op_string, 1, k_ui_align_middle_center, 0 );
       return;
    }
 
@@ -1464,14 +1555,17 @@ static void workshop_form_gui(void)
    /* content page */
    ui_rect_pad( content, (ui_px[2]){8,8} );
 
-   if( stable_page == k_workshop_form_edit ){
-      workshop_form_gui_edit_page( content );
+   if( stable_page == k_workshop_form_edit )
+   {
+      workshop_form_gui_edit_page( ctx, content );
    }
-   else if( stable_page == k_workshop_form_open ){
-      ui_text( content, "Nothing selected.", 1, k_ui_align_middle_center,
-               ui_colour( k_ui_fg+4 ) );
+   else if( stable_page == k_workshop_form_open )
+   {
+      ui_text( ctx, content, "Nothing selected.", 1, k_ui_align_middle_center,
+               ui_colour( ctx, k_ui_fg+4 ) );
    }
-   else if( stable_page >= k_workshop_form_cclosing ){
+   else if( stable_page >= k_workshop_form_cclosing )
+   {
       ui_rect submission_row;
       ui_split( content, k_ui_axis_h, content[3]-32-8, 0, content, 
                    submission_row );
@@ -1479,11 +1573,11 @@ static void workshop_form_gui(void)
       u32 colour;
 
       if( stable_page == k_workshop_form_closing_bad )
-         colour = ui_colour( k_ui_red+k_ui_brighter );
+         colour = ui_colour( ctx, k_ui_red+k_ui_brighter );
       else
-         colour = ui_colour( k_ui_green+k_ui_brighter );
+         colour = ui_colour( ctx, k_ui_green+k_ui_brighter );
 
-      ui_text( content, workshop_form.failure_or_success_string, 1, 
+      ui_text( ctx, content, workshop_form.failure_or_success_string, 1, 
                k_ui_align_middle_center, colour );
 
       ui_rect submission_center;
@@ -1492,12 +1586,13 @@ static void workshop_form_gui(void)
       ui_rect_center( submission_row, submission_center );
       ui_rect_pad( submission_center, (ui_px[2]){8,8} );
 
-      if( ui_button_text( submission_center, "OK", 1 ) == 1 ){
+      if( ui_button_text( ctx, submission_center, "OK", 1 ) == 1 )
+      {
          workshop_form.page = k_workshop_form_open;
       }
    }
 
-   workshop_form_gui_sidebar( sidebar );
+   workshop_form_gui_sidebar( ctx, sidebar );
 }
 
 /*
@@ -1505,7 +1600,7 @@ static void workshop_form_gui(void)
  * -----------------------------------------------------------------------------
  */
 
-static void async_workshop_get_filepath( void *data, u32 len )
+void async_workshop_get_filepath( void *data, u32 len )
 {
    struct async_workshop_filepath_info *info = data;
 
@@ -1521,7 +1616,7 @@ static void async_workshop_get_filepath( void *data, u32 len )
    }
 }
 
-static void async_workshop_get_installed_files( void *data, u32 len )
+void async_workshop_get_installed_files( void *data, u32 len )
 {
    struct async_workshop_installed_files_info *info = data;
 
@@ -1541,5 +1636,3 @@ static void async_workshop_get_installed_files( void *data, u32 len )
 
    *info->len = j;
 }
-
-#endif /* WORKSHOP_C */