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
) ){
73 * Create a string version of addon alias in buf
75 static void addon_alias_uid( addon_alias
*alias
, char buf
[ADDON_UID_MAX
] ){
76 if( alias
->workshop_id
){
77 snprintf( buf
, 128, "sr%03d-steam-"PRINTF_U64
,
78 alias
->type
, alias
->workshop_id
);
81 snprintf( buf
, 128, "sr%03d-local-%s",
82 alias
->type
, alias
->foldername
);
87 * parse uid to alias. returns 1 if successful
89 static int addon_uid_to_alias( char uid
[ADDON_UID_MAX
], addon_alias
*alias
){
98 if( strlen(uid
) < 13 ) return 0;
99 if( !((uid
[0] == 's') && (uid
[1] == 'r')) ) return 0;
102 memcpy( type
, uid
+2, 3 );
104 alias
->type
= atoi(type
);
107 memcpy( location
, uid
+6, 5 );
110 if( !strcmp(location
,"steam") )
111 alias
->workshop_id
= atoll( uid
+12 );
112 else if( !strcmp(location
,"local") ){
113 alias
->workshop_id
= 0;
114 vg_strncpy( uid
+12, alias
->foldername
, 64, k_strncpy_always_add_null
);
122 static void addon_system_init( void ){
123 u32 reg_size
= sizeof(addon_reg
)*ADDON_MOUNTED_MAX
;
124 addon_system
.registry
= vg_linear_alloc( vg_mem
.rtmemory
, reg_size
);
126 for( u32 type
=0; type
<k_addon_type_max
; type
++ ){
127 struct addon_type_info
*inf
= &addon_type_infos
[type
];
128 struct addon_cache
*cache
= &addon_system
.cache
[type
];
130 if( inf
->cache_count
){
131 /* create the allocations pool */
132 u32 alloc_size
= sizeof(struct addon_cache_entry
)*inf
->cache_count
;
133 cache
->allocs
= vg_linear_alloc( vg_mem
.rtmemory
, alloc_size
);
134 memset( cache
->allocs
, 0, alloc_size
);
136 cache
->pool
.buffer
= cache
->allocs
;
137 cache
->pool
.count
= inf
->cache_count
;
138 cache
->pool
.stride
= sizeof( struct addon_cache_entry
);
139 cache
->pool
.offset
= offsetof( struct addon_cache_entry
, poolnode
);
140 vg_pool_init( &cache
->pool
);
142 /* create the real memory */
143 u32 cache_size
= inf
->cache_stride
*inf
->cache_count
;
144 cache
->items
= vg_linear_alloc( vg_mem
.rtmemory
, cache_size
);
145 cache
->stride
= inf
->cache_stride
;
146 memset( cache
->items
, 0, cache_size
);
148 for( i32 j
=0; j
<inf
->cache_count
; j
++ ){
149 struct addon_cache_entry
*alloc
= &cache
->allocs
[j
];
150 alloc
->reg_ptr
= NULL
;
151 alloc
->reg_index
= 0xffffffff;
159 * -----------------------------------------------------------------------------
163 * Reciever for scan completion. copies the registry counts back into main fred
165 static void async_addon_reg_update( void *data
, u32 size
)
167 vg_info( "Registry update notify\n" );
169 for( u32 i
=0; i
<k_addon_type_max
; i
++ ){
170 addon_system
.registry_type_counts
[i
] = 0;
173 for( u32 i
=0; i
<addon_system
.registry_count
; i
++ ){
174 enum addon_type type
= addon_system
.registry
[i
].alias
.type
;
175 addon_system
.registry_type_counts
[ type
] ++;
179 static void addon_set_foldername( addon_reg
*reg
, const char name
[64] ){
180 vg_strncpy( name
, reg
->alias
.foldername
, 64, k_strncpy_always_add_null
);
181 reg
->foldername_hash
= vg_strdjb2( reg
->alias
.foldername
);
185 * Create a new registry
187 static addon_reg
*addon_alloc_reg( PublishedFileId_t workshop_id
,
188 enum addon_type type
){
189 if( addon_system
.registry_count
== ADDON_MOUNTED_MAX
){
190 vg_error( "You have too many addons installed!\n" );
194 addon_reg
*reg
= &addon_system
.registry
[ addon_system
.registry_count
];
195 reg
->metadata_len
= 0;
197 reg
->state
= k_addon_state_indexed
;
198 reg
->alias
.workshop_id
= workshop_id
;
199 reg
->alias
.foldername
[0] = '\0';
200 reg
->alias
.type
= type
;
204 snprintf( foldername
, 64, PRINTF_U64
, workshop_id
);
205 addon_set_foldername( reg
, foldername
);
211 * If the addon.inf exists int the folder, load into the reg
213 static int addon_try_load_metadata( addon_reg
*reg
, vg_str folder_path
){
214 vg_str meta_path
= folder_path
;
215 vg_strcat( &meta_path
, "/addon.inf" );
216 if( !vg_strgood( &meta_path
) ){
217 vg_error( "The metadata path is too long\n" );
221 FILE *fp
= fopen( meta_path
.buffer
, "rb" );
223 vg_error( "Could not open the '%s'\n", meta_path
.buffer
);
227 reg
->metadata_len
= fread( reg
->metadata
, 1, 512, fp
);
228 if( reg
->metadata_len
!= 512 ){
231 vg_error( "unknown error codition" );
232 reg
->metadata_len
= 0;
240 static void addon_print_info( addon_reg
*reg
){
241 vg_info( "addon_reg #%u{\n", addon_system
.registry_count
);
242 vg_info( " type: %d\n", reg
->alias
.type
);
243 vg_info( " workshop_id: " PRINTF_U64
"\n", reg
->alias
.workshop_id
);
244 vg_info( " folder: [%u]%s\n", reg
->foldername_hash
, reg
->alias
.foldername
);
245 vg_info( " metadata_len: %u\n", reg
->metadata_len
);
246 vg_info( " cache_id: %hu\n", reg
->cache_id
);
250 static void addon_mount_finish( addon_reg
*reg
){
252 addon_print_info( reg
);
254 addon_system
.registry_count
++;
258 * Mount a fully packaged addon, one that certainly has a addon.inf
260 static addon_reg
*addon_mount_workshop_folder( PublishedFileId_t workshop_id
,
263 addon_reg
*reg
= addon_alloc_reg( workshop_id
, k_addon_type_none
);
264 if( !reg
) return NULL
;
266 if( !addon_try_load_metadata( reg
, folder_path
) ){
270 enum addon_type type
= k_addon_type_none
;
272 root
.buf
= reg
->metadata
;
273 root
.len
= reg
->metadata_len
;
274 root
.max
= sizeof(reg
->metadata
);
276 vg_msg workshop
= root
;
277 if( vg_msg_seekframe( &workshop
, "workshop", k_vg_msg_first
)){
278 type
= vg_msg_seekkvu32( &workshop
, "type", k_vg_msg_first
);
281 if( type
== k_addon_type_none
){
282 vg_error( "Cannot determine addon type\n" );
286 reg
->alias
.type
= type
;
287 addon_mount_finish( reg
);
292 * Mount a local folder. may or may not have addon.inf
294 static addon_reg
*addon_mount_local_addon( const char *folder
,
295 enum addon_type type
,
296 const char *content_ext
)
298 char folder_path_buf
[4096];
300 vg_strnull( &folder_path
, folder_path_buf
, 4096 );
301 vg_strcat( &folder_path
, folder
);
303 const char *folder_name
= vg_strch( &folder_path
, '/' )+1;
304 u32 folder_hash
= vg_strdjb2(folder_name
);
305 for( u32 i
=0; i
<addon_system
.registry_count
; i
++ ){
306 addon_reg
*reg
= &addon_system
.registry
[i
];
308 if( (reg
->alias
.type
== type
) && (reg
->foldername_hash
== folder_hash
) ){
309 if( !strcmp( reg
->alias
.foldername
, folder_name
) ){
310 reg
->state
= k_addon_state_indexed
;
316 addon_reg
*reg
= addon_alloc_reg( 0, type
);
317 if( !reg
) return NULL
;
318 addon_set_foldername( reg
, folder_name
);
319 addon_try_load_metadata( reg
, folder_path
);
321 if( reg
->metadata_len
== 0 ){
322 /* create our own content commands */
324 msg
.buf
= reg
->metadata
;
326 msg
.max
= sizeof(reg
->metadata
);
328 u32 content_count
= 0;
330 vg_strcat( &folder_path
, "" );
331 vg_warn( "Creating own metadata for: %s\n", folder_path
.buffer
);
334 if( !vg_dir_open(&subdir
, folder_path
.buffer
) ){
335 vg_error( "Failed to open '%s'\n", folder_path
.buffer
);
339 while( vg_dir_next_entry(&subdir
) ){
340 if( vg_dir_entry_type(&subdir
) == k_vg_entry_type_file
){
341 const char *fname
= vg_dir_entry_name(&subdir
);
342 vg_str file
= folder_path
;
343 vg_strcat( &file
, "/" );
344 vg_strcat( &file
, fname
);
345 if( !vg_strgood( &file
) ) continue;
347 char *ext
= vg_strch( &file
, '.' );
349 if( strcmp(ext
,content_ext
) ) continue;
351 vg_msg_wkvstr( &msg
, "content", fname
);
355 vg_dir_close(&subdir
);
357 if( !content_count
) return NULL
;
358 if( msg
.error
== k_vg_msg_error_OK
)
359 reg
->metadata_len
= msg
.cur
;
361 vg_error( "Error creating metadata: %d\n", msg
.error
);
366 addon_mount_finish( reg
);
371 * Check all subscribed items
373 static void addon_mount_workshop_items(void){
374 if( !steam_ready
) return;
376 * Steam workshop scan
378 vg_info( "Mounting steam workshop subscriptions\n" );
379 PublishedFileId_t workshop_ids
[ ADDON_MOUNTED_MAX
];
380 u32 workshop_count
= ADDON_MOUNTED_MAX
;
382 vg_async_item
*call
= vg_async_alloc(
383 sizeof(struct async_workshop_installed_files_info
));
384 struct async_workshop_installed_files_info
*info
= call
->payload
;
385 info
->buffer
= workshop_ids
;
386 info
->len
= &workshop_count
;
387 vg_async_dispatch( call
, async_workshop_get_installed_files
);
390 for( u32 j
=0; j
<workshop_count
; j
++ ){
391 /* check for existance in both our caches
392 * ----------------------------------------------------------*/
393 PublishedFileId_t id
= workshop_ids
[j
];
394 for( u32 i
=0; i
<addon_system
.registry_count
; i
++ ){
395 addon_reg
*reg
= &addon_system
.registry
[i
];
397 if( reg
->alias
.workshop_id
== id
){
398 reg
->state
= k_addon_state_indexed
;
399 goto next_file_workshop
;
403 vg_async_item
*call1
=
404 vg_async_alloc( sizeof(struct async_workshop_filepath_info
) );
408 struct async_workshop_filepath_info
*info
= call1
->payload
;
411 info
->len
= vg_list_size(path
);
412 vg_async_dispatch( call1
, async_workshop_get_filepath
);
413 vg_async_stall(); /* too bad! */
415 vg_str folder
= {.buffer
= path
, .i
=strlen(path
), .len
=4096};
416 addon_mount_workshop_folder( id
, folder
);
422 * Scan a local content folder for addons. It must find at least one file with
423 * the specified content_ext to be considered.
425 static void addon_mount_content_folder( enum addon_type type
,
426 const char *base_folder
,
427 const char *content_ext
)
429 vg_info( "Mounting addons(type:%d) matching skaterift/%s/*/*%s\n",
430 type
, base_folder
, content_ext
);
434 vg_strnull( &path
, path_buf
, 4096 );
435 vg_strcat( &path
, base_folder
);
438 if( !vg_dir_open(&dir
,path
.buffer
) ){
439 vg_error( "vg_dir_open('%s') failed\n", path
.buffer
);
443 vg_strcat(&path
,"/");
445 while( vg_dir_next_entry(&dir
) ){
446 if( vg_dir_entry_type(&dir
) == k_vg_entry_type_dir
){
447 const char *d_name
= vg_dir_entry_name(&dir
);
449 vg_str folder
= path
;
450 if( strlen( d_name
) > ADDON_FOLDERNAME_MAX
){
451 vg_warn( "folder too long: %s\n", d_name
);
455 vg_strcat( &folder
, d_name
);
456 if( !vg_strgood( &folder
) ) continue;
458 addon_mount_local_addon( folder
.buffer
, type
, content_ext
);
465 * write the full path of the addon's folder into the vg_str
467 static int addon_get_content_folder( addon_reg
*reg
, vg_str
*folder
){
468 if( reg
->alias
.workshop_id
){
469 vg_async_item
*call
=
470 vg_async_alloc( sizeof(struct async_workshop_filepath_info
) );
471 struct async_workshop_filepath_info
*info
= call
->payload
;
472 info
->buf
= folder
->buffer
;
473 info
->id
= reg
->alias
.workshop_id
;
474 info
->len
= folder
->len
;
475 vg_async_dispatch( call
, async_workshop_get_filepath
);
476 vg_async_stall(); /* too bad! */
477 if( info
->buf
[0] == '\0' ){
478 vg_error( "Failed SteamAPI_GetItemInstallInfo(" PRINTF_U64
")\n",
479 reg
->alias
.workshop_id
);
482 folder
->i
= strlen( folder
->buffer
);
488 const char *local_folder
=
489 addon_type_infos
[reg
->alias
.type
].local_content_folder
;
491 if( !local_folder
) return 0;
492 vg_strcat( folder
, local_folder
);
493 vg_strcat( folder
, reg
->alias
.foldername
);
499 * Return existing cache id if reg_index points to a registry with its cache
502 static u16
addon_cache_fetch( enum addon_type type
, u32 reg_index
){
503 addon_reg
*reg
= NULL
;
505 if( reg_index
< addon_count( type
) ){
506 reg
= get_addon_from_index( type
, reg_index
);
508 return reg
->cache_id
;
515 * Allocate a new cache item from the pool
517 static u16
addon_cache_alloc( enum addon_type type
, u32 reg_index
){
518 struct addon_cache
*cache
= &addon_system
.cache
[ type
];
520 u16 new_id
= vg_pool_lru( &cache
->pool
);
521 struct addon_cache_entry
*new_entry
= vg_pool_item( &cache
->pool
, new_id
);
523 addon_reg
*reg
= NULL
;
524 if( reg_index
< addon_count( type
) )
525 reg
= get_addon_from_index( type
, reg_index
);
528 if( new_entry
->reg_ptr
)
529 new_entry
->reg_ptr
->cache_id
= 0;
532 reg
->cache_id
= new_id
;
534 new_entry
->reg_ptr
= reg
;
535 new_entry
->reg_index
= reg_index
;
539 vg_error( "cache full (type: %u)!\n", type
);
545 * Get the real item data for cache id
547 static void *addon_cache_item( enum addon_type type
, u16 id
){
548 if( !id
) return NULL
;
550 struct addon_cache
*cache
= &addon_system
.cache
[type
];
551 return cache
->items
+ ((size_t)(id
-1) * cache
->stride
);
555 * Get the real item data for cache id ONLY if the item is completely loaded.
557 static void *addon_cache_item_if_loaded( enum addon_type type
, u16 id
){
558 if( !id
) return NULL
;
560 struct addon_cache
*cache
= &addon_system
.cache
[type
];
561 struct addon_cache_entry
*entry
= vg_pool_item( &cache
->pool
, id
);
563 if( entry
->state
== k_addon_cache_state_loaded
)
564 return addon_cache_item( type
, id
);
569 * Updates the item state from the main thread
571 static void async_addon_setstate( void *_entry
, u32 _state
){
572 addon_cache_entry
*entry
= _entry
;
573 SDL_AtomicLock( &addon_system
.sl_cache_using_resources
);
574 entry
->state
= _state
;
575 SDL_AtomicUnlock( &addon_system
.sl_cache_using_resources
);
576 vg_success( " loaded (%s)\n", entry
->reg_ptr
->alias
.foldername
);
580 * Handles the loading of an individual item
582 static int addon_cache_load_request( enum addon_type type
, u16 id
,
583 addon_reg
*reg
, vg_str folder
){
585 /* load content files
586 * --------------------------------- */
587 vg_str content_path
= folder
;
590 root
.buf
= reg
->metadata
;
591 root
.len
= reg
->metadata_len
;
592 root
.max
= sizeof(reg
->metadata
);
594 const char *kv_content
= vg_msg_seekkvstr( &root
, "content", 0 );
596 vg_strcat( &content_path
, "/" );
597 vg_strcat( &content_path
, kv_content
);
600 vg_error( " No content paths in metadata\n" );
604 if( !vg_strgood( &content_path
) ) {
605 vg_error( " Metadata path too long\n" );
609 if( type
== k_addon_type_board
){
610 struct player_board
*board
= addon_cache_item( type
, id
);
611 player_board_load( board
, content_path
.buffer
);
614 else if( type
== k_addon_type_player
){
615 struct player_model
*model
= addon_cache_item( type
, id
);
616 player_model_load( model
, content_path
.buffer
);
626 static void addon_cache_free_item( enum addon_type type
, u16 id
){
627 if( type
== k_addon_type_board
){
628 struct player_board
*board
= addon_cache_item( type
, id
);
629 player_board_unload( board
);
631 else if( type
== k_addon_type_player
){
632 struct player_model
*model
= addon_cache_item( type
, id
);
633 player_model_unload( model
);
638 * Goes over cache item load requests and calls the above ^
640 static void addon_cache_load_loop(void){
641 vg_info( "Running load loop\n" );
644 for( u32 type
=0; type
<k_addon_type_max
; type
++ ){
645 struct addon_cache
*cache
= &addon_system
.cache
[type
];
647 for( u32 id
=1; id
<=cache
->pool
.count
; id
++ ){
648 addon_cache_entry
*entry
= vg_pool_item( &cache
->pool
, id
);
650 SDL_AtomicLock( &addon_system
.sl_cache_using_resources
);
651 if( entry
->state
== k_addon_cache_state_load_request
){
652 vg_info( "process cache load request (%u#%u, reg:%u)\n",
653 type
, id
, entry
->reg_index
);
655 if( entry
->reg_index
>= addon_count(type
) ){
656 /* should maybe have a different value for this case */
657 entry
->state
= k_addon_cache_state_none
;
658 SDL_AtomicUnlock( &addon_system
.sl_cache_using_resources
);
662 SDL_AtomicUnlock( &addon_system
.sl_cache_using_resources
);
664 /* continue with the request */
665 addon_reg
*reg
= get_addon_from_index( type
, entry
->reg_index
);
666 entry
->reg_ptr
= reg
;
669 vg_strnull( &folder
, path_buf
, 4096 );
670 if( addon_get_content_folder( reg
, &folder
) ){
671 if( addon_cache_load_request( type
, id
, reg
, folder
) ){
672 vg_async_call( async_addon_setstate
,
673 entry
, k_addon_cache_state_loaded
);
678 vg_warn( "cache item did not load (%u#%u)\n", type
, id
);
679 SDL_AtomicLock( &addon_system
.sl_cache_using_resources
);
680 entry
->state
= k_addon_cache_state_none
;
681 SDL_AtomicUnlock( &addon_system
.sl_cache_using_resources
);
684 SDL_AtomicUnlock( &addon_system
.sl_cache_using_resources
);
690 * Perform the cache interactions required to create a viewslot which will
691 * eventually be loaded by other parts of the system.
693 static u16
addon_cache_create_viewer( enum addon_type type
, u16 reg_id
){
694 struct addon_cache
*cache
= &addon_system
.cache
[type
];
695 vg_pool
*pool
= &cache
->pool
;
697 u16 cache_id
= addon_cache_fetch( type
, reg_id
);
699 cache_id
= addon_cache_alloc( type
, reg_id
);
702 SDL_AtomicLock( &addon_system
.sl_cache_using_resources
);
703 addon_cache_entry
*entry
= vg_pool_item( pool
, cache_id
);
705 if( entry
->state
== k_addon_cache_state_loaded
){
706 addon_cache_free_item( type
, cache_id
);
709 entry
->state
= k_addon_cache_state_load_request
;
710 SDL_AtomicUnlock( &addon_system
.sl_cache_using_resources
);
715 vg_pool_watch( pool
, cache_id
);
720 static u16
addon_cache_create_viewer_from_uid( enum addon_type type
,
721 char uid
[ADDON_UID_MAX
] ){
723 if( !addon_uid_to_alias( uid
, &q
) ) return 0;
724 if( q
.type
!= type
) return 0;
726 u32 reg_id
= addon_match( &q
);
728 if( reg_id
== 0xffffffff ){
729 vg_warn( "We dont have the addon '%s' installed.\n", uid
);
733 return addon_cache_create_viewer( type
, reg_id
);
737 static void addon_cache_watch( enum addon_type type
, u16 cache_id
){
738 if( !cache_id
) return;
740 struct addon_cache
*cache
= &addon_system
.cache
[type
];
741 vg_pool
*pool
= &cache
->pool
;
742 vg_pool_watch( pool
, cache_id
);
745 static void addon_cache_unwatch( enum addon_type type
, u16 cache_id
){
746 if( !cache_id
) return;
748 struct addon_cache
*cache
= &addon_system
.cache
[type
];
749 vg_pool
*pool
= &cache
->pool
;
750 vg_pool_unwatch( pool
, cache_id
);