1 #include "vg/vg_engine.h"
3 #include "vg/vg_loader.h"
5 #include "addon_types.h"
11 struct addon_system addon_system
;
13 u32
addon_count( enum addon_type type
, u32 ignoreflags
)
16 u32 typecount
= 0, count
= 0;
17 for( u32 i
=0; typecount
<addon_count( type
, 0 ); i
++ ){
18 addon_reg
*reg
= &addon_system
.registry
[i
];
19 if( reg
->alias
.type
== type
){
22 if( reg
->flags
& ignoreflags
)
32 return addon_system
.registry_type_counts
[ type
];
36 /* these kind of suck, oh well. */
37 addon_reg
*get_addon_from_index( enum addon_type type
, u32 index
,
40 u32 typecount
= 0, count
= 0;
41 for( u32 i
=0; typecount
<addon_count(type
,0); i
++ ){
42 addon_reg
*reg
= &addon_system
.registry
[i
];
43 if( reg
->alias
.type
== type
){
46 if( reg
->flags
& ignoreflags
)
59 u32
get_index_from_addon( enum addon_type type
, addon_reg
*a
)
62 for( u32 i
=0; count
<addon_system
.registry_type_counts
[type
]; i
++ ){
63 addon_reg
*reg
= &addon_system
.registry
[i
];
64 if( reg
->alias
.type
== type
){
75 u32
addon_match( addon_alias
*alias
)
77 if( alias
->type
== k_addon_type_none
) return 0xffffffff;
79 u32 foldername_djb2
= 0;
80 if( !alias
->workshop_id
)
81 foldername_djb2
= vg_strdjb2( alias
->foldername
);
84 for( u32 i
=0; count
<addon_system
.registry_type_counts
[alias
->type
]; i
++ ){
85 addon_reg
*reg
= &addon_system
.registry
[i
];
86 if( reg
->alias
.type
== alias
->type
){
88 if( alias
->workshop_id
){
89 if( alias
->workshop_id
== reg
->alias
.workshop_id
)
93 if( reg
->foldername_hash
== foldername_djb2
){
94 if( !strcmp( reg
->alias
.foldername
, alias
->foldername
) ){
108 * Create a string version of addon alias in buf
110 void addon_alias_uid( addon_alias
*alias
, char buf
[ADDON_UID_MAX
] )
112 if( alias
->workshop_id
){
113 snprintf( buf
, 128, "sr%03d-steam-"PRINTF_U64
,
114 alias
->type
, alias
->workshop_id
);
117 snprintf( buf
, 128, "sr%03d-local-%s",
118 alias
->type
, alias
->foldername
);
125 int addon_alias_eq( addon_alias
*a
, addon_alias
*b
)
127 if( a
->type
== b
->type
){
128 if( a
->workshop_id
== b
->workshop_id
){
132 return !strcmp( a
->foldername
, b
->foldername
);
141 * make alias represent NULL.
143 void invalidate_addon_alias( addon_alias
*alias
)
145 alias
->type
= k_addon_type_none
;
146 alias
->workshop_id
= 0;
147 alias
->foldername
[0] = '\0';
151 * parse uid to alias. returns 1 if successful
153 int addon_uid_to_alias( const char *uid
, addon_alias
*alias
)
163 if( strlen(uid
) < 13 ){
164 invalidate_addon_alias( alias
);
167 if( !((uid
[0] == 's') && (uid
[1] == 'r')) ){
168 invalidate_addon_alias( alias
);
173 memcpy( type
, uid
+2, 3 );
175 alias
->type
= atoi(type
);
178 memcpy( location
, uid
+6, 5 );
181 if( !strcmp(location
,"steam") )
182 alias
->workshop_id
= atoll( uid
+12 );
183 else if( !strcmp(location
,"local") ){
184 alias
->workshop_id
= 0;
185 vg_strncpy( uid
+12, alias
->foldername
, 64, k_strncpy_always_add_null
);
188 invalidate_addon_alias( alias
);
195 void addon_system_init( void )
197 u32 reg_size
= sizeof(addon_reg
)*ADDON_MOUNTED_MAX
;
198 addon_system
.registry
= vg_linear_alloc( vg_mem
.rtmemory
, reg_size
);
200 for( u32 type
=0; type
<k_addon_type_max
; type
++ ){
201 struct addon_type_info
*inf
= &addon_type_infos
[type
];
202 struct addon_cache
*cache
= &addon_system
.cache
[type
];
204 if( inf
->cache_count
){
205 /* create the allocations pool */
206 u32 alloc_size
= sizeof(struct addon_cache_entry
)*inf
->cache_count
;
207 cache
->allocs
= vg_linear_alloc( vg_mem
.rtmemory
, alloc_size
);
208 memset( cache
->allocs
, 0, alloc_size
);
210 cache
->pool
.buffer
= cache
->allocs
;
211 cache
->pool
.count
= inf
->cache_count
;
212 cache
->pool
.stride
= sizeof( struct addon_cache_entry
);
213 cache
->pool
.offset
= offsetof( struct addon_cache_entry
, poolnode
);
214 vg_pool_init( &cache
->pool
);
216 /* create the real memory */
217 u32 cache_size
= inf
->cache_stride
*inf
->cache_count
;
218 cache
->items
= vg_linear_alloc( vg_mem
.rtmemory
, cache_size
);
219 cache
->stride
= inf
->cache_stride
;
220 memset( cache
->items
, 0, cache_size
);
222 for( i32 j
=0; j
<inf
->cache_count
; j
++ ){
223 struct addon_cache_entry
*alloc
= &cache
->allocs
[j
];
224 alloc
->reg_ptr
= NULL
;
225 alloc
->reg_index
= 0xffffffff;
233 * -----------------------------------------------------------------------------
237 * Reciever for scan completion. copies the registry counts back into main fred
239 void async_addon_reg_update( void *data
, u32 size
)
241 vg_info( "Registry update notify\n" );
243 for( u32 i
=0; i
<k_addon_type_max
; i
++ ){
244 addon_system
.registry_type_counts
[i
] = 0;
247 for( u32 i
=0; i
<addon_system
.registry_count
; i
++ ){
248 enum addon_type type
= addon_system
.registry
[i
].alias
.type
;
249 addon_system
.registry_type_counts
[ type
] ++;
253 static void addon_set_foldername( addon_reg
*reg
, const char name
[64] ){
254 vg_strncpy( name
, reg
->alias
.foldername
, 64, k_strncpy_always_add_null
);
255 reg
->foldername_hash
= vg_strdjb2( reg
->alias
.foldername
);
259 * Create a new registry
261 static addon_reg
*addon_alloc_reg( PublishedFileId_t workshop_id
,
262 enum addon_type type
){
263 if( addon_system
.registry_count
== ADDON_MOUNTED_MAX
){
264 vg_error( "You have too many addons installed!\n" );
268 addon_reg
*reg
= &addon_system
.registry
[ addon_system
.registry_count
];
270 reg
->metadata_len
= 0;
272 reg
->state
= k_addon_state_indexed
;
273 reg
->alias
.workshop_id
= workshop_id
;
274 reg
->alias
.foldername
[0] = '\0';
275 reg
->alias
.type
= type
;
279 snprintf( foldername
, 64, PRINTF_U64
, workshop_id
);
280 addon_set_foldername( reg
, foldername
);
286 * If the addon.inf exists int the folder, load into the reg
288 static int addon_try_load_metadata( addon_reg
*reg
, vg_str folder_path
){
289 vg_str meta_path
= folder_path
;
290 vg_strcat( &meta_path
, "/addon.inf" );
291 if( !vg_strgood( &meta_path
) ){
292 vg_error( "The metadata path is too long\n" );
296 FILE *fp
= fopen( meta_path
.buffer
, "rb" );
298 vg_error( "Could not open the '%s'\n", meta_path
.buffer
);
302 reg
->metadata_len
= fread( reg
->metadata
, 1, 512, fp
);
303 if( reg
->metadata_len
!= 512 ){
306 vg_error( "unknown error codition" );
307 reg
->metadata_len
= 0;
315 static void addon_print_info( addon_reg
*reg
){
316 vg_info( "addon_reg #%u{\n", addon_system
.registry_count
);
317 vg_info( " type: %d\n", reg
->alias
.type
);
318 vg_info( " workshop_id: " PRINTF_U64
"\n", reg
->alias
.workshop_id
);
319 vg_info( " folder: [%u]%s\n", reg
->foldername_hash
, reg
->alias
.foldername
);
320 vg_info( " metadata_len: %u\n", reg
->metadata_len
);
321 vg_info( " cache_id: %hu\n", reg
->cache_id
);
325 static void addon_mount_finish( addon_reg
*reg
){
327 addon_print_info( reg
);
329 addon_system
.registry_count
++;
333 * Mount a fully packaged addon, one that certainly has a addon.inf
335 static addon_reg
*addon_mount_workshop_folder( PublishedFileId_t workshop_id
,
338 addon_reg
*reg
= addon_alloc_reg( workshop_id
, k_addon_type_none
);
339 if( !reg
) return NULL
;
341 if( !addon_try_load_metadata( reg
, folder_path
) ){
345 enum addon_type type
= k_addon_type_none
;
347 vg_msg_init( &msg
, reg
->metadata
, reg
->metadata_len
);
349 if( vg_msg_seekframe( &msg
, "workshop" )){
350 vg_msg_getkvintg( &msg
, "type", k_vg_msg_u32
, &type
);
353 if( type
== k_addon_type_none
){
354 vg_error( "Cannot determine addon type\n" );
358 reg
->alias
.type
= type
;
359 addon_mount_finish( reg
);
364 * Mount a local folder. may or may not have addon.inf
366 addon_reg
*addon_mount_local_addon( const char *folder
,
367 enum addon_type type
,
368 const char *content_ext
)
370 char folder_path_buf
[4096];
372 vg_strnull( &folder_path
, folder_path_buf
, 4096 );
373 vg_strcat( &folder_path
, folder
);
375 const char *folder_name
= vg_strch( &folder_path
, '/' )+1;
376 u32 folder_hash
= vg_strdjb2(folder_name
);
377 for( u32 i
=0; i
<addon_system
.registry_count
; i
++ ){
378 addon_reg
*reg
= &addon_system
.registry
[i
];
380 if( (reg
->alias
.type
== type
) && (reg
->foldername_hash
== folder_hash
) ){
381 if( !strcmp( reg
->alias
.foldername
, folder_name
) ){
382 reg
->state
= k_addon_state_indexed
;
388 addon_reg
*reg
= addon_alloc_reg( 0, type
);
389 if( !reg
) return NULL
;
390 addon_set_foldername( reg
, folder_name
);
391 addon_try_load_metadata( reg
, folder_path
);
393 if( reg
->metadata_len
== 0 ){
394 /* create our own content commands */
396 vg_msg_init( &msg
, reg
->metadata
, sizeof(reg
->metadata
) );
398 u32 content_count
= 0;
400 vg_strcat( &folder_path
, "" );
401 vg_warn( "Creating own metadata for: %s\n", folder_path
.buffer
);
404 if( !vg_dir_open(&subdir
, folder_path
.buffer
) ){
405 vg_error( "Failed to open '%s'\n", folder_path
.buffer
);
409 while( vg_dir_next_entry(&subdir
) ){
410 if( vg_dir_entry_type(&subdir
) == k_vg_entry_type_file
){
411 const char *fname
= vg_dir_entry_name(&subdir
);
412 vg_str file
= folder_path
;
413 vg_strcat( &file
, "/" );
414 vg_strcat( &file
, fname
);
415 if( !vg_strgood( &file
) ) continue;
417 char *ext
= vg_strch( &file
, '.' );
419 if( strcmp(ext
,content_ext
) ) continue;
421 vg_msg_wkvstr( &msg
, "content", fname
);
425 vg_dir_close(&subdir
);
427 if( !content_count
) return NULL
;
428 if( msg
.error
== k_vg_msg_error_OK
)
429 reg
->metadata_len
= msg
.cur
.co
;
431 vg_error( "Error creating metadata: %d\n", msg
.error
);
436 addon_mount_finish( reg
);
441 * Check all subscribed items
443 void addon_mount_workshop_items(void)
445 if( skaterift
.demo_mode
){
446 vg_info( "Won't load workshop items in demo mode\n" );
449 if( !steam_ready
) return;
452 * Steam workshop scan
454 vg_info( "Mounting steam workshop subscriptions\n" );
455 PublishedFileId_t workshop_ids
[ ADDON_MOUNTED_MAX
];
456 u32 workshop_count
= ADDON_MOUNTED_MAX
;
458 vg_async_item
*call
= vg_async_alloc(
459 sizeof(struct async_workshop_installed_files_info
));
460 struct async_workshop_installed_files_info
*info
= call
->payload
;
461 info
->buffer
= workshop_ids
;
462 info
->len
= &workshop_count
;
463 vg_async_dispatch( call
, async_workshop_get_installed_files
);
466 for( u32 j
=0; j
<workshop_count
; j
++ ){
467 /* check for existance in both our caches
468 * ----------------------------------------------------------*/
469 PublishedFileId_t id
= workshop_ids
[j
];
470 for( u32 i
=0; i
<addon_system
.registry_count
; i
++ ){
471 addon_reg
*reg
= &addon_system
.registry
[i
];
473 if( reg
->alias
.workshop_id
== id
){
474 reg
->state
= k_addon_state_indexed
;
475 goto next_file_workshop
;
479 vg_async_item
*call1
=
480 vg_async_alloc( sizeof(struct async_workshop_filepath_info
) );
484 struct async_workshop_filepath_info
*info
= call1
->payload
;
487 info
->len
= vg_list_size(path
);
488 vg_async_dispatch( call1
, async_workshop_get_filepath
);
489 vg_async_stall(); /* too bad! */
491 vg_str folder
= {.buffer
= path
, .i
=strlen(path
), .len
=4096};
492 addon_mount_workshop_folder( id
, folder
);
498 * Scan a local content folder for addons. It must find at least one file with
499 * the specified content_ext to be considered.
501 void addon_mount_content_folder( enum addon_type type
,
502 const char *base_folder
,
503 const char *content_ext
)
505 vg_info( "Mounting addons(type:%d) matching skaterift/%s/*/*%s\n",
506 type
, base_folder
, content_ext
);
510 vg_strnull( &path
, path_buf
, 4096 );
511 vg_strcat( &path
, base_folder
);
514 if( !vg_dir_open(&dir
,path
.buffer
) ){
515 vg_error( "vg_dir_open('%s') failed\n", path
.buffer
);
519 vg_strcat(&path
,"/");
521 while( vg_dir_next_entry(&dir
) ){
522 if( vg_dir_entry_type(&dir
) == k_vg_entry_type_dir
){
523 const char *d_name
= vg_dir_entry_name(&dir
);
525 vg_str folder
= path
;
526 if( strlen( d_name
) > ADDON_FOLDERNAME_MAX
){
527 vg_warn( "folder too long: %s\n", d_name
);
531 vg_strcat( &folder
, d_name
);
532 if( !vg_strgood( &folder
) ) continue;
534 addon_mount_local_addon( folder
.buffer
, type
, content_ext
);
541 * write the full path of the addon's folder into the vg_str
543 int addon_get_content_folder( addon_reg
*reg
, vg_str
*folder
, int async
)
545 if( reg
->alias
.workshop_id
){
546 struct async_workshop_filepath_info
*info
= NULL
;
547 vg_async_item
*call
= NULL
;
550 call
= vg_async_alloc( sizeof(struct async_workshop_filepath_info
) );
551 info
= call
->payload
;
554 info
= alloca( sizeof(struct async_workshop_filepath_info
) );
556 info
->buf
= folder
->buffer
;
557 info
->id
= reg
->alias
.workshop_id
;
558 info
->len
= folder
->len
;
561 vg_async_dispatch( call
, async_workshop_get_filepath
);
562 vg_async_stall(); /* too bad! */
565 async_workshop_get_filepath( info
, 0 );
568 if( info
->buf
[0] == '\0' ){
569 vg_error( "Failed SteamAPI_GetItemInstallInfo(" PRINTF_U64
")\n",
570 reg
->alias
.workshop_id
);
573 folder
->i
= strlen( folder
->buffer
);
579 const char *local_folder
=
580 addon_type_infos
[reg
->alias
.type
].local_content_folder
;
582 if( !local_folder
) return 0;
583 vg_strcat( folder
, local_folder
);
584 vg_strcat( folder
, reg
->alias
.foldername
);
590 * Return existing cache id if reg_index points to a registry with its cache
593 u16
addon_cache_fetch( enum addon_type type
, u32 reg_index
)
595 addon_reg
*reg
= NULL
;
597 if( reg_index
< addon_count( type
, 0 ) ){
598 reg
= get_addon_from_index( type
, reg_index
, 0 );
600 return reg
->cache_id
;
607 * Allocate a new cache item from the pool
609 u16
addon_cache_alloc( enum addon_type type
, u32 reg_index
)
611 struct addon_cache
*cache
= &addon_system
.cache
[ type
];
613 u16 new_id
= vg_pool_lru( &cache
->pool
);
614 struct addon_cache_entry
*new_entry
= vg_pool_item( &cache
->pool
, new_id
);
616 addon_reg
*reg
= NULL
;
617 if( reg_index
< addon_count( type
, 0 ) )
618 reg
= get_addon_from_index( type
, reg_index
, 0 );
621 if( new_entry
->reg_ptr
)
622 new_entry
->reg_ptr
->cache_id
= 0;
625 reg
->cache_id
= new_id
;
627 new_entry
->reg_ptr
= reg
;
628 new_entry
->reg_index
= reg_index
;
632 vg_error( "cache full (type: %u)!\n", type
);
638 * Get the real item data for cache id
640 void *addon_cache_item( enum addon_type type
, u16 id
)
642 if( !id
) return NULL
;
644 struct addon_cache
*cache
= &addon_system
.cache
[type
];
645 return cache
->items
+ ((size_t)(id
-1) * cache
->stride
);
649 * Get the real item data for cache id ONLY if the item is completely loaded.
651 void *addon_cache_item_if_loaded( enum addon_type type
, u16 id
)
653 if( !id
) return NULL
;
655 struct addon_cache
*cache
= &addon_system
.cache
[type
];
656 struct addon_cache_entry
*entry
= vg_pool_item( &cache
->pool
, id
);
658 if( entry
->state
== k_addon_cache_state_loaded
)
659 return addon_cache_item( type
, id
);
664 * Updates the item state from the main thread
666 void async_addon_setstate( void *_entry
, u32 _state
)
668 addon_cache_entry
*entry
= _entry
;
669 SDL_AtomicLock( &addon_system
.sl_cache_using_resources
);
670 entry
->state
= _state
;
671 SDL_AtomicUnlock( &addon_system
.sl_cache_using_resources
);
672 vg_success( " loaded (%s)\n", entry
->reg_ptr
->alias
.foldername
);
676 * Handles the loading of an individual item
678 static int addon_cache_load_request( enum addon_type type
, u16 id
,
679 addon_reg
*reg
, vg_str folder
){
681 /* load content files
682 * --------------------------------- */
683 vg_str content_path
= folder
;
686 vg_msg_init( &msg
, reg
->metadata
, reg
->metadata_len
);
688 const char *kv_content
= vg_msg_getkvstr( &msg
, "content" );
690 vg_strcat( &content_path
, "/" );
691 vg_strcat( &content_path
, kv_content
);
694 vg_error( " No content paths in metadata\n" );
698 if( !vg_strgood( &content_path
) ) {
699 vg_error( " Metadata path too long\n" );
703 if( type
== k_addon_type_board
){
704 struct player_board
*board
= addon_cache_item( type
, id
);
705 player_board_load( board
, content_path
.buffer
);
708 else if( type
== k_addon_type_player
){
709 struct player_model
*model
= addon_cache_item( type
, id
);
710 player_model_load( model
, content_path
.buffer
);
720 static void addon_cache_free_item( enum addon_type type
, u16 id
){
721 if( type
== k_addon_type_board
){
722 struct player_board
*board
= addon_cache_item( type
, id
);
723 player_board_unload( board
);
725 else if( type
== k_addon_type_player
){
726 struct player_model
*model
= addon_cache_item( type
, id
);
727 player_model_unload( model
);
732 * Goes over cache item load requests and calls the above ^
734 static void T1_addon_cache_load_loop(void *_
)
736 vg_info( "Running load loop\n" );
739 for( u32 type
=0; type
<k_addon_type_max
; type
++ )
741 struct addon_cache
*cache
= &addon_system
.cache
[type
];
743 for( u32 id
=1; id
<=cache
->pool
.count
; id
++ )
745 addon_cache_entry
*entry
= vg_pool_item( &cache
->pool
, id
);
747 SDL_AtomicLock( &addon_system
.sl_cache_using_resources
);
748 if( entry
->state
== k_addon_cache_state_load_request
)
750 vg_info( "process cache load request (%u#%u, reg:%u)\n",
751 type
, id
, entry
->reg_index
);
753 if( entry
->reg_index
>= addon_count(type
,0) )
755 /* should maybe have a different value for this case */
756 entry
->state
= k_addon_cache_state_none
;
757 SDL_AtomicUnlock( &addon_system
.sl_cache_using_resources
);
761 SDL_AtomicUnlock( &addon_system
.sl_cache_using_resources
);
763 /* continue with the request */
764 addon_reg
*reg
= get_addon_from_index( type
, entry
->reg_index
, 0 );
765 entry
->reg_ptr
= reg
;
768 vg_strnull( &folder
, path_buf
, 4096 );
769 if( addon_get_content_folder( reg
, &folder
, 1 ) )
771 if( addon_cache_load_request( type
, id
, reg
, folder
) )
773 vg_async_call( async_addon_setstate
,
774 entry
, k_addon_cache_state_loaded
);
779 vg_warn( "cache item did not load (%u#%u)\n", type
, id
);
780 SDL_AtomicLock( &addon_system
.sl_cache_using_resources
);
781 entry
->state
= k_addon_cache_state_none
;
782 SDL_AtomicUnlock( &addon_system
.sl_cache_using_resources
);
785 SDL_AtomicUnlock( &addon_system
.sl_cache_using_resources
);
790 void addon_system_pre_update(void)
792 if( !vg_loader_availible() ) return;
794 SDL_AtomicLock( &addon_system
.sl_cache_using_resources
);
795 for( u32 type
=0; type
<k_addon_type_max
; type
++ )
797 struct addon_cache
*cache
= &addon_system
.cache
[type
];
799 for( u32 id
=1; id
<=cache
->pool
.count
; id
++ )
801 addon_cache_entry
*entry
= vg_pool_item( &cache
->pool
, id
);
802 if( entry
->state
== k_addon_cache_state_load_request
)
804 SDL_AtomicUnlock( &addon_system
.sl_cache_using_resources
);
805 vg_loader_start( T1_addon_cache_load_loop
, NULL
);
810 SDL_AtomicUnlock( &addon_system
.sl_cache_using_resources
);
814 * Perform the cache interactions required to create a viewslot which will
815 * eventually be loaded by other parts of the system.
817 u16
addon_cache_create_viewer( enum addon_type type
, u16 reg_id
)
819 struct addon_cache
*cache
= &addon_system
.cache
[type
];
820 vg_pool
*pool
= &cache
->pool
;
822 u16 cache_id
= addon_cache_fetch( type
, reg_id
);
824 cache_id
= addon_cache_alloc( type
, reg_id
);
827 SDL_AtomicLock( &addon_system
.sl_cache_using_resources
);
828 addon_cache_entry
*entry
= vg_pool_item( pool
, cache_id
);
830 if( entry
->state
== k_addon_cache_state_loaded
){
831 addon_cache_free_item( type
, cache_id
);
834 entry
->state
= k_addon_cache_state_load_request
;
835 SDL_AtomicUnlock( &addon_system
.sl_cache_using_resources
);
840 vg_pool_watch( pool
, cache_id
);
845 u16
addon_cache_create_viewer_from_uid( enum addon_type type
,
846 char uid
[ADDON_UID_MAX
] )
849 if( !addon_uid_to_alias( uid
, &q
) ) return 0;
850 if( q
.type
!= type
) return 0;
852 u32 reg_id
= addon_match( &q
);
854 if( reg_id
== 0xffffffff ){
855 vg_warn( "We dont have the addon '%s' installed.\n", uid
);
859 return addon_cache_create_viewer( type
, reg_id
);
863 void addon_cache_watch( enum addon_type type
, u16 cache_id
)
865 if( !cache_id
) return;
867 struct addon_cache
*cache
= &addon_system
.cache
[type
];
868 vg_pool
*pool
= &cache
->pool
;
869 vg_pool_watch( pool
, cache_id
);
872 void addon_cache_unwatch( enum addon_type type
, u16 cache_id
)
874 if( !cache_id
) return;
876 struct addon_cache
*cache
= &addon_system
.cache
[type
];
877 vg_pool
*pool
= &cache
->pool
;
878 vg_pool_unwatch( pool
, cache_id
);