5 #include "addon_types.h"
10 static u32
addon_count( enum addon_type type
, u32 ignoreflags
){
12 u32 typecount
= 0, count
= 0;
13 for( u32 i
=0; typecount
<addon_count( type
, 0 ); i
++ ){
14 addon_reg
*reg
= &addon_system
.registry
[i
];
15 if( reg
->alias
.type
== type
){
18 if( reg
->flags
& ignoreflags
)
28 return addon_system
.registry_type_counts
[ type
];
32 /* these kind of suck, oh well. */
33 static addon_reg
*get_addon_from_index( enum addon_type type
, u32 index
,
35 u32 typecount
= 0, count
= 0;
36 for( u32 i
=0; typecount
<addon_count(type
,0); i
++ ){
37 addon_reg
*reg
= &addon_system
.registry
[i
];
38 if( reg
->alias
.type
== type
){
41 if( reg
->flags
& ignoreflags
)
54 static u32
get_index_from_addon( enum addon_type type
, addon_reg
*a
){
56 for( u32 i
=0; count
<addon_system
.registry_type_counts
[type
]; i
++ ){
57 addon_reg
*reg
= &addon_system
.registry
[i
];
58 if( reg
->alias
.type
== type
){
69 static u32
addon_match( addon_alias
*alias
){
70 if( alias
->type
== k_addon_type_none
) return 0xffffffff;
72 u32 foldername_djb2
= 0;
73 if( !alias
->workshop_id
)
74 foldername_djb2
= vg_strdjb2( alias
->foldername
);
77 for( u32 i
=0; count
<addon_system
.registry_type_counts
[alias
->type
]; i
++ ){
78 addon_reg
*reg
= &addon_system
.registry
[i
];
79 if( reg
->alias
.type
== alias
->type
){
81 if( alias
->workshop_id
){
82 if( alias
->workshop_id
== reg
->alias
.workshop_id
)
86 if( reg
->foldername_hash
== foldername_djb2
){
87 if( !strcmp( reg
->alias
.foldername
, alias
->foldername
) ){
101 * Create a string version of addon alias in buf
103 static void addon_alias_uid( addon_alias
*alias
, char buf
[ADDON_UID_MAX
] ){
104 if( alias
->workshop_id
){
105 snprintf( buf
, 128, "sr%03d-steam-"PRINTF_U64
,
106 alias
->type
, alias
->workshop_id
);
109 snprintf( buf
, 128, "sr%03d-local-%s",
110 alias
->type
, alias
->foldername
);
117 static int addon_alias_eq( addon_alias
*a
, addon_alias
*b
){
118 if( a
->type
== b
->type
){
119 if( a
->workshop_id
== b
->workshop_id
){
123 return !strcmp( a
->foldername
, b
->foldername
);
132 * make alias represent NULL.
134 static void invalidate_addon_alias( addon_alias
*alias
){
135 alias
->type
= k_addon_type_none
;
136 alias
->workshop_id
= 0;
137 alias
->foldername
[0] = '\0';
141 * parse uid to alias. returns 1 if successful
143 static int addon_uid_to_alias( const char *uid
, addon_alias
*alias
){
152 if( strlen(uid
) < 13 ){
153 invalidate_addon_alias( alias
);
156 if( !((uid
[0] == 's') && (uid
[1] == 'r')) ){
157 invalidate_addon_alias( alias
);
162 memcpy( type
, uid
+2, 3 );
164 alias
->type
= atoi(type
);
167 memcpy( location
, uid
+6, 5 );
170 if( !strcmp(location
,"steam") )
171 alias
->workshop_id
= atoll( uid
+12 );
172 else if( !strcmp(location
,"local") ){
173 alias
->workshop_id
= 0;
174 vg_strncpy( uid
+12, alias
->foldername
, 64, k_strncpy_always_add_null
);
177 invalidate_addon_alias( alias
);
184 static void addon_system_init( void ){
185 u32 reg_size
= sizeof(addon_reg
)*ADDON_MOUNTED_MAX
;
186 addon_system
.registry
= vg_linear_alloc( vg_mem
.rtmemory
, reg_size
);
188 for( u32 type
=0; type
<k_addon_type_max
; type
++ ){
189 struct addon_type_info
*inf
= &addon_type_infos
[type
];
190 struct addon_cache
*cache
= &addon_system
.cache
[type
];
192 if( inf
->cache_count
){
193 /* create the allocations pool */
194 u32 alloc_size
= sizeof(struct addon_cache_entry
)*inf
->cache_count
;
195 cache
->allocs
= vg_linear_alloc( vg_mem
.rtmemory
, alloc_size
);
196 memset( cache
->allocs
, 0, alloc_size
);
198 cache
->pool
.buffer
= cache
->allocs
;
199 cache
->pool
.count
= inf
->cache_count
;
200 cache
->pool
.stride
= sizeof( struct addon_cache_entry
);
201 cache
->pool
.offset
= offsetof( struct addon_cache_entry
, poolnode
);
202 vg_pool_init( &cache
->pool
);
204 /* create the real memory */
205 u32 cache_size
= inf
->cache_stride
*inf
->cache_count
;
206 cache
->items
= vg_linear_alloc( vg_mem
.rtmemory
, cache_size
);
207 cache
->stride
= inf
->cache_stride
;
208 memset( cache
->items
, 0, cache_size
);
210 for( i32 j
=0; j
<inf
->cache_count
; j
++ ){
211 struct addon_cache_entry
*alloc
= &cache
->allocs
[j
];
212 alloc
->reg_ptr
= NULL
;
213 alloc
->reg_index
= 0xffffffff;
221 * -----------------------------------------------------------------------------
225 * Reciever for scan completion. copies the registry counts back into main fred
227 static void async_addon_reg_update( void *data
, u32 size
)
229 vg_info( "Registry update notify\n" );
231 for( u32 i
=0; i
<k_addon_type_max
; i
++ ){
232 addon_system
.registry_type_counts
[i
] = 0;
235 for( u32 i
=0; i
<addon_system
.registry_count
; i
++ ){
236 enum addon_type type
= addon_system
.registry
[i
].alias
.type
;
237 addon_system
.registry_type_counts
[ type
] ++;
241 static void addon_set_foldername( addon_reg
*reg
, const char name
[64] ){
242 vg_strncpy( name
, reg
->alias
.foldername
, 64, k_strncpy_always_add_null
);
243 reg
->foldername_hash
= vg_strdjb2( reg
->alias
.foldername
);
247 * Create a new registry
249 static addon_reg
*addon_alloc_reg( PublishedFileId_t workshop_id
,
250 enum addon_type type
){
251 if( addon_system
.registry_count
== ADDON_MOUNTED_MAX
){
252 vg_error( "You have too many addons installed!\n" );
256 addon_reg
*reg
= &addon_system
.registry
[ addon_system
.registry_count
];
258 reg
->metadata_len
= 0;
260 reg
->state
= k_addon_state_indexed
;
261 reg
->alias
.workshop_id
= workshop_id
;
262 reg
->alias
.foldername
[0] = '\0';
263 reg
->alias
.type
= type
;
267 snprintf( foldername
, 64, PRINTF_U64
, workshop_id
);
268 addon_set_foldername( reg
, foldername
);
274 * If the addon.inf exists int the folder, load into the reg
276 static int addon_try_load_metadata( addon_reg
*reg
, vg_str folder_path
){
277 vg_str meta_path
= folder_path
;
278 vg_strcat( &meta_path
, "/addon.inf" );
279 if( !vg_strgood( &meta_path
) ){
280 vg_error( "The metadata path is too long\n" );
284 FILE *fp
= fopen( meta_path
.buffer
, "rb" );
286 vg_error( "Could not open the '%s'\n", meta_path
.buffer
);
290 reg
->metadata_len
= fread( reg
->metadata
, 1, 512, fp
);
291 if( reg
->metadata_len
!= 512 ){
294 vg_error( "unknown error codition" );
295 reg
->metadata_len
= 0;
303 static void addon_print_info( addon_reg
*reg
){
304 vg_info( "addon_reg #%u{\n", addon_system
.registry_count
);
305 vg_info( " type: %d\n", reg
->alias
.type
);
306 vg_info( " workshop_id: " PRINTF_U64
"\n", reg
->alias
.workshop_id
);
307 vg_info( " folder: [%u]%s\n", reg
->foldername_hash
, reg
->alias
.foldername
);
308 vg_info( " metadata_len: %u\n", reg
->metadata_len
);
309 vg_info( " cache_id: %hu\n", reg
->cache_id
);
313 static void addon_mount_finish( addon_reg
*reg
){
315 addon_print_info( reg
);
317 addon_system
.registry_count
++;
321 * Mount a fully packaged addon, one that certainly has a addon.inf
323 static addon_reg
*addon_mount_workshop_folder( PublishedFileId_t workshop_id
,
326 addon_reg
*reg
= addon_alloc_reg( workshop_id
, k_addon_type_none
);
327 if( !reg
) return NULL
;
329 if( !addon_try_load_metadata( reg
, folder_path
) ){
333 enum addon_type type
= k_addon_type_none
;
335 vg_msg_init( &msg
, reg
->metadata
, reg
->metadata_len
);
337 if( vg_msg_seekframe( &msg
, "workshop" )){
338 type
= vg_msg_getkvu32( &msg
, "type", 0 );
341 if( type
== k_addon_type_none
){
342 vg_error( "Cannot determine addon type\n" );
346 reg
->alias
.type
= type
;
347 addon_mount_finish( reg
);
352 * Mount a local folder. may or may not have addon.inf
354 static addon_reg
*addon_mount_local_addon( const char *folder
,
355 enum addon_type type
,
356 const char *content_ext
)
358 char folder_path_buf
[4096];
360 vg_strnull( &folder_path
, folder_path_buf
, 4096 );
361 vg_strcat( &folder_path
, folder
);
363 const char *folder_name
= vg_strch( &folder_path
, '/' )+1;
364 u32 folder_hash
= vg_strdjb2(folder_name
);
365 for( u32 i
=0; i
<addon_system
.registry_count
; i
++ ){
366 addon_reg
*reg
= &addon_system
.registry
[i
];
368 if( (reg
->alias
.type
== type
) && (reg
->foldername_hash
== folder_hash
) ){
369 if( !strcmp( reg
->alias
.foldername
, folder_name
) ){
370 reg
->state
= k_addon_state_indexed
;
376 addon_reg
*reg
= addon_alloc_reg( 0, type
);
377 if( !reg
) return NULL
;
378 addon_set_foldername( reg
, folder_name
);
379 addon_try_load_metadata( reg
, folder_path
);
381 if( reg
->metadata_len
== 0 ){
382 /* create our own content commands */
384 vg_msg_init( &msg
, reg
->metadata
, sizeof(reg
->metadata
) );
386 u32 content_count
= 0;
388 vg_strcat( &folder_path
, "" );
389 vg_warn( "Creating own metadata for: %s\n", folder_path
.buffer
);
392 if( !vg_dir_open(&subdir
, folder_path
.buffer
) ){
393 vg_error( "Failed to open '%s'\n", folder_path
.buffer
);
397 while( vg_dir_next_entry(&subdir
) ){
398 if( vg_dir_entry_type(&subdir
) == k_vg_entry_type_file
){
399 const char *fname
= vg_dir_entry_name(&subdir
);
400 vg_str file
= folder_path
;
401 vg_strcat( &file
, "/" );
402 vg_strcat( &file
, fname
);
403 if( !vg_strgood( &file
) ) continue;
405 char *ext
= vg_strch( &file
, '.' );
407 if( strcmp(ext
,content_ext
) ) continue;
409 vg_msg_wkvstr( &msg
, "content", fname
);
413 vg_dir_close(&subdir
);
415 if( !content_count
) return NULL
;
416 if( msg
.error
== k_vg_msg_error_OK
)
417 reg
->metadata_len
= msg
.cur
.co
;
419 vg_error( "Error creating metadata: %d\n", msg
.error
);
424 addon_mount_finish( reg
);
429 * Check all subscribed items
431 static void addon_mount_workshop_items(void){
432 if( skaterift
.demo_mode
){
433 vg_info( "Won't load workshop items in demo mode\n" );
436 if( !steam_ready
) return;
439 * Steam workshop scan
441 vg_info( "Mounting steam workshop subscriptions\n" );
442 PublishedFileId_t workshop_ids
[ ADDON_MOUNTED_MAX
];
443 u32 workshop_count
= ADDON_MOUNTED_MAX
;
445 vg_async_item
*call
= vg_async_alloc(
446 sizeof(struct async_workshop_installed_files_info
));
447 struct async_workshop_installed_files_info
*info
= call
->payload
;
448 info
->buffer
= workshop_ids
;
449 info
->len
= &workshop_count
;
450 vg_async_dispatch( call
, async_workshop_get_installed_files
);
453 for( u32 j
=0; j
<workshop_count
; j
++ ){
454 /* check for existance in both our caches
455 * ----------------------------------------------------------*/
456 PublishedFileId_t id
= workshop_ids
[j
];
457 for( u32 i
=0; i
<addon_system
.registry_count
; i
++ ){
458 addon_reg
*reg
= &addon_system
.registry
[i
];
460 if( reg
->alias
.workshop_id
== id
){
461 reg
->state
= k_addon_state_indexed
;
462 goto next_file_workshop
;
466 vg_async_item
*call1
=
467 vg_async_alloc( sizeof(struct async_workshop_filepath_info
) );
471 struct async_workshop_filepath_info
*info
= call1
->payload
;
474 info
->len
= vg_list_size(path
);
475 vg_async_dispatch( call1
, async_workshop_get_filepath
);
476 vg_async_stall(); /* too bad! */
478 vg_str folder
= {.buffer
= path
, .i
=strlen(path
), .len
=4096};
479 addon_mount_workshop_folder( id
, folder
);
485 * Scan a local content folder for addons. It must find at least one file with
486 * the specified content_ext to be considered.
488 static void addon_mount_content_folder( enum addon_type type
,
489 const char *base_folder
,
490 const char *content_ext
)
492 vg_info( "Mounting addons(type:%d) matching skaterift/%s/*/*%s\n",
493 type
, base_folder
, content_ext
);
497 vg_strnull( &path
, path_buf
, 4096 );
498 vg_strcat( &path
, base_folder
);
501 if( !vg_dir_open(&dir
,path
.buffer
) ){
502 vg_error( "vg_dir_open('%s') failed\n", path
.buffer
);
506 vg_strcat(&path
,"/");
508 while( vg_dir_next_entry(&dir
) ){
509 if( vg_dir_entry_type(&dir
) == k_vg_entry_type_dir
){
510 const char *d_name
= vg_dir_entry_name(&dir
);
512 vg_str folder
= path
;
513 if( strlen( d_name
) > ADDON_FOLDERNAME_MAX
){
514 vg_warn( "folder too long: %s\n", d_name
);
518 vg_strcat( &folder
, d_name
);
519 if( !vg_strgood( &folder
) ) continue;
521 addon_mount_local_addon( folder
.buffer
, type
, content_ext
);
528 * write the full path of the addon's folder into the vg_str
530 static int addon_get_content_folder( addon_reg
*reg
, vg_str
*folder
, int async
){
531 if( reg
->alias
.workshop_id
){
532 struct async_workshop_filepath_info
*info
= NULL
;
533 vg_async_item
*call
= NULL
;
536 call
= vg_async_alloc( sizeof(struct async_workshop_filepath_info
) );
537 info
= call
->payload
;
540 info
= alloca( sizeof(struct async_workshop_filepath_info
) );
542 info
->buf
= folder
->buffer
;
543 info
->id
= reg
->alias
.workshop_id
;
544 info
->len
= folder
->len
;
547 vg_async_dispatch( call
, async_workshop_get_filepath
);
548 vg_async_stall(); /* too bad! */
551 async_workshop_get_filepath( info
, 0 );
554 if( info
->buf
[0] == '\0' ){
555 vg_error( "Failed SteamAPI_GetItemInstallInfo(" PRINTF_U64
")\n",
556 reg
->alias
.workshop_id
);
559 folder
->i
= strlen( folder
->buffer
);
565 const char *local_folder
=
566 addon_type_infos
[reg
->alias
.type
].local_content_folder
;
568 if( !local_folder
) return 0;
569 vg_strcat( folder
, local_folder
);
570 vg_strcat( folder
, reg
->alias
.foldername
);
576 * Return existing cache id if reg_index points to a registry with its cache
579 static u16
addon_cache_fetch( enum addon_type type
, u32 reg_index
){
580 addon_reg
*reg
= NULL
;
582 if( reg_index
< addon_count( type
, 0 ) ){
583 reg
= get_addon_from_index( type
, reg_index
, 0 );
585 return reg
->cache_id
;
592 * Allocate a new cache item from the pool
594 static u16
addon_cache_alloc( enum addon_type type
, u32 reg_index
){
595 struct addon_cache
*cache
= &addon_system
.cache
[ type
];
597 u16 new_id
= vg_pool_lru( &cache
->pool
);
598 struct addon_cache_entry
*new_entry
= vg_pool_item( &cache
->pool
, new_id
);
600 addon_reg
*reg
= NULL
;
601 if( reg_index
< addon_count( type
, 0 ) )
602 reg
= get_addon_from_index( type
, reg_index
, 0 );
605 if( new_entry
->reg_ptr
)
606 new_entry
->reg_ptr
->cache_id
= 0;
609 reg
->cache_id
= new_id
;
611 new_entry
->reg_ptr
= reg
;
612 new_entry
->reg_index
= reg_index
;
616 vg_error( "cache full (type: %u)!\n", type
);
622 * Get the real item data for cache id
624 static void *addon_cache_item( enum addon_type type
, u16 id
){
625 if( !id
) return NULL
;
627 struct addon_cache
*cache
= &addon_system
.cache
[type
];
628 return cache
->items
+ ((size_t)(id
-1) * cache
->stride
);
632 * Get the real item data for cache id ONLY if the item is completely loaded.
634 static void *addon_cache_item_if_loaded( enum addon_type type
, u16 id
){
635 if( !id
) return NULL
;
637 struct addon_cache
*cache
= &addon_system
.cache
[type
];
638 struct addon_cache_entry
*entry
= vg_pool_item( &cache
->pool
, id
);
640 if( entry
->state
== k_addon_cache_state_loaded
)
641 return addon_cache_item( type
, id
);
646 * Updates the item state from the main thread
648 static void async_addon_setstate( void *_entry
, u32 _state
){
649 addon_cache_entry
*entry
= _entry
;
650 SDL_AtomicLock( &addon_system
.sl_cache_using_resources
);
651 entry
->state
= _state
;
652 SDL_AtomicUnlock( &addon_system
.sl_cache_using_resources
);
653 vg_success( " loaded (%s)\n", entry
->reg_ptr
->alias
.foldername
);
657 * Handles the loading of an individual item
659 static int addon_cache_load_request( enum addon_type type
, u16 id
,
660 addon_reg
*reg
, vg_str folder
){
662 /* load content files
663 * --------------------------------- */
664 vg_str content_path
= folder
;
667 vg_msg_init( &msg
, reg
->metadata
, reg
->metadata_len
);
669 const char *kv_content
= vg_msg_getkvstr( &msg
, "content" );
671 vg_strcat( &content_path
, "/" );
672 vg_strcat( &content_path
, kv_content
);
675 vg_error( " No content paths in metadata\n" );
679 if( !vg_strgood( &content_path
) ) {
680 vg_error( " Metadata path too long\n" );
684 if( type
== k_addon_type_board
){
685 struct player_board
*board
= addon_cache_item( type
, id
);
686 player_board_load( board
, content_path
.buffer
);
689 else if( type
== k_addon_type_player
){
690 struct player_model
*model
= addon_cache_item( type
, id
);
691 player_model_load( model
, content_path
.buffer
);
701 static void addon_cache_free_item( enum addon_type type
, u16 id
){
702 if( type
== k_addon_type_board
){
703 struct player_board
*board
= addon_cache_item( type
, id
);
704 player_board_unload( board
);
706 else if( type
== k_addon_type_player
){
707 struct player_model
*model
= addon_cache_item( type
, id
);
708 player_model_unload( model
);
713 * Goes over cache item load requests and calls the above ^
715 static void addon_cache_load_loop(void){
716 vg_info( "Running load loop\n" );
719 for( u32 type
=0; type
<k_addon_type_max
; type
++ ){
720 struct addon_cache
*cache
= &addon_system
.cache
[type
];
722 for( u32 id
=1; id
<=cache
->pool
.count
; id
++ ){
723 addon_cache_entry
*entry
= vg_pool_item( &cache
->pool
, id
);
725 SDL_AtomicLock( &addon_system
.sl_cache_using_resources
);
726 if( entry
->state
== k_addon_cache_state_load_request
){
727 vg_info( "process cache load request (%u#%u, reg:%u)\n",
728 type
, id
, entry
->reg_index
);
730 if( entry
->reg_index
>= addon_count(type
,0) ){
731 /* should maybe have a different value for this case */
732 entry
->state
= k_addon_cache_state_none
;
733 SDL_AtomicUnlock( &addon_system
.sl_cache_using_resources
);
737 SDL_AtomicUnlock( &addon_system
.sl_cache_using_resources
);
739 /* continue with the request */
740 addon_reg
*reg
= get_addon_from_index( type
, entry
->reg_index
, 0 );
741 entry
->reg_ptr
= reg
;
744 vg_strnull( &folder
, path_buf
, 4096 );
745 if( addon_get_content_folder( reg
, &folder
, 1 ) ){
746 if( addon_cache_load_request( type
, id
, reg
, folder
) ){
747 vg_async_call( async_addon_setstate
,
748 entry
, k_addon_cache_state_loaded
);
753 vg_warn( "cache item did not load (%u#%u)\n", type
, id
);
754 SDL_AtomicLock( &addon_system
.sl_cache_using_resources
);
755 entry
->state
= k_addon_cache_state_none
;
756 SDL_AtomicUnlock( &addon_system
.sl_cache_using_resources
);
759 SDL_AtomicUnlock( &addon_system
.sl_cache_using_resources
);
765 * Perform the cache interactions required to create a viewslot which will
766 * eventually be loaded by other parts of the system.
768 static u16
addon_cache_create_viewer( enum addon_type type
, u16 reg_id
){
769 struct addon_cache
*cache
= &addon_system
.cache
[type
];
770 vg_pool
*pool
= &cache
->pool
;
772 u16 cache_id
= addon_cache_fetch( type
, reg_id
);
774 cache_id
= addon_cache_alloc( type
, reg_id
);
777 SDL_AtomicLock( &addon_system
.sl_cache_using_resources
);
778 addon_cache_entry
*entry
= vg_pool_item( pool
, cache_id
);
780 if( entry
->state
== k_addon_cache_state_loaded
){
781 addon_cache_free_item( type
, cache_id
);
784 entry
->state
= k_addon_cache_state_load_request
;
785 SDL_AtomicUnlock( &addon_system
.sl_cache_using_resources
);
790 vg_pool_watch( pool
, cache_id
);
795 static u16
addon_cache_create_viewer_from_uid( enum addon_type type
,
796 char uid
[ADDON_UID_MAX
] ){
798 if( !addon_uid_to_alias( uid
, &q
) ) return 0;
799 if( q
.type
!= type
) return 0;
801 u32 reg_id
= addon_match( &q
);
803 if( reg_id
== 0xffffffff ){
804 vg_warn( "We dont have the addon '%s' installed.\n", uid
);
808 return addon_cache_create_viewer( type
, reg_id
);
812 static void addon_cache_watch( enum addon_type type
, u16 cache_id
){
813 if( !cache_id
) return;
815 struct addon_cache
*cache
= &addon_system
.cache
[type
];
816 vg_pool
*pool
= &cache
->pool
;
817 vg_pool_watch( pool
, cache_id
);
820 static void addon_cache_unwatch( enum addon_type type
, u16 cache_id
){
821 if( !cache_id
) return;
823 struct addon_cache
*cache
= &addon_system
.cache
[type
];
824 vg_pool
*pool
= &cache
->pool
;
825 vg_pool_unwatch( pool
, cache_id
);