X-Git-Url: https://harrygodden.com/git/?a=blobdiff_plain;f=ent_skateshop.c;h=4c55bb939477117aac5c1cb36ae900aa50830ed9;hb=67928190c66b05bd48b6b515278e930974d25cd2;hp=39dec86a993115c9058cdd17118f9a8ae2b24cb6;hpb=9c915fa64d52d8ecfcb6c2a892829f2419dea8a1;p=carveJwlIkooP6JGAAIwe30JlM.git diff --git a/ent_skateshop.c b/ent_skateshop.c index 39dec86..4c55bb9 100644 --- a/ent_skateshop.c +++ b/ent_skateshop.c @@ -27,85 +27,20 @@ static inline int const_str_eq( u32 hash, const char *str, const char *cmp ) return 0; } -/* - * Get an existing cache instance, allocate a new one to be loaded, or NULL if - * there is no space - */ -VG_STATIC struct cache_board *skateshop_cache_fetch_board( u32 registry_index ) -{ - addon_reg *reg = NULL; - - if( registry_index < addon_count( k_workshop_file_type_board ) ){ - reg = get_addon_from_index( k_workshop_file_type_board, registry_index ); - - if( reg->userdata ){ - return reg->userdata; - } - } - -#if 0 - /* lru eviction. should be a linked list maybe... */ - double min_time = 1e300; - struct cache_board *min_board = NULL; - - SDL_AtomicLock( &global_skateshop.sl_cache_access ); - for( u32 i=0; istate == k_cache_board_state_load_request ) continue; - if( cache_ptr->ref_count ) continue; - - if( cache_ptr->last_use_time < min_time ){ - min_time = cache_ptr->last_use_time; - 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 ){ - player_board_unload( &min_board->board ); - min_board->reg_ptr->userdata = NULL; - } - - if( reg ){ - vg_info( "Allocating board (reg:%u) '%s'\n", - registry_index, reg->foldername ); - } - else{ - vg_info( "Pre-allocating board (reg:%u) 'null'\n", registry_index ); - } +VG_STATIC void skateshop_update_viewpage(void){ + u32 page = global_skateshop.selected_board_id/SKATESHOP_VIEW_SLOT_MAX; - min_board->reg_ptr = reg; - min_board->reg_index = registry_index; - min_board->ref_count = 0; - min_board->state = k_cache_board_state_load_request; - } - else{ - vg_error( "No free boards to load registry!\n" ); + for( u32 i=0; icache_id ); } - - SDL_AtomicUnlock( &global_skateshop.sl_cache_access ); - return min_board; -} - -VG_STATIC void skateshop_update_viewpage(void) -{ - u32 page = global_skateshop.selected_board_id/SKATESHOP_VIEW_SLOT_MAX; for( u32 i=0; icache_ptr ) unwatch_cache_board( slot->cache_ptr ); - - slot->cache_ptr = skateshop_cache_fetch_board( request_id ); - if( slot->cache_ptr ) watch_cache_board( slot->cache_ptr ); + slot->cache_id = addon_cache_create_viewer( k_addon_type_board, + request_id ); } } @@ -114,117 +49,25 @@ VG_STATIC void skateshop_update_viewpage(void) * ----------------------------------------------------------------------------- */ -/* - * Reciever for board completion; only promotes the status in the main thread - */ -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->state = k_cache_board_state_loaded; - - cache_ptr->reg_ptr->userdata = cache_ptr; - SDL_AtomicUnlock( &global_skateshop.sl_cache_access ); - vg_success( "Async board loaded (%s)\n", cache_ptr->reg_ptr->foldername ); -} - -/* - * Thread(or subroutine of thread), for checking view slots that weve installed. - * Load the model if a view slot wants it - */ -VG_STATIC void workshop_visibile_load_loop(void) -{ - vg_info( "Running load loop\n" ); - char path_buf[4096]; - - for( u32 i=0; istate == k_cache_board_state_load_request ){ - if( cache_ptr->reg_index >= addon_count(k_workshop_file_type_board) ){ - /* should maybe have a different value for this case */ - cache_ptr->state = k_cache_board_state_none; - SDL_AtomicUnlock( &global_skateshop.sl_cache_access ); - continue; - } - - /* continue with the request */ - SDL_AtomicUnlock( &global_skateshop.sl_cache_access ); - addon_reg *reg = get_addon_from_index( k_workshop_file_type_board, - cache_ptr->reg_index ); - cache_ptr->reg_ptr = reg; - - vg_str folder; - vg_strnull( &folder, path_buf, 4096 ); - if( !addon_get_content_folder( reg, &folder ) ) - goto file_is_broken; - - - /* load content files - * --------------------------------- */ - - vg_str content_path = folder; - - - 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( !vg_strgood( &content_path ) ) { - vg_error( "Metadata path too long\n" ); - goto file_is_broken; - } - - vg_info( "Load content: %s\n", content_path.buffer ); - player_board_load( &cache_ptr->board, content_path.buffer ); - vg_async_call( skateshop_async_board_loaded, cache_ptr, 0 ); - continue; - -file_is_broken:; - SDL_AtomicLock( &global_skateshop.sl_cache_access ); - cache_ptr->state = k_cache_board_state_none; - SDL_AtomicUnlock( &global_skateshop.sl_cache_access ); - } - else - SDL_AtomicUnlock( &global_skateshop.sl_cache_access ); - } -} - - VG_STATIC void world_scan_thread( void *_args ){ - addon_mount_content_folder( k_workshop_file_type_world, "maps", ".mdl" ); + addon_mount_content_folder( k_addon_type_world, "maps", ".mdl" ); addon_mount_workshop_items(); vg_async_call( async_addon_reg_update, NULL, 0 ); - skaterift_end_op(); } /* * Asynchronous scan of local disk for worlds */ VG_STATIC void skateshop_op_world_scan(void){ - skaterift_begin_op( k_async_op_world_scan ); vg_loader_start( world_scan_thread, NULL ); } VG_STATIC void board_processview_thread( void *_args ){ - workshop_visibile_load_loop(); - skaterift_end_op(); + addon_cache_load_loop(); } VG_STATIC void board_scan_thread( void *_args ){ - addon_mount_content_folder( k_workshop_file_type_board, "boards", ".mdl" ); + addon_mount_content_folder( k_addon_type_board, "boards", ".mdl" ); addon_mount_workshop_items(); vg_async_call( async_addon_reg_update, NULL, 0 ); vg_async_stall(); @@ -232,12 +75,10 @@ VG_STATIC void board_scan_thread( void *_args ){ } VG_STATIC void skateshop_op_board_scan(void){ - skaterift_begin_op( k_async_op_board_scan ); 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 ); } @@ -246,96 +87,19 @@ VG_STATIC void skateshop_op_processview(void){ * ----------------------------------------------------------------------------- */ -/* 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->ref_count ++; -} - -/* 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 */ VG_STATIC void skateshop_init(void){ - u32 cache_size = sizeof(struct cache_board)*SKATESHOP_BOARD_CACHE_MAX; - global_skateshop.cache = vg_linear_alloc( vg_mem.rtmemory, cache_size ); - memset( global_skateshop.cache, 0, cache_size ); - - 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) -{ - if( addon_count(k_workshop_file_type_board) ){ - addon_reg *reg = get_addon_from_index(k_workshop_file_type_board, +static u16 skateshop_selected_cache_id(void){ + if( addon_count(k_addon_type_board) ){ + addon_reg *reg = get_addon_from_index(k_addon_type_board, global_skateshop.selected_board_id); - - SDL_AtomicLock( &global_skateshop.sl_cache_access ); - if( reg->userdata ){ - struct cache_board *cache_ptr = reg->userdata; - if( cache_ptr->state == k_cache_board_state_loaded ){ - SDL_AtomicUnlock( &global_skateshop.sl_cache_access ); - return cache_ptr; - } - } - SDL_AtomicUnlock( &global_skateshop.sl_cache_access ); + return reg->cache_id; } - - return NULL; + else return 0; } VG_STATIC void pointcloud_async_end(void *_, u32 __) @@ -349,7 +113,7 @@ VG_STATIC void pointcloud_clear_async(void *_, u32 __) pointcloud_animate( k_pointcloud_anim_opening ); } -VG_STATIC void skateshop_preview_loader_thread( void *_data ) +VG_STATIC void skateshop_world_preview_loader_thread( void *_data ) { addon_reg *reg = _data; @@ -387,15 +151,12 @@ VG_STATIC void skateshop_preview_loader_thread( void *_data ) } } -VG_STATIC void skateshop_preview_loader_thread_and_end( void *_data ){ - skateshop_preview_loader_thread( _data ); - skaterift_end_op(); +VG_STATIC void skateshop_world_preview_loader_thread_and_end( void *_data ){ + skateshop_world_preview_loader_thread( _data ); } -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_and_end, reg ); +VG_STATIC void skateshop_load_world_preview( addon_reg *reg ){ + vg_loader_start( skateshop_world_preview_loader_thread_and_end, reg ); } /* @@ -447,24 +208,20 @@ VG_STATIC void global_skateshop_preupdate(void) /* input */ if( shop->type == k_skateshop_type_boardshop ){ - if( skaterift.async_op != k_async_op_none ) return; + if( !vg_loader_availible() ) return; gui_helper_action( axis_display_string( k_sraxis_mbrowse_h ), "browse" ); gui_helper_action( button_display_string( k_srbind_mback ), "exit" ); - struct cache_board *selected_cache = skateshop_selected_cache_if_loaded(); - - if( selected_cache ){ + u16 cache_id = skateshop_selected_cache_id(); + if( cache_id ){ gui_helper_action( button_display_string( k_srbind_maccept ), "pick" ); } /* * Controls * ---------------------- - * - * TODO: Crash if switch page too quick, delist browse if loading.... */ - u32 opage = global_skateshop.selected_board_id/SKATESHOP_VIEW_SLOT_MAX; if( button_down( k_srbind_mleft ) ){ @@ -475,7 +232,7 @@ VG_STATIC void global_skateshop_preupdate(void) if( button_down( k_srbind_mright ) ){ if( global_skateshop.selected_board_id+1 < - addon_count(k_workshop_file_type_board) ) + addon_count(k_addon_type_board) ) { global_skateshop.selected_board_id ++; } @@ -487,45 +244,55 @@ VG_STATIC void global_skateshop_preupdate(void) skateshop_update_viewpage(); skateshop_op_processview(); } - else if( selected_cache && button_down( k_srbind_maccept ) ){ + else if( cache_id && button_down( k_srbind_maccept )){ vg_info( "chose board from skateshop (%u)\n", global_skateshop.selected_board_id ); - if( localplayer.board_view_slot ){ - unwatch_cache_board( localplayer.board_view_slot ); - } + addon_cache_unwatch( k_addon_type_board, localplayer.board_view_slot ); + addon_cache_watch( k_addon_type_board, cache_id ); + localplayer.board_view_slot = cache_id; - localplayer.board_view_slot = selected_cache; - watch_cache_board( localplayer.board_view_slot ); global_skateshop_exit(); skaterift_write_savedata(); return; } } else if( shop->type == k_skateshop_type_charshop ){ + if( !vg_loader_availible() ) return; + gui_helper_action( axis_display_string( k_sraxis_mbrowse_h ), "browse" ); gui_helper_action( button_display_string( k_srbind_mback ), "exit" ); gui_helper_action( button_display_string( k_srbind_maccept ), "pick" ); + int changed = 0; + if( button_down( k_srbind_mleft ) ){ - if( k_playermdl_id > 0 ){ - k_playermdl_id --; + if( global_skateshop.selected_player_id > 0 ){ + global_skateshop.selected_player_id --; } else{ - k_playermdl_id = 2; /* HACK */ + global_skateshop.selected_player_id = + addon_count(k_addon_type_player) -1; } - temp_update_playermodel(); /* HACK */ + + changed = 1; } if( button_down( k_srbind_mright ) ){ - if( k_playermdl_id+1 < 3 ){ - k_playermdl_id ++; + if( global_skateshop.selected_player_id+1 < + addon_count(k_addon_type_player) ){ + global_skateshop.selected_player_id ++; } else{ - k_playermdl_id = 0; /* HACK */ + global_skateshop.selected_player_id = 0; } - temp_update_playermodel(); /* HACK */ - /*lol*/ + + changed = 1; + } + + if( changed ){ + player__use_model( &localplayer, global_skateshop.selected_player_id ); + skateshop_op_processview(); } if( button_down( k_srbind_maccept ) ){ @@ -536,15 +303,12 @@ VG_STATIC void global_skateshop_preupdate(void) int browseable = 0, loadable = 0; - if( addon_count(k_workshop_file_type_world) && - ((skaterift.async_op == k_async_op_none)|| - (skaterift.async_op == k_async_op_world_load_preview))){ + if( addon_count(k_addon_type_world) && vg_loader_availible() ){ gui_helper_action( axis_display_string(k_sraxis_mbrowse_h), "browse" ); browseable = 1; } - if( (skaterift.async_op == k_async_op_none) && - global_skateshop.selected_world_id > 0 ){ + if( vg_loader_availible() && global_skateshop.selected_world_id > 0 ){ gui_helper_action( button_display_string(k_srbind_maccept), "open rift" ); loadable = 1; @@ -563,7 +327,7 @@ VG_STATIC void global_skateshop_preupdate(void) if( button_down( k_srbind_mright ) ){ if( global_skateshop.selected_world_id+1 < - addon_count(k_workshop_file_type_world) ) + addon_count(k_addon_type_world) ) { global_skateshop.selected_world_id ++; change = 1; @@ -575,8 +339,8 @@ VG_STATIC void global_skateshop_preupdate(void) pointcloud_animate( k_pointcloud_anim_hiding ); } - if( skaterift.async_op == k_async_op_none ){ - addon_reg *reg = get_addon_from_index( k_workshop_file_type_world, + if( vg_loader_availible() ){ + addon_reg *reg = get_addon_from_index( k_addon_type_world, global_skateshop.selected_world_id ); /* automatically load in clouds */ @@ -633,25 +397,24 @@ VG_STATIC void skateshop_render_boardshop(void) *mark_display = mdl_arritm( &world->ent_marker, mdl_entity_id_id(shop->boards.id_display)); - int visibility[ SKATESHOP_VIEW_SLOT_MAX ]; - SDL_AtomicLock( &global_skateshop.sl_cache_access ); - for( u32 i=0; icache_ptr == NULL ) visibility[i] = 0; - else if( slot->cache_ptr->state != k_cache_board_state_loaded ) - visibility[i] = 0; - } - SDL_AtomicUnlock( &global_skateshop.sl_cache_access ); + SDL_AtomicLock( &addon_system.sl_cache_using_resources ); + struct addon_cache *cache = &addon_system.cache[k_addon_type_board]; /* Render loaded boards in the view slots */ for( u32 i=0; icache_id ) + goto fade_out; + + addon_cache_entry *entry = vg_pool_item( &cache->pool, slot->cache_id ); + + if( entry->state != k_addon_cache_state_loaded ) + goto fade_out; + + struct player_board *board = + addon_cache_item( k_addon_type_board, slot->cache_id ); mdl_transform xform; transform_identity( &xform ); @@ -659,7 +422,8 @@ VG_STATIC void skateshop_render_boardshop(void) xform.co[0] = -((float)i - ((float)slot_count)*0.5f)*0.45f; mdl_transform_mul( &mark_rack->transform, &xform, &xform ); - if( slot->cache_ptr->reg_index == global_skateshop.selected_board_id ){ + + if( entry->reg_index == global_skateshop.selected_board_id ){ selected = 1.0f; } @@ -668,10 +432,11 @@ VG_STATIC void skateshop_render_boardshop(void) q_nlerp( xform.q, mark_display->transform.q, t, xform.q ); v3_lerp( xform.s, mark_display->transform.s, t, xform.s ); + struct board_pose pose = {0}; m4x3f mmdl; mdl_transform_m4x3( &xform, mmdl ); - render_board( &main_camera, world, &slot->cache_ptr->board, mmdl, - k_board_shader_entity ); + render_board( &main_camera, world, board, mmdl, + &pose, k_board_shader_entity ); fade_out:; float rate = 5.0f*vg.time_delta; @@ -708,12 +473,12 @@ fade_out:; mlocal[3][2] = -0.7f; m4x3_mul( mrack, mlocal, mmdl ); - if( addon_count(k_workshop_file_type_board) ){ + if( addon_count(k_addon_type_board) ){ char buf[16]; int i=0; i+=highscore_intl( buf+i, global_skateshop.selected_board_id+1, 3 ); buf[i++] = '/'; - i+=highscore_intl( buf+i, addon_count(k_workshop_file_type_board), 3 ); + i+=highscore_intl( buf+i, addon_count(k_addon_type_board), 3 ); buf[i++] = '\0'; font3d_simple_draw( &gui.font, 0, buf, &main_camera, mmdl ); @@ -723,9 +488,14 @@ fade_out:; "Nothing installed", &main_camera, mmdl ); } - struct cache_board *cache_ptr = skateshop_selected_cache_if_loaded(); + u16 cache_id = skateshop_selected_cache_id(); + struct addon_cache_entry *entry = vg_pool_item( &cache->pool, cache_id ); + addon_reg *reg = NULL; + + if( entry ) reg = entry->reg_ptr; - if( !cache_ptr ){ + if( !reg ){ + SDL_AtomicUnlock( &addon_system.sl_cache_using_resources ); global_skateshop.render.item_title = ""; global_skateshop.render.item_desc = ""; return; @@ -734,7 +504,6 @@ fade_out:; 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; @@ -752,8 +521,6 @@ fade_out:; global_skateshop.render.reg_id = global_skateshop.selected_board_id; } - addon_reg *reg = cache_ptr->reg_ptr; - /* Skin title * ----------------------------------------------------------------- */ m3x3_zero( mlocal ); @@ -779,6 +546,8 @@ fade_out:; m4x3_mul( mtext, mlocal, mmdl ); font3d_simple_draw( &gui.font, 0, global_skateshop.render.item_desc, &main_camera, mmdl ); + + SDL_AtomicUnlock( &addon_system.sl_cache_using_resources ); } VG_STATIC void skateshop_render_charshop(void) @@ -798,7 +567,7 @@ VG_STATIC void skateshop_render_worldshop(void) 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, + addon_reg *reg = get_addon_from_index( k_addon_type_world, global_skateshop.selected_world_id ); vg_msg root = {0}; root.buf = reg->metadata; @@ -819,28 +588,27 @@ VG_STATIC void skateshop_render_worldshop(void) vg_strnull( &info, buftext, 128 ); vg_strnull( &subtext, bufsubtext, 128 ); - if( addon_count(k_workshop_file_type_world) ){ - addon_reg *reg = get_addon_from_index( k_workshop_file_type_world, + if( addon_count(k_addon_type_world) ){ + addon_reg *reg = get_addon_from_index( k_addon_type_world, global_skateshop.selected_world_id ); info.i+=highscore_intl( info.buffer+info.i, global_skateshop.selected_world_id+1, 3 ); info.buffer[info.i++] = '/'; info.i+=highscore_intl( info.buffer+info.i, - addon_count(k_workshop_file_type_world), 3 ); + addon_count(k_addon_type_world), 3 ); info.buffer[info.i++] = ' '; info.buffer[info.i] = '\0'; 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 ){ + if( !vg_loader_availible() ){ vg_strcat( &subtext, "Loading..." ); } else{ - addon_reg *reg = get_addon_from_index( k_workshop_file_type_world, + addon_reg *reg = get_addon_from_index( k_addon_type_world, global_skateshop.selected_world_id ); - if( reg->workshop_id ) + if( reg->alias.workshop_id ) vg_strcat( &subtext, "(Workshop) " ); vg_strcat( &subtext, global_skateshop.render.world_loc ); @@ -920,7 +688,7 @@ VG_STATIC void ent_skateshop_call( world_instance *world, ent_call *call ) vg_info( "skateshop_call\n" ); if( menu.active ) return; - if( skaterift.async_op != k_async_op_none ) return; + if( !vg_loader_availible() ) return; if( call->function == k_ent_function_trigger ){ if( localplayer.subsystem != k_player_subsystem_walk ){ @@ -941,6 +709,9 @@ VG_STATIC void ent_skateshop_call( world_instance *world, ent_call *call ) if( shop->type == k_skateshop_type_boardshop ){ skateshop_update_viewpage(); skateshop_op_board_scan(); + } + else if( shop->type == k_skateshop_type_charshop ){ + } else if( shop->type == k_skateshop_type_worldshop ){ pointcloud_animate( k_pointcloud_anim_opening );