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( !steam_ready
) return;
434 * Steam workshop scan
436 vg_info( "Mounting steam workshop subscriptions\n" );
437 PublishedFileId_t workshop_ids
[ ADDON_MOUNTED_MAX
];
438 u32 workshop_count
= ADDON_MOUNTED_MAX
;
440 vg_async_item
*call
= vg_async_alloc(
441 sizeof(struct async_workshop_installed_files_info
));
442 struct async_workshop_installed_files_info
*info
= call
->payload
;
443 info
->buffer
= workshop_ids
;
444 info
->len
= &workshop_count
;
445 vg_async_dispatch( call
, async_workshop_get_installed_files
);
448 for( u32 j
=0; j
<workshop_count
; j
++ ){
449 /* check for existance in both our caches
450 * ----------------------------------------------------------*/
451 PublishedFileId_t id
= workshop_ids
[j
];
452 for( u32 i
=0; i
<addon_system
.registry_count
; i
++ ){
453 addon_reg
*reg
= &addon_system
.registry
[i
];
455 if( reg
->alias
.workshop_id
== id
){
456 reg
->state
= k_addon_state_indexed
;
457 goto next_file_workshop
;
461 vg_async_item
*call1
=
462 vg_async_alloc( sizeof(struct async_workshop_filepath_info
) );
466 struct async_workshop_filepath_info
*info
= call1
->payload
;
469 info
->len
= vg_list_size(path
);
470 vg_async_dispatch( call1
, async_workshop_get_filepath
);
471 vg_async_stall(); /* too bad! */
473 vg_str folder
= {.buffer
= path
, .i
=strlen(path
), .len
=4096};
474 addon_mount_workshop_folder( id
, folder
);
480 * Scan a local content folder for addons. It must find at least one file with
481 * the specified content_ext to be considered.
483 static void addon_mount_content_folder( enum addon_type type
,
484 const char *base_folder
,
485 const char *content_ext
)
487 vg_info( "Mounting addons(type:%d) matching skaterift/%s/*/*%s\n",
488 type
, base_folder
, content_ext
);
492 vg_strnull( &path
, path_buf
, 4096 );
493 vg_strcat( &path
, base_folder
);
496 if( !vg_dir_open(&dir
,path
.buffer
) ){
497 vg_error( "vg_dir_open('%s') failed\n", path
.buffer
);
501 vg_strcat(&path
,"/");
503 while( vg_dir_next_entry(&dir
) ){
504 if( vg_dir_entry_type(&dir
) == k_vg_entry_type_dir
){
505 const char *d_name
= vg_dir_entry_name(&dir
);
507 vg_str folder
= path
;
508 if( strlen( d_name
) > ADDON_FOLDERNAME_MAX
){
509 vg_warn( "folder too long: %s\n", d_name
);
513 vg_strcat( &folder
, d_name
);
514 if( !vg_strgood( &folder
) ) continue;
516 addon_mount_local_addon( folder
.buffer
, type
, content_ext
);
523 * write the full path of the addon's folder into the vg_str
525 static int addon_get_content_folder( addon_reg
*reg
, vg_str
*folder
, int async
){
526 if( reg
->alias
.workshop_id
){
527 struct async_workshop_filepath_info
*info
= NULL
;
528 vg_async_item
*call
= NULL
;
531 call
= vg_async_alloc( sizeof(struct async_workshop_filepath_info
) );
532 info
= call
->payload
;
535 info
= alloca( sizeof(struct async_workshop_filepath_info
) );
537 info
->buf
= folder
->buffer
;
538 info
->id
= reg
->alias
.workshop_id
;
539 info
->len
= folder
->len
;
542 vg_async_dispatch( call
, async_workshop_get_filepath
);
543 vg_async_stall(); /* too bad! */
546 async_workshop_get_filepath( info
, 0 );
549 if( info
->buf
[0] == '\0' ){
550 vg_error( "Failed SteamAPI_GetItemInstallInfo(" PRINTF_U64
")\n",
551 reg
->alias
.workshop_id
);
554 folder
->i
= strlen( folder
->buffer
);
560 const char *local_folder
=
561 addon_type_infos
[reg
->alias
.type
].local_content_folder
;
563 if( !local_folder
) return 0;
564 vg_strcat( folder
, local_folder
);
565 vg_strcat( folder
, reg
->alias
.foldername
);
571 * Return existing cache id if reg_index points to a registry with its cache
574 static u16
addon_cache_fetch( enum addon_type type
, u32 reg_index
){
575 addon_reg
*reg
= NULL
;
577 if( reg_index
< addon_count( type
, 0 ) ){
578 reg
= get_addon_from_index( type
, reg_index
, 0 );
580 return reg
->cache_id
;
587 * Allocate a new cache item from the pool
589 static u16
addon_cache_alloc( enum addon_type type
, u32 reg_index
){
590 struct addon_cache
*cache
= &addon_system
.cache
[ type
];
592 u16 new_id
= vg_pool_lru( &cache
->pool
);
593 struct addon_cache_entry
*new_entry
= vg_pool_item( &cache
->pool
, new_id
);
595 addon_reg
*reg
= NULL
;
596 if( reg_index
< addon_count( type
, 0 ) )
597 reg
= get_addon_from_index( type
, reg_index
, 0 );
600 if( new_entry
->reg_ptr
)
601 new_entry
->reg_ptr
->cache_id
= 0;
604 reg
->cache_id
= new_id
;
606 new_entry
->reg_ptr
= reg
;
607 new_entry
->reg_index
= reg_index
;
611 vg_error( "cache full (type: %u)!\n", type
);
617 * Get the real item data for cache id
619 static void *addon_cache_item( enum addon_type type
, u16 id
){
620 if( !id
) return NULL
;
622 struct addon_cache
*cache
= &addon_system
.cache
[type
];
623 return cache
->items
+ ((size_t)(id
-1) * cache
->stride
);
627 * Get the real item data for cache id ONLY if the item is completely loaded.
629 static void *addon_cache_item_if_loaded( enum addon_type type
, u16 id
){
630 if( !id
) return NULL
;
632 struct addon_cache
*cache
= &addon_system
.cache
[type
];
633 struct addon_cache_entry
*entry
= vg_pool_item( &cache
->pool
, id
);
635 if( entry
->state
== k_addon_cache_state_loaded
)
636 return addon_cache_item( type
, id
);
641 * Updates the item state from the main thread
643 static void async_addon_setstate( void *_entry
, u32 _state
){
644 addon_cache_entry
*entry
= _entry
;
645 SDL_AtomicLock( &addon_system
.sl_cache_using_resources
);
646 entry
->state
= _state
;
647 SDL_AtomicUnlock( &addon_system
.sl_cache_using_resources
);
648 vg_success( " loaded (%s)\n", entry
->reg_ptr
->alias
.foldername
);
652 * Handles the loading of an individual item
654 static int addon_cache_load_request( enum addon_type type
, u16 id
,
655 addon_reg
*reg
, vg_str folder
){
657 /* load content files
658 * --------------------------------- */
659 vg_str content_path
= folder
;
662 vg_msg_init( &msg
, reg
->metadata
, reg
->metadata_len
);
664 const char *kv_content
= vg_msg_getkvstr( &msg
, "content" );
666 vg_strcat( &content_path
, "/" );
667 vg_strcat( &content_path
, kv_content
);
670 vg_error( " No content paths in metadata\n" );
674 if( !vg_strgood( &content_path
) ) {
675 vg_error( " Metadata path too long\n" );
679 if( type
== k_addon_type_board
){
680 struct player_board
*board
= addon_cache_item( type
, id
);
681 player_board_load( board
, content_path
.buffer
);
684 else if( type
== k_addon_type_player
){
685 struct player_model
*model
= addon_cache_item( type
, id
);
686 player_model_load( model
, content_path
.buffer
);
696 static void addon_cache_free_item( enum addon_type type
, u16 id
){
697 if( type
== k_addon_type_board
){
698 struct player_board
*board
= addon_cache_item( type
, id
);
699 player_board_unload( board
);
701 else if( type
== k_addon_type_player
){
702 struct player_model
*model
= addon_cache_item( type
, id
);
703 player_model_unload( model
);
708 * Goes over cache item load requests and calls the above ^
710 static void addon_cache_load_loop(void){
711 vg_info( "Running load loop\n" );
714 for( u32 type
=0; type
<k_addon_type_max
; type
++ ){
715 struct addon_cache
*cache
= &addon_system
.cache
[type
];
717 for( u32 id
=1; id
<=cache
->pool
.count
; id
++ ){
718 addon_cache_entry
*entry
= vg_pool_item( &cache
->pool
, id
);
720 SDL_AtomicLock( &addon_system
.sl_cache_using_resources
);
721 if( entry
->state
== k_addon_cache_state_load_request
){
722 vg_info( "process cache load request (%u#%u, reg:%u)\n",
723 type
, id
, entry
->reg_index
);
725 if( entry
->reg_index
>= addon_count(type
,0) ){
726 /* should maybe have a different value for this case */
727 entry
->state
= k_addon_cache_state_none
;
728 SDL_AtomicUnlock( &addon_system
.sl_cache_using_resources
);
732 SDL_AtomicUnlock( &addon_system
.sl_cache_using_resources
);
734 /* continue with the request */
735 addon_reg
*reg
= get_addon_from_index( type
, entry
->reg_index
, 0 );
736 entry
->reg_ptr
= reg
;
739 vg_strnull( &folder
, path_buf
, 4096 );
740 if( addon_get_content_folder( reg
, &folder
, 1 ) ){
741 if( addon_cache_load_request( type
, id
, reg
, folder
) ){
742 vg_async_call( async_addon_setstate
,
743 entry
, k_addon_cache_state_loaded
);
748 vg_warn( "cache item did not load (%u#%u)\n", type
, id
);
749 SDL_AtomicLock( &addon_system
.sl_cache_using_resources
);
750 entry
->state
= k_addon_cache_state_none
;
751 SDL_AtomicUnlock( &addon_system
.sl_cache_using_resources
);
754 SDL_AtomicUnlock( &addon_system
.sl_cache_using_resources
);
760 * Perform the cache interactions required to create a viewslot which will
761 * eventually be loaded by other parts of the system.
763 static u16
addon_cache_create_viewer( enum addon_type type
, u16 reg_id
){
764 struct addon_cache
*cache
= &addon_system
.cache
[type
];
765 vg_pool
*pool
= &cache
->pool
;
767 u16 cache_id
= addon_cache_fetch( type
, reg_id
);
769 cache_id
= addon_cache_alloc( type
, reg_id
);
772 SDL_AtomicLock( &addon_system
.sl_cache_using_resources
);
773 addon_cache_entry
*entry
= vg_pool_item( pool
, cache_id
);
775 if( entry
->state
== k_addon_cache_state_loaded
){
776 addon_cache_free_item( type
, cache_id
);
779 entry
->state
= k_addon_cache_state_load_request
;
780 SDL_AtomicUnlock( &addon_system
.sl_cache_using_resources
);
785 vg_pool_watch( pool
, cache_id
);
790 static u16
addon_cache_create_viewer_from_uid( enum addon_type type
,
791 char uid
[ADDON_UID_MAX
] ){
793 if( !addon_uid_to_alias( uid
, &q
) ) return 0;
794 if( q
.type
!= type
) return 0;
796 u32 reg_id
= addon_match( &q
);
798 if( reg_id
== 0xffffffff ){
799 vg_warn( "We dont have the addon '%s' installed.\n", uid
);
803 return addon_cache_create_viewer( type
, reg_id
);
807 static void addon_cache_watch( enum addon_type type
, u16 cache_id
){
808 if( !cache_id
) return;
810 struct addon_cache
*cache
= &addon_system
.cache
[type
];
811 vg_pool
*pool
= &cache
->pool
;
812 vg_pool_watch( pool
, cache_id
);
815 static void addon_cache_unwatch( enum addon_type type
, u16 cache_id
){
816 if( !cache_id
) return;
818 struct addon_cache
*cache
= &addon_system
.cache
[type
];
819 vg_pool
*pool
= &cache
->pool
;
820 vg_pool_unwatch( pool
, cache_id
);