5 #include "addon_types.h"
10 static u32
addon_count( enum addon_type type
){
11 return addon_system
.registry_type_counts
[ type
];
14 /* these kind of suck, oh well. */
15 static addon_reg
*get_addon_from_index(enum addon_type type
, u32 index
){
17 for( u32 i
=0; count
<addon_count(type
); i
++ ){
18 addon_reg
*reg
= &addon_system
.registry
[i
];
19 if( reg
->alias
.type
== type
){
30 static u32
get_index_from_addon( enum addon_type type
, addon_reg
*a
){
32 for( u32 i
=0; count
<addon_system
.registry_type_counts
[type
]; i
++ ){
33 addon_reg
*reg
= &addon_system
.registry
[i
];
34 if( reg
->alias
.type
== type
){
45 static u32
addon_match( addon_alias
*alias
){
46 if( alias
->type
== k_addon_type_none
) return 0xffffffff;
48 u32 foldername_djb2
= 0;
49 if( !alias
->workshop_id
)
50 foldername_djb2
= vg_strdjb2( alias
->foldername
);
53 for( u32 i
=0; count
<addon_system
.registry_type_counts
[alias
->type
]; i
++ ){
54 addon_reg
*reg
= &addon_system
.registry
[i
];
55 if( reg
->alias
.type
== alias
->type
){
57 if( alias
->workshop_id
){
58 if( alias
->workshop_id
== reg
->alias
.workshop_id
)
62 if( reg
->foldername_hash
== foldername_djb2
){
63 if( !strcmp( reg
->alias
.foldername
, alias
->foldername
) ){
77 * Create a string version of addon alias in buf
79 static void addon_alias_uid( addon_alias
*alias
, char buf
[ADDON_UID_MAX
] ){
80 if( alias
->workshop_id
){
81 snprintf( buf
, 128, "sr%03d-steam-"PRINTF_U64
,
82 alias
->type
, alias
->workshop_id
);
85 snprintf( buf
, 128, "sr%03d-local-%s",
86 alias
->type
, alias
->foldername
);
93 static int addon_alias_eq( addon_alias
*a
, addon_alias
*b
){
94 if( a
->type
== b
->type
){
95 if( a
->workshop_id
== b
->workshop_id
){
99 return !strcmp( a
->foldername
, b
->foldername
);
108 * make alias represent NULL.
110 static void invalidate_addon_alias( addon_alias
*alias
){
111 alias
->type
= k_addon_type_none
;
112 alias
->workshop_id
= 0;
113 alias
->foldername
[0] = '\0';
117 * parse uid to alias. returns 1 if successful
119 static int addon_uid_to_alias( const char *uid
, addon_alias
*alias
){
128 if( strlen(uid
) < 13 ){
129 invalidate_addon_alias( alias
);
132 if( !((uid
[0] == 's') && (uid
[1] == 'r')) ){
133 invalidate_addon_alias( alias
);
138 memcpy( type
, uid
+2, 3 );
140 alias
->type
= atoi(type
);
143 memcpy( location
, uid
+6, 5 );
146 if( !strcmp(location
,"steam") )
147 alias
->workshop_id
= atoll( uid
+12 );
148 else if( !strcmp(location
,"local") ){
149 alias
->workshop_id
= 0;
150 vg_strncpy( uid
+12, alias
->foldername
, 64, k_strncpy_always_add_null
);
153 invalidate_addon_alias( alias
);
160 static void addon_system_init( void ){
161 u32 reg_size
= sizeof(addon_reg
)*ADDON_MOUNTED_MAX
;
162 addon_system
.registry
= vg_linear_alloc( vg_mem
.rtmemory
, reg_size
);
164 for( u32 type
=0; type
<k_addon_type_max
; type
++ ){
165 struct addon_type_info
*inf
= &addon_type_infos
[type
];
166 struct addon_cache
*cache
= &addon_system
.cache
[type
];
168 if( inf
->cache_count
){
169 /* create the allocations pool */
170 u32 alloc_size
= sizeof(struct addon_cache_entry
)*inf
->cache_count
;
171 cache
->allocs
= vg_linear_alloc( vg_mem
.rtmemory
, alloc_size
);
172 memset( cache
->allocs
, 0, alloc_size
);
174 cache
->pool
.buffer
= cache
->allocs
;
175 cache
->pool
.count
= inf
->cache_count
;
176 cache
->pool
.stride
= sizeof( struct addon_cache_entry
);
177 cache
->pool
.offset
= offsetof( struct addon_cache_entry
, poolnode
);
178 vg_pool_init( &cache
->pool
);
180 /* create the real memory */
181 u32 cache_size
= inf
->cache_stride
*inf
->cache_count
;
182 cache
->items
= vg_linear_alloc( vg_mem
.rtmemory
, cache_size
);
183 cache
->stride
= inf
->cache_stride
;
184 memset( cache
->items
, 0, cache_size
);
186 for( i32 j
=0; j
<inf
->cache_count
; j
++ ){
187 struct addon_cache_entry
*alloc
= &cache
->allocs
[j
];
188 alloc
->reg_ptr
= NULL
;
189 alloc
->reg_index
= 0xffffffff;
197 * -----------------------------------------------------------------------------
201 * Reciever for scan completion. copies the registry counts back into main fred
203 static void async_addon_reg_update( void *data
, u32 size
)
205 vg_info( "Registry update notify\n" );
207 for( u32 i
=0; i
<k_addon_type_max
; i
++ ){
208 addon_system
.registry_type_counts
[i
] = 0;
211 for( u32 i
=0; i
<addon_system
.registry_count
; i
++ ){
212 enum addon_type type
= addon_system
.registry
[i
].alias
.type
;
213 addon_system
.registry_type_counts
[ type
] ++;
217 static void addon_set_foldername( addon_reg
*reg
, const char name
[64] ){
218 vg_strncpy( name
, reg
->alias
.foldername
, 64, k_strncpy_always_add_null
);
219 reg
->foldername_hash
= vg_strdjb2( reg
->alias
.foldername
);
223 * Create a new registry
225 static addon_reg
*addon_alloc_reg( PublishedFileId_t workshop_id
,
226 enum addon_type type
){
227 if( addon_system
.registry_count
== ADDON_MOUNTED_MAX
){
228 vg_error( "You have too many addons installed!\n" );
232 addon_reg
*reg
= &addon_system
.registry
[ addon_system
.registry_count
];
233 reg
->metadata_len
= 0;
235 reg
->state
= k_addon_state_indexed
;
236 reg
->alias
.workshop_id
= workshop_id
;
237 reg
->alias
.foldername
[0] = '\0';
238 reg
->alias
.type
= type
;
242 snprintf( foldername
, 64, PRINTF_U64
, workshop_id
);
243 addon_set_foldername( reg
, foldername
);
249 * If the addon.inf exists int the folder, load into the reg
251 static int addon_try_load_metadata( addon_reg
*reg
, vg_str folder_path
){
252 vg_str meta_path
= folder_path
;
253 vg_strcat( &meta_path
, "/addon.inf" );
254 if( !vg_strgood( &meta_path
) ){
255 vg_error( "The metadata path is too long\n" );
259 FILE *fp
= fopen( meta_path
.buffer
, "rb" );
261 vg_error( "Could not open the '%s'\n", meta_path
.buffer
);
265 reg
->metadata_len
= fread( reg
->metadata
, 1, 512, fp
);
266 if( reg
->metadata_len
!= 512 ){
269 vg_error( "unknown error codition" );
270 reg
->metadata_len
= 0;
278 static void addon_print_info( addon_reg
*reg
){
279 vg_info( "addon_reg #%u{\n", addon_system
.registry_count
);
280 vg_info( " type: %d\n", reg
->alias
.type
);
281 vg_info( " workshop_id: " PRINTF_U64
"\n", reg
->alias
.workshop_id
);
282 vg_info( " folder: [%u]%s\n", reg
->foldername_hash
, reg
->alias
.foldername
);
283 vg_info( " metadata_len: %u\n", reg
->metadata_len
);
284 vg_info( " cache_id: %hu\n", reg
->cache_id
);
288 static void addon_mount_finish( addon_reg
*reg
){
290 addon_print_info( reg
);
292 addon_system
.registry_count
++;
296 * Mount a fully packaged addon, one that certainly has a addon.inf
298 static addon_reg
*addon_mount_workshop_folder( PublishedFileId_t workshop_id
,
301 addon_reg
*reg
= addon_alloc_reg( workshop_id
, k_addon_type_none
);
302 if( !reg
) return NULL
;
304 if( !addon_try_load_metadata( reg
, folder_path
) ){
308 enum addon_type type
= k_addon_type_none
;
310 vg_msg_init( &msg
, reg
->metadata
, reg
->metadata_len
);
312 if( vg_msg_seekframe( &msg
, "workshop" )){
313 type
= vg_msg_getkvu32( &msg
, "type", 0 );
316 if( type
== k_addon_type_none
){
317 vg_error( "Cannot determine addon type\n" );
321 reg
->alias
.type
= type
;
322 addon_mount_finish( reg
);
327 * Mount a local folder. may or may not have addon.inf
329 static addon_reg
*addon_mount_local_addon( const char *folder
,
330 enum addon_type type
,
331 const char *content_ext
)
333 char folder_path_buf
[4096];
335 vg_strnull( &folder_path
, folder_path_buf
, 4096 );
336 vg_strcat( &folder_path
, folder
);
338 const char *folder_name
= vg_strch( &folder_path
, '/' )+1;
339 u32 folder_hash
= vg_strdjb2(folder_name
);
340 for( u32 i
=0; i
<addon_system
.registry_count
; i
++ ){
341 addon_reg
*reg
= &addon_system
.registry
[i
];
343 if( (reg
->alias
.type
== type
) && (reg
->foldername_hash
== folder_hash
) ){
344 if( !strcmp( reg
->alias
.foldername
, folder_name
) ){
345 reg
->state
= k_addon_state_indexed
;
351 addon_reg
*reg
= addon_alloc_reg( 0, type
);
352 if( !reg
) return NULL
;
353 addon_set_foldername( reg
, folder_name
);
354 addon_try_load_metadata( reg
, folder_path
);
356 if( reg
->metadata_len
== 0 ){
357 /* create our own content commands */
359 vg_msg_init( &msg
, reg
->metadata
, sizeof(reg
->metadata
) );
361 u32 content_count
= 0;
363 vg_strcat( &folder_path
, "" );
364 vg_warn( "Creating own metadata for: %s\n", folder_path
.buffer
);
367 if( !vg_dir_open(&subdir
, folder_path
.buffer
) ){
368 vg_error( "Failed to open '%s'\n", folder_path
.buffer
);
372 while( vg_dir_next_entry(&subdir
) ){
373 if( vg_dir_entry_type(&subdir
) == k_vg_entry_type_file
){
374 const char *fname
= vg_dir_entry_name(&subdir
);
375 vg_str file
= folder_path
;
376 vg_strcat( &file
, "/" );
377 vg_strcat( &file
, fname
);
378 if( !vg_strgood( &file
) ) continue;
380 char *ext
= vg_strch( &file
, '.' );
382 if( strcmp(ext
,content_ext
) ) continue;
384 vg_msg_wkvstr( &msg
, "content", fname
);
388 vg_dir_close(&subdir
);
390 if( !content_count
) return NULL
;
391 if( msg
.error
== k_vg_msg_error_OK
)
392 reg
->metadata_len
= msg
.cur
.co
;
394 vg_error( "Error creating metadata: %d\n", msg
.error
);
399 addon_mount_finish( reg
);
404 * Check all subscribed items
406 static void addon_mount_workshop_items(void){
407 if( !steam_ready
) return;
409 * Steam workshop scan
411 vg_info( "Mounting steam workshop subscriptions\n" );
412 PublishedFileId_t workshop_ids
[ ADDON_MOUNTED_MAX
];
413 u32 workshop_count
= ADDON_MOUNTED_MAX
;
415 vg_async_item
*call
= vg_async_alloc(
416 sizeof(struct async_workshop_installed_files_info
));
417 struct async_workshop_installed_files_info
*info
= call
->payload
;
418 info
->buffer
= workshop_ids
;
419 info
->len
= &workshop_count
;
420 vg_async_dispatch( call
, async_workshop_get_installed_files
);
423 for( u32 j
=0; j
<workshop_count
; j
++ ){
424 /* check for existance in both our caches
425 * ----------------------------------------------------------*/
426 PublishedFileId_t id
= workshop_ids
[j
];
427 for( u32 i
=0; i
<addon_system
.registry_count
; i
++ ){
428 addon_reg
*reg
= &addon_system
.registry
[i
];
430 if( reg
->alias
.workshop_id
== id
){
431 reg
->state
= k_addon_state_indexed
;
432 goto next_file_workshop
;
436 vg_async_item
*call1
=
437 vg_async_alloc( sizeof(struct async_workshop_filepath_info
) );
441 struct async_workshop_filepath_info
*info
= call1
->payload
;
444 info
->len
= vg_list_size(path
);
445 vg_async_dispatch( call1
, async_workshop_get_filepath
);
446 vg_async_stall(); /* too bad! */
448 vg_str folder
= {.buffer
= path
, .i
=strlen(path
), .len
=4096};
449 addon_mount_workshop_folder( id
, folder
);
455 * Scan a local content folder for addons. It must find at least one file with
456 * the specified content_ext to be considered.
458 static void addon_mount_content_folder( enum addon_type type
,
459 const char *base_folder
,
460 const char *content_ext
)
462 vg_info( "Mounting addons(type:%d) matching skaterift/%s/*/*%s\n",
463 type
, base_folder
, content_ext
);
467 vg_strnull( &path
, path_buf
, 4096 );
468 vg_strcat( &path
, base_folder
);
471 if( !vg_dir_open(&dir
,path
.buffer
) ){
472 vg_error( "vg_dir_open('%s') failed\n", path
.buffer
);
476 vg_strcat(&path
,"/");
478 while( vg_dir_next_entry(&dir
) ){
479 if( vg_dir_entry_type(&dir
) == k_vg_entry_type_dir
){
480 const char *d_name
= vg_dir_entry_name(&dir
);
482 vg_str folder
= path
;
483 if( strlen( d_name
) > ADDON_FOLDERNAME_MAX
){
484 vg_warn( "folder too long: %s\n", d_name
);
488 vg_strcat( &folder
, d_name
);
489 if( !vg_strgood( &folder
) ) continue;
491 addon_mount_local_addon( folder
.buffer
, type
, content_ext
);
498 * write the full path of the addon's folder into the vg_str
500 static int addon_get_content_folder( addon_reg
*reg
, vg_str
*folder
){
501 if( reg
->alias
.workshop_id
){
502 vg_async_item
*call
=
503 vg_async_alloc( sizeof(struct async_workshop_filepath_info
) );
504 struct async_workshop_filepath_info
*info
= call
->payload
;
505 info
->buf
= folder
->buffer
;
506 info
->id
= reg
->alias
.workshop_id
;
507 info
->len
= folder
->len
;
508 vg_async_dispatch( call
, async_workshop_get_filepath
);
509 vg_async_stall(); /* too bad! */
510 if( info
->buf
[0] == '\0' ){
511 vg_error( "Failed SteamAPI_GetItemInstallInfo(" PRINTF_U64
")\n",
512 reg
->alias
.workshop_id
);
515 folder
->i
= strlen( folder
->buffer
);
521 const char *local_folder
=
522 addon_type_infos
[reg
->alias
.type
].local_content_folder
;
524 if( !local_folder
) return 0;
525 vg_strcat( folder
, local_folder
);
526 vg_strcat( folder
, reg
->alias
.foldername
);
532 * Return existing cache id if reg_index points to a registry with its cache
535 static u16
addon_cache_fetch( enum addon_type type
, u32 reg_index
){
536 addon_reg
*reg
= NULL
;
538 if( reg_index
< addon_count( type
) ){
539 reg
= get_addon_from_index( type
, reg_index
);
541 return reg
->cache_id
;
548 * Allocate a new cache item from the pool
550 static u16
addon_cache_alloc( enum addon_type type
, u32 reg_index
){
551 struct addon_cache
*cache
= &addon_system
.cache
[ type
];
553 u16 new_id
= vg_pool_lru( &cache
->pool
);
554 struct addon_cache_entry
*new_entry
= vg_pool_item( &cache
->pool
, new_id
);
556 addon_reg
*reg
= NULL
;
557 if( reg_index
< addon_count( type
) )
558 reg
= get_addon_from_index( type
, reg_index
);
561 if( new_entry
->reg_ptr
)
562 new_entry
->reg_ptr
->cache_id
= 0;
565 reg
->cache_id
= new_id
;
567 new_entry
->reg_ptr
= reg
;
568 new_entry
->reg_index
= reg_index
;
572 vg_error( "cache full (type: %u)!\n", type
);
578 * Get the real item data for cache id
580 static void *addon_cache_item( enum addon_type type
, u16 id
){
581 if( !id
) return NULL
;
583 struct addon_cache
*cache
= &addon_system
.cache
[type
];
584 return cache
->items
+ ((size_t)(id
-1) * cache
->stride
);
588 * Get the real item data for cache id ONLY if the item is completely loaded.
590 static void *addon_cache_item_if_loaded( enum addon_type type
, u16 id
){
591 if( !id
) return NULL
;
593 struct addon_cache
*cache
= &addon_system
.cache
[type
];
594 struct addon_cache_entry
*entry
= vg_pool_item( &cache
->pool
, id
);
596 if( entry
->state
== k_addon_cache_state_loaded
)
597 return addon_cache_item( type
, id
);
602 * Updates the item state from the main thread
604 static void async_addon_setstate( void *_entry
, u32 _state
){
605 addon_cache_entry
*entry
= _entry
;
606 SDL_AtomicLock( &addon_system
.sl_cache_using_resources
);
607 entry
->state
= _state
;
608 SDL_AtomicUnlock( &addon_system
.sl_cache_using_resources
);
609 vg_success( " loaded (%s)\n", entry
->reg_ptr
->alias
.foldername
);
613 * Handles the loading of an individual item
615 static int addon_cache_load_request( enum addon_type type
, u16 id
,
616 addon_reg
*reg
, vg_str folder
){
618 /* load content files
619 * --------------------------------- */
620 vg_str content_path
= folder
;
623 vg_msg_init( &msg
, reg
->metadata
, reg
->metadata_len
);
625 const char *kv_content
= vg_msg_getkvstr( &msg
, "content" );
627 vg_strcat( &content_path
, "/" );
628 vg_strcat( &content_path
, kv_content
);
631 vg_error( " No content paths in metadata\n" );
635 if( !vg_strgood( &content_path
) ) {
636 vg_error( " Metadata path too long\n" );
640 if( type
== k_addon_type_board
){
641 struct player_board
*board
= addon_cache_item( type
, id
);
642 player_board_load( board
, content_path
.buffer
);
645 else if( type
== k_addon_type_player
){
646 struct player_model
*model
= addon_cache_item( type
, id
);
647 player_model_load( model
, content_path
.buffer
);
657 static void addon_cache_free_item( enum addon_type type
, u16 id
){
658 if( type
== k_addon_type_board
){
659 struct player_board
*board
= addon_cache_item( type
, id
);
660 player_board_unload( board
);
662 else if( type
== k_addon_type_player
){
663 struct player_model
*model
= addon_cache_item( type
, id
);
664 player_model_unload( model
);
669 * Goes over cache item load requests and calls the above ^
671 static void addon_cache_load_loop(void){
672 vg_info( "Running load loop\n" );
675 for( u32 type
=0; type
<k_addon_type_max
; type
++ ){
676 struct addon_cache
*cache
= &addon_system
.cache
[type
];
678 for( u32 id
=1; id
<=cache
->pool
.count
; id
++ ){
679 addon_cache_entry
*entry
= vg_pool_item( &cache
->pool
, id
);
681 SDL_AtomicLock( &addon_system
.sl_cache_using_resources
);
682 if( entry
->state
== k_addon_cache_state_load_request
){
683 vg_info( "process cache load request (%u#%u, reg:%u)\n",
684 type
, id
, entry
->reg_index
);
686 if( entry
->reg_index
>= addon_count(type
) ){
687 /* should maybe have a different value for this case */
688 entry
->state
= k_addon_cache_state_none
;
689 SDL_AtomicUnlock( &addon_system
.sl_cache_using_resources
);
693 SDL_AtomicUnlock( &addon_system
.sl_cache_using_resources
);
695 /* continue with the request */
696 addon_reg
*reg
= get_addon_from_index( type
, entry
->reg_index
);
697 entry
->reg_ptr
= reg
;
700 vg_strnull( &folder
, path_buf
, 4096 );
701 if( addon_get_content_folder( reg
, &folder
) ){
702 if( addon_cache_load_request( type
, id
, reg
, folder
) ){
703 vg_async_call( async_addon_setstate
,
704 entry
, k_addon_cache_state_loaded
);
709 vg_warn( "cache item did not load (%u#%u)\n", type
, id
);
710 SDL_AtomicLock( &addon_system
.sl_cache_using_resources
);
711 entry
->state
= k_addon_cache_state_none
;
712 SDL_AtomicUnlock( &addon_system
.sl_cache_using_resources
);
715 SDL_AtomicUnlock( &addon_system
.sl_cache_using_resources
);
721 * Perform the cache interactions required to create a viewslot which will
722 * eventually be loaded by other parts of the system.
724 static u16
addon_cache_create_viewer( enum addon_type type
, u16 reg_id
){
725 struct addon_cache
*cache
= &addon_system
.cache
[type
];
726 vg_pool
*pool
= &cache
->pool
;
728 u16 cache_id
= addon_cache_fetch( type
, reg_id
);
730 cache_id
= addon_cache_alloc( type
, reg_id
);
733 SDL_AtomicLock( &addon_system
.sl_cache_using_resources
);
734 addon_cache_entry
*entry
= vg_pool_item( pool
, cache_id
);
736 if( entry
->state
== k_addon_cache_state_loaded
){
737 addon_cache_free_item( type
, cache_id
);
740 entry
->state
= k_addon_cache_state_load_request
;
741 SDL_AtomicUnlock( &addon_system
.sl_cache_using_resources
);
746 vg_pool_watch( pool
, cache_id
);
751 static u16
addon_cache_create_viewer_from_uid( enum addon_type type
,
752 char uid
[ADDON_UID_MAX
] ){
754 if( !addon_uid_to_alias( uid
, &q
) ) return 0;
755 if( q
.type
!= type
) return 0;
757 u32 reg_id
= addon_match( &q
);
759 if( reg_id
== 0xffffffff ){
760 vg_warn( "We dont have the addon '%s' installed.\n", uid
);
764 return addon_cache_create_viewer( type
, reg_id
);
768 static void addon_cache_watch( enum addon_type type
, u16 cache_id
){
769 if( !cache_id
) return;
771 struct addon_cache
*cache
= &addon_system
.cache
[type
];
772 vg_pool
*pool
= &cache
->pool
;
773 vg_pool_watch( pool
, cache_id
);
776 static void addon_cache_unwatch( enum addon_type type
, u16 cache_id
){
777 if( !cache_id
) return;
779 struct addon_cache
*cache
= &addon_system
.cache
[type
];
780 vg_pool
*pool
= &cache
->pool
;
781 vg_pool_unwatch( pool
, cache_id
);