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 u32 foldername_djb2
= vg_strdjb2( alias
->foldername
);
49 for( u32 i
=0; count
<addon_system
.registry_type_counts
[alias
->type
]; i
++ ){
50 addon_reg
*reg
= &addon_system
.registry
[i
];
51 if( reg
->alias
.type
== alias
->type
){
53 if( alias
->workshop_id
){
54 if( alias
->workshop_id
== reg
->alias
.workshop_id
)
58 if( reg
->foldername_hash
== foldername_djb2
){
59 if( !strcmp( reg
->alias
.foldername
, alias
->foldername
) ){
72 static void addon_alias_uid( addon_alias
*alias
, char buf
[76] ){
73 if( alias
->workshop_id
){
74 snprintf( buf
, 128, "sr%03d-steam-"PRINTF_U64
,
75 alias
->type
, alias
->workshop_id
);
78 snprintf( buf
, 128, "sr%03d-local-%s",
79 alias
->type
, alias
->foldername
);
83 static void addon_system_init( void ){
84 u32 reg_size
= sizeof(addon_reg
)*ADDON_MOUNTED_MAX
;
85 addon_system
.registry
= vg_linear_alloc( vg_mem
.rtmemory
, reg_size
);
87 for( u32 type
=0; type
<k_addon_type_max
; type
++ ){
88 struct addon_type_info
*inf
= &addon_type_infos
[type
];
89 struct addon_cache
*cache
= &addon_system
.cache
[type
];
91 if( inf
->cache_count
){
92 /* create the allocations pool */
93 u32 alloc_size
= sizeof(struct addon_cache_entry
)*inf
->cache_count
;
94 cache
->allocs
= vg_linear_alloc( vg_mem
.rtmemory
, alloc_size
);
95 memset( cache
->allocs
, 0, alloc_size
);
97 cache
->pool
.buffer
= cache
->allocs
;
98 cache
->pool
.count
= inf
->cache_count
;
99 cache
->pool
.stride
= sizeof( struct addon_cache_entry
);
100 cache
->pool
.offset
= offsetof( struct addon_cache_entry
, poolnode
);
101 vg_pool_init( &cache
->pool
);
103 /* create the real memory */
104 u32 cache_size
= inf
->cache_stride
*inf
->cache_count
;
105 cache
->items
= vg_linear_alloc( vg_mem
.rtmemory
, cache_size
);
106 cache
->stride
= inf
->cache_stride
;
107 memset( cache
->items
, 0, cache_size
);
109 for( i32 j
=0; j
<inf
->cache_count
; j
++ ){
110 struct addon_cache_entry
*alloc
= &cache
->allocs
[j
];
111 alloc
->reg_ptr
= NULL
;
112 alloc
->reg_index
= 0xffffffff;
120 * -----------------------------------------------------------------------------
124 * Reciever for scan completion. copies the registry counts back into main fred
126 VG_STATIC
void async_addon_reg_update( void *data
, u32 size
)
128 vg_info( "Registry update notify\n" );
130 for( u32 i
=0; i
<k_addon_type_max
; i
++ ){
131 addon_system
.registry_type_counts
[i
] = 0;
134 for( u32 i
=0; i
<addon_system
.registry_count
; i
++ ){
135 enum addon_type type
= addon_system
.registry
[i
].alias
.type
;
136 addon_system
.registry_type_counts
[ type
] ++;
140 VG_STATIC
void addon_set_foldername( addon_reg
*reg
, const char name
[64] ){
141 vg_strncpy( name
, reg
->alias
.foldername
, 64, k_strncpy_always_add_null
);
142 reg
->foldername_hash
= vg_strdjb2( reg
->alias
.foldername
);
146 * Create a new registry
148 VG_STATIC addon_reg
*addon_alloc_reg( PublishedFileId_t workshop_id
,
149 enum addon_type type
){
150 if( addon_system
.registry_count
== ADDON_MOUNTED_MAX
){
151 vg_error( "You have too many addons installed!\n" );
155 addon_reg
*reg
= &addon_system
.registry
[ addon_system
.registry_count
];
156 reg
->metadata_len
= 0;
158 reg
->state
= k_addon_state_indexed
;
159 reg
->alias
.workshop_id
= workshop_id
;
160 reg
->alias
.foldername
[0] = '\0';
161 reg
->alias
.type
= type
;
165 snprintf( foldername
, 64, PRINTF_U64
, workshop_id
);
166 addon_set_foldername( reg
, foldername
);
172 * If the addon.inf exists int the folder, load into the reg
174 VG_STATIC
int addon_try_load_metadata( addon_reg
*reg
, vg_str folder_path
){
175 vg_str meta_path
= folder_path
;
176 vg_strcat( &meta_path
, "/addon.inf" );
177 if( !vg_strgood( &meta_path
) ){
178 vg_error( "The metadata path is too long\n" );
182 FILE *fp
= fopen( meta_path
.buffer
, "rb" );
184 vg_error( "Could not open the '%s'\n", meta_path
.buffer
);
188 reg
->metadata_len
= fread( reg
->metadata
, 1, 512, fp
);
189 if( reg
->metadata_len
!= 512 ){
192 vg_error( "unknown error codition" );
193 reg
->metadata_len
= 0;
201 VG_STATIC
void addon_print_info( addon_reg
*reg
){
202 vg_info( "addon_reg #%u{\n", addon_system
.registry_count
);
203 vg_info( " type: %d\n", reg
->alias
.type
);
204 vg_info( " workshop_id: " PRINTF_U64
"\n", reg
->alias
.workshop_id
);
205 vg_info( " folder: [%u]%s\n", reg
->foldername_hash
, reg
->alias
.foldername
);
206 vg_info( " metadata_len: %u\n", reg
->metadata_len
);
207 vg_info( " cache_id: %hu\n", reg
->cache_id
);
211 VG_STATIC
void addon_mount_finish( addon_reg
*reg
){
213 addon_print_info( reg
);
215 addon_system
.registry_count
++;
219 * Mount a fully packaged addon, one that certainly has a addon.inf
221 VG_STATIC addon_reg
*addon_mount_workshop_folder( PublishedFileId_t workshop_id
,
224 addon_reg
*reg
= addon_alloc_reg( workshop_id
, k_addon_type_none
);
225 if( !reg
) return NULL
;
227 if( !addon_try_load_metadata( reg
, folder_path
) ){
231 enum addon_type type
= k_addon_type_none
;
233 root
.buf
= reg
->metadata
;
234 root
.len
= reg
->metadata_len
;
235 root
.max
= sizeof(reg
->metadata
);
237 vg_msg workshop
= root
;
238 if( vg_msg_seekframe( &workshop
, "workshop", k_vg_msg_first
)){
239 type
= vg_msg_seekkvu32( &workshop
, "type", k_vg_msg_first
);
242 if( type
== k_addon_type_none
){
243 vg_error( "Cannot determine addon type\n" );
247 reg
->alias
.type
= type
;
248 addon_mount_finish( reg
);
253 * Mount a local folder. may or may not have addon.inf
255 VG_STATIC addon_reg
*addon_mount_local_addon( const char *folder
,
256 enum addon_type type
,
257 const char *content_ext
)
259 char folder_path_buf
[4096];
261 vg_strnull( &folder_path
, folder_path_buf
, 4096 );
262 vg_strcat( &folder_path
, folder
);
264 const char *folder_name
= vg_strch( &folder_path
, '/' )+1;
265 u32 folder_hash
= vg_strdjb2(folder_name
);
266 for( u32 i
=0; i
<addon_system
.registry_count
; i
++ ){
267 addon_reg
*reg
= &addon_system
.registry
[i
];
269 if( (reg
->alias
.type
== type
) && (reg
->foldername_hash
== folder_hash
) ){
270 if( !strcmp( reg
->alias
.foldername
, folder_name
) ){
271 reg
->state
= k_addon_state_indexed
;
277 addon_reg
*reg
= addon_alloc_reg( 0, type
);
278 if( !reg
) return NULL
;
279 addon_set_foldername( reg
, folder_name
);
280 addon_try_load_metadata( reg
, folder_path
);
282 if( reg
->metadata_len
== 0 ){
283 /* create our own content commands */
285 msg
.buf
= reg
->metadata
;
287 msg
.max
= sizeof(reg
->metadata
);
289 u32 content_count
= 0;
291 vg_strcat( &folder_path
, "" );
292 vg_warn( "Creating own metadata for: %s\n", folder_path
.buffer
);
295 if( !vg_dir_open(&subdir
, folder_path
.buffer
) ){
296 vg_error( "Failed to open '%s'\n", folder_path
.buffer
);
300 while( vg_dir_next_entry(&subdir
) ){
301 if( vg_dir_entry_type(&subdir
) == k_vg_entry_type_file
){
302 const char *fname
= vg_dir_entry_name(&subdir
);
303 vg_str file
= folder_path
;
304 vg_strcat( &file
, "/" );
305 vg_strcat( &file
, fname
);
306 if( !vg_strgood( &file
) ) continue;
308 char *ext
= vg_strch( &file
, '.' );
310 if( strcmp(ext
,content_ext
) ) continue;
312 vg_msg_wkvstr( &msg
, "content", fname
);
316 vg_dir_close(&subdir
);
318 if( !content_count
) return NULL
;
319 if( msg
.error
== k_vg_msg_error_OK
)
320 reg
->metadata_len
= msg
.cur
;
322 vg_error( "Error creating metadata: %d\n", msg
.error
);
327 addon_mount_finish( reg
);
332 * Check all subscribed items
334 VG_STATIC
void addon_mount_workshop_items(void){
335 if( !steam_ready
) return;
337 * Steam workshop scan
339 vg_info( "Mounting steam workshop subscriptions\n" );
340 PublishedFileId_t workshop_ids
[ ADDON_MOUNTED_MAX
];
341 u32 workshop_count
= ADDON_MOUNTED_MAX
;
343 vg_async_item
*call
= vg_async_alloc(
344 sizeof(struct async_workshop_installed_files_info
));
345 struct async_workshop_installed_files_info
*info
= call
->payload
;
346 info
->buffer
= workshop_ids
;
347 info
->len
= &workshop_count
;
348 vg_async_dispatch( call
, async_workshop_get_installed_files
);
351 for( u32 j
=0; j
<workshop_count
; j
++ ){
352 /* check for existance in both our caches
353 * ----------------------------------------------------------*/
354 PublishedFileId_t id
= workshop_ids
[j
];
355 for( u32 i
=0; i
<addon_system
.registry_count
; i
++ ){
356 addon_reg
*reg
= &addon_system
.registry
[i
];
358 if( reg
->alias
.workshop_id
== id
){
359 reg
->state
= k_addon_state_indexed
;
360 goto next_file_workshop
;
364 vg_async_item
*call1
=
365 vg_async_alloc( sizeof(struct async_workshop_filepath_info
) );
369 struct async_workshop_filepath_info
*info
= call1
->payload
;
372 info
->len
= vg_list_size(path
);
373 vg_async_dispatch( call1
, async_workshop_get_filepath
);
374 vg_async_stall(); /* too bad! */
376 vg_str folder
= {.buffer
= path
, .i
=strlen(path
), .len
=4096};
377 addon_mount_workshop_folder( id
, folder
);
383 * Scan a local content folder for addons. It must find at least one file with
384 * the specified content_ext to be considered.
386 VG_STATIC
void addon_mount_content_folder( enum addon_type type
,
387 const char *base_folder
,
388 const char *content_ext
)
390 vg_info( "Mounting addons(type:%d) matching skaterift/%s/*/*%s\n",
391 type
, base_folder
, content_ext
);
395 vg_strnull( &path
, path_buf
, 4096 );
396 vg_strcat( &path
, base_folder
);
399 if( !vg_dir_open(&dir
,path
.buffer
) ){
400 vg_error( "vg_dir_open('%s') failed\n", path
.buffer
);
404 vg_strcat(&path
,"/");
406 while( vg_dir_next_entry(&dir
) ){
407 if( vg_dir_entry_type(&dir
) == k_vg_entry_type_dir
){
408 const char *d_name
= vg_dir_entry_name(&dir
);
410 vg_str folder
= path
;
411 if( strlen( d_name
) > ADDON_FOLDERNAME_MAX
){
412 vg_warn( "folder too long: %s\n", d_name
);
416 vg_strcat( &folder
, d_name
);
417 if( !vg_strgood( &folder
) ) continue;
419 addon_mount_local_addon( folder
.buffer
, type
, content_ext
);
426 * write the full path of the addon's folder into the vg_str
428 static int addon_get_content_folder( addon_reg
*reg
, vg_str
*folder
){
429 if( reg
->alias
.workshop_id
){
430 vg_async_item
*call
=
431 vg_async_alloc( sizeof(struct async_workshop_filepath_info
) );
432 struct async_workshop_filepath_info
*info
= call
->payload
;
433 info
->buf
= folder
->buffer
;
434 info
->id
= reg
->alias
.workshop_id
;
435 info
->len
= folder
->len
;
436 vg_async_dispatch( call
, async_workshop_get_filepath
);
437 vg_async_stall(); /* too bad! */
438 if( info
->buf
[0] == '\0' ){
439 vg_error( "Failed SteamAPI_GetItemInstallInfo(" PRINTF_U64
")\n",
440 reg
->alias
.workshop_id
);
443 folder
->i
= strlen( folder
->buffer
);
449 const char *local_folder
=
450 addon_type_infos
[reg
->alias
.type
].local_content_folder
;
452 if( !local_folder
) return 0;
453 vg_strcat( folder
, local_folder
);
454 vg_strcat( folder
, reg
->alias
.foldername
);
460 * Return existing cache id if reg_index points to a registry with its cache
463 static u16
addon_cache_fetch( enum addon_type type
, u32 reg_index
){
464 addon_reg
*reg
= NULL
;
466 if( reg_index
< addon_count( type
) ){
467 reg
= get_addon_from_index( type
, reg_index
);
469 return reg
->cache_id
;
476 * Allocate a new cache item from the pool
478 static u16
addon_cache_alloc( enum addon_type type
, u32 reg_index
){
479 struct addon_cache
*cache
= &addon_system
.cache
[ type
];
481 u16 new_id
= vg_pool_lru( &cache
->pool
);
482 struct addon_cache_entry
*new_entry
= vg_pool_item( &cache
->pool
, new_id
);
484 addon_reg
*reg
= NULL
;
485 if( reg_index
< addon_count( type
) )
486 reg
= get_addon_from_index( type
, reg_index
);
489 if( new_entry
->reg_ptr
)
490 new_entry
->reg_ptr
->cache_id
= 0;
493 reg
->cache_id
= new_id
;
495 new_entry
->reg_ptr
= reg
;
496 new_entry
->reg_index
= reg_index
;
500 vg_error( "cache full (type: %u)!\n", type
);
506 * Get the real item data for cache id
508 static void *addon_cache_item( enum addon_type type
, u16 id
){
509 if( !id
) return NULL
;
511 struct addon_cache
*cache
= &addon_system
.cache
[type
];
512 return cache
->items
+ ((size_t)(id
-1) * cache
->stride
);
516 * Get the real item data for cache id ONLY if the item is completely loaded.
518 static void *addon_cache_item_if_loaded( enum addon_type type
, u16 id
){
519 if( !id
) return NULL
;
521 struct addon_cache
*cache
= &addon_system
.cache
[type
];
522 struct addon_cache_entry
*entry
= vg_pool_item( &cache
->pool
, id
);
524 if( entry
->state
== k_addon_cache_state_loaded
)
525 return addon_cache_item( type
, id
);
530 * Updates the item state from the main thread
532 static void async_addon_setstate( void *_entry
, u32 _state
){
533 addon_cache_entry
*entry
= _entry
;
534 SDL_AtomicLock( &addon_system
.sl_cache_using_resources
);
535 entry
->state
= _state
;
536 SDL_AtomicUnlock( &addon_system
.sl_cache_using_resources
);
537 vg_success( " loaded (%s)\n", entry
->reg_ptr
->alias
.foldername
);
541 * Handles the loading of an individual item
543 static int addon_cache_load_request( enum addon_type type
, u16 id
,
544 addon_reg
*reg
, vg_str folder
){
546 /* load content files
547 * --------------------------------- */
548 vg_str content_path
= folder
;
551 root
.buf
= reg
->metadata
;
552 root
.len
= reg
->metadata_len
;
553 root
.max
= sizeof(reg
->metadata
);
555 const char *kv_content
= vg_msg_seekkvstr( &root
, "content", 0 );
557 vg_strcat( &content_path
, "/" );
558 vg_strcat( &content_path
, kv_content
);
561 vg_error( " No content paths in metadata\n" );
565 if( !vg_strgood( &content_path
) ) {
566 vg_error( " Metadata path too long\n" );
570 if( type
== k_addon_type_board
){
571 struct player_board
*board
= addon_cache_item( type
, id
);
572 player_board_load( board
, content_path
.buffer
);
575 else if( type
== k_addon_type_player
){
576 struct player_model
*model
= addon_cache_item( type
, id
);
577 player_model_load( model
, content_path
.buffer
);
587 static void addon_cache_free_item( enum addon_type type
, u16 id
){
588 if( type
== k_addon_type_board
){
589 struct player_board
*board
= addon_cache_item( type
, id
);
590 player_board_unload( board
);
592 else if( type
== k_addon_type_player
){
593 struct player_model
*model
= addon_cache_item( type
, id
);
594 player_model_unload( model
);
599 * Goes over cache item load requests and calls the above ^
601 static void addon_cache_load_loop(void){
602 vg_info( "Running load loop\n" );
605 for( u32 type
=0; type
<k_addon_type_max
; type
++ ){
606 struct addon_cache
*cache
= &addon_system
.cache
[type
];
608 for( u32 id
=1; id
<=cache
->pool
.count
; id
++ ){
609 addon_cache_entry
*entry
= vg_pool_item( &cache
->pool
, id
);
611 SDL_AtomicLock( &addon_system
.sl_cache_using_resources
);
612 if( entry
->state
== k_addon_cache_state_load_request
){
613 vg_info( "process cache load request (%u#%u, reg:%u)\n",
614 type
, id
, entry
->reg_index
);
616 if( entry
->reg_index
>= addon_count(type
) ){
617 /* should maybe have a different value for this case */
618 entry
->state
= k_addon_cache_state_none
;
619 SDL_AtomicUnlock( &addon_system
.sl_cache_using_resources
);
623 SDL_AtomicUnlock( &addon_system
.sl_cache_using_resources
);
625 /* continue with the request */
626 addon_reg
*reg
= get_addon_from_index( type
, entry
->reg_index
);
627 entry
->reg_ptr
= reg
;
630 vg_strnull( &folder
, path_buf
, 4096 );
631 if( addon_get_content_folder( reg
, &folder
) ){
632 if( addon_cache_load_request( type
, id
, reg
, folder
) ){
633 vg_async_call( async_addon_setstate
,
634 entry
, k_addon_cache_state_loaded
);
639 vg_warn( "cache item did not load (%u#%u)\n", type
, id
);
640 SDL_AtomicLock( &addon_system
.sl_cache_using_resources
);
641 entry
->state
= k_addon_cache_state_none
;
642 SDL_AtomicUnlock( &addon_system
.sl_cache_using_resources
);
645 SDL_AtomicUnlock( &addon_system
.sl_cache_using_resources
);
651 * Perform the cache interactions required to create a viewslot which will
652 * eventually be loaded by other parts of the system.
654 static u16
addon_cache_create_viewer( enum addon_type type
, u16 reg_id
){
655 struct addon_cache
*cache
= &addon_system
.cache
[type
];
656 vg_pool
*pool
= &cache
->pool
;
658 u16 cache_id
= addon_cache_fetch( type
, reg_id
);
660 cache_id
= addon_cache_alloc( type
, reg_id
);
663 SDL_AtomicLock( &addon_system
.sl_cache_using_resources
);
664 addon_cache_entry
*entry
= vg_pool_item( pool
, cache_id
);
666 if( entry
->state
== k_addon_cache_state_loaded
){
667 addon_cache_free_item( type
, cache_id
);
670 entry
->state
= k_addon_cache_state_load_request
;
671 SDL_AtomicUnlock( &addon_system
.sl_cache_using_resources
);
676 vg_pool_watch( pool
, cache_id
);
681 static void addon_cache_watch( enum addon_type type
, u16 cache_id
){
682 if( !cache_id
) return;
684 struct addon_cache
*cache
= &addon_system
.cache
[type
];
685 vg_pool
*pool
= &cache
->pool
;
686 vg_pool_watch( pool
, cache_id
);
689 static void addon_cache_unwatch( enum addon_type type
, u16 cache_id
){
690 if( !cache_id
) return;
692 struct addon_cache
*cache
= &addon_system
.cache
[type
];
693 vg_pool
*pool
= &cache
->pool
;
694 vg_pool_unwatch( pool
, cache_id
);