X-Git-Url: https://harrygodden.com/git/?a=blobdiff_plain;f=ent_skateshop.c;h=2957c8f4be07e8240af807a42feb931091a49b1d;hb=b1d22bbb87583cea9fde7620eb1fc16189be5113;hp=b3e648f45aff2a7676a667c792d6d0a3b4543c26;hpb=badfa88dd109bbae5628f58504402f4707569f73;p=carveJwlIkooP6JGAAIwe30JlM.git diff --git a/ent_skateshop.c b/ent_skateshop.c index b3e648f..2957c8f 100644 --- a/ent_skateshop.c +++ b/ent_skateshop.c @@ -14,6 +14,7 @@ #include "highscores.h" #include "steam.h" #include "addon.h" +#include "save.h" /* * Checks string equality but does a hash check first @@ -42,6 +43,7 @@ VG_STATIC struct cache_board *skateshop_cache_fetch_board( u32 registry_index ) } } +#if 0 /* lru eviction. should be a linked list maybe... */ double min_time = 1e300; struct cache_board *min_board = NULL; @@ -58,6 +60,12 @@ VG_STATIC struct cache_board *skateshop_cache_fetch_board( u32 registry_index ) min_board = cache_ptr; } } +#else + + SDL_AtomicLock( &global_skateshop.sl_cache_access ); + struct cache_board *min_board = lru_volatile_cache_board(); + +#endif if( min_board ){ if( min_board->state == k_cache_board_state_loaded ){ @@ -75,7 +83,6 @@ VG_STATIC struct cache_board *skateshop_cache_fetch_board( u32 registry_index ) min_board->reg_ptr = reg; min_board->reg_index = registry_index; - min_board->last_use_time = vg.time; min_board->ref_count = 0; min_board->state = k_cache_board_state_load_request; } @@ -114,7 +121,6 @@ VG_STATIC void skateshop_async_board_loaded( void *payload, u32 size ) { SDL_AtomicLock( &global_skateshop.sl_cache_access ); struct cache_board *cache_ptr = payload; - cache_ptr->last_use_time = vg.time; cache_ptr->state = k_cache_board_state_loaded; cache_ptr->reg_ptr->userdata = cache_ptr; @@ -130,7 +136,6 @@ VG_STATIC void workshop_visibile_load_loop(void) { vg_info( "Running load loop\n" ); char path_buf[4096]; - vg_str folder; for( u32 i=0; ireg_index ); + cache_ptr->reg_ptr = reg; - cache_ptr->reg_ptr = get_addon_from_index( k_workshop_file_type_board, - cache_ptr->reg_index ); - - if( cache_ptr->reg_ptr->workshop_id ){ - vg_async_item *call = - vg_async_alloc( sizeof(struct async_workshop_filepath_info) ); - - struct async_workshop_filepath_info *info = call->payload; - info->buf = path_buf; - info->id = cache_ptr->reg_ptr->workshop_id; - info->len = vg_list_size(path_buf); - vg_async_dispatch( call, async_workshop_get_filepath ); - vg_async_stall(); /* too bad! */ - - if( path_buf[0] == '\0' ){ - vg_error( "Failed SteamAPI_GetItemInstallInfo(" PRINTF_U64 ")\n", - cache_ptr->reg_ptr->workshop_id ); - goto file_is_broken; - } + vg_str folder; + vg_strnull( &folder, path_buf, 4096 ); + if( !addon_get_content_folder( reg, &folder ) ) + goto file_is_broken; - folder.buffer = path_buf; - folder.i = strlen(path_buf); - folder.len = 4096; - } - else{ - vg_strnull( &folder, path_buf, 4096 ); - vg_strcat( &folder, "boards/" ); - vg_strcat( &folder, cache_ptr->reg_ptr->foldername ); - } /* load content files * --------------------------------- */ vg_str content_path = folder; - int found = 0; - vg_msg msg; - vg_msg_init( &msg, cache_ptr->reg_ptr->metadata, - cache_ptr->reg_ptr->metadata_len ); - vg_msg_cmd cmd; - while( vg_msg_next( &msg, &cmd ) ){ - if( (msg.depth == 0) && (cmd.code == k_vg_msg_code_kvstring) ){ - if( VG_STRDJB2_EQ( "content", cmd.key, cmd.key_djb2 ) ){ - vg_strcat( &content_path, "/" ); - vg_strcat( &content_path, cmd.value._buf ); - found = 1; - break; - } - } - } - if( !vg_strgood( &content_path ) ) { - vg_error( "Metadata path too long\n" ); + vg_msg root = {0}; + root.buf = reg->metadata; + root.len = reg->metadata_len; + root.max = sizeof(reg->metadata); + + const char *kv_content = vg_msg_seekkvstr( &root, "content", 0 ); + if( kv_content ){ + vg_strcat( &content_path, "/" ); + vg_strcat( &content_path, kv_content ); + } + else{ + vg_error( "No content paths in metadata\n" ); goto file_is_broken; } - if( !found ){ - vg_error( "No content paths in metadata\n" ); + if( !vg_strgood( &content_path ) ) { + vg_error( "Metadata path too long\n" ); goto file_is_broken; } @@ -225,7 +204,7 @@ file_is_broken:; VG_STATIC void world_scan_thread( void *_args ){ - addon_mount_local_folder( k_workshop_file_type_world, "maps", ".mdl" ); + addon_mount_content_folder( k_workshop_file_type_world, "maps", ".mdl" ); addon_mount_workshop_items(); vg_async_call( async_addon_reg_update, NULL, 0 ); skaterift_end_op(); @@ -239,13 +218,17 @@ VG_STATIC void skateshop_op_world_scan(void){ vg_loader_start( world_scan_thread, NULL ); } +VG_STATIC void board_processview_thread( void *_args ){ + workshop_visibile_load_loop(); + skaterift_end_op(); +} + VG_STATIC void board_scan_thread( void *_args ){ - addon_mount_local_folder( k_workshop_file_type_board, "boards", ".mdl" ); + addon_mount_content_folder( k_workshop_file_type_board, "boards", ".mdl" ); addon_mount_workshop_items(); vg_async_call( async_addon_reg_update, NULL, 0 ); vg_async_stall(); - workshop_visibile_load_loop(); - skaterift_end_op(); + board_processview_thread(NULL); } VG_STATIC void skateshop_op_board_scan(void){ @@ -253,32 +236,59 @@ VG_STATIC void skateshop_op_board_scan(void){ vg_loader_start( board_scan_thread, NULL ); } +VG_STATIC void skateshop_op_processview(void){ + skaterift_begin_op( k_async_op_board_scan ); + vg_loader_start( board_processview_thread, NULL ); +} + /* * Regular stuff * ----------------------------------------------------------------------------- */ -/* we can only keep using a viewslot pointer for multiple frames if we watch it - * using this function */ +/* adds one more watch */ VG_STATIC void watch_cache_board( struct cache_board *ptr ){ if( ptr->ref_count >= 32 ){ vg_fatal_error( "dynamic board watch missmatch (limit is 32)\n" ); } - ptr->last_use_time = vg.time; ptr->ref_count ++; } -/* after this is called, the calling code only has access to the pointer for the - * duration of the rest of the frame */ +/* if after this no more watches, places back into the volatile list */ VG_STATIC void unwatch_cache_board( struct cache_board *ptr ){ if( ptr->ref_count == 0 ){ vg_fatal_error( "dynamic board unwatch missmatch (no watchers)\n" ); } ptr->ref_count --; + if( !ptr->ref_count ){ + struct cache_board *head = global_skateshop.cache_head, + *tail = global_skateshop.cache_tail; + + if( tail ) tail->right = ptr; + ptr->left = tail; + global_skateshop.cache_tail = ptr; + + if( !head ) global_skateshop.cache_head = ptr; + } } +/* retrieve oldest pointer from the volatile list (and remove it) */ +VG_STATIC struct cache_board *lru_volatile_cache_board(void){ + struct cache_board *head = global_skateshop.cache_head, + *tail = global_skateshop.cache_tail; + + if( head ){ + if( head == tail ) global_skateshop.cache_tail = NULL; + global_skateshop.cache_head = head->right; + + head->left = NULL; + head->right = NULL; + return head; + } + else return NULL; +} /* * VG event init @@ -288,14 +298,24 @@ VG_STATIC void skateshop_init(void){ global_skateshop.cache = vg_linear_alloc( vg_mem.rtmemory, cache_size ); memset( global_skateshop.cache, 0, cache_size ); - for( u32 i=0; istate = k_cache_board_state_none; - board->reg_ptr= NULL; - board->reg_index = 0xffffffff; - board->last_use_time = -99999.9; - board->ref_count = 0; + for( i32 ib=0; ib=0? &arr[ia]: NULL, + *pc = icleft = pa; + pb->right = pc; + + pb->state = k_cache_board_state_none; + pb->reg_ptr= NULL; + pb->reg_index = 0xffffffff; + pb->ref_count = 0; } + + global_skateshop.cache_head = global_skateshop.cache; + global_skateshop.cache_tail = + &global_skateshop.cache[SKATESHOP_BOARD_CACHE_MAX-1]; } VG_STATIC struct cache_board *skateshop_selected_cache_if_loaded(void) @@ -321,65 +341,61 @@ VG_STATIC struct cache_board *skateshop_selected_cache_if_loaded(void) VG_STATIC void pointcloud_async_end(void *_, u32 __) { pointcloud_animate( k_pointcloud_anim_opening ); - skaterift_end_op(); } VG_STATIC void pointcloud_clear_async(void *_, u32 __) { pointcloud.count = 0; pointcloud_animate( k_pointcloud_anim_opening ); - skaterift_end_op(); } VG_STATIC void skateshop_preview_loader_thread( void *_data ) { addon_reg *reg = _data; + + char path_buf[4096]; + vg_str path; + vg_strnull( &path, path_buf, 4096 ); + addon_get_content_folder( reg, &path ); + vg_strcat( &path, "/preview.bin" ); + + vg_linear_clear(vg_mem.scratch); + u32 size; - if( reg->workshop_id ){ - vg_error( "Workshop files unsupported\n" ); - vg_async_call( pointcloud_clear_async, NULL, 0 ); - } - else{ - char path_buf[4096]; - vg_str path; - vg_strnull( &path, path_buf, 4096 ); - vg_strcat( &path, "maps/" ); - vg_strcat( &path, reg->foldername ); - vg_strcat( &path, "/preview.bin" ); - - vg_linear_clear(vg_mem.scratch); - u32 size; - - void *data = vg_file_read( vg_mem.scratch, path_buf, &size ); - if( data ){ - if( size < sizeof(pointcloud_buffer) ){ - vg_async_call( pointcloud_clear_async, NULL, 0 ); - return; - } - - vg_async_item *call = vg_async_alloc(size); - pointcloud_buffer *pcbuf = call->payload; - memcpy( pcbuf, data, size ); - - u32 point_count = (size-sizeof(pointcloud_buffer)) / - sizeof(struct pointcloud_vert); - pcbuf->max = point_count; - pcbuf->count = point_count; - pcbuf->op = k_pointcloud_op_clear; - - vg_async_dispatch( call, async_pointcloud_sub ); - vg_async_call( pointcloud_async_end, NULL, 0 ); - } - else{ + void *data = vg_file_read( vg_mem.scratch, path_buf, &size ); + if( data ){ + if( size < sizeof(pointcloud_buffer) ){ vg_async_call( pointcloud_clear_async, NULL, 0 ); + return; } + + vg_async_item *call = vg_async_alloc(size); + pointcloud_buffer *pcbuf = call->payload; + memcpy( pcbuf, data, size ); + + u32 point_count = (size-sizeof(pointcloud_buffer)) / + sizeof(struct pointcloud_vert); + pcbuf->max = point_count; + pcbuf->count = point_count; + pcbuf->op = k_pointcloud_op_clear; + + vg_async_dispatch( call, async_pointcloud_sub ); + vg_async_call( pointcloud_async_end, NULL, 0 ); } + else{ + vg_async_call( pointcloud_clear_async, NULL, 0 ); + } +} + +VG_STATIC void skateshop_preview_loader_thread_and_end( void *_data ){ + skateshop_preview_loader_thread( _data ); + skaterift_end_op(); } VG_STATIC void skateshop_load_world_preview( addon_reg *reg ) { skaterift_begin_op( k_async_op_world_load_preview ); - vg_loader_start( skateshop_preview_loader_thread, reg ); + vg_loader_start( skateshop_preview_loader_thread_and_end, reg ); } /* @@ -445,6 +461,8 @@ VG_STATIC void global_skateshop_preupdate(void) * ---------------------- */ + u32 opage = global_skateshop.selected_board_id/SKATESHOP_VIEW_SLOT_MAX; + if( button_down( k_srbind_mleft ) ){ if( global_skateshop.selected_board_id > 0 ){ global_skateshop.selected_board_id --; @@ -459,7 +477,13 @@ VG_STATIC void global_skateshop_preupdate(void) } } - if( selected_cache && button_down( k_srbind_maccept ) ){ + u32 npage = global_skateshop.selected_board_id/SKATESHOP_VIEW_SLOT_MAX; + + if( opage != npage ){ + skateshop_update_viewpage(); + skateshop_op_processview(); + } + else if( selected_cache && button_down( k_srbind_maccept ) ){ vg_info( "chose board from skateshop (%u)\n", global_skateshop.selected_board_id ); @@ -470,6 +494,7 @@ VG_STATIC void global_skateshop_preupdate(void) localplayer.board_view_slot = selected_cache; watch_cache_board( localplayer.board_view_slot ); global_skateshop_exit(); + skaterift_write_savedata(); return; } } @@ -479,21 +504,21 @@ VG_STATIC void global_skateshop_preupdate(void) gui_helper_action( button_display_string( k_srbind_maccept ), "pick" ); if( button_down( k_srbind_mleft ) ){ - if( cl_playermdl_id > 0 ){ - cl_playermdl_id --; + if( k_playermdl_id > 0 ){ + k_playermdl_id --; } else{ - cl_playermdl_id = 2; /* HACK */ + k_playermdl_id = 2; /* HACK */ } temp_update_playermodel(); /* HACK */ } if( button_down( k_srbind_mright ) ){ - if( cl_playermdl_id+1 < 3 ){ - cl_playermdl_id ++; + if( k_playermdl_id+1 < 3 ){ + k_playermdl_id ++; } else{ - cl_playermdl_id = 0; /* HACK */ + k_playermdl_id = 0; /* HACK */ } temp_update_playermodel(); /* HACK */ /*lol*/ @@ -514,8 +539,10 @@ VG_STATIC void global_skateshop_preupdate(void) browseable = 1; } - if( skaterift.async_op == k_async_op_none ){ - gui_helper_action( button_display_string(k_srbind_maccept), "load" ); + if( (skaterift.async_op == k_async_op_none) && + global_skateshop.selected_world_id > 0 ){ + gui_helper_action( button_display_string(k_srbind_maccept), + "open rift" ); loadable = 1; } @@ -550,9 +577,11 @@ VG_STATIC void global_skateshop_preupdate(void) /* automatically load in clouds */ if( loadable && button_down( k_srbind_maccept ) ){ - vg_info( "Select world (%u)\n", + vg_info( "Select rift (%u)\n", global_skateshop.selected_world_id ); - skaterift_change_world( reg->foldername ); + world_loader.reg = reg; + world_loader.override_name[0] = '\0'; + skaterift_change_world_start(); return; } else{ @@ -691,7 +720,33 @@ fade_out:; } struct cache_board *cache_ptr = skateshop_selected_cache_if_loaded(); - if( !cache_ptr ) return; + + if( !cache_ptr ){ + global_skateshop.render.item_title = ""; + global_skateshop.render.item_desc = ""; + return; + } + + if( global_skateshop.render.reg_id != global_skateshop.selected_board_id ){ + global_skateshop.render.item_title = ""; + global_skateshop.render.item_desc = ""; + addon_reg *reg = cache_ptr->reg_ptr; + vg_msg root = {0}; + root.buf = reg->metadata; + root.len = reg->metadata_len; + root.max = sizeof(reg->metadata); + + vg_msg workshop = root; + if( vg_msg_seekframe( &workshop, "workshop", 0 ) ){ + const char *title = vg_msg_seekkvstr( &workshop, "title", 0 ); + if( title ) global_skateshop.render.item_title = title; + + const char *dsc = vg_msg_seekkvstr( &workshop, "author", 0 ); + if( dsc ) global_skateshop.render.item_desc = dsc; + } + + global_skateshop.render.reg_id = global_skateshop.selected_board_id; + } addon_reg *reg = cache_ptr->reg_ptr; @@ -699,23 +754,27 @@ fade_out:; * ----------------------------------------------------------------- */ m3x3_zero( mlocal ); m3x3_setdiagonalv3( mlocal, (v3f){ scale, scale, thickness } ); - mlocal[3][0] = -font3d_string_width( &gui.font, 0, "Flubber" ); + mlocal[3][0] = -font3d_string_width( &gui.font, 0, + global_skateshop.render.item_title ); mlocal[3][0] *= scale*0.5f; mlocal[3][1] = 0.1f; mlocal[3][2] = 0.0f; m4x3_mul( mtext, mlocal, mmdl ); - font3d_simple_draw( &gui.font, 0, "Flubber", &main_camera, mmdl ); + font3d_simple_draw( &gui.font, 0, global_skateshop.render.item_title, + &main_camera, mmdl ); /* Author name * ----------------------------------------------------------------- */ scale *= 0.4f; m3x3_setdiagonalv3( mlocal, (v3f){ scale, scale, thickness } ); - mlocal[3][0] = -font3d_string_width( &gui.font, 0, "JA" ); + mlocal[3][0] = -font3d_string_width( &gui.font, 0, + global_skateshop.render.item_desc ); mlocal[3][0] *= scale*0.5f; mlocal[3][1] = 0.0f; mlocal[3][2] = 0.0f; m4x3_mul( mtext, mlocal, mmdl ); - font3d_simple_draw( &gui.font, 0, "JA", &main_camera, mmdl ); + font3d_simple_draw( &gui.font, 0, global_skateshop.render.item_desc, + &main_camera, mmdl ); } VG_STATIC void skateshop_render_charshop(void) @@ -732,6 +791,24 @@ VG_STATIC void skateshop_render_worldshop(void) *mark_info = mdl_arritm( &world->ent_marker, mdl_entity_id_id(shop->boards.id_info)); + if( global_skateshop.render.world_reg != global_skateshop.selected_world_id){ + global_skateshop.render.world_title = ""; + + addon_reg *reg = get_addon_from_index( k_workshop_file_type_world, + global_skateshop.selected_world_id ); + vg_msg root = {0}; + root.buf = reg->metadata; + root.len = reg->metadata_len; + root.max = sizeof(reg->metadata); + vg_msg workshop = root; + if( vg_msg_seekframe( &workshop, "workshop", 0 ) ){ + global_skateshop.render.world_title = vg_msg_seekkvstr( &workshop, + "title", 0 ); + } + global_skateshop.render.world_loc = vg_msg_seekkvstr(&root,"location",0); + global_skateshop.render.world_reg = global_skateshop.selected_world_id; + } + /* Text */ char buftext[128], bufsubtext[128]; vg_str info, subtext; @@ -750,13 +827,19 @@ VG_STATIC void skateshop_render_worldshop(void) info.buffer[info.i++] = ' '; info.buffer[info.i] = '\0'; - vg_strcat( &info, "AFAWJFKAW" ); + vg_strcat( &info, global_skateshop.render.world_title ); if( skaterift.async_op == k_async_op_world_loading || skaterift.async_op == k_async_op_world_preloading ){ vg_strcat( &subtext, "Loading..." ); } else{ - vg_strcat( &subtext, "No information" ); + addon_reg *reg = get_addon_from_index( k_workshop_file_type_world, + global_skateshop.selected_world_id ); + + if( reg->workshop_id ) + vg_strcat( &subtext, "(Workshop) " ); + + vg_strcat( &subtext, global_skateshop.render.world_loc ); } } else{