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_system_init( void ){
73 u32 reg_size
= sizeof(addon_reg
)*ADDON_MOUNTED_MAX
;
74 addon_system
.registry
= vg_linear_alloc( vg_mem
.rtmemory
, reg_size
);
76 for( u32 type
=0; type
<k_addon_type_max
; type
++ ){
77 struct addon_type_info
*inf
= &addon_type_infos
[type
];
78 struct addon_cache
*cache
= &addon_system
.cache
[type
];
80 if( inf
->cache_count
){
81 /* create the allocations pool */
82 u32 alloc_size
= sizeof(struct addon_cache_entry
)*inf
->cache_count
;
83 cache
->allocs
= vg_linear_alloc( vg_mem
.rtmemory
, alloc_size
);
84 memset( cache
->allocs
, 0, alloc_size
);
86 cache
->pool
.buffer
= cache
->allocs
;
87 cache
->pool
.count
= inf
->cache_count
;
88 cache
->pool
.stride
= sizeof( struct addon_cache_entry
);
89 cache
->pool
.offset
= offsetof( struct addon_cache_entry
, poolnode
);
90 vg_pool_init( &cache
->pool
);
92 /* create the real memory */
93 u32 cache_size
= inf
->cache_stride
*inf
->cache_count
;
94 cache
->items
= vg_linear_alloc( vg_mem
.rtmemory
, cache_size
);
95 cache
->stride
= inf
->cache_stride
;
96 memset( cache
->items
, 0, cache_size
);
98 for( i32 j
=0; j
<inf
->cache_count
; j
++ ){
99 struct addon_cache_entry
*alloc
= &cache
->allocs
[j
];
100 alloc
->reg_ptr
= NULL
;
101 alloc
->reg_index
= 0xffffffff;
109 * -----------------------------------------------------------------------------
113 * Reciever for scan completion. copies the registry counts back into main fred
115 VG_STATIC
void async_addon_reg_update( void *data
, u32 size
)
117 vg_info( "Registry update notify\n" );
119 for( u32 i
=0; i
<k_addon_type_max
; i
++ ){
120 addon_system
.registry_type_counts
[i
] = 0;
123 for( u32 i
=0; i
<addon_system
.registry_count
; i
++ ){
124 enum addon_type type
= addon_system
.registry
[i
].alias
.type
;
125 addon_system
.registry_type_counts
[ type
] ++;
129 VG_STATIC
void addon_set_foldername( addon_reg
*reg
, const char name
[64] ){
130 vg_strncpy( name
, reg
->alias
.foldername
, 64, k_strncpy_always_add_null
);
131 reg
->foldername_hash
= vg_strdjb2( reg
->alias
.foldername
);
135 * Create a new registry
137 VG_STATIC addon_reg
*addon_alloc_reg( PublishedFileId_t workshop_id
,
138 enum addon_type type
){
139 if( addon_system
.registry_count
== ADDON_MOUNTED_MAX
){
140 vg_error( "You have too many addons installed!\n" );
144 addon_reg
*reg
= &addon_system
.registry
[ addon_system
.registry_count
];
145 reg
->metadata_len
= 0;
147 reg
->state
= k_addon_state_indexed
;
148 reg
->alias
.workshop_id
= workshop_id
;
149 reg
->alias
.foldername
[0] = '\0';
150 reg
->alias
.type
= type
;
154 snprintf( foldername
, 64, PRINTF_U64
, workshop_id
);
155 addon_set_foldername( reg
, foldername
);
161 * If the addon.inf exists int the folder, load into the reg
163 VG_STATIC
int addon_try_load_metadata( addon_reg
*reg
, vg_str folder_path
){
164 vg_str meta_path
= folder_path
;
165 vg_strcat( &meta_path
, "/addon.inf" );
166 if( !vg_strgood( &meta_path
) ){
167 vg_error( "The metadata path is too long\n" );
171 FILE *fp
= fopen( meta_path
.buffer
, "rb" );
173 vg_error( "Could not open the '%s'\n", meta_path
.buffer
);
177 reg
->metadata_len
= fread( reg
->metadata
, 1, 512, fp
);
178 if( reg
->metadata_len
!= 512 ){
181 vg_error( "unknown error codition" );
182 reg
->metadata_len
= 0;
190 VG_STATIC
void addon_print_info( addon_reg
*reg
){
191 vg_info( "addon_reg #%u{\n", addon_system
.registry_count
);
192 vg_info( " type: %d\n", reg
->alias
.type
);
193 vg_info( " workshop_id: " PRINTF_U64
"\n", reg
->alias
.workshop_id
);
194 vg_info( " folder: [%u]%s\n", reg
->foldername_hash
, reg
->alias
.foldername
);
195 vg_info( " metadata_len: %u\n", reg
->metadata_len
);
196 vg_info( " cache_id: %hu\n", reg
->cache_id
);
200 VG_STATIC
void addon_mount_finish( addon_reg
*reg
){
201 addon_print_info( reg
);
202 addon_system
.registry_count
++;
206 * Mount a fully packaged addon, one that certainly has a addon.inf
208 VG_STATIC addon_reg
*addon_mount_workshop_folder( PublishedFileId_t workshop_id
,
211 addon_reg
*reg
= addon_alloc_reg( workshop_id
, k_addon_type_none
);
212 if( !reg
) return NULL
;
214 if( !addon_try_load_metadata( reg
, folder_path
) ){
218 enum addon_type type
= k_addon_type_none
;
220 root
.buf
= reg
->metadata
;
221 root
.len
= reg
->metadata_len
;
222 root
.max
= sizeof(reg
->metadata
);
224 vg_msg workshop
= root
;
225 if( vg_msg_seekframe( &workshop
, "workshop", k_vg_msg_first
)){
226 type
= vg_msg_seekkvu32( &workshop
, "type", k_vg_msg_first
);
229 if( type
== k_addon_type_none
){
230 vg_error( "Cannot determine addon type\n" );
234 reg
->alias
.type
= type
;
235 addon_mount_finish( reg
);
240 * Mount a local folder. may or may not have addon.inf
242 VG_STATIC addon_reg
*addon_mount_local_addon( const char *folder
,
243 enum addon_type type
,
244 const char *content_ext
)
246 char folder_path_buf
[4096];
248 vg_strnull( &folder_path
, folder_path_buf
, 4096 );
249 vg_strcat( &folder_path
, folder
);
251 const char *folder_name
= vg_strch( &folder_path
, '/' )+1;
252 u32 folder_hash
= vg_strdjb2(folder_name
);
253 for( u32 i
=0; i
<addon_system
.registry_count
; i
++ ){
254 addon_reg
*reg
= &addon_system
.registry
[i
];
256 if( (reg
->alias
.type
== type
) && (reg
->foldername_hash
== folder_hash
) ){
257 if( !strcmp( reg
->alias
.foldername
, folder_name
) ){
258 reg
->state
= k_addon_state_indexed
;
264 addon_reg
*reg
= addon_alloc_reg( 0, type
);
265 if( !reg
) return NULL
;
266 addon_set_foldername( reg
, folder_name
);
267 addon_try_load_metadata( reg
, folder_path
);
269 if( reg
->metadata_len
== 0 ){
270 /* create our own content commands */
272 msg
.buf
= reg
->metadata
;
274 msg
.max
= sizeof(reg
->metadata
);
276 u32 content_count
= 0;
278 vg_strcat( &folder_path
, "" );
279 vg_warn( "Creating own metadata for: %s\n", folder_path
.buffer
);
282 if( !vg_dir_open(&subdir
, folder_path
.buffer
) ){
283 vg_error( "Failed to open '%s'\n", folder_path
.buffer
);
287 while( vg_dir_next_entry(&subdir
) ){
288 if( vg_dir_entry_type(&subdir
) == k_vg_entry_type_file
){
289 const char *fname
= vg_dir_entry_name(&subdir
);
290 vg_str file
= folder_path
;
291 vg_strcat( &file
, "/" );
292 vg_strcat( &file
, fname
);
293 if( !vg_strgood( &file
) ) continue;
295 char *ext
= vg_strch( &file
, '.' );
297 if( strcmp(ext
,content_ext
) ) continue;
299 vg_msg_wkvstr( &msg
, "content", fname
);
303 vg_dir_close(&subdir
);
305 if( !content_count
) return NULL
;
306 if( msg
.error
== k_vg_msg_error_OK
)
307 reg
->metadata_len
= msg
.cur
;
309 vg_error( "Error creating metadata: %d\n", msg
.error
);
314 addon_mount_finish( reg
);
319 * Check all subscribed items
321 VG_STATIC
void addon_mount_workshop_items(void){
322 if( !steam_ready
) return;
324 * Steam workshop scan
326 vg_info( "Mounting steam workshop subscriptions\n" );
327 PublishedFileId_t workshop_ids
[ ADDON_MOUNTED_MAX
];
328 u32 workshop_count
= ADDON_MOUNTED_MAX
;
330 vg_async_item
*call
= vg_async_alloc(
331 sizeof(struct async_workshop_installed_files_info
));
332 struct async_workshop_installed_files_info
*info
= call
->payload
;
333 info
->buffer
= workshop_ids
;
334 info
->len
= &workshop_count
;
335 vg_async_dispatch( call
, async_workshop_get_installed_files
);
338 for( u32 j
=0; j
<workshop_count
; j
++ ){
339 /* check for existance in both our caches
340 * ----------------------------------------------------------*/
341 PublishedFileId_t id
= workshop_ids
[j
];
342 for( u32 i
=0; i
<addon_system
.registry_count
; i
++ ){
343 addon_reg
*reg
= &addon_system
.registry
[i
];
345 if( reg
->alias
.workshop_id
== id
){
346 reg
->state
= k_addon_state_indexed
;
347 goto next_file_workshop
;
351 vg_async_item
*call1
=
352 vg_async_alloc( sizeof(struct async_workshop_filepath_info
) );
356 struct async_workshop_filepath_info
*info
= call1
->payload
;
359 info
->len
= vg_list_size(path
);
360 vg_async_dispatch( call1
, async_workshop_get_filepath
);
361 vg_async_stall(); /* too bad! */
363 vg_str folder
= {.buffer
= path
, .i
=strlen(path
), .len
=4096};
364 addon_mount_workshop_folder( id
, folder
);
370 * Scan a local content folder for addons. It must find at least one file with
371 * the specified content_ext to be considered.
373 VG_STATIC
void addon_mount_content_folder( enum addon_type type
,
374 const char *base_folder
,
375 const char *content_ext
)
377 vg_info( "Mounting addons(type:%d) matching skaterift/%s/*/*%s\n",
378 type
, base_folder
, content_ext
);
382 vg_strnull( &path
, path_buf
, 4096 );
383 vg_strcat( &path
, base_folder
);
386 if( !vg_dir_open(&dir
,path
.buffer
) ){
387 vg_error( "vg_dir_open('%s') failed\n", path
.buffer
);
391 vg_strcat(&path
,"/");
393 while( vg_dir_next_entry(&dir
) ){
394 if( vg_dir_entry_type(&dir
) == k_vg_entry_type_dir
){
395 const char *d_name
= vg_dir_entry_name(&dir
);
397 vg_str folder
= path
;
398 if( strlen( d_name
) > ADDON_FOLDERNAME_MAX
){
399 vg_warn( "folder too long: %s\n", d_name
);
403 vg_strcat( &folder
, d_name
);
404 if( !vg_strgood( &folder
) ) continue;
406 addon_mount_local_addon( folder
.buffer
, type
, content_ext
);
413 * write the full path of the addon's folder into the vg_str
415 static int addon_get_content_folder( addon_reg
*reg
, vg_str
*folder
){
416 if( reg
->alias
.workshop_id
){
417 vg_async_item
*call
=
418 vg_async_alloc( sizeof(struct async_workshop_filepath_info
) );
419 struct async_workshop_filepath_info
*info
= call
->payload
;
420 info
->buf
= folder
->buffer
;
421 info
->id
= reg
->alias
.workshop_id
;
422 info
->len
= folder
->len
;
423 vg_async_dispatch( call
, async_workshop_get_filepath
);
424 vg_async_stall(); /* too bad! */
425 if( info
->buf
[0] == '\0' ){
426 vg_error( "Failed SteamAPI_GetItemInstallInfo(" PRINTF_U64
")\n",
427 reg
->alias
.workshop_id
);
430 folder
->i
= strlen( folder
->buffer
);
436 const char *local_folder
=
437 addon_type_infos
[reg
->alias
.type
].local_content_folder
;
439 if( !local_folder
) return 0;
440 vg_strcat( folder
, local_folder
);
441 vg_strcat( folder
, reg
->alias
.foldername
);
447 * Return existing cache id if reg_index points to a registry with its cache
450 static u16
addon_cache_fetch( enum addon_type type
, u32 reg_index
){
451 addon_reg
*reg
= NULL
;
453 if( reg_index
< addon_count( type
) ){
454 reg
= get_addon_from_index( type
, reg_index
);
456 return reg
->cache_id
;
463 * Allocate a new cache item from the pool
465 static u16
addon_cache_alloc( enum addon_type type
, u32 reg_index
){
466 struct addon_cache
*cache
= &addon_system
.cache
[ type
];
468 u16 new_id
= vg_pool_lru( &cache
->pool
);
469 struct addon_cache_entry
*new_entry
= vg_pool_item( &cache
->pool
, new_id
);
471 addon_reg
*reg
= NULL
;
472 if( reg_index
< addon_count( type
) )
473 reg
= get_addon_from_index( type
, reg_index
);
476 if( new_entry
->reg_ptr
)
477 new_entry
->reg_ptr
->cache_id
= 0;
480 reg
->cache_id
= new_id
;
482 new_entry
->reg_ptr
= reg
;
483 new_entry
->reg_index
= reg_index
;
487 vg_error( "cache full (type: %u)!\n", type
);
493 * Get the real item data for cache id
495 static void *addon_cache_item( enum addon_type type
, u16 id
){
496 if( !id
) return NULL
;
498 struct addon_cache
*cache
= &addon_system
.cache
[type
];
499 return cache
->items
+ ((size_t)(id
-1) * cache
->stride
);
503 * Get the real item data for cache id ONLY if the item is completely loaded.
505 static void *addon_cache_item_if_loaded( enum addon_type type
, u16 id
){
506 if( !id
) return NULL
;
508 struct addon_cache
*cache
= &addon_system
.cache
[type
];
509 struct addon_cache_entry
*entry
= vg_pool_item( &cache
->pool
, id
);
511 if( entry
->state
== k_addon_cache_state_loaded
)
512 return addon_cache_item( type
, id
);
517 * Updates the item state from the main thread
519 static void async_addon_setstate( void *_entry
, u32 _state
){
520 addon_cache_entry
*entry
= _entry
;
521 SDL_AtomicLock( &addon_system
.sl_cache_using_resources
);
522 entry
->state
= _state
;
523 SDL_AtomicUnlock( &addon_system
.sl_cache_using_resources
);
524 vg_success( " loaded (%s)\n", entry
->reg_ptr
->alias
.foldername
);
528 * Handles the loading of an individual item
530 static int addon_cache_load_request( enum addon_type type
, u16 id
,
531 addon_reg
*reg
, vg_str folder
){
533 /* load content files
534 * --------------------------------- */
535 vg_str content_path
= folder
;
538 root
.buf
= reg
->metadata
;
539 root
.len
= reg
->metadata_len
;
540 root
.max
= sizeof(reg
->metadata
);
542 const char *kv_content
= vg_msg_seekkvstr( &root
, "content", 0 );
544 vg_strcat( &content_path
, "/" );
545 vg_strcat( &content_path
, kv_content
);
548 vg_error( " No content paths in metadata\n" );
552 if( !vg_strgood( &content_path
) ) {
553 vg_error( " Metadata path too long\n" );
557 if( type
== k_addon_type_board
){
558 struct player_board
*board
= addon_cache_item( type
, id
);
559 player_board_load( board
, content_path
.buffer
);
562 else if( type
== k_addon_type_player
){
563 struct player_model
*model
= addon_cache_item( type
, id
);
564 player_model_load( model
, content_path
.buffer
);
574 static void addon_cache_free_item( enum addon_type type
, u16 id
){
575 if( type
== k_addon_type_board
){
576 struct player_board
*board
= addon_cache_item( type
, id
);
577 player_board_unload( board
);
579 else if( type
== k_addon_type_player
){
580 struct player_model
*model
= addon_cache_item( type
, id
);
581 player_model_unload( model
);
586 * Goes over cache item load requests and calls the above ^
588 static void addon_cache_load_loop(void){
589 vg_info( "Running load loop\n" );
592 for( u32 type
=0; type
<k_addon_type_max
; type
++ ){
593 struct addon_cache
*cache
= &addon_system
.cache
[type
];
595 for( u32 id
=1; id
<=cache
->pool
.count
; id
++ ){
596 addon_cache_entry
*entry
= vg_pool_item( &cache
->pool
, id
);
598 SDL_AtomicLock( &addon_system
.sl_cache_using_resources
);
599 if( entry
->state
== k_addon_cache_state_load_request
){
600 vg_info( "process cache load request (%u#%u, reg:%u)\n",
601 type
, id
, entry
->reg_index
);
603 if( entry
->reg_index
>= addon_count(type
) ){
604 /* should maybe have a different value for this case */
605 entry
->state
= k_addon_cache_state_none
;
606 SDL_AtomicUnlock( &addon_system
.sl_cache_using_resources
);
610 SDL_AtomicUnlock( &addon_system
.sl_cache_using_resources
);
612 /* continue with the request */
613 addon_reg
*reg
= get_addon_from_index( type
, entry
->reg_index
);
614 entry
->reg_ptr
= reg
;
617 vg_strnull( &folder
, path_buf
, 4096 );
618 if( addon_get_content_folder( reg
, &folder
) ){
619 if( addon_cache_load_request( type
, id
, reg
, folder
) ){
620 vg_async_call( async_addon_setstate
,
621 entry
, k_addon_cache_state_loaded
);
626 vg_warn( "cache item did not load (%u#%u)\n", type
, id
);
627 SDL_AtomicLock( &addon_system
.sl_cache_using_resources
);
628 entry
->state
= k_addon_cache_state_none
;
629 SDL_AtomicUnlock( &addon_system
.sl_cache_using_resources
);
632 SDL_AtomicUnlock( &addon_system
.sl_cache_using_resources
);
638 * Perform the cache interactions required to create a viewslot which will
639 * eventually be loaded by other parts of the system.
641 static u16
addon_cache_create_viewer( enum addon_type type
, u16 reg_id
){
642 struct addon_cache
*cache
= &addon_system
.cache
[type
];
643 vg_pool
*pool
= &cache
->pool
;
645 u16 cache_id
= addon_cache_fetch( type
, reg_id
);
647 cache_id
= addon_cache_alloc( type
, reg_id
);
650 SDL_AtomicLock( &addon_system
.sl_cache_using_resources
);
651 addon_cache_entry
*entry
= vg_pool_item( pool
, cache_id
);
653 if( entry
->state
== k_addon_cache_state_loaded
){
654 addon_cache_free_item( type
, cache_id
);
657 entry
->state
= k_addon_cache_state_load_request
;
658 SDL_AtomicUnlock( &addon_system
.sl_cache_using_resources
);
663 vg_pool_watch( pool
, cache_id
);
668 static void addon_cache_watch( enum addon_type type
, u16 cache_id
){
669 if( !cache_id
) return;
671 struct addon_cache
*cache
= &addon_system
.cache
[type
];
672 vg_pool
*pool
= &cache
->pool
;
673 vg_pool_watch( pool
, cache_id
);
676 static void addon_cache_unwatch( enum addon_type type
, u16 cache_id
){
677 if( !cache_id
) return;
679 struct addon_cache
*cache
= &addon_system
.cache
[type
];
680 vg_pool
*pool
= &cache
->pool
;
681 vg_pool_unwatch( pool
, cache_id
);